From ef492409892eedb85d7555002eebebe3f046b873 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 15 Dec 2015 13:41:29 +0100 Subject: Avoid duplication accross resource implementations --- common/genericresource.cpp | 6 ++++ common/genericresource.h | 3 ++ common/indexupdater.h | 87 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 common/indexupdater.h (limited to 'common') diff --git a/common/genericresource.cpp b/common/genericresource.cpp index 3655a63..4250a02 100644 --- a/common/genericresource.cpp +++ b/common/genericresource.cpp @@ -285,6 +285,12 @@ GenericResource::~GenericResource() delete mSourceChangeReplay; } +void GenericResource::addType(const QByteArray &type, DomainTypeAdaptorFactoryInterface::Ptr factory, const QVector &preprocessors) +{ + mPipeline->setPreprocessors(type, preprocessors); + mPipeline->setAdaptorFactory(type, factory); +} + KAsync::Job GenericResource::replay(const QByteArray &type, const QByteArray &key, const QByteArray &value) { return KAsync::null(); diff --git a/common/genericresource.h b/common/genericresource.h index f756273..82c8a49 100644 --- a/common/genericresource.h +++ b/common/genericresource.h @@ -23,6 +23,7 @@ #include #include #include +#include #include class Processor; @@ -31,6 +32,7 @@ class ChangeReplay; namespace Akonadi2 { class Pipeline; +class Preprocessor; /** * Generic Resource implementation. @@ -54,6 +56,7 @@ private Q_SLOTS: void updateLowerBoundRevision(); protected: + void addType(const QByteArray &type, DomainTypeAdaptorFactoryInterface::Ptr factory, const QVector &preprocessors); virtual KAsync::Job replay(const QByteArray &type, const QByteArray &key, const QByteArray &value); void onProcessorError(int errorCode, const QString &errorMessage); void enqueueCommand(MessageQueue &mq, int commandId, const QByteArray &data); diff --git a/common/indexupdater.h b/common/indexupdater.h new file mode 100644 index 0000000..48144e6 --- /dev/null +++ b/common/indexupdater.h @@ -0,0 +1,87 @@ +/* + * 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. + */ +#pragma once + +#include +#include + +class IndexUpdater : public Akonadi2::Preprocessor { +public: + IndexUpdater(const QByteArray &index, const QByteArray &type, const QByteArray &property) + :mIndexIdentifier(index), + mBufferType(type), + mProperty(property) + { + + } + + void newEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE + { + add(newEntity.getProperty(mProperty), uid, transaction); + } + + void modifiedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE + { + remove(oldEntity.getProperty(mProperty), uid, transaction); + add(newEntity.getProperty(mProperty), uid, transaction); + } + + void deletedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE + { + remove(oldEntity.getProperty(mProperty), uid, transaction); + } + +private: + void add(const QVariant &value, const QByteArray &uid, Akonadi2::Storage::Transaction &transaction) + { + if (value.isValid()) { + Index(mIndexIdentifier, transaction).add(value.toByteArray(), uid); + } + } + + void remove(const QVariant &value, const QByteArray &uid, Akonadi2::Storage::Transaction &transaction) + { + //TODO hide notfound error + Index(mIndexIdentifier, transaction).remove(value.toByteArray(), uid); + } + + QByteArray mIndexIdentifier; + QByteArray mBufferType; + QByteArray mProperty; +}; + +template +class DefaultIndexUpdater : public Akonadi2::Preprocessor { +public: + void newEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE + { + Akonadi2::ApplicationDomain::TypeImplementation::index(uid, newEntity, transaction); + } + + void modifiedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE + { + Akonadi2::ApplicationDomain::TypeImplementation::removeIndex(uid, oldEntity, transaction); + Akonadi2::ApplicationDomain::TypeImplementation::index(uid, newEntity, transaction); + } + + void deletedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE + { + Akonadi2::ApplicationDomain::TypeImplementation::removeIndex(uid, oldEntity, transaction); + } +}; -- cgit v1.2.3