summaryrefslogtreecommitdiffstats
path: root/framework/domain/composercontroller.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-12-29 20:23:58 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-12-29 20:37:22 +0100
commit48da7de1b6bcd87c57b4c7f60133f3f13fc4bff2 (patch)
treeafad136d1179480301646355fab0d95c1302d7e7 /framework/domain/composercontroller.cpp
parent08dbe5251cb818b4f548029a6e96b4500fe7a35f (diff)
downloadkube-48da7de1b6bcd87c57b4c7f60133f3f13fc4bff2.tar.gz
kube-48da7de1b6bcd87c57b4c7f60133f3f13fc4bff2.zip
A typesafe action context
Diffstat (limited to 'framework/domain/composercontroller.cpp')
-rw-r--r--framework/domain/composercontroller.cpp47
1 files changed, 19 insertions, 28 deletions
diff --git a/framework/domain/composercontroller.cpp b/framework/domain/composercontroller.cpp
index a47f4755..7129e342 100644
--- a/framework/domain/composercontroller.cpp
+++ b/framework/domain/composercontroller.cpp
@@ -44,6 +44,7 @@ Q_DECLARE_METATYPE(KMime::Types::Mailbox)
44 44
45ComposerController::ComposerController(QObject *parent) : QObject(parent) 45ComposerController::ComposerController(QObject *parent) : QObject(parent)
46{ 46{
47 QQmlEngine::setObjectOwnership(&mContext, QQmlEngine::CppOwnership);
47} 48}
48 49
49QString ComposerController::recepientSearchString() const 50QString ComposerController::recepientSearchString() const
@@ -51,14 +52,9 @@ QString ComposerController::recepientSearchString() const
51 return QString(); 52 return QString();
52} 53}
53 54
54Kube::Context* ComposerController::mailContext() const 55Kube::Context* ComposerController::mailContext()
55{ 56{
56 return mContext; 57 return &mContext;
57}
58
59void ComposerController::setMailContext(Kube::Context *context)
60{
61 mContext = context;
62} 58}
63 59
64void ComposerController::setRecepientSearchString(const QString &s) 60void ComposerController::setRecepientSearchString(const QString &s)
@@ -84,11 +80,11 @@ QAbstractItemModel *ComposerController::recepientAutocompletionModel() const
84 80
85void ComposerController::setMessage(const KMime::Message::Ptr &msg) 81void ComposerController::setMessage(const KMime::Message::Ptr &msg)
86{ 82{
87 mContext->setProperty("to", msg->to(true)->asUnicodeString()); 83 mContext.setTo(msg->to(true)->asUnicodeString());
88 mContext->setProperty("cc", msg->cc(true)->asUnicodeString()); 84 mContext.setCc(msg->cc(true)->asUnicodeString());
89 mContext->setProperty("subject", msg->subject(true)->asUnicodeString()); 85 mContext.setSubject(msg->subject(true)->asUnicodeString());
90 mContext->setProperty("body", msg->body()); 86 mContext.setBody(msg->body());
91 mContext->setProperty("existingMessage", QVariant::fromValue(msg)); 87 mContext.setProperty("existingMessage", QVariant::fromValue(msg));
92} 88}
93 89
94void ComposerController::loadMessage(const QVariant &message, bool loadAsDraft) 90void ComposerController::loadMessage(const QVariant &message, bool loadAsDraft)
@@ -96,7 +92,7 @@ void ComposerController::loadMessage(const QVariant &message, bool loadAsDraft)
96 Sink::Query query(*message.value<Sink::ApplicationDomain::Mail::Ptr>()); 92 Sink::Query query(*message.value<Sink::ApplicationDomain::Mail::Ptr>());
97 query.request<Sink::ApplicationDomain::Mail::MimeMessage>(); 93 query.request<Sink::ApplicationDomain::Mail::MimeMessage>();
98 Sink::Store::fetchOne<Sink::ApplicationDomain::Mail>(query).syncThen<void, Sink::ApplicationDomain::Mail>([this, loadAsDraft](const Sink::ApplicationDomain::Mail &mail) { 94 Sink::Store::fetchOne<Sink::ApplicationDomain::Mail>(query).syncThen<void, Sink::ApplicationDomain::Mail>([this, loadAsDraft](const Sink::ApplicationDomain::Mail &mail) {
99 mContext->setProperty("existingMail", QVariant::fromValue(mail)); 95 mContext.setProperty("existingMail", QVariant::fromValue(mail));
100 const auto mailData = KMime::CRLFtoLF(mail.getMimeMessage()); 96 const auto mailData = KMime::CRLFtoLF(mail.getMimeMessage());
101 if (!mailData.isEmpty()) { 97 if (!mailData.isEmpty()) {
102 KMime::Message::Ptr mail(new KMime::Message); 98 KMime::Message::Ptr mail(new KMime::Message);
@@ -135,11 +131,7 @@ void applyAddresses(const QString &list, std::function<void(const QByteArray &,
135 131
136void ComposerController::clear() 132void ComposerController::clear()
137{ 133{
138 mContext->setProperty("subject", QVariant()); 134 mContext.clear();
139 mContext->setProperty("body", QVariant());
140 mContext->setProperty("to", QVariant());
141 mContext->setProperty("cc", QVariant());
142 mContext->setProperty("bcc", QVariant());
143} 135}
144 136
145 137
@@ -155,23 +147,23 @@ Kube::ActionHandler *ComposerController::messageHandler()
155 if (!mail) { 147 if (!mail) {
156 mail = KMime::Message::Ptr::create(); 148 mail = KMime::Message::Ptr::create();
157 } 149 }
158 applyAddresses(context->property("to").toString(), [&](const QByteArray &addrSpec, const QByteArray &displayName) { 150 applyAddresses(context->property(ComposerContext::To::name).toString(), [&](const QByteArray &addrSpec, const QByteArray &displayName) {
159 mail->to(true)->addAddress(addrSpec, displayName); 151 mail->to(true)->addAddress(addrSpec, displayName);
160 recordForAutocompletion(addrSpec, displayName); 152 recordForAutocompletion(addrSpec, displayName);
161 }); 153 });
162 applyAddresses(context->property("cc").toString(), [&](const QByteArray &addrSpec, const QByteArray &displayName) { 154 applyAddresses(context->property(ComposerContext::Cc::name).toString(), [&](const QByteArray &addrSpec, const QByteArray &displayName) {
163 mail->cc(true)->addAddress(addrSpec, displayName); 155 mail->cc(true)->addAddress(addrSpec, displayName);
164 recordForAutocompletion(addrSpec, displayName); 156 recordForAutocompletion(addrSpec, displayName);
165 }); 157 });
166 applyAddresses(context->property("bcc").toString(), [&](const QByteArray &addrSpec, const QByteArray &displayName) { 158 applyAddresses(context->property(ComposerContext::Bcc::name).toString(), [&](const QByteArray &addrSpec, const QByteArray &displayName) {
167 mail->bcc(true)->addAddress(addrSpec, displayName); 159 mail->bcc(true)->addAddress(addrSpec, displayName);
168 recordForAutocompletion(addrSpec, displayName); 160 recordForAutocompletion(addrSpec, displayName);
169 }); 161 });
170 162
171 mail->from(true)->addAddress(context->property("identity").value<KMime::Types::Mailbox>()); 163 mail->from(true)->addAddress(context->property("identity").value<KMime::Types::Mailbox>());
172 164
173 mail->subject(true)->fromUnicodeString(context->property("subject").toString(), "utf-8"); 165 mail->subject(true)->fromUnicodeString(context->property(ComposerContext::Subject::name).toString(), "utf-8");
174 mail->setBody(context->property("body").toString().toUtf8()); 166 mail->setBody(context->property(ComposerContext::Body::name).toString().toUtf8());
175 mail->assemble(); 167 mail->assemble();
176 168
177 context->setProperty("message", QVariant::fromValue(mail)); 169 context->setProperty("message", QVariant::fromValue(mail));
@@ -181,7 +173,7 @@ Kube::ActionHandler *ComposerController::messageHandler()
181 173
182Kube::Action* ComposerController::saveAsDraftAction() 174Kube::Action* ComposerController::saveAsDraftAction()
183{ 175{
184 auto action = new Kube::Action("org.kde.kube.actions.save-as-draft", *mContext); 176 auto action = new Kube::Action("org.kde.kube.actions.save-as-draft", mContext);
185 action->addPreHandler(messageHandler()); 177 action->addPreHandler(messageHandler());
186 action->addPostHandler(new Kube::ActionHandlerHelper( 178 action->addPostHandler(new Kube::ActionHandlerHelper(
187 [this](Kube::Context *context) { 179 [this](Kube::Context *context) {
@@ -192,8 +184,7 @@ Kube::Action* ComposerController::saveAsDraftAction()
192 184
193Kube::Action* ComposerController::sendAction() 185Kube::Action* ComposerController::sendAction()
194{ 186{
195 qWarning() << "send action"; 187 auto action = new Kube::Action("org.kde.kube.actions.sendmail", mContext);
196 auto action = new Kube::Action("org.kde.kube.actions.sendmail", *mContext);
197 // action->addPreHandler(identityHandler()); 188 // action->addPreHandler(identityHandler());
198 action->addPreHandler(messageHandler()); 189 action->addPreHandler(messageHandler());
199 // action->addPreHandler(encryptionHandler()); 190 // action->addPreHandler(encryptionHandler());
@@ -214,8 +205,8 @@ void ComposerController::setCurrentIdentityIndex(int index)
214 mb.setName(currentIndex.data(IdentitiesModel::Username).toString()); 205 mb.setName(currentIndex.data(IdentitiesModel::Username).toString());
215 mb.setAddress(currentIndex.data(IdentitiesModel::Address).toString().toUtf8()); 206 mb.setAddress(currentIndex.data(IdentitiesModel::Address).toString().toUtf8());
216 SinkLog() << "Setting current identity: " << mb.prettyAddress() << "Account: " << currentAccountId; 207 SinkLog() << "Setting current identity: " << mb.prettyAddress() << "Account: " << currentAccountId;
217 mContext->setProperty("identity", QVariant::fromValue(mb)); 208 mContext.setProperty("identity", QVariant::fromValue(mb));
218 mContext->setProperty("accountId", QVariant::fromValue(currentAccountId)); 209 mContext.setProperty("accountId", QVariant::fromValue(currentAccountId));
219 } else { 210 } else {
220 SinkWarning() << "No valid identity for index: " << index << " out of available in model: " << identityModel()->rowCount(); 211 SinkWarning() << "No valid identity for index: " << index << " out of available in model: " << identityModel()->rowCount();
221 } 212 }