Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 3 files). Show all
Unified · Split
| @@ -113,11 +113,12 @@ void ScriptEngine::cleanupMenuItems() { · 112 unmodified lines | |||
| 113 | } | 113 | } |
| 114 | } | 114 | } |
| 115 | 115 | ||
| 116 | bool ScriptEngine::setScriptContents(const QString& scriptContents) { | 116 | bool ScriptEngine::setScriptContents(const QString& scriptContents, const QString& fileNameString) { |
| 117 | if (_isRunning) { | 117 | if (_isRunning) { |
| 118 | return false; | 118 | return false; |
| 119 | } | 119 | } |
| 120 | _scriptContents = scriptContents; | 120 | _scriptContents = scriptContents; |
| 121 | _fileNameString = fileNameString; | ||
| 121 | return true; | 122 | return true; |
| 122 | } | 123 | } |
| 123 | 124 | ||
| @@ -436,3 +437,55 @@ void ScriptEngine::stopTimer(QTimer *timer) { · 312 unmodified lines | |||
| 436 | delete timer; | 437 | delete timer; |
| 437 | } | 438 | } |
| 438 | } | 439 | } |
| 440 | |||
| 441 | QUrl ScriptEngine::resolveInclude(const QString& include) const { | ||
| 442 | // first lets check to see if it's already a full URL | ||
| 443 | QUrl url(include); | ||
| 444 | if (!url.scheme().isEmpty()) { | ||
| 445 | return url; | ||
| 446 | } | ||
| 447 | |||
| 448 | // we apparently weren't a fully qualified url, so, let's assume we're relative | ||
| 449 | // to the original URL of our script | ||
| 450 | QUrl parentURL(_fileNameString); | ||
| 451 | |||
| 452 | // if the parent URL's scheme is empty, then this is probably a local file... | ||
| 453 | if (parentURL.scheme().isEmpty()) { | ||
| 454 | parentURL = QUrl::fromLocalFile(_fileNameString); | ||
| 455 | } | ||
| 456 | |||
| 457 | // at this point we should have a legitimate fully qualified URL for our parent | ||
| 458 | url = parentURL.resolved(url); | ||
| 459 | return url; | ||
| 460 | } | ||
| 461 | |||
| 462 | void ScriptEngine::include(const QString& includeFile) { | ||
| 463 | QUrl url = resolveInclude(includeFile); | ||
| 464 | QString includeContents; | ||
| 465 | |||
| 466 | if (url.scheme() == "file") { | ||
| 467 | QString fileName = url.toLocalFile(); | ||
| 468 | QFile scriptFile(fileName); | ||
| 469 | if (scriptFile.open(QFile::ReadOnly | QFile::Text)) { | ||
| 470 | qDebug() << "Loading file:" << fileName; | ||
| 471 | QTextStream in(&scriptFile); | ||
| 472 | includeContents = in.readAll(); | ||
| 473 | } else { | ||
| 474 | qDebug() << "ERROR Loading file:" << fileName; | ||
| 475 | } | ||
| 476 | } else { | ||
| 477 | QNetworkAccessManager* networkManager = new QNetworkAccessManager(this); | ||
| 478 | QNetworkReply* reply = networkManager->get(QNetworkRequest(url)); | ||
| 479 | qDebug() << "Downloading included script at" << includeFile; | ||
| 480 | QEventLoop loop; | ||
| 481 | QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); | ||
| 482 | loop.exec(); | ||
| 483 | includeContents = reply->readAll(); | ||
| 484 | } | ||
| 485 | |||
| 486 | QScriptValue result = _engine.evaluate(includeContents); | ||
| 487 | if (_engine.hasUncaughtException()) { | ||
| 488 | int line = _engine.uncaughtExceptionLineNumber(); | ||
| 489 | qDebug() << "Uncaught exception at (" << includeFile << ") line" << line << ":" << result.toString(); | ||
| 490 | } | ||
| 491 | } | ||