diff options
Diffstat (limited to 'framework/src/entitymodel.cpp')
-rw-r--r-- | framework/src/entitymodel.cpp | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/framework/src/entitymodel.cpp b/framework/src/entitymodel.cpp new file mode 100644 index 00000000..5d8ac83e --- /dev/null +++ b/framework/src/entitymodel.cpp | |||
@@ -0,0 +1,142 @@ | |||
1 | /* | ||
2 | Copyright (c) 2018 Christian Mollekopf <mollekopf@kolabsys.com> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU Library General Public License as published by | ||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
12 | License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301, USA. | ||
18 | */ | ||
19 | |||
20 | #include "entitymodel.h" | ||
21 | |||
22 | #include <sink/store.h> | ||
23 | #include <sink/log.h> | ||
24 | |||
25 | using namespace Sink; | ||
26 | using namespace Sink::ApplicationDomain; | ||
27 | |||
28 | EntityModel::EntityModel(QObject *parent) : QSortFilterProxyModel(parent) | ||
29 | { | ||
30 | setDynamicSortFilter(true); | ||
31 | sort(0, Qt::AscendingOrder); | ||
32 | } | ||
33 | |||
34 | EntityModel::~EntityModel() | ||
35 | { | ||
36 | |||
37 | } | ||
38 | |||
39 | QHash< int, QByteArray > EntityModel::roleNames() const | ||
40 | { | ||
41 | return mRoleNames; | ||
42 | } | ||
43 | |||
44 | QVariant EntityModel::data(const QModelIndex &idx, int role) const | ||
45 | { | ||
46 | auto srcIdx = mapToSource(idx); | ||
47 | auto entity = srcIdx.data(Sink::Store::DomainObjectBaseRole).value<Sink::ApplicationDomain::ApplicationDomainType::Ptr>(); | ||
48 | |||
49 | const auto roleName = mRoleNames.value(role); | ||
50 | qWarning() << "Fetch data" << idx << role << roleName; | ||
51 | if (roleName == "identifier") { | ||
52 | return entity->identifier(); | ||
53 | } else if (roleName == "object") { | ||
54 | return QVariant::fromValue(entity); | ||
55 | } else { | ||
56 | return entity->getProperty(roleName); | ||
57 | } | ||
58 | } | ||
59 | |||
60 | void EntityModel::runQuery(const Query &query) | ||
61 | { | ||
62 | if (mType == "calendar") { | ||
63 | mModel = Store::loadModel<Calendar>(query); | ||
64 | } else { | ||
65 | qWarning() << "Type not supported " << mType; | ||
66 | } | ||
67 | setSourceModel(mModel.data()); | ||
68 | } | ||
69 | |||
70 | void EntityModel::updateQuery() | ||
71 | { | ||
72 | if (mType.isEmpty() || mRoles.isEmpty()) { | ||
73 | return; | ||
74 | } | ||
75 | |||
76 | Query query; | ||
77 | if (!mAccountId.isEmpty()) { | ||
78 | query.resourceFilter<SinkResource::Account>(mAccountId.toUtf8()); | ||
79 | } | ||
80 | query.setFlags(Sink::Query::LiveQuery | Sink::Query::UpdateStatus); | ||
81 | |||
82 | for (const auto &property: mRoles.keys()) { | ||
83 | query.requestedProperties << property; | ||
84 | } | ||
85 | runQuery(query); | ||
86 | } | ||
87 | |||
88 | void EntityModel::setAccountId(const QString &accountId) | ||
89 | { | ||
90 | |||
91 | //Get all folders of an account | ||
92 | mAccountId = accountId; | ||
93 | updateQuery(); | ||
94 | } | ||
95 | |||
96 | QString EntityModel::accountId() const | ||
97 | { | ||
98 | return {}; | ||
99 | } | ||
100 | |||
101 | void EntityModel::setType(const QString &type) | ||
102 | { | ||
103 | mType = type; | ||
104 | updateQuery(); | ||
105 | } | ||
106 | |||
107 | QString EntityModel::type() const | ||
108 | { | ||
109 | return {}; | ||
110 | } | ||
111 | |||
112 | void EntityModel::setRoles(const QStringList &roles) | ||
113 | { | ||
114 | mRoleNames.clear(); | ||
115 | int role = Qt::UserRole + 1; | ||
116 | mRoleNames.insert(role++, "identifier"); | ||
117 | mRoleNames.insert(role++, "object"); | ||
118 | for (int i = 0; i < roles.size(); i++) { | ||
119 | mRoleNames.insert(role++, roles.at(i).toLatin1()); | ||
120 | } | ||
121 | mRoles.clear(); | ||
122 | for (const auto &r : mRoleNames.keys()) { | ||
123 | mRoles.insert(mRoleNames.value(r), r); | ||
124 | } | ||
125 | updateQuery(); | ||
126 | } | ||
127 | |||
128 | QStringList EntityModel::roles() const | ||
129 | { | ||
130 | // return mRoleNames.values(); | ||
131 | return {}; | ||
132 | } | ||
133 | |||
134 | void EntityModel::setFilter(const QVariantMap &) | ||
135 | { | ||
136 | //TODO | ||
137 | } | ||
138 | |||
139 | QVariantMap EntityModel::filter() const | ||
140 | { | ||
141 | return {}; | ||
142 | } | ||