diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-12-13 16:24:31 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-12-16 14:54:14 +0100 |
commit | 630f45719a527f8ee739b03bc62f886badea6df3 (patch) | |
tree | b9d859cbfedb30ad8a9570e3b87a419bf24ba6c7 /framework/domain/composercontroller.cpp | |
parent | b1a2e2de201985a00980bead5272977cda4ef637 (diff) | |
download | kube-630f45719a527f8ee739b03bc62f886badea6df3.tar.gz kube-630f45719a527f8ee739b03bc62f886badea6df3.zip |
Revamp of composercontroller to use actions more.
Instead of setting all properties individually we directly assign all
properties to a context that we assign to the actions.
This way actions can automatically update themselves as new data becomes
available, and we avoid the setter/getter boilerplate, at the cost of a
less explicit interface (But that could be improved by allowing to
define the required properties of a context in c++).
By relying on prehandler/posthandler to execute certain actions we
simplify the control flow and enable the future extension with handlers
that i.e. do encryption etc.
Diffstat (limited to 'framework/domain/composercontroller.cpp')
-rw-r--r-- | framework/domain/composercontroller.cpp | 229 |
1 files changed, 85 insertions, 144 deletions
diff --git a/framework/domain/composercontroller.cpp b/framework/domain/composercontroller.cpp index 7fd2593d..18ebc4c4 100644 --- a/framework/domain/composercontroller.cpp +++ b/framework/domain/composercontroller.cpp | |||
@@ -21,6 +21,7 @@ | |||
21 | #include "composercontroller.h" | 21 | #include "composercontroller.h" |
22 | #include <actions/context.h> | 22 | #include <actions/context.h> |
23 | #include <actions/action.h> | 23 | #include <actions/action.h> |
24 | #include <actions/actionhandler.h> | ||
24 | #include <settings/settings.h> | 25 | #include <settings/settings.h> |
25 | #include <KMime/Message> | 26 | #include <KMime/Message> |
26 | #include <KCodecs/KEmailAddress> | 27 | #include <KCodecs/KEmailAddress> |
@@ -39,78 +40,25 @@ | |||
39 | 40 | ||
40 | SINK_DEBUG_AREA("composercontroller"); | 41 | SINK_DEBUG_AREA("composercontroller"); |
41 | 42 | ||
42 | ComposerController::ComposerController(QObject *parent) : QObject(parent) | 43 | Q_DECLARE_METATYPE(KMime::Types::Mailbox) |
43 | { | ||
44 | } | ||
45 | |||
46 | QString ComposerController::to() const | ||
47 | { | ||
48 | return m_to; | ||
49 | } | ||
50 | |||
51 | void ComposerController::setTo(const QString &to) | ||
52 | { | ||
53 | if(m_to != to) { | ||
54 | m_to = to; | ||
55 | emit toChanged(); | ||
56 | } | ||
57 | } | ||
58 | |||
59 | QString ComposerController::cc() const | ||
60 | { | ||
61 | return m_cc; | ||
62 | } | ||
63 | |||
64 | void ComposerController::setCc(const QString &cc) | ||
65 | { | ||
66 | if(m_cc != cc) { | ||
67 | m_cc = cc; | ||
68 | emit ccChanged(); | ||
69 | } | ||
70 | } | ||
71 | |||
72 | QString ComposerController::bcc() const | ||
73 | { | ||
74 | return m_bcc; | ||
75 | } | ||
76 | |||
77 | void ComposerController::setBcc(const QString &bcc) | ||
78 | { | ||
79 | if(m_bcc != bcc) { | ||
80 | m_bcc = bcc; | ||
81 | emit bccChanged(); | ||
82 | } | ||
83 | } | ||
84 | 44 | ||
85 | QString ComposerController::subject() const | 45 | ComposerController::ComposerController(QObject *parent) : QObject(parent) |
86 | { | ||
87 | return m_subject; | ||
88 | } | ||
89 | |||
90 | void ComposerController::setSubject(const QString &subject) | ||
91 | { | 46 | { |
92 | if(m_subject != subject) { | ||
93 | m_subject = subject; | ||
94 | emit subjectChanged(); | ||
95 | } | ||
96 | } | 47 | } |
97 | 48 | ||
98 | QString ComposerController::body() const | 49 | QString ComposerController::recepientSearchString() const |
99 | { | 50 | { |
100 | return m_body; | 51 | return QString(); |
101 | } | 52 | } |
102 | 53 | ||
103 | void ComposerController::setBody(const QString &body) | 54 | Kube::Context* ComposerController::mailContext() const |
104 | { | 55 | { |
105 | if(m_body != body) { | 56 | return mContext; |
106 | m_body = body; | ||
107 | emit bodyChanged(); | ||
108 | } | ||
109 | } | 57 | } |
110 | 58 | ||
111 | QString ComposerController::recepientSearchString() const | 59 | void ComposerController::setMailContext(Kube::Context *context) |
112 | { | 60 | { |
113 | return QString(); | 61 | mContext = context; |
114 | } | 62 | } |
115 | 63 | ||
116 | void ComposerController::setRecepientSearchString(const QString &s) | 64 | void ComposerController::setRecepientSearchString(const QString &s) |
@@ -134,24 +82,13 @@ QAbstractItemModel *ComposerController::recepientAutocompletionModel() const | |||
134 | return model; | 82 | return model; |
135 | } | 83 | } |
136 | 84 | ||
137 | QStringList ComposerController::attachemts() const | ||
138 | { | ||
139 | return m_attachments; | ||
140 | } | ||
141 | |||
142 | void ComposerController::addAttachment(const QUrl &fileUrl) | ||
143 | { | ||
144 | m_attachments.append(fileUrl.toString()); | ||
145 | emit attachmentsChanged(); | ||
146 | } | ||
147 | |||
148 | void ComposerController::setMessage(const KMime::Message::Ptr &msg) | 85 | void ComposerController::setMessage(const KMime::Message::Ptr &msg) |
149 | { | 86 | { |
150 | setTo(msg->to(true)->asUnicodeString()); | 87 | mContext->setProperty("to", msg->to(true)->asUnicodeString()); |
151 | setCc(msg->cc(true)->asUnicodeString()); | 88 | mContext->setProperty("cc", msg->cc(true)->asUnicodeString()); |
152 | setSubject(msg->subject(true)->asUnicodeString()); | 89 | mContext->setProperty("subject", msg->subject(true)->asUnicodeString()); |
153 | setBody(msg->body()); | 90 | mContext->setProperty("body", msg->body()); |
154 | m_msg = QVariant::fromValue(msg); | 91 | mContext->setProperty("existingMessage", QVariant::fromValue(msg)); |
155 | } | 92 | } |
156 | 93 | ||
157 | void ComposerController::loadMessage(const QVariant &message, bool loadAsDraft) | 94 | void ComposerController::loadMessage(const QVariant &message, bool loadAsDraft) |
@@ -159,7 +96,7 @@ void ComposerController::loadMessage(const QVariant &message, bool loadAsDraft) | |||
159 | Sink::Query query(*message.value<Sink::ApplicationDomain::Mail::Ptr>()); | 96 | Sink::Query query(*message.value<Sink::ApplicationDomain::Mail::Ptr>()); |
160 | query.request<Sink::ApplicationDomain::Mail::MimeMessage>(); | 97 | query.request<Sink::ApplicationDomain::Mail::MimeMessage>(); |
161 | Sink::Store::fetchOne<Sink::ApplicationDomain::Mail>(query).syncThen<void, Sink::ApplicationDomain::Mail>([this, loadAsDraft](const Sink::ApplicationDomain::Mail &mail) { | 98 | Sink::Store::fetchOne<Sink::ApplicationDomain::Mail>(query).syncThen<void, Sink::ApplicationDomain::Mail>([this, loadAsDraft](const Sink::ApplicationDomain::Mail &mail) { |
162 | m_existingMail = mail; | 99 | mContext->setProperty("existingMail", QVariant::fromValue(mail)); |
163 | const auto mailData = KMime::CRLFtoLF(mail.getMimeMessage()); | 100 | const auto mailData = KMime::CRLFtoLF(mail.getMimeMessage()); |
164 | if (!mailData.isEmpty()) { | 101 | if (!mailData.isEmpty()) { |
165 | KMime::Message::Ptr mail(new KMime::Message); | 102 | KMime::Message::Ptr mail(new KMime::Message); |
@@ -196,84 +133,88 @@ void applyAddresses(const QString &list, std::function<void(const QByteArray &, | |||
196 | } | 133 | } |
197 | } | 134 | } |
198 | 135 | ||
199 | bool ComposerController::identityIsSet() const | 136 | void ComposerController::clear() |
200 | { | 137 | { |
201 | return (identityModel()->rowCount() > 0) && (m_currentAccountIndex >= 0); | 138 | mContext->setProperty("subject", QVariant()); |
139 | mContext->setProperty("body", QVariant()); | ||
140 | mContext->setProperty("to", QVariant()); | ||
141 | mContext->setProperty("cc", QVariant()); | ||
142 | mContext->setProperty("bcc", QVariant()); | ||
202 | } | 143 | } |
203 | 144 | ||
204 | KMime::Message::Ptr ComposerController::assembleMessage() | 145 | |
146 | Kube::ActionHandler *ComposerController::messageHandler() | ||
205 | { | 147 | { |
206 | auto mail = m_msg.value<KMime::Message::Ptr>(); | 148 | return new Kube::ActionHandlerHelper( |
207 | if (!mail) { | 149 | [](Kube::Context *context) { |
208 | mail = KMime::Message::Ptr::create(); | 150 | auto identity = context->property("identity"); |
209 | } | 151 | return identity.isValid(); |
210 | applyAddresses(m_to, [&](const QByteArray &addrSpec, const QByteArray &displayName) { | 152 | }, |
211 | mail->to(true)->addAddress(addrSpec, displayName); | 153 | [this](Kube::Context *context) { |
212 | recordForAutocompletion(addrSpec, displayName); | 154 | auto mail = context->property("existingMessage").value<KMime::Message::Ptr>(); |
213 | }); | 155 | if (!mail) { |
214 | applyAddresses(m_cc, [&](const QByteArray &addrSpec, const QByteArray &displayName) { | 156 | mail = KMime::Message::Ptr::create(); |
215 | mail->cc(true)->addAddress(addrSpec, displayName); | 157 | } |
216 | recordForAutocompletion(addrSpec, displayName); | 158 | applyAddresses(context->property("to").toString(), [&](const QByteArray &addrSpec, const QByteArray &displayName) { |
217 | }); | 159 | mail->to(true)->addAddress(addrSpec, displayName); |
218 | applyAddresses(m_bcc, [&](const QByteArray &addrSpec, const QByteArray &displayName) { | 160 | recordForAutocompletion(addrSpec, displayName); |
219 | mail->bcc(true)->addAddress(addrSpec, displayName); | 161 | }); |
220 | recordForAutocompletion(addrSpec, displayName); | 162 | applyAddresses(context->property("cc").toString(), [&](const QByteArray &addrSpec, const QByteArray &displayName) { |
221 | }); | 163 | mail->cc(true)->addAddress(addrSpec, displayName); |
222 | if (!identityIsSet()) { | 164 | recordForAutocompletion(addrSpec, displayName); |
223 | SinkWarning() << "We don't have an identity to send the mail with."; | 165 | }); |
224 | } else { | 166 | applyAddresses(context->property("bcc").toString(), [&](const QByteArray &addrSpec, const QByteArray &displayName) { |
225 | auto currentIndex = identityModel()->index(m_currentAccountIndex, 0); | 167 | mail->bcc(true)->addAddress(addrSpec, displayName); |
226 | KMime::Types::Mailbox mb; | 168 | recordForAutocompletion(addrSpec, displayName); |
227 | mb.setName(currentIndex.data(IdentitiesModel::Username).toString()); | 169 | }); |
228 | mb.setAddress(currentIndex.data(IdentitiesModel::Address).toString().toUtf8()); | 170 | |
229 | mail->from(true)->addAddress(mb); | 171 | mail->from(true)->addAddress(context->property("identity").value<KMime::Types::Mailbox>()); |
230 | mail->subject(true)->fromUnicodeString(m_subject, "utf-8"); | 172 | |
231 | mail->setBody(m_body.toUtf8()); | 173 | mail->subject(true)->fromUnicodeString(context->property("subject").toString(), "utf-8"); |
232 | mail->assemble(); | 174 | mail->setBody(context->property("body").toString().toUtf8()); |
233 | return mail; | 175 | mail->assemble(); |
234 | } | 176 | |
235 | return KMime::Message::Ptr(); | 177 | context->setProperty("message", QVariant::fromValue(mail)); |
178 | } | ||
179 | ); | ||
236 | } | 180 | } |
237 | 181 | ||
238 | void ComposerController::send() | 182 | Kube::Action* ComposerController::saveAsDraftAction() |
239 | { | 183 | { |
240 | auto mail = assembleMessage(); | 184 | auto action = new Kube::Action("org.kde.kube.actions.save-as-draft", *mContext); |
241 | 185 | action->addPreHandler(messageHandler()); | |
242 | //TODO deactivate action if we don't have the identiy set | 186 | return action; |
243 | if (!identityIsSet()) { | ||
244 | SinkWarning() << "We don't have an identity to send the mail with."; | ||
245 | } else { | ||
246 | auto currentAccountId = identityModel()->index(m_currentAccountIndex, 0).data(IdentitiesModel::AccountId).toByteArray(); | ||
247 | |||
248 | Kube::Context context; | ||
249 | context.setProperty("message", QVariant::fromValue(mail)); | ||
250 | context.setProperty("accountId", QVariant::fromValue(currentAccountId)); | ||
251 | |||
252 | qDebug() << "Current account " << currentAccountId; | ||
253 | |||
254 | Kube::Action("org.kde.kube.actions.sendmail", context).execute(); | ||
255 | clear(); | ||
256 | } | ||
257 | } | 187 | } |
258 | 188 | ||
259 | void ComposerController::saveAsDraft() | 189 | Kube::Action* ComposerController::sendAction() |
260 | { | 190 | { |
261 | auto mail = assembleMessage(); | 191 | qWarning() << "send action"; |
262 | auto currentAccountId = identityModel()->index(m_currentAccountIndex, 0).data(IdentitiesModel::AccountId).toByteArray(); | 192 | auto action = new Kube::Action("org.kde.kube.actions.sendmail", *mContext); |
193 | // action->addPreHandler(identityHandler()); | ||
194 | action->addPreHandler(messageHandler()); | ||
195 | // action->addPreHandler(encryptionHandler()); | ||
196 | return action; | ||
197 | } | ||
263 | 198 | ||
264 | Kube::Context context; | 199 | void ComposerController::setCurrentIdentityIndex(int index) |
265 | context.setProperty("message", QVariant::fromValue(mail)); | 200 | { |
266 | context.setProperty("accountId", QVariant::fromValue(currentAccountId)); | 201 | m_currentAccountIndex = index; |
267 | context.setProperty("existingMail", QVariant::fromValue(m_existingMail)); | 202 | auto currentIndex = identityModel()->index(m_currentAccountIndex, 0); |
268 | Kube::Action("org.kde.kube.actions.save-as-draft", context).execute(); | 203 | if (currentIndex.isValid()) { |
269 | clear(); | 204 | auto currentAccountId = currentIndex.data(IdentitiesModel::AccountId).toByteArray(); |
205 | SinkWarning() << "valid identity for index: " << index << " out of available in model: " << identityModel()->rowCount(); | ||
206 | KMime::Types::Mailbox mb; | ||
207 | mb.setName(currentIndex.data(IdentitiesModel::Username).toString()); | ||
208 | mb.setAddress(currentIndex.data(IdentitiesModel::Address).toString().toUtf8()); | ||
209 | SinkLog() << "Setting current identity: " << mb.prettyAddress() << "Account: " << currentAccountId; | ||
210 | mContext->setProperty("identity", QVariant::fromValue(mb)); | ||
211 | mContext->setProperty("accountId", QVariant::fromValue(currentAccountId)); | ||
212 | } else { | ||
213 | SinkWarning() << "No valid identity for index: " << index << " out of available in model: " << identityModel()->rowCount(); | ||
214 | } | ||
270 | } | 215 | } |
271 | 216 | ||
272 | void ComposerController::clear() | 217 | int ComposerController::currentIdentityIndex() const |
273 | { | 218 | { |
274 | setSubject(""); | 219 | return m_currentAccountIndex; |
275 | setBody(""); | ||
276 | setTo(""); | ||
277 | setCc(""); | ||
278 | setBcc(""); | ||
279 | } | 220 | } |