Use QSettings in AppConfig

Some more changes

Changes

Fix review items

Rename init-settings to restore-native-settings

Restore application name (thanks @churik !)

Remove org name/domain setting for desktop

Change organization name to include domain

Re-use default values in :desktop/desktop

Signed-off-by: Vitaliy Vlasov <siphiuel@gmail.com>
This commit is contained in:
Vitaliy Vlasov 2018-12-18 19:30:26 +02:00
parent e122ebdb84
commit e40e495e11
No known key found for this signature in database
GPG Key ID: A7D57C347F2B2964
12 changed files with 89 additions and 47 deletions

View File

@ -2,6 +2,7 @@
#include <QLoggingCategory> #include <QLoggingCategory>
#include <QSettings>
const QStringList loggingCategories = const QStringList loggingCategories =
{"UIManager", {"UIManager",
@ -14,25 +15,50 @@ const QStringList loggingCategories =
"RCTStatus", "RCTStatus",
"jsserver", "jsserver",
"status"}; "status"};
const QString SETTINGS_GROUP_NAME = "im/status";
const QString AppConfig::LOGGING_ENABLED = "logging_enabled";
Q_LOGGING_CATEGORY(APPCONFIG, "AppConfig") Q_LOGGING_CATEGORY(APPCONFIG, "AppConfig")
AppConfig AppConfig::appConfig; AppConfig AppConfig::appConfig;
AppConfig::AppConfig() { AppConfig::AppConfig()
: settings("Status.im", "StatusDesktop") {
settings.beginGroup(SETTINGS_GROUP_NAME);
// Set default values
if (settings.value(LOGGING_ENABLED).isNull()) {
settings.setValue(LOGGING_ENABLED, false);
}
QStringList keys = settings.allKeys();
for (int i = 0; i < keys.size(); ++i) {
processFx(keys[i], settings.value(keys[i]));
}
} }
AppConfig& AppConfig::inst() { AppConfig& AppConfig::inst() {
return appConfig; return appConfig;
} }
bool AppConfig::getLoggingEnabled() const { QVariant AppConfig::getValue(const QString& name) const {
return loggingEnabled; return settings.value(name);
} }
void AppConfig::setLoggingEnabled(bool enabled) { void AppConfig::setValue(const QString& name, const QVariant& value) {
//qCDebug(APPCONFIG) << "### appconfig setLoggingEnabled " << enabled; processFx(name, value);
settings.setValue(name, value);
}
// This fn is for processing side-effects of a particular value
void AppConfig::processFx(const QString& name, const QVariant& value) const {
//qCDebug(APPCONFIG) << "### processFx group" << settings.group() << " " << name << ": " << value;
if (name == LOGGING_ENABLED) {
bool enabled = value.toBool();
//qCDebug(APPCONFIG) << "### processFx" << name << ": " << value << ": " << enabled;
QLoggingCategory::setFilterRules(getLoggingFilterRules(enabled)); QLoggingCategory::setFilterRules(getLoggingFilterRules(enabled));
this->loggingEnabled = enabled; }
} }
QString AppConfig::getLoggingFilterRules(bool enabled) const { QString AppConfig::getLoggingFilterRules(bool enabled) const {
@ -47,3 +73,4 @@ QString AppConfig::getLoggingFilterRules(bool enabled) const {
return filterRules; return filterRules;
} }
} }

View File

@ -2,6 +2,8 @@
#define APPCONFIG_H #define APPCONFIG_H
#include <QString> #include <QString>
#include <QVariant>
#include <QSettings>
// This class is intended to store app configuration // This class is intended to store app configuration
// modifiable from JS side // modifiable from JS side
@ -12,15 +14,18 @@ public:
static AppConfig& inst(); static AppConfig& inst();
bool getLoggingEnabled() const; QVariant getValue(const QString& name) const;
void setLoggingEnabled(bool enable); void setValue(const QString& name, const QVariant& value);
const static QString LOGGING_ENABLED;
private: private:
AppConfig(); AppConfig();
bool loggingEnabled = false;
static AppConfig appConfig; static AppConfig appConfig;
QSettings settings;
QString getLoggingFilterRules(bool enabled) const; QString getLoggingFilterRules(bool enabled) const;
void processFx(const QString& name, const QVariant& value) const;
}; };
#endif // APPCONFIG_H #endif // APPCONFIG_H

