Showing the changes to libraries/script-engine/src/ScriptEngines.cpp (1 of 3 files). Show all
Unified · Split
| @@ -26,8 +26,12 @@ · 25 unmodified lines | ||
| 26 | 26 | #define __LOC__ __FILE__ "(" __STR1__(__LINE__) ") : Warning Msg: " |
| 27 | 27 | |
| 28 | 28 | static const QString DESKTOP_LOCATION = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); |
| 29 | − | |
| 30 | 29 | static const bool HIFI_SCRIPT_DEBUGGABLES { true }; |
| 30 | +static const QString SETTINGS_KEY { "RunningScripts" }; | |
| 31 | +static const QUrl DEFAULT_SCRIPTS_LOCATION { "file:///~//defaultScripts.js" }; | |
| 32 | +// Using a QVariantList so this is human-readable in the settings file | |
| 33 | +static Setting::Handle<QVariantList> runningScriptsHandle(SETTINGS_KEY, { QVariant(DEFAULT_SCRIPTS_LOCATION) }); | |
| 34 | + | |
| 31 | 35 | |
| 32 | 36 | ScriptsModel& getScriptsModel() { |
| 33 | 37 | static ScriptsModel scriptsModel; |
| @@ -61,19 +65,6 @@ ScriptEngines::ScriptEngines(ScriptEngine::Context context) · 27 unmodified lines | ||
| 61 | 65 | _scriptsModelFilter.setSourceModel(&_scriptsModel); |
| 62 | 66 | _scriptsModelFilter.sort(0, Qt::AscendingOrder); |
| 63 | 67 | _scriptsModelFilter.setDynamicSortFilter(true); |
| 64 | − | |
| 65 | − static const int SCRIPT_SAVE_COUNTDOWN_INTERVAL_MS = 5000; | |
| 66 | − QTimer* scriptSaveTimer = new QTimer(this); | |
| 67 | − scriptSaveTimer->setSingleShot(true); | |
| 68 | − QMetaObject::Connection timerConnection = connect(scriptSaveTimer, &QTimer::timeout, [] { | |
| 69 | − DependencyManager::get<ScriptEngines>()->saveScripts(); | |
| 70 | − }); | |
| 71 | − connect(qApp, &QCoreApplication::aboutToQuit, [=] { | |
| 72 | − disconnect(timerConnection); | |
| 73 | − }); | |
| 74 | − connect(this, &ScriptEngines::scriptCountChanged, this, [scriptSaveTimer] { | |
| 75 | − scriptSaveTimer->start(SCRIPT_SAVE_COUNTDOWN_INTERVAL_MS); | |
| 76 | − }, Qt::QueuedConnection); | |
| 77 | 68 | } |
| 78 | 69 | |
| 79 | 70 | QUrl normalizeScriptURL(const QUrl& rawScriptURL) { |
| @@ -280,13 +271,8 @@ QVariantList ScriptEngines::getRunning() { · 200 unmodified lines | ||
| 280 | 271 | return result; |
| 281 | 272 | } |
| 282 | 273 | |
| 283 | − | |
| 284 | −static const QString SETTINGS_KEY = "RunningScripts"; | |
| 285 | − | |
| 286 | 274 | void ScriptEngines::loadDefaultScripts() { |
| 287 | − QUrl defaultScriptsLoc = defaultScriptsLocation(); | |
| 288 | − defaultScriptsLoc.setPath(defaultScriptsLoc.path() + "/defaultScripts.js"); | |
| 289 | − loadScript(defaultScriptsLoc.toString()); | |
| 275 | + loadScript(DEFAULT_SCRIPTS_LOCATION); | |
| 290 | 276 | } |
| 291 | 277 | |
| 292 | 278 | void ScriptEngines::loadOneScript(const QString& scriptFilename) { |
| @@ -294,17 +280,11 @@ void ScriptEngines::loadOneScript(const QString& scriptFilename) { · 1 unmodified line | ||
| 294 | 280 | } |
| 295 | 281 | |
| 296 | 282 | void ScriptEngines::loadScripts() { |
| 297 | − // check first run... | |
| 298 | − Setting::Handle<bool> firstRun { Settings::firstRun, true }; | |
| 299 | − if (firstRun.get()) { | |
| 300 | − qCDebug(scriptengine) << "This is a first run..."; | |
| 301 | − // clear the scripts, and set out script to our default scripts | |
| 302 | − clearScripts(); | |
| 303 | − loadDefaultScripts(); | |
| 304 | − return; | |
| 305 | − } | |
| 306 | − | |
| 307 | − // loads all saved scripts | |
| 283 | + // START BACKWARD COMPATIBILITY CODE | |
| 284 | + // The following code makes sure people don't lose all their scripts | |
| 285 | + // This should be removed after a reasonable ammount of time went by | |
| 286 | + // Load old setting format if present | |
| 287 | + bool foundDeprecatedSetting = false; | |
| 308 | 288 | Settings settings; |
| 309 | 289 | int size = settings.beginReadArray(SETTINGS_KEY); |
| 310 | 290 | for (int i = 0; i < size; ++i) { |
| @@ -312,35 +292,51 @@ void ScriptEngines::loadScripts() { · 1 unmodified line | ||
| 312 | 292 | QString string = settings.value("script").toString(); |
| 313 | 293 | if (!string.isEmpty()) { |
| 314 | 294 | loadScript(string); |
| 295 | + foundDeprecatedSetting = true; | |
| 315 | 296 | } |
| 316 | 297 | } |
| 317 | 298 | settings.endArray(); |
| 318 | −} | |
| 299 | + if (foundDeprecatedSetting) { | |
| 300 | + // Remove old settings found and return | |
| 301 | + settings.beginWriteArray(SETTINGS_KEY); | |
| 302 | + settings.remove(""); | |
| 303 | + settings.endArray(); | |
| 304 | + settings.remove(SETTINGS_KEY + "/size"); | |
| 305 | + return; | |
| 306 | + } | |
| 307 | + // END BACKWARD COMPATIBILITY CODE | |
| 319 | 308 | |
| 320 | −void ScriptEngines::clearScripts() { | |
| 321 | − // clears all scripts from the settingsSettings settings; | |
| 322 | − Settings settings; | |
| 323 | − settings.beginWriteArray(SETTINGS_KEY); | |
| 324 | − settings.remove(""); | |
| 325 | − settings.endArray(); | |
| 309 | + // loads all saved scripts | |
| 310 | + auto runningScripts = runningScriptsHandle.get(); | |
| 311 | + for (auto script : runningScripts) { | |
| 312 | + auto string = script.toString(); | |
| 313 | + if (!string.isEmpty()) { | |
| 314 | + loadScript(string); | |
| 315 | + } | |
| 316 | + } | |
| 326 | 317 | } |
| 327 | 318 | |
| 328 | 319 | void ScriptEngines::saveScripts() { |
| 320 | + // Do not save anything if we are in the process of shutting down | |
| 321 | + if (qApp->closingDown()) { | |
| 322 | + qWarning() << "Trying to save scripts during shutdown."; | |
| 323 | + return; | |
| 324 | + } | |
| 325 | + | |
| 329 | 326 | // Saves all currently running user-loaded scripts |
| 330 | − Settings settings; | |
| 331 | − settings.beginWriteArray(SETTINGS_KEY); | |
| 332 | − settings.remove(""); | |
| 333 | − | |
| 334 | − QStringList runningScripts = getRunningScripts(); | |
| 335 | − int i = 0; | |
| 336 | − for (auto it = runningScripts.begin(); it != runningScripts.end(); ++it) { | |
| 337 | − if (getScriptEngine(*it)->isUserLoaded()) { | |
| 338 | − settings.setArrayIndex(i); | |
| 339 | − settings.setValue("script", normalizeScriptURL(*it).toString()); | |
| 340 | − ++i; | |
| 327 | + QVariantList list; | |
| 328 | + | |
| 329 | + { | |
| 330 | + QReadLocker lock(&_scriptEnginesHashLock); | |
| 331 | + for (auto it = _scriptEnginesHash.begin(); it != _scriptEnginesHash.end(); ++it) { | |
| 332 | + if (it.value() && it.value()->isUserLoaded()) { | |
| 333 | + auto normalizedUrl = normalizeScriptURL(it.key()); | |
| 334 | + list.append(normalizedUrl.toString()); | |
| 335 | + } | |
| 341 | 336 | } |
| 342 | 337 | } |
| 343 | − settings.endArray(); | |
| 338 | + | |
| 339 | + runningScriptsHandle.set(list); | |
| 344 | 340 | } |
| 345 | 341 | |
| 346 | 342 | QStringList ScriptEngines::getRunningScripts() { |
| @@ -513,6 +509,9 @@ void ScriptEngines::onScriptEngineLoaded(const QString& rawScriptURL) { · 166 unmodified lines | ||
| 513 | 509 | QUrl normalized = normalizeScriptURL(url); |
| 514 | 510 | _scriptEnginesHash.insertMulti(normalized, scriptEngine); |
| 515 | 511 | } |
| 512 | + | |
| 513 | + // Update settings with new script | |
| 514 | + saveScripts(); | |
| 516 | 515 | emit scriptCountChanged(); |
| 517 | 516 | } |
| 518 | 517 | |
| @@ -553,6 +552,8 @@ void ScriptEngines::onScriptFinished(const QString& rawScriptURL, ScriptEngine* · 34 unmodified lines | ||
| 553 | 552 | } |
| 554 | 553 | |
| 555 | 554 | if (removed) { |
| 555 | + // Update settings with removed script | |
| 556 | + saveScripts(); | |
| 556 | 557 | emit scriptCountChanged(); |
| 557 | 558 | } |
| 558 | 559 | } |