From d8e13159711576394099f8954368aeb9da7fa87a Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Thu, 10 Mar 2016 14:45:15 +0100 Subject: AccountsController --- components/package/contents/ui/Settings.qml | 24 +++++++++---- framework/domain/CMakeLists.txt | 1 + framework/domain/accountfactory.cpp | 20 ++++++----- framework/domain/accountfactory.h | 2 +- framework/domain/accountscontroller.cpp | 54 +++++++++++++++++++++++++++++ framework/domain/accountscontroller.h | 43 +++++++++++++++++++++++ framework/domain/mailplugin.cpp | 2 ++ framework/settings/settings.cpp | 30 +++++++++++++--- framework/settings/settings.h | 2 ++ 9 files changed, 158 insertions(+), 20 deletions(-) create mode 100644 framework/domain/accountscontroller.cpp create mode 100644 framework/domain/accountscontroller.h diff --git a/components/package/contents/ui/Settings.qml b/components/package/contents/ui/Settings.qml index 692ddd0a..1f5fc972 100644 --- a/components/package/contents/ui/Settings.qml +++ b/components/package/contents/ui/Settings.qml @@ -57,15 +57,19 @@ Rectangle { id: contextSettings identifier: "applicationcontext" property string currentAccountId: "current" - property var availableAccountTypes: ["maildir"] - property var accounts: ["current"] } - Column { + KubeFramework.AccountsController { + id: accountsController + } + + ColumnLayout { spacing: 5 Repeater { - model: contextSettings.accounts - delegate: Rectangle { + model: accountsController.accounts + delegate: ColumnLayout { + height: 100 + width: 100 KubeFramework.AccountFactory { id: accountFactory accountId: modelData @@ -81,11 +85,19 @@ Rectangle { Label { text: accountFactory.name } - Loader { source: accountFactory.uiPath } + // Loader { source: accountFactory.uiPath } } } } + Button { + id: button + text: "Create New" + onClicked: { + accountsController.createAccount("maildir"); + } + } + //TODO: Add possibility to add more accounts } } diff --git a/framework/domain/CMakeLists.txt b/framework/domain/CMakeLists.txt index 10a8c065..7560219f 100644 --- a/framework/domain/CMakeLists.txt +++ b/framework/domain/CMakeLists.txt @@ -13,6 +13,7 @@ set(mailplugin_SRCS mailtemplates.cpp retriever.cpp accountfactory.cpp + accountscontroller.cpp ) add_definitions(-DMAIL_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data") diff --git a/framework/domain/accountfactory.cpp b/framework/domain/accountfactory.cpp index af37710a..ab1a09e5 100644 --- a/framework/domain/accountfactory.cpp +++ b/framework/domain/accountfactory.cpp @@ -24,28 +24,30 @@ #include #include +#include "settings/settings.h" + AccountFactory::AccountFactory(QObject *parent) : QObject(parent) { - } void AccountFactory::setAccountId(const QString &accountId) { - qWarning() << "setting account id: " << accountId; mAccountId = accountId; - loadPackage(); -} -QByteArray AccountFactory::getAccountType() const -{ - return "maildir"; + Kube::Account account(mAccountId.toUtf8()); + mAccountType = account.type(); + + loadPackage(); } void AccountFactory::loadPackage() { - auto accountType = getAccountType(); - auto package = KPackage::PackageLoader::self()->loadPackage("KPackage/GenericQML", "org.kube.accounts." + accountType); + auto package = KPackage::PackageLoader::self()->loadPackage("KPackage/GenericQML", "org.kube.accounts." + mAccountType); + if (!package.isValid()) { + qWarning() << "Failed to load account package: " << "org.kube.accounts." + mAccountType; + return; + } Q_ASSERT(package.isValid()); mUiPath = package.filePath("mainscript"); mName = package.metadata().name(); diff --git a/framework/domain/accountfactory.h b/framework/domain/accountfactory.h index 1da32885..ed3c21d9 100644 --- a/framework/domain/accountfactory.h +++ b/framework/domain/accountfactory.h @@ -42,9 +42,9 @@ signals: private: void loadPackage(); - QByteArray getAccountType() const; QString mAccountId; QString mName; QString mIcon; QString mUiPath; + QByteArray mAccountType; }; diff --git a/framework/domain/accountscontroller.cpp b/framework/domain/accountscontroller.cpp new file mode 100644 index 00000000..1be03ba9 --- /dev/null +++ b/framework/domain/accountscontroller.cpp @@ -0,0 +1,54 @@ +/* + Copyright (c) 2016 Michael Bohlender + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + + +#include "accountscontroller.h" + +#include + +#include +#include +#include + +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(); +} diff --git a/framework/domain/accountscontroller.h b/framework/domain/accountscontroller.h new file mode 100644 index 00000000..9d98de71 --- /dev/null +++ b/framework/domain/accountscontroller.h @@ -0,0 +1,43 @@ +/* + Copyright (c) 2016 Michael Bohlender + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#pragma once + +#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/mailplugin.cpp b/framework/domain/mailplugin.cpp index dd438114..c19818ec 100644 --- a/framework/domain/mailplugin.cpp +++ b/framework/domain/mailplugin.cpp @@ -26,6 +26,7 @@ #include "messageparser.h" #include "retriever.h" #include "accountfactory.h" +#include "accountscontroller.h" #include @@ -39,4 +40,5 @@ void MailPlugin::registerTypes (const char *uri) qmlRegisterType(uri, 1, 0, "MessageParser"); qmlRegisterType(uri, 1, 0, "Retriever"); qmlRegisterType(uri, 1, 0, "AccountFactory"); + qmlRegisterType(uri, 1, 0, "AccountsController"); } diff --git a/framework/settings/settings.cpp b/framework/settings/settings.cpp index a4e28190..246c4a99 100644 --- a/framework/settings/settings.cpp +++ b/framework/settings/settings.cpp @@ -26,21 +26,24 @@ using namespace Kube; Settings::Settings(QObject *parent) - : QObject(parent) + : QObject(parent), + mLoaded(false) { } Settings::Settings(const QByteArray &id, QObject *parent) : QObject(parent), - mIdentifier(id) + mIdentifier(id), + mLoaded(false) { load(); } Settings::Settings(const Settings &other) : QObject(other.parent()), - mIdentifier(other.mIdentifier) + mIdentifier(other.mIdentifier), + mLoaded(false) { load(); } @@ -59,8 +62,19 @@ void Settings::save() { qWarning() << "Saving" << mIdentifier; auto settings = getSettings(); + + for (const auto &p : dynamicPropertyNames()) { + qWarning() << "setting " << p << property(p); + if (p == "identifier") { + continue; + } + settings->setValue(p, property(p)); + } for (int i = metaObject()->propertyOffset(); i < metaObject()->propertyCount(); i++) { const auto p = metaObject()->property(i).name(); + if (p == QByteArray("identifier")) { + continue; + } qWarning() << "setting " << p << property(p); settings->setValue(p, property(p)); } @@ -69,7 +83,10 @@ void Settings::save() void Settings::load() { - qWarning() << "loading" << mIdentifier; + if (mLoaded) { + return; + } + mLoaded = true; for (int i = metaObject()->propertyOffset(); i < metaObject()->propertyCount(); i++) { auto p = metaObject()->property(i).name(); setProperty(p, QVariant()); @@ -114,6 +131,11 @@ Identity Account::primaryIdentity() const return Identity(property("primaryIdentityId").toByteArray()); } +QByteArray Account::type() const +{ + return property("type").toByteArray(); +} + Identity::Identity(const QByteArray &identifier) : Settings("identity." + identifier) { diff --git a/framework/settings/settings.h b/framework/settings/settings.h index bfee55cb..685d67aa 100644 --- a/framework/settings/settings.h +++ b/framework/settings/settings.h @@ -42,6 +42,7 @@ private: void load(); QSharedPointer getSettings(); QByteArray mIdentifier; + bool mLoaded; }; class Account; @@ -63,6 +64,7 @@ class Account : public Settings public: Account(const QByteArray &identifier); Identity primaryIdentity() const; + QByteArray type() const; }; class Identity : public Settings -- cgit v1.2.3