1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
/*
Copyright (c) 2016 Michael Bohlender <michael.bohlender@kdemail.net>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "composercontroller.h"
#include <actions/context.h>
#include <actions/action.h>
#include <actions/actionhandler.h>
#include <settings/settings.h>
#include <KMime/Message>
#include <KCodecs/KEmailAddress>
#include <QVariant>
#include <QSortFilterProxyModel>
#include <QList>
#include <QDebug>
#include <QQmlEngine>
#include <sink/store.h>
#include <sink/log.h>
#include "accountsmodel.h"
#include "identitiesmodel.h"
#include "recepientautocompletionmodel.h"
#include "mailtemplates.h"
SINK_DEBUG_AREA("composercontroller");
Q_DECLARE_METATYPE(KMime::Types::Mailbox)
ComposerController::ComposerController(QObject *parent) : QObject(parent)
{
}
QString ComposerController::recepientSearchString() const
{
return QString();
}
Kube::Context* ComposerController::mailContext() const
{
return mContext;
}
void ComposerController::setMailContext(Kube::Context *context)
{
mContext = context;
}
void ComposerController::setRecepientSearchString(const QString &s)
{
if (auto model = static_cast<RecipientAutocompletionModel*>(recepientAutocompletionModel())) {
model->setFilter(s);
}
}
QAbstractItemModel *ComposerController::identityModel() const
{
static auto model = new IdentitiesModel();
QQmlEngine::setObjectOwnership(model, QQmlEngine::CppOwnership);
return model;
}
QAbstractItemModel *ComposerController::recepientAutocompletionModel() const
{
static auto model = new RecipientAutocompletionModel();
QQmlEngine::setObjectOwnership(model, QQmlEngine::CppOwnership);
return model;
}
void ComposerController::setMessage(const KMime::Message::Ptr &msg)
{
mContext->setProperty("to", msg->to(true)->asUnicodeString());
mContext->setProperty("cc", msg->cc(true)->asUnicodeString());
mContext->setProperty("subject", msg->subject(true)->asUnicodeString());
mContext->setProperty("body", msg->body());
mContext->setProperty("existingMessage", QVariant::fromValue(msg));
}
void ComposerController::loadMessage(const QVariant &message, bool loadAsDraft)
{
Sink::Query query(*message.value<Sink::ApplicationDomain::Mail::Ptr>());
query.request<Sink::ApplicationDomain::Mail::MimeMessage>();
Sink::Store::fetchOne<Sink::ApplicationDomain::Mail>(query).syncThen<void, Sink::ApplicationDomain::Mail>([this, loadAsDraft](const Sink::ApplicationDomain::Mail &mail) {
mContext->setProperty("existingMail", QVariant::fromValue(mail));
const auto mailData = KMime::CRLFtoLF(mail.getMimeMessage());
if (!mailData.isEmpty()) {
KMime::Message::Ptr mail(new KMime::Message);
mail->setContent(mailData);
mail->parse();
if (loadAsDraft) {
auto reply = MailTemplates::reply(mail);
//We assume reply
setMessage(reply);
} else {
setMessage(mail);
}
} else {
qWarning() << "Retrieved empty message";
}
}).exec();
}
void ComposerController::recordForAutocompletion(const QByteArray &addrSpec, const QByteArray &displayName)
{
if (auto model = static_cast<RecipientAutocompletionModel*>(recepientAutocompletionModel())) {
model->addEntry(addrSpec, displayName);
}
}
void applyAddresses(const QString &list, std::function<void(const QByteArray &, const QByteArray &)> callback)
{
for (const auto &to : KEmailAddress::splitAddressList(list)) {
QByteArray displayName;
QByteArray addrSpec;
QByteArray comment;
KEmailAddress::splitAddress(to.toUtf8(), displayName, addrSpec, comment);
callback(addrSpec, displayName);
}
}
void ComposerController::clear()
{
mContext->setProperty("subject", QVariant());
mContext->setProperty("body", QVariant());
mContext->setProperty("to", QVariant());
mContext->setProperty("cc", QVariant());
mContext->setProperty("bcc", QVariant());
}
Kube::ActionHandler *ComposerController::messageHandler()
{
return new Kube::ActionHandlerHelper(
[](Kube::Context *context) {
auto identity = context->property("identity");
return identity.isValid();
},
[this](Kube::Context *context) {
auto mail = context->property("existingMessage").value<KMime::Message::Ptr>();
if (!mail) {
mail = KMime::Message::Ptr::create();
}
applyAddresses(context->property("to").toString(), [&](const QByteArray &addrSpec, const QByteArray &displayName) {
mail->to(true)->addAddress(addrSpec, displayName);
recordForAutocompletion(addrSpec, displayName);
});
applyAddresses(context->property("cc").toString(), [&](const QByteArray &addrSpec, const QByteArray &displayName) {
mail->cc(true)->addAddress(addrSpec, displayName);
recordForAutocompletion(addrSpec, displayName);
});
applyAddresses(context->property("bcc").toString(), [&](const QByteArray &addrSpec, const QByteArray &displayName) {
mail->bcc(true)->addAddress(addrSpec, displayName);
recordForAutocompletion(addrSpec, displayName);
});
mail->from(true)->addAddress(context->property("identity").value<KMime::Types::Mailbox>());
mail->subject(true)->fromUnicodeString(context->property("subject").toString(), "utf-8");
mail->setBody(context->property("body").toString().toUtf8());
mail->assemble();
context->setProperty("message", QVariant::fromValue(mail));
}
);
}
Kube::Action* ComposerController::saveAsDraftAction()
{
auto action = new Kube::Action("org.kde.kube.actions.save-as-draft", *mContext);
action->addPreHandler(messageHandler());
return action;
}
Kube::Action* ComposerController::sendAction()
{
qWarning() << "send action";
auto action = new Kube::Action("org.kde.kube.actions.sendmail", *mContext);
// action->addPreHandler(identityHandler());
action->addPreHandler(messageHandler());
// action->addPreHandler(encryptionHandler());
return action;
}
void ComposerController::setCurrentIdentityIndex(int index)
{
m_currentAccountIndex = index;
auto currentIndex = identityModel()->index(m_currentAccountIndex, 0);
if (currentIndex.isValid()) {
auto currentAccountId = currentIndex.data(IdentitiesModel::AccountId).toByteArray();
SinkWarning() << "valid identity for index: " << index << " out of available in model: " << identityModel()->rowCount();
KMime::Types::Mailbox mb;
mb.setName(currentIndex.data(IdentitiesModel::Username).toString());
mb.setAddress(currentIndex.data(IdentitiesModel::Address).toString().toUtf8());
SinkLog() << "Setting current identity: " << mb.prettyAddress() << "Account: " << currentAccountId;
mContext->setProperty("identity", QVariant::fromValue(mb));
mContext->setProperty("accountId", QVariant::fromValue(currentAccountId));
} else {
SinkWarning() << "No valid identity for index: " << index << " out of available in model: " << identityModel()->rowCount();
}
}
int ComposerController::currentIdentityIndex() const
{
return m_currentAccountIndex;
}
|