From 48da7de1b6bcd87c57b4c7f60133f3f13fc4bff2 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Thu, 29 Dec 2016 20:23:58 +0100 Subject: A typesafe action context --- framework/domain/actions/sinkactions.cpp | 2 +- framework/domain/composercontroller.cpp | 47 +++++++++++++------------------- framework/domain/composercontroller.h | 17 +++++++++--- 3 files changed, 33 insertions(+), 33 deletions(-) (limited to 'framework/domain') diff --git a/framework/domain/actions/sinkactions.cpp b/framework/domain/actions/sinkactions.cpp index e8127ee6..fd791a91 100644 --- a/framework/domain/actions/sinkactions.cpp +++ b/framework/domain/actions/sinkactions.cpp @@ -118,7 +118,7 @@ static ActionHandlerHelper sendMailHandler("org.kde.kube.actions.sendmail", ActionHandlerHelper::JobHandler{[](Context *context) -> KAsync::Job { auto accountId = context->property("accountId").value(); auto message = context->property("message").value(); - SinkLog() << "Sending a mail: "; + SinkLog() << "Sending a mail: " << *context; Query query; query.containsFilter(ApplicationDomain::ResourceCapabilities::Mail::transport); 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) ComposerController::ComposerController(QObject *parent) : QObject(parent) { + QQmlEngine::setObjectOwnership(&mContext, QQmlEngine::CppOwnership); } QString ComposerController::recepientSearchString() const @@ -51,14 +52,9 @@ QString ComposerController::recepientSearchString() const return QString(); } -Kube::Context* ComposerController::mailContext() const +Kube::Context* ComposerController::mailContext() { - return mContext; -} - -void ComposerController::setMailContext(Kube::Context *context) -{ - mContext = context; + return &mContext; } void ComposerController::setRecepientSearchString(const QString &s) @@ -84,11 +80,11 @@ QAbstractItemModel *ComposerController::recepientAutocompletionModel() const 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)); + mContext.setTo(msg->to(true)->asUnicodeString()); + mContext.setCc(msg->cc(true)->asUnicodeString()); + mContext.setSubject(msg->subject(true)->asUnicodeString()); + mContext.setBody(msg->body()); + mContext.setProperty("existingMessage", QVariant::fromValue(msg)); } void ComposerController::loadMessage(const QVariant &message, bool loadAsDraft) @@ -96,7 +92,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) { - mContext->setProperty("existingMail", QVariant::fromValue(mail)); + mContext.setProperty("existingMail", QVariant::fromValue(mail)); const auto mailData = KMime::CRLFtoLF(mail.getMimeMessage()); if (!mailData.isEmpty()) { KMime::Message::Ptr mail(new KMime::Message); @@ -135,11 +131,7 @@ void applyAddresses(const QString &list, std::functionsetProperty("subject", QVariant()); - mContext->setProperty("body", QVariant()); - mContext->setProperty("to", QVariant()); - mContext->setProperty("cc", QVariant()); - mContext->setProperty("bcc", QVariant()); + mContext.clear(); } @@ -155,23 +147,23 @@ Kube::ActionHandler *ComposerController::messageHandler() if (!mail) { mail = KMime::Message::Ptr::create(); } - applyAddresses(context->property("to").toString(), [&](const QByteArray &addrSpec, const QByteArray &displayName) { + applyAddresses(context->property(ComposerContext::To::name).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) { + applyAddresses(context->property(ComposerContext::Cc::name).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) { + applyAddresses(context->property(ComposerContext::Bcc::name).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->subject(true)->fromUnicodeString(context->property(ComposerContext::Subject::name).toString(), "utf-8"); + mail->setBody(context->property(ComposerContext::Body::name).toString().toUtf8()); mail->assemble(); context->setProperty("message", QVariant::fromValue(mail)); @@ -181,7 +173,7 @@ Kube::ActionHandler *ComposerController::messageHandler() Kube::Action* ComposerController::saveAsDraftAction() { - auto action = new Kube::Action("org.kde.kube.actions.save-as-draft", *mContext); + auto action = new Kube::Action("org.kde.kube.actions.save-as-draft", mContext); action->addPreHandler(messageHandler()); action->addPostHandler(new Kube::ActionHandlerHelper( [this](Kube::Context *context) { @@ -192,8 +184,7 @@ Kube::Action* ComposerController::saveAsDraftAction() Kube::Action* ComposerController::sendAction() { - qWarning() << "send action"; - auto action = new Kube::Action("org.kde.kube.actions.sendmail", *mContext); + auto action = new Kube::Action("org.kde.kube.actions.sendmail", mContext); // action->addPreHandler(identityHandler()); action->addPreHandler(messageHandler()); // action->addPreHandler(encryptionHandler()); @@ -214,8 +205,8 @@ void ComposerController::setCurrentIdentityIndex(int index) 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)); + 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(); } diff --git a/framework/domain/composercontroller.h b/framework/domain/composercontroller.h index b7ff0757..11da517e 100644 --- a/framework/domain/composercontroller.h +++ b/framework/domain/composercontroller.h @@ -33,10 +33,20 @@ namespace KMime { class Message; } +class ComposerContext : public Kube::Context { + Q_OBJECT + KUBE_CONTEXT_PROPERTY(QString, To, to) + KUBE_CONTEXT_PROPERTY(QString, Cc, cc) + KUBE_CONTEXT_PROPERTY(QString, Bcc, bcc) + KUBE_CONTEXT_PROPERTY(QString, From, from) + KUBE_CONTEXT_PROPERTY(QString, Subject, subject) + KUBE_CONTEXT_PROPERTY(QString, Body, body) +}; + class ComposerController : public QObject { Q_OBJECT - Q_PROPERTY (Kube::Context* mailContext READ mailContext WRITE setMailContext) + Q_PROPERTY (Kube::Context* mailContext READ mailContext CONSTANT) Q_PROPERTY (int currentIdentityIndex READ currentIdentityIndex WRITE setCurrentIdentityIndex) Q_PROPERTY (QString recepientSearchString READ recepientSearchString WRITE setRecepientSearchString) @@ -49,8 +59,7 @@ class ComposerController : public QObject public: explicit ComposerController(QObject *parent = Q_NULLPTR); - Kube::Context* mailContext() const; - void setMailContext(Kube::Context *context); + Kube::Context* mailContext(); QString recepientSearchString() const; void setRecepientSearchString(const QString &body); @@ -78,5 +87,5 @@ private: void setMessage(const QSharedPointer &msg); int m_currentAccountIndex = -1; - Kube::Context *mContext; + ComposerContext mContext; }; -- cgit v1.2.3