From 630f45719a527f8ee739b03bc62f886badea6df3 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 13 Dec 2016 16:24:31 +0100 Subject: 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. --- framework/domain/composercontroller.cpp | 229 ++++++++++++-------------------- 1 file changed, 85 insertions(+), 144 deletions(-) (limited to 'framework/domain/composercontroller.cpp') 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 @@ #include "composercontroller.h" #include #include +#include #include #include #include @@ -39,78 +40,25 @@ SINK_DEBUG_AREA("composercontroller"); -ComposerController::ComposerController(QObject *parent) : QObject(parent) -{ -} - -QString ComposerController::to() const -{ - return m_to; -} - -void ComposerController::setTo(const QString &to) -{ - if(m_to != to) { - m_to = to; - emit toChanged(); - } -} - -QString ComposerController::cc() const -{ - return m_cc; -} - -void ComposerController::setCc(const QString &cc) -{ - if(m_cc != cc) { - m_cc = cc; - emit ccChanged(); - } -} - -QString ComposerController::bcc() const -{ - return m_bcc; -} - -void ComposerController::setBcc(const QString &bcc) -{ - if(m_bcc != bcc) { - m_bcc = bcc; - emit bccChanged(); - } -} +Q_DECLARE_METATYPE(KMime::Types::Mailbox) -QString ComposerController::subject() const -{ - return m_subject; -} - -void ComposerController::setSubject(const QString &subject) +ComposerController::ComposerController(QObject *parent) : QObject(parent) { - if(m_subject != subject) { - m_subject = subject; - emit subjectChanged(); - } } -QString ComposerController::body() const +QString ComposerController::recepientSearchString() const { - return m_body; + return QString(); } -void ComposerController::setBody(const QString &body) +Kube::Context* ComposerController::mailContext() const { - if(m_body != body) { - m_body = body; - emit bodyChanged(); - } + return mContext; } -QString ComposerController::recepientSearchString() const +void ComposerController::setMailContext(Kube::Context *context) { - return QString(); + mContext = context; } void ComposerController::setRecepientSearchString(const QString &s) @@ -134,24 +82,13 @@ QAbstractItemModel *ComposerController::recepientAutocompletionModel() const return model; } -QStringList ComposerController::attachemts() const -{ - return m_attachments; -} - -void ComposerController::addAttachment(const QUrl &fileUrl) -{ - m_attachments.append(fileUrl.toString()); - emit attachmentsChanged(); -} - void ComposerController::setMessage(const KMime::Message::Ptr &msg) { - setTo(msg->to(true)->asUnicodeString()); - setCc(msg->cc(true)->asUnicodeString()); - setSubject(msg->subject(true)->asUnicodeString()); - setBody(msg->body()); - m_msg = QVariant::fromValue(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) @@ -159,7 +96,7 @@ void ComposerController::loadMessage(const QVariant &message, bool loadAsDraft) Sink::Query query(*message.value()); query.request(); Sink::Store::fetchOne(query).syncThen([this, loadAsDraft](const Sink::ApplicationDomain::Mail &mail) { - m_existingMail = mail; + mContext->setProperty("existingMail", QVariant::fromValue(mail)); const auto mailData = KMime::CRLFtoLF(mail.getMimeMessage()); if (!mailData.isEmpty()) { KMime::Message::Ptr mail(new KMime::Message); @@ -196,84 +133,88 @@ void applyAddresses(const QString &list, std::functionrowCount() > 0) && (m_currentAccountIndex >= 0); + mContext->setProperty("subject", QVariant()); + mContext->setProperty("body", QVariant()); + mContext->setProperty("to", QVariant()); + mContext->setProperty("cc", QVariant()); + mContext->setProperty("bcc", QVariant()); } -KMime::Message::Ptr ComposerController::assembleMessage() + +Kube::ActionHandler *ComposerController::messageHandler() { - auto mail = m_msg.value(); - if (!mail) { - mail = KMime::Message::Ptr::create(); - } - applyAddresses(m_to, [&](const QByteArray &addrSpec, const QByteArray &displayName) { - mail->to(true)->addAddress(addrSpec, displayName); - recordForAutocompletion(addrSpec, displayName); - }); - applyAddresses(m_cc, [&](const QByteArray &addrSpec, const QByteArray &displayName) { - mail->cc(true)->addAddress(addrSpec, displayName); - recordForAutocompletion(addrSpec, displayName); - }); - applyAddresses(m_bcc, [&](const QByteArray &addrSpec, const QByteArray &displayName) { - mail->bcc(true)->addAddress(addrSpec, displayName); - recordForAutocompletion(addrSpec, displayName); - }); - if (!identityIsSet()) { - SinkWarning() << "We don't have an identity to send the mail with."; - } else { - auto currentIndex = identityModel()->index(m_currentAccountIndex, 0); - KMime::Types::Mailbox mb; - mb.setName(currentIndex.data(IdentitiesModel::Username).toString()); - mb.setAddress(currentIndex.data(IdentitiesModel::Address).toString().toUtf8()); - mail->from(true)->addAddress(mb); - mail->subject(true)->fromUnicodeString(m_subject, "utf-8"); - mail->setBody(m_body.toUtf8()); - mail->assemble(); - return mail; - } - return KMime::Message::Ptr(); + 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(); + 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()); + + 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)); + } + ); } -void ComposerController::send() +Kube::Action* ComposerController::saveAsDraftAction() { - auto mail = assembleMessage(); - - //TODO deactivate action if we don't have the identiy set - if (!identityIsSet()) { - SinkWarning() << "We don't have an identity to send the mail with."; - } else { - auto currentAccountId = identityModel()->index(m_currentAccountIndex, 0).data(IdentitiesModel::AccountId).toByteArray(); - - Kube::Context context; - context.setProperty("message", QVariant::fromValue(mail)); - context.setProperty("accountId", QVariant::fromValue(currentAccountId)); - - qDebug() << "Current account " << currentAccountId; - - Kube::Action("org.kde.kube.actions.sendmail", context).execute(); - clear(); - } + auto action = new Kube::Action("org.kde.kube.actions.save-as-draft", *mContext); + action->addPreHandler(messageHandler()); + return action; } -void ComposerController::saveAsDraft() +Kube::Action* ComposerController::sendAction() { - auto mail = assembleMessage(); - auto currentAccountId = identityModel()->index(m_currentAccountIndex, 0).data(IdentitiesModel::AccountId).toByteArray(); + 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; +} - Kube::Context context; - context.setProperty("message", QVariant::fromValue(mail)); - context.setProperty("accountId", QVariant::fromValue(currentAccountId)); - context.setProperty("existingMail", QVariant::fromValue(m_existingMail)); - Kube::Action("org.kde.kube.actions.save-as-draft", context).execute(); - clear(); +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(); + } } -void ComposerController::clear() +int ComposerController::currentIdentityIndex() const { - setSubject(""); - setBody(""); - setTo(""); - setCc(""); - setBcc(""); + return m_currentAccountIndex; } -- cgit v1.2.3