From 80859db2fb6746441668efc851c500695aaf4d58 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sat, 16 Jul 2016 10:02:36 +0200 Subject: Share the settings implementation --- accounts/imap/CMakeLists.txt | 4 +- accounts/imap/imapsettings.cpp | 237 ++---------------- accounts/imap/imapsettings.h | 51 +--- accounts/maildir/CMakeLists.txt | 4 +- accounts/maildir/maildirsettings.cpp | 247 ++----------------- accounts/maildir/maildirsettings.h | 51 +--- framework/domain/CMakeLists.txt | 1 + framework/domain/settings/accountsettings.cpp | 337 ++++++++++++++++++++++++++ framework/domain/settings/accountsettings.h | 110 +++++++++ 9 files changed, 490 insertions(+), 552 deletions(-) create mode 100644 framework/domain/settings/accountsettings.cpp create mode 100644 framework/domain/settings/accountsettings.h diff --git a/accounts/imap/CMakeLists.txt b/accounts/imap/CMakeLists.txt index 07c53701..42b50899 100644 --- a/accounts/imap/CMakeLists.txt +++ b/accounts/imap/CMakeLists.txt @@ -35,11 +35,11 @@ set(SRCS add_library(imapaccountplugin SHARED ${SRCS}) qt5_use_modules(imapaccountplugin Core Quick Qml) -target_link_libraries(imapaccountplugin sink settingsplugin) +target_link_libraries(imapaccountplugin sink settingsplugin mailplugin) add_library(imapaccount_static STATIC ${SRCS}) qt5_use_modules(imapaccount_static Core Quick Qml) -target_link_libraries(imapaccount_static sink settingsplugin) +target_link_libraries(imapaccount_static sink settingsplugin mailplugin) add_subdirectory(tests) kpackage_install_package(package org.kube.accounts.imap "genericqml") diff --git a/accounts/imap/imapsettings.cpp b/accounts/imap/imapsettings.cpp index fcef8971..1f338e83 100644 --- a/accounts/imap/imapsettings.cpp +++ b/accounts/imap/imapsettings.cpp @@ -18,241 +18,32 @@ */ #include "imapsettings.h" -#include - -#include -#include -#include -#include - ImapSettings::ImapSettings(QObject *parent) - : QObject(parent) -{ -} - - -void ImapSettings::setAccountIdentifier(const QByteArray &id) -{ - if (id.isEmpty()) { - return; - } - mAccountIdentifier = id; - - //Clear - mIcon = QString(); - mName = QString(); - mImapServer = QString(); - mImapUsername = QString(); - mImapPassword = QString(); - mSmtpServer = QString(); - mSmtpUsername = QString(); - mSmtpPassword = QString(); - emit changed(); - emit imapResourceChanged(); - emit smtpResourceChanged(); - - Q_ASSERT(!id.isEmpty()); - 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::AccountFilter(id) + Sink::Query::CapabilityFilter(Sink::ApplicationDomain::ResourceCapabilities::Mail::storage)) - .then([this](const Sink::ApplicationDomain::SinkResource &resource) { - mIdentifier = resource.identifier(); - mImapServer = resource.getProperty("server").toString(); - mImapUsername = resource.getProperty("username").toString(); - mImapPassword = resource.getProperty("password").toString(); - emit imapResourceChanged(); - }, - [](int errorCode, const QString &errorMessage) { - qWarning() << "Failed to find the imap resource: " << errorMessage; - }).exec(); - - Sink::Store::fetchOne(Sink::Query::AccountFilter(id) + Sink::Query::CapabilityFilter(Sink::ApplicationDomain::ResourceCapabilities::Mail::transport)) - .then([this](const Sink::ApplicationDomain::SinkResource &resource) { - mMailtransportIdentifier = resource.identifier(); - mSmtpServer = resource.getProperty("server").toString(); - mSmtpUsername = resource.getProperty("username").toString(); - mSmtpPassword = resource.getProperty("password").toString(); - emit smtpResourceChanged(); - }, - [](int errorCode, const QString &errorMessage) { - qWarning() << "Failed to find the smtp resource: " << errorMessage; - }).exec(); - - //FIXME this assumes that we only ever have one identity per account - Sink::Store::fetchOne(Sink::Query::AccountFilter(id)) - .then([this](const Sink::ApplicationDomain::Identity &identity) { - mIdentityIdentifier = identity.identifier(); - mUsername = identity.getProperty("username").toString(); - mEmailAddress = identity.getProperty("address").toString(); - emit identityChanged(); - }, - [](int errorCode, const QString &errorMessage) { - qWarning() << "Failed to find the identity resource: " << errorMessage; - }).exec(); -} - -QByteArray ImapSettings::accountIdentifier() const -{ - return mAccountIdentifier; -} - -QValidator *ImapSettings::imapServerValidator() const + : AccountSettings(parent) { - class ImapServerValidator : public QValidator { - State validate(QString &input, int &pos) const { - Q_UNUSED(pos); - // imaps://mainserver.example.net:475 - const QUrl url(input); - static QSet validProtocols = QSet() << "imap" << "imaps"; - if (url.isValid() && validProtocols.contains(url.scheme().toLower())) { - return Acceptable; - } else { - return Intermediate; - } - } - }; - static ImapServerValidator *validator = new ImapServerValidator; - return validator; } -QValidator *ImapSettings::smtpServerValidator() const +void ImapSettings::load() { - class SmtpServerValidator : public QValidator { - State validate(QString &input, int &pos) const { - Q_UNUSED(pos); - // smtps://mainserver.example.net:475 - const QUrl url(input); - static QSet validProtocols = QSet() << "smtp" << "smtps"; - if (url.isValid() && validProtocols.contains(url.scheme().toLower())) { - return Acceptable; - } else { - return Intermediate; - } - } - }; - static SmtpServerValidator *validator = new SmtpServerValidator; - return validator; + loadAccount(); + loadImapResource(); + loadMailtransportResource(); + loadIdentity(); } void ImapSettings::save() { - qDebug() << "Saving account " << mAccountIdentifier << mIdentifier << mMailtransportIdentifier; - Q_ASSERT(!mAccountIdentifier.isEmpty()); - Sink::ApplicationDomain::SinkAccount account(mAccountIdentifier); - account.setProperty("type", "imap"); - 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("server", mImapServer); - resource.setProperty("username", mImapUsername); - resource.setProperty("password", mImapPassword); - Sink::Store::modify(resource).then([](){}, [](int errorCode, const QString &errorMessage) { - qWarning() << "Error while modifying resource: " << errorMessage; - }) - .exec(); - } else { - auto resource = Sink::ApplicationDomain::ImapResource::create(mAccountIdentifier); - mIdentifier = resource.identifier(); - resource.setProperty("server", mImapServer); - resource.setProperty("username", mImapUsername); - resource.setProperty("password", mImapPassword); - Sink::Store::create(resource).then([]() {}, - [](int errorCode, const QString &errorMessage) { - qWarning() << "Error while creating resource: " << errorMessage; - }) - .exec(); - } - - if (!mMailtransportIdentifier.isEmpty()) { - Sink::ApplicationDomain::SinkResource resource(mMailtransportIdentifier); - resource.setProperty("server", mSmtpServer); - resource.setProperty("username", mSmtpUsername); - resource.setProperty("password", mSmtpPassword); - Sink::Store::modify(resource).then([](){}, [](int errorCode, const QString &errorMessage) { - qWarning() << "Error while modifying resource: " << errorMessage; - }) - .exec(); - } else { - auto resource = Sink::ApplicationDomain::MailtransportResource::create(mAccountIdentifier); - mMailtransportIdentifier = resource.identifier(); - resource.setProperty("server", mSmtpServer); - resource.setProperty("username", mSmtpUsername); - resource.setProperty("password", mSmtpPassword); - Sink::Store::create(resource).then([]() {}, - [](int errorCode, const QString &errorMessage) { - qWarning() << "Error while creating resource: " << errorMessage; - }) - .exec(); - } - - if (!mIdentityIdentifier.isEmpty()) { - Sink::ApplicationDomain::Identity identity(mMailtransportIdentifier); - identity.setProperty("username", mUsername); - identity.setProperty("address", mEmailAddress); - Sink::Store::modify(identity).then([](){}, [](int errorCode, const QString &errorMessage) { - qWarning() << "Error while modifying identity: " << errorMessage; - }) - .exec(); - } else { - auto identity = Sink::ApplicationDomain::ApplicationDomainType::createEntity(); - mIdentityIdentifier = identity.identifier(); - identity.setProperty("account", mAccountIdentifier); - identity.setProperty("username", mUsername); - identity.setProperty("address", mEmailAddress); - Sink::Store::create(identity).then([]() {}, - [](int errorCode, const QString &errorMessage) { - qWarning() << "Error while creating identity: " << errorMessage; - }) - .exec(); - } + saveAccount(); + saveImapResource(); + saveMailtransportResource(); + saveIdentity(); } void ImapSettings::remove() { - if (mMailtransportIdentifier.isEmpty()) { - qWarning() << "We're missing an identifier"; - } else { - Sink::ApplicationDomain::SinkResource mailTransportResource("", mMailtransportIdentifier, 0, QSharedPointer::create()); - Sink::Store::remove(mailTransportResource).then([]() {}, - [](int errorCode, const QString &errorMessage) { - qWarning() << "Error while removing resource: " << errorMessage; - }) - .exec(); - } - - if (mIdentifier.isEmpty()) { - qWarning() << "We're missing an identifier"; - } else { - Sink::ApplicationDomain::SinkResource resource("", mIdentifier, 0, QSharedPointer::create()); - Sink::Store::remove(resource).then([]() {}, - [](int errorCode, const QString &errorMessage) { - qWarning() << "Error while removing resource: " << errorMessage; - }) - .exec(); - } - - if (mAccountIdentifier.isEmpty()) { - qWarning() << "We're missing an identifier"; - } else { - 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(); - } + removeResource(mMailtransportIdentifier); + removeResource(mImapIdentifier); + removeIdentity(); + removeAccount(); } diff --git a/accounts/imap/imapsettings.h b/accounts/imap/imapsettings.h index 30bd67d1..ca2fd1ca 100644 --- a/accounts/imap/imapsettings.h +++ b/accounts/imap/imapsettings.h @@ -18,57 +18,16 @@ */ #pragma once -#include -#include +#include -class ImapSettings : public QObject +class ImapSettings : public AccountSettings { Q_OBJECT - Q_PROPERTY(QByteArray accountIdentifier READ accountIdentifier WRITE setAccountIdentifier) - Q_PROPERTY(QString icon MEMBER mIcon NOTIFY changed) - Q_PROPERTY(QString accountName MEMBER mName NOTIFY changed) - Q_PROPERTY(QString userName MEMBER mUsername NOTIFY identityChanged) - Q_PROPERTY(QString emailAddress MEMBER mEmailAddress NOTIFY identityChanged) - Q_PROPERTY(QString imapServer MEMBER mImapServer NOTIFY imapResourceChanged) - Q_PROPERTY(QValidator* imapServerValidator READ imapServerValidator CONSTANT) - Q_PROPERTY(QString imapUsername MEMBER mImapUsername NOTIFY imapResourceChanged) - Q_PROPERTY(QString imapPassword MEMBER mImapPassword NOTIFY imapResourceChanged) - Q_PROPERTY(QString smtpServer MEMBER mSmtpServer NOTIFY smtpResourceChanged) - Q_PROPERTY(QValidator* smtpServerValidator READ smtpServerValidator CONSTANT) - Q_PROPERTY(QString smtpUsername MEMBER mSmtpUsername NOTIFY smtpResourceChanged) - Q_PROPERTY(QString smtpPassword MEMBER mSmtpPassword NOTIFY smtpResourceChanged) public: ImapSettings(QObject *parent = 0); - void setAccountIdentifier(const QByteArray &); - QByteArray accountIdentifier() const; - - QValidator *imapServerValidator() const; - QValidator *smtpServerValidator() const; - - Q_INVOKABLE void save(); - Q_INVOKABLE void remove(); - -signals: - void imapResourceChanged(); - void smtpResourceChanged(); - void identityChanged(); - void changed(); - -private: - QByteArray mIdentifier; - QByteArray mAccountIdentifier; - QByteArray mMailtransportIdentifier; - QByteArray mIdentityIdentifier; - QString mIcon; - QString mName; - QString mUsername; - QString mEmailAddress; - QString mImapServer; - QString mImapUsername; - QString mImapPassword; - QString mSmtpServer; - QString mSmtpUsername; - QString mSmtpPassword; + Q_INVOKABLE virtual void load() Q_DECL_OVERRIDE; + Q_INVOKABLE virtual void save() Q_DECL_OVERRIDE; + Q_INVOKABLE virtual void remove() Q_DECL_OVERRIDE; }; diff --git a/accounts/maildir/CMakeLists.txt b/accounts/maildir/CMakeLists.txt index a48295d5..e50d4179 100644 --- a/accounts/maildir/CMakeLists.txt +++ b/accounts/maildir/CMakeLists.txt @@ -35,11 +35,11 @@ set(SRCS add_library(maildiraccountplugin SHARED ${SRCS}) qt5_use_modules(maildiraccountplugin Core Quick Qml) -target_link_libraries(maildiraccountplugin sink settingsplugin) +target_link_libraries(maildiraccountplugin sink settingsplugin mailplugin) add_library(maildiraccount_static STATIC ${SRCS}) qt5_use_modules(maildiraccount_static Core Quick Qml) -target_link_libraries(maildiraccount_static sink settingsplugin) +target_link_libraries(maildiraccount_static sink settingsplugin mailplugin) add_subdirectory(tests) kpackage_install_package(package org.kube.accounts.maildir "genericqml") diff --git a/accounts/maildir/maildirsettings.cpp b/accounts/maildir/maildirsettings.cpp index 5c1fb177..7611251e 100644 --- a/accounts/maildir/maildirsettings.cpp +++ b/accounts/maildir/maildirsettings.cpp @@ -18,251 +18,32 @@ */ #include "maildirsettings.h" -#include - -#include -#include -#include -#include - MaildirSettings::MaildirSettings(QObject *parent) - : QObject(parent) -{ -} - - -void MaildirSettings::setAccountIdentifier(const QByteArray &id) -{ - if (id.isEmpty()) { - return; - } - mAccountIdentifier = id; - - //Clear - mIcon = QString(); - mName = QString(); - mPath = QString(); - mSmtpServer = QString(); - mSmtpUsername = QString(); - mSmtpPassword = QString(); - emit changed(); - emit pathChanged(); - emit smtpResourceChanged(); - - Q_ASSERT(!id.isEmpty()); - 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::AccountFilter(id) + Sink::Query::CapabilityFilter(Sink::ApplicationDomain::ResourceCapabilities::Mail::storage)) - .then([this](const Sink::ApplicationDomain::SinkResource &resource) { - mIdentifier = resource.identifier(); - auto path = resource.getProperty("path").toString(); - if (mPath != path) { - mPath = path; - emit pathChanged(); - } - }, - [](int errorCode, const QString &errorMessage) { - qWarning() << "Failed to find the maildir resource: " << errorMessage; - }).exec(); - - Sink::Store::fetchOne(Sink::Query::AccountFilter(id) + Sink::Query::CapabilityFilter(Sink::ApplicationDomain::ResourceCapabilities::Mail::transport)) - .then([this](const Sink::ApplicationDomain::SinkResource &resource) { - mMailtransportIdentifier = resource.identifier(); - mSmtpServer = resource.getProperty("server").toString(); - mSmtpUsername = resource.getProperty("username").toString(); - mSmtpPassword = resource.getProperty("password").toString(); - emit smtpResourceChanged(); - }, - [](int errorCode, const QString &errorMessage) { - qWarning() << "Failed to find the maildir resource: " << errorMessage; - }).exec(); - - //FIXME this assumes that we only ever have one identity per account - Sink::Store::fetchOne(Sink::Query::AccountFilter(id)) - .then([this](const Sink::ApplicationDomain::Identity &identity) { - mIdentityIdentifier = identity.identifier(); - mUsername = identity.getProperty("username").toString(); - mEmailAddress = identity.getProperty("address").toString(); - emit identityChanged(); - }, - [](int errorCode, const QString &errorMessage) { - qWarning() << "Failed to find the identity resource: " << errorMessage; - }).exec(); -} - -QByteArray MaildirSettings::accountIdentifier() const + : AccountSettings(parent) { - return mAccountIdentifier; } -void MaildirSettings::setPath(const QUrl &path) +void MaildirSettings::load() { - auto normalizedPath = path.path(); - if (mPath != normalizedPath) { - mPath = normalizedPath; - emit pathChanged(); - } -} - -QUrl MaildirSettings::path() const -{ - return QUrl(mPath); -} - -QValidator *MaildirSettings::pathValidator() const -{ - class PathValidator : public QValidator { - State validate(QString &input, int &pos) const { - Q_UNUSED(pos); - if (!input.isEmpty() && QDir(input).exists()) { - return Acceptable; - } else { - return Intermediate; - } - } - }; - static PathValidator *pathValidator = new PathValidator; - return pathValidator; -} - -QValidator *MaildirSettings::smtpServerValidator() const -{ - class SmtpServerValidator : public QValidator { - State validate(QString &input, int &pos) const { - Q_UNUSED(pos); - // smtps://mainserver.example.net:475 - const QUrl url(input); - static QSet validProtocols = QSet() << "smtp" << "smtps"; - if (url.isValid() && validProtocols.contains(url.scheme().toLower())) { - return Acceptable; - } else { - return Intermediate; - } - } - }; - static SmtpServerValidator *validator = new SmtpServerValidator; - return validator; + loadAccount(); + loadMaildirResource(); + loadMailtransportResource(); + loadIdentity(); } void MaildirSettings::save() { - if (!QDir(mPath).exists()) { - qWarning() << "The path doesn't exist: " << mPath; - return; - } - qDebug() << "Saving account " << mAccountIdentifier << mIdentifier << mMailtransportIdentifier; - 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); - Sink::Store::modify(resource).then([](){}, [](int errorCode, const QString &errorMessage) { - qWarning() << "Error while modifying resource: " << errorMessage; - }) - .exec(); - } else { - auto resource = Sink::ApplicationDomain::MaildirResource::create(mAccountIdentifier); - resource.setProperty("path", property("path")); - mIdentifier = resource.identifier(); - Sink::Store::create(resource).then([]() {}, - [](int errorCode, const QString &errorMessage) { - qWarning() << "Error while creating resource: " << errorMessage; - }) - .exec(); - } - - if (!mMailtransportIdentifier.isEmpty()) { - Sink::ApplicationDomain::SinkResource resource(mMailtransportIdentifier); - resource.setProperty("server", mSmtpServer); - resource.setProperty("username", mSmtpUsername); - resource.setProperty("password", mSmtpPassword); - Sink::Store::modify(resource).then([](){}, [](int errorCode, const QString &errorMessage) { - qWarning() << "Error while modifying resource: " << errorMessage; - }) - .exec(); - } else { - auto resource = Sink::ApplicationDomain::MailtransportResource::create(mAccountIdentifier); - mMailtransportIdentifier = resource.identifier(); - resource.setProperty("server", mSmtpServer); - resource.setProperty("username", mSmtpUsername); - resource.setProperty("password", mSmtpPassword); - Sink::Store::create(resource).then([]() {}, - [](int errorCode, const QString &errorMessage) { - qWarning() << "Error while creating resource: " << errorMessage; - }) - .exec(); - } - - if (!mIdentityIdentifier.isEmpty()) { - Sink::ApplicationDomain::Identity identity(mMailtransportIdentifier); - identity.setProperty("username", mUsername); - identity.setProperty("address", mEmailAddress); - Sink::Store::modify(identity).then([](){}, [](int errorCode, const QString &errorMessage) { - qWarning() << "Error while modifying identity: " << errorMessage; - }) - .exec(); - } else { - auto identity = Sink::ApplicationDomain::ApplicationDomainType::createEntity(); - mIdentityIdentifier = identity.identifier(); - identity.setProperty("account", mAccountIdentifier); - identity.setProperty("username", mUsername); - identity.setProperty("address", mEmailAddress); - Sink::Store::create(identity).then([]() {}, - [](int errorCode, const QString &errorMessage) { - qWarning() << "Error while creating identity: " << errorMessage; - }) - .exec(); - } + saveAccount(); + saveMaildirResource(); + saveMailtransportResource(); + saveIdentity(); } void MaildirSettings::remove() { - if (mMailtransportIdentifier.isEmpty()) { - qWarning() << "We're missing an identifier"; - } else { - Sink::ApplicationDomain::SinkResource mailTransportResource("", mMailtransportIdentifier, 0, QSharedPointer::create()); - Sink::Store::remove(mailTransportResource).then([]() {}, - [](int errorCode, const QString &errorMessage) { - qWarning() << "Error while removing resource: " << errorMessage; - }) - .exec(); - } - - if (mIdentifier.isEmpty()) { - qWarning() << "We're missing an identifier"; - } else { - Sink::ApplicationDomain::SinkResource resource("", mIdentifier, 0, QSharedPointer::create()); - Sink::Store::remove(resource).then([]() {}, - [](int errorCode, const QString &errorMessage) { - qWarning() << "Error while removing resource: " << errorMessage; - }) - .exec(); - } - - if (mAccountIdentifier.isEmpty()) { - qWarning() << "We're missing an identifier"; - } else { - 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(); - } + removeResource(mMailtransportIdentifier); + removeResource(mMaildirIdentifier); + removeIdentity(); + removeAccount(); } diff --git a/accounts/maildir/maildirsettings.h b/accounts/maildir/maildirsettings.h index a02944d9..42336535 100644 --- a/accounts/maildir/maildirsettings.h +++ b/accounts/maildir/maildirsettings.h @@ -18,56 +18,15 @@ */ #pragma once -#include -#include +#include -class MaildirSettings : public QObject +class MaildirSettings : public AccountSettings { Q_OBJECT - 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) - Q_PROPERTY(QString userName MEMBER mUsername NOTIFY identityChanged) - Q_PROPERTY(QString emailAddress MEMBER mEmailAddress NOTIFY identityChanged) - Q_PROPERTY(QString smtpServer MEMBER mSmtpServer NOTIFY smtpResourceChanged) - Q_PROPERTY(QValidator* smtpServerValidator READ smtpServerValidator CONSTANT) - Q_PROPERTY(QString smtpUsername MEMBER mSmtpUsername NOTIFY smtpResourceChanged) - Q_PROPERTY(QString smtpPassword MEMBER mSmtpPassword NOTIFY smtpResourceChanged) - public: MaildirSettings(QObject *parent = 0); - void setAccountIdentifier(const QByteArray &); - QByteArray accountIdentifier() const; - - void setPath(const QUrl &); - QUrl path() const; - QValidator *pathValidator() const; - - QValidator *smtpServerValidator() const; - - Q_INVOKABLE void save(); - Q_INVOKABLE void remove(); - -signals: - void pathChanged(); - void smtpResourceChanged(); - void identityChanged(); - void changed(); - -private: - QByteArray mIdentifier; - QByteArray mAccountIdentifier; - QByteArray mMailtransportIdentifier; - QByteArray mIdentityIdentifier; - QString mPath; - QString mIcon; - QString mName; - QString mUsername; - QString mEmailAddress; - QString mSmtpServer; - QString mSmtpUsername; - QString mSmtpPassword; + Q_INVOKABLE virtual void load() Q_DECL_OVERRIDE; + Q_INVOKABLE virtual void save() Q_DECL_OVERRIDE; + Q_INVOKABLE virtual void remove() Q_DECL_OVERRIDE; }; diff --git a/framework/domain/CMakeLists.txt b/framework/domain/CMakeLists.txt index 114b054d..39a47111 100644 --- a/framework/domain/CMakeLists.txt +++ b/framework/domain/CMakeLists.txt @@ -14,6 +14,7 @@ set(mailplugin_SRCS accountscontroller.cpp accountsmodel.cpp identitiesmodel.cpp + settings/accountsettings.cpp ) add_definitions(-DMAIL_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data") diff --git a/framework/domain/settings/accountsettings.cpp b/framework/domain/settings/accountsettings.cpp new file mode 100644 index 00000000..cf348f39 --- /dev/null +++ b/framework/domain/settings/accountsettings.cpp @@ -0,0 +1,337 @@ +/* + Copyright (c) 2016 Christian Mollekopf + + 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 "accountsettings.h" + +#include +#include +#include +#include + +AccountSettings::AccountSettings(QObject *parent) + : QObject(parent) +{ +} + + +void AccountSettings::setAccountIdentifier(const QByteArray &id) +{ + if (id.isEmpty()) { + return; + } + mAccountIdentifier = id; + + //Clear + mIcon = QString(); + mName = QString(); + mImapServer = QString(); + mImapUsername = QString(); + mImapPassword = QString(); + mSmtpServer = QString(); + mSmtpUsername = QString(); + mSmtpPassword = QString(); + emit changed(); + emit imapResourceChanged(); + emit smtpResourceChanged(); + + load(); + +} + +QByteArray AccountSettings::accountIdentifier() const +{ + return mAccountIdentifier; +} + +void AccountSettings::setPath(const QUrl &path) +{ + auto normalizedPath = path.path(); + if (mPath != normalizedPath) { + mPath = normalizedPath; + emit pathChanged(); + } +} + +QUrl AccountSettings::path() const +{ + return QUrl(mPath); +} + +QValidator *AccountSettings::pathValidator() const +{ + class PathValidator : public QValidator { + State validate(QString &input, int &pos) const { + Q_UNUSED(pos); + if (!input.isEmpty() && QDir(input).exists()) { + return Acceptable; + } else { + return Intermediate; + } + } + }; + static PathValidator *pathValidator = new PathValidator; + return pathValidator; +} + +QValidator *AccountSettings::imapServerValidator() const +{ + class ImapServerValidator : public QValidator { + State validate(QString &input, int &pos) const { + Q_UNUSED(pos); + // imaps://mainserver.example.net:475 + const QUrl url(input); + static QSet validProtocols = QSet() << "imap" << "imaps"; + if (url.isValid() && validProtocols.contains(url.scheme().toLower())) { + return Acceptable; + } else { + return Intermediate; + } + } + }; + static ImapServerValidator *validator = new ImapServerValidator; + return validator; +} + +QValidator *AccountSettings::smtpServerValidator() const +{ + class SmtpServerValidator : public QValidator { + State validate(QString &input, int &pos) const { + Q_UNUSED(pos); + // smtps://mainserver.example.net:475 + const QUrl url(input); + static QSet validProtocols = QSet() << "smtp" << "smtps"; + if (url.isValid() && validProtocols.contains(url.scheme().toLower())) { + return Acceptable; + } else { + return Intermediate; + } + } + }; + static SmtpServerValidator *validator = new SmtpServerValidator; + return validator; +} + +void AccountSettings::saveAccount() +{ + qDebug() << "Saving account " << mAccountIdentifier << mMailtransportIdentifier; + Q_ASSERT(!mAccountIdentifier.isEmpty()); + Sink::ApplicationDomain::SinkAccount account(mAccountIdentifier); + account.setProperty("type", "imap"); + 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(); +} + +void AccountSettings::loadAccount() +{ + Q_ASSERT(!mAccountIdentifier.isEmpty()); + Sink::Store::fetchOne(Sink::Query::IdentityFilter(mAccountIdentifier)) + .then([this](const Sink::ApplicationDomain::SinkAccount &account) { + mIcon = account.getProperty("icon").toString(); + mName = account.getProperty("name").toString(); + emit changed(); + }).exec(); +} + +void AccountSettings::loadImapResource() +{ + Sink::Store::fetchOne(Sink::Query::AccountFilter(mAccountIdentifier) + Sink::Query::CapabilityFilter(Sink::ApplicationDomain::ResourceCapabilities::Mail::storage)) + .then([this](const Sink::ApplicationDomain::SinkResource &resource) { + mImapIdentifier = resource.identifier(); + mImapServer = resource.getProperty("server").toString(); + mImapUsername = resource.getProperty("username").toString(); + mImapPassword = resource.getProperty("password").toString(); + emit imapResourceChanged(); + }, + [](int errorCode, const QString &errorMessage) { + qWarning() << "Failed to find the imap resource: " << errorMessage; + }).exec(); +} + +void AccountSettings::loadMaildirResource() +{ + Sink::Store::fetchOne(Sink::Query::AccountFilter(mAccountIdentifier) + Sink::Query::CapabilityFilter(Sink::ApplicationDomain::ResourceCapabilities::Mail::storage)) + .then([this](const Sink::ApplicationDomain::SinkResource &resource) { + mMaildirIdentifier = resource.identifier(); + auto path = resource.getProperty("path").toString(); + if (mPath != path) { + mPath = path; + emit pathChanged(); + } + }, + [](int errorCode, const QString &errorMessage) { + qWarning() << "Failed to find the maildir resource: " << errorMessage; + }).exec(); +} + +void AccountSettings::loadMailtransportResource() +{ + Sink::Store::fetchOne(Sink::Query::AccountFilter(mAccountIdentifier) + Sink::Query::CapabilityFilter(Sink::ApplicationDomain::ResourceCapabilities::Mail::transport)) + .then([this](const Sink::ApplicationDomain::SinkResource &resource) { + mMailtransportIdentifier = resource.identifier(); + mSmtpServer = resource.getProperty("server").toString(); + mSmtpUsername = resource.getProperty("username").toString(); + mSmtpPassword = resource.getProperty("password").toString(); + emit smtpResourceChanged(); + }, + [](int errorCode, const QString &errorMessage) { + qWarning() << "Failed to find the smtp resource: " << errorMessage; + }).exec(); +} + +void AccountSettings::loadIdentity() +{ + //FIXME this assumes that we only ever have one identity per account + Sink::Store::fetchOne(Sink::Query::AccountFilter(mAccountIdentifier)) + .then([this](const Sink::ApplicationDomain::Identity &identity) { + mIdentityIdentifier = identity.identifier(); + mUsername = identity.getProperty("username").toString(); + mEmailAddress = identity.getProperty("address").toString(); + emit identityChanged(); + }, + [](int errorCode, const QString &errorMessage) { + qWarning() << "Failed to find the identity resource: " << errorMessage; + }).exec(); +} + + + +template +static QByteArray saveResource(const QByteArray &accountIdentifier, const QByteArray &identifier, const std::map &properties) +{ + if (!identifier.isEmpty()) { + Sink::ApplicationDomain::SinkResource resource(identifier); + for (const auto &pair : properties) { + resource.setProperty(pair.first, pair.second); + } + Sink::Store::modify(resource).then([](){}, [](int errorCode, const QString &errorMessage) { + qWarning() << "Error while modifying resource: " << errorMessage; + }) + .exec(); + } else { + auto resource = ResourceType::create(accountIdentifier); + auto newIdentifier = resource.identifier(); + for (const auto &pair : properties) { + resource.setProperty(pair.first, pair.second); + } + Sink::Store::create(resource).template then([]() {}, + [](int errorCode, const QString &errorMessage) { + qWarning() << "Error while creating resource: " << errorMessage; + }) + .exec(); + return newIdentifier; + } + return identifier; +} + +void AccountSettings::saveImapResource() +{ + mImapIdentifier = saveResource(mAccountIdentifier, mImapIdentifier, { + {"server", mImapServer}, + {"username", mImapUsername}, + {"password", mImapPassword}, + }); +} + +void AccountSettings::saveMaildirResource() +{ + mMaildirIdentifier = saveResource(mAccountIdentifier, mMaildirIdentifier, { + {"path", mPath}, + }); +} + +void AccountSettings::saveMailtransportResource() +{ + mMailtransportIdentifier = saveResource(mAccountIdentifier, mMailtransportIdentifier, { + {"server", mSmtpServer}, + {"username", mSmtpUsername}, + {"password", mSmtpPassword}, + }); +} + +void AccountSettings::saveIdentity() +{ + if (!mIdentityIdentifier.isEmpty()) { + Sink::ApplicationDomain::Identity identity(mMailtransportIdentifier); + identity.setProperty("username", mUsername); + identity.setProperty("address", mEmailAddress); + Sink::Store::modify(identity).then([](){}, [](int errorCode, const QString &errorMessage) { + qWarning() << "Error while modifying identity: " << errorMessage; + }) + .exec(); + } else { + auto identity = Sink::ApplicationDomain::ApplicationDomainType::createEntity(); + mIdentityIdentifier = identity.identifier(); + identity.setProperty("account", mAccountIdentifier); + identity.setProperty("username", mUsername); + identity.setProperty("address", mEmailAddress); + Sink::Store::create(identity).then([]() {}, + [](int errorCode, const QString &errorMessage) { + qWarning() << "Error while creating identity: " << errorMessage; + }) + .exec(); + } +} + +void AccountSettings::removeResource(const QByteArray &identifier) +{ + if (identifier.isEmpty()) { + qWarning() << "We're missing an identifier"; + } else { + Sink::ApplicationDomain::SinkResource resource("", identifier, 0, QSharedPointer::create()); + Sink::Store::remove(resource).template then([]() {}, + [](int errorCode, const QString &errorMessage) { + qWarning() << "Error while removing resource: " << errorMessage; + }) + .exec(); + } +} + +void AccountSettings::removeAccount() +{ + if (mAccountIdentifier.isEmpty()) { + qWarning() << "We're missing an identifier"; + } else { + 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(); + } +} + +void AccountSettings::removeIdentity() +{ + if (mIdentityIdentifier.isEmpty()) { + qWarning() << "We're missing an identifier"; + } else { + Sink::ApplicationDomain::Identity identity("", mIdentityIdentifier, 0, QSharedPointer::create()); + Sink::Store::remove(identity).then([]() {}, + [](int errorCode, const QString &errorMessage) { + qWarning() << "Error while removing identity: " << errorMessage; + }) + .exec(); + } +} + diff --git a/framework/domain/settings/accountsettings.h b/framework/domain/settings/accountsettings.h new file mode 100644 index 00000000..08215a32 --- /dev/null +++ b/framework/domain/settings/accountsettings.h @@ -0,0 +1,110 @@ +/* + Copyright (c) 2016 Christian Mollekopf + + 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 + +class AccountSettings : public QObject +{ + Q_OBJECT + Q_PROPERTY(QByteArray accountIdentifier READ accountIdentifier WRITE setAccountIdentifier) + Q_PROPERTY(QString icon MEMBER mIcon NOTIFY changed) + Q_PROPERTY(QString accountName MEMBER mName NOTIFY changed) + + Q_PROPERTY(QString userName MEMBER mUsername NOTIFY identityChanged) + Q_PROPERTY(QString emailAddress MEMBER mEmailAddress NOTIFY identityChanged) + + Q_PROPERTY(QString imapServer MEMBER mImapServer NOTIFY imapResourceChanged) + Q_PROPERTY(QValidator* imapServerValidator READ imapServerValidator CONSTANT) + Q_PROPERTY(QString imapUsername MEMBER mImapUsername NOTIFY imapResourceChanged) + Q_PROPERTY(QString imapPassword MEMBER mImapPassword NOTIFY imapResourceChanged) + + Q_PROPERTY(QString smtpServer MEMBER mSmtpServer NOTIFY smtpResourceChanged) + Q_PROPERTY(QValidator* smtpServerValidator READ smtpServerValidator CONSTANT) + Q_PROPERTY(QString smtpUsername MEMBER mSmtpUsername NOTIFY smtpResourceChanged) + Q_PROPERTY(QString smtpPassword MEMBER mSmtpPassword NOTIFY smtpResourceChanged) + + Q_PROPERTY(QUrl path READ path WRITE setPath NOTIFY pathChanged) + Q_PROPERTY(QValidator* pathValidator READ pathValidator CONSTANT) + +public: + AccountSettings(QObject *parent = 0); + + void setAccountIdentifier(const QByteArray &); + QByteArray accountIdentifier() const; + + void setPath(const QUrl &); + QUrl path() const; + + virtual QValidator *imapServerValidator() const; + virtual QValidator *smtpServerValidator() const; + virtual QValidator *pathValidator() const; + + Q_INVOKABLE virtual void load() = 0; + Q_INVOKABLE virtual void save() = 0; + Q_INVOKABLE virtual void remove() = 0; + +signals: + void imapResourceChanged(); + void smtpResourceChanged(); + void identityChanged(); + void pathChanged(); + void changed(); + +protected: + void saveAccount(); + void saveImapResource(); + void saveMaildirResource(); + void saveMailtransportResource(); + void saveIdentity(); + + void loadAccount(); + void loadImapResource(); + void loadMaildirResource(); + void loadMailtransportResource(); + void loadIdentity(); + + void removeAccount(); + void removeResource(const QByteArray &identifier); + + void removeIdentity(); + + QByteArray mAccountIdentifier; + QString mIcon; + QString mName; + + QByteArray mImapIdentifier; + QString mImapServer; + QString mImapUsername; + QString mImapPassword; + + QByteArray mMaildirIdentifier; + QString mPath; + + QByteArray mMailtransportIdentifier; + QString mSmtpServer; + QString mSmtpUsername; + QString mSmtpPassword; + + QByteArray mIdentityIdentifier; + QString mUsername; + QString mEmailAddress; +}; + -- cgit v1.2.3