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 --- common/domain/applicationdomaintype.h | 1 + common/domain/mail.cpp | 2 + common/domain/mail.fbs | 1 + common/synchronizer.cpp | 20 ++++++- common/synchronizer.h | 7 +++ .../mailtransportresource.cpp | 70 ++++++++++++++-------- .../tests/mailtransporttest.cpp | 20 +------ 7 files changed, 77 insertions(+), 44 deletions(-) diff --git a/common/domain/applicationdomaintype.h b/common/domain/applicationdomaintype.h index 12a9e56..928ea58 100644 --- a/common/domain/applicationdomaintype.h +++ b/common/domain/applicationdomaintype.h @@ -211,6 +211,7 @@ struct SINK_EXPORT Mail : public Entity { SINK_BLOB_PROPERTY(MimeMessage, mimeMessage); SINK_PROPERTY(bool, Draft, draft); SINK_PROPERTY(bool, Trash, trash); + SINK_PROPERTY(bool, Sent, sent); }; /** diff --git a/common/domain/mail.cpp b/common/domain/mail.cpp index dd4eca8..5b35a9a 100644 --- a/common/domain/mail.cpp +++ b/common/domain/mail.cpp @@ -86,6 +86,7 @@ QSharedPointer::Buffer> > TypeImplem propertyMapper->addMapping(&Buffer::mimeMessage); propertyMapper->addMapping(&Buffer::draft); propertyMapper->addMapping(&Buffer::trash); + propertyMapper->addMapping(&Buffer::sent); return propertyMapper; } @@ -104,5 +105,6 @@ QSharedPointer::BufferBuilder> > Ty propertyMapper->addMapping(&BufferBuilder::add_mimeMessage); propertyMapper->addMapping(&BufferBuilder::add_draft); propertyMapper->addMapping(&BufferBuilder::add_trash); + propertyMapper->addMapping(&BufferBuilder::add_sent); return propertyMapper; } diff --git a/common/domain/mail.fbs b/common/domain/mail.fbs index 3a84ca1..a0c0d82 100644 --- a/common/domain/mail.fbs +++ b/common/domain/mail.fbs @@ -12,6 +12,7 @@ table Mail { mimeMessage:string; draft:bool = false; trash:bool = false; + sent:bool = false; } root_type Mail; diff --git a/common/synchronizer.cpp b/common/synchronizer.cpp index be969d2..ee7a7ba 100644 --- a/common/synchronizer.cpp +++ b/common/synchronizer.cpp @@ -92,17 +92,20 @@ void Synchronizer::createEntity(const QByteArray &sinkId, const QByteArray &buff void Synchronizer::modifyEntity(const QByteArray &sinkId, qint64 revision, const QByteArray &bufferType, const Sink::ApplicationDomain::ApplicationDomainType &domainObject, DomainTypeAdaptorFactoryInterface &adaptorFactory, std::function callback) { + // FIXME removals + QByteArrayList deletedProperties; // These changes are coming from the source const auto replayToSource = false; flatbuffers::FlatBufferBuilder entityFbb; adaptorFactory.createBuffer(domainObject, entityFbb); flatbuffers::FlatBufferBuilder fbb; auto entityId = fbb.CreateString(sinkId.toStdString()); + auto modifiedProperties = BufferUtils::toVector(fbb, domainObject.changedProperties()); + auto deletions = BufferUtils::toVector(fbb, deletedProperties); // This is the resource type and not the domain type auto type = fbb.CreateString(bufferType.toStdString()); auto delta = Sink::EntityBuffer::appendAsVector(fbb, entityFbb.GetBufferPointer(), entityFbb.GetSize()); - // FIXME removals - auto location = Sink::Commands::CreateModifyEntity(fbb, revision, entityId, 0, type, delta, replayToSource); + auto location = Sink::Commands::CreateModifyEntity(fbb, revision, entityId, deletions, type, delta, replayToSource, modifiedProperties); Sink::Commands::FinishModifyEntityBuffer(fbb, location); callback(BufferUtils::extractBuffer(fbb)); } @@ -223,6 +226,16 @@ void Synchronizer::createOrModify(const QByteArray &bufferType, const QByteArray } } +template +void Synchronizer::modify(const DomainType &entity) +{ + const auto bufferType = ApplicationDomain::getTypeName(); + const auto adaptorFactory = Sink::AdaptorFactoryRegistry::instance().getFactory(mResourceType, bufferType); + Q_ASSERT(adaptorFactory); + modifyEntity(entity.identifier(), entity.revision(), bufferType, entity, *adaptorFactory, + [this](const QByteArray &buffer) { enqueueCommand(Sink::Commands::ModifyEntityCommand, buffer); }); +} + KAsync::Job Synchronizer::synchronize() { Trace() << "Synchronizing"; @@ -263,7 +276,8 @@ Sink::Storage::Transaction &Synchronizer::syncTransaction() } #define REGISTER_TYPE(T) \ - template void Synchronizer::createOrModify(const QByteArray &bufferType, const QByteArray &remoteId, const T &entity, const QHash &mergeCriteria) + template void Synchronizer::createOrModify(const QByteArray &bufferType, const QByteArray &remoteId, const T &entity, const QHash &mergeCriteria); \ + template void Synchronizer::modify(const T &entity); REGISTER_TYPE(ApplicationDomain::Event); REGISTER_TYPE(ApplicationDomain::Mail); diff --git a/common/synchronizer.h b/common/synchronizer.h index 6521876..6f98f35 100644 --- a/common/synchronizer.h +++ b/common/synchronizer.h @@ -86,6 +86,13 @@ protected: template void createOrModify(const QByteArray &bufferType, const QByteArray &remoteId, const DomainType &entity, const QHash &mergeCriteria); + // template + // void create(const DomainType &entity); + template + void modify(const DomainType &entity); + // template + // void remove(const DomainType &entity); + virtual KAsync::Job synchronizeWithSource() = 0; private: 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