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 /options | |
parent | ced9bbd4c1b7d3a3968dbf8b1731bc49ff9f9164 (diff) |
add complete(?) portable config support
Diffstat (limited to 'options')
-rw-r--r-- | options/group.cpp | 52 | ||||
-rw-r--r-- | options/group.hpp | 33 |
2 files changed, 63 insertions, 22 deletions
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); } }; |