Actually store the login details under the selected profile
This commit is contained in:
Nicolas Werner 2020-12-27 22:56:43 +01:00
parent 71aea17ac6
commit 391b1b3285
5 changed files with 53 additions and 22 deletions

View file

@ -167,14 +167,14 @@ Cache::Cache(const QString &userId, QObject *parent)
void void
Cache::setup() Cache::setup()
{ {
UserSettings settings; auto settings = UserSettings::instance();
nhlog::db()->debug("setting up cache"); nhlog::db()->debug("setting up cache");
cacheDirectory_ = QString("%1/%2%3") cacheDirectory_ = QString("%1/%2%3")
.arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)) .arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation))
.arg(QString::fromUtf8(localUserId_.toUtf8().toHex())) .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_); bool isInitial = !QFile::exists(cacheDirectory_);
@ -578,12 +578,12 @@ Cache::restoreOlmAccount()
void void
Cache::storeSecret(const std::string &name, const std::string &secret) Cache::storeSecret(const std::string &name, const std::string &secret)
{ {
UserSettings settings; auto settings = UserSettings::instance();
QKeychain::WritePasswordJob job(QCoreApplication::applicationName()); QKeychain::WritePasswordJob job(QCoreApplication::applicationName());
job.setAutoDelete(false); job.setAutoDelete(false);
job.setInsecureFallback(true); job.setInsecureFallback(true);
job.setKey("matrix." + job.setKey("matrix." +
QString(QCryptographicHash::hash(settings.profile().toUtf8(), QString(QCryptographicHash::hash(settings->profile().toUtf8(),
QCryptographicHash::Sha256)) + QCryptographicHash::Sha256)) +
"." + name.c_str()); "." + name.c_str());
job.setTextData(QString::fromStdString(secret)); job.setTextData(QString::fromStdString(secret));
@ -603,12 +603,12 @@ Cache::storeSecret(const std::string &name, const std::string &secret)
void void
Cache::deleteSecret(const std::string &name) Cache::deleteSecret(const std::string &name)
{ {
UserSettings settings; auto settings = UserSettings::instance();
QKeychain::DeletePasswordJob job(QCoreApplication::applicationName()); QKeychain::DeletePasswordJob job(QCoreApplication::applicationName());
job.setAutoDelete(false); job.setAutoDelete(false);
job.setInsecureFallback(true); job.setInsecureFallback(true);
job.setKey("matrix." + job.setKey("matrix." +
QString(QCryptographicHash::hash(settings.profile().toUtf8(), QString(QCryptographicHash::hash(settings->profile().toUtf8(),
QCryptographicHash::Sha256)) + QCryptographicHash::Sha256)) +
"." + name.c_str()); "." + name.c_str());
QEventLoop loop; QEventLoop loop;
@ -622,12 +622,12 @@ Cache::deleteSecret(const std::string &name)
std::optional<std::string> std::optional<std::string>
Cache::secret(const std::string &name) Cache::secret(const std::string &name)
{ {
UserSettings settings; auto settings = UserSettings::instance();
QKeychain::ReadPasswordJob job(QCoreApplication::applicationName()); QKeychain::ReadPasswordJob job(QCoreApplication::applicationName());
job.setAutoDelete(false); job.setAutoDelete(false);
job.setInsecureFallback(true); job.setInsecureFallback(true);
job.setKey("matrix." + job.setKey("matrix." +
QString(QCryptographicHash::hash(settings.profile().toUtf8(), QString(QCryptographicHash::hash(settings->profile().toUtf8(),
QCryptographicHash::Sha256)) + QCryptographicHash::Sha256)) +
"." + name.c_str()); "." + name.c_str());
QEventLoop loop; QEventLoop loop;

View file

@ -57,7 +57,7 @@ MainWindow *MainWindow::instance_ = nullptr;
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
, userSettings_{QSharedPointer<UserSettings>{new UserSettings}} , userSettings_{UserSettings::instance()}
{ {
setWindowTitle(0); setWindowTitle(0);
setObjectName("MainWindow"); setObjectName("MainWindow");

View file

@ -50,10 +50,30 @@
#include "config/nheko.h" #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 void
UserSettings::load() UserSettings::initialize(std::optional<QString> profile)
{
instance_.reset(new UserSettings());
instance_->load(profile);
}
void
UserSettings::load(std::optional<QString> profile)
{ {
QSettings settings; QSettings settings;
tray_ = settings.value("user/window/tray", false).toBool(); tray_ = settings.value("user/window/tray", false).toBool();
@ -89,6 +109,10 @@ UserSettings::load()
cameraResolution_ = settings.value("user/camera_resolution", QString()).toString(); cameraResolution_ = settings.value("user/camera_resolution", QString()).toString();
cameraFrameRate_ = settings.value("user/camera_frame_rate", QString()).toString(); cameraFrameRate_ = settings.value("user/camera_frame_rate", QString()).toString();
useStunServer_ = settings.value("user/use_stun_server", false).toBool(); useStunServer_ = settings.value("user/use_stun_server", false).toBool();
if (profile)
profile_ = *profile;
else
profile_ = settings.value("user/currentProfile", "").toString(); profile_ = settings.value("user/currentProfile", "").toString();
QString prefix = QString prefix =
@ -527,6 +551,8 @@ UserSettings::save()
settings.setValue("use_stun_server", useStunServer_); settings.setValue("use_stun_server", useStunServer_);
settings.setValue("currentProfile", profile_); settings.setValue("currentProfile", profile_);
settings.endGroup(); // user
QString prefix = QString prefix =
(profile_ != "" && profile_ != "default") ? "profile/" + profile_ + "/" : ""; (profile_ != "" && profile_ != "default") ? "profile/" + profile_ + "/" : "";
settings.setValue(prefix + "auth/access_token", accessToken_); settings.setValue(prefix + "auth/access_token", accessToken_);
@ -534,8 +560,6 @@ UserSettings::save()
settings.setValue(prefix + "auth/user_id", userId_); settings.setValue(prefix + "auth/user_id", userId_);
settings.setValue(prefix + "auth/device_id", deviceId_); settings.setValue(prefix + "auth/device_id", deviceId_);
settings.endGroup(); // user
settings.sync(); settings.sync();
} }

View file

@ -91,9 +91,12 @@ class UserSettings : public QObject
Q_PROPERTY(QString deviceId READ deviceId WRITE setDeviceId NOTIFY deviceIdChanged) Q_PROPERTY(QString deviceId READ deviceId WRITE setDeviceId NOTIFY deviceIdChanged)
Q_PROPERTY(QString homeserver READ homeserver WRITE setHomeserver NOTIFY homeserverChanged) Q_PROPERTY(QString homeserver READ homeserver WRITE setHomeserver NOTIFY homeserverChanged)
public:
UserSettings(); UserSettings();
public:
static QSharedPointer<UserSettings> instance();
static void initialize(std::optional<QString> profile);
enum class Presence enum class Presence
{ {
AutomaticPresence, AutomaticPresence,
@ -104,7 +107,7 @@ public:
Q_ENUM(Presence) Q_ENUM(Presence)
void save(); void save();
void load(); void load(std::optional<QString> profile);
void applyTheme(); void applyTheme();
void setTheme(QString theme); void setTheme(QString theme);
void setMessageHoverHighlight(bool state); void setMessageHoverHighlight(bool state);
@ -252,6 +255,8 @@ private:
QString accessToken_; QString accessToken_;
QString deviceId_; QString deviceId_;
QString homeserver_; QString homeserver_;
static QSharedPointer<UserSettings> instance_;
}; };
class HorizontalLine : public QFrame class HorizontalLine : public QFrame

View file

@ -196,17 +196,19 @@ main(int argc, char *argv[])
std::exit(1); std::exit(1);
} }
UserSettings settings;
if (parser.isSet(configName)) 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; QFont font;
QString userFontFamily = settings.font(); QString userFontFamily = settings.lock()->font();
if (!userFontFamily.isEmpty()) { if (!userFontFamily.isEmpty()) {
font.setFamily(userFontFamily); font.setFamily(userFontFamily);
} }
font.setPointSizeF(settings.fontSize()); font.setPointSizeF(settings.lock()->fontSize());
app.setFont(font); app.setFont(font);
@ -226,7 +228,7 @@ main(int argc, char *argv[])
// Move the MainWindow to the center // Move the MainWindow to the center
w.move(screenCenter(w.width(), w.height())); w.move(screenCenter(w.width(), w.height()));
if (!settings.startInTray() && !settings.tray()) if (!settings.lock()->startInTray() && !settings.lock()->tray())
w.show(); w.show();
QObject::connect(&app, &QApplication::aboutToQuit, &w, [&w]() { QObject::connect(&app, &QApplication::aboutToQuit, &w, [&w]() {