From b0d06e04e4f4a0c8645288d4a31449215ce58770 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Thu, 17 Mar 2016 20:59:19 +0100 Subject: Store accounts in sink --- accounts/maildir/maildirsettings.cpp | 75 +++++++++++----------- accounts/maildir/maildirsettings.h | 9 +-- .../package/contents/ui/MaildirAccountSettings.qml | 12 +--- components/package/contents/ui/Settings.qml | 7 +- framework/domain/accountfactory.cpp | 19 ++++-- framework/domain/accountfactory.h | 3 +- framework/domain/accountscontroller.cpp | 29 +++------ framework/domain/accountscontroller.h | 10 --- framework/domain/accountsmodel.cpp | 29 ++++----- framework/domain/accountsmodel.h | 17 +++-- framework/domain/folderlistmodel.cpp | 27 ++++---- 11 files changed, 115 insertions(+), 122 deletions(-) diff --git a/accounts/maildir/maildirsettings.cpp b/accounts/maildir/maildirsettings.cpp index 1d48fc24..847ce92e 100644 --- a/accounts/maildir/maildirsettings.cpp +++ b/accounts/maildir/maildirsettings.cpp @@ -31,23 +31,6 @@ MaildirSettings::MaildirSettings(QObject *parent) { } -void MaildirSettings::setIdentifier(const QByteArray &id) -{ - mIdentifier = id; - Sink::Store::fetchOne(Sink::Query::IdentityFilter(mIdentifier) + Sink::Query::RequestedProperties(QList() << "path")) - .then([this](const Sink::ApplicationDomain::SinkResource &resource) { - auto path = resource.getProperty("path").toString(); - if (mPath != path) { - mPath = path; - emit pathChanged(); - } - }).exec(); -} - -QByteArray MaildirSettings::identifier() const -{ - return mIdentifier; -} void MaildirSettings::setAccountIdentifier(const QByteArray &id) { @@ -56,9 +39,22 @@ void MaildirSettings::setAccountIdentifier(const QByteArray &id) } mAccountIdentifier = id; Q_ASSERT(!id.isEmpty()); - Kube::Account account(id); - auto maildirResource = account.property("maildirResource").toByteArray(); - setIdentifier(maildirResource); + Sink::Store::fetchOne(Sink::Query::IdentityFilter(id)) + .then([this](const Sink::ApplicationDomain::SinkAccount &account) { + mIcon = account.getProperty("icon").toString(); + mName = account.getProperty("name").toString(); + emit changed(); + }).exec(); + + Sink::Store::fetchOne(Sink::Query::PropertyFilter("account", QVariant::fromValue(id))) + .then([this](const Sink::ApplicationDomain::SinkResource &resource) { + mIdentifier = resource.identifier(); + auto path = resource.getProperty("path").toString(); + if (mPath != path) { + mPath = path; + emit pathChanged(); + } + }).exec(); } QByteArray MaildirSettings::accountIdentifier() const @@ -102,6 +98,19 @@ void MaildirSettings::save() qWarning() << "The path doesn't exist: " << mPath; return; } + qDebug() << "Saving account " << mAccountIdentifier << mIdentifier; + Q_ASSERT(!mAccountIdentifier.isEmpty()); + Sink::ApplicationDomain::SinkAccount account(mAccountIdentifier); + account.setProperty("type", "maildir"); + account.setProperty("name", mName); + account.setProperty("icon", mIcon); + Q_ASSERT(!account.identifier().isEmpty()); + Sink::Store::modify(account).then([]() {}, + [](int errorCode, const QString &errorMessage) { + qWarning() << "Error while creating account: " << errorMessage; + }) + .exec(); + if (!mIdentifier.isEmpty()) { Sink::ApplicationDomain::SinkResource resource(mIdentifier); resource.setProperty("path", mPath); @@ -117,12 +126,8 @@ void MaildirSettings::save() resource.setProperty("path", property("path")); resource.setProperty("identifier", resourceIdentifier); resource.setProperty("type", "org.kde.maildir"); - Sink::Store::create(resource).then([this, resourceIdentifier]() { - Q_ASSERT(!mAccountIdentifier.isEmpty()); - Kube::Account account(mAccountIdentifier); - account.setProperty("maildirResource", resourceIdentifier); - account.save(); - }, + resource.setProperty("account", mAccountIdentifier); + Sink::Store::create(resource).then([]() {}, [](int errorCode, const QString &errorMessage) { qWarning() << "Error while creating resource: " << errorMessage; }) @@ -136,20 +141,18 @@ void MaildirSettings::remove() qWarning() << "We're missing an identifier"; } else { Sink::ApplicationDomain::SinkResource resource("", mIdentifier, 0, QSharedPointer::create()); - Sink::Store::remove(resource).then([this]() { - Kube::Account account(mAccountIdentifier); - account.remove(); - - Kube::Settings settings("accounts"); - auto accounts = settings.property("accounts").toStringList(); - accounts.removeAll(mAccountIdentifier); - settings.setProperty("accounts", accounts); - settings.save(); - }, + Sink::Store::remove(resource).then([]() {}, [](int errorCode, const QString &errorMessage) { qWarning() << "Error while removing resource: " << errorMessage; }) .exec(); + + Sink::ApplicationDomain::SinkAccount account("", mAccountIdentifier, 0, QSharedPointer::create()); + Sink::Store::remove(account).then([]() {}, + [](int errorCode, const QString &errorMessage) { + qWarning() << "Error while removing account: " << errorMessage; + }) + .exec(); } } diff --git a/accounts/maildir/maildirsettings.h b/accounts/maildir/maildirsettings.h index 9e5b6a4c..f79208db 100644 --- a/accounts/maildir/maildirsettings.h +++ b/accounts/maildir/maildirsettings.h @@ -24,17 +24,15 @@ class MaildirSettings : public QObject { Q_OBJECT - Q_PROPERTY(QByteArray identifier READ identifier WRITE setIdentifier) Q_PROPERTY(QByteArray accountIdentifier READ accountIdentifier WRITE setAccountIdentifier) Q_PROPERTY(QUrl path READ path WRITE setPath NOTIFY pathChanged) Q_PROPERTY(QValidator* pathValidator READ pathValidator CONSTANT) + Q_PROPERTY(QString icon MEMBER mIcon NOTIFY changed) + Q_PROPERTY(QString accountName MEMBER mName NOTIFY changed) public: MaildirSettings(QObject *parent = 0); - void setIdentifier(const QByteArray &); - QByteArray identifier() const; - void setAccountIdentifier(const QByteArray &); QByteArray accountIdentifier() const; @@ -47,9 +45,12 @@ public: signals: void pathChanged(); + void changed(); private: QByteArray mIdentifier; QByteArray mAccountIdentifier; QString mPath; + QString mIcon; + QString mName; }; diff --git a/accounts/maildir/package/contents/ui/MaildirAccountSettings.qml b/accounts/maildir/package/contents/ui/MaildirAccountSettings.qml index 174683c4..0175fc46 100644 --- a/accounts/maildir/package/contents/ui/MaildirAccountSettings.qml +++ b/accounts/maildir/package/contents/ui/MaildirAccountSettings.qml @@ -49,8 +49,8 @@ Rectangle { id: name placeholderText: accountName Layout.fillWidth: true - text: accountSettings.accountName - onTextChanged: { accountSettings.accountName = text; } + text: maildirSettings.accountName + onTextChanged: { maildirSettings.accountName = text; } } Text { @@ -149,19 +149,11 @@ Rectangle { property string password; } - KubeSettings.Settings { - id: accountSettings - identifier: "account." + accountId - property string accountName; - property string icon: root.icon; - } - Button { text: "Save" onClicked: { transportSettings.save(); maildirSettings.save(); - accountSettings.save(); } } Button { diff --git a/components/package/contents/ui/Settings.qml b/components/package/contents/ui/Settings.qml index c17109a0..fa646372 100644 --- a/components/package/contents/ui/Settings.qml +++ b/components/package/contents/ui/Settings.qml @@ -61,6 +61,9 @@ Rectangle { KubeFramework.AccountsController { id: accountsController } + KubeFramework.AccountsModel { + id: accountsModel + } SplitView { anchors.fill: parent @@ -76,7 +79,7 @@ Rectangle { ListView { id: listView - model: accountsController.accounts + model: accountsModel currentIndex: -1 @@ -90,7 +93,7 @@ Rectangle { KubeFramework.AccountFactory { id: accountFactory - accountId: modelData + accountId: model.accountId } MouseArea { diff --git a/framework/domain/accountfactory.cpp b/framework/domain/accountfactory.cpp index ab1a09e5..10f01f65 100644 --- a/framework/domain/accountfactory.cpp +++ b/framework/domain/accountfactory.cpp @@ -25,20 +25,29 @@ #include #include "settings/settings.h" +#include AccountFactory::AccountFactory(QObject *parent) : QObject(parent) { } +QString AccountFactory::name() const +{ + if (mName.isEmpty()) { + return tr("Account"); + } + return mName; +} + void AccountFactory::setAccountId(const QString &accountId) { mAccountId = accountId; - - Kube::Account account(mAccountId.toUtf8()); - mAccountType = account.type(); - - loadPackage(); + Sink::Store::fetchOne(Sink::Query::IdentityFilter(accountId.toUtf8())) + .then([this](const Sink::ApplicationDomain::SinkAccount &account) { + mAccountType = account.getProperty("type").toByteArray(); + loadPackage(); + }).exec(); } void AccountFactory::loadPackage() diff --git a/framework/domain/accountfactory.h b/framework/domain/accountfactory.h index ed3c21d9..047454ae 100644 --- a/framework/domain/accountfactory.h +++ b/framework/domain/accountfactory.h @@ -29,13 +29,14 @@ class AccountFactory : public QObject { Q_OBJECT Q_PROPERTY(QString accountId MEMBER mAccountId WRITE setAccountId); - Q_PROPERTY(QString name MEMBER mName NOTIFY accountLoaded); + Q_PROPERTY(QString name MEMBER mName READ name NOTIFY accountLoaded); Q_PROPERTY(QString icon MEMBER mIcon NOTIFY accountLoaded); Q_PROPERTY(QString uiPath MEMBER mUiPath NOTIFY accountLoaded); public: explicit AccountFactory(QObject *parent = Q_NULLPTR); void setAccountId(const QString &); + QString name() const; signals: void accountLoaded(); diff --git a/framework/domain/accountscontroller.cpp b/framework/domain/accountscontroller.cpp index 1be03ba9..b5e7e9ca 100644 --- a/framework/domain/accountscontroller.cpp +++ b/framework/domain/accountscontroller.cpp @@ -21,6 +21,7 @@ #include "accountscontroller.h" #include +#include #include #include @@ -28,27 +29,17 @@ AccountsController::AccountsController(QObject *parent) : QObject(parent) { - Kube::Settings settings("accounts"); - mAccounts = settings.property("accounts").toStringList(); - qWarning() << "Loaded accounts" << mAccounts; } void AccountsController::createAccount(const QString &accountType) { - auto identifier = QUuid::createUuid().toByteArray(); - Kube::Account accountSettings(identifier); - accountSettings.setProperty("type", accountType); - accountSettings.save(); - - Kube::Settings settings("accounts"); - auto accounts = settings.property("accounts").toStringList(); - accounts.append(identifier); - settings.setProperty("accounts", accounts); - settings.save(); - - //TODO setup sink resources etc via plugin - - qWarning() << "Created account " << identifier; - mAccounts.append(identifier); - emit accountsChanged(); + const auto identifier = QUuid::createUuid().toByteArray(); + Sink::ApplicationDomain::SinkAccount account; + account.setProperty("identifier", identifier); + account.setProperty("type", accountType); + Sink::Store::create(account).then([]() {}, + [](int errorCode, const QString &errorMessage) { + qWarning() << "Error while creating account: " << errorMessage; + }) + .exec(); } diff --git a/framework/domain/accountscontroller.h b/framework/domain/accountscontroller.h index 9d98de71..a4421de8 100644 --- a/framework/domain/accountscontroller.h +++ b/framework/domain/accountscontroller.h @@ -21,23 +21,13 @@ #include #include -#include -#include class AccountsController : public QObject { Q_OBJECT - Q_PROPERTY (QStringList accounts MEMBER mAccounts NOTIFY accountsChanged) - public: explicit AccountsController(QObject *parent = Q_NULLPTR); -signals: - void accountsChanged(); - public slots: void createAccount(const QString &accountId); - -private: - QStringList mAccounts; }; diff --git a/framework/domain/accountsmodel.cpp b/framework/domain/accountsmodel.cpp index b5eafd53..8a3ab317 100644 --- a/framework/domain/accountsmodel.cpp +++ b/framework/domain/accountsmodel.cpp @@ -17,15 +17,14 @@ 02110-1301, USA. */ #include "accountsmodel.h" +#include -#include - -#include - -AccountsModel::AccountsModel(QObject *parent) : QAbstractListModel() +AccountsModel::AccountsModel(QObject *parent) : QIdentityProxyModel() { - Kube::Settings settings("accounts"); - mAccounts = settings.property("accounts").toStringList(); + Sink::Query query; + query.liveQuery = true; + query.requestedProperties << "name" << "icon"; + runQuery(query); } AccountsModel::~AccountsModel() @@ -46,20 +45,20 @@ QHash< int, QByteArray > AccountsModel::roleNames() const QVariant AccountsModel::data(const QModelIndex &idx, int role) const { - const auto identifier = mAccounts.at(idx.row()); - Kube::Account accountSettings(identifier.toLatin1()); + auto srcIdx = mapToSource(idx); switch (role) { case Name: - return accountSettings.property("accountName").toString(); + return srcIdx.sibling(srcIdx.row(), 0).data(Qt::DisplayRole).toString(); case Icon: - return accountSettings.property("icon").toString(); + return srcIdx.sibling(srcIdx.row(), 1).data(Qt::DisplayRole).toString(); case AccountId: - return identifier; + return srcIdx.data(Sink::Store::DomainObjectBaseRole).value()->identifier(); } - return QVariant(); + return QIdentityProxyModel::data(idx, role); } -int AccountsModel::rowCount(const QModelIndex &idx) const +void AccountsModel::runQuery(const Sink::Query &query) { - return mAccounts.size(); + mModel = Sink::Store::loadModel(query); + setSourceModel(mModel.data()); } diff --git a/framework/domain/accountsmodel.h b/framework/domain/accountsmodel.h index a50c2e4a..9180bc09 100644 --- a/framework/domain/accountsmodel.h +++ b/framework/domain/accountsmodel.h @@ -20,11 +20,15 @@ #pragma once #include -#include +#include #include #include -class AccountsModel : public QAbstractListModel +namespace Sink { + class Query; +} + +class AccountsModel : public QIdentityProxyModel { Q_OBJECT @@ -32,8 +36,7 @@ public: AccountsModel(QObject *parent = Q_NULLPTR); ~AccountsModel(); - int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; - QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; enum Roles { Name = Qt::UserRole + 1, @@ -42,7 +45,9 @@ public: }; Q_ENUMS(Roles) - QHash roleNames() const Q_DECL_OVERRIDE; + QHash roleNames() const; + private: - QStringList mAccounts; + void runQuery(const Sink::Query &query); + QSharedPointer mModel; }; diff --git a/framework/domain/folderlistmodel.cpp b/framework/domain/folderlistmodel.cpp index f212e336..14d3d5a7 100644 --- a/framework/domain/folderlistmodel.cpp +++ b/framework/domain/folderlistmodel.cpp @@ -72,20 +72,19 @@ void FolderListModel::runQuery(const Sink::Query &query) void FolderListModel::setAccountId(const QVariant &accountId) { - const auto account = accountId.toString(); - Kube::Account accountSettings(account.toUtf8()); - //FIXME maildirResource is obviously not good. We need a way to find resources that belong to the account and provide folders. - const auto resourceId = accountSettings.property("maildirResource").toString(); - qDebug() << "Running query for account " << account; - qDebug() << "Found resources " << resourceId; - - Sink::Query query; - query.liveQuery = true; - query.requestedProperties << "name" << "icon" << "parent"; - query.parentProperty = "parent"; - query.resources << resourceId.toUtf8(); - - runQuery(query); + const auto account = accountId.toString().toUtf8(); + Sink::Store::fetchAll(Sink::Query::PropertyFilter("account", QVariant::fromValue(account))) + .then>([this, account](const QList &resources) { + Sink::Query query; + query.liveQuery = true; + query.requestedProperties << "name" << "icon" << "parent"; + query.parentProperty = "parent"; + for (const auto &r : resources) { + qDebug() << "Found resources for account: " << r->identifier() << account; + query.resources << r->identifier(); + } + runQuery(query); + }).exec(); } QVariant FolderListModel::accountId() const -- cgit v1.2.3