diff options
Diffstat (limited to 'akonadish/akonadish_utils.h')
-rw-r--r-- | akonadish/akonadish_utils.h | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/akonadish/akonadish_utils.h b/akonadish/akonadish_utils.h new file mode 100644 index 0000000..c15162f --- /dev/null +++ b/akonadish/akonadish_utils.h | |||
@@ -0,0 +1,81 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2015 Aaron Seigo <aseigo@kolabsystems.com> | ||
3 | * Copyright (C) 2015 Christian Mollekopf <mollekopf@kolabsystems.com> | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation; either version 2 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program; if not, write to the | ||
17 | * Free Software Foundation, Inc., | ||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
19 | */ | ||
20 | |||
21 | #pragma once | ||
22 | |||
23 | #include <QAbstractItemModel> | ||
24 | #include <QSharedPointer> | ||
25 | |||
26 | #include "common/query.h" | ||
27 | #include "common/clientapi.h" | ||
28 | |||
29 | namespace AkonadishUtils | ||
30 | { | ||
31 | |||
32 | class StoreBase; | ||
33 | |||
34 | bool isValidStoreType(const QString &type); | ||
35 | StoreBase &getStore(const QString &type); | ||
36 | QSharedPointer<QAbstractItemModel> loadModel(const QString &type, Akonadi2::Query query); | ||
37 | |||
38 | /** | ||
39 | * A small abstraction layer to use the akonadi store with the type available as string. | ||
40 | */ | ||
41 | class StoreBase { | ||
42 | public: | ||
43 | virtual Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject() = 0; | ||
44 | virtual Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject(const QByteArray &resourceInstanceIdentifier, const QByteArray &identifier = QByteArray()) = 0; | ||
45 | virtual KAsync::Job<void> create(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) = 0; | ||
46 | virtual KAsync::Job<void> modify(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) = 0; | ||
47 | virtual KAsync::Job<void> remove(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) = 0; | ||
48 | virtual QSharedPointer<QAbstractItemModel> loadModel(const Akonadi2::Query &query) = 0; | ||
49 | }; | ||
50 | |||
51 | template <typename T> | ||
52 | class Store : public StoreBase { | ||
53 | public: | ||
54 | Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject() Q_DECL_OVERRIDE { | ||
55 | return T::Ptr::create(); | ||
56 | } | ||
57 | |||
58 | Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject(const QByteArray &resourceInstanceIdentifier, const QByteArray &identifier = QByteArray()) Q_DECL_OVERRIDE { | ||
59 | return T::Ptr::create(resourceInstanceIdentifier, identifier, 0, QSharedPointer<Akonadi2::ApplicationDomain::MemoryBufferAdaptor>::create()); | ||
60 | } | ||
61 | |||
62 | KAsync::Job<void> create(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) Q_DECL_OVERRIDE { | ||
63 | return Akonadi2::Store::create<T>(*static_cast<const T*>(&type)); | ||
64 | } | ||
65 | |||
66 | KAsync::Job<void> modify(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) Q_DECL_OVERRIDE { | ||
67 | return Akonadi2::Store::modify<T>(*static_cast<const T*>(&type)); | ||
68 | } | ||
69 | |||
70 | KAsync::Job<void> remove(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) Q_DECL_OVERRIDE { | ||
71 | return Akonadi2::Store::remove<T>(*static_cast<const T*>(&type)); | ||
72 | } | ||
73 | |||
74 | QSharedPointer<QAbstractItemModel> loadModel(const Akonadi2::Query &query) Q_DECL_OVERRIDE { | ||
75 | return Akonadi2::Store::loadModel<T>(query); | ||
76 | } | ||
77 | }; | ||
78 | |||
79 | |||
80 | } | ||
81 | |||