History

trying again on script loading changes

Seth Alves committed 10 years ago

Commit 35affe21 parent 0f288244 files · +67 −36Browse files

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

Unified · Split

libraries/script-engine/src/ScriptEngines.cpp+51−27
@@ -44,16 +44,21 @@ ScriptEngines::ScriptEngines() · 43 unmodified lines
44 44
45QUrl normalizeScriptURL(const QUrl& rawScriptURL) {45QUrl normalizeScriptURL(const QUrl& rawScriptURL) {
46 if (rawScriptURL.scheme() == "file") {46 if (rawScriptURL.scheme() == "file") {
47 qDebug() << "+";
48 qDebug() << "+";
49 qDebug() << "+";
50
47 QUrl fullNormal = rawScriptURL;51 QUrl fullNormal = rawScriptURL;
48 QUrl defaultScriptLoc = defaultScriptsLocation();52 QUrl defaultScriptLoc = defaultScriptsLocation();
49 53
50 #ifdef Q_OS_LINUX54 // #ifdef Q_OS_LINUX
51 #else55 // #else
52 // Force lowercase on file scripts because of drive letter weirdness.56 // // Force lowercase on file scripts because of drive letter weirdness.
53 if (rawScriptURL.isLocalFile()) {57 // if (rawScriptURL.isLocalFile()) {
54 fullNormal.setPath(fullNormal.path().toLower());58 // fullNormal.setPath(fullNormal.path()); // .toLower());
55 }59 // }
56 #endif60 // #endif
61
57 // if this url is something "beneath" the default script url, replace the local path with ~62 // if this url is something "beneath" the default script url, replace the local path with ~
58 if (fullNormal.scheme() == defaultScriptLoc.scheme() &&63 if (fullNormal.scheme() == defaultScriptLoc.scheme() &&
59 fullNormal.host() == defaultScriptLoc.host() &&64 fullNormal.host() == defaultScriptLoc.host() &&
@@ -69,21 +74,37 @@ QUrl normalizeScriptURL(const QUrl& rawScriptURL) { · 9 unmodified lines
69 }74 }
70}75}
71 76
72QUrl expandScriptUrl(const QUrl& normalizedScriptURL) {77QUrl expandScriptUrl(const QUrl& rawScriptURL) {
78 QUrl normalizedScriptURL = normalizeScriptURL(rawScriptURL);
79
80 qDebug() << "expandScriptUrl in = " << rawScriptURL;
81
73 if (normalizedScriptURL.scheme() == "http" ||82 if (normalizedScriptURL.scheme() == "http" ||
74 normalizedScriptURL.scheme() == "https" ||83 normalizedScriptURL.scheme() == "https" ||
75 normalizedScriptURL.scheme() == "atp") {84 normalizedScriptURL.scheme() == "atp") {
85
86 qDebug() << "expandScriptUrl out(net) = " << normalizedScriptURL;
87
76 return normalizedScriptURL;88 return normalizedScriptURL;
77 } else if (normalizedScriptURL.scheme() == "file") {89 } else if (normalizedScriptURL.scheme() == "file") {
78 if (normalizedScriptURL.path().startsWith("/~/")) {90 if (normalizedScriptURL.path().startsWith("/~/")) {
79 QUrl url = normalizedScriptURL;91 QUrl url = normalizedScriptURL;
92 qDebug() << "in expand, normalizedScriptURL = " << normalizedScriptURL;
80 QStringList splitPath = url.path().split("/");93 QStringList splitPath = url.path().split("/");
81 QUrl defaultScriptsLoc = defaultScriptsLocation();94 QUrl defaultScriptsLoc = defaultScriptsLocation();
82 url.setPath(defaultScriptsLoc.path() + "/" + splitPath.mid(2).join("/")); // 2 to skip the slashes in /~/95 url.setPath(defaultScriptsLoc.path() + "/" + splitPath.mid(2).join("/")); // 2 to skip the slashes in /~/
96
97 qDebug() << "expandScriptUrl out(file 0) = " << url;
98
83 return url;99 return url;
84 }100 }
101
102 qDebug() << "expandScriptUrl out(file 1) = " << normalizedScriptURL;
103
85 return normalizedScriptURL;104 return normalizedScriptURL;
86 } else {105 } else {
106 qDebug() << "expandScriptUrl out(bad scheme) = " << normalizedScriptURL;
107
87 return QUrl("");108 return QUrl("");
88 }109 }
89}110}
@@ -230,7 +251,7 @@ QVariantList ScriptEngines::getRunning() { · 140 unmodified lines
230 }251 }
231 QVariantMap resultNode;252 QVariantMap resultNode;
232 resultNode.insert("name", runningScriptURL.fileName());253 resultNode.insert("name", runningScriptURL.fileName());
233 QUrl displayURL = expandScriptUrl(QUrl(runningScriptURL));254 QUrl displayURL = expandScriptUrl(runningScriptURL);
234 QString displayURLString;255 QString displayURLString;
235 if (displayURL.isLocalFile()) {256 if (displayURL.isLocalFile()) {
236 displayURLString = displayURL.toLocalFile();257 displayURLString = displayURL.toLocalFile();
@@ -268,7 +289,7 @@ void ScriptEngines::loadScripts() { · 31 unmodified lines
268 loadDefaultScripts();289 loadDefaultScripts();
269 _firstRun.set(false);290 _firstRun.set(false);
270 return;291 return;
271 } 292 }
272 293
273 // loads all saved scripts294 // loads all saved scripts
274 Settings settings;295 Settings settings;
@@ -310,7 +331,12 @@ void ScriptEngines::saveScripts() { · 35 unmodified lines
310 331
311QStringList ScriptEngines::getRunningScripts() {332QStringList ScriptEngines::getRunningScripts() {
312 QReadLocker lock(&_scriptEnginesHashLock);333 QReadLocker lock(&_scriptEnginesHashLock);
313 return _scriptEnginesHash.keys();334 QList<QUrl> urls = _scriptEnginesHash.keys();
335 QStringList result;
336 for (auto url : urls) {
337 result.append(url.toString());
338 }
339 return result;
314}340}
315 341
316void ScriptEngines::stopAllScripts(bool restart) {342void ScriptEngines::stopAllScripts(bool restart) {
@@ -318,7 +344,7 @@ void ScriptEngines::stopAllScripts(bool restart) { · 1 unmodified line
318 if (restart) {344 if (restart) {
319 // Delete all running scripts from cache so that they are re-downloaded when they are restarted345 // Delete all running scripts from cache so that they are re-downloaded when they are restarted
320 auto scriptCache = DependencyManager::get<ScriptCache>();346 auto scriptCache = DependencyManager::get<ScriptCache>();
321 for (QHash<QString, ScriptEngine*>::const_iterator it = _scriptEnginesHash.constBegin();347 for (QHash<QUrl, ScriptEngine*>::const_iterator it = _scriptEnginesHash.constBegin();
322 it != _scriptEnginesHash.constEnd(); it++) {348 it != _scriptEnginesHash.constEnd(); it++) {
323 if (!it.value()->isFinished()) {349 if (!it.value()->isFinished()) {
324 scriptCache->deleteScript(it.key());350 scriptCache->deleteScript(it.key());
@@ -327,7 +353,7 @@ void ScriptEngines::stopAllScripts(bool restart) { · 2 unmodified lines
327 }353 }
328 354
329 // Stop and possibly restart all currently running scripts355 // Stop and possibly restart all currently running scripts
330 for (QHash<QString, ScriptEngine*>::const_iterator it = _scriptEnginesHash.constBegin();356 for (QHash<QUrl, ScriptEngine*>::const_iterator it = _scriptEnginesHash.constBegin();
331 it != _scriptEnginesHash.constEnd(); it++) {357 it != _scriptEnginesHash.constEnd(); it++) {
332 if (it.value()->isFinished()) {358 if (it.value()->isFinished()) {
333 continue;359 continue;
@@ -350,21 +376,20 @@ bool ScriptEngines::stopScript(const QString& rawScriptURL, bool restart) { · 16 unmodified lines
350 if (!scriptURL.isValid()) {376 if (!scriptURL.isValid()) {
351 scriptURL = normalizeScriptURL(QUrl::fromLocalFile(rawScriptURL));377 scriptURL = normalizeScriptURL(QUrl::fromLocalFile(rawScriptURL));
352 }378 }
353 const QString scriptURLString = scriptURL.toString();
354 379
355 QReadLocker lock(&_scriptEnginesHashLock);380 QReadLocker lock(&_scriptEnginesHashLock);
356 if (_scriptEnginesHash.contains(scriptURLString)) {381 if (_scriptEnginesHash.contains(scriptURL)) {
357 ScriptEngine* scriptEngine = _scriptEnginesHash[scriptURLString];382 ScriptEngine* scriptEngine = _scriptEnginesHash[scriptURL];
358 if (restart) {383 if (restart) {
359 auto scriptCache = DependencyManager::get<ScriptCache>();384 auto scriptCache = DependencyManager::get<ScriptCache>();
360 scriptCache->deleteScript(QUrl(scriptURLString));385 scriptCache->deleteScript(scriptURL);
361 connect(scriptEngine, &ScriptEngine::finished, this, [this](QString scriptName, ScriptEngine* engine) {386 connect(scriptEngine, &ScriptEngine::finished, this, [this](QString scriptName, ScriptEngine* engine) {
362 reloadScript(scriptName);387 reloadScript(scriptName);
363 });388 });
364 }389 }
365 scriptEngine->stop();390 scriptEngine->stop();
366 stoppedScript = true;391 stoppedScript = true;
367 qCDebug(scriptengine) << "stopping script..." << scriptURLString;392 qCDebug(scriptengine) << "stopping script..." << scriptURL;
368 }393 }
369 }394 }
370 return stoppedScript;395 return stoppedScript;
@@ -379,7 +404,7 @@ void ScriptEngines::setScriptsLocation(const QString& scriptsLocation) { · 8 unmodified lines
379 _scriptsModel.updateScriptsLocation(scriptsLocation);404 _scriptsModel.updateScriptsLocation(scriptsLocation);
380}405}
381 406
382void ScriptEngines::reloadAllScripts() { 407void ScriptEngines::reloadAllScripts() {
383 DependencyManager::get<ScriptCache>()->clearCache();408 DependencyManager::get<ScriptCache>()->clearCache();
384 emit scriptsReloading();409 emit scriptsReloading();
385 stopAllScripts(true);410 stopAllScripts(true);
@@ -409,7 +434,7 @@ ScriptEngine* ScriptEngines::loadScript(const QUrl& scriptFilename, bool isUserL · 23 unmodified lines
409 scriptUrl = normalizeScriptURL(scriptFilename);434 scriptUrl = normalizeScriptURL(scriptFilename);
410 }435 }
411 436
412 auto scriptEngine = getScriptEngine(scriptUrl.toString());437 auto scriptEngine = getScriptEngine(scriptUrl);
413 if (scriptEngine) {438 if (scriptEngine) {
414 return scriptEngine;439 return scriptEngine;
415 }440 }
@@ -435,12 +460,12 @@ ScriptEngine* ScriptEngines::loadScript(const QUrl& scriptFilename, bool isUserL · 19 unmodified lines
435 return scriptEngine;460 return scriptEngine;
436}461}
437 462
438ScriptEngine* ScriptEngines::getScriptEngine(const QString& rawScriptURL) {463ScriptEngine* ScriptEngines::getScriptEngine(const QUrl& rawScriptURL) {
439 ScriptEngine* result = nullptr;464 ScriptEngine* result = nullptr;
440 {465 {
441 QReadLocker lock(&_scriptEnginesHashLock);466 QReadLocker lock(&_scriptEnginesHashLock);
442 const QString scriptURLString = normalizeScriptURL(QUrl(rawScriptURL)).toString();467 const QUrl scriptURL = normalizeScriptURL(rawScriptURL);
443 auto it = _scriptEnginesHash.find(scriptURLString);468 auto it = _scriptEnginesHash.find(scriptURL);
444 if (it != _scriptEnginesHash.end()) {469 if (it != _scriptEnginesHash.end()) {
445 result = it.value();470 result = it.value();
446 }471 }
@@ -459,8 +484,7 @@ void ScriptEngines::onScriptEngineLoaded(const QString& rawScriptURL) { · 12 unmodified lines
459 QWriteLocker lock(&_scriptEnginesHashLock);484 QWriteLocker lock(&_scriptEnginesHashLock);
460 QUrl url = QUrl(rawScriptURL);485 QUrl url = QUrl(rawScriptURL);
461 QUrl normalized = normalizeScriptURL(url);486 QUrl normalized = normalizeScriptURL(url);
462 const QString scriptURLString = normalized.toString();487 _scriptEnginesHash.insertMulti(normalized, scriptEngine);
463 _scriptEnginesHash.insertMulti(scriptURLString, scriptEngine);
464 }488 }
465 emit scriptCountChanged();489 emit scriptCountChanged();
466}490}
@@ -486,8 +510,8 @@ void ScriptEngines::onScriptFinished(const QString& rawScriptURL, ScriptEngine* · 19 unmodified lines
486 bool removed = false;510 bool removed = false;
487 {511 {
488 QWriteLocker lock(&_scriptEnginesHashLock);512 QWriteLocker lock(&_scriptEnginesHashLock);
489 const QString scriptURLString = normalizeScriptURL(QUrl(rawScriptURL)).toString();513 const QUrl scriptURL = normalizeScriptURL(QUrl(rawScriptURL));
490 for (auto it = _scriptEnginesHash.find(scriptURLString); it != _scriptEnginesHash.end(); ++it) {514 for (auto it = _scriptEnginesHash.find(scriptURL); it != _scriptEnginesHash.end(); ++it) {
491 if (it.value() == engine) {515 if (it.value() == engine) {
492 _scriptEnginesHash.erase(it);516 _scriptEnginesHash.erase(it);
493 removed = true;517 removed = true;