From 2a60a0723257cfd6855233b9af27dc9735915435 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Fri, 22 Sep 2017 10:23:44 +0200 Subject: Keyring fixes * Avoid double free * track unlocked state * Ensure we bring up the login screen on startup and after saving the configuration. --- components/kube/contents/ui/Kube.qml | 17 ++++++++++------- framework/src/frameworkplugin.cpp | 4 +++- framework/src/keyring.cpp | 16 +++++++++++++++- framework/src/keyring.h | 4 ++++ 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/components/kube/contents/ui/Kube.qml b/components/kube/contents/ui/Kube.qml index 603a499f..7625c52c 100644 --- a/components/kube/contents/ui/Kube.qml +++ b/components/kube/contents/ui/Kube.qml @@ -47,8 +47,11 @@ Controls2.ApplicationWindow { property variant currentAccount onCurrentAccountChanged: { if (!!currentAccount) { - console.warn("Syncing account", currentAccount) - Kube.Fabric.postMessage(Kube.Messages.synchronize, {"accountId": currentAccount}) + if (kubeViews.currentItem && !Kube.Keyring.isUnlocked(currentAccount)) { + kubeViews.setLoginView() + } else { + Kube.Fabric.postMessage(Kube.Messages.synchronize, {"accountId": currentAccount}) + } } } @@ -282,7 +285,9 @@ Controls2.ApplicationWindow { } function setLoginView() { - pushView(loginView, {accountId: currentAccount}) + if (currentItem != loginView) { + pushView(loginView, {accountId: currentAccount}) + } } function openComposer(newMessage, recipients) { @@ -301,11 +306,9 @@ Controls2.ApplicationWindow { Component.onCompleted: { if (!currentItem) { - if (!Kube.Keyring.isUnlocked(app.currentAccount)) { - setMailView(); + setMailView(); + if (!!app.currentAccount && !Kube.Keyring.isUnlocked(app.currentAccount)) { setLoginView() - } else { - setMailView(); } } } diff --git a/framework/src/frameworkplugin.cpp b/framework/src/frameworkplugin.cpp index 1e1a169e..c4f7e85d 100644 --- a/framework/src/frameworkplugin.cpp +++ b/framework/src/frameworkplugin.cpp @@ -60,7 +60,9 @@ static QObject *keyring_singletontype_provider(QQmlEngine *engine, QJSEngine *sc { Q_UNUSED(engine) Q_UNUSED(scriptEngine) - return new Kube::Keyring; + auto instance = Kube::Keyring::instance(); + QQmlEngine::setObjectOwnership(instance, QQmlEngine::CppOwnership); + return instance; } void FrameworkPlugin::registerTypes (const char *uri) diff --git a/framework/src/keyring.cpp b/framework/src/keyring.cpp index e3fdb1cb..7bd406c0 100644 --- a/framework/src/keyring.cpp +++ b/framework/src/keyring.cpp @@ -20,18 +20,31 @@ #include #include +#include using namespace Kube; +Q_GLOBAL_STATIC(Keyring, sKeyring); + Keyring::Keyring() : QObject() { } +Keyring *Keyring::instance() +{ + return sKeyring; +} + bool Keyring::isUnlocked(const QByteArray &accountId) { - return false; + return mUnlocked.contains(accountId); +} + +void Keyring::unlock(const QByteArray &accountId) +{ + mUnlocked.insert(accountId); } AccountKeyring::AccountKeyring(const QByteArray &accountId, QObject *parent) @@ -45,6 +58,7 @@ void AccountKeyring::storePassword(const QByteArray &resourceId, const QString & QSettings settings{mAccountIdentifier + ".keyring", QSettings::IniFormat}; settings.setValue(resourceId, password); Sink::SecretStore::instance().insert(resourceId, password); + Keyring::instance()->unlock(mAccountIdentifier); } void AccountKeyring::unlock() diff --git a/framework/src/keyring.h b/framework/src/keyring.h index ce4e137d..df25dbff 100644 --- a/framework/src/keyring.h +++ b/framework/src/keyring.h @@ -18,6 +18,7 @@ */ #include +#include namespace Kube { @@ -25,10 +26,13 @@ class Keyring : public QObject { Q_OBJECT public: Keyring(); + static Keyring *instance(); Q_INVOKABLE bool isUnlocked(const QByteArray &accountId); + void unlock(const QByteArray &accountId); private: Q_DISABLE_COPY(Keyring); + QSet mUnlocked; }; class AccountKeyring : public QObject { -- cgit v1.2.3