diff options
Diffstat (limited to 'common/indexupdater.h')
-rw-r--r-- | common/indexupdater.h | 87 |
1 files changed, 87 insertions, 0 deletions
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 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2015 Christian Mollekopf <chrigi_1@fastmail.fm> | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, write to the | ||
16 | * Free Software Foundation, Inc., | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
18 | */ | ||
19 | #pragma once | ||
20 | |||
21 | #include <pipeline.h> | ||
22 | #include <index.h> | ||
23 | |||
24 | class IndexUpdater : public Akonadi2::Preprocessor { | ||
25 | public: | ||
26 | IndexUpdater(const QByteArray &index, const QByteArray &type, const QByteArray &property) | ||
27 | :mIndexIdentifier(index), | ||
28 | mBufferType(type), | ||
29 | mProperty(property) | ||
30 | { | ||
31 | |||
32 | } | ||
33 | |||
34 | void newEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
35 | { | ||
36 | add(newEntity.getProperty(mProperty), uid, transaction); | ||
37 | } | ||
38 | |||
39 | void modifiedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
40 | { | ||
41 | remove(oldEntity.getProperty(mProperty), uid, transaction); | ||
42 | add(newEntity.getProperty(mProperty), uid, transaction); | ||
43 | } | ||
44 | |||
45 | void deletedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
46 | { | ||
47 | remove(oldEntity.getProperty(mProperty), uid, transaction); | ||
48 | } | ||
49 | |||
50 | private: | ||
51 | void add(const QVariant &value, const QByteArray &uid, Akonadi2::Storage::Transaction &transaction) | ||
52 | { | ||
53 | if (value.isValid()) { | ||
54 | Index(mIndexIdentifier, transaction).add(value.toByteArray(), uid); | ||
55 | } | ||
56 | } | ||
57 | |||
58 | void remove(const QVariant &value, const QByteArray &uid, Akonadi2::Storage::Transaction &transaction) | ||
59 | { | ||
60 | //TODO hide notfound error | ||
61 | Index(mIndexIdentifier, transaction).remove(value.toByteArray(), uid); | ||
62 | } | ||
63 | |||
64 | QByteArray mIndexIdentifier; | ||
65 | QByteArray mBufferType; | ||
66 | QByteArray mProperty; | ||
67 | }; | ||
68 | |||
69 | template<typename DomainType> | ||
70 | class DefaultIndexUpdater : public Akonadi2::Preprocessor { | ||
71 | public: | ||
72 | void newEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
73 | { | ||
74 | Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::index(uid, newEntity, transaction); | ||
75 | } | ||
76 | |||
77 | void modifiedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
78 | { | ||
79 | Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::removeIndex(uid, oldEntity, transaction); | ||
80 | Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::index(uid, newEntity, transaction); | ||
81 | } | ||
82 | |||
83 | void deletedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
84 | { | ||
85 | Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::removeIndex(uid, oldEntity, transaction); | ||
86 | } | ||
87 | }; | ||