From b29a17465d1e52bd7dd5c57f08e7af53e915eee6 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Thu, 23 Jun 2016 17:05:47 +0200 Subject: Share special purpose preprocessor implementation. --- common/CMakeLists.txt | 1 + common/specialpurposepreprocessor.cpp | 88 ++++++++++++++++++++++++++++++++++ common/specialpurposepreprocessor.h | 43 +++++++++++++++++ examples/imapresource/imapresource.cpp | 88 ++-------------------------------- 4 files changed, 137 insertions(+), 83 deletions(-) create mode 100644 common/specialpurposepreprocessor.cpp create mode 100644 common/specialpurposepreprocessor.h diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 3083a2e..5eb15ba 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -74,6 +74,7 @@ set(command_SRCS sourcewriteback.cpp entityreader.cpp mailpreprocessor.cpp + specialpurposepreprocessor.cpp ${storage_SRCS}) add_library(${PROJECT_NAME} SHARED ${command_SRCS}) diff --git a/common/specialpurposepreprocessor.cpp b/common/specialpurposepreprocessor.cpp new file mode 100644 index 0000000..2892105 --- /dev/null +++ b/common/specialpurposepreprocessor.cpp @@ -0,0 +1,88 @@ +#include "specialpurposepreprocessor.h" +#include "entityreader.h" +#include "query.h" +#include "applicationdomaintype.h" + +using namespace Sink; + +static QHash specialPurposeFolders() +{ + QHash hash; + //FIXME localize + hash.insert("drafts", "Drafts"); + hash.insert("trash", "Trash"); + return hash; +} + +static QHash specialPurposeNames() +{ + QHash hash; + for (const auto &value : specialPurposeFolders().values()) { + hash.insert(value.toLower(), specialPurposeFolders().key(value)); + } + return hash; +} + +//specialpurpose, name +static QHash sSpecialPurposeFolders = specialPurposeFolders(); +//Lowercase-name, specialpurpose +static QHash sSpecialPurposeNames = specialPurposeNames(); + +namespace SpecialPurpose { +bool isSpecialPurposeFolderName(const QString &name) +{ + return sSpecialPurposeNames.contains(name.toLower()); +} + +QByteArray getSpecialPurposeType(const QString &name) +{ + return sSpecialPurposeNames.value(name.toLower()); +} +} + +SpecialPurposeProcessor::SpecialPurposeProcessor(const QByteArray &resourceType, const QByteArray &resourceInstanceIdentifier) : mResourceType(resourceType), mResourceInstanceIdentifier(resourceInstanceIdentifier) {} + +QByteArray SpecialPurposeProcessor::ensureFolder(Sink::Storage::Transaction &transaction, const QByteArray &specialPurpose) +{ + if (!mSpecialPurposeFolders.contains(specialPurpose)) { + //Try to find an existing drafts folder + Sink::EntityReader reader(mResourceType, mResourceInstanceIdentifier, transaction); + reader.query(Sink::Query().filter(Query::Comparator(specialPurpose, Query::Comparator::Contains)), + [this, specialPurpose](const ApplicationDomain::Folder &f) -> bool{ + mSpecialPurposeFolders.insert(specialPurpose, f.identifier()); + return false; + }); + if (!mSpecialPurposeFolders.contains(specialPurpose)) { + Trace() << "Failed to find a drafts folder, creating a new one"; + auto folder = ApplicationDomain::Folder::create(mResourceInstanceIdentifier); + folder.setSpecialPurpose(QByteArrayList() << specialPurpose); + folder.setName(sSpecialPurposeFolders.value(specialPurpose)); + folder.setIcon("folder"); + //This processes the pipeline synchronously + createEntity(folder); + mSpecialPurposeFolders.insert(specialPurpose, folder.identifier()); + } + } + return mSpecialPurposeFolders.value(specialPurpose); +} + +void SpecialPurposeProcessor::moveToFolder(Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction) +{ + if (newEntity.getProperty("trash").toBool()) { + newEntity.setProperty("folder", ensureFolder(transaction, "trash")); + return; + } + if (newEntity.getProperty("draft").toBool()) { + newEntity.setProperty("folder", ensureFolder(transaction, "drafts")); + } +} + +void SpecialPurposeProcessor::newEntity(const QByteArray &uid, qint64 revision, Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction) +{ + moveToFolder(newEntity, transaction); +} + +void SpecialPurposeProcessor::modifiedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction) +{ + moveToFolder(newEntity, transaction); +} diff --git a/common/specialpurposepreprocessor.h b/common/specialpurposepreprocessor.h new file mode 100644 index 0000000..a33701b --- /dev/null +++ b/common/specialpurposepreprocessor.h @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2015 Christian Mollekopf + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#include "sink_export.h" + +#include "pipeline.h" + +namespace SpecialPurpose { + bool SINK_EXPORT isSpecialPurposeFolderName(const QString &name); + QByteArray SINK_EXPORT getSpecialPurposeType(const QString &name); +} + +class SINK_EXPORT SpecialPurposeProcessor : public Sink::Preprocessor +{ +public: + SpecialPurposeProcessor(const QByteArray &resourceType, const QByteArray &resourceInstanceIdentifier); + + QByteArray ensureFolder(Sink::Storage::Transaction &transaction, const QByteArray &specialPurpose); + + void moveToFolder(Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction); + + void newEntity(const QByteArray &uid, qint64 revision, Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE; + void modifiedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE; + + QHash mSpecialPurposeFolders; + QByteArray mResourceType; + QByteArray mResourceInstanceIdentifier; +}; diff --git a/examples/imapresource/imapresource.cpp b/examples/imapresource/imapresource.cpp index 18747bf..605dbc2 100644 --- a/examples/imapresource/imapresource.cpp +++ b/examples/imapresource/imapresource.cpp @@ -48,6 +48,7 @@ #include "imapserverproxy.h" #include "entityreader.h" #include "mailpreprocessor.h" +#include "specialpurposepreprocessor.h" //This is the resources entity type, and not the domain type #define ENTITY_TYPE_MAIL "mail" @@ -59,85 +60,6 @@ using namespace Imap; using namespace Sink; -static QHash specialPurposeFolders() -{ - QHash hash; - //FIXME localize - hash.insert("drafts", "Drafts"); - hash.insert("trash", "Trash"); - return hash; -} - -static QHash specialPurposeNames() -{ - QHash hash; - for (const auto &value : specialPurposeFolders().values()) { - hash.insert(value.toLower(), specialPurposeFolders().key(value)); - } - return hash; -} - -//specialpurpose, name -static QHash sSpecialPurposeFolders = specialPurposeFolders(); -//Lowercase-name, specialpurpose -static QHash sSpecialPurposeNames = specialPurposeNames(); - -class SpecialPurposeProcessor : public Sink::Preprocessor -{ -public: - SpecialPurposeProcessor(const QByteArray &resourceType, const QByteArray &resourceInstanceIdentifier) : mResourceType(resourceType), mResourceInstanceIdentifier(resourceInstanceIdentifier) {} - - QByteArray ensureFolder(Sink::Storage::Transaction &transaction, const QByteArray &specialPurpose) - { - if (!mSpecialPurposeFolders.contains(specialPurpose)) { - //Try to find an existing drafts folder - Sink::EntityReader reader(mResourceType, mResourceInstanceIdentifier, transaction); - reader.query(Sink::Query().filter(Query::Comparator("drafts", Query::Comparator::Contains)), - [this, specialPurpose](const ApplicationDomain::Folder &f) -> bool{ - mSpecialPurposeFolders.insert(specialPurpose, f.identifier()); - return false; - }); - if (!mSpecialPurposeFolders.contains(specialPurpose)) { - Trace() << "Failed to find a drafts folder, creating a new one"; - auto folder = ApplicationDomain::Folder::create(mResourceInstanceIdentifier); - folder.setSpecialPurpose(QByteArrayList() << specialPurpose); - folder.setName(sSpecialPurposeFolders.value(specialPurpose)); - folder.setIcon("folder"); - //This processes the pipeline synchronously - createEntity(folder); - mSpecialPurposeFolders.insert(specialPurpose, folder.identifier()); - } - } - return mSpecialPurposeFolders.value(specialPurpose); - } - - void moveToFolder(Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction) - { - if (newEntity.getProperty("trash").toBool()) { - newEntity.setProperty("folder", ensureFolder(transaction, "trash")); - return; - } - if (newEntity.getProperty("draft").toBool()) { - newEntity.setProperty("folder", ensureFolder(transaction, "drafts")); - } - } - - void newEntity(const QByteArray &uid, qint64 revision, Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE - { - moveToFolder(newEntity, transaction); - } - - void modifiedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE - { - moveToFolder(newEntity, transaction); - } - - QHash mSpecialPurposeFolders; - QByteArray mResourceType; - QByteArray mResourceInstanceIdentifier; -}; - - static qint64 uidFromMailRid(const QByteArray &remoteId) { auto ridParts = remoteId.split(':'); @@ -180,8 +102,8 @@ public: folder.setProperty(ApplicationDomain::Folder::Name::name, folderName); folder.setProperty(ApplicationDomain::Folder::Icon::name, icon); QHash mergeCriteria; - if (sSpecialPurposeNames.contains(folderName.toLower())) { - auto type = sSpecialPurposeNames.value(folderName.toLower()); + if (SpecialPurpose::isSpecialPurposeFolderName(folderName)) { + auto type = SpecialPurpose::getSpecialPurposeType(folderName); folder.setProperty(ApplicationDomain::Folder::SpecialPurpose::name, QVariant::fromValue(QByteArrayList() << type)); mergeCriteria.insert(ApplicationDomain::Folder::SpecialPurpose::name, Query::Comparator(type, Query::Comparator::Contains)); } @@ -481,8 +403,8 @@ public: auto mergeJob = imap->login(mUser, mPassword) .then(imap->fetchFolders([=](const QVector &folders) { for (const auto &f : folders) { - if (sSpecialPurposeNames.contains(f.pathParts.last().toLower())) { - specialPurposeFolders->insert(sSpecialPurposeNames.value(f.pathParts.last().toLower()), f.path); + if (SpecialPurpose::isSpecialPurposeFolderName(f.pathParts.last())) { + specialPurposeFolders->insert(SpecialPurpose::getSpecialPurposeType(f.pathParts.last()), f.path); }; } })) -- cgit v1.2.3