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 --- framework/domain/settings/accountsettings.cpp | 337 ++++++++++++++++++++++++++ 1 file changed, 337 insertions(+) create mode 100644 framework/domain/settings/accountsettings.cpp (limited to 'framework/domain/settings/accountsettings.cpp') 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(); + } +} + -- cgit v1.2.3