From 938554f267193b652478fc12343819fa45d76034 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 28 Nov 2016 19:33:01 +0100 Subject: Moved inspection commands to a separate inspector. --- examples/maildirresource/maildirresource.cpp | 185 ++++++++++++++------------- examples/maildirresource/maildirresource.h | 3 +- 2 files changed, 99 insertions(+), 89 deletions(-) (limited to 'examples/maildirresource') diff --git a/examples/maildirresource/maildirresource.cpp b/examples/maildirresource/maildirresource.cpp index ee84bde..2b19789 100644 --- a/examples/maildirresource/maildirresource.cpp +++ b/examples/maildirresource/maildirresource.cpp @@ -27,6 +27,7 @@ #include "libmaildir/maildir.h" #include "inspection.h" #include "synchronizer.h" +#include "inspector.h" #include "facadefactory.h" #include "adaptorfactoryregistry.h" @@ -425,6 +426,102 @@ public: QString mMaildirPath; }; +class MaildirInspector : public Sink::Inspector { +public: + MaildirInspector(const Sink::ResourceContext &resourceContext) + : Sink::Inspector(resourceContext) + { + + } +protected: + + KAsync::Job inspect(int inspectionType, const QByteArray &inspectionId, const QByteArray &domainType, const QByteArray &entityId, const QByteArray &property, const QVariant &expectedValue) Q_DECL_OVERRIDE { + auto synchronizationStore = QSharedPointer::create(Sink::storageLocation(), mResourceContext.instanceId() + ".synchronization", Sink::Storage::DataStore::ReadOnly); + auto synchronizationTransaction = synchronizationStore->createTransaction(Sink::Storage::DataStore::ReadOnly); + + auto mainStore = QSharedPointer::create(Sink::storageLocation(), mResourceContext.instanceId(), Sink::Storage::DataStore::ReadOnly); + auto transaction = mainStore->createTransaction(Sink::Storage::DataStore::ReadOnly); + + Sink::Storage::EntityStore entityStore(mResourceContext); + auto syncStore = QSharedPointer::create(synchronizationTransaction); + + SinkTrace() << "Inspecting " << inspectionType << domainType << entityId << property << expectedValue; + + if (domainType == ENTITY_TYPE_MAIL) { + auto mail = entityStore.readLatest(entityId); + const auto filePath = getFilePathFromMimeMessagePath(mail.getMimeMessagePath()); + + if (inspectionType == Sink::ResourceControl::Inspection::PropertyInspectionType) { + if (property == "unread") { + const auto flags = KPIM::Maildir::readEntryFlags(filePath.split('/').last()); + if (expectedValue.toBool() && (flags & KPIM::Maildir::Seen)) { + return KAsync::error(1, "Expected unread but couldn't find it."); + } + if (!expectedValue.toBool() && !(flags & KPIM::Maildir::Seen)) { + return KAsync::error(1, "Expected read but couldn't find it."); + } + return KAsync::null(); + } + if (property == "subject") { + KMime::Message *msg = new KMime::Message; + msg->setHead(KMime::CRLFtoLF(KPIM::Maildir::readEntryHeadersFromFile(filePath))); + msg->parse(); + + if (msg->subject(true)->asUnicodeString() != expectedValue.toString()) { + return KAsync::error(1, "Subject not as expected: " + msg->subject(true)->asUnicodeString()); + } + return KAsync::null(); + } + } + if (inspectionType == Sink::ResourceControl::Inspection::ExistenceInspectionType) { + if (QFileInfo(filePath).exists() != expectedValue.toBool()) { + return KAsync::error(1, "Wrong file existence: " + filePath); + } + } + } + if (domainType == ENTITY_TYPE_FOLDER) { + const auto remoteId = syncStore->resolveLocalId(ENTITY_TYPE_FOLDER, entityId); + auto folder = entityStore.readLatest(entityId); + + if (inspectionType == Sink::ResourceControl::Inspection::CacheIntegrityInspectionType) { + SinkTrace() << "Inspecting cache integrity" << remoteId; + if (!QDir(remoteId).exists()) { + return KAsync::error(1, "The directory is not existing: " + remoteId); + } + + int expectedCount = 0; + Index index("mail.index.folder", transaction); + index.lookup(entityId, [&](const QByteArray &sinkId) { + expectedCount++; + }, + [&](const Index::Error &error) { + SinkWarning() << "Error in index: " << error.message << property; + }); + + QDir dir(remoteId + "/cur"); + const QFileInfoList list = dir.entryInfoList(QDir::Files); + if (list.size() != expectedCount) { + for (const auto &fileInfo : list) { + SinkWarning() << "Found in cache: " << fileInfo.fileName(); + } + return KAsync::error(1, QString("Wrong number of files; found %1 instead of %2.").arg(list.size()).arg(expectedCount)); + } + } + if (inspectionType == Sink::ResourceControl::Inspection::ExistenceInspectionType) { + if (!remoteId.endsWith(folder.getName().toUtf8())) { + return KAsync::error(1, "Wrong folder name: " + remoteId); + } + //TODO we shouldn't use the remoteId here to figure out the path, it could be gone/changed already + if (QDir(remoteId).exists() != expectedValue.toBool()) { + return KAsync::error(1, "Wrong folder existence: " + remoteId); + } + } + + } + return KAsync::null(); + } +}; + MaildirResource::MaildirResource(const Sink::ResourceContext &resourceContext) : Sink::GenericResource(resourceContext) @@ -439,6 +536,7 @@ MaildirResource::MaildirResource(const Sink::ResourceContext &resourceContext) auto synchronizer = QSharedPointer::create(resourceContext); synchronizer->mMaildirPath = mMaildirPath; setupSynchronizer(synchronizer); + setupInspector(QSharedPointer::create(resourceContext)); setupPreprocessors(ENTITY_TYPE_MAIL, QVector() << new SpecialPurposeProcessor(resourceContext.resourceType, resourceContext.instanceId()) << new MaildirMimeMessageMover(resourceContext.instanceId(), mMaildirPath) << new MaildirMailPropertyExtractor); setupPreprocessors(ENTITY_TYPE_FOLDER, QVector() << new FolderPreprocessor(mMaildirPath)); @@ -458,93 +556,6 @@ MaildirResource::MaildirResource(const Sink::ResourceContext &resourceContext) synchronizer->commit(); } -KAsync::Job MaildirResource::inspect(int inspectionType, const QByteArray &inspectionId, const QByteArray &domainType, const QByteArray &entityId, const QByteArray &property, const QVariant &expectedValue) -{ - auto synchronizationStore = QSharedPointer::create(Sink::storageLocation(), mResourceContext.instanceId() + ".synchronization", Sink::Storage::DataStore::ReadOnly); - auto synchronizationTransaction = synchronizationStore->createTransaction(Sink::Storage::DataStore::ReadOnly); - - auto mainStore = QSharedPointer::create(Sink::storageLocation(), mResourceContext.instanceId(), Sink::Storage::DataStore::ReadOnly); - auto transaction = mainStore->createTransaction(Sink::Storage::DataStore::ReadOnly); - - Sink::Storage::EntityStore entityStore(mResourceContext); - auto syncStore = QSharedPointer::create(synchronizationTransaction); - - SinkTrace() << "Inspecting " << inspectionType << domainType << entityId << property << expectedValue; - - if (domainType == ENTITY_TYPE_MAIL) { - auto mail = entityStore.readLatest(entityId); - const auto filePath = getFilePathFromMimeMessagePath(mail.getMimeMessagePath()); - - if (inspectionType == Sink::ResourceControl::Inspection::PropertyInspectionType) { - if (property == "unread") { - const auto flags = KPIM::Maildir::readEntryFlags(filePath.split('/').last()); - if (expectedValue.toBool() && (flags & KPIM::Maildir::Seen)) { - return KAsync::error(1, "Expected unread but couldn't find it."); - } - if (!expectedValue.toBool() && !(flags & KPIM::Maildir::Seen)) { - return KAsync::error(1, "Expected read but couldn't find it."); - } - return KAsync::null(); - } - if (property == "subject") { - KMime::Message *msg = new KMime::Message; - msg->setHead(KMime::CRLFtoLF(KPIM::Maildir::readEntryHeadersFromFile(filePath))); - msg->parse(); - - if (msg->subject(true)->asUnicodeString() != expectedValue.toString()) { - return KAsync::error(1, "Subject not as expected: " + msg->subject(true)->asUnicodeString()); - } - return KAsync::null(); - } - } - if (inspectionType == Sink::ResourceControl::Inspection::ExistenceInspectionType) { - if (QFileInfo(filePath).exists() != expectedValue.toBool()) { - return KAsync::error(1, "Wrong file existence: " + filePath); - } - } - } - if (domainType == ENTITY_TYPE_FOLDER) { - const auto remoteId = syncStore->resolveLocalId(ENTITY_TYPE_FOLDER, entityId); - auto folder = entityStore.readLatest(entityId); - - if (inspectionType == Sink::ResourceControl::Inspection::CacheIntegrityInspectionType) { - SinkTrace() << "Inspecting cache integrity" << remoteId; - if (!QDir(remoteId).exists()) { - return KAsync::error(1, "The directory is not existing: " + remoteId); - } - - int expectedCount = 0; - Index index("mail.index.folder", transaction); - index.lookup(entityId, [&](const QByteArray &sinkId) { - expectedCount++; - }, - [&](const Index::Error &error) { - SinkWarning() << "Error in index: " << error.message << property; - }); - - QDir dir(remoteId + "/cur"); - const QFileInfoList list = dir.entryInfoList(QDir::Files); - if (list.size() != expectedCount) { - for (const auto &fileInfo : list) { - SinkWarning() << "Found in cache: " << fileInfo.fileName(); - } - return KAsync::error(1, QString("Wrong number of files; found %1 instead of %2.").arg(list.size()).arg(expectedCount)); - } - } - if (inspectionType == Sink::ResourceControl::Inspection::ExistenceInspectionType) { - if (!remoteId.endsWith(folder.getName().toUtf8())) { - return KAsync::error(1, "Wrong folder name: " + remoteId); - } - //TODO we shouldn't use the remoteId here to figure out the path, it could be gone/changed already - if (QDir(remoteId).exists() != expectedValue.toBool()) { - return KAsync::error(1, "Wrong folder existence: " + remoteId); - } - } - - } - return KAsync::null(); -} - MaildirResourceFactory::MaildirResourceFactory(QObject *parent) : Sink::ResourceFactory(parent) diff --git a/examples/maildirresource/maildirresource.h b/examples/maildirresource/maildirresource.h index 4eb2042..61fe438 100644 --- a/examples/maildirresource/maildirresource.h +++ b/examples/maildirresource/maildirresource.h @@ -43,9 +43,8 @@ class MaildirResource : public Sink::GenericResource { public: MaildirResource(const Sink::ResourceContext &resourceContext); - KAsync::Job inspect(int inspectionType, const QByteArray &inspectionId, const QByteArray &domainType, const QByteArray &entityId, const QByteArray &property, const QVariant &expectedValue) Q_DECL_OVERRIDE; -private: +private: QStringList listAvailableFolders(); QString mMaildirPath; QString mDraftsFolder; -- cgit v1.2.3