From bd1622c27e744971b8fc70b90e9c9d175acec2f2 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sun, 10 Apr 2016 11:00:57 +0200 Subject: Use the MailtransportResource for mailtransport. --- accounts/maildir/maildirsettings.cpp | 85 +++++++++++++++++++++- accounts/maildir/maildirsettings.h | 11 +++ .../package/contents/ui/MaildirAccountSettings.qml | 40 ++++------ accounts/maildir/tests/settingstest.cpp | 13 +++- 4 files changed, 122 insertions(+), 27 deletions(-) (limited to 'accounts') diff --git a/accounts/maildir/maildirsettings.cpp b/accounts/maildir/maildirsettings.cpp index 847ce92e..fa30851c 100644 --- a/accounts/maildir/maildirsettings.cpp +++ b/accounts/maildir/maildirsettings.cpp @@ -38,6 +38,18 @@ void MaildirSettings::setAccountIdentifier(const QByteArray &id) 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) { @@ -46,7 +58,7 @@ void MaildirSettings::setAccountIdentifier(const QByteArray &id) emit changed(); }).exec(); - Sink::Store::fetchOne(Sink::Query::PropertyFilter("account", QVariant::fromValue(id))) + Sink::Store::fetchOne(Sink::Query::PropertyFilter("account", QVariant::fromValue(id)) + Sink::Query::PropertyFilter("type", QString("org.kde.maildir"))) .then([this](const Sink::ApplicationDomain::SinkResource &resource) { mIdentifier = resource.identifier(); auto path = resource.getProperty("path").toString(); @@ -54,6 +66,21 @@ void MaildirSettings::setAccountIdentifier(const QByteArray &id) mPath = path; emit pathChanged(); } + }, + [](int errorCode, const QString &errorMessage) { + qWarning() << "Failed to find the maildir resource: " << errorMessage; + }).exec(); + + Sink::Store::fetchOne(Sink::Query::PropertyFilter("account", QVariant::fromValue(id)) + Sink::Query::PropertyFilter("type", QString("org.kde.mailtransport"))) + .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(); } @@ -92,13 +119,32 @@ QValidator *MaildirSettings::pathValidator() const 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; +} + void MaildirSettings::save() { if (!QDir(mPath).exists()) { qWarning() << "The path doesn't exist: " << mPath; return; } - qDebug() << "Saving account " << mAccountIdentifier << mIdentifier; + qDebug() << "Saving account " << mAccountIdentifier << mIdentifier << mMailtransportIdentifier; Q_ASSERT(!mAccountIdentifier.isEmpty()); Sink::ApplicationDomain::SinkAccount account(mAccountIdentifier); account.setProperty("type", "maildir"); @@ -133,6 +179,34 @@ void MaildirSettings::save() }) .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 { + //FIXME we shouldn't have to do this magic + const auto resourceIdentifier = "org.kde.mailtransport." + QUuid::createUuid().toByteArray(); + mMailtransportIdentifier = resourceIdentifier; + + Sink::ApplicationDomain::SinkResource resource; + resource.setProperty("identifier", resourceIdentifier); + resource.setProperty("type", "org.kde.mailtransport"); + resource.setProperty("account", mAccountIdentifier); + 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(); + } } void MaildirSettings::remove() @@ -140,6 +214,13 @@ void MaildirSettings::remove() if (mIdentifier.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(); + Sink::ApplicationDomain::SinkResource resource("", mIdentifier, 0, QSharedPointer::create()); Sink::Store::remove(resource).then([]() {}, [](int errorCode, const QString &errorMessage) { diff --git a/accounts/maildir/maildirsettings.h b/accounts/maildir/maildirsettings.h index f79208db..be69ffb8 100644 --- a/accounts/maildir/maildirsettings.h +++ b/accounts/maildir/maildirsettings.h @@ -29,6 +29,10 @@ class MaildirSettings : public QObject 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 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); @@ -40,17 +44,24 @@ public: QUrl path() const; QValidator *pathValidator() const; + QValidator *smtpServerValidator() const; + Q_INVOKABLE void save(); Q_INVOKABLE void remove(); signals: void pathChanged(); + void smtpResourceChanged(); void changed(); private: QByteArray mIdentifier; QByteArray mAccountIdentifier; + QByteArray mMailtransportIdentifier; QString mPath; QString mIcon; QString mName; + QString mSmtpServer; + QString mSmtpUsername; + QString mSmtpPassword; }; diff --git a/accounts/maildir/package/contents/ui/MaildirAccountSettings.qml b/accounts/maildir/package/contents/ui/MaildirAccountSettings.qml index d6292072..e508a475 100644 --- a/accounts/maildir/package/contents/ui/MaildirAccountSettings.qml +++ b/accounts/maildir/package/contents/ui/MaildirAccountSettings.qml @@ -78,7 +78,7 @@ Rectangle { Button { iconName: "folder" onClicked: { - fileDialogObject = fileDialogComponent.createObject(parent); + fileDialogComponent.createObject(parent); } Component { @@ -92,10 +92,8 @@ Rectangle { onAccepted: { maildirSettings.path = fileDialog.fileUrl - fileDialogObject.destroy() } onRejected: { - fileDialogObject.destroy() } } } @@ -110,29 +108,33 @@ Rectangle { Label { text: "Username" } TextField { - id: username - placeholderText: "username" + placeholderText: "Username" Layout.fillWidth: true - text: transportSettings.username - onTextChanged: { transportSettings.username = text; } + text: maildirSettings.smtpUsername + onTextChanged: { maildirSettings.smtpUsername = text; } } Label { text: "Password" } TextField { - id: password - placeholderText: "password" + placeholderText: "Password" Layout.fillWidth: true - text: transportSettings.password - onTextChanged: { transportSettings.password = text; } + text: maildirSettings.smtpPassword + onTextChanged: { maildirSettings.smtpPassword = text; } } Label { text: "Server" } TextField { id: server - placeholderText: "server" + placeholderText: "smtps://mainserver.example.net:465" Layout.fillWidth: true - text: transportSettings.server - onTextChanged: { transportSettings.server = text; } + text: maildirSettings.smtpServer + onTextChanged: { maildirSettings.smtpServer = text; } + validator: maildirSettings.smtpServerValidator + Rectangle { + anchors.fill: parent + opacity: 0.2 + color: server.acceptableInput ? "green" : "yellow" + } } MaildirAccount.MaildirSettings { @@ -140,19 +142,9 @@ Rectangle { accountIdentifier: accountId } - KubeSettings.Settings { - id: transportSettings - //TODO set a proper identifier - identifier: "transport.current" - property string server; - property string username; - property string password; - } - Button { text: "Save" onClicked: { - transportSettings.save(); maildirSettings.save(); } } diff --git a/accounts/maildir/tests/settingstest.cpp b/accounts/maildir/tests/settingstest.cpp index 8f8471f0..07665c24 100644 --- a/accounts/maildir/tests/settingstest.cpp +++ b/accounts/maildir/tests/settingstest.cpp @@ -23,14 +23,20 @@ private slots: { auto accountId = "accountid"; auto maildirPath = QDir::tempPath(); + auto smtpServer = QString("smtpserver"); + auto smtpUsername = QString("username"); + auto smtpPassword = QString("password"); MaildirSettings settings; settings.setAccountIdentifier(accountId); settings.setPath(maildirPath); + settings.setProperty("smtpServer", smtpServer); + settings.setProperty("smtpUsername", smtpUsername); + settings.setProperty("smtpPassword", smtpPassword); settings.save(); Sink::Store::fetchAll(Sink::Query()).then>([](const QList &resources) { - QCOMPARE(resources.size(), 1); + QCOMPARE(resources.size(), 2); }) .exec().waitForFinished(); @@ -38,10 +44,15 @@ private slots: { MaildirSettings readSettings; QSignalSpy spy(&readSettings, &MaildirSettings::pathChanged); + QSignalSpy spy1(&readSettings, &MaildirSettings::smtpResourceChanged); readSettings.setAccountIdentifier(accountId); QTRY_VERIFY(spy.count()); + QTRY_VERIFY(spy1.count()); QVERIFY(!readSettings.accountIdentifier().isEmpty()); QCOMPARE(readSettings.path().toString(), maildirPath); + QCOMPARE(readSettings.property("smtpServer").toString(), smtpServer); + QCOMPARE(readSettings.property("smtpUsername").toString(), smtpUsername); + QCOMPARE(readSettings.property("smtpPassword").toString(), smtpPassword); } { -- cgit v1.2.3