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
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;

View file

@ -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");

View file

@ -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::load()
UserSettings::initialize(std::optional<QString> profile)
{
instance_.reset(new UserSettings());
instance_->load(profile);
}
void
UserSettings::load(std::optional<QString> profile)
{
QSettings settings;
tray_ = settings.value("user/window/tray", false).toBool();
@ -89,6 +109,10 @@ 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();
if (profile)
profile_ = *profile;
else
profile_ = settings.value("user/currentProfile", "").toString();
QString prefix =
@ -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();
}

View file

@ -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

View file

@ -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]() {