diff options
Diffstat (limited to 'framework/domain/composercontroller.cpp')
-rw-r--r-- | framework/domain/composercontroller.cpp | 106 |
1 files changed, 85 insertions, 21 deletions
diff --git a/framework/domain/composercontroller.cpp b/framework/domain/composercontroller.cpp index b2ad7ecc..7fd2593d 100644 --- a/framework/domain/composercontroller.cpp +++ b/framework/domain/composercontroller.cpp | |||
@@ -25,14 +25,20 @@ | |||
25 | #include <KMime/Message> | 25 | #include <KMime/Message> |
26 | #include <KCodecs/KEmailAddress> | 26 | #include <KCodecs/KEmailAddress> |
27 | #include <QVariant> | 27 | #include <QVariant> |
28 | #include <QSortFilterProxyModel> | ||
29 | #include <QList> | ||
28 | #include <QDebug> | 30 | #include <QDebug> |
29 | #include <QQmlEngine> | 31 | #include <QQmlEngine> |
30 | #include <sink/store.h> | 32 | #include <sink/store.h> |
33 | #include <sink/log.h> | ||
31 | 34 | ||
32 | #include "accountsmodel.h" | 35 | #include "accountsmodel.h" |
33 | #include "identitiesmodel.h" | 36 | #include "identitiesmodel.h" |
37 | #include "recepientautocompletionmodel.h" | ||
34 | #include "mailtemplates.h" | 38 | #include "mailtemplates.h" |
35 | 39 | ||
40 | SINK_DEBUG_AREA("composercontroller"); | ||
41 | |||
36 | ComposerController::ComposerController(QObject *parent) : QObject(parent) | 42 | ComposerController::ComposerController(QObject *parent) : QObject(parent) |
37 | { | 43 | { |
38 | } | 44 | } |
@@ -102,6 +108,18 @@ void ComposerController::setBody(const QString &body) | |||
102 | } | 108 | } |
103 | } | 109 | } |
104 | 110 | ||
111 | QString ComposerController::recepientSearchString() const | ||
112 | { | ||
113 | return QString(); | ||
114 | } | ||
115 | |||
116 | void ComposerController::setRecepientSearchString(const QString &s) | ||
117 | { | ||
118 | if (auto model = static_cast<RecipientAutocompletionModel*>(recepientAutocompletionModel())) { | ||
119 | model->setFilter(s); | ||
120 | } | ||
121 | } | ||
122 | |||
105 | QAbstractItemModel *ComposerController::identityModel() const | 123 | QAbstractItemModel *ComposerController::identityModel() const |
106 | { | 124 | { |
107 | static auto model = new IdentitiesModel(); | 125 | static auto model = new IdentitiesModel(); |
@@ -109,6 +127,13 @@ QAbstractItemModel *ComposerController::identityModel() const | |||
109 | return model; | 127 | return model; |
110 | } | 128 | } |
111 | 129 | ||
130 | QAbstractItemModel *ComposerController::recepientAutocompletionModel() const | ||
131 | { | ||
132 | static auto model = new RecipientAutocompletionModel(); | ||
133 | QQmlEngine::setObjectOwnership(model, QQmlEngine::CppOwnership); | ||
134 | return model; | ||
135 | } | ||
136 | |||
112 | QStringList ComposerController::attachemts() const | 137 | QStringList ComposerController::attachemts() const |
113 | { | 138 | { |
114 | return m_attachments; | 139 | return m_attachments; |
@@ -153,43 +178,82 @@ void ComposerController::loadMessage(const QVariant &message, bool loadAsDraft) | |||
153 | }).exec(); | 178 | }).exec(); |
154 | } | 179 | } |
155 | 180 | ||
156 | KMime::Message::Ptr ComposerController::assembleMessage() | 181 | void ComposerController::recordForAutocompletion(const QByteArray &addrSpec, const QByteArray &displayName) |
157 | { | 182 | { |
158 | auto mail = m_msg.value<KMime::Message::Ptr>(); | 183 | if (auto model = static_cast<RecipientAutocompletionModel*>(recepientAutocompletionModel())) { |
159 | if (!mail) { | 184 | model->addEntry(addrSpec, displayName); |
160 | mail = KMime::Message::Ptr::create(); | ||
161 | } | 185 | } |
162 | for (const auto &to : KEmailAddress::splitAddressList(m_to)) { | 186 | } |
187 | |||
188 | void applyAddresses(const QString &list, std::function<void(const QByteArray &, const QByteArray &)> callback) | ||
189 | { | ||
190 | for (const auto &to : KEmailAddress::splitAddressList(list)) { | ||
163 | QByteArray displayName; | 191 | QByteArray displayName; |
164 | QByteArray addrSpec; | 192 | QByteArray addrSpec; |
165 | QByteArray comment; | 193 | QByteArray comment; |
166 | KEmailAddress::splitAddress(to.toUtf8(), displayName, addrSpec, comment); | 194 | KEmailAddress::splitAddress(to.toUtf8(), displayName, addrSpec, comment); |
195 | callback(addrSpec, displayName); | ||
196 | } | ||
197 | } | ||
198 | |||
199 | bool ComposerController::identityIsSet() const | ||
200 | { | ||
201 | return (identityModel()->rowCount() > 0) && (m_currentAccountIndex >= 0); | ||
202 | } | ||
203 | |||
204 | KMime::Message::Ptr ComposerController::assembleMessage() | ||
205 | { | ||
206 | auto mail = m_msg.value<KMime::Message::Ptr>(); | ||
207 | if (!mail) { | ||
208 | mail = KMime::Message::Ptr::create(); | ||
209 | } | ||
210 | applyAddresses(m_to, [&](const QByteArray &addrSpec, const QByteArray &displayName) { | ||
167 | mail->to(true)->addAddress(addrSpec, displayName); | 211 | mail->to(true)->addAddress(addrSpec, displayName); |
212 | recordForAutocompletion(addrSpec, displayName); | ||
213 | }); | ||
214 | applyAddresses(m_cc, [&](const QByteArray &addrSpec, const QByteArray &displayName) { | ||
215 | mail->cc(true)->addAddress(addrSpec, displayName); | ||
216 | recordForAutocompletion(addrSpec, displayName); | ||
217 | }); | ||
218 | applyAddresses(m_bcc, [&](const QByteArray &addrSpec, const QByteArray &displayName) { | ||
219 | mail->bcc(true)->addAddress(addrSpec, displayName); | ||
220 | recordForAutocompletion(addrSpec, displayName); | ||
221 | }); | ||
222 | if (!identityIsSet()) { | ||
223 | SinkWarning() << "We don't have an identity to send the mail with."; | ||
224 | } else { | ||
225 | auto currentIndex = identityModel()->index(m_currentAccountIndex, 0); | ||
226 | KMime::Types::Mailbox mb; | ||
227 | mb.setName(currentIndex.data(IdentitiesModel::Username).toString()); | ||
228 | mb.setAddress(currentIndex.data(IdentitiesModel::Address).toString().toUtf8()); | ||
229 | mail->from(true)->addAddress(mb); | ||
230 | mail->subject(true)->fromUnicodeString(m_subject, "utf-8"); | ||
231 | mail->setBody(m_body.toUtf8()); | ||
232 | mail->assemble(); | ||
233 | return mail; | ||
168 | } | 234 | } |
169 | auto currentIndex = identityModel()->index(m_currentAccountIndex, 0); | 235 | return KMime::Message::Ptr(); |
170 | KMime::Types::Mailbox mb; | ||
171 | mb.setName(currentIndex.data(IdentitiesModel::Username).toString()); | ||
172 | mb.setAddress(currentIndex.data(IdentitiesModel::Address).toString().toUtf8()); | ||
173 | mail->from(true)->addAddress(mb); | ||
174 | mail->subject(true)->fromUnicodeString(m_subject, "utf-8"); | ||
175 | mail->setBody(m_body.toUtf8()); | ||
176 | mail->assemble(); | ||
177 | return mail; | ||
178 | } | 236 | } |
179 | 237 | ||
180 | void ComposerController::send() | 238 | void ComposerController::send() |
181 | { | 239 | { |
182 | auto mail = assembleMessage(); | 240 | auto mail = assembleMessage(); |
183 | auto currentAccountId = identityModel()->index(m_currentAccountIndex, 0).data(IdentitiesModel::AccountId).toByteArray(); | ||
184 | 241 | ||
185 | Kube::Context context; | 242 | //TODO deactivate action if we don't have the identiy set |
186 | context.setProperty("message", QVariant::fromValue(mail)); | 243 | if (!identityIsSet()) { |
187 | context.setProperty("accountId", QVariant::fromValue(currentAccountId)); | 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(); | ||
188 | 247 | ||
189 | qDebug() << "Current account " << currentAccountId; | 248 | Kube::Context context; |
249 | context.setProperty("message", QVariant::fromValue(mail)); | ||
250 | context.setProperty("accountId", QVariant::fromValue(currentAccountId)); | ||
190 | 251 | ||
191 | Kube::Action("org.kde.kube.actions.sendmail", context).execute(); | 252 | qDebug() << "Current account " << currentAccountId; |
192 | clear(); | 253 | |
254 | Kube::Action("org.kde.kube.actions.sendmail", context).execute(); | ||
255 | clear(); | ||
256 | } | ||
193 | } | 257 | } |
194 | 258 | ||
195 | void ComposerController::saveAsDraft() | 259 | void ComposerController::saveAsDraft() |