diff options
-rw-r--r-- | common/domain/applicationdomaintype.cpp | 35 | ||||
-rw-r--r-- | common/domain/propertyregistry.cpp | 112 | ||||
-rw-r--r-- | common/domain/propertyregistry.h | 87 |
3 files changed, 232 insertions, 2 deletions
diff --git a/common/domain/applicationdomaintype.cpp b/common/domain/applicationdomaintype.cpp index 29b50b9..4bd17fe 100644 --- a/common/domain/applicationdomaintype.cpp +++ b/common/domain/applicationdomaintype.cpp | |||
@@ -21,16 +21,47 @@ | |||
21 | #include "log.h" | 21 | #include "log.h" |
22 | #include "../bufferadaptor.h" | 22 | #include "../bufferadaptor.h" |
23 | #include "definitions.h" | 23 | #include "definitions.h" |
24 | #include "propertyregistry.h" | ||
24 | #include "storage.h" //for generateUid() | 25 | #include "storage.h" //for generateUid() |
25 | #include <QFile> | 26 | #include <QFile> |
26 | 27 | ||
27 | SINK_DEBUG_AREA("applicationdomaintype"); | 28 | SINK_DEBUG_AREA("applicationdomaintype"); |
28 | 29 | ||
30 | template <typename DomainType, typename Property> | ||
31 | int registerProperty() { | ||
32 | Sink::Private::PropertyRegistry::instance().registerProperty<Property>(Sink::ApplicationDomain::getTypeName<DomainType>()); | ||
33 | return 0; | ||
34 | } | ||
35 | |||
36 | #define SINK_REGISTER_PROPERTY(ENTITYTYPE, PROPERTY) \ | ||
37 | constexpr const char *ENTITYTYPE::PROPERTY::name; \ | ||
38 | static int foo##ENTITYTYPE##PROPERTY = registerProperty<ENTITYTYPE, ENTITYTYPE::PROPERTY>(); | ||
39 | |||
29 | namespace Sink { | 40 | namespace Sink { |
30 | namespace ApplicationDomain { | 41 | namespace ApplicationDomain { |
31 | 42 | ||
32 | constexpr const char *Mail::ThreadId::name; | 43 | SINK_REGISTER_PROPERTY(Mail, Sender); |
33 | constexpr const char *Mail::Folder::name; | 44 | SINK_REGISTER_PROPERTY(Mail, To); |
45 | SINK_REGISTER_PROPERTY(Mail, Cc); | ||
46 | SINK_REGISTER_PROPERTY(Mail, Bcc); | ||
47 | SINK_REGISTER_PROPERTY(Mail, Subject); | ||
48 | SINK_REGISTER_PROPERTY(Mail, Date); | ||
49 | SINK_REGISTER_PROPERTY(Mail, Unread); | ||
50 | SINK_REGISTER_PROPERTY(Mail, Important); | ||
51 | SINK_REGISTER_PROPERTY(Mail, Folder); | ||
52 | SINK_REGISTER_PROPERTY(Mail, MimeMessage); | ||
53 | SINK_REGISTER_PROPERTY(Mail, FullPayloadAvailable); | ||
54 | SINK_REGISTER_PROPERTY(Mail, Draft); | ||
55 | SINK_REGISTER_PROPERTY(Mail, Trash); | ||
56 | SINK_REGISTER_PROPERTY(Mail, Sent); | ||
57 | SINK_REGISTER_PROPERTY(Mail, MessageId); | ||
58 | SINK_REGISTER_PROPERTY(Mail, ParentMessageId); | ||
59 | SINK_REGISTER_PROPERTY(Mail, ThreadId); | ||
60 | |||
61 | SINK_REGISTER_PROPERTY(Folder, Name); | ||
62 | SINK_REGISTER_PROPERTY(Folder, Icon); | ||
63 | SINK_REGISTER_PROPERTY(Folder, SpecialPurpose); | ||
64 | SINK_REGISTER_PROPERTY(Folder, Enabled); | ||
34 | 65 | ||
35 | static const int foo = [] { | 66 | static const int foo = [] { |
36 | QMetaType::registerEqualsComparator<Reference>(); | 67 | QMetaType::registerEqualsComparator<Reference>(); |
diff --git a/common/domain/propertyregistry.cpp b/common/domain/propertyregistry.cpp new file mode 100644 index 0000000..2208193 --- /dev/null +++ b/common/domain/propertyregistry.cpp | |||
@@ -0,0 +1,112 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2017 Christian Mollekopf <chrigi_1@fastmail.fm> | ||
3 | * | ||
4 | * This library is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU Lesser General Public | ||
6 | * License as published by the Free Software Foundation; either | ||
7 | * version 2.1 of the License, or (at your option) version 3, or any | ||
8 | * later version accepted by the membership of KDE e.V. (or its | ||
9 | * successor approved by the membership of KDE e.V.), which shall | ||
10 | * act as a proxy defined in Section 6 of version 3 of the license. | ||
11 | * | ||
12 | * This library is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
19 | */ | ||
20 | #include "propertyregistry.h" | ||
21 | |||
22 | #include <log.h> | ||
23 | |||
24 | namespace Sink { | ||
25 | namespace Private { | ||
26 | |||
27 | template <typename T> | ||
28 | QVariant parseString(const QString &); | ||
29 | |||
30 | template <> | ||
31 | QVariant parseString<QString>(const QString &s) | ||
32 | { | ||
33 | return QVariant::fromValue(s); | ||
34 | } | ||
35 | |||
36 | template <> | ||
37 | QVariant parseString<QByteArray>(const QString &s) | ||
38 | { | ||
39 | return QVariant::fromValue(s.toUtf8()); | ||
40 | } | ||
41 | |||
42 | template <> | ||
43 | QVariant parseString<Sink::ApplicationDomain::Reference>(const QString &s) | ||
44 | { | ||
45 | return QVariant::fromValue(Sink::ApplicationDomain::Reference{s.toLatin1()}); | ||
46 | } | ||
47 | |||
48 | template <> | ||
49 | QVariant parseString<Sink::ApplicationDomain::BLOB>(const QString &s) | ||
50 | { | ||
51 | //TODO copy path | ||
52 | // return QVariant::fromValue(Sink::ApplicationDomain::BLOB{s.toLatin1()}); | ||
53 | Q_ASSERT(false); | ||
54 | return QVariant{}; | ||
55 | } | ||
56 | |||
57 | template <> | ||
58 | QVariant parseString<bool>(const QString &s) | ||
59 | { | ||
60 | if (s == "true") { | ||
61 | return QVariant::fromValue(true); | ||
62 | } | ||
63 | return QVariant::fromValue(false); | ||
64 | } | ||
65 | |||
66 | template <> | ||
67 | QVariant parseString<QList<QByteArray>>(const QString &s) | ||
68 | { | ||
69 | auto list = s.split(','); | ||
70 | QList<QByteArray> result; | ||
71 | std::transform(list.constBegin(), list.constEnd(), std::back_inserter(result), [] (const QString &s) { return s.toUtf8(); }); | ||
72 | return QVariant::fromValue(result); | ||
73 | } | ||
74 | |||
75 | template <> | ||
76 | QVariant parseString<QDateTime>(const QString &s) | ||
77 | { | ||
78 | return QVariant::fromValue(QDateTime::fromString(s)); | ||
79 | } | ||
80 | |||
81 | template <> | ||
82 | QVariant parseString<Sink::ApplicationDomain::Mail::Contact>(const QString &s) | ||
83 | { | ||
84 | Q_ASSERT(false); | ||
85 | return QVariant{}; | ||
86 | } | ||
87 | |||
88 | template <> | ||
89 | QVariant parseString<QList<Sink::ApplicationDomain::Mail::Contact>>(const QString &s) | ||
90 | { | ||
91 | Q_ASSERT(false); | ||
92 | return QVariant{}; | ||
93 | } | ||
94 | |||
95 | PropertyRegistry &PropertyRegistry::instance() | ||
96 | { | ||
97 | static PropertyRegistry instance; | ||
98 | return instance; | ||
99 | } | ||
100 | |||
101 | QVariant PropertyRegistry::parse(const QByteArray &type, const QByteArray &property, const QString &value) | ||
102 | { | ||
103 | auto parser = registry[type].properties[property].parser; | ||
104 | if (parser) { | ||
105 | return parser(value); | ||
106 | } | ||
107 | SinkWarningCtx(Sink::Log::Context{"PropertyRegistry"}) << "Couldn't find a parser for " << type << property; | ||
108 | return QVariant{}; | ||
109 | } | ||
110 | |||
111 | } | ||
112 | } | ||
diff --git a/common/domain/propertyregistry.h b/common/domain/propertyregistry.h new file mode 100644 index 0000000..16df23b --- /dev/null +++ b/common/domain/propertyregistry.h | |||
@@ -0,0 +1,87 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2017 Christian Mollekopf <chrigi_1@fastmail.fm> | ||
3 | * | ||
4 | * This library is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU Lesser General Public | ||
6 | * License as published by the Free Software Foundation; either | ||
7 | * version 2.1 of the License, or (at your option) version 3, or any | ||
8 | * later version accepted by the membership of KDE e.V. (or its | ||
9 | * successor approved by the membership of KDE e.V.), which shall | ||
10 | * act as a proxy defined in Section 6 of version 3 of the license. | ||
11 | * | ||
12 | * This library is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
19 | */ | ||
20 | |||
21 | #pragma once | ||
22 | |||
23 | #include <QHash> | ||
24 | #include <QString> | ||
25 | #include <QVariant> | ||
26 | #include <functional> | ||
27 | |||
28 | #include "applicationdomaintype.h" | ||
29 | |||
30 | namespace Sink { | ||
31 | namespace Private { | ||
32 | |||
33 | template <typename T> | ||
34 | QVariant parseString(const QString &); | ||
35 | |||
36 | template <> | ||
37 | QVariant parseString<QString>(const QString &s); | ||
38 | |||
39 | template <> | ||
40 | QVariant parseString<QByteArray>(const QString &s); | ||
41 | |||
42 | template <> | ||
43 | QVariant parseString<Sink::ApplicationDomain::Reference>(const QString &s); | ||
44 | |||
45 | template <> | ||
46 | QVariant parseString<Sink::ApplicationDomain::BLOB>(const QString &s); | ||
47 | |||
48 | template <> | ||
49 | QVariant parseString<bool>(const QString &s); | ||
50 | |||
51 | template <> | ||
52 | QVariant parseString<QList<QByteArray>>(const QString &s); | ||
53 | |||
54 | template <> | ||
55 | QVariant parseString<QDateTime>(const QString &s); | ||
56 | |||
57 | template <> | ||
58 | QVariant parseString<Sink::ApplicationDomain::Mail::Contact>(const QString &s); | ||
59 | |||
60 | template <> | ||
61 | QVariant parseString<QList<Sink::ApplicationDomain::Mail::Contact>>(const QString &s); | ||
62 | |||
63 | class PropertyRegistry | ||
64 | { | ||
65 | public: | ||
66 | struct Type { | ||
67 | struct Property { | ||
68 | std::function<QVariant(const QString &)> parser; | ||
69 | }; | ||
70 | QHash<QByteArray, Property> properties; | ||
71 | }; | ||
72 | |||
73 | QHash<QByteArray, Type> registry; | ||
74 | |||
75 | static PropertyRegistry &instance(); | ||
76 | |||
77 | template <typename PropertyType> | ||
78 | void registerProperty(const QByteArray &entityType) { | ||
79 | registry[entityType].properties[PropertyType::name].parser = Sink::Private::parseString<typename PropertyType::Type>; | ||
80 | } | ||
81 | |||
82 | QVariant parse(const QByteArray &type, const QByteArray &property, const QString &value); | ||
83 | }; | ||
84 | |||
85 | } | ||
86 | } | ||
87 | |||