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/maildir/maildirsettings.cpp | 247 ++--------------------------------- 1 file changed, 14 insertions(+), 233 deletions(-) (limited to 'accounts/maildir/maildirsettings.cpp') 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(); } -- cgit v1.2.3