diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-04-17 12:28:56 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-04-17 12:28:56 +0200 |
commit | a204de1c989067a6aff95e81949405a45e8cbe3f (patch) | |
tree | e2a396ee57585f7f8656079b15383381b3d870c0 /framework/domain/identitiesmodel.cpp | |
parent | 634772c6da51c1d69f804bd45bbedaeb88789cd6 (diff) | |
download | kube-a204de1c989067a6aff95e81949405a45e8cbe3f.tar.gz kube-a204de1c989067a6aff95e81949405a45e8cbe3f.zip |
Show identities instead of accounts
Diffstat (limited to 'framework/domain/identitiesmodel.cpp')
-rw-r--r-- | framework/domain/identitiesmodel.cpp | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/framework/domain/identitiesmodel.cpp b/framework/domain/identitiesmodel.cpp new file mode 100644 index 00000000..adb5d625 --- /dev/null +++ b/framework/domain/identitiesmodel.cpp | |||
@@ -0,0 +1,93 @@ | |||
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 | |||
22 | IdentitiesModel::IdentitiesModel(QObject *parent) : QIdentityProxyModel() | ||
23 | { | ||
24 | Sink::Query query; | ||
25 | query.liveQuery = true; | ||
26 | query.requestedProperties << "name" << "username" << "address" << "account"; | ||
27 | runQuery(query); | ||
28 | } | ||
29 | |||
30 | IdentitiesModel::~IdentitiesModel() | ||
31 | { | ||
32 | |||
33 | } | ||
34 | |||
35 | QHash< int, QByteArray > IdentitiesModel::roleNames() const | ||
36 | { | ||
37 | QHash<int, QByteArray> roles; | ||
38 | |||
39 | roles[Name] = "name"; | ||
40 | roles[Username] = "username"; | ||
41 | roles[Address] = "address"; | ||
42 | roles[IdentityId] = "identityId"; | ||
43 | roles[AccountId] = "accountId"; | ||
44 | roles[AccountName] = "accountName"; | ||
45 | roles[AccountIcon] = "accountIcon"; | ||
46 | roles[DisplayName] = "displayName"; | ||
47 | |||
48 | return roles; | ||
49 | } | ||
50 | |||
51 | QVariant IdentitiesModel::data(const QModelIndex &idx, int role) const | ||
52 | { | ||
53 | auto srcIdx = mapToSource(idx); | ||
54 | switch (role) { | ||
55 | case Name: | ||
56 | return srcIdx.sibling(srcIdx.row(), 0).data(Qt::DisplayRole).toString(); | ||
57 | case Username: | ||
58 | return srcIdx.sibling(srcIdx.row(), 1).data(Qt::DisplayRole).toString(); | ||
59 | case Address: | ||
60 | return srcIdx.sibling(srcIdx.row(), 2).data(Qt::DisplayRole).toString(); | ||
61 | case IdentityId: | ||
62 | return srcIdx.data(Sink::Store::DomainObjectBaseRole).value<Sink::ApplicationDomain::ApplicationDomainType::Ptr>()->identifier(); | ||
63 | case AccountId: | ||
64 | return srcIdx.data(Sink::Store::DomainObjectBaseRole).value<Sink::ApplicationDomain::ApplicationDomainType::Ptr>()->getProperty("account").toByteArray(); | ||
65 | case AccountName: { | ||
66 | const auto accountId = srcIdx.sibling(srcIdx.row(), 3).data(Qt::DisplayRole).toByteArray(); | ||
67 | return mAccountNames.value(accountId); | ||
68 | } | ||
69 | case AccountIcon: { | ||
70 | const auto accountId = srcIdx.sibling(srcIdx.row(), 3).data(Qt::DisplayRole).toByteArray(); | ||
71 | return mAccountIcons.value(accountId); | ||
72 | } | ||
73 | case DisplayName: { | ||
74 | return data(idx, AccountName).toString() + ": " + data(idx, Username).toString() + ", " + data(idx, Address).toString(); | ||
75 | } | ||
76 | } | ||
77 | return QIdentityProxyModel::data(idx, role); | ||
78 | } | ||
79 | |||
80 | void IdentitiesModel::runQuery(const Sink::Query &query) | ||
81 | { | ||
82 | mModel = Sink::Store::loadModel<Sink::ApplicationDomain::Identity>(query); | ||
83 | setSourceModel(mModel.data()); | ||
84 | |||
85 | Sink::Store::fetchAll<Sink::ApplicationDomain::SinkAccount>(Sink::Query()) | ||
86 | .then<void, QList<Sink::ApplicationDomain::SinkAccount::Ptr> >([this](const QList<Sink::ApplicationDomain::SinkAccount::Ptr> &accounts) { | ||
87 | for (const auto &account : accounts) { | ||
88 | mAccountNames.insert(account->identifier(), account->getProperty("name").toString()); | ||
89 | mAccountIcons.insert(account->identifier(), account->getProperty("icon").toString()); | ||
90 | } | ||
91 | emit layoutChanged(); | ||
92 | }).exec(); | ||
93 | } | ||