diff options
Diffstat (limited to 'framework/src/domain/identitiesmodel.cpp')
-rw-r--r-- | framework/src/domain/identitiesmodel.cpp | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/framework/src/domain/identitiesmodel.cpp b/framework/src/domain/identitiesmodel.cpp new file mode 100644 index 00000000..0d97fc6a --- /dev/null +++ b/framework/src/domain/identitiesmodel.cpp | |||
@@ -0,0 +1,99 @@ | |||
1 | /* | ||
2 | Copyright (c) 2016 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 | #include "identitiesmodel.h" | ||
20 | #include <sink/store.h> | ||
21 | #include <sink/log.h> | ||
22 | |||
23 | using namespace Sink; | ||
24 | |||
25 | IdentitiesModel::IdentitiesModel(QObject *parent) : QIdentityProxyModel() | ||
26 | { | ||
27 | Sink::Query query; | ||
28 | query.setFlags(Sink::Query::LiveQuery); | ||
29 | query.request<Sink::ApplicationDomain::Identity::Name>() | ||
30 | .request<Sink::ApplicationDomain::Identity::Address>() | ||
31 | .request<Sink::ApplicationDomain::Identity::Account>(); | ||
32 | runQuery(query); | ||
33 | } | ||
34 | |||
35 | IdentitiesModel::~IdentitiesModel() | ||
36 | { | ||
37 | |||
38 | } | ||
39 | |||
40 | QHash< int, QByteArray > IdentitiesModel::roleNames() const | ||
41 | { | ||
42 | QHash<int, QByteArray> roles; | ||
43 | |||
44 | roles[Name] = "name"; | ||
45 | roles[Username] = "username"; | ||
46 | roles[Address] = "address"; | ||
47 | roles[IdentityId] = "identityId"; | ||
48 | roles[AccountId] = "accountId"; | ||
49 | roles[AccountName] = "accountName"; | ||
50 | roles[AccountIcon] = "accountIcon"; | ||
51 | roles[DisplayName] = "displayName"; | ||
52 | |||
53 | return roles; | ||
54 | } | ||
55 | |||
56 | QVariant IdentitiesModel::data(const QModelIndex &idx, int role) const | ||
57 | { | ||
58 | auto srcIdx = mapToSource(idx); | ||
59 | switch (role) { | ||
60 | case Name: | ||
61 | return srcIdx.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Identity::Ptr>()->getName(); | ||
62 | case Username: | ||
63 | return srcIdx.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Identity::Ptr>()->getName(); | ||
64 | case Address: | ||
65 | return srcIdx.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Identity::Ptr>()->getAddress(); | ||
66 | case IdentityId: | ||
67 | return srcIdx.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Identity::Ptr>()->identifier(); | ||
68 | case AccountId: | ||
69 | return srcIdx.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Identity::Ptr>()->getAccount(); | ||
70 | case AccountName: { | ||
71 | const auto accountId = srcIdx.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Identity::Ptr>()->getAccount(); | ||
72 | return mAccountNames.value(accountId); | ||
73 | } | ||
74 | case AccountIcon: { | ||
75 | const auto accountId = srcIdx.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Identity::Ptr>()->getAccount(); | ||
76 | return mAccountIcons.value(accountId); | ||
77 | } | ||
78 | case DisplayName: { | ||
79 | return data(idx, AccountName).toString() + ": " + data(idx, Username).toString() + ", " + data(idx, Address).toString(); | ||
80 | } | ||
81 | } | ||
82 | return QIdentityProxyModel::data(idx, role); | ||
83 | } | ||
84 | |||
85 | void IdentitiesModel::runQuery(const Sink::Query &query) | ||
86 | { | ||
87 | using namespace Sink::ApplicationDomain; | ||
88 | mModel = Sink::Store::loadModel<Identity>(query); | ||
89 | setSourceModel(mModel.data()); | ||
90 | |||
91 | Sink::Store::fetchAll<SinkAccount>(Sink::Query{}.request<SinkAccount::Icon>().request<SinkAccount::Name>()) | ||
92 | .then([this](const QList<SinkAccount::Ptr> &accounts) { | ||
93 | for (const auto &account : accounts) { | ||
94 | mAccountNames.insert(account->identifier(), account->getName()); | ||
95 | mAccountIcons.insert(account->identifier(), account->getIcon()); | ||
96 | } | ||
97 | emit layoutChanged(); | ||
98 | }).exec(); | ||
99 | } | ||