diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-04-10 11:00:57 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-04-10 11:00:57 +0200 |
commit | bd1622c27e744971b8fc70b90e9c9d175acec2f2 (patch) | |
tree | 140ed46693d86627c53fe4a1344c083efe87c909 | |
parent | 3d3ddd44c59cd0fdaf8af0c2ac27d1379fa3df3f (diff) | |
download | kube-bd1622c27e744971b8fc70b90e9c9d175acec2f2.tar.gz kube-bd1622c27e744971b8fc70b90e9c9d175acec2f2.zip |
Use the MailtransportResource for mailtransport.
-rw-r--r-- | accounts/maildir/maildirsettings.cpp | 85 | ||||
-rw-r--r-- | accounts/maildir/maildirsettings.h | 11 | ||||
-rw-r--r-- | accounts/maildir/package/contents/ui/MaildirAccountSettings.qml | 40 | ||||
-rw-r--r-- | accounts/maildir/tests/settingstest.cpp | 13 | ||||
-rw-r--r-- | components/package/contents/ui/Composer.qml | 6 | ||||
-rw-r--r-- | framework/domain/actions/mailactions.cpp | 19 | ||||
-rw-r--r-- | framework/domain/actions/sinkactions.cpp | 43 | ||||
-rw-r--r-- | framework/domain/composercontroller.cpp | 35 | ||||
-rw-r--r-- | framework/domain/composercontroller.h | 13 |
9 files changed, 184 insertions, 81 deletions
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) | |||
38 | return; | 38 | return; |
39 | } | 39 | } |
40 | mAccountIdentifier = id; | 40 | mAccountIdentifier = id; |
41 | |||
42 | //Clear | ||
43 | mIcon = QString(); | ||
44 | mName = QString(); | ||
45 | mPath = QString(); | ||
46 | mSmtpServer = QString(); | ||
47 | mSmtpUsername = QString(); | ||
48 | mSmtpPassword = QString(); | ||
49 | emit changed(); | ||
50 | emit pathChanged(); | ||
51 | emit smtpResourceChanged(); | ||
52 | |||
41 | Q_ASSERT(!id.isEmpty()); | 53 | Q_ASSERT(!id.isEmpty()); |
42 | Sink::Store::fetchOne<Sink::ApplicationDomain::SinkAccount>(Sink::Query::IdentityFilter(id)) | 54 | Sink::Store::fetchOne<Sink::ApplicationDomain::SinkAccount>(Sink::Query::IdentityFilter(id)) |
43 | .then<void, Sink::ApplicationDomain::SinkAccount>([this](const Sink::ApplicationDomain::SinkAccount &account) { | 55 | .then<void, Sink::ApplicationDomain::SinkAccount>([this](const Sink::ApplicationDomain::SinkAccount &account) { |
@@ -46,7 +58,7 @@ void MaildirSettings::setAccountIdentifier(const QByteArray &id) | |||
46 | emit changed(); | 58 | emit changed(); |
47 | }).exec(); | 59 | }).exec(); |
48 | 60 | ||
49 | Sink::Store::fetchOne<Sink::ApplicationDomain::SinkResource>(Sink::Query::PropertyFilter("account", QVariant::fromValue(id))) | 61 | Sink::Store::fetchOne<Sink::ApplicationDomain::SinkResource>(Sink::Query::PropertyFilter("account", QVariant::fromValue(id)) + Sink::Query::PropertyFilter("type", QString("org.kde.maildir"))) |
50 | .then<void, Sink::ApplicationDomain::SinkResource>([this](const Sink::ApplicationDomain::SinkResource &resource) { | 62 | .then<void, Sink::ApplicationDomain::SinkResource>([this](const Sink::ApplicationDomain::SinkResource &resource) { |
51 | mIdentifier = resource.identifier(); | 63 | mIdentifier = resource.identifier(); |
52 | auto path = resource.getProperty("path").toString(); | 64 | auto path = resource.getProperty("path").toString(); |
@@ -54,6 +66,21 @@ void MaildirSettings::setAccountIdentifier(const QByteArray &id) | |||
54 | mPath = path; | 66 | mPath = path; |
55 | emit pathChanged(); | 67 | emit pathChanged(); |
56 | } | 68 | } |
69 | }, | ||
70 | [](int errorCode, const QString &errorMessage) { | ||
71 | qWarning() << "Failed to find the maildir resource: " << errorMessage; | ||
72 | }).exec(); | ||
73 | |||
74 | Sink::Store::fetchOne<Sink::ApplicationDomain::SinkResource>(Sink::Query::PropertyFilter("account", QVariant::fromValue(id)) + Sink::Query::PropertyFilter("type", QString("org.kde.mailtransport"))) | ||
75 | .then<void, Sink::ApplicationDomain::SinkResource>([this](const Sink::ApplicationDomain::SinkResource &resource) { | ||
76 | mMailtransportIdentifier = resource.identifier(); | ||
77 | mSmtpServer = resource.getProperty("server").toString(); | ||
78 | mSmtpUsername = resource.getProperty("username").toString(); | ||
79 | mSmtpPassword = resource.getProperty("password").toString(); | ||
80 | emit smtpResourceChanged(); | ||
81 | }, | ||
82 | [](int errorCode, const QString &errorMessage) { | ||
83 | qWarning() << "Failed to find the maildir resource: " << errorMessage; | ||
57 | }).exec(); | 84 | }).exec(); |
58 | } | 85 | } |
59 | 86 | ||
@@ -92,13 +119,32 @@ QValidator *MaildirSettings::pathValidator() const | |||
92 | return pathValidator; | 119 | return pathValidator; |
93 | } | 120 | } |
94 | 121 | ||
122 | QValidator *MaildirSettings::smtpServerValidator() const | ||
123 | { | ||
124 | class SmtpServerValidator : public QValidator { | ||
125 | State validate(QString &input, int &pos) const { | ||
126 | Q_UNUSED(pos); | ||
127 | // smtps://mainserver.example.net:475 | ||
128 | const QUrl url(input); | ||
129 | static QSet<QString> validProtocols = QSet<QString>() << "smtp" << "smtps"; | ||
130 | if (url.isValid() && validProtocols.contains(url.scheme().toLower())) { | ||
131 | return Acceptable; | ||
132 | } else { | ||
133 | return Intermediate; | ||
134 | } | ||
135 | } | ||
136 | }; | ||
137 | static SmtpServerValidator *validator = new SmtpServerValidator; | ||
138 | return validator; | ||
139 | } | ||
140 | |||
95 | void MaildirSettings::save() | 141 | void MaildirSettings::save() |
96 | { | 142 | { |
97 | if (!QDir(mPath).exists()) { | 143 | if (!QDir(mPath).exists()) { |
98 | qWarning() << "The path doesn't exist: " << mPath; | 144 | qWarning() << "The path doesn't exist: " << mPath; |
99 | return; | 145 | return; |
100 | } | 146 | } |
101 | qDebug() << "Saving account " << mAccountIdentifier << mIdentifier; | 147 | qDebug() << "Saving account " << mAccountIdentifier << mIdentifier << mMailtransportIdentifier; |
102 | Q_ASSERT(!mAccountIdentifier.isEmpty()); | 148 | Q_ASSERT(!mAccountIdentifier.isEmpty()); |
103 | Sink::ApplicationDomain::SinkAccount account(mAccountIdentifier); | 149 | Sink::ApplicationDomain::SinkAccount account(mAccountIdentifier); |
104 | account.setProperty("type", "maildir"); | 150 | account.setProperty("type", "maildir"); |
@@ -133,6 +179,34 @@ void MaildirSettings::save() | |||
133 | }) | 179 | }) |
134 | .exec(); | 180 | .exec(); |
135 | } | 181 | } |
182 | |||
183 | if (!mMailtransportIdentifier.isEmpty()) { | ||
184 | Sink::ApplicationDomain::SinkResource resource(mMailtransportIdentifier); | ||
185 | resource.setProperty("server", mSmtpServer); | ||
186 | resource.setProperty("username", mSmtpUsername); | ||
187 | resource.setProperty("password", mSmtpPassword); | ||
188 | Sink::Store::modify(resource).then<void>([](){}, [](int errorCode, const QString &errorMessage) { | ||
189 | qWarning() << "Error while modifying resource: " << errorMessage; | ||
190 | }) | ||
191 | .exec(); | ||
192 | } else { | ||
193 | //FIXME we shouldn't have to do this magic | ||
194 | const auto resourceIdentifier = "org.kde.mailtransport." + QUuid::createUuid().toByteArray(); | ||
195 | mMailtransportIdentifier = resourceIdentifier; | ||
196 | |||
197 | Sink::ApplicationDomain::SinkResource resource; | ||
198 | resource.setProperty("identifier", resourceIdentifier); | ||
199 | resource.setProperty("type", "org.kde.mailtransport"); | ||
200 | resource.setProperty("account", mAccountIdentifier); | ||
201 | resource.setProperty("server", mSmtpServer); | ||
202 | resource.setProperty("username", mSmtpUsername); | ||
203 | resource.setProperty("password", mSmtpPassword); | ||
204 | Sink::Store::create(resource).then<void>([]() {}, | ||
205 | [](int errorCode, const QString &errorMessage) { | ||
206 | qWarning() << "Error while creating resource: " << errorMessage; | ||
207 | }) | ||
208 | .exec(); | ||
209 | } | ||
136 | } | 210 | } |
137 | 211 | ||
138 | void MaildirSettings::remove() | 212 | void MaildirSettings::remove() |
@@ -140,6 +214,13 @@ void MaildirSettings::remove() | |||
140 | if (mIdentifier.isEmpty()) { | 214 | if (mIdentifier.isEmpty()) { |
141 | qWarning() << "We're missing an identifier"; | 215 | qWarning() << "We're missing an identifier"; |
142 | } else { | 216 | } else { |
217 | Sink::ApplicationDomain::SinkResource mailTransportResource("", mMailtransportIdentifier, 0, QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create()); | ||
218 | Sink::Store::remove(mailTransportResource).then<void>([]() {}, | ||
219 | [](int errorCode, const QString &errorMessage) { | ||
220 | qWarning() << "Error while removing resource: " << errorMessage; | ||
221 | }) | ||
222 | .exec(); | ||
223 | |||
143 | Sink::ApplicationDomain::SinkResource resource("", mIdentifier, 0, QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create()); | 224 | Sink::ApplicationDomain::SinkResource resource("", mIdentifier, 0, QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create()); |
144 | Sink::Store::remove(resource).then<void>([]() {}, | 225 | Sink::Store::remove(resource).then<void>([]() {}, |
145 | [](int errorCode, const QString &errorMessage) { | 226 | [](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 | |||
29 | Q_PROPERTY(QValidator* pathValidator READ pathValidator CONSTANT) | 29 | Q_PROPERTY(QValidator* pathValidator READ pathValidator CONSTANT) |
30 | Q_PROPERTY(QString icon MEMBER mIcon NOTIFY changed) | 30 | Q_PROPERTY(QString icon MEMBER mIcon NOTIFY changed) |
31 | Q_PROPERTY(QString accountName MEMBER mName NOTIFY changed) | 31 | Q_PROPERTY(QString accountName MEMBER mName NOTIFY changed) |
32 | Q_PROPERTY(QString smtpServer MEMBER mSmtpServer NOTIFY smtpResourceChanged) | ||
33 | Q_PROPERTY(QValidator* smtpServerValidator READ smtpServerValidator CONSTANT) | ||
34 | Q_PROPERTY(QString smtpUsername MEMBER mSmtpUsername NOTIFY smtpResourceChanged) | ||
35 | Q_PROPERTY(QString smtpPassword MEMBER mSmtpPassword NOTIFY smtpResourceChanged) | ||
32 | 36 | ||
33 | public: | 37 | public: |
34 | MaildirSettings(QObject *parent = 0); | 38 | MaildirSettings(QObject *parent = 0); |
@@ -40,17 +44,24 @@ public: | |||
40 | QUrl path() const; | 44 | QUrl path() const; |
41 | QValidator *pathValidator() const; | 45 | QValidator *pathValidator() const; |
42 | 46 | ||
47 | QValidator *smtpServerValidator() const; | ||
48 | |||
43 | Q_INVOKABLE void save(); | 49 | Q_INVOKABLE void save(); |
44 | Q_INVOKABLE void remove(); | 50 | Q_INVOKABLE void remove(); |
45 | 51 | ||
46 | signals: | 52 | signals: |
47 | void pathChanged(); | 53 | void pathChanged(); |
54 | void smtpResourceChanged(); | ||
48 | void changed(); | 55 | void changed(); |
49 | 56 | ||
50 | private: | 57 | private: |
51 | QByteArray mIdentifier; | 58 | QByteArray mIdentifier; |
52 | QByteArray mAccountIdentifier; | 59 | QByteArray mAccountIdentifier; |
60 | QByteArray mMailtransportIdentifier; | ||
53 | QString mPath; | 61 | QString mPath; |
54 | QString mIcon; | 62 | QString mIcon; |
55 | QString mName; | 63 | QString mName; |
64 | QString mSmtpServer; | ||
65 | QString mSmtpUsername; | ||
66 | QString mSmtpPassword; | ||
56 | }; | 67 | }; |
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 { | |||
78 | Button { | 78 | Button { |
79 | iconName: "folder" | 79 | iconName: "folder" |
80 | onClicked: { | 80 | onClicked: { |
81 | fileDialogObject = fileDialogComponent.createObject(parent); | 81 | fileDialogComponent.createObject(parent); |
82 | } | 82 | } |
83 | 83 | ||
84 | Component { | 84 | Component { |
@@ -92,10 +92,8 @@ Rectangle { | |||
92 | 92 | ||
93 | onAccepted: { | 93 | onAccepted: { |
94 | maildirSettings.path = fileDialog.fileUrl | 94 | maildirSettings.path = fileDialog.fileUrl |
95 | fileDialogObject.destroy() | ||
96 | } | 95 | } |
97 | onRejected: { | 96 | onRejected: { |
98 | fileDialogObject.destroy() | ||
99 | } | 97 | } |
100 | } | 98 | } |
101 | } | 99 | } |
@@ -110,29 +108,33 @@ Rectangle { | |||
110 | 108 | ||
111 | Label { text: "Username" } | 109 | Label { text: "Username" } |
112 | TextField { | 110 | TextField { |
113 | id: username | 111 | placeholderText: "Username" |
114 | placeholderText: "username" | ||
115 | Layout.fillWidth: true | 112 | Layout.fillWidth: true |
116 | text: transportSettings.username | 113 | text: maildirSettings.smtpUsername |
117 | onTextChanged: { transportSettings.username = text; } | 114 | onTextChanged: { maildirSettings.smtpUsername = text; } |
118 | } | 115 | } |
119 | 116 | ||
120 | Label { text: "Password" } | 117 | Label { text: "Password" } |
121 | TextField { | 118 | TextField { |
122 | id: password | 119 | placeholderText: "Password" |
123 | placeholderText: "password" | ||
124 | Layout.fillWidth: true | 120 | Layout.fillWidth: true |
125 | text: transportSettings.password | 121 | text: maildirSettings.smtpPassword |
126 | onTextChanged: { transportSettings.password = text; } | 122 | onTextChanged: { maildirSettings.smtpPassword = text; } |
127 | } | 123 | } |
128 | 124 | ||
129 | Label { text: "Server" } | 125 | Label { text: "Server" } |
130 | TextField { | 126 | TextField { |
131 | id: server | 127 | id: server |
132 | placeholderText: "server" | 128 | placeholderText: "smtps://mainserver.example.net:465" |
133 | Layout.fillWidth: true | 129 | Layout.fillWidth: true |
134 | text: transportSettings.server | 130 | text: maildirSettings.smtpServer |
135 | onTextChanged: { transportSettings.server = text; } | 131 | onTextChanged: { maildirSettings.smtpServer = text; } |
132 | validator: maildirSettings.smtpServerValidator | ||
133 | Rectangle { | ||
134 | anchors.fill: parent | ||
135 | opacity: 0.2 | ||
136 | color: server.acceptableInput ? "green" : "yellow" | ||
137 | } | ||
136 | } | 138 | } |
137 | 139 | ||
138 | MaildirAccount.MaildirSettings { | 140 | MaildirAccount.MaildirSettings { |
@@ -140,19 +142,9 @@ Rectangle { | |||
140 | accountIdentifier: accountId | 142 | accountIdentifier: accountId |
141 | } | 143 | } |
142 | 144 | ||
143 | KubeSettings.Settings { | ||
144 | id: transportSettings | ||
145 | //TODO set a proper identifier | ||
146 | identifier: "transport.current" | ||
147 | property string server; | ||
148 | property string username; | ||
149 | property string password; | ||
150 | } | ||
151 | |||
152 | Button { | 145 | Button { |
153 | text: "Save" | 146 | text: "Save" |
154 | onClicked: { | 147 | onClicked: { |
155 | transportSettings.save(); | ||
156 | maildirSettings.save(); | 148 | maildirSettings.save(); |
157 | } | 149 | } |
158 | } | 150 | } |
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: | |||
23 | { | 23 | { |
24 | auto accountId = "accountid"; | 24 | auto accountId = "accountid"; |
25 | auto maildirPath = QDir::tempPath(); | 25 | auto maildirPath = QDir::tempPath(); |
26 | auto smtpServer = QString("smtpserver"); | ||
27 | auto smtpUsername = QString("username"); | ||
28 | auto smtpPassword = QString("password"); | ||
26 | 29 | ||
27 | MaildirSettings settings; | 30 | MaildirSettings settings; |
28 | settings.setAccountIdentifier(accountId); | 31 | settings.setAccountIdentifier(accountId); |
29 | settings.setPath(maildirPath); | 32 | settings.setPath(maildirPath); |
33 | settings.setProperty("smtpServer", smtpServer); | ||
34 | settings.setProperty("smtpUsername", smtpUsername); | ||
35 | settings.setProperty("smtpPassword", smtpPassword); | ||
30 | settings.save(); | 36 | settings.save(); |
31 | 37 | ||
32 | Sink::Store::fetchAll<Sink::ApplicationDomain::SinkResource>(Sink::Query()).then<void, QList<Sink::ApplicationDomain::SinkResource>>([](const QList<Sink::ApplicationDomain::SinkResource> &resources) { | 38 | Sink::Store::fetchAll<Sink::ApplicationDomain::SinkResource>(Sink::Query()).then<void, QList<Sink::ApplicationDomain::SinkResource>>([](const QList<Sink::ApplicationDomain::SinkResource> &resources) { |
33 | QCOMPARE(resources.size(), 1); | 39 | QCOMPARE(resources.size(), 2); |
34 | }) | 40 | }) |
35 | .exec().waitForFinished(); | 41 | .exec().waitForFinished(); |
36 | 42 | ||
@@ -38,10 +44,15 @@ private slots: | |||
38 | { | 44 | { |
39 | MaildirSettings readSettings; | 45 | MaildirSettings readSettings; |
40 | QSignalSpy spy(&readSettings, &MaildirSettings::pathChanged); | 46 | QSignalSpy spy(&readSettings, &MaildirSettings::pathChanged); |
47 | QSignalSpy spy1(&readSettings, &MaildirSettings::smtpResourceChanged); | ||
41 | readSettings.setAccountIdentifier(accountId); | 48 | readSettings.setAccountIdentifier(accountId); |
42 | QTRY_VERIFY(spy.count()); | 49 | QTRY_VERIFY(spy.count()); |
50 | QTRY_VERIFY(spy1.count()); | ||
43 | QVERIFY(!readSettings.accountIdentifier().isEmpty()); | 51 | QVERIFY(!readSettings.accountIdentifier().isEmpty()); |
44 | QCOMPARE(readSettings.path().toString(), maildirPath); | 52 | QCOMPARE(readSettings.path().toString(), maildirPath); |
53 | QCOMPARE(readSettings.property("smtpServer").toString(), smtpServer); | ||
54 | QCOMPARE(readSettings.property("smtpUsername").toString(), smtpUsername); | ||
55 | QCOMPARE(readSettings.property("smtpPassword").toString(), smtpPassword); | ||
45 | } | 56 | } |
46 | 57 | ||
47 | { | 58 | { |
diff --git a/components/package/contents/ui/Composer.qml b/components/package/contents/ui/Composer.qml index 16cd9830..34fd3ca4 100644 --- a/components/package/contents/ui/Composer.qml +++ b/components/package/contents/ui/Composer.qml | |||
@@ -67,14 +67,14 @@ Item { | |||
67 | } | 67 | } |
68 | 68 | ||
69 | ComboBox { | 69 | ComboBox { |
70 | id: identityCombo | ||
70 | model: composer.identityModel | 71 | model: composer.identityModel |
72 | textRole: "name" | ||
71 | 73 | ||
72 | Layout.fillWidth: true | 74 | Layout.fillWidth: true |
73 | 75 | ||
74 | currentIndex: composer.fromIndex | ||
75 | |||
76 | onCurrentIndexChanged: { | 76 | onCurrentIndexChanged: { |
77 | composer.fromIndex = currentIndex | 77 | composer.currentIdentityIndex = currentIndex |
78 | } | 78 | } |
79 | } | 79 | } |
80 | 80 | ||
diff --git a/framework/domain/actions/mailactions.cpp b/framework/domain/actions/mailactions.cpp index dab0533e..fde98c85 100644 --- a/framework/domain/actions/mailactions.cpp +++ b/framework/domain/actions/mailactions.cpp | |||
@@ -27,22 +27,3 @@ | |||
27 | 27 | ||
28 | using namespace Kube; | 28 | using namespace Kube; |
29 | 29 | ||
30 | static ActionHandlerHelper sendMailHandler("org.kde.kube.actions.sendmail", | ||
31 | [](Context *context) -> bool { | ||
32 | auto username = context->property("username").value<QByteArray>(); | ||
33 | auto password = context->property("password").value<QByteArray>(); | ||
34 | auto server = context->property("server").value<QByteArray>(); | ||
35 | auto message = context->property("message").value<KMime::Message::Ptr>(); | ||
36 | return !username.isEmpty() && !password.isEmpty() && !server.isEmpty() && message; | ||
37 | }, | ||
38 | [](Context *context) { | ||
39 | auto username = context->property("username").value<QByteArray>(); | ||
40 | auto password = context->property("password").value<QByteArray>(); | ||
41 | auto server = context->property("server").value<QByteArray>(); | ||
42 | //For ssl use "smtps://mainserver.example.net | ||
43 | QByteArray cacert; // = "/path/to/certificate.pem"; | ||
44 | auto message = context->property("message").value<KMime::Message::Ptr>(); | ||
45 | qWarning() << "Sending a mail: "; | ||
46 | MailTransport::sendMessage(message, server, username, password, cacert); | ||
47 | } | ||
48 | ); | ||
diff --git a/framework/domain/actions/sinkactions.cpp b/framework/domain/actions/sinkactions.cpp index 57d63752..dea6fc72 100644 --- a/framework/domain/actions/sinkactions.cpp +++ b/framework/domain/actions/sinkactions.cpp | |||
@@ -19,6 +19,9 @@ | |||
19 | #include <actions/context.h> | 19 | #include <actions/context.h> |
20 | #include <actions/actionhandler.h> | 20 | #include <actions/actionhandler.h> |
21 | 21 | ||
22 | #include <KMime/Message> | ||
23 | #include <QFile> | ||
24 | |||
22 | #include <sink/store.h> | 25 | #include <sink/store.h> |
23 | 26 | ||
24 | using namespace Kube; | 27 | using namespace Kube; |
@@ -70,6 +73,46 @@ static ActionHandlerHelper synchronizeHandler("org.kde.kube.actions.synchronize" | |||
70 | } | 73 | } |
71 | ); | 74 | ); |
72 | 75 | ||
76 | static ActionHandlerHelper sendMailHandler("org.kde.kube.actions.sendmail", | ||
77 | [](Context *context) -> bool { | ||
78 | auto accountId = context->property("accountId").value<QByteArray>(); | ||
79 | auto message = context->property("message").value<KMime::Message::Ptr>(); | ||
80 | return !accountId.isEmpty() && message; | ||
81 | }, | ||
82 | [](Context *context) { | ||
83 | auto accountId = context->property("accountId").value<QByteArray>(); | ||
84 | //For ssl use "smtps://mainserver.example.net | ||
85 | // QByteArray cacert; // = "/path/to/certificate.pem"; | ||
86 | auto message = context->property("message").value<KMime::Message::Ptr>(); | ||
87 | auto mimeMessage = Sink::Store::getTemporaryFilePath(); | ||
88 | QFile file(mimeMessage); | ||
89 | if (!file.open(QIODevice::ReadWrite)) { | ||
90 | qWarning() << "Failed to open the file: " << file.errorString() << mimeMessage; | ||
91 | return; | ||
92 | } | ||
93 | file.write(message->encodedContent()); | ||
94 | qWarning() << "Sending a mail: "; | ||
95 | |||
96 | Sink::Query query; | ||
97 | query += Sink::Query::PropertyFilter("type", "org.kde.mailtransport"); | ||
98 | query += Sink::Query::PropertyFilter("account", QVariant::fromValue(accountId)); | ||
99 | Sink::Store::fetchAll<Sink::ApplicationDomain::SinkResource>(query) | ||
100 | .then<void, QList<Sink::ApplicationDomain::SinkResource::Ptr>>([=](const QList<Sink::ApplicationDomain::SinkResource::Ptr> &resources) { | ||
101 | if (resources.isEmpty()) { | ||
102 | qWarning() << "Failed to find a mailtransport resource"; | ||
103 | } else { | ||
104 | auto resourceId = resources[0]->identifier(); | ||
105 | qDebug() << "Sending message via resource: " << resourceId; | ||
106 | Sink::ApplicationDomain::Mail mail(resourceId); | ||
107 | mail.setProperty("mimeMessage", mimeMessage); | ||
108 | Sink::Store::create(mail).exec(); | ||
109 | // return Sink::Store::create(mail); | ||
110 | } | ||
111 | return KAsync::error<void>(0, "Failed to find a MailTransport resource."); | ||
112 | }).exec(); | ||
113 | } | ||
114 | ); | ||
115 | |||
73 | // static ActionHandlerHelper saveAsDraft("org.kde.kube.actions.save-as-draft", | 116 | // static ActionHandlerHelper saveAsDraft("org.kde.kube.actions.save-as-draft", |
74 | // [](Context *context) -> bool { | 117 | // [](Context *context) -> bool { |
75 | // return context->property("mail").isValid(); | 118 | // return context->property("mail").isValid(); |
diff --git a/framework/domain/composercontroller.cpp b/framework/domain/composercontroller.cpp index 4ab4ac21..a383de26 100644 --- a/framework/domain/composercontroller.cpp +++ b/framework/domain/composercontroller.cpp | |||
@@ -26,12 +26,13 @@ | |||
26 | #include <KCodecs/KEmailAddress> | 26 | #include <KCodecs/KEmailAddress> |
27 | #include <QVariant> | 27 | #include <QVariant> |
28 | #include <QDebug> | 28 | #include <QDebug> |
29 | #include <QQmlEngine> | ||
29 | 30 | ||
31 | #include "accountsmodel.h" | ||
30 | #include "mailtemplates.h" | 32 | #include "mailtemplates.h" |
31 | 33 | ||
32 | ComposerController::ComposerController(QObject *parent) : QObject(parent) | 34 | ComposerController::ComposerController(QObject *parent) : QObject(parent) |
33 | { | 35 | { |
34 | m_identityModel << "Kuberich <kuberich@kolabnow.com>" << "Uni <kuberich@university.edu>" << "Spam <hello.spam@spam.to>"; | ||
35 | } | 36 | } |
36 | 37 | ||
37 | QString ComposerController::to() const | 38 | QString ComposerController::to() const |
@@ -99,22 +100,11 @@ void ComposerController::setBody(const QString &body) | |||
99 | } | 100 | } |
100 | } | 101 | } |
101 | 102 | ||
102 | QStringList ComposerController::identityModel() const | 103 | QAbstractItemModel *ComposerController::identityModel() const |
103 | { | 104 | { |
104 | return m_identityModel; | 105 | static auto accountsModel = new AccountsModel(); |
105 | } | 106 | QQmlEngine::setObjectOwnership(accountsModel, QQmlEngine::CppOwnership); |
106 | 107 | return accountsModel;; | |
107 | int ComposerController::fromIndex() const | ||
108 | { | ||
109 | return m_fromIndex; | ||
110 | } | ||
111 | |||
112 | void ComposerController::setFromIndex(int fromIndex) | ||
113 | { | ||
114 | if(m_fromIndex != fromIndex) { | ||
115 | m_fromIndex = fromIndex; | ||
116 | emit fromIndexChanged(); | ||
117 | } | ||
118 | } | 108 | } |
119 | 109 | ||
120 | QStringList ComposerController::attachemts() const | 110 | QStringList ComposerController::attachemts() const |
@@ -165,6 +155,8 @@ KMime::Message::Ptr ComposerController::assembleMessage() | |||
165 | KEmailAddress::splitAddress(to.toUtf8(), displayName, addrSpec, comment); | 155 | KEmailAddress::splitAddress(to.toUtf8(), displayName, addrSpec, comment); |
166 | mail->to(true)->addAddress(addrSpec, displayName); | 156 | mail->to(true)->addAddress(addrSpec, displayName); |
167 | } | 157 | } |
158 | //FIXME set "from" from identity (or do that in the action directly?) | ||
159 | // mail->from(true)->addAddress("test@example.com", "John Doe"); | ||
168 | mail->subject(true)->fromUnicodeString(m_subject, "utf-8"); | 160 | mail->subject(true)->fromUnicodeString(m_subject, "utf-8"); |
169 | mail->setBody(m_body.toUtf8()); | 161 | mail->setBody(m_body.toUtf8()); |
170 | mail->assemble(); | 162 | mail->assemble(); |
@@ -174,17 +166,13 @@ KMime::Message::Ptr ComposerController::assembleMessage() | |||
174 | void ComposerController::send() | 166 | void ComposerController::send() |
175 | { | 167 | { |
176 | auto mail = assembleMessage(); | 168 | auto mail = assembleMessage(); |
177 | Kube::ApplicationContext settings; | 169 | auto currentAccountId = identityModel()->index(m_currentAccountIndex, 0).data(AccountsModel::AccountId).toByteArray(); |
178 | auto account = settings.currentAccount(); | ||
179 | auto identity = account.primaryIdentity(); | ||
180 | auto transport = identity.transport(); | ||
181 | 170 | ||
182 | Kube::Context context; | 171 | Kube::Context context; |
183 | context.setProperty("message", QVariant::fromValue(mail)); | 172 | context.setProperty("message", QVariant::fromValue(mail)); |
173 | context.setProperty("accountId", QVariant::fromValue(currentAccountId)); | ||
184 | 174 | ||
185 | context.setProperty("username", transport.username()); | 175 | qDebug() << "Current account " << currentAccountId; |
186 | context.setProperty("password", transport.password()); | ||
187 | context.setProperty("server", transport.server()); | ||
188 | 176 | ||
189 | Kube::Action("org.kde.kube.actions.sendmail", context).execute(); | 177 | Kube::Action("org.kde.kube.actions.sendmail", context).execute(); |
190 | clear(); | 178 | clear(); |
@@ -206,5 +194,4 @@ void ComposerController::clear() | |||
206 | setTo(""); | 194 | setTo(""); |
207 | setCc(""); | 195 | setCc(""); |
208 | setBcc(""); | 196 | setBcc(""); |
209 | setFromIndex(-1); | ||
210 | } | 197 | } |
diff --git a/framework/domain/composercontroller.h b/framework/domain/composercontroller.h index b410ce9b..4ad505d8 100644 --- a/framework/domain/composercontroller.h +++ b/framework/domain/composercontroller.h | |||
@@ -23,6 +23,7 @@ | |||
23 | #include <QString> | 23 | #include <QString> |
24 | #include <QStringList> | 24 | #include <QStringList> |
25 | #include <QVariant> | 25 | #include <QVariant> |
26 | #include <QAbstractItemModel> | ||
26 | 27 | ||
27 | namespace KMime { | 28 | namespace KMime { |
28 | class Message; | 29 | class Message; |
@@ -37,8 +38,8 @@ class ComposerController : public QObject | |||
37 | Q_PROPERTY (QString bcc READ bcc WRITE setBcc NOTIFY bccChanged) | 38 | Q_PROPERTY (QString bcc READ bcc WRITE setBcc NOTIFY bccChanged) |
38 | Q_PROPERTY (QString subject READ subject WRITE setSubject NOTIFY subjectChanged) | 39 | Q_PROPERTY (QString subject READ subject WRITE setSubject NOTIFY subjectChanged) |
39 | Q_PROPERTY (QString body READ body WRITE setBody NOTIFY bodyChanged) | 40 | Q_PROPERTY (QString body READ body WRITE setBody NOTIFY bodyChanged) |
40 | Q_PROPERTY (QStringList identityModel READ identityModel) | 41 | Q_PROPERTY (QAbstractItemModel* identityModel READ identityModel CONSTANT) |
41 | Q_PROPERTY (int fromIndex READ fromIndex WRITE setFromIndex NOTIFY fromIndexChanged) | 42 | Q_PROPERTY (int currentIdentityIndex MEMBER m_currentAccountIndex) |
42 | Q_PROPERTY (QStringList attachments READ attachemts NOTIFY attachmentsChanged) | 43 | Q_PROPERTY (QStringList attachments READ attachemts NOTIFY attachmentsChanged) |
43 | 44 | ||
44 | public: | 45 | public: |
@@ -59,10 +60,7 @@ public: | |||
59 | QString body() const; | 60 | QString body() const; |
60 | void setBody(const QString &body); | 61 | void setBody(const QString &body); |
61 | 62 | ||
62 | QStringList identityModel() const; | 63 | QAbstractItemModel *identityModel() const; |
63 | |||
64 | int fromIndex() const; | ||
65 | void setFromIndex(int fromIndex); | ||
66 | 64 | ||
67 | QStringList attachemts() const; | 65 | QStringList attachemts() const; |
68 | 66 | ||
@@ -91,9 +89,8 @@ private: | |||
91 | QString m_bcc; | 89 | QString m_bcc; |
92 | QString m_subject; | 90 | QString m_subject; |
93 | QString m_body; | 91 | QString m_body; |
94 | QStringList m_identityModel; | ||
95 | int m_fromIndex; | ||
96 | QStringList m_attachments; | 92 | QStringList m_attachments; |
97 | QVariant m_originalMessage; | 93 | QVariant m_originalMessage; |
98 | QVariant m_msg; | 94 | QVariant m_msg; |
95 | int m_currentAccountIndex; | ||
99 | }; | 96 | }; |