started on making ~ mean the application directory in script paths

This commit is contained in:
Seth Alves 2016-03-29 18:32:46 -07:00
parent 2ad02941e4
commit 7c0bb72aff
4 changed files with 46 additions and 20 deletions

View file

@ -43,24 +43,55 @@ ScriptEngines::ScriptEngines()
} }
QString normalizeScriptUrl(const QString& rawScriptUrl) { QString normalizeScriptUrl(const QString& rawScriptUrl) {
if (!rawScriptUrl.startsWith("http:") && !rawScriptUrl.startsWith("https:") && !rawScriptUrl.startsWith("atp:")) { if (!rawScriptUrl.startsWith("http:") && !rawScriptUrl.startsWith("https:") && !rawScriptUrl.startsWith("atp:")) {
#ifdef Q_OS_LINUX #ifdef Q_OS_LINUX
if (rawScriptUrl.startsWith("file:")) { if (rawScriptUrl.startsWith("file:")) {
return rawScriptUrl; return rawScriptUrl;
} }
return QUrl::fromLocalFile(rawScriptUrl).toString(); return QUrl::fromLocalFile(rawScriptUrl).toString();
#else #else
QString fullNormal;
if (rawScriptUrl.startsWith("file:")) { if (rawScriptUrl.startsWith("file:")) {
return rawScriptUrl.toLower(); fullNormal = rawScriptUrl.toLower();
} else {
// Force lowercase on file scripts because of drive letter weirdness.
fullNormal = QUrl::fromLocalFile(rawScriptUrl).toString().toLower();
} }
// Force lowercase on file scripts because of drive letter weirdness. QString defaultScriptLoc = defaultScriptsLocation();
return QUrl::fromLocalFile(rawScriptUrl).toString().toLower(); if (fullNormal.startsWith(defaultScriptLoc)) {
return "~" + fullNormal.mid(defaultScriptLoc.size());
}
return fullNormal;
#endif #endif
} }
return QUrl(rawScriptUrl).toString(); return QUrl(rawScriptUrl).toString();
} }
QString expandScriptUrl(const QString& normalizedScriptURL) {
if (normalizedScriptURL.startsWith("http:") ||
normalizedScriptURL.startsWith("https:") ||
normalizedScriptURL.startsWith("atp:")) {
return QUrl(normalizedScriptURL).toString();
}
QUrl url;
if (normalizedScriptURL.startsWith("file:")) {
url = QUrl(normalizedScriptURL);
} else {
url = QUrl::fromLocalFile(normalizedScriptURL);
}
QString path = url.path();
QStringList splitPath = path.split("/");
if (splitPath.size() > 0 && splitPath[0] == "~") {
QString defaultScriptLoc = defaultScriptsLocation();
url.setPath(defaultScriptLoc + splitPath.mid(1).join("/"));
return url.toString();
}
}
QObject* scriptsModel(); QObject* scriptsModel();
void ScriptEngines::registerScriptInitializer(ScriptInitializer initializer) { void ScriptEngines::registerScriptInitializer(ScriptInitializer initializer) {

View file

@ -96,4 +96,7 @@ protected:
ScriptsModelFilter _scriptsModelFilter; ScriptsModelFilter _scriptsModelFilter;
}; };
QString normalizeScriptUrl(const QString& rawScriptUrl);
QString expandScriptUrl(const QString& normalizedScriptURL);
#endif // hifi_ScriptEngine_h #endif // hifi_ScriptEngine_h

View file

