summary refs log tree commit diff
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2020-12-27 22:56:43 +0100
committerNicolas Werner <nicolas.werner@hotmail.de>2020-12-27 22:56:54 +0100
commit391b1b32856a5964e429fd95ab588a5e9e745801 (patch)
tree2dd2e32746c5a0efa053418c1ad80645290c7550
parentExpose appimage (diff)
downloadnheko-391b1b32856a5964e429fd95ab588a5e9e745801.tar.xz
Fix #359
Actually store the login details under the selected profile
-rw-r--r--src/Cache.cpp16
-rw-r--r--src/MainWindow.cpp2
-rw-r--r--src/UserSettingsPage.cpp34
-rw-r--r--src/UserSettingsPage.h9
-rw-r--r--src/main.cpp14
5 files changed, 53 insertions, 22 deletions
diff --git a/src/Cache.cpp b/src/Cache.cpp
index 7efae881..dac0b23a 100644
--- a/src/Cache.cpp
+++ b/src/Cache.cpp
@@ -167,14 +167,14 @@ Cache::Cache(const QString &userId, QObject *parent)
 void
 Cache::setup()
 {
-        UserSettings settings;
+        auto settings = UserSettings::instance();
 
         nhlog::db()->debug("setting up cache");
 
         cacheDirectory_ = QString("%1/%2%3")
                             .arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation))
                             .arg(QString::fromUtf8(localUserId_.toUtf8().toHex()))
-                            .arg(QString::fromUtf8(settings.profile().toUtf8().toHex()));
+                            .arg(QString::fromUtf8(settings->profile().toUtf8().toHex()));
 
         bool isInitial = !QFile::exists(cacheDirectory_);
 
