bardescriptordocument.cpp 18.2 KB
Newer Older
1 2
/**************************************************************************
**
3
** Copyright (C) 2014 BlackBerry Limited. All rights reserved.
4
**
5
** Contact: BlackBerry (qt@blackberry.com)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
** Contact: KDAB (info@kdab.com)
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "bardescriptordocument.h"

#include "qnxconstants.h"

36
#include <coreplugin/editormanager/editormanager.h>
37 38
#include <utils/qtcassert.h>

39
#include <QDir>
40 41
#include <QFileInfo>
#include <QMetaEnum>
42
#include <QTextCodec>
43 44 45 46

using namespace Qnx;
using namespace Qnx::Internal;

47 48
BarDescriptorDocument::BarDescriptorDocument(QObject *parent)
    : Core::TextDocument(parent)
49
{
50 51 52 53 54
    // blackberry-nativepackager requires the XML file to be in UTF-8 encoding,
    // force if possible
    if (QTextCodec *defaultUTF8 = QTextCodec::codecForName("UTF-8"))
        setCodec(defaultUTF8);
    else
55
        setCodec(Core::EditorManager::defaultTextCodec());
56 57 58 59 60 61 62 63 64 65 66
}

BarDescriptorDocument::~BarDescriptorDocument()
{
}

bool BarDescriptorDocument::open(QString *errorString, const QString &fileName) {
    QString contents;
    if (read(fileName, &contents, errorString) != Utils::TextFileFormat::ReadSuccess)
        return false;

67
    setFilePath(fileName);
68

69
    const bool result = loadContent(contents, false);
70 71 72 73 74 75 76

    if (!result)
        *errorString = tr("%1 does not appear to be a valid application descriptor file").arg(QDir::toNativeSeparators(fileName));

    return result;
}

77
bool BarDescriptorDocument::save(QString *errorString, const QString &fn, bool autoSave)
78 79
{
    QTC_ASSERT(!autoSave, return false);
80
    QTC_ASSERT(fn.isEmpty(), return false);
81

82
    const bool result = write(filePath(), xmlSource(), errorString);
83 84 85
    if (!result)
        return false;

86 87
    m_dirty = false;
    emit Core::IDocument::changed();
88 89 90 91 92
    return true;
}

QString BarDescriptorDocument::defaultPath() const
{
93
    QFileInfo fi(filePath());
94 95 96 97 98
    return fi.absolutePath();
}

QString BarDescriptorDocument::suggestedFileName() const
{
99
    QFileInfo fi(filePath());
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    return fi.fileName();
}

QString BarDescriptorDocument::mimeType() const
{
    return QLatin1String(Constants::QNX_BAR_DESCRIPTOR_MIME_TYPE);
}

bool BarDescriptorDocument::shouldAutoSave() const
{
    return false;
}

bool BarDescriptorDocument::isModified() const
{
115
    return m_dirty;
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
}

bool BarDescriptorDocument::isSaveAsAllowed() const
{
    return false;
}

Core::IDocument::ReloadBehavior BarDescriptorDocument::reloadBehavior(Core::IDocument::ChangeTrigger state, Core::IDocument::ChangeType type) const
{
    if (type == TypeRemoved || type == TypePermissions)
        return BehaviorSilent;
    if (type == TypeContents && state == TriggerInternal && !isModified())
        return BehaviorSilent;
    return BehaviorAsk;
}

bool BarDescriptorDocument::reload(QString *errorString, Core::IDocument::ReloadFlag flag, Core::IDocument::ChangeType type)
{
    Q_UNUSED(type);

    if (flag == Core::IDocument::FlagIgnore)
        return true;

139
    return open(errorString, filePath());
140 141 142 143
}

QString BarDescriptorDocument::xmlSource() const
{
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    const int indent = 4;
    return m_barDocument.toString(indent);
}

bool BarDescriptorDocument::loadContent(const QString &xmlCode, bool setDirty, QString *errorMessage, int *errorLine)
{
    if (xmlCode == xmlSource())
        return true;

    bool result = m_barDocument.setContent(xmlCode, errorMessage, errorLine);

    m_dirty = setDirty;

    emitAllChanged();
    emit Core::IDocument::changed();
    return result;
}

QVariant BarDescriptorDocument::value(BarDescriptorDocument::Tag tag) const
{
    const QString tagName = QString::fromLatin1(metaObject()->enumerator(metaObject()->enumeratorOffset()).valueToKey(tag));

    switch (tag) {
    case id:
    case versionNumber:
    case buildId:
    case name:
    case description:
    case author:
    case publisher:
    case authorId:
        return stringValue(tagName);
    case icon:
        return childStringListValue(tagName, QLatin1String("image")).value(0);
    case splashScreens:
        return childStringListValue(tagName, QLatin1String("image"));
    case asset: {
        QVariant var;
        var.setValue(assets());
        return var;
    }
    case aspectRatio:
    case autoOrients:
    case systemChrome:
        return childStringListValue(QLatin1String("initialWindow"), tagName).value(0);
    case transparent:
        return childStringListValue(QLatin1String("initialWindow"), tagName).value(0) == QLatin1String("true");
    case arg:
    case action:
        return stringListValue(tagName);
    case env:
        QVariant var;
        var.setValue(environment());
        return var;
    }

    return QVariant();
}

void BarDescriptorDocument::setValue(BarDescriptorDocument::Tag tag, const QVariant &value)
{
    const QMetaEnum tagEnum = metaObject()->enumerator(metaObject()->enumeratorOffset());
    const QString tagName = QString::fromLatin1(tagEnum.valueToKey(tag));

    switch (tag) {
    case id:
    case versionNumber:
    case buildId:
    case name:
    case description:
    case authorId:
        setStringValue(tagName, value.toString());
        break;
    case icon:
    case splashScreens:
        setChildStringListValue(tagName, QLatin1String("image"), value.toStringList());
        break;
    case asset:
        setAssets(value.value<BarDescriptorAssetList>());
        break;
    case aspectRatio:
    case autoOrients:
    case systemChrome:
        setChildStringListValue(QLatin1String("initialWindow"), tagName, value.toStringList());
        break;
    case transparent:
        setChildStringListValue(QLatin1String("initialWindow"), tagName, QStringList() << (value.toBool() ? QLatin1String("true") : QLatin1String("false")));
        break;
    case arg:
    case action:
        setStringListValue(tagName, value.toStringList());
        break;
    case env:
        setEnvironment(value.value<QList<Utils::EnvironmentItem> >());
        break;
    case author:
    case publisher:
        // Unset <publisher> when setting <author> as only one should be used
        setStringValue(QString::fromLatin1(tagEnum.valueToKey(author)), value.toString());
        setStringValue(QString::fromLatin1(tagEnum.valueToKey(publisher)), QLatin1String(""));
        break;
    }

    m_dirty = true;
    emit changed(tag, value);
    emit Core::IDocument::changed();
}

QString BarDescriptorDocument::stringValue(const QString &tagName) const
{
    QDomNodeList nodes = m_barDocument.elementsByTagName(tagName);
    if (nodes.isEmpty() || nodes.size() > 1)
        return QString();

    QDomNode node = nodes.item(0);
    QDomText textNode = node.firstChild().toText();
    if (textNode.isNull())
        return QString();

    return textNode.data();
}

void BarDescriptorDocument::setStringValue(const QString &tagName, const QString &value)
{
    QDomNodeList nodes = m_barDocument.elementsByTagName(tagName);

    if (nodes.size() > 1)
        return;
272

273 274 275 276 277 278 279 280 281 282
    QDomNode existingNode = nodes.item(0);
    if (existingNode.isNull() && value.isEmpty())
        return;

    if (!existingNode.isNull() && value.isEmpty()) {
        m_barDocument.documentElement().removeChild(existingNode);
    } else if (existingNode.isNull()) {
        QDomElement newNode = m_barDocument.createElement(tagName);
        newNode.appendChild(m_barDocument.createTextNode(value));
        m_barDocument.documentElement().appendChild(newNode);
283
    } else {
284 285 286 287 288 289
        QDomText textNode = existingNode.firstChild().toText();
        if (textNode.isNull())
            return;
        textNode.setData(value);
    }
}
290

291 292 293 294 295
QStringList BarDescriptorDocument::childStringListValue(const QString &tagName, const QString &childTagName) const
{
    QDomNodeList nodes = m_barDocument.elementsByTagName(tagName);
    if (nodes.isEmpty() || nodes.size() > 1)
        return QStringList();
296

297 298 299 300
    QDomNode parentNode = nodes.item(0);
    QDomElement childElm = parentNode.firstChildElement(childTagName);
    if (childElm.isNull())
        return QStringList();
301

302 303 304 305 306
    QStringList result;
    while (!childElm.isNull()) {
        QDomText textNode = childElm.firstChild().toText();
        if (textNode.isNull())
            return QStringList();
307

308
        result.append(textNode.data());
309

310
        childElm = childElm.nextSiblingElement(childTagName);
311
    }
312 313

    return result;
314 315
}

316
void BarDescriptorDocument::setChildStringListValue(const QString &tagName, const QString &childTagName, const QStringList &stringList)
317
{
318
    QDomNodeList nodes = m_barDocument.elementsByTagName(tagName);
319

320 321
    if (nodes.size() > 1)
        return;
322

323
    QDomNode existingNode = nodes.item(0);
324

325 326
    if (existingNode.isNull()) {
        QDomElement newParentNode = m_barDocument.createElement(tagName);
327

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
        foreach (const QString &value, stringList) {
            QDomElement newChildNode = m_barDocument.createElement(childTagName);
            QDomText newTextNode = m_barDocument.createTextNode(value);
            newChildNode.appendChild(newTextNode);
            newParentNode.appendChild(newChildNode);
        }
        m_barDocument.documentElement().appendChild(newParentNode);
    } else {
        QStringList values = stringList;
        QDomElement childElm = existingNode.firstChildElement(childTagName);
        if (!childElm.isNull()) {
            // Loop through existing elements, remove the existing nodes
            // that no longer are in "values", and remove from "values"
            // the existing nodes that don't need re-creation
            while (!childElm.isNull()) {
                QDomText textNode = childElm.firstChild().toText();
                if (textNode.isNull())
                    continue;

                QDomElement toRemove;
                if (!values.contains(textNode.data()))
                    toRemove = childElm;
                else
                    values.removeAll(textNode.data());

                childElm = childElm.nextSiblingElement(childTagName);

                if (!toRemove.isNull())
                    existingNode.removeChild(toRemove);
            }
358 359
        }

360 361 362 363 364 365 366 367 368 369
        // Add the new elements
        int newElementCount = 0;
        foreach (const QString &value, values) {
            if (value.isEmpty())
                continue;
            QDomElement newChildNode = m_barDocument.createElement(childTagName);
            newChildNode.appendChild(m_barDocument.createTextNode(value));
            existingNode.appendChild(newChildNode);
            ++newElementCount;
        }
370

371 372
        if (newElementCount == 0)
            m_barDocument.documentElement().removeChild(existingNode);
373
    }
374
}
375

376 377 378
QStringList BarDescriptorDocument::stringListValue(const QString &tagName) const
{
    QStringList result;
379

380 381 382 383 384 385 386 387 388 389 390 391
    QDomElement childElm = m_barDocument.documentElement().firstChildElement(tagName);
    while (!childElm.isNull()) {
        QDomText textNode = childElm.firstChild().toText();
        if (textNode.isNull())
            continue;

        result.append(textNode.data());

        childElm = childElm.nextSiblingElement(tagName);
    }

    return result;
392 393
}

394
void BarDescriptorDocument::setStringListValue(const QString &tagName, const QStringList &stringList)
395
{
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
    QStringList values = stringList;
    QDomElement childElm = m_barDocument.documentElement().firstChildElement(tagName);
    if (!childElm.isNull()) {
        // Loop through existing elements, remove the existing nodes
        // that no longer are in "values", and remove from "values"
        // the existing nodes that don't need re-creation
        while (!childElm.isNull()) {
            QDomText textNode = childElm.firstChild().toText();
            if (textNode.isNull())
                continue;

            QDomElement toRemove;
            if (!values.contains(textNode.data()))
                toRemove = childElm;
            else
                values.removeAll(textNode.data());

            childElm = childElm.nextSiblingElement(tagName);

            if (!toRemove.isNull())
                m_barDocument.documentElement().removeChild(toRemove);
        }
    }

    // Add the new elements
    foreach (const QString &value, values) {
        if (value.isEmpty())
            continue;
        QDomElement newChildNode = m_barDocument.createElement(tagName);
        newChildNode.appendChild(m_barDocument.createTextNode(value));
        m_barDocument.documentElement().appendChild(newChildNode);
    }
428 429
}

430
BarDescriptorAssetList BarDescriptorDocument::assets() const
431
{
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
    BarDescriptorAssetList result;
    QDomNodeList nodes = m_barDocument.elementsByTagName(QLatin1String("asset"));
    if (nodes.isEmpty())
        return result;

    for (int i = 0; i < nodes.size(); ++i) {
        QDomElement assetElm = nodes.item(i).toElement();
        if (assetElm.isNull())
            continue;

        QDomText textNode = assetElm.firstChild().toText();
        if (textNode.isNull())
            continue;

        QString path = assetElm.attribute(QLatin1String("path"));
        QString entry = assetElm.attribute(QLatin1String("entry"));
        QString dest = textNode.data();

        BarDescriptorAsset asset;
        asset.source = path;
        asset.destination = dest;
        asset.entry = entry == QLatin1String("true");
        result.append(asset);
455 456
    }

457
    return result;
458 459
}

460
void BarDescriptorDocument::setAssets(const BarDescriptorAssetList &assets)
461
{
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
    QDomNodeList nodes = m_barDocument.elementsByTagName(QLatin1String("asset"));

    BarDescriptorAssetList newAssets = assets;
    QList<QDomNode> toRemove;

    for (int i = 0; i < nodes.size(); ++i) {
        QDomElement assetElm = nodes.at(i).toElement();
        if (assetElm.isNull())
            continue;

        QDomText textNode = assetElm.firstChild().toText();
        if (textNode.isNull())
            continue;

        QString source = assetElm.attribute(QLatin1String("path"));
        bool found = false;
        foreach (const BarDescriptorAsset &asset, newAssets) {
            if (asset.source == source) {
                found = true;
                if (asset.entry) {
                    assetElm.setAttribute(QLatin1String("type"), QLatin1String("Qnx/Elf"));
                    assetElm.setAttribute(QLatin1String("entry"), QLatin1String("true"));
                } else {
                    assetElm.removeAttribute(QLatin1String("type"));
                    assetElm.removeAttribute(QLatin1String("entry"));
                }
                textNode.setData(asset.destination);

                newAssets.removeAll(asset);
                break;
            }
        }

        if (!found)
            toRemove.append(assetElm);
    }

    foreach (const QDomNode &node, toRemove)
        m_barDocument.documentElement().removeChild(node);

    foreach (const BarDescriptorAsset &asset, newAssets) {
        QDomElement assetElm = m_barDocument.createElement(QLatin1String("asset"));
        assetElm.setAttribute(QLatin1String("path"), asset.source);
        if (asset.entry) {
            assetElm.setAttribute(QLatin1String("type"), QLatin1String("Qnx/Elf"));
            assetElm.setAttribute(QLatin1String("entry"), QLatin1String("true"));
508
        }
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
        assetElm.appendChild(m_barDocument.createTextNode(asset.destination));
        m_barDocument.documentElement().appendChild(assetElm);
    }
}

QList<Utils::EnvironmentItem> BarDescriptorDocument::environment() const
{
    QList<Utils::EnvironmentItem> result;

    QDomElement envElm = m_barDocument.documentElement().firstChildElement(QLatin1String("env"));
    while (!envElm.isNull()) {
        QString var = envElm.attribute(QLatin1String("var"));
        QString value = envElm.attribute(QLatin1String("value"));

        Utils::EnvironmentItem item(var, value);
        result.append(item);

        envElm = envElm.nextSiblingElement(QLatin1String("env"));
    }
    return result;
}

void BarDescriptorDocument::setEnvironment(const QList<Utils::EnvironmentItem> &environment)
{
    QDomNodeList envNodes = m_barDocument.elementsByTagName(QLatin1String("env"));

    QList<Utils::EnvironmentItem> newEnvironment = environment;
    QList<QDomElement> toRemove;
    for (int i = 0; i < envNodes.size(); ++i) {
        QDomElement elm = envNodes.at(i).toElement();
        if (elm.isNull())
            continue;

        QString var = elm.attribute(QLatin1String("var"));
        bool found = false;
        foreach (const Utils::EnvironmentItem item, newEnvironment) {
            if (item.name == var) {
                found = true;
                elm.setAttribute(QLatin1String("value"), item.value);
                newEnvironment.removeAll(item);
                break;
            }
        }

        if (!found)
            toRemove.append(elm);
    }

    foreach (const QDomNode &node, toRemove)
        m_barDocument.documentElement().removeChild(node);

    foreach (const Utils::EnvironmentItem item, newEnvironment) {
        QDomElement elm = m_barDocument.createElement(QLatin1String("env"));
        elm.setAttribute(QLatin1String("var"), item.name);
        elm.setAttribute(QLatin1String("value"), item.value);
        m_barDocument.documentElement().appendChild(elm);
    }
}

void BarDescriptorDocument::emitAllChanged()
{
    QMetaEnum tags = metaObject()->enumerator(metaObject()->enumeratorOffset());
    for (int i = 0; i < tags.keyCount(); ++i) {
        Tag tag = static_cast<Tag>(tags.value(i));
        emit changed(tag, value(tag));
574 575
    }
}