summaryrefslogtreecommitdiffstats
path: root/examples/mailtransportresource/mailtransportresource.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/mailtransportresource/mailtransportresource.cpp')
-rw-r--r--examples/mailtransportresource/mailtransportresource.cpp70
1 files changed, 46 insertions, 24 deletions
diff --git a/examples/mailtransportresource/mailtransportresource.cpp b/examples/mailtransportresource/mailtransportresource.cpp
index e8a15ee..51e4976 100644
--- a/examples/mailtransportresource/mailtransportresource.cpp
+++ b/examples/mailtransportresource/mailtransportresource.cpp
@@ -34,6 +34,7 @@
34#include "resultprovider.h" 34#include "resultprovider.h"
35#include "mailtransport.h" 35#include "mailtransport.h"
36#include "mail_generated.h" 36#include "mail_generated.h"
37#include "inspection.h"
37#include <synchronizer.h> 38#include <synchronizer.h>
38#include <log.h> 39#include <log.h>
39#include <resourceconfig.h> 40#include <resourceconfig.h>
@@ -91,24 +92,6 @@ public:
91 QByteArray mResourceInstanceIdentifier; 92 QByteArray mResourceInstanceIdentifier;
92}; 93};
93 94
94static KAsync::Job<void>send(const ApplicationDomain::Mail &mail, const MailtransportResource::Settings &settings)
95{
96 const auto data = mail.getMimeMessage();
97 auto msg = KMime::Message::Ptr::create();
98 msg->setHead(KMime::CRLFtoLF(data));
99 msg->parse();
100 if (settings.testMode) {
101 Log() << "I would totally send that mail, but I'm in test mode.";
102 } else {
103 if (MailTransport::sendMessage(msg, settings.server.toUtf8(), settings.username.toUtf8(), settings.password.toUtf8(), settings.cacert.toUtf8())) {
104 Log() << "Sent message successfully";
105 } else {
106 Log() << "Failed to send message";
107 return KAsync::error<void>(1, "Failed to send the message.");
108 }
109 }
110 return KAsync::null<void>();
111}
112 95
113//TODO fold into synchronizer 96//TODO fold into synchronizer
114class MailtransportWriteback : public Sink::SourceWriteBack 97class MailtransportWriteback : public Sink::SourceWriteBack
@@ -137,11 +120,39 @@ public:
137class MailtransportSynchronizer : public Sink::Synchronizer { 120class MailtransportSynchronizer : public Sink::Synchronizer {
138public: 121public:
139 MailtransportSynchronizer(const QByteArray &resourceType, const QByteArray &resourceInstanceIdentifier) 122 MailtransportSynchronizer(const QByteArray &resourceType, const QByteArray &resourceInstanceIdentifier)
140 : Sink::Synchronizer(resourceType, resourceInstanceIdentifier) 123 : Sink::Synchronizer(resourceType, resourceInstanceIdentifier),
124 mResourceInstanceIdentifier(resourceInstanceIdentifier)
141 { 125 {
142 126
143 } 127 }
144 128
129 KAsync::Job<void>send(const ApplicationDomain::Mail &mail, const MailtransportResource::Settings &settings)
130 {
131 const auto data = mail.getMimeMessage();
132 auto msg = KMime::Message::Ptr::create();
133 msg->setHead(KMime::CRLFtoLF(data));
134 msg->parse();
135 if (settings.testMode) {
136 Log() << "I would totally send that mail, but I'm in test mode." << mail.identifier();
137 auto path = resourceStorageLocation(mResourceInstanceIdentifier) + "/test/";
138 Trace() << path;
139 QDir dir;
140 dir.mkpath(path);
141 QFile f(path+ mail.identifier());
142 f.open(QIODevice::ReadWrite);
143 f.write("foo");
144 f.close();
145 } else {
146 if (MailTransport::sendMessage(msg, settings.server.toUtf8(), settings.username.toUtf8(), settings.password.toUtf8(), settings.cacert.toUtf8())) {
147 Log() << "Sent message successfully";
148 } else {
149 Log() << "Failed to send message";
150 return KAsync::error<void>(1, "Failed to send the message.");
151 }
152 }
153 return KAsync::null<void>();
154 }
155
145 KAsync::Job<void> synchronizeWithSource() Q_DECL_OVERRIDE 156 KAsync::Job<void> synchronizeWithSource() Q_DECL_OVERRIDE
146 { 157 {
147 Log() << " Synchronizing"; 158 Log() << " Synchronizing";
@@ -151,16 +162,18 @@ public:
151 Log() << " Looking for mail"; 162 Log() << " Looking for mail";
152 store().reader<ApplicationDomain::Mail>().query(query, [&](const ApplicationDomain::Mail &mail) -> bool { 163 store().reader<ApplicationDomain::Mail>().query(query, [&](const ApplicationDomain::Mail &mail) -> bool {
153 Trace() << "Found mail: " << mail.identifier(); 164 Trace() << "Found mail: " << mail.identifier();
154 // if (!mail.isSent()) { 165 if (!mail.getSent()) {
155 toSend << mail; 166 toSend << mail;
156 // } 167 }
157 return true; 168 return true;
158 }); 169 });
159 auto job = KAsync::null<void>(); 170 auto job = KAsync::null<void>();
160 for (const auto &m : toSend) { 171 for (const auto &m : toSend) {
161 job = job.then(send(m, mSettings)).then<void>([]() { 172 job = job.then(send(m, mSettings)).then<void>([this, m]() {
162 //on success, mark the mail as sent and move it to a separate place 173 auto modifiedMail = ApplicationDomain::Mail(mResourceInstanceIdentifier, m.identifier(), m.revision(), QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create());
163 //TODO 174 modifiedMail.setSent(true);
175 modify(modifiedMail);
176 //TODO copy to a sent mail folder as well
164 }); 177 });
165 } 178 }
166 job = job.then<void>([&future]() { 179 job = job.then<void>([&future]() {
@@ -208,6 +221,15 @@ void MailtransportResource::removeFromDisk(const QByteArray &instanceIdentifier)
208 221
209KAsync::Job<void> MailtransportResource::inspect(int inspectionType, const QByteArray &inspectionId, const QByteArray &domainType, const QByteArray &entityId, const QByteArray &property, const QVariant &expectedValue) 222KAsync::Job<void> MailtransportResource::inspect(int inspectionType, const QByteArray &inspectionId, const QByteArray &domainType, const QByteArray &entityId, const QByteArray &property, const QVariant &expectedValue)
210{ 223{
224 if (domainType == ENTITY_TYPE_MAIL) {
225 if (inspectionType == Sink::ResourceControl::Inspection::ExistenceInspectionType) {
226 auto path = resourceStorageLocation(mResourceInstanceIdentifier) + "/test/" + entityId;
227 if (QFileInfo::exists(path)) {
228 return KAsync::null<void>();
229 }
230 return KAsync::error<void>(1, "Couldn't find message: " + path);
231 }
232 }
211 return KAsync::null<void>(); 233 return KAsync::null<void>();
212} 234}
213 235