View File

@ -1,4 +1,3 @@
/** /**
* Copyright (C) 2016, Canonical Ltd. * Copyright (C) 2016, Canonical Ltd.
* All rights reserved. * All rights reserved.
@ -240,6 +239,9 @@ int main(int argc, char **argv) {
QCoreApplication::setApplicationName("Status"); QCoreApplication::setApplicationName("Status");
// Init AppConfig
AppConfig::inst().getValue(AppConfig::LOGGING_ENABLED);
QString appPath = QCoreApplication::applicationDirPath(); QString appPath = QCoreApplication::applicationDirPath();
QString dataStoragePath = getDataStoragePath(); QString dataStoragePath = getDataStoragePath();
#ifdef BUILD_FOR_BUNDLE #ifdef BUILD_FOR_BUNDLE
@ -263,7 +265,6 @@ int main(int argc, char **argv) {
} }
//qCDebug(STATUS) << "###STATUS_NO_LOGGING"; //qCDebug(STATUS) << "###STATUS_NO_LOGGING";
AppConfig::inst().setLoggingEnabled(false);
#ifdef BUILD_FOR_BUNDLE #ifdef BUILD_FOR_BUNDLE
@ -558,3 +559,4 @@ void saveMessage(QtMsgType type, const QMessageLogContext &context,
} }
#include "main.moc" #include "main.moc"

View File

@ -33,12 +33,14 @@ QList<ModuleMethod *> DesktopConfig::methodsToExport() {
QVariantMap DesktopConfig::constantsToExport() { return QVariantMap(); } QVariantMap DesktopConfig::constantsToExport() { return QVariantMap(); }
void DesktopConfig::getLoggingEnabled(double callback) { void DesktopConfig::getValue(const QString& name, double callback) {
bridge->invokePromiseCallback(callback, QVariantList{AppConfig::inst().getLoggingEnabled()}); //qCDebug(DESKTOPCONFIG) << "### getValue" << name;
bridge->invokePromiseCallback(callback, QVariantList{AppConfig::inst().getValue(name)});
} }
void DesktopConfig::setLoggingEnabled(bool enable) { void DesktopConfig::setValue(const QString& name, const QVariant& value) {
//qCDebug(DESKTOPCONFIG) << "### setLoggingEnabled " << enable; //qCDebug(DESKTOPCONFIG) << "### setValue" << name << ": " << value;
AppConfig::inst().setLoggingEnabled(enable); AppConfig::inst().setValue(name, value);
} }

View File

@ -2,7 +2,7 @@
#define DESKTOPCONFIG_H #define DESKTOPCONFIG_H
#include "moduleinterface.h" #include "moduleinterface.h"
#include <QVariant>
#include <QLoggingCategory> #include <QLoggingCategory>
Q_DECLARE_LOGGING_CATEGORY(CONFIG) Q_DECLARE_LOGGING_CATEGORY(CONFIG)
@ -22,8 +22,8 @@ public:
QList<ModuleMethod*> methodsToExport() override; QList<ModuleMethod*> methodsToExport() override;
QVariantMap constantsToExport() override; QVariantMap constantsToExport() override;
Q_INVOKABLE void getLoggingEnabled(double callback); Q_INVOKABLE void getValue(const QString& name, double callback);
Q_INVOKABLE void setLoggingEnabled(bool enable); Q_INVOKABLE void setValue(const QString& name, const QVariant& value);
private: private:
Bridge* bridge = nullptr; Bridge* bridge = nullptr;

View File

@ -4,15 +4,12 @@ const NativeModules = require('react-native').NativeModules;
class DesktopConfig { class DesktopConfig {
static getLoggingEnabled(callbackFn) { static getValue(name, callbackFn) {
NativeModules.DesktopConfigManager.getLoggingEnabled( NativeModules.DesktopConfigManager.getValue(name, callbackFn);
(enabled) => {
callbackFn(enabled);
});
} }
static setLoggingEnabled(enabled) { static setValue(name, value) {
NativeModules.DesktopConfigManager.setLoggingEnabled(enabled); NativeModules.DesktopConfigManager.setValue(name, value);
} }
} }

View File

@ -102,10 +102,6 @@
#(re-frame/dispatch #(re-frame/dispatch
[:web3/fetch-node-version-callback %])]} [:web3/fetch-node-version-callback %])]}
(protocol/initialize-protocol address) (protocol/initialize-protocol address)
#(when platform/desktop?
(let [logging-enabled (get-in db [:account/account :settings :logging-enabled])]
(log/debug "### user-login-callback .setLoggingEnabled" logging-enabled)
(.setLoggingEnabled rn-dependencies/desktop-config logging-enabled)))
#(when-not platform/desktop? #(when-not platform/desktop?
(initialize-wallet %))) (initialize-wallet %)))
(account-and-db-password-do-not-match cofx error))))) (account-and-db-password-do-not-match cofx error)))))

View File

@ -5,6 +5,7 @@
[status-im.accounts.login.core :as accounts.login] [status-im.accounts.login.core :as accounts.login]
[status-im.accounts.update.core :as accounts.update] [status-im.accounts.update.core :as accounts.update]
[status-im.constants :as constants] [status-im.constants :as constants]
[status-im.react-native.js-dependencies :as rn-dependencies]
[status-im.data-store.core :as data-store] [status-im.data-store.core :as data-store]
[status-im.data-store.realm.core :as realm] [status-im.data-store.realm.core :as realm]
[status-im.extensions.registry :as extensions.registry] [status-im.extensions.registry :as extensions.registry]
@ -42,6 +43,11 @@
(log/warn "Could not decrypt database" error) (log/warn "Could not decrypt database" error)
(re-frame/dispatch [:init.callback/init-store-error encryption-key]))))) (re-frame/dispatch [:init.callback/init-store-error encryption-key])))))
(defn restore-native-settings! []
(when platform/desktop?
(.getValue rn-dependencies/desktop-config "logging_enabled"
#(re-frame/dispatch [:set-in [:desktop/desktop :logging-enabled] %1]))))
;; TODO (yenda) move keychain functions to dedicated namespace ;; TODO (yenda) move keychain functions to dedicated namespace
(defn reset-keychain! [] (defn reset-keychain! []
(.. (keychain/reset) (.. (keychain/reset)
@ -67,6 +73,7 @@
(fx/defn start-app [cofx] (fx/defn start-app [cofx]
(fx/merge cofx (fx/merge cofx
{:init/get-device-UUID nil {:init/get-device-UUID nil
:init/restore-native-settings nil
:ui/listen-to-window-dimensions-change nil :ui/listen-to-window-dimensions-change nil
:notifications/handle-initial-push-notification nil :notifications/handle-initial-push-notification nil
:network/listen-to-network-status nil :network/listen-to-network-status nil
@ -78,13 +85,14 @@
(fx/defn initialize-app-db (fx/defn initialize-app-db
"Initialize db to initial state" "Initialize db to initial state"
[{{:keys [status-module-initialized? view-id hardwallet [{{:keys [status-module-initialized? view-id hardwallet
initial-props initial-props desktop/desktop
network-status network peers-count peers-summary device-UUID] network-status network peers-count peers-summary device-UUID]
:node/keys [status] :node/keys [status]
:or {network (get app-db :network)}} :db}] :or {network (get app-db :network)}} :db}]
{:db (assoc app-db {:db (assoc app-db
:contacts/contacts {} :contacts/contacts {}
:initial-props initial-props :initial-props initial-props
:desktop/desktop (merge desktop (:desktop/desktop app-db))
:network-status network-status :network-status network-status
:peers-count (or peers-count 0) :peers-count (or peers-count 0)
:peers-summary (or peers-summary []) :peers-summary (or peers-summary [])
@ -149,6 +157,7 @@
(let [{:universal-links/keys [url] (let [{:universal-links/keys [url]
:keys [accounts/accounts accounts/create networks/networks network :keys [accounts/accounts accounts/create networks/networks network
network-status peers-count peers-summary view-id navigation-stack network-status peers-count peers-summary view-id navigation-stack
desktop/desktop
status-module-initialized? device-UUID semaphores accounts/login] status-module-initialized? device-UUID semaphores accounts/login]
:node/keys [status on-ready] :node/keys [status on-ready]
:or {network (get app-db :network)}} db :or {network (get app-db :network)}} db
@ -162,6 +171,7 @@
:node/status status :node/status status
:node/on-ready on-ready :node/on-ready on-ready
:accounts/create create :accounts/create create
:desktop/desktop (merge desktop (:desktop/desktop app-db))
:networks/networks networks :networks/networks networks
:account/account current-account :account/account current-account
:accounts/login login :accounts/login login
@ -215,6 +225,10 @@
:init/init-store :init/init-store
init-store!) init-store!)
(re-frame/reg-fx
:init/restore-native-settings
restore-native-settings!)
(re-frame/reg-fx (re-frame/reg-fx
:init/status-module-initialized :init/status-module-initialized
status/module-initialized!) status/module-initialized!)
@ -231,3 +245,4 @@
(re-frame/reg-fx (re-frame/reg-fx
:init/reset-account-data :init/reset-account-data
reset-account-data!) reset-account-data!)

View File

@ -34,12 +34,10 @@
:on-cancel nil}}) :on-cancel nil}})
(fx/defn save-logging-enabled (fx/defn save-logging-enabled
[{:keys [db now] :as cofx} enabled] [{:keys [db] :as cofx} enabled]
(.setValue rn-dependencies/desktop-config "logging_enabled" enabled)
(let [settings (get-in db [:account/account :settings])] (let [settings (get-in db [:account/account :settings])]
(.setLoggingEnabled rn-dependencies/desktop-config enabled) (accounts.update/update-settings (assoc-in cofx [:db :desktop/desktop :logging-enabled] enabled)
(accounts.update/update-settings cofx (if enabled (assoc settings :log-level "INFO") (dissoc settings :log-level))
(-> settings
(assoc :logging-enabled enabled)
(#(if enabled (assoc %1 :log-level "INFO") (dissoc %1 :log-level))))
{:success-event [:accounts.update.callback/save-settings-success]}))) {:success-event [:accounts.update.callback/save-settings-success]})))

View File

@ -134,13 +134,10 @@
(views/letsubs [logging-enabled [:settings/logging-enabled]] (views/letsubs [logging-enabled [:settings/logging-enabled]]
[react/view {:style (styles/profile-row false)} [react/view {:style (styles/profile-row false)}
[react/text {:style (assoc (styles/profile-row-text colors/black) [react/text {:style (assoc (styles/profile-row-text colors/black)
:font-size 14)} "Logging enabled?"] :font-size 14)} (i18n/label :t/logging-enabled)]
(let [_ (log/debug "### logging-display" logging-enabled)]
[react/switch {:on-tint-color colors/blue [react/switch {:on-tint-color colors/blue
:value logging-enabled :value logging-enabled
:on-value-change #(do :on-value-change #(re-frame/dispatch [:log-level.ui/logging-enabled (not logging-enabled)])}]]))
(log/debug "### changelogging-enabled:" logging-enabled)
(re-frame/dispatch [:log-level.ui/logging-enabled (not logging-enabled)]))}])]))
(views/defview advanced-settings [] (views/defview advanced-settings []
(views/letsubs [installations [:pairing/installations] (views/letsubs [installations [:pairing/installations]
@ -180,7 +177,7 @@
(installations-section installations)) (installations-section installations))
; ;
[react/view {:style styles/title-separator}] [react/view {:style styles/title-separator}]
[react/text {:style styles/adv-settings-subtitle} "Logging"] [react/text {:style styles/adv-settings-subtitle} (i18n/label :t/logging)]
[logging-display]]))) [logging-display]])))
(views/defview backup-recovery-phrase [] (views/defview backup-recovery-phrase []

View File

@ -11,4 +11,5 @@
(re-frame/reg-sub (re-frame/reg-sub
:settings/logging-enabled :settings/logging-enabled
(fn [db _] (fn [db _]
(or (get-in db [:account/account :settings :logging-enabled]) false))) (or (get-in db [:desktop/desktop :logging-enabled]) false)))

View File

@ -564,6 +564,8 @@
"transactions-sign-transaction": "Sign transaction", "transactions-sign-transaction": "Sign transaction",
"wallet-backup-recovery-title": "Backup your recovery phrase", "wallet-backup-recovery-title": "Backup your recovery phrase",
"change-log-level": "Change log level to {{log-level}}", "change-log-level": "Change log level to {{log-level}}",
"logging": "Logging",
"logging-enabled": "Logging enabled?",
"change-logging-enabled": "Are you sure you want to {{enable}} logging?", "change-logging-enabled": "Are you sure you want to {{enable}} logging?",
"enable": "enable", "enable": "enable",
"disable": "disable", "disable": "disable",