History

rewrite a bunch of path/url mangling code

Seth Alves committed 10 years ago

Commit 26e01bb1 parent bd5757d6 files · +104 −79Browse files

Showing the changes to libraries/script-engine/src/ScriptsModel.cpp (1 of 6 files). Show all

Unified · Split

libraries/script-engine/src/ScriptsModel.cpp+21−12
@@ -158,14 +158,21 @@ void ScriptsModel::requestDefaultFiles(QString marker) { · 157 unmodified lines
158158 QUrl url(defaultScriptsLocation());
159159
160160 if (url.isLocalFile()) {
161− QString localDir = url.toLocalFile() + "/scripts";
161+ // if the url indicates a local directory, use QDirIterator
162+ // QString localDir = url.toLocalFile() + "/scripts";
163+ QString localDir = expandScriptUrl(url).toLocalFile() + "/scripts";
164+ int localDirPartCount = localDir.split("/").size();
162165 QDirIterator it(localDir, QStringList() << "*.js", QDir::Files, QDirIterator::Subdirectories);
163166 while (it.hasNext()) {
164− QString jsFullPath = normalizeScriptUrl(it.next());
165− QString jsPartialPath = normalizeScriptUrl(jsFullPath.mid(localDir.length() + 1)); // + 1 to skip a separator
166− _treeNodes.append(new TreeNodeScript(jsPartialPath, jsFullPath, SCRIPT_ORIGIN_DEFAULT));
167+ QUrl jsFullPath = QUrl::fromLocalFile(it.next());
168+ // QString jsPartialPath = jsFullPath.path().mid(localDir.length() + 1); // + 1 to skip a separator
169+ QString jsPartialPath = jsFullPath.path().split("/").mid(localDirPartCount).join("/");
170+ jsFullPath = normalizeScriptURL(jsFullPath);
171+ _treeNodes.append(new TreeNodeScript(jsPartialPath, jsFullPath.toString(), SCRIPT_ORIGIN_DEFAULT));
167172 }
173+ _loadingScripts = false;
168174 } else {
175+ // the url indicates http(s), use QNetworkRequest
169176 QUrlQuery query;
170177 query.addQueryItem(PREFIX_PARAMETER_NAME, MODELS_LOCATION);
171178 if (!marker.isEmpty()) {
@@ -227,11 +234,13 @@ bool ScriptsModel::parseXML(QByteArray xmlFile) { · 55 unmodified lines
227234 while (!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == CONTAINER_NAME)) {
228235 if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == KEY_NAME) {
229236 xml.readNext();
230− lastKey = normalizeScriptUrl(xml.text().toString());
237+ lastKey = xml.text().toString();
231238 if (jsRegex.exactMatch(xml.text().toString())) {
232− _treeNodes.append(new TreeNodeScript(lastKey.mid(MODELS_LOCATION.length()),
233− defaultScriptsLocation() + "/" + lastKey,
234− SCRIPT_ORIGIN_DEFAULT));
239+ QString localPath = lastKey.split("/").mid(1).join("/");
240+ QUrl fullPath = defaultScriptsLocation();
241+ fullPath.setPath(fullPath.path() + "/" + lastKey);
242+ const QString fullPathStr = normalizeScriptURL(fullPath).toString();
243+ _treeNodes.append(new TreeNodeScript(localPath, fullPathStr, SCRIPT_ORIGIN_DEFAULT));
235244 }
236245 }
237246 xml.readNext();
@@ -274,9 +283,9 @@ void ScriptsModel::reloadLocalFiles() { · 36 unmodified lines
274283 const QFileInfoList localFiles = _localDirectory.entryInfoList();
275284 for (int i = 0; i < localFiles.size(); i++) {
276285 QFileInfo file = localFiles[i];
277− QString fileName = normalizeScriptUrl(file.fileName());
278− QString absPath = normalizeScriptUrl(file.absoluteFilePath());
279− _treeNodes.append(new TreeNodeScript(fileName, absPath, SCRIPT_ORIGIN_LOCAL));
286+ QString fileName = file.fileName();
287+ QUrl absPath = normalizeScriptURL(QUrl::fromLocalFile(file.absoluteFilePath()));
288+ _treeNodes.append(new TreeNodeScript(fileName, absPath.toString(), SCRIPT_ORIGIN_LOCAL));
280289 }
281290 rebuildTree();
282291 endResetModel();
@@ -302,7 +311,7 @@ void ScriptsModel::rebuildTree() { · 19 unmodified lines
302311 for (pathIterator = pathList.constBegin(); pathIterator != pathList.constEnd(); ++pathIterator) {
303312 hash.append(*pathIterator + "/");
304313 if (!folders.contains(hash)) {
305− folders[hash] = new TreeNodeFolder(normalizeScriptUrl(*pathIterator), parent);
314+ folders[hash] = new TreeNodeFolder(*pathIterator, parent);
306315 }
307316 parent = folders[hash];
308317 }