@@ -578,12 +578,12 @@ Cache::restoreOlmAccount()
 void
 Cache::storeSecret(const std::string &name, const std::string &secret)
 {
-        UserSettings settings;
+        auto settings = UserSettings::instance();
         QKeychain::WritePasswordJob job(QCoreApplication::applicationName());
         job.setAutoDelete(false);
         job.setInsecureFallback(true);
         job.setKey("matrix." +
-                   QString(QCryptographicHash::hash(settings.profile().toUtf8(),
+                   QString(QCryptographicHash::hash(settings->profile().toUtf8(),
                                                     QCryptographicHash::Sha256)) +
                    "." + name.c_str());
         job.setTextData(QString::fromStdString(secret));
@@ -603,12 +603,12 @@ Cache::storeSecret(const std::string &name, const std::string &secret)
 void
 Cache::deleteSecret(const std::string &name)
 {
-        UserSettings settings;
+        auto settings = UserSettings::instance();
         QKeychain::DeletePasswordJob job(QCoreApplication::applicationName());
         job.setAutoDelete(false);
         job.setInsecureFallback(true);
         job.setKey("matrix." +
-                   QString(QCryptographicHash::hash(settings.profile().toUtf8(),
+                   QString(QCryptographicHash::hash(settings->profile().toUtf8(),
                                                     QCryptographicHash::Sha256)) +
                    "." + name.c_str());
         QEventLoop loop;
@@ -622,12 +622,12 @@ Cache::deleteSecret(const std::string &name)
 std::optional<std::string>
 Cache::secret(const std::string &name)
 {
-        UserSettings settings;
+        auto settings = UserSettings::instance();
         QKeychain::ReadPasswordJob job(QCoreApplication::applicationName());
         job.setAutoDelete(false);
         job.setInsecureFallback(true);
         job.setKey("matrix." +
-                   QString(QCryptographicHash::hash(settings.profile().toUtf8(),
+                   QString(QCryptographicHash::hash(settings->profile().toUtf8(),
                                                     QCryptographicHash::Sha256)) +
                    "." + name.c_str());
         QEventLoop loop;
diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp
index bf5fd026..77269008 100644
--- a/src/MainWindow.cpp
+++ b/src/MainWindow.cpp
@@ -57,7 +57,7 @@ MainWindow *MainWindow::instance_ = nullptr;
 
 MainWindow::MainWindow(QWidget *parent)
   : QMainWindow(parent)
-  , userSettings_{QSharedPointer<UserSettings>{new UserSettings}}
+  , userSettings_{UserSettings::instance()}
 {
         setWindowTitle(0);
         setObjectName("MainWindow");
diff --git a/src/UserSettingsPage.cpp b/src/UserSettingsPage.cpp
index 55f666c1..4ca3be49 100644
--- a/src/UserSettingsPage.cpp
+++ b/src/UserSettingsPage.cpp
@@ -50,10 +50,30 @@
 
 #include "config/nheko.h"
 
-UserSettings::UserSettings() { load(); }
+QSharedPointer<UserSettings> UserSettings::instance_;
+
+UserSettings::UserSettings()
+{
+        connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, [this]() {
+                instance_.clear();
+        });
+}
+
+QSharedPointer<UserSettings>
+UserSettings::instance()
+{
+        return instance_;
+}
+
+void
+UserSettings::initialize(std::optional<QString> profile)
+{
+        instance_.reset(new UserSettings());
+        instance_->load(profile);
+}
 
 void
-UserSettings::load()
+UserSettings::load(std::optional<QString> profile)
 {
         QSettings settings;
         tray_                    = settings.value("user/window/tray", false).toBool();
@@ -89,7 +109,11 @@ UserSettings::load()
         cameraResolution_ = settings.value("user/camera_resolution", QString()).toString();
         cameraFrameRate_  = settings.value("user/camera_frame_rate", QString()).toString();
         useStunServer_    = settings.value("user/use_stun_server", false).toBool();
-        profile_          = settings.value("user/currentProfile", "").toString();
+
+        if (profile)
+                profile_ = *profile;
+        else
+                profile_ = settings.value("user/currentProfile", "").toString();
 
         QString prefix =
           (profile_ != "" && profile_ != "default") ? "profile/" + profile_ + "/" : "";
@@ -527,6 +551,8 @@ UserSettings::save()
         settings.setValue("use_stun_server", useStunServer_);
         settings.setValue("currentProfile", profile_);
 
+        settings.endGroup(); // user
+
         QString prefix =
           (profile_ != "" && profile_ != "default") ? "profile/" + profile_ + "/" : "";
         settings.setValue(prefix + "auth/access_token", accessToken_);
@@ -534,8 +560,6 @@ UserSettings::save()
         settings.setValue(prefix + "auth/user_id", userId_);
         settings.setValue(prefix + "auth/device_id", deviceId_);
 
-        settings.endGroup(); // user
-
         settings.sync();
 }
 
diff --git a/src/UserSettingsPage.h b/src/UserSettingsPage.h
index dd1e26d9..af73202e 100644
--- a/src/UserSettingsPage.h
+++ b/src/UserSettingsPage.h
@@ -91,9 +91,12 @@ class UserSettings : public QObject
         Q_PROPERTY(QString deviceId READ deviceId WRITE setDeviceId NOTIFY deviceIdChanged)
         Q_PROPERTY(QString homeserver READ homeserver WRITE setHomeserver NOTIFY homeserverChanged)
 
-public:
         UserSettings();
 
+public:
+        static QSharedPointer<UserSettings> instance();
+        static void initialize(std::optional<QString> profile);
+
         enum class Presence
         {
                 AutomaticPresence,
@@ -104,7 +107,7 @@ public:
         Q_ENUM(Presence)
 
         void save();
-        void load();
+        void load(std::optional<QString> profile);
         void applyTheme();
         void setTheme(QString theme);
         void setMessageHoverHighlight(bool state);
@@ -252,6 +255,8 @@ private:
         QString accessToken_;
         QString deviceId_;
         QString homeserver_;
+
+        static QSharedPointer<UserSettings> instance_;
 };
 
 class HorizontalLine : public QFrame
diff --git a/src/main.cpp b/src/main.cpp
index 58bdda34..a60c66c4 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -196,17 +196,19 @@ main(int argc, char *argv[])
                 std::exit(1);
         }
 
-        UserSettings settings;
-
         if (parser.isSet(configName))
-                settings.setProfile(parser.value(configName));
+                UserSettings::initialize(parser.value(configName));
+        else
+                UserSettings::initialize(std::nullopt);
+
+        auto settings = UserSettings::instance().toWeakRef();
 
         QFont font;
-        QString userFontFamily = settings.font();
+        QString userFontFamily = settings.lock()->font();
         if (!userFontFamily.isEmpty()) {
                 font.setFamily(userFontFamily);
         }
-        font.setPointSizeF(settings.fontSize());
+        font.setPointSizeF(settings.lock()->fontSize());
 
         app.setFont(font);
 
@@ -226,7 +228,7 @@ main(int argc, char *argv[])
         // Move the MainWindow to the center
         w.move(screenCenter(w.width(), w.height()));
 
-        if (!settings.startInTray() && !settings.tray())
+        if (!settings.lock()->startInTray() && !settings.lock()->tray())
                 w.show();
 
         QObject::connect(&app, &QApplication::aboutToQuit, &w, [&w]() {