diff options
author | jan Iversen <jani@apache.org> | 2018-08-15 13:26:09 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2018-08-25 11:49:47 -0700 |
commit | 69f21d9aedcea8713cf5af6463ebacba5832162e (patch) | |
tree | 7a345586bd1a4caa311e99679b766681e03aeb41 /core | |
parent | c3a54826a2ddb17ac00ec4fdbbd18eea8a0ecbe1 (diff) | |
download | subsurface-69f21d9aedcea8713cf5af6463ebacba5832162e.tar.gz |
core: make methods in qPrefPrivate static
Small cleanup, using static methods is simpler and faster
Added propSetValue and propValue instead of exposing setting
variable.
Signed-off-by: Jan Iversen <jani@apache.org>
Diffstat (limited to 'core')
-rw-r--r-- | core/settings/qPrefCloudStorage.cpp | 14 | ||||
-rw-r--r-- | core/settings/qPrefDisplay.cpp | 13 | ||||
-rw-r--r-- | core/settings/qPrefFacebook.cpp | 6 | ||||
-rw-r--r-- | core/settings/qPrefGeneral.cpp | 4 | ||||
-rw-r--r-- | core/settings/qPrefGeocoding.cpp | 12 | ||||
-rw-r--r-- | core/settings/qPrefPrivate.cpp | 27 | ||||
-rw-r--r-- | core/settings/qPrefPrivate.h | 61 |
7 files changed, 64 insertions, 73 deletions
diff --git a/core/settings/qPrefCloudStorage.cpp b/core/settings/qPrefCloudStorage.cpp index d35b3b61c..ddb370630 100644 --- a/core/settings/qPrefCloudStorage.cpp +++ b/core/settings/qPrefCloudStorage.cpp @@ -46,9 +46,9 @@ void qPrefCloudStorage::set_cloud_base_url(const QString &value) void qPrefCloudStorage::disk_cloud_base_url(bool doSync) { if (doSync) { - qPrefPrivate::instance()->setting.setValue(group + "/cloud_base_url", prefs.cloud_base_url); + qPrefPrivate::propSetValue(group + "/cloud_base_url", prefs.cloud_base_url); } else { - prefs.cloud_base_url = copy_qstring(qPrefPrivate::instance()->setting.value(group + "/cloud_base_url", default_prefs.cloud_base_url).toString()); + prefs.cloud_base_url = copy_qstring(qPrefPrivate::propValue(group + "/cloud_base_url", default_prefs.cloud_base_url).toString()); qPrefPrivate::copy_txt(&prefs.cloud_git_url, QString(prefs.cloud_base_url) + "/git"); } } @@ -80,9 +80,9 @@ void qPrefCloudStorage::disk_cloud_storage_password(bool doSync) { if (doSync) { if (prefs.save_password_local) - qPrefPrivate::instance()->setting.setValue(group + "/password", prefs.cloud_storage_password); + qPrefPrivate::propSetValue(group + "/password", prefs.cloud_storage_password); } else { - prefs.cloud_storage_password = copy_qstring(qPrefPrivate::instance()->setting.value(group + "/password", default_prefs.cloud_storage_password).toString()); + prefs.cloud_storage_password = copy_qstring(qPrefPrivate::propValue(group + "/password", default_prefs.cloud_storage_password).toString()); } } @@ -103,14 +103,14 @@ void qPrefCloudStorage::disk_userid(bool doSync) { if (doSync) { // always save in new position (part of cloud storage group) - qPrefPrivate::instance()->setting.setValue(group + "subsurface_webservice_uid", prefs.userid); + qPrefPrivate::propSetValue(group + "subsurface_webservice_uid", prefs.userid); } else { //WARNING: UserId was stored outside of any group. // try to read from new location, if it fails read from old location - prefs.userid = copy_qstring(qPrefPrivate::instance()->setting.value(group + "subsurface_webservice_uid", "NoUserIdHere").toString()); + prefs.userid = copy_qstring(qPrefPrivate::propValue(group + "subsurface_webservice_uid", "NoUserIdHere").toString()); if (QString(prefs.userid) == "NoUserIdHere") { const QString group = QStringLiteral(""); - prefs.userid = copy_qstring(qPrefPrivate::instance()->setting.value(group + "subsurface_webservice_uid", default_prefs.userid).toString()); + prefs.userid = copy_qstring(qPrefPrivate::propValue(group + "subsurface_webservice_uid", default_prefs.userid).toString()); } } } diff --git a/core/settings/qPrefDisplay.cpp b/core/settings/qPrefDisplay.cpp index 152f2fbfc..a75d1f48b 100644 --- a/core/settings/qPrefDisplay.cpp +++ b/core/settings/qPrefDisplay.cpp @@ -44,7 +44,7 @@ void qPrefDisplay::set_divelist_font(const QString &value) void qPrefDisplay::disk_divelist_font(bool doSync) { if (doSync) - qPrefPrivate::instance()->setting.setValue(group + "/divelist_font", prefs.divelist_font); + qPrefPrivate::propSetValue(group + "/divelist_font", prefs.divelist_font); else setCorrectFont(); } @@ -64,7 +64,7 @@ void qPrefDisplay::set_font_size(double value) void qPrefDisplay::disk_font_size(bool doSync) { if (doSync) - qPrefPrivate::instance()->setting.setValue(group + "/font_size", prefs.font_size); + qPrefPrivate::propSetValue(group + "/font_size", prefs.font_size); else setCorrectFont(); } @@ -78,18 +78,15 @@ HANDLE_PREFERENCE_TXT(Display, "/theme", theme); void qPrefDisplay::setCorrectFont() { - QSettings s; - QVariant v; - // get the font from the settings or our defaults // respect the system default font size if none is explicitly set - QFont defaultFont = s.value(group + "/divelist_font", prefs.divelist_font).value<QFont>(); + QFont defaultFont = qPrefPrivate::propValue(group + "/divelist_font", prefs.divelist_font).value<QFont>(); if (IS_FP_SAME(system_divelist_default_font_size, -1.0)) { prefs.font_size = qApp->font().pointSizeF(); system_divelist_default_font_size = prefs.font_size; // this way we don't save it on exit } - prefs.font_size = s.value(group + "/font_size", prefs.font_size).toFloat(); + prefs.font_size = qPrefPrivate::propValue(group + "/font_size", prefs.font_size).toFloat(); // painful effort to ignore previous default fonts on Windows - ridiculous QString fontName = defaultFont.toString(); if (fontName.contains(",")) @@ -103,5 +100,5 @@ void qPrefDisplay::setCorrectFont() defaultFont.setPointSizeF(prefs.font_size); qApp->setFont(defaultFont); - prefs.display_invalid_dives = qPrefPrivate::instance()->setting.value(group + "/displayinvalid", default_prefs.display_invalid_dives).toBool(); + prefs.display_invalid_dives = qPrefPrivate::propValue(group + "/displayinvalid", default_prefs.display_invalid_dives).toBool(); } diff --git a/core/settings/qPrefFacebook.cpp b/core/settings/qPrefFacebook.cpp index 0c37b2c6d..8728313ab 100644 --- a/core/settings/qPrefFacebook.cpp +++ b/core/settings/qPrefFacebook.cpp @@ -21,7 +21,7 @@ void qPrefFacebook::loadSync(bool doSync) void qPrefFacebook::set_access_token(const QString &value) { if (value != prefs.facebook.access_token) { - qPrefPrivate::instance()->copy_txt(&prefs.facebook.access_token, value); + qPrefPrivate::copy_txt(&prefs.facebook.access_token, value); emit instance()->access_token_changed(value); } } @@ -29,7 +29,7 @@ void qPrefFacebook::set_access_token(const QString &value) void qPrefFacebook::set_album_id(const QString &value) { if (value != prefs.facebook.album_id) { - qPrefPrivate::instance()->copy_txt(&prefs.facebook.album_id, value); + qPrefPrivate::copy_txt(&prefs.facebook.album_id, value); emit instance()->album_id_changed(value); } } @@ -37,7 +37,7 @@ void qPrefFacebook::set_album_id(const QString &value) void qPrefFacebook::set_user_id(const QString &value) { if (value != prefs.facebook.user_id) { - qPrefPrivate::instance()->copy_txt(&prefs.facebook.user_id, value); + qPrefPrivate::copy_txt(&prefs.facebook.user_id, value); emit instance()->access_token_changed(value); } } diff --git a/core/settings/qPrefGeneral.cpp b/core/settings/qPrefGeneral.cpp index 81a3eb0fe..b092232f5 100644 --- a/core/settings/qPrefGeneral.cpp +++ b/core/settings/qPrefGeneral.cpp @@ -57,9 +57,9 @@ void qPrefGeneral::set_default_file_behavior(enum def_file_behavior value) void qPrefGeneral::disk_default_file_behavior(bool doSync) { if (doSync) { - qPrefPrivate::instance()->setting.setValue(group + "/default_file_behavior", prefs.default_file_behavior); + qPrefPrivate::propSetValue(group + "/default_file_behavior", prefs.default_file_behavior); } else { - prefs.default_file_behavior = (enum def_file_behavior)qPrefPrivate::instance()->setting.value(group + "/default_file_behavior", default_prefs.default_file_behavior).toInt(); + prefs.default_file_behavior = (enum def_file_behavior)qPrefPrivate::propValue(group + "/default_file_behavior", default_prefs.default_file_behavior).toInt(); if (prefs.default_file_behavior == UNDEFINED_DEFAULT_FILE) // undefined, so check if there's a filename set and // use that, otherwise go with no default file diff --git a/core/settings/qPrefGeocoding.cpp b/core/settings/qPrefGeocoding.cpp index 7f272b572..7bb043e0f 100644 --- a/core/settings/qPrefGeocoding.cpp +++ b/core/settings/qPrefGeocoding.cpp @@ -32,9 +32,9 @@ void qPrefGeocoding::set_first_taxonomy_category(taxonomy_category value) void qPrefGeocoding::disk_first_taxonomy_category(bool doSync) { if (doSync) - qPrefPrivate::instance()->setting.setValue(group + "/cat0", prefs.geocoding.category[0]); + qPrefPrivate::propSetValue(group + "/cat0", prefs.geocoding.category[0]); else - prefs.geocoding.category[0] = (enum taxonomy_category)qPrefPrivate::instance()->setting.value(group + "/cat0", default_prefs.geocoding.category[0]).toInt(); + prefs.geocoding.category[0] = (enum taxonomy_category)qPrefPrivate::propValue(group + "/cat0", default_prefs.geocoding.category[0]).toInt(); } @@ -49,9 +49,9 @@ void qPrefGeocoding::set_second_taxonomy_category(taxonomy_category value) void qPrefGeocoding::disk_second_taxonomy_category(bool doSync) { if (doSync) - qPrefPrivate::instance()->setting.setValue(group + "/cat1", prefs.geocoding.category[1]); + qPrefPrivate::propSetValue(group + "/cat1", prefs.geocoding.category[1]); else - prefs.geocoding.category[1] = (enum taxonomy_category)qPrefPrivate::instance()->setting.value(group + "/cat1", default_prefs.geocoding.category[1]).toInt(); + prefs.geocoding.category[1] = (enum taxonomy_category)qPrefPrivate::propValue(group + "/cat1", default_prefs.geocoding.category[1]).toInt(); } @@ -66,7 +66,7 @@ void qPrefGeocoding::set_third_taxonomy_category(taxonomy_category value) void qPrefGeocoding::disk_third_taxonomy_category(bool doSync) { if (doSync) - qPrefPrivate::instance()->setting.setValue(group + "/cat2", prefs.geocoding.category[2]); + qPrefPrivate::propSetValue(group + "/cat2", prefs.geocoding.category[2]); else - prefs.geocoding.category[2] = (enum taxonomy_category)qPrefPrivate::instance()->setting.value(group + "/cat2", default_prefs.geocoding.category[2]).toInt(); + prefs.geocoding.category[2] = (enum taxonomy_category)qPrefPrivate::propValue(group + "/cat2", default_prefs.geocoding.category[2]).toInt(); } diff --git a/core/settings/qPrefPrivate.cpp b/core/settings/qPrefPrivate.cpp index 0275791cf..ff335a94f 100644 --- a/core/settings/qPrefPrivate.cpp +++ b/core/settings/qPrefPrivate.cpp @@ -1,17 +1,30 @@ // SPDX-License-Identifier: GPL-2.0 #include "qPrefPrivate.h" -qPrefPrivate::qPrefPrivate(QObject *parent) : QObject(parent) +#include <QSettings> + +void qPrefPrivate::copy_txt(const char **name, const QString &string) { + free((void *)*name); + *name = copy_qstring(string); } -qPrefPrivate *qPrefPrivate::instance() + +void qPrefPrivate::propSetValue(const QString &key, const QVariant &value) { - static qPrefPrivate *self = new qPrefPrivate; - return self; + // REMARK: making s static (which would be logical) does NOT work + // because it gets initialized too early. + // Having it as a local variable is light weight, because it is an + // interface class. + QSettings s; + s.setValue(key, value); } -void qPrefPrivate::copy_txt(const char **name, const QString &string) +QVariant qPrefPrivate::propValue(const QString &key, const QVariant &defaultValue) { - free((void *)*name); - *name = copy_qstring(string); + // REMARK: making s static (which would be logical) does NOT work + // because it gets initialized too early. + // Having it as a local variable is light weight, because it is an + // interface class. + QSettings s; + return s.value(key, defaultValue); } diff --git a/core/settings/qPrefPrivate.h b/core/settings/qPrefPrivate.h index 623bed2c1..3242b5fdc 100644 --- a/core/settings/qPrefPrivate.h +++ b/core/settings/qPrefPrivate.h @@ -6,39 +6,20 @@ #include "core/qthelper.h" #include "qPref.h" #include <QObject> -#include <QSettings> #include <QVariant> // implementation class of the interface classes -class qPrefPrivate : public QObject { - Q_OBJECT +class qPrefPrivate { public: - friend class qPrefAnimations; - friend class qPrefCloudStorage; - friend class qPrefDisplay; - friend class qPrefDiveComputer; - friend class qPrefDivePlanner; - friend class qPrefFacebook; - friend class qPrefGeneral; - friend class qPrefGeocoding; - friend class qPrefLanguage; - friend class qPrefLocationService; - friend class qPrefPartialPressureGas; - friend class qPrefProxy; - friend class qPrefTechnicalDetails; - friend class qPrefUnits; - friend class qPrefUpdateManager; - -private: - static qPrefPrivate *instance(); - - QSettings setting; - // Helper functions static void copy_txt(const char **name, const QString &string); - qPrefPrivate(QObject *parent = NULL); + static void propSetValue(const QString &key, const QVariant &value); + static QVariant propValue(const QString &key, const QVariant &defaultValue); + +private: + qPrefPrivate() {} }; @@ -47,9 +28,9 @@ private: void qPref##usegroup::disk_##field(bool doSync) \ { \ if (doSync) \ - qPrefPrivate::instance()->setting.setValue(group + name, prefs.usestruct field); \ + qPrefPrivate::propSetValue(group + name, prefs.usestruct field); \ else \ - prefs.usestruct field = qPrefPrivate::instance()->setting.value(group + name, default_prefs.usestruct field).toBool(); \ + prefs.usestruct field = qPrefPrivate::propValue(group + name, default_prefs.usestruct field).toBool(); \ } #define DISK_LOADSYNC_BOOL(usegroup, name, field) \ DISK_LOADSYNC_BOOL_EXT(usegroup, name, field, ) @@ -58,9 +39,9 @@ private: void qPref##usegroup::disk_##field(bool doSync) \ { \ if (doSync) \ - qPrefPrivate::instance()->setting.setValue(group + name, prefs.usestruct field); \ + qPrefPrivate::propSetValue(group + name, prefs.usestruct field); \ else \ - prefs.usestruct field = qPrefPrivate::instance()->setting.value(group + name, default_prefs.usestruct field).toDouble(); \ + prefs.usestruct field = qPrefPrivate::propValue(group + name, default_prefs.usestruct field).toDouble(); \ } #define DISK_LOADSYNC_DOUBLE(usegroup, name, field) \ DISK_LOADSYNC_DOUBLE_EXT(usegroup, name, field, ) @@ -69,9 +50,9 @@ private: void qPref##usegroup::disk_##field(bool doSync) \ { \ if (doSync) \ - qPrefPrivate::instance()->setting.setValue(group + name, prefs.usestruct field); \ + qPrefPrivate::propSetValue(group + name, prefs.usestruct field); \ else \ - prefs.usestruct field = (enum type)qPrefPrivate::instance()->setting.value(group + name, default_prefs.usestruct field).toInt(); \ + prefs.usestruct field = (enum type)qPrefPrivate::propValue(group + name, default_prefs.usestruct field).toInt(); \ } #define DISK_LOADSYNC_ENUM(usegroup, name, type, field) \ DISK_LOADSYNC_ENUM_EXT(usegroup, name, type, field, ) @@ -80,9 +61,9 @@ private: void qPref##usegroup::disk_##field(bool doSync) \ { \ if (doSync) \ - qPrefPrivate::instance()->setting.setValue(group + name, prefs.usestruct field); \ + qPrefPrivate::propSetValue(group + name, prefs.usestruct field); \ else \ - prefs.usestruct field = qPrefPrivate::instance()->setting.value(group + name, default_prefs.usestruct field).toInt(); \ + prefs.usestruct field = qPrefPrivate::propValue(group + name, default_prefs.usestruct field).toInt(); \ } #define DISK_LOADSYNC_INT(usegroup, name, field) \ DISK_LOADSYNC_INT_EXT(usegroup, name, field, ) @@ -91,9 +72,9 @@ private: void qPref##usegroup::disk_##field(bool doSync) \ { \ if (doSync) \ - qPrefPrivate::instance()->setting.setValue(group + name, prefs.usestruct field); \ + qPrefPrivate::propSetValue(group + name, prefs.usestruct field); \ else \ - prefs.usestruct field = qPrefPrivate::instance()->setting.value(group + name, defval).toInt(); \ + prefs.usestruct field = qPrefPrivate::propValue(group + name, defval).toInt(); \ } #define DISK_LOADSYNC_INT_DEF(usegroup, name, field, defval) \ DISK_LOADSYNC_INT_DEF_EXT(usegroup, name, field, defval, ) @@ -102,9 +83,9 @@ private: void qPref##usegroup::disk_##field(bool doSync) \ { \ if (doSync) \ - qPrefPrivate::instance()->setting.setValue(group + name, prefs.usestruct field . var); \ + qPrefPrivate::propSetValue(group + name, prefs.usestruct field . var); \ else \ - prefs.usestruct field . var = qPrefPrivate::instance()->setting.value(group + name, default_prefs.usestruct field . var).toInt(); \ + prefs.usestruct field . var = qPrefPrivate::propValue(group + name, default_prefs.usestruct field . var).toInt(); \ } #define DISK_LOADSYNC_STRUCT(usegroup, name, field, var) \ DISK_LOADSYNC_STRUCT_EXT(usegroup, name, field, var, ) @@ -113,9 +94,9 @@ private: void qPref##usegroup::disk_##field(bool doSync) \ { \ if (doSync) \ - qPrefPrivate::instance()->setting.setValue(group + name, prefs.usestruct field); \ + qPrefPrivate::propSetValue(group + name, prefs.usestruct field); \ else \ - prefs.usestruct field = copy_qstring(qPrefPrivate::instance()->setting.value(group + name, default_prefs.usestruct field).toString()); \ + prefs.usestruct field = copy_qstring(qPrefPrivate::propValue(group + name, default_prefs.usestruct field).toString()); \ } #define DISK_LOADSYNC_TXT(usegroup, name, field) \ DISK_LOADSYNC_TXT_EXT(usegroup, name, field, ) @@ -185,7 +166,7 @@ private: void qPref##usegroup::set_##field(const QString &value) \ { \ if (value != prefs.usestruct field) { \ - qPrefPrivate::instance()->copy_txt(&prefs.usestruct field, value); \ + qPrefPrivate::copy_txt(&prefs.usestruct field, value); \ disk_##field(true); \ emit qPref##usegroup::instance()->field##_changed(value); \ } \ |