From 207effdd7112141ad4fc5cdd46f332870a0c065c Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Fri, 17 Jun 2016 17:34:34 +0200 Subject: A working mailtransport resource --- .../mailtransportresource.cpp | 70 ++++++++++++++-------- 1 file changed, 46 insertions(+), 24 deletions(-) (limited to 'examples/mailtransportresource/mailtransportresource.cpp') 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 @@ #include "resultprovider.h" #include "mailtransport.h" #include "mail_generated.h" +#include "inspection.h" #include #include #include @@ -91,24 +92,6 @@ public: QByteArray mResourceInstanceIdentifier; }; -static KAsync::Jobsend(const ApplicationDomain::Mail &mail, const MailtransportResource::Settings &settings) -{ - const auto data = mail.getMimeMessage(); - auto msg = KMime::Message::Ptr::create(); - msg->setHead(KMime::CRLFtoLF(data)); - msg->parse(); - if (settings.testMode) { - Log() << "I would totally send that mail, but I'm in test mode."; - } else { - if (MailTransport::sendMessage(msg, settings.server.toUtf8(), settings.username.toUtf8(), settings.password.toUtf8(), settings.cacert.toUtf8())) { - Log() << "Sent message successfully"; - } else { - Log() << "Failed to send message"; - return KAsync::error(1, "Failed to send the message."); - } - } - return KAsync::null(); -} //TODO fold into synchronizer class MailtransportWriteback : public Sink::SourceWriteBack @@ -137,11 +120,39 @@ public: class MailtransportSynchronizer : public Sink::Synchronizer { public: MailtransportSynchronizer(const QByteArray &resourceType, const QByteArray &resourceInstanceIdentifier) - : Sink::Synchronizer(resourceType, resourceInstanceIdentifier) + : Sink::Synchronizer(resourceType, resourceInstanceIdentifier), + mResourceInstanceIdentifier(resourceInstanceIdentifier) { } + KAsync::Jobsend(const ApplicationDomain::Mail &mail, const MailtransportResource::Settings &settings) + { + const auto data = mail.getMimeMessage(); + auto msg = KMime::Message::Ptr::create(); + msg->setHead(KMime::CRLFtoLF(data)); + msg->parse(); + if (settings.testMode) { + Log() << "I would totally send that mail, but I'm in test mode." << mail.identifier(); + auto path = resourceStorageLocation(mResourceInstanceIdentifier) + "/test/"; + Trace() << path; + QDir dir; + dir.mkpath(path); + QFile f(path+ mail.identifier()); + f.open(QIODevice::ReadWrite); + f.write("foo"); + f.close(); + } else { + if (MailTransport::sendMessage(msg, settings.server.toUtf8(), settings.username.toUtf8(), settings.password.toUtf8(), settings.cacert.toUtf8())) { + Log() << "Sent message successfully"; + } else { + Log() << "Failed to send message"; + return KAsync::error(1, "Failed to send the message."); + } + } + return KAsync::null(); + } + KAsync::Job synchronizeWithSource() Q_DECL_OVERRIDE { Log() << " Synchronizing"; @@ -151,16 +162,18 @@ public: Log() << " Looking for mail"; store().reader().query(query, [&](const ApplicationDomain::Mail &mail) -> bool { Trace() << "Found mail: " << mail.identifier(); - // if (!mail.isSent()) { + if (!mail.getSent()) { toSend << mail; - // } + } return true; }); auto job = KAsync::null(); for (const auto &m : toSend) { - job = job.then(send(m, mSettings)).then([]() { - //on success, mark the mail as sent and move it to a separate place - //TODO + job = job.then(send(m, mSettings)).then([this, m]() { + auto modifiedMail = ApplicationDomain::Mail(mResourceInstanceIdentifier, m.identifier(), m.revision(), QSharedPointer::create()); + modifiedMail.setSent(true); + modify(modifiedMail); + //TODO copy to a sent mail folder as well }); } job = job.then([&future]() { @@ -208,6 +221,15 @@ void MailtransportResource::removeFromDisk(const QByteArray &instanceIdentifier) KAsync::Job MailtransportResource::inspect(int inspectionType, const QByteArray &inspectionId, const QByteArray &domainType, const QByteArray &entityId, const QByteArray &property, const QVariant &expectedValue) { + if (domainType == ENTITY_TYPE_MAIL) { + if (inspectionType == Sink::ResourceControl::Inspection::ExistenceInspectionType) { + auto path = resourceStorageLocation(mResourceInstanceIdentifier) + "/test/" + entityId; + if (QFileInfo::exists(path)) { + return KAsync::null(); + } + return KAsync::error(1, "Couldn't find message: " + path); + } + } return KAsync::null(); } -- cgit v1.2.3