@ -161,12 +161,8 @@ void ScriptsModel::requestDefaultFiles(QString marker) {
QString localDir = url.toLocalFile() + "/scripts"; QString localDir = url.toLocalFile() + "/scripts";
QDirIterator it(localDir, QStringList() << "*.js", QDir::Files, QDirIterator::Subdirectories); QDirIterator it(localDir, QStringList() << "*.js", QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()) { while (it.hasNext()) {
QString jsFullPath = it.next(); QString jsFullPath = normalizeScriptUrl(it.next());
QString jsPartialPath = jsFullPath.mid(localDir.length() + 1); // + 1 to skip a separator QString jsPartialPath = normalizeScriptUrl(jsFullPath.mid(localDir.length() + 1)); // + 1 to skip a separator
#if defined(Q_OS_WIN) || defined(Q_OS_OSX)
jsFullPath = jsFullPath.toLower();
jsPartialPath = jsPartialPath.toLower();
#endif
_treeNodes.append(new TreeNodeScript(jsPartialPath, jsFullPath, SCRIPT_ORIGIN_DEFAULT)); _treeNodes.append(new TreeNodeScript(jsPartialPath, jsFullPath, SCRIPT_ORIGIN_DEFAULT));
} }
} else { } else {
@ -231,7 +227,7 @@ bool ScriptsModel::parseXML(QByteArray xmlFile) {
while (!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == CONTAINER_NAME)) { while (!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == CONTAINER_NAME)) {
if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == KEY_NAME) { if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == KEY_NAME) {
xml.readNext(); xml.readNext();
lastKey = xml.text().toString(); lastKey = normalizeScriptUrl(xml.text().toString());
if (jsRegex.exactMatch(xml.text().toString())) { if (jsRegex.exactMatch(xml.text().toString())) {
_treeNodes.append(new TreeNodeScript(lastKey.mid(MODELS_LOCATION.length()), _treeNodes.append(new TreeNodeScript(lastKey.mid(MODELS_LOCATION.length()),
defaultScriptsLocation() + "/" + lastKey, defaultScriptsLocation() + "/" + lastKey,
@ -278,12 +274,8 @@ void ScriptsModel::reloadLocalFiles() {
const QFileInfoList localFiles = _localDirectory.entryInfoList(); const QFileInfoList localFiles = _localDirectory.entryInfoList();
for (int i = 0; i < localFiles.size(); i++) { for (int i = 0; i < localFiles.size(); i++) {
QFileInfo file = localFiles[i]; QFileInfo file = localFiles[i];
QString fileName = file.fileName(); QString fileName = normalizeScriptUrl(file.fileName());
QString absPath = file.absoluteFilePath(); QString absPath = normalizeScriptUrl(file.absoluteFilePath());
#if defined(Q_OS_WIN) || defined(Q_OS_OSX)
fileName = fileName.toLower();
absPath = absPath.toLower();
#endif
_treeNodes.append(new TreeNodeScript(fileName, absPath, SCRIPT_ORIGIN_LOCAL)); _treeNodes.append(new TreeNodeScript(fileName, absPath, SCRIPT_ORIGIN_LOCAL));
} }
rebuildTree(); rebuildTree();
@ -310,7 +302,7 @@ void ScriptsModel::rebuildTree() {
for (pathIterator = pathList.constBegin(); pathIterator != pathList.constEnd(); ++pathIterator) { for (pathIterator = pathList.constBegin(); pathIterator != pathList.constEnd(); ++pathIterator) {
hash.append(*pathIterator + "/"); hash.append(*pathIterator + "/");
if (!folders.contains(hash)) { if (!folders.contains(hash)) {
folders[hash] = new TreeNodeFolder(*pathIterator, parent); folders[hash] = new TreeNodeFolder(normalizeScriptUrl(*pathIterator), parent);
} }
parent = folders[hash]; parent = folders[hash];
} }

View file

@ -56,9 +56,9 @@ QString findMostRecentFileExtension(const QString& originalFileName, QVector<QSt
QString defaultScriptsLocation() { QString defaultScriptsLocation() {
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
return "file:///" + QCoreApplication::applicationDirPath(); return QString("file:///" + QCoreApplication::applicationDirPath()).toLower();
#elif defined(Q_OS_OSX) #elif defined(Q_OS_OSX)
return "file:///" + QCoreApplication::applicationDirPath() + "/../.."; return QString("file:///" + QCoreApplication::applicationDirPath() + "/../..").toLower();
#else #else
return "http://s3.amazonaws.com/hifi-public"; return "http://s3.amazonaws.com/hifi-public";
#endif #endif