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 +++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 common/specialpurposepreprocessor.cpp create mode 100644 common/specialpurposepreprocessor.h (limited to 'common') 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; +}; -- cgit v1.2.3