diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2017-10-08 13:39:23 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2017-10-08 13:42:40 +0200 |
commit | 863b8cad6d95ed2492ac929066f843664a49998d (patch) | |
tree | 527a20e268234ac81b86d6e88506c3b847f5dc25 | |
parent | ced9bbd4c1b7d3a3968dbf8b1731bc49ff9f9164 (diff) |
add complete(?) portable config support
-rw-r--r-- | gui/main-window.cpp | 5 | ||||
-rw-r--r-- | gui/main.cpp | 6 | ||||
-rw-r--r-- | gui/options-dialog.cpp | 10 | ||||
-rw-r--r-- | gui/process_detector.cpp | 20 | ||||
-rw-r--r-- | options/group.cpp | 52 | ||||
-rw-r--r-- | options/group.hpp | 33 |
6 files changed, 90 insertions, 36 deletions
diff --git a/gui/main-window.cpp b/gui/main-window.cpp index e12b85a7..7791f520 100644 --- a/gui/main-window.cpp +++ b/gui/main-window.cpp @@ -873,6 +873,7 @@ bool MainWindow::start_in_tray() void MainWindow::set_profile_in_registry(const QString &profile) { - QSettings settings(OPENTRACK_ORG); - settings.setValue(OPENTRACK_CONFIG_FILENAME_KEY, profile); + group::with_global_settings_object([&](QSettings& s) { + s.setValue(OPENTRACK_CONFIG_FILENAME_KEY, profile); + }); } diff --git a/gui/main.cpp b/gui/main.cpp index d929c543..3739232f 100644 --- a/gui/main.cpp +++ b/gui/main.cpp @@ -167,9 +167,11 @@ main(int argc, char** argv) // QLocale::setDefault(QLocale("ru_RU")); // force i18n for testing - if (!QSettings(OPENTRACK_ORG).value("disable-translation", false).toBool()) + if (group::with_global_settings_object([&](QSettings& s) { + return !s.value("disable-translation", false).toBool(); + })) { - (void) t.load(QLocale(), "", "", QCoreApplication::applicationDirPath() + "/" OPENTRACK_I18N_PATH, ".qm"); + (void) t.load(QLocale(), "", "", OPENTRACK_BASE_PATH + "/" OPENTRACK_I18N_PATH, ".qm"); (void) QCoreApplication::installTranslator(&t); } diff --git a/gui/options-dialog.cpp b/gui/options-dialog.cpp index 7eb6f68d..c35a6b52 100644 --- a/gui/options-dialog.cpp +++ b/gui/options-dialog.cpp @@ -33,7 +33,10 @@ QString OptionsDialog::kopts_to_string(const key_opts& kopts) void OptionsDialog::set_disable_translation_state(bool value) { - QSettings(OPENTRACK_ORG).setValue("disable-translation", value); + group::with_global_settings_object([&](QSettings& s) + { + s.setValue("disable-translation", value); + }); } OptionsDialog::OptionsDialog(std::function<void(bool)> pause_keybindings) : @@ -88,7 +91,10 @@ OptionsDialog::OptionsDialog(std::function<void(bool)> pause_keybindings) : tie_setting(main.neck_enable, ui.neck_enable); - ui.disable_translation->setChecked(QSettings(OPENTRACK_ORG).value("disable-translation", false).toBool()); + const bool is_translation_disabled = group::with_global_settings_object([] (QSettings& s) { + return s.value("disable-translation", false).toBool(); + }); + ui.disable_translation->setChecked(is_translation_disabled); struct tmp { diff --git a/gui/process_detector.cpp b/gui/process_detector.cpp index e45c0215..b1ad541d 100644 --- a/gui/process_detector.cpp +++ b/gui/process_detector.cpp @@ -19,26 +19,30 @@ void settings::set_game_list(const QString &game_list) { - QSettings settings(OPENTRACK_ORG); - settings.setValue("executable-list", game_list); + group::with_global_settings_object([&](QSettings& settings) { + settings.setValue("executable-list", game_list); + }); } QString settings::get_game_list() { - QSettings settings(OPENTRACK_ORG); - return settings.value("executable-list").toString(); + return group::with_global_settings_object([&](QSettings& settings) { + return settings.value("executable-list").toString(); + }); } bool settings::is_enabled() { - QSettings settings(OPENTRACK_ORG); - return settings.value("executable-detector-enabled", false).toBool(); + return group::with_global_settings_object([&](QSettings& settings) { + return settings.value("executable-detector-enabled", false).toBool(); + }); } void settings::set_is_enabled(bool enabled) { - QSettings settings(OPENTRACK_ORG); - settings.setValue("executable-detector-enabled", enabled); + group::with_global_settings_object([&](QSettings& settings) { + settings.setValue("executable-detector-enabled", enabled); + }); } QHash<QString, QString> settings::split_process_names() diff --git a/options/group.cpp b/options/group.cpp index 545ea99f..964ad791 100644 --- a/options/group.cpp +++ b/options/group.cpp @@ -107,11 +107,12 @@ QString group::ini_directory() QString group::ini_filename() { - QSettings settings(OPENTRACK_ORG); - const QString ret = settings.value(OPENTRACK_CONFIG_FILENAME_KEY, OPENTRACK_DEFAULT_CONFIG).toString(); - if (ret.size() == 0) - return OPENTRACK_DEFAULT_CONFIG; - return ret; + return with_global_settings_object([&](QSettings& settings) { + const QString ret = settings.value(OPENTRACK_CONFIG_FILENAME_KEY, OPENTRACK_DEFAULT_CONFIG).toString(); + if (ret.size() == 0) + return QStringLiteral(OPENTRACK_DEFAULT_CONFIG); + return ret; + }); } QString group::ini_pathname() @@ -145,11 +146,17 @@ void group::mark_ini_modified() } QString group::cur_ini_pathname; + std::shared_ptr<QSettings> group::cur_ini; QMutex group::cur_ini_mtx(QMutex::Recursive); int group::ini_refcount = 0; bool group::ini_modifiedp = false; +std::shared_ptr<QSettings> group::cur_global_ini; +QMutex group::global_ini_mtx(QMutex::Recursive); +int group::global_ini_refcount = 0; +bool group::global_ini_modifiedp = false; + std::shared_ptr<QSettings> group::cur_settings_object() { const QString pathname = ini_pathname(); @@ -157,8 +164,6 @@ std::shared_ptr<QSettings> group::cur_settings_object() if (pathname.isEmpty()) return std::make_shared<QSettings>(); - QMutexLocker l(&cur_ini_mtx); - if (pathname != cur_ini_pathname) { cur_ini = std::make_shared<QSettings>(pathname, QSettings::IniFormat); @@ -168,22 +173,39 @@ std::shared_ptr<QSettings> group::cur_settings_object() return cur_ini; } +std::shared_ptr<QSettings> group::cur_global_settings_object() +{ + if (cur_global_ini) + return cur_global_ini; + + if (!is_portable_installation()) + cur_global_ini = std::make_shared<QSettings>(OPENTRACK_ORG); + else + { + static const QString pathname = OPENTRACK_BASE_PATH + QStringLiteral("/globals.ini"); + cur_global_ini = std::make_shared<QSettings>(pathname, QSettings::IniFormat); + } + + return cur_global_ini; +} + +never_inline group::saver_::~saver_() { - if (--ini_refcount == 0 && ini_modifiedp) + if (--refcount == 0 && modifiedp) { - ini_modifiedp = false; - static Timer t; - const double tm = t.elapsed_seconds(); - qDebug() << QStringLiteral("%1.%2").arg(int(tm)).arg(int(std::fmod(tm, 1.)*10)).toLatin1().data() - << "saving .ini file" << cur_ini_pathname; + modifiedp = false; s.sync(); + if (s.status() != QSettings::NoError) + qDebug() << "error with .ini file" << s.fileName() << s.status(); } } -group::saver_::saver_(QSettings& s, QMutex& mtx) : s(s), mtx(mtx), lck(&mtx) +never_inline +group::saver_::saver_(QSettings& s, int& refcount, bool& modifiedp) : + s(s), refcount(refcount), modifiedp(modifiedp) { - ini_refcount++; + refcount++; } } // ns options diff --git a/options/group.hpp b/options/group.hpp index c07fe576..b1aad156 100644 --- a/options/group.hpp +++ b/options/group.hpp @@ -23,20 +23,28 @@ class OTR_OPTIONS_EXPORT group final QString name; static QString cur_ini_pathname; + static std::shared_ptr<QSettings> cur_ini; - static QMutex cur_ini_mtx; static int ini_refcount; static bool ini_modifiedp; + static QMutex cur_ini_mtx; + + static std::shared_ptr<QSettings> cur_global_ini; + static int global_ini_refcount; + static bool global_ini_modifiedp; + static QMutex global_ini_mtx; + struct OTR_OPTIONS_EXPORT saver_ final { QSettings& s; - QMutex& mtx; - QMutexLocker lck; + int& refcount; + bool& modifiedp; - ~saver_(); - saver_(QSettings& s, QMutex&); + never_inline ~saver_(); + never_inline saver_(QSettings& s, int& refcount, bool& modifiedp); }; static std::shared_ptr<QSettings> cur_settings_object(); + static std::shared_ptr<QSettings> cur_global_settings_object(); public: std::map<QString, QVariant> kvs; @@ -67,9 +75,20 @@ public: never_inline static auto with_settings_object(F&& fun) { - saver_ saver { *cur_settings_object(), cur_ini_mtx }; + QMutexLocker l(&cur_ini_mtx); + saver_ saver { *cur_settings_object(), ini_refcount, ini_modifiedp }; + + return fun(saver.s); + } + + template<typename F> + static auto with_global_settings_object(F&& fun) + { + QMutexLocker l(&global_ini_mtx); + saver_ saver { *cur_global_settings_object(), global_ini_refcount, ini_modifiedp }; + global_ini_modifiedp = true; - return fun(static_cast<QSettings&>(saver.s)); + return fun(saver.s); } }; |