diff options
author | Aaron Seigo <aseigo@kde.org> | 2015-12-23 20:21:23 +0100 |
---|---|---|
committer | Aaron Seigo <aseigo@kde.org> | 2015-12-23 20:21:23 +0100 |
commit | 00a4b8b6a90539f42334a489e8e44a3d0e5a246c (patch) | |
tree | def0a1754b9b8e813f21fe02798280fa8a58cb36 /akonadi2_cli | |
parent | 01526599a191d2e31a836a9d12f089f564c646e6 (diff) | |
download | sink-00a4b8b6a90539f42334a489e8e44a3d0e5a246c.tar.gz sink-00a4b8b6a90539f42334a489e8e44a3d0e5a246c.zip |
some useful common utilities
Diffstat (limited to 'akonadi2_cli')
-rw-r--r-- | akonadi2_cli/akonadish_utils.cpp | 74 | ||||
-rw-r--r-- | akonadi2_cli/akonadish_utils.h | 81 |
2 files changed, 155 insertions, 0 deletions
diff --git a/akonadi2_cli/akonadish_utils.cpp b/akonadi2_cli/akonadish_utils.cpp new file mode 100644 index 0000000..bfb72ca --- /dev/null +++ b/akonadi2_cli/akonadish_utils.cpp | |||
@@ -0,0 +1,74 @@ | |||
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 | #include "akonadish_utils.h" | ||
22 | |||
23 | #include "common/clientapi.h" | ||
24 | |||
25 | namespace AkonadishUtils | ||
26 | { | ||
27 | |||
28 | bool isValidStoreType(const QString &type) | ||
29 | { | ||
30 | static const QSet<QString> types = QSet<QString>() << "folder" << "mail" << "event" << "resource"; | ||
31 | return types.contains(type); | ||
32 | } | ||
33 | |||
34 | StoreBase &getStore(const QString &type) | ||
35 | { | ||
36 | if (type == "folder") { | ||
37 | static Store<Akonadi2::ApplicationDomain::Folder> store; | ||
38 | return store; | ||
39 | } else if (type == "mail") { | ||
40 | static Store<Akonadi2::ApplicationDomain::Mail> store; | ||
41 | return store; | ||
42 | } else if (type == "event") { | ||
43 | static Store<Akonadi2::ApplicationDomain::Event> store; | ||
44 | return store; | ||
45 | } else if (type == "resource") { | ||
46 | static Store<Akonadi2::ApplicationDomain::AkonadiResource> store; | ||
47 | return store; | ||
48 | } | ||
49 | |||
50 | //TODO: reinstate the warning+assert | ||
51 | //Q_ASSERT(false); | ||
52 | //qWarning() << "Trying to get a store that doesn't exist, falling back to event"; | ||
53 | static Store<Akonadi2::ApplicationDomain::Event> store; | ||
54 | return store; | ||
55 | } | ||
56 | |||
57 | QSharedPointer<QAbstractItemModel> loadModel(const QString &type, Akonadi2::Query query) | ||
58 | { | ||
59 | if (type == "folder") { | ||
60 | query.requestedProperties << "name" << "parent"; | ||
61 | } else if (type == "mail") { | ||
62 | query.requestedProperties << "subject" << "folder" << "date"; | ||
63 | } else if (type == "event") { | ||
64 | query.requestedProperties << "summary"; | ||
65 | } else if (type == "resource") { | ||
66 | query.requestedProperties << "type"; | ||
67 | } | ||
68 | auto model = getStore(type).loadModel(query); | ||
69 | Q_ASSERT(model); | ||
70 | return model; | ||
71 | } | ||
72 | |||
73 | } | ||
74 | |||
diff --git a/akonadi2_cli/akonadish_utils.h b/akonadi2_cli/akonadish_utils.h new file mode 100644 index 0000000..c15162f --- /dev/null +++ b/akonadi2_cli/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 | |||