diff options
Diffstat (limited to 'examples/mailtransportresource/mailtransportresource.cpp')
-rw-r--r-- | examples/mailtransportresource/mailtransportresource.cpp | 67 |
1 files changed, 57 insertions, 10 deletions
diff --git a/examples/mailtransportresource/mailtransportresource.cpp b/examples/mailtransportresource/mailtransportresource.cpp index 88a90c6..8a4ef92 100644 --- a/examples/mailtransportresource/mailtransportresource.cpp +++ b/examples/mailtransportresource/mailtransportresource.cpp | |||
@@ -30,7 +30,6 @@ | |||
30 | #include <KMime/Message> | 30 | #include <KMime/Message> |
31 | 31 | ||
32 | #include "mailtransport.h" | 32 | #include "mailtransport.h" |
33 | #include "mail_generated.h" | ||
34 | #include "inspection.h" | 33 | #include "inspection.h" |
35 | #include <synchronizer.h> | 34 | #include <synchronizer.h> |
36 | #include <log.h> | 35 | #include <log.h> |
@@ -44,6 +43,47 @@ SINK_DEBUG_AREA("mailtransportresource") | |||
44 | 43 | ||
45 | using namespace Sink; | 44 | using namespace Sink; |
46 | 45 | ||
46 | class MailtransportPreprocessor : public Sink::Preprocessor | ||
47 | { | ||
48 | public: | ||
49 | MailtransportPreprocessor() : Sink::Preprocessor() {} | ||
50 | |||
51 | QByteArray getTargetResource() | ||
52 | { | ||
53 | using namespace Sink::ApplicationDomain; | ||
54 | |||
55 | auto resource = Store::readOne<ApplicationDomain::SinkResource>(Query{}.filter(resourceInstanceIdentifier()).request<ApplicationDomain::SinkResource::Account>()); | ||
56 | if (resource.identifier().isEmpty()) { | ||
57 | SinkWarning() << "Failed to retrieve this resource: " << resourceInstanceIdentifier(); | ||
58 | } | ||
59 | Query query; | ||
60 | query.containsFilter<ApplicationDomain::SinkResource::Capabilities>(ApplicationDomain::ResourceCapabilities::Mail::sent); | ||
61 | query.filter<ApplicationDomain::SinkResource::Account>(resource.getAccount()); | ||
62 | auto targetResource = Store::readOne<ApplicationDomain::SinkResource>(query); | ||
63 | if (targetResource.identifier().isEmpty()) { | ||
64 | SinkWarning() << "Failed to find target resource: " << targetResource.identifier(); | ||
65 | } | ||
66 | return targetResource.identifier(); | ||
67 | } | ||
68 | |||
69 | virtual Result processModification(Type type, const ApplicationDomain::ApplicationDomainType ¤t, ApplicationDomain::ApplicationDomainType &diff) Q_DECL_OVERRIDE | ||
70 | { | ||
71 | if (type == Preprocessor::Modification) { | ||
72 | using namespace Sink::ApplicationDomain; | ||
73 | if (diff.changedProperties().contains(Mail::Trash::name)) { | ||
74 | //Move back to regular resource | ||
75 | diff.setResource(getTargetResource()); | ||
76 | return {MoveToResource}; | ||
77 | } else if (diff.changedProperties().contains(Mail::Draft::name)) { | ||
78 | //Move back to regular resource | ||
79 | diff.setResource(getTargetResource()); | ||
80 | return {MoveToResource}; | ||
81 | } | ||
82 | } | ||
83 | return {NoAction}; | ||
84 | } | ||
85 | }; | ||
86 | |||
47 | class MailtransportSynchronizer : public Sink::Synchronizer { | 87 | class MailtransportSynchronizer : public Sink::Synchronizer { |
48 | public: | 88 | public: |
49 | MailtransportSynchronizer(const Sink::ResourceContext &resourceContext) | 89 | MailtransportSynchronizer(const Sink::ResourceContext &resourceContext) |
@@ -55,17 +95,22 @@ public: | |||
55 | 95 | ||
56 | KAsync::Job<void> send(const ApplicationDomain::Mail &mail, const MailtransportResource::Settings &settings) | 96 | KAsync::Job<void> send(const ApplicationDomain::Mail &mail, const MailtransportResource::Settings &settings) |
57 | { | 97 | { |
58 | return KAsync::start<void>([=] { | 98 | return KAsync::start([=] { |
59 | if (!syncStore().readValue(mail.identifier()).isEmpty()) { | 99 | if (!syncStore().readValue(mail.identifier()).isEmpty()) { |
60 | SinkLog() << "Mail is already sent: " << mail.identifier(); | 100 | SinkLog() << "Mail is already sent: " << mail.identifier(); |
61 | return KAsync::null(); | 101 | return KAsync::null(); |
62 | } | 102 | } |
103 | emitNotification(Notification::Info, ApplicationDomain::SyncInProgress, "Sending message.", {}, {mail.identifier()}); | ||
63 | const auto data = mail.getMimeMessage(); | 104 | const auto data = mail.getMimeMessage(); |
64 | auto msg = KMime::Message::Ptr::create(); | 105 | auto msg = KMime::Message::Ptr::create(); |
65 | msg->setHead(KMime::CRLFtoLF(data)); | 106 | msg->setHead(KMime::CRLFtoLF(data)); |
66 | msg->parse(); | 107 | msg->parse(); |
67 | if (settings.testMode) { | 108 | if (settings.testMode) { |
68 | SinkLog() << "I would totally send that mail, but I'm in test mode." << mail.identifier(); | 109 | auto subject = msg->subject(true)->asUnicodeString(); |
110 | SinkLog() << "I would totally send that mail, but I'm in test mode." << mail.identifier() << subject; | ||
111 | if (!subject.contains("send")) { | ||
112 | return KAsync::error("Failed to send the message."); | ||
113 | } | ||
69 | auto path = resourceStorageLocation(mResourceInstanceIdentifier) + "/test/"; | 114 | auto path = resourceStorageLocation(mResourceInstanceIdentifier) + "/test/"; |
70 | SinkTrace() << path; | 115 | SinkTrace() << path; |
71 | QDir dir; | 116 | QDir dir; |
@@ -77,11 +122,16 @@ public: | |||
77 | } else { | 122 | } else { |
78 | MailTransport::Options options; | 123 | MailTransport::Options options; |
79 | if (settings.server.contains("smtps")) { | 124 | if (settings.server.contains("smtps")) { |
80 | options &= MailTransport::UseTls; | 125 | options |= MailTransport::UseTls; |
81 | } | 126 | } |
82 | if (!MailTransport::sendMessage(msg, settings.server.toUtf8(), settings.username.toUtf8(), settings.password.toUtf8(), settings.cacert.toUtf8(), options)) { | 127 | if (!MailTransport::sendMessage(msg, settings.server.toUtf8(), settings.username.toUtf8(), settings.password.toUtf8(), settings.cacert.toUtf8(), options)) { |
83 | SinkWarning() << "Failed to send message: " << mail; | 128 | SinkWarning() << "Failed to send message: " << mail; |
129 | emitNotification(Notification::Warning, ApplicationDomain::SyncError, "Failed to send message.", {}, {mail.identifier()}); | ||
130 | emitNotification(Notification::Warning, ApplicationDomain::TransmissionError, "Failed to send message.", {}, {mail.identifier()}); | ||
84 | return KAsync::error("Failed to send the message."); | 131 | return KAsync::error("Failed to send the message."); |
132 | } else { | ||
133 | emitNotification(Notification::Info, ApplicationDomain::SyncSuccess, "Message successfully sent.", {}, {mail.identifier()}); | ||
134 | emitNotification(Notification::Info, ApplicationDomain::TransmissionSuccess, "Message successfully sent.", {}, {mail.identifier()}); | ||
85 | } | 135 | } |
86 | } | 136 | } |
87 | syncStore().writeValue(mail.identifier(), "sent"); | 137 | syncStore().writeValue(mail.identifier(), "sent"); |
@@ -100,9 +150,8 @@ public: | |||
100 | query.filter<ApplicationDomain::SinkResource::Account>(resource.getAccount()); | 150 | query.filter<ApplicationDomain::SinkResource::Account>(resource.getAccount()); |
101 | return Store::fetchOne<ApplicationDomain::SinkResource>(query) | 151 | return Store::fetchOne<ApplicationDomain::SinkResource>(query) |
102 | .then([this, modifiedMail](const ApplicationDomain::SinkResource &resource) { | 152 | .then([this, modifiedMail](const ApplicationDomain::SinkResource &resource) { |
103 | //First modify the mail to have the sent property set to true | 153 | //Modify the mail to have the sent property set to true, and move it to the new resource. |
104 | modify(modifiedMail, resource.identifier(), true); | 154 | modify(modifiedMail, resource.identifier(), true); |
105 | return KAsync::null<void>(); | ||
106 | }); | 155 | }); |
107 | }); | 156 | }); |
108 | } | 157 | } |
@@ -112,12 +161,10 @@ public: | |||
112 | return KAsync::start<void>([this]() { | 161 | return KAsync::start<void>([this]() { |
113 | QList<ApplicationDomain::Mail> toSend; | 162 | QList<ApplicationDomain::Mail> toSend; |
114 | SinkLog() << "Looking for mails to send."; | 163 | SinkLog() << "Looking for mails to send."; |
115 | store().readAll<ApplicationDomain::Mail>([&](const ApplicationDomain::Mail &mail) -> bool { | 164 | store().readAll<ApplicationDomain::Mail>([&](const ApplicationDomain::Mail &mail) { |
116 | SinkTrace() << "Found mail: " << mail.identifier(); | ||
117 | if (!mail.getSent()) { | 165 | if (!mail.getSent()) { |
118 | toSend << mail; | 166 | toSend << mail; |
119 | } | 167 | } |
120 | return true; | ||
121 | }); | 168 | }); |
122 | SinkLog() << "Found " << toSend.size() << " mails to send"; | 169 | SinkLog() << "Found " << toSend.size() << " mails to send"; |
123 | auto job = KAsync::null<void>(); | 170 | auto job = KAsync::null<void>(); |
@@ -192,7 +239,7 @@ MailtransportResource::MailtransportResource(const Sink::ResourceContext &resour | |||
192 | setupSynchronizer(synchronizer); | 239 | setupSynchronizer(synchronizer); |
193 | setupInspector(QSharedPointer<MailtransportInspector>::create(resourceContext)); | 240 | setupInspector(QSharedPointer<MailtransportInspector>::create(resourceContext)); |
194 | 241 | ||
195 | setupPreprocessors(ENTITY_TYPE_MAIL, QVector<Sink::Preprocessor*>() << new MailPropertyExtractor); | 242 | setupPreprocessors(ENTITY_TYPE_MAIL, QVector<Sink::Preprocessor*>() << new MailPropertyExtractor << new MailtransportPreprocessor); |
196 | } | 243 | } |
197 | 244 | ||
198 | MailtransportResourceFactory::MailtransportResourceFactory(QObject *parent) | 245 | MailtransportResourceFactory::MailtransportResourceFactory(QObject *parent) |