Showing the changes to libraries/script-engine/src/ScriptEngines.cpp (1 of 6 files). Show all
Unified · Split
| @@ -42,52 +42,49 @@ ScriptEngines::ScriptEngines() · 41 unmodified lines | ||
| 42 | 42 | _scriptsModelFilter.setDynamicSortFilter(true); |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | −QString normalizeScriptUrl(const QString& rawScriptUrl) { | |
| 46 | − if (!rawScriptUrl.startsWith("http:") && !rawScriptUrl.startsWith("https:") && !rawScriptUrl.startsWith("atp:")) { | |
| 47 | −#ifdef Q_OS_LINUX | |
| 48 | − if (rawScriptUrl.startsWith("file:")) { | |
| 49 | − return rawScriptUrl; | |
| 45 | +QUrl normalizeScriptURL(const QUrl& rawScriptURL) { | |
| 46 | + if (rawScriptURL.scheme() == "file") { | |
| 47 | + QUrl fullNormal = rawScriptURL; | |
| 48 | + QUrl defaultScriptLoc = defaultScriptsLocation(); | |
| 49 | + | |
| 50 | + #ifdef Q_OS_LINUX | |
| 51 | + #else | |
| 52 | + // Force lowercase on file scripts because of drive letter weirdness. | |
| 53 | + if (rawScriptURL.isLocalFile()) { | |
| 54 | + fullNormal.setPath(fullNormal.path.toLower()); | |
| 50 | 55 | } |
| 51 | − return QUrl::fromLocalFile(rawScriptUrl).toString(); | |
| 52 | −#else | |
| 53 | − QString fullNormal; | |
| 54 | − if (rawScriptUrl.startsWith("file:")) { | |
| 55 | − fullNormal = rawScriptUrl.toLower(); | |
| 56 | − } else { | |
| 57 | − // Force lowercase on file scripts because of drive letter weirdness. | |
| 58 | − fullNormal = QUrl::fromLocalFile(rawScriptUrl).toString().toLower(); | |
| 59 | − } | |
| 60 | − QString defaultScriptLoc = defaultScriptsLocation(); | |
| 61 | − if (fullNormal.startsWith(defaultScriptLoc)) { | |
| 62 | − return "~" + fullNormal.mid(defaultScriptLoc.size()); | |
| 56 | + #endif | |
| 57 | + // if this url is something "beneath" the default script url, replace the local path with ~ | |
| 58 | + if (fullNormal.scheme() == defaultScriptLoc.scheme() && | |
| 59 | + fullNormal.host() == defaultScriptLoc.host() && | |
| 60 | + fullNormal.path().startsWith(defaultScriptLoc.path())) { | |
| 61 | + fullNormal.setPath("/~/" + fullNormal.path().mid(defaultScriptLoc.path().size())); | |
| 63 | 62 | } |
| 64 | 63 | return fullNormal; |
| 65 | −#endif | |
| 66 | − | |
| 64 | + } else if (rawScriptURL.scheme() == "http" || rawScriptURL.scheme() == "https" || rawScriptURL.scheme() == "atp") { | |
| 65 | + return rawScriptURL; | |
| 66 | + } else { | |
| 67 | + // don't accidently support gopher | |
| 68 | + return QUrl(""); | |
| 67 | 69 | } |
| 68 | − return QUrl(rawScriptUrl).toString(); | |
| 69 | 70 | } |
| 70 | 71 | |
| 71 | −QString expandScriptUrl(const QString& normalizedScriptURL) { | |
| 72 | − if (normalizedScriptURL.startsWith("http:") || | |
| 73 | − normalizedScriptURL.startsWith("https:") || | |
| 74 | − normalizedScriptURL.startsWith("atp:")) { | |
| 75 | − return QUrl(normalizedScriptURL).toString(); | |
| 76 | − } | |
| 77 | − | |
| 78 | − QUrl url; | |
| 79 | − if (normalizedScriptURL.startsWith("file:")) { | |
| 80 | − url = QUrl(normalizedScriptURL); | |
| 72 | +QUrl expandScriptUrl(const QUrl& normalizedScriptURL) { | |
| 73 | + if (normalizedScriptURL.scheme() == "http" || | |
| 74 | + normalizedScriptURL.scheme() == "https" || | |
| 75 | + normalizedScriptURL.scheme() == "atp") { | |
| 76 | + return normalizedScriptURL; | |
| 77 | + } else if (normalizedScriptURL.scheme() == "file") { | |
| 78 | + if (normalizedScriptURL.path().startsWith("/~/")) { | |
| 79 | + QUrl url = normalizedScriptURL; | |
| 80 | + QStringList splitPath = url.path().split("/"); | |
| 81 | + QUrl defaultScriptsLoc = defaultScriptsLocation(); | |
| 82 | + url.setPath(defaultScriptsLoc.path() + "/" + splitPath.mid(2).join("/")); // 2 to skip the slashes in /~/ | |
| 83 | + return url; | |
| 84 | + } | |
| 85 | + return normalizedScriptURL; | |
| 81 | 86 | } else { |
| 82 | − url = QUrl::fromLocalFile(normalizedScriptURL); | |
| 83 | − } | |
| 84 | − | |
| 85 | − QString path = url.path(); | |
| 86 | − QStringList splitPath = path.split("/"); | |
| 87 | − if (splitPath.size() > 0 && splitPath[0] == "~") { | |
| 88 | − QString defaultScriptLoc = defaultScriptsLocation(); | |
| 89 | − url.setPath(defaultScriptLoc + splitPath.mid(1).join("/")); | |
| 90 | − return url.toString(); | |
| 87 | + return QUrl(""); | |
| 91 | 88 | } |
| 92 | 89 | } |
| 93 | 90 | |
| @@ -229,14 +226,21 @@ QVariantList ScriptEngines::getRunning() { · 135 unmodified lines | ||
| 229 | 226 | auto runningScripts = getRunningScripts(); |
| 230 | 227 | foreach(const QString& runningScript, runningScripts) { |
| 231 | 228 | QUrl runningScriptURL = QUrl(runningScript); |
| 232 | − if (runningScriptURL.scheme().size() <= WINDOWS_DRIVE_LETTER_SIZE) { | |
| 229 | + if (!runningScriptURL.isValid()) { | |
| 233 | 230 | runningScriptURL = QUrl::fromLocalFile(runningScriptURL.toDisplayString(QUrl::FormattingOptions(QUrl::FullyEncoded))); |
| 234 | 231 | } |
| 235 | 232 | QVariantMap resultNode; |
| 236 | 233 | resultNode.insert("name", runningScriptURL.fileName()); |
| 237 | − resultNode.insert("url", runningScriptURL.toDisplayString(QUrl::FormattingOptions(QUrl::FullyEncoded))); | |
| 234 | + QUrl displayURL = expandScriptUrl(QUrl(runningScriptURL)); | |
| 235 | + QString displayURLString; | |
| 236 | + if (displayURL.isLocalFile()) { | |
| 237 | + displayURLString = displayURL.toLocalFile(); | |
| 238 | + } else { | |
| 239 | + displayURLString = displayURL.toDisplayString(QUrl::FormattingOptions(QUrl::FullyEncoded)); | |
| 240 | + } | |
| 241 | + resultNode.insert("url", displayURLString); | |
| 238 | 242 | // The path contains the exact path/URL of the script, which also is used in the stopScript function. |
| 239 | − resultNode.insert("path", runningScript); | |
| 243 | + resultNode.insert("path", normalizeScriptURL(runningScript).toString()); | |
| 240 | 244 | resultNode.insert("local", runningScriptURL.isLocalFile()); |
| 241 | 245 | result.append(resultNode); |
| 242 | 246 | } |
| @@ -247,7 +251,9 @@ QVariantList ScriptEngines::getRunning() { · 4 unmodified lines | ||
| 247 | 251 | static const QString SETTINGS_KEY = "Settings"; |
| 248 | 252 | |
| 249 | 253 | void ScriptEngines::loadDefaultScripts() { |
| 250 | − loadScript(defaultScriptsLocation() + "/scripts/defaultScripts.js"); | |
| 254 | + QUrl defaultScriptsLoc = defaultScriptsLocation(); | |
| 255 | + defaultScriptsLoc.setPath(defaultScriptsLoc.path() + "/scripts/defaultScripts.js"); | |
| 256 | + loadScript(defaultScriptsLoc.toString()); | |
| 251 | 257 | } |
| 252 | 258 | |
| 253 | 259 | void ScriptEngines::loadOneScript(const QString& scriptFilename) { |
| @@ -296,7 +302,7 @@ void ScriptEngines::saveScripts() { · 42 unmodified lines | ||
| 296 | 302 | for (auto it = runningScripts.begin(); it != runningScripts.end(); ++it) { |
| 297 | 303 | if (getScriptEngine(*it)->isUserLoaded()) { |
| 298 | 304 | settings.setArrayIndex(i); |
| 299 | − settings.setValue("script", *it); | |
| 305 | + settings.setValue("script", normalizeScriptURL(*it).toString()); | |
| 300 | 306 | ++i; |
| 301 | 307 | } |
| 302 | 308 | } |
| @@ -338,11 +344,16 @@ void ScriptEngines::stopAllScripts(bool restart) { · 35 unmodified lines | ||
| 338 | 344 | } |
| 339 | 345 | } |
| 340 | 346 | |
| 341 | −bool ScriptEngines::stopScript(const QString& rawScriptUrl, bool restart) { | |
| 347 | +bool ScriptEngines::stopScript(const QString& rawScriptURL, bool restart) { | |
| 342 | 348 | bool stoppedScript = false; |
| 343 | 349 | { |
| 350 | + QUrl scriptURL = normalizeScriptURL(QUrl(rawScriptURL)); | |
| 351 | + if (!scriptURL.isValid()) { | |
| 352 | + scriptURL = normalizeScriptURL(QUrl::fromLocalFile(rawScriptURL)); | |
| 353 | + } | |
| 354 | + const QString scriptURLString = scriptURL.toString(); | |
| 355 | + | |
| 344 | 356 | QReadLocker lock(&_scriptEnginesHashLock); |
| 345 | − const QString scriptURLString = normalizeScriptUrl(rawScriptUrl); | |
| 346 | 357 | if (_scriptEnginesHash.contains(scriptURLString)) { |
| 347 | 358 | ScriptEngine* scriptEngine = _scriptEnginesHash[scriptURLString]; |
| 348 | 359 | if (restart) { |
| @@ -375,18 +386,19 @@ void ScriptEngines::reloadAllScripts() { · 26 unmodified lines | ||
| 375 | 386 | stopAllScripts(true); |
| 376 | 387 | } |
| 377 | 388 | |
| 378 | −ScriptEngine* ScriptEngines::loadScript(const QString& scriptFilename, bool isUserLoaded, bool loadScriptFromEditor, bool activateMainWindow, bool reload) { | |
| 389 | +ScriptEngine* ScriptEngines::loadScript(const QUrl& scriptFilename, bool isUserLoaded, bool loadScriptFromEditor, | |
| 390 | + bool activateMainWindow, bool reload) { | |
| 379 | 391 | if (thread() != QThread::currentThread()) { |
| 380 | 392 | ScriptEngine* result { nullptr }; |
| 381 | 393 | QMetaObject::invokeMethod(this, "loadScript", Qt::BlockingQueuedConnection, Q_RETURN_ARG(ScriptEngine*, result), |
| 382 | − Q_ARG(QString, scriptFilename), | |
| 394 | + Q_ARG(QUrl, scriptFilename), | |
| 383 | 395 | Q_ARG(bool, isUserLoaded), |
| 384 | 396 | Q_ARG(bool, loadScriptFromEditor), |
| 385 | 397 | Q_ARG(bool, activateMainWindow), |
| 386 | 398 | Q_ARG(bool, reload)); |
| 387 | 399 | return result; |
| 388 | 400 | } |
| 389 | − QUrl scriptUrl(scriptFilename); | |
| 401 | + QUrl scriptUrl = normalizeScriptURL(scriptFilename); | |
| 390 | 402 | auto scriptEngine = getScriptEngine(scriptUrl.toString()); |
| 391 | 403 | if (scriptEngine) { |
| 392 | 404 | return scriptEngine; |
| @@ -399,7 +411,7 @@ ScriptEngine* ScriptEngines::loadScript(const QString& scriptFilename, bool isUs · 6 unmodified lines | ||
| 399 | 411 | }, Qt::QueuedConnection); |
| 400 | 412 | |
| 401 | 413 | |
| 402 | − if (scriptFilename.isNull()) { | |
| 414 | + if (scriptFilename.isEmpty()) { | |
| 403 | 415 | launchScriptEngine(scriptEngine); |
| 404 | 416 | } else { |
| 405 | 417 | // connect to the appropriate signals of this script engine |
| @@ -407,17 +419,17 @@ ScriptEngine* ScriptEngines::loadScript(const QString& scriptFilename, bool isUs · 1 unmodified line | ||
| 407 | 419 | connect(scriptEngine, &ScriptEngine::errorLoadingScript, this, &ScriptEngines::onScriptEngineError); |
| 408 | 420 | |
| 409 | 421 | // get the script engine object to load the script at the designated script URL |
| 410 | − scriptEngine->loadURL(scriptUrl, reload); | |
| 422 | + scriptEngine->loadURL(QUrl(expandScriptUrl(scriptUrl.toString())), reload); | |
| 411 | 423 | } |
| 412 | 424 | |
| 413 | 425 | return scriptEngine; |
| 414 | 426 | } |
| 415 | 427 | |
| 416 | −ScriptEngine* ScriptEngines::getScriptEngine(const QString& rawScriptUrl) { | |
| 428 | +ScriptEngine* ScriptEngines::getScriptEngine(const QString& rawScriptURL) { | |
| 417 | 429 | ScriptEngine* result = nullptr; |
| 418 | 430 | { |
| 419 | 431 | QReadLocker lock(&_scriptEnginesHashLock); |
| 420 | − const QString scriptURLString = normalizeScriptUrl(rawScriptUrl); | |
| 432 | + const QString scriptURLString = normalizeScriptURL(QUrl(rawScriptURL)).toString(); | |
| 421 | 433 | auto it = _scriptEnginesHash.find(scriptURLString); |
| 422 | 434 | if (it != _scriptEnginesHash.end()) { |
| 423 | 435 | result = it.value(); |
| @@ -427,15 +439,17 @@ ScriptEngine* ScriptEngines::getScriptEngine(const QString& rawScriptUrl) { · 3 unmodified lines | ||
| 427 | 439 | } |
| 428 | 440 | |
| 429 | 441 | // FIXME - change to new version of ScriptCache loading notification |
| 430 | −void ScriptEngines::onScriptEngineLoaded(const QString& rawScriptUrl) { | |
| 431 | − UserActivityLogger::getInstance().loadedScript(rawScriptUrl); | |
| 442 | +void ScriptEngines::onScriptEngineLoaded(const QString& rawScriptURL) { | |
| 443 | + UserActivityLogger::getInstance().loadedScript(rawScriptURL); | |
| 432 | 444 | ScriptEngine* scriptEngine = qobject_cast<ScriptEngine*>(sender()); |
| 433 | 445 | |
| 434 | 446 | launchScriptEngine(scriptEngine); |
| 435 | 447 | |
| 436 | 448 | { |
| 437 | 449 | QWriteLocker lock(&_scriptEnginesHashLock); |
| 438 | − const QString scriptURLString = normalizeScriptUrl(rawScriptUrl); | |
| 450 | + QUrl url = QUrl(rawScriptURL); | |
| 451 | + QUrl normalized = normalizeScriptURL(url); | |
| 452 | + const QString scriptURLString = normalized.toString(); | |
| 439 | 453 | _scriptEnginesHash.insertMulti(scriptURLString, scriptEngine); |
| 440 | 454 | } |
| 441 | 455 | emit scriptCountChanged(); |
| @@ -458,11 +472,11 @@ void ScriptEngines::launchScriptEngine(ScriptEngine* scriptEngine) { · 16 unmodified lines | ||
| 458 | 472 | } |
| 459 | 473 | |
| 460 | 474 | |
| 461 | −void ScriptEngines::onScriptFinished(const QString& rawScriptUrl, ScriptEngine* engine) { | |
| 475 | +void ScriptEngines::onScriptFinished(const QString& rawScriptURL, ScriptEngine* engine) { | |
| 462 | 476 | bool removed = false; |
| 463 | 477 | { |
| 464 | 478 | QWriteLocker lock(&_scriptEnginesHashLock); |
| 465 | − const QString scriptURLString = normalizeScriptUrl(rawScriptUrl); | |
| 479 | + const QString scriptURLString = normalizeScriptURL(QUrl(rawScriptURL)).toString(); | |
| 466 | 480 | for (auto it = _scriptEnginesHash.find(scriptURLString); it != _scriptEnginesHash.end(); ++it) { |
| 467 | 481 | if (it.value() == engine) { |
| 468 | 482 | _scriptEnginesHash.erase(it); |