void ScriptsModel::reloadLocalFiles() {
    beginResetModel();

    for (int i = _treeNodes.size() - 1; i >= 0; i--) {
        TreeNodeBase* node = _treeNodes.at(i);
        if (node->getType() == TREE_NODE_TYPE_SCRIPT &&
            static_cast<TreeNodeScript*>(node)->getOrigin() == SCRIPT_ORIGIN_LOCAL)
        {
            delete node;
            _treeNodes.removeAt(i);
        }
    }

    _localDirectory.refresh();

    const QFileInfoList localFiles = _localDirectory.entryInfoList();
    for (int i = 0; i < localFiles.size(); i++) {
        QFileInfo file = localFiles[i];
        QString fileName = file.fileName();
        QUrl absPath = normalizeScriptURL(QUrl::fromLocalFile(file.absoluteFilePath()));
        _treeNodes.append(new TreeNodeScript(fileName, absPath.toString(), SCRIPT_ORIGIN_LOCAL));
    }
    rebuildTree();
    endResetModel();
}
bool ScriptsModel::parseXML(QByteArray xmlFile) {
    beginResetModel();

    QXmlStreamReader xml(xmlFile);
    QRegExp jsRegex(".*\\.js");
    bool truncated = false;
    QString lastKey;

    while (!xml.atEnd() && !xml.hasError()) {
        if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == IS_TRUNCATED_NAME) {
            while (!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == IS_TRUNCATED_NAME)) {
                xml.readNext();
                if (xml.text().toString() == "true") {
                    truncated = true;
                }
            }
        }

        if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == CONTAINER_NAME) {
            while (!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == CONTAINER_NAME)) {
                if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == KEY_NAME) {
                    xml.readNext();
                    lastKey = xml.text().toString();
                    if (jsRegex.exactMatch(xml.text().toString())) {
                        QString localPath = lastKey.split("/").mid(1).join("/");
                        QUrl fullPath = defaultScriptsLocation();
                        fullPath.setPath(fullPath.path() + lastKey);
                        const QString fullPathStr = normalizeScriptURL(fullPath).toString();
                        _treeNodes.append(new TreeNodeScript(localPath, fullPathStr, SCRIPT_ORIGIN_DEFAULT));
                    }
                }
                xml.readNext();
            }
        }
        xml.readNext();
    }
    rebuildTree();
    endResetModel();

    // Error handling
    if (xml.hasError()) {
        qCDebug(scriptengine) << "Error loading default scripts: " << xml.errorString();
        return true;
    }

    if (truncated) {
        requestDefaultFiles(lastKey);
    }

    // If this request was not truncated, we are done.
    return !truncated;
}
Exemple #3
0
void ScriptsModel::requestDefaultFiles(QString marker) {
    QUrl url(PathUtils::defaultScriptsLocation());

    // targets that don't have a scripts folder in the appropriate location will have an empty URL here
    if (!url.isEmpty()) {
        if (url.isLocalFile()) {
            // if the url indicates a local directory, use QDirIterator
            QString localDir = expandScriptUrl(url).toLocalFile();
            int localDirPartCount = localDir.split("/").size();
            if (localDir.endsWith("/")) {
                localDirPartCount--;
            }
#ifdef Q_OS_WIN
            localDirPartCount++; // one for the drive letter
#endif
            QDirIterator it(localDir, QStringList() << "*.js", QDir::Files, QDirIterator::Subdirectories);
            while (it.hasNext()) {
                QUrl jsFullPath = QUrl::fromLocalFile(it.next());
                QString jsPartialPath = jsFullPath.path().split("/").mid(localDirPartCount).join("/");
                jsFullPath = normalizeScriptURL(jsFullPath);
                _treeNodes.append(new TreeNodeScript(jsPartialPath, jsFullPath.toString(), SCRIPT_ORIGIN_DEFAULT));
            }
            _loadingScripts = false;
        } else {
            // the url indicates http(s), use QNetworkRequest
            QUrlQuery query;
            query.addQueryItem(PREFIX_PARAMETER_NAME, ".");
            if (!marker.isEmpty()) {
                query.addQueryItem(MARKER_PARAMETER_NAME, marker);
            }
            url.setQuery(query);

            QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance();
            QNetworkRequest request(url);
            request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
            request.setHeader(QNetworkRequest::UserAgentHeader, HIGH_FIDELITY_USER_AGENT);
            QNetworkReply* reply = networkAccessManager.get(request);
            connect(reply, SIGNAL(finished()), SLOT(downloadFinished()));
        }
    }
}