diff --git a/desktop/appconfig.cpp b/desktop/appconfig.cpp index bff6d87005..7a90204414 100644 --- a/desktop/appconfig.cpp +++ b/desktop/appconfig.cpp @@ -2,6 +2,7 @@ #include +#include const QStringList loggingCategories = {"UIManager", @@ -14,25 +15,50 @@ const QStringList loggingCategories = "RCTStatus", "jsserver", "status"}; + +const QString SETTINGS_GROUP_NAME = "im/status"; +const QString AppConfig::LOGGING_ENABLED = "logging_enabled"; + Q_LOGGING_CATEGORY(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() { return appConfig; } -bool AppConfig::getLoggingEnabled() const { - return loggingEnabled; +QVariant AppConfig::getValue(const QString& name) const { + return settings.value(name); } -void AppConfig::setLoggingEnabled(bool enabled) { - //qCDebug(APPCONFIG) << "### appconfig setLoggingEnabled " << enabled; - QLoggingCategory::setFilterRules(getLoggingFilterRules(enabled)); - this->loggingEnabled = enabled; +void AppConfig::setValue(const QString& name, const QVariant& value) { + 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)); + } } QString AppConfig::getLoggingFilterRules(bool enabled) const { @@ -47,3 +73,4 @@ QString AppConfig::getLoggingFilterRules(bool enabled) const { return filterRules; } } + diff --git a/desktop/appconfig.h b/desktop/appconfig.h index 04689b8d9f..812fc75935 100644 --- a/desktop/appconfig.h +++ b/desktop/appconfig.h @@ -2,6 +2,8 @@ #define APPCONFIG_H #include +#include +#include // This class is intended to store app configuration // modifiable from JS side @@ -12,15 +14,18 @@ public: static AppConfig& inst(); - bool getLoggingEnabled() const; - void setLoggingEnabled(bool enable); + QVariant getValue(const QString& name) const; + void setValue(const QString& name, const QVariant& value); + const static QString LOGGING_ENABLED; private: AppConfig(); - bool loggingEnabled = false; static AppConfig appConfig; + QSettings settings; QString getLoggingFilterRules(bool enabled) const; + void processFx(const QString& name, const QVariant& value) const; }; #endif // APPCONFIG_H + diff --git a/desktop/main.cpp b/desktop/main.cpp index be8a940231..0b3cdaeebf 100644 --- a/desktop/main.cpp +++ b/desktop/main.cpp @@ -1,4 +1,3 @@ - /** * Copyright (C) 2016, Canonical Ltd. * All rights reserved. @@ -240,6 +239,9 @@ int main(int argc, char **argv) { QCoreApplication::setApplicationName("Status"); + // Init AppConfig + AppConfig::inst().getValue(AppConfig::LOGGING_ENABLED); + QString appPath = QCoreApplication::applicationDirPath(); QString dataStoragePath = getDataStoragePath(); #ifdef BUILD_FOR_BUNDLE @@ -263,7 +265,6 @@ int main(int argc, char **argv) { } //qCDebug(STATUS) << "###STATUS_NO_LOGGING"; - AppConfig::inst().setLoggingEnabled(false); #ifdef BUILD_FOR_BUNDLE @@ -558,3 +559,4 @@ void saveMessage(QtMsgType type, const QMessageLogContext &context, } #include "main.moc" + diff --git a/modules/react-native-desktop-config/desktop/desktopconfig.cpp b/modules/react-native-desktop-config/desktop/desktopconfig.cpp index 648c37953b..392f851200 100644 --- a/modules/react-native-desktop-config/desktop/desktopconfig.cpp +++ b/modules/react-native-desktop-config/desktop/desktopconfig.cpp @@ -33,12 +33,14 @@ QList DesktopConfig::methodsToExport() { QVariantMap DesktopConfig::constantsToExport() { return QVariantMap(); } -void DesktopConfig::getLoggingEnabled(double callback) { - bridge->invokePromiseCallback(callback, QVariantList{AppConfig::inst().getLoggingEnabled()}); +void DesktopConfig::getValue(const QString& name, double callback) { + //qCDebug(DESKTOPCONFIG) << "### getValue" << name; + bridge->invokePromiseCallback(callback, QVariantList{AppConfig::inst().getValue(name)}); } -void DesktopConfig::setLoggingEnabled(bool enable) { - //qCDebug(DESKTOPCONFIG) << "### setLoggingEnabled " << enable; - AppConfig::inst().setLoggingEnabled(enable); +void DesktopConfig::setValue(const QString& name, const QVariant& value) { + //qCDebug(DESKTOPCONFIG) << "### setValue" << name << ": " << value; + AppConfig::inst().setValue(name, value); } + diff --git a/modules/react-native-desktop-config/desktop/desktopconfig.h b/modules/react-native-desktop-config/desktop/desktopconfig.h index 0f9e9b5ce5..e027076956 100644 --- a/modules/react-native-desktop-config/desktop/desktopconfig.h +++ b/modules/react-native-desktop-config/desktop/desktopconfig.h @@ -2,7 +2,7 @@ #define DESKTOPCONFIG_H #include "moduleinterface.h" - +#include #include Q_DECLARE_LOGGING_CATEGORY(CONFIG) @@ -22,8 +22,8 @@ public: QList methodsToExport() override; QVariantMap constantsToExport() override; - Q_INVOKABLE void getLoggingEnabled(double callback); - Q_INVOKABLE void setLoggingEnabled(bool enable); + Q_INVOKABLE void getValue(const QString& name, double callback); + Q_INVOKABLE void setValue(const QString& name, const QVariant& value); private: Bridge* bridge = nullptr; diff --git a/modules/react-native-desktop-config/index.js b/modules/react-native-desktop-config/index.js index c7a383ed8d..90ed0275a3 100644 --- a/modules/react-native-desktop-config/index.js +++ b/modules/react-native-desktop-config/index.js @@ -4,15 +4,12 @@ const NativeModules = require('react-native').NativeModules; class DesktopConfig { - static getLoggingEnabled(callbackFn) { - NativeModules.DesktopConfigManager.getLoggingEnabled( - (enabled) => { - callbackFn(enabled); - }); + static getValue(name, callbackFn) { + NativeModules.DesktopConfigManager.getValue(name, callbackFn); } - static setLoggingEnabled(enabled) { - NativeModules.DesktopConfigManager.setLoggingEnabled(enabled); + static setValue(name, value) { + NativeModules.DesktopConfigManager.setValue(name, value); } } diff --git a/src/status_im/accounts/login/core.cljs b/src/status_im/accounts/login/core.cljs index d8d23cbb5d..f6e7630919 100644 --- a/src/status_im/accounts/login/core.cljs +++ b/src/status_im/accounts/login/core.cljs @@ -102,10 +102,6 @@ #(re-frame/dispatch [:web3/fetch-node-version-callback %])]} (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? (initialize-wallet %))) (account-and-db-password-do-not-match cofx error))))) diff --git a/src/status_im/init/core.cljs b/src/status_im/init/core.cljs index 59c8f0b61e..46dfe65b40 100644 --- a/src/status_im/init/core.cljs +++ b/src/status_im/init/core.cljs @@ -5,6 +5,7 @@ [status-im.accounts.login.core :as accounts.login] [status-im.accounts.update.core :as accounts.update] [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.realm.core :as realm] [status-im.extensions.registry :as extensions.registry] @@ -42,6 +43,11 @@ (log/warn "Could not decrypt database" error) (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 (defn reset-keychain! [] (.. (keychain/reset) @@ -67,6 +73,7 @@ (fx/defn start-app [cofx] (fx/merge cofx {:init/get-device-UUID nil + :init/restore-native-settings nil :ui/listen-to-window-dimensions-change nil :notifications/handle-initial-push-notification nil :network/listen-to-network-status nil @@ -78,13 +85,14 @@ (fx/defn initialize-app-db "Initialize db to initial state" [{{:keys [status-module-initialized? view-id hardwallet - initial-props + initial-props desktop/desktop network-status network peers-count peers-summary device-UUID] :node/keys [status] :or {network (get app-db :network)}} :db}] {:db (assoc app-db :contacts/contacts {} :initial-props initial-props + :desktop/desktop (merge desktop (:desktop/desktop app-db)) :network-status network-status :peers-count (or peers-count 0) :peers-summary (or peers-summary []) @@ -149,6 +157,7 @@ (let [{:universal-links/keys [url] :keys [accounts/accounts accounts/create networks/networks network network-status peers-count peers-summary view-id navigation-stack + desktop/desktop status-module-initialized? device-UUID semaphores accounts/login] :node/keys [status on-ready] :or {network (get app-db :network)}} db @@ -162,6 +171,7 @@ :node/status status :node/on-ready on-ready :accounts/create create + :desktop/desktop (merge desktop (:desktop/desktop app-db)) :networks/networks networks :account/account current-account :accounts/login login @@ -215,6 +225,10 @@ :init/init-store init-store!) +(re-frame/reg-fx + :init/restore-native-settings + restore-native-settings!) + (re-frame/reg-fx :init/status-module-initialized status/module-initialized!) @@ -231,3 +245,4 @@ (re-frame/reg-fx :init/reset-account-data reset-account-data!) + diff --git a/src/status_im/log_level/core.cljs b/src/status_im/log_level/core.cljs index d868705f55..1d68e1829f 100644 --- a/src/status_im/log_level/core.cljs +++ b/src/status_im/log_level/core.cljs @@ -34,12 +34,10 @@ :on-cancel nil}}) (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])] - (.setLoggingEnabled rn-dependencies/desktop-config enabled) - (accounts.update/update-settings cofx - (-> settings - (assoc :logging-enabled enabled) - (#(if enabled (assoc %1 :log-level "INFO") (dissoc %1 :log-level)))) + (accounts.update/update-settings (assoc-in cofx [:db :desktop/desktop :logging-enabled] enabled) + (if enabled (assoc settings :log-level "INFO") (dissoc settings :log-level)) {:success-event [:accounts.update.callback/save-settings-success]}))) diff --git a/src/status_im/ui/screens/desktop/main/tabs/profile/views.cljs b/src/status_im/ui/screens/desktop/main/tabs/profile/views.cljs index b6638ed99d..bfba0d318b 100644 --- a/src/status_im/ui/screens/desktop/main/tabs/profile/views.cljs +++ b/src/status_im/ui/screens/desktop/main/tabs/profile/views.cljs @@ -134,13 +134,10 @@ (views/letsubs [logging-enabled [:settings/logging-enabled]] [react/view {:style (styles/profile-row false)} [react/text {:style (assoc (styles/profile-row-text colors/black) - :font-size 14)} "Logging enabled?"] - (let [_ (log/debug "### logging-display" logging-enabled)] - [react/switch {:on-tint-color colors/blue - :value logging-enabled - :on-value-change #(do - (log/debug "### changelogging-enabled:" logging-enabled) - (re-frame/dispatch [:log-level.ui/logging-enabled (not logging-enabled)]))}])])) + :font-size 14)} (i18n/label :t/logging-enabled)] + [react/switch {:on-tint-color colors/blue + :value logging-enabled + :on-value-change #(re-frame/dispatch [:log-level.ui/logging-enabled (not logging-enabled)])}]])) (views/defview advanced-settings [] (views/letsubs [installations [:pairing/installations] @@ -180,7 +177,7 @@ (installations-section installations)) ; [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]]))) (views/defview backup-recovery-phrase [] diff --git a/src/status_im/ui/screens/log_level_settings/subs.cljs b/src/status_im/ui/screens/log_level_settings/subs.cljs index 411cc52975..818970e305 100644 --- a/src/status_im/ui/screens/log_level_settings/subs.cljs +++ b/src/status_im/ui/screens/log_level_settings/subs.cljs @@ -11,4 +11,5 @@ (re-frame/reg-sub :settings/logging-enabled (fn [db _] - (or (get-in db [:account/account :settings :logging-enabled]) false))) + (or (get-in db [:desktop/desktop :logging-enabled]) false))) + diff --git a/translations/en.json b/translations/en.json index 31dfd57b75..89750d0488 100644 --- a/translations/en.json +++ b/translations/en.json @@ -564,6 +564,8 @@ "transactions-sign-transaction": "Sign transaction", "wallet-backup-recovery-title": "Backup your recovery phrase", "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?", "enable": "enable", "disable": "disable",