From dc33cea58f4b920f60648e81379c58bd62861b9b Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 11 Apr 2017 16:14:24 +0200 Subject: Copy mail back from mailtransport --- .../mailtransportresource.cpp | 46 +++++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-) (limited to 'examples/mailtransportresource/mailtransportresource.cpp') diff --git a/examples/mailtransportresource/mailtransportresource.cpp b/examples/mailtransportresource/mailtransportresource.cpp index 75d9898..8e5b22e 100644 --- a/examples/mailtransportresource/mailtransportresource.cpp +++ b/examples/mailtransportresource/mailtransportresource.cpp @@ -43,6 +43,44 @@ SINK_DEBUG_AREA("mailtransportresource") using namespace Sink; +class MailtransportPreprocessor : public Sink::Preprocessor +{ +public: + MailtransportPreprocessor() : Sink::Preprocessor() {} + + QByteArray getTargetResource() + { + using namespace Sink::ApplicationDomain; + + auto resource = Store::readOne(Query{}.filter(resourceInstanceIdentifier()).request()); + if (resource.identifier().isEmpty()) { + SinkWarning() << "Failed to retrieve this resource: " << resourceInstanceIdentifier(); + } + Query query; + query.containsFilter(ApplicationDomain::ResourceCapabilities::Mail::sent); + query.filter(resource.getAccount()); + auto targetResource = Store::readOne(query); + if (targetResource.identifier().isEmpty()) { + SinkWarning() << "Failed to find target resource: " << targetResource.identifier(); + } + return targetResource.identifier(); + } + + void modifiedEntity(const Sink::ApplicationDomain::ApplicationDomainType &oldEntity, Sink::ApplicationDomain::ApplicationDomainType &newEntity) Q_DECL_OVERRIDE + { + using namespace Sink::ApplicationDomain; + auto mail = newEntity.cast(); + if (mail.changedProperties().contains(Mail::Trash::name)) { + //Move back to regular resource + newEntity.setResource(getTargetResource()); + } else if (mail.changedProperties().contains(Mail::Draft::name)) { + //Move back to regular resource + newEntity.setResource(getTargetResource()); + } + //TODO this will only copy it back, but not yet move + } +}; + class MailtransportSynchronizer : public Sink::Synchronizer { public: MailtransportSynchronizer(const Sink::ResourceContext &resourceContext) @@ -65,7 +103,11 @@ public: msg->setHead(KMime::CRLFtoLF(data)); msg->parse(); if (settings.testMode) { - SinkLog() << "I would totally send that mail, but I'm in test mode." << mail.identifier(); + auto subject = msg->subject(true)->asUnicodeString(); + SinkLog() << "I would totally send that mail, but I'm in test mode." << mail.identifier() << subject; + if (!subject.contains("send")) { + return KAsync::error("Failed to send the message."); + } auto path = resourceStorageLocation(mResourceInstanceIdentifier) + "/test/"; SinkTrace() << path; QDir dir; @@ -194,7 +236,7 @@ MailtransportResource::MailtransportResource(const Sink::ResourceContext &resour setupSynchronizer(synchronizer); setupInspector(QSharedPointer::create(resourceContext)); - setupPreprocessors(ENTITY_TYPE_MAIL, QVector() << new MailPropertyExtractor); + setupPreprocessors(ENTITY_TYPE_MAIL, QVector() << new MailPropertyExtractor << new MailtransportPreprocessor); } MailtransportResourceFactory::MailtransportResourceFactory(QObject *parent) -- cgit v1.2.3