diff --git a/src/gui/query/apply_sort_query.h b/src/gui/query/apply_sort_query.h index 4123c57e..e3eafda5 100644 --- a/src/gui/query/apply_sort_query.h +++ b/src/gui/query/apply_sort_query.h @@ -33,16 +33,17 @@ public: ApplySortQuery(LootState& state, const std::vector& plugins) : state_(state), plugins_(plugins) {} - void execute(CefRefPtr callback) { + std::string executeLogic() { BOOST_LOG_TRIVIAL(trace) << "User has accepted sorted load order, applying it."; state_.decrementUnappliedChangeCounter(); state_.getCurrentGame().SetLoadOrder(plugins_); - callback->Success(""); + + return ""; } private: LootState& state_; - std::vector plugins_; + const std::vector plugins_; }; } diff --git a/src/gui/query/cancel_find_query.h b/src/gui/query/cancel_find_query.h index f2b70450..70fbe1b4 100644 --- a/src/gui/query/cancel_find_query.h +++ b/src/gui/query/cancel_find_query.h @@ -34,13 +34,13 @@ class CancelFindQuery : public Query { public: CancelFindQuery(CefRefPtr browser) : browser_(browser) {} - void execute(CefRefPtr callback) { + std::string executeLogic() { browser_->GetHost()->StopFinding(true); - callback->Success(""); + return ""; } private: - CefRefPtr browser_; + const CefRefPtr browser_; }; } diff --git a/src/gui/query/cancel_sort_query.h b/src/gui/query/cancel_sort_query.h index ac32a4c8..b75dc7c7 100644 --- a/src/gui/query/cancel_sort_query.h +++ b/src/gui/query/cancel_sort_query.h @@ -36,12 +36,11 @@ public: MetadataQuery(state.getCurrentGame(), state.getLanguage().GetCode()), state_(state) {} - void execute(CefRefPtr callback) { + std::string executeLogic() { state_.decrementUnappliedChangeCounter(); state_.getCurrentGame().DecrementLoadOrderSortCount(); - YAML::Node node(getGeneralMessages()); - callback->Success(JSON::stringify(node)); + return JSON::stringify(YAML::Node(getGeneralMessages())); } private: diff --git a/src/gui/query/change_game_query.h b/src/gui/query/change_game_query.h index e618fc11..dea40451 100644 --- a/src/gui/query/change_game_query.h +++ b/src/gui/query/change_game_query.h @@ -38,10 +38,10 @@ public: state_(state), gameFolder_(gameFolder) {} - void execute(CefRefPtr callback) { + std::string executeLogic() { state_.changeGame(gameFolder_); - GetGameDataQuery::execute(callback); + return GetGameDataQuery::executeLogic(); } private: diff --git a/src/gui/query/clear_all_metadata_query.h b/src/gui/query/clear_all_metadata_query.h index 231b517d..0eb8ff8e 100644 --- a/src/gui/query/clear_all_metadata_query.h +++ b/src/gui/query/clear_all_metadata_query.h @@ -36,7 +36,7 @@ public: MetadataQuery(state.getCurrentGame(), state.getLanguage().GetCode()), game_(state.getCurrentGame()) {} - void execute(CefRefPtr callback) { + std::string executeLogic() { BOOST_LOG_TRIVIAL(debug) << "Clearing all user metadata."; // Record which plugins have userlist entries. @@ -47,7 +47,8 @@ public: game_.GetUserlist().Save(game_.UserlistPath()); BOOST_LOG_TRIVIAL(trace) << "Rederiving display metadata for " << userlistPluginNames.size() << " plugins that had user metadata."; - callback->Success(getDerivedMetadataJson(userlistPluginNames)); + + return getDerivedMetadataJson(userlistPluginNames); } private: diff --git a/src/gui/query/clear_plugin_metadata_query.h b/src/gui/query/clear_plugin_metadata_query.h index 423ab51c..3aa16252 100644 --- a/src/gui/query/clear_plugin_metadata_query.h +++ b/src/gui/query/clear_plugin_metadata_query.h @@ -37,7 +37,7 @@ public: game_(state.getCurrentGame()), pluginName_(pluginName) {} - void execute(CefRefPtr callback) { + std::string executeLogic() { BOOST_LOG_TRIVIAL(debug) << "Clearing user metadata for plugin " << pluginName_; game_.GetUserlist().ErasePlugin(PluginMetadata(pluginName_)); @@ -46,14 +46,14 @@ public: // Now rederive the displayed metadata from the masterlist. YAML::Node derivedMetadata = generateDerivedMetadata(pluginName_); if (derivedMetadata.size() > 0) - callback->Success(JSON::stringify(derivedMetadata)); - else - callback->Success("null"); + return JSON::stringify(derivedMetadata); + + return "null"; } private: Game& game_; - std::string pluginName_; + const std::string pluginName_; }; } diff --git a/src/gui/query/close_settings_query.h b/src/gui/query/close_settings_query.h index d897a605..f6738d79 100644 --- a/src/gui/query/close_settings_query.h +++ b/src/gui/query/close_settings_query.h @@ -34,11 +34,11 @@ public: CloseSettingsQuery(LootState& state, YAML::Node settings) : GetInstalledGamesQuery(state), state_(state), settings_(settings) {} - void execute(CefRefPtr callback) { + std::string executeLogic() { BOOST_LOG_TRIVIAL(trace) << "Settings dialog closed and changes accepted, updating settings object."; state_.load(settings_); - GetInstalledGamesQuery::execute(callback); + return GetInstalledGamesQuery::executeLogic(); } private: diff --git a/src/gui/query/copy_content_query.h b/src/gui/query/copy_content_query.h index be7f95d6..ef8bb16a 100644 --- a/src/gui/query/copy_content_query.h +++ b/src/gui/query/copy_content_query.h @@ -34,11 +34,11 @@ class CopyContentQuery : public ClipboardQuery { public: CopyContentQuery(const YAML::Node& content) : content_(content) {} - void execute(CefRefPtr callback) { - std::string text = "[spoiler][code]" + getContentAsText() + "[/code][/spoiler]"; + std::string executeLogic() { + const std::string text = "[spoiler][code]" + getContentAsText() + "[/code][/spoiler]"; copyToClipboard(text); - callback->Success(""); + return ""; } private: @@ -53,7 +53,7 @@ private: return text; } - YAML::Node content_; + const YAML::Node content_; }; } diff --git a/src/gui/query/copy_load_order_query.h b/src/gui/query/copy_load_order_query.h index ab266d39..6ea42af8 100644 --- a/src/gui/query/copy_load_order_query.h +++ b/src/gui/query/copy_load_order_query.h @@ -33,7 +33,7 @@ public: CopyLoadOrderQuery(LootState& state, const std::vector& plugins) : state_(state), plugins_(plugins) {} - void execute(CefRefPtr callback) { + std::string executeLogic() { int numberOfIndexDigits = getNumberOfIndexDigits(); size_t activeIndex = 0; @@ -43,7 +43,7 @@ public: } copyToClipboard(stream.str()); - callback->Success(""); + return ""; } private: @@ -67,7 +67,7 @@ private: } LootState& state_; - std::vector plugins_; + const std::vector plugins_; }; } diff --git a/src/gui/query/copy_metadata_query.h b/src/gui/query/copy_metadata_query.h index 2d561034..a8f3c8ee 100644 --- a/src/gui/query/copy_metadata_query.h +++ b/src/gui/query/copy_metadata_query.h @@ -35,7 +35,7 @@ public: CopyMetadataQuery(LootState& state, const std::string& pluginName) : state_(state), pluginName_(pluginName) {} - void execute(CefRefPtr callback) { + std::string executeLogic() { BOOST_LOG_TRIVIAL(debug) << "Copying metadata for plugin " << pluginName_; // Get metadata from masterlist and userlist. @@ -48,6 +48,8 @@ public: copyToClipboard(text); BOOST_LOG_TRIVIAL(info) << "Exported userlist metadata text for \"" << pluginName_ << "\": " << text; + + return ""; } private: diff --git a/src/gui/query/discard_unapplied_changes_query.h b/src/gui/query/discard_unapplied_changes_query.h index ed9b66b0..94ae7106 100644 --- a/src/gui/query/discard_unapplied_changes_query.h +++ b/src/gui/query/discard_unapplied_changes_query.h @@ -33,10 +33,11 @@ class DiscardUnappliedChangesQuery : public Query { public: DiscardUnappliedChangesQuery(LootState& state) : state_(state) {} - void execute(CefRefPtr callback) { + std::string executeLogic() { while (state_.hasUnappliedChanges()) state_.decrementUnappliedChangeCounter(); - callback->Success(""); + + return ""; } private: diff --git a/src/gui/query/editor_closed_query.h b/src/gui/query/editor_closed_query.h index 9ee1bbe8..67610d73 100644 --- a/src/gui/query/editor_closed_query.h +++ b/src/gui/query/editor_closed_query.h @@ -35,9 +35,12 @@ public: MetadataQuery(state.getCurrentGame(), state.getLanguage().GetCode()), state_(state), metadata_(metadata) {} - void execute(CefRefPtr callback) { + std::string executeLogic() { try { - callback->Success(applyUserEdits()); + const std::string json = applyUserEdits(); + state_.decrementUnappliedChangeCounter(); + + return json; } catch (Error&) { throw; } catch (std::exception& e) { @@ -51,7 +54,6 @@ public: throw std::runtime_error(error); } - state_.decrementUnappliedChangeCounter(); } private: diff --git a/src/gui/query/editor_opened_query.h b/src/gui/query/editor_opened_query.h index dd7da973..d2fd4037 100644 --- a/src/gui/query/editor_opened_query.h +++ b/src/gui/query/editor_opened_query.h @@ -33,9 +33,9 @@ class EditorOpenedQuery : public Query { public: EditorOpenedQuery(LootState& state) : state_(state) {} - void execute(CefRefPtr callback) { + std::string executeLogic() { state_.incrementUnappliedChangeCounter(); - callback->Success(""); + return ""; } private: diff --git a/src/gui/query/get_conflicting_plugins_query.h b/src/gui/query/get_conflicting_plugins_query.h index 945e62fa..526e1997 100644 --- a/src/gui/query/get_conflicting_plugins_query.h +++ b/src/gui/query/get_conflicting_plugins_query.h @@ -37,7 +37,7 @@ public: game_(state.getCurrentGame()), pluginName_(pluginName) {} - void execute(CefRefPtr callback) { + std::string executeLogic() { BOOST_LOG_TRIVIAL(debug) << "Searching for plugins that conflict with " << pluginName_; // Checking for FormID overlap will only work if the plugins have been loaded, so check if @@ -52,9 +52,9 @@ public: } if (node.size() > 0) - callback->Success(JSON::stringify(node)); - else - callback->Success("[]"); + return JSON::stringify(node); + + return "[]"; } private: @@ -76,7 +76,7 @@ private: } Game& game_; - std::string pluginName_; + const std::string pluginName_; }; } diff --git a/src/gui/query/get_game_data_query.h b/src/gui/query/get_game_data_query.h index 528fc014..120ea023 100644 --- a/src/gui/query/get_game_data_query.h +++ b/src/gui/query/get_game_data_query.h @@ -41,7 +41,7 @@ public: state_(state), frame_(frame) {} - void execute(CefRefPtr callback) { + std::string executeLogic() { sendProgressUpdate(frame_, boost::locale::translate("Parsing, merging and evaluating metadata...")); // First clear CRC and condition caches, otherwise they could lead to incorrect evaluations. @@ -64,7 +64,7 @@ public: } catch (...) {} } - callback->Success(generateJsonResponse(installed)); + return generateJsonResponse(installed); } private: diff --git a/src/gui/query/get_game_types_query.h b/src/gui/query/get_game_types_query.h index 37b2642a..1e466047 100644 --- a/src/gui/query/get_game_types_query.h +++ b/src/gui/query/get_game_types_query.h @@ -32,9 +32,9 @@ along with LOOT. If not, see namespace loot { class GetGameTypesQuery : public Query { public: - void execute(CefRefPtr callback) { + std::string executeLogic() { BOOST_LOG_TRIVIAL(info) << "Getting LOOT's supported languages."; - callback->Success(getGameTypesAsJson()); + return getGameTypesAsJson(); } private: diff --git a/src/gui/query/get_init_errors_query.h b/src/gui/query/get_init_errors_query.h index e79c20e8..d2d36b75 100644 --- a/src/gui/query/get_init_errors_query.h +++ b/src/gui/query/get_init_errors_query.h @@ -34,12 +34,12 @@ class GetInitErrorsQuery : public Query { public: GetInitErrorsQuery(LootState& state) : state_(state) {} - void execute(CefRefPtr callback) { + std::string executeLogic() { YAML::Node node(state_.getInitErrors()); if (node.size() > 0) - callback->Success(JSON::stringify(node)); - else - callback->Success("null"); + return JSON::stringify(node); + + return "null"; } private: diff --git a/src/gui/query/get_installed_games_query.h b/src/gui/query/get_installed_games_query.h index 67085fe1..bb83084d 100644 --- a/src/gui/query/get_installed_games_query.h +++ b/src/gui/query/get_installed_games_query.h @@ -34,9 +34,9 @@ class GetInstalledGamesQuery : public Query { public: GetInstalledGamesQuery(LootState& state) : state_(state) {} - void execute(CefRefPtr callback) { + std::string executeLogic() { BOOST_LOG_TRIVIAL(info) << "Getting LOOT's detected games."; - callback->Success(getInstalledGamesAsJson()); + return getInstalledGamesAsJson(); } private: diff --git a/src/gui/query/get_languages_query.h b/src/gui/query/get_languages_query.h index 5acb7f65..dc259364 100644 --- a/src/gui/query/get_languages_query.h +++ b/src/gui/query/get_languages_query.h @@ -32,9 +32,9 @@ along with LOOT. If not, see namespace loot { class GetLanguagesQuery : public Query { public: - void execute(CefRefPtr callback) { + std::string executeLogic() { BOOST_LOG_TRIVIAL(info) << "Getting LOOT's supported languages."; - callback->Success(getLanguagesAsJson()); + return getLanguagesAsJson(); } private: diff --git a/src/gui/query/get_settings_query.h b/src/gui/query/get_settings_query.h index e5c0e068..0aac9fe7 100644 --- a/src/gui/query/get_settings_query.h +++ b/src/gui/query/get_settings_query.h @@ -34,13 +34,13 @@ class GetSettingsQuery : public Query { public: GetSettingsQuery(LootSettings& settings) : settings_(settings) {} - void execute(CefRefPtr callback) { + std::string executeLogic() { BOOST_LOG_TRIVIAL(info) << "Getting LOOT settings."; - callback->Success(JSON::stringify(settings_.toYaml())); + return JSON::stringify(settings_.toYaml()); } private: - LootSettings& settings_; + const LootSettings& settings_; }; } diff --git a/src/gui/query/get_version_query.h b/src/gui/query/get_version_query.h index 592806a3..a477b07c 100644 --- a/src/gui/query/get_version_query.h +++ b/src/gui/query/get_version_query.h @@ -31,9 +31,9 @@ along with LOOT. If not, see namespace loot { class GetVersionQuery : public Query { public: - void execute(CefRefPtr callback) { + std::string executeLogic() { BOOST_LOG_TRIVIAL(info) << "Getting LOOT version."; - callback->Success("\"" + LootVersion::string() + "." + LootVersion::revision + "\""); + return "\"" + LootVersion::string() + "." + LootVersion::revision + "\""; } }; } diff --git a/src/gui/query/metadata_query.h b/src/gui/query/metadata_query.h index 1476ee8c..07175510 100644 --- a/src/gui/query/metadata_query.h +++ b/src/gui/query/metadata_query.h @@ -148,7 +148,7 @@ private: } Game& game_; - LanguageCode language_; + const LanguageCode language_; }; } diff --git a/src/gui/query/open_log_location_query.h b/src/gui/query/open_log_location_query.h index bb2d9575..978996ff 100644 --- a/src/gui/query/open_log_location_query.h +++ b/src/gui/query/open_log_location_query.h @@ -32,11 +32,11 @@ along with LOOT. If not, see namespace loot { class OpenLogLocationQuery : public Query { public: - void execute(CefRefPtr callback) { + std::string executeLogic() { BOOST_LOG_TRIVIAL(info) << "Opening LOOT local appdata folder."; OpenInDefaultApplication(LootPaths::getLogPath().parent_path()); - callback->Success(""); + return ""; } }; } diff --git a/src/gui/query/open_readme_query.h b/src/gui/query/open_readme_query.h index 2d247afd..542e6cd0 100644 --- a/src/gui/query/open_readme_query.h +++ b/src/gui/query/open_readme_query.h @@ -32,11 +32,11 @@ along with LOOT. If not, see namespace loot { class OpenReadmeQuery : public Query { public: - void execute(CefRefPtr callback) { + std::string executeLogic() { BOOST_LOG_TRIVIAL(info) << "Opening LOOT readme."; OpenInDefaultApplication(LootPaths::getReadmePath()); - callback->Success(""); + return ""; } }; } diff --git a/src/gui/query/query.h b/src/gui/query/query.h index a9ebd72f..b10861bd 100644 --- a/src/gui/query/query.h +++ b/src/gui/query/query.h @@ -28,12 +28,26 @@ along with LOOT. If not, see #include #include +#include "loot/error.h" + namespace loot { class Query : public CefBase { public: - virtual void execute(CefRefPtr callback) = 0; + void execute(CefRefPtr callback) { + try { + callback->Success(executeLogic()); + } catch (Error &e) { + BOOST_LOG_TRIVIAL(error) << e.what(); + callback->Failure(e.codeAsUnsignedInt(), e.what()); + } catch (std::exception &e) { + BOOST_LOG_TRIVIAL(error) << e.what(); + callback->Failure(-1, e.what()); + } + } protected: + virtual std::string executeLogic() = 0; + void sendProgressUpdate(CefRefPtr frame, const std::string& message) { BOOST_LOG_TRIVIAL(trace) << "Sending progress update: " << message; frame->ExecuteJavaScript("loot.Dialog.showProgress('" + message + "');", frame->GetURL(), 0); diff --git a/src/gui/query/redate_plugins_query.h b/src/gui/query/redate_plugins_query.h index 9c11bbd0..5afe886f 100644 --- a/src/gui/query/redate_plugins_query.h +++ b/src/gui/query/redate_plugins_query.h @@ -33,9 +33,9 @@ class RedatePluginsQuery : public Query { public: RedatePluginsQuery(LootState& state) : state_(state) {} - void execute(CefRefPtr callback) { + std::string executeLogic() { state_.getCurrentGame().RedatePlugins(); - callback->Success(""); + return ""; } private: diff --git a/src/gui/query/save_filter_state_query.h b/src/gui/query/save_filter_state_query.h index 84737e15..fb2f8b20 100644 --- a/src/gui/query/save_filter_state_query.h +++ b/src/gui/query/save_filter_state_query.h @@ -38,10 +38,10 @@ public: bool enabled) : state_(state), filterId_(filterId), enabled_(enabled) {} - void execute(CefRefPtr callback) { + std::string executeLogic() { BOOST_LOG_TRIVIAL(trace) << "Saving filter states."; state_.storeFilterState(filterId_, enabled_); - callback->Success(""); + return ""; } private: diff --git a/src/gui/query/sort_plugins_query.h b/src/gui/query/sort_plugins_query.h index 8ecdb5d0..d3c17f56 100644 --- a/src/gui/query/sort_plugins_query.h +++ b/src/gui/query/sort_plugins_query.h @@ -39,7 +39,7 @@ public: state_(state), frame_(frame) {} - void execute(CefRefPtr callback) { + std::string executeLogic() { BOOST_LOG_TRIVIAL(info) << "Beginning sorting operation."; // Always reload all the plugins. @@ -52,11 +52,13 @@ public: if ((state_.getCurrentGame().Type() == GameType::tes5 || state_.getCurrentGame().Type() == GameType::fo4)) applyUnchangedLoadOrder(plugins); - callback->Success(generateJsonResponse(plugins)); + std::string json = generateJsonResponse(plugins); // plugins will be empty if there was a sorting error. if (!plugins.empty()) state_.incrementUnappliedChangeCounter(); + + return json; } private: @@ -78,7 +80,7 @@ private: } void applyUnchangedLoadOrder(const std::vector& plugins) { - if (!equal(begin(plugins), end(plugins), begin(state_.getCurrentGame().GetLoadOrder()))) + if (plugins.empty() || !equal(begin(plugins), end(plugins), begin(state_.getCurrentGame().GetLoadOrder()))) return; // Load order has not been changed, set it without asking for user input diff --git a/src/gui/query/update_masterlist_query.h b/src/gui/query/update_masterlist_query.h index 19bfbf28..6d786a27 100644 --- a/src/gui/query/update_masterlist_query.h +++ b/src/gui/query/update_masterlist_query.h @@ -36,16 +36,14 @@ public: MetadataQuery(state.getCurrentGame(), state.getLanguage().GetCode()), game_(state.getCurrentGame()) {} - void execute(CefRefPtr callback) { + std::string executeLogic() { BOOST_LOG_TRIVIAL(debug) << "Updating and parsing masterlist."; - if (!updateMasterlist()) { - callback->Success("null"); - return; - } + if (!updateMasterlist()) + return "null"; // Now regenerate the JS-side masterlist data if the masterlist was changed. - callback->Success(generateJsonResponse()); + return generateJsonResponse(); } private: diff --git a/src/gui/query_handler.cpp b/src/gui/query_handler.cpp index 94a5ec3d..bee78c5c 100644 --- a/src/gui/query_handler.cpp +++ b/src/gui/query_handler.cpp @@ -111,15 +111,7 @@ bool QueryHandler::OnQuery(CefRefPtr browser, if (!query) return false; - try { - CefPostTask(TID_FILE, base::Bind(&Query::execute, query, callback)); - } catch (Error &e) { - BOOST_LOG_TRIVIAL(error) << e.what(); - callback->Failure(e.codeAsUnsignedInt(), e.what()); - } catch (exception &e) { - BOOST_LOG_TRIVIAL(error) << e.what(); - callback->Failure(-1, e.what()); - } + CefPostTask(TID_FILE, base::Bind(&Query::execute, query, callback)); return true; }