diff --git a/src/Cache.cpp b/src/Cache.cpp
index 5d11a178..dac0b23a 100644
--- a/src/Cache.cpp
+++ b/src/Cache.cpp
@@ -167,13 +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(localUserId_.toUtf8().toHex()))
+ .arg(QString::fromUtf8(settings->profile().toUtf8().toHex()));
bool isInitial = !QFile::exists(cacheDirectory_);
@@ -186,7 +187,9 @@ Cache::setup()
if (!QDir().mkpath(cacheDirectory_)) {
throw std::runtime_error(
- ("Unable to create state directory:" + cacheDirectory_).toStdString().c_str());
+ ("Unable to create state directory:" + cacheDirectory_)
+ .toStdString()
+ .c_str());
}
}
@@ -575,14 +578,14 @@ 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(), QCryptographicHash::Sha256)) +
- "." + name.c_str());
+ job.setKey("matrix." +
+ QString(QCryptographicHash::hash(settings->profile().toUtf8(),
+ QCryptographicHash::Sha256)) +
+ "." + name.c_str());
job.setTextData(QString::fromStdString(secret));
QEventLoop loop;
job.connect(&job, &QKeychain::Job::finished, &loop, &QEventLoop::quit);
@@ -600,14 +603,14 @@ 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(), QCryptographicHash::Sha256)) +
- "." + name.c_str());
+ job.setKey("matrix." +
+ QString(QCryptographicHash::hash(settings->profile().toUtf8(),
+ QCryptographicHash::Sha256)) +
+ "." + name.c_str());
QEventLoop loop;
job.connect(&job, &QKeychain::Job::finished, &loop, &QEventLoop::quit);
job.start();
@@ -619,14 +622,14 @@ 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(), QCryptographicHash::Sha256)) +
- "." + name.c_str());
+ job.setKey("matrix." +
+ QString(QCryptographicHash::hash(settings->profile().toUtf8(),
+ QCryptographicHash::Sha256)) +
+ "." + name.c_str());
QEventLoop loop;
job.connect(&job, &QKeychain::Job::finished, &loop, &QEventLoop::quit);
job.start();
diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp
index bc7b5223..77269008 100644
--- a/src/MainWindow.cpp
+++ b/src/MainWindow.cpp
@@ -56,8 +56,8 @@
MainWindow *MainWindow::instance_ = nullptr;
MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent),
- userSettings_{QSharedPointer<UserSettings>{new UserSettings}}
+ : QMainWindow(parent)
+ , 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]() {
|