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 ++++++++++++++-------- .../tests/mailtransporttest.cpp | 20 +------ 2 files changed, 49 insertions(+), 41 deletions(-) (limited to 'examples') 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(); } diff --git a/examples/mailtransportresource/tests/mailtransporttest.cpp b/examples/mailtransportresource/tests/mailtransporttest.cpp index 564b4cb..f1190a7 100644 --- a/examples/mailtransportresource/tests/mailtransporttest.cpp +++ b/examples/mailtransportresource/tests/mailtransporttest.cpp @@ -26,11 +26,6 @@ class MailtransportTest : public QObject resource.setProperty("testmode", true); return resource; } - - void removeResourceFromDisk(const QByteArray &identifier) - { - // ::MailtransportResource::removeFromDisk(identifier); - } QByteArray mResourceInstanceIdentifier; private slots: @@ -39,13 +34,10 @@ private slots: { Test::initTest(); Log::setDebugOutputLevel(Sink::Log::Trace); - // resetTestEnvironment(); auto resource = createResource(); QVERIFY(!resource.identifier().isEmpty()); VERIFYEXEC(Store::create(resource)); mResourceInstanceIdentifier = resource.identifier(); - // mCapabilities = resource.getProperty("capabilities").value(); - qWarning() << "FOooooooooooooooooooo"; } void cleanup() @@ -56,9 +48,6 @@ private slots: void init() { - // qDebug(); - // qDebug() << "-----------------------------------------"; - // qDebug(); // VERIFYEXEC(ResourceControl::start(mResourceInstanceIdentifier)); } @@ -74,13 +63,10 @@ private slots: VERIFYEXEC(Store::create(mail)); VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier)); VERIFYEXEC(Store::synchronize(Query::ResourceFilter(mResourceInstanceIdentifier))); + VERIFYEXEC(ResourceControl::inspect(ResourceControl::Inspection::ExistenceInspection(mail, true))); - //TODO verify the mail has been sent - - // auto modifiedMail = Store::readOne(Query::IdentityFilter(mail)); - // VERIFYEXEC(Store::modify(modifiedMail)); - // VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier)); - // VERIFYEXEC(ResourceControl::flushReplayQueue(QByteArrayList() << mResourceInstanceIdentifier)); + auto sentMail = Store::readOne(Query::IdentityFilter(mail).request()); + QVERIFY(sentMail.getSent()); } -- cgit v1.2.3