diff options
Diffstat (limited to 'framework/accounts/accountsmodel.cpp')
-rw-r--r-- | framework/accounts/accountsmodel.cpp | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/framework/accounts/accountsmodel.cpp b/framework/accounts/accountsmodel.cpp new file mode 100644 index 00000000..ea37784b --- /dev/null +++ b/framework/accounts/accountsmodel.cpp | |||
@@ -0,0 +1,82 @@ | |||
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 "accountsmodel.h" | ||
20 | #include <sink/store.h> | ||
21 | |||
22 | AccountsModel::AccountsModel(QObject *parent) : QIdentityProxyModel() | ||
23 | { | ||
24 | Sink::Query query; | ||
25 | query.setFlags(Sink::Query::LiveQuery); | ||
26 | query.request<Sink::ApplicationDomain::SinkAccount::Name>(); | ||
27 | query.request<Sink::ApplicationDomain::SinkAccount::Icon>(); | ||
28 | query.request<Sink::ApplicationDomain::SinkAccount::Status>(); | ||
29 | runQuery(query); | ||
30 | } | ||
31 | |||
32 | AccountsModel::~AccountsModel() | ||
33 | { | ||
34 | |||
35 | } | ||
36 | |||
37 | QHash< int, QByteArray > AccountsModel::roleNames() const | ||
38 | { | ||
39 | QHash<int, QByteArray> roles; | ||
40 | |||
41 | roles[Name] = "name"; | ||
42 | roles[Icon] = "icon"; | ||
43 | roles[AccountId] = "accountId"; | ||
44 | roles[Status] = "status"; | ||
45 | roles[StatusIcon] = "statusIcon"; | ||
46 | roles[ShowStatus] = "showStatus"; | ||
47 | |||
48 | return roles; | ||
49 | } | ||
50 | |||
51 | QVariant AccountsModel::data(const QModelIndex &idx, int role) const | ||
52 | { | ||
53 | auto srcIdx = mapToSource(idx); | ||
54 | auto account = srcIdx.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::SinkAccount::Ptr>(); | ||
55 | switch (role) { | ||
56 | case Name: | ||
57 | return account->getName(); | ||
58 | case Icon: | ||
59 | return account->getIcon(); | ||
60 | case AccountId: | ||
61 | return account->identifier(); | ||
62 | case Status: | ||
63 | return account->getStatus(); | ||
64 | case StatusIcon: | ||
65 | if (account->getStatus() == Sink::ApplicationDomain::ErrorStatus) { | ||
66 | return "emblem-error"; | ||
67 | } else if (account->getStatus() == Sink::ApplicationDomain::BusyStatus) { | ||
68 | return "view-refresh"; | ||
69 | } else if (account->getStatus() == Sink::ApplicationDomain::ConnectedStatus) { | ||
70 | return "checkmark"; | ||
71 | } | ||
72 | case ShowStatus: | ||
73 | return (account->getStatus() != Sink::ApplicationDomain::OfflineStatus); | ||
74 | } | ||
75 | return QIdentityProxyModel::data(idx, role); | ||
76 | } | ||
77 | |||
78 | void AccountsModel::runQuery(const Sink::Query &query) | ||
79 | { | ||
80 | mModel = Sink::Store::loadModel<Sink::ApplicationDomain::SinkAccount>(query); | ||
81 | setSourceModel(mModel.data()); | ||
82 | } | ||