Debian Codegraph @

online-accounts-ui/src/access-model.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2013 Canonical Ltd.
3 *
4 * Contact: Alberto Mardegan <[email protected]>
5 *
6 * This file is part of lomiri-online-accounts-ui
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 3, as published
10 * by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranties of
14 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "access-model.h"
22 #include <online-accounts-common/debug.h>
23
24 #include <Accounts/Account>
25 #include <Accounts/Application>
26 #include <Accounts/Service>
27 #include <LomiriOnlineAccountsPlugin/account-manager.h>
28
29 using namespace OnlineAccountsUi;
30
31 namespace OnlineAccountsUi {
32
33 class AccessModelPrivate
34 {
35 Q_DECLARE_PUBLIC(AccessModel)
36
37 public:
38 inline AccessModelPrivate(AccessModel *accessModel);
39 inline ~AccessModelPrivate();
40
41 void ensureSupportedServices() const;
42
43 private:
44 mutable AccessModel *q_ptr;
45 mutable Accounts::ServiceList m_supportedServices;
46 QString m_lastItemText;
47 QString m_applicationId;
48 };
49
50 } // namespace
51
52 AccessModelPrivate::AccessModelPrivate(AccessModel *accessModel):
53 q_ptr(accessModel)
54 {
55 }
56
57 AccessModelPrivate::~AccessModelPrivate()
58 {
59 }
60
61 void AccessModelPrivate::ensureSupportedServices() const
62 {
63 Q_Q(const AccessModel);
64
65 if (!m_supportedServices.isEmpty()) return; // Nothing to do
66
67 /* List all services supported by the accounts's provider. We know that the
68 * account model is an instance of the accounts-qml-module's
69 * AccountServiceModel, and that its "provider" property must be set. */
70 QAbstractItemModel *accountModel = q->sourceModel();
71 if (!accountModel) return;
72
73 QString providerId = accountModel->property("provider").toString();
74 if (providerId.isEmpty()) return;
75
76 AccountManager *manager = AccountManager::instance();
77 Accounts::Application application = manager->application(m_applicationId);
78 Accounts::ServiceList allServices = manager->serviceList();
79 Q_FOREACH(const Accounts::Service &service, allServices) {
80 if (service.provider() == providerId &&
81 !application.serviceUsage(service).isEmpty()) {
82 m_supportedServices.append(service);
83 }
84 }
85 }
86
87 AccessModel::AccessModel(QObject *parent):
88 QSortFilterProxyModel(parent),
89 d_ptr(new AccessModelPrivate(this))
90 {
91 QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex&,int,int)),
92 this, SIGNAL(countChanged()));
93 QObject::connect(this, SIGNAL(rowsRemoved(const QModelIndex&,int,int)),
94 this, SIGNAL(countChanged()));
95 setDynamicSortFilter(true);
96 }
97
98 AccessModel::~AccessModel()
99 {
100 }
101
102 void AccessModel::setAccountModel(QAbstractItemModel *accountModel)
103 {
104 setSourceModel(accountModel);
105 Q_EMIT accountModelChanged();
106 }
107
108 QAbstractItemModel *AccessModel::accountModel() const
109 {
110 return sourceModel();
111 }
112
113 void AccessModel::setApplicationId(const QString &applicationId)
114 {
115 Q_D(AccessModel);
116
117 if (applicationId == d->m_applicationId) return;
118 d->m_applicationId = applicationId;
119 Q_EMIT applicationIdChanged();
120
121 d->m_supportedServices.clear();
122 /* Trigger a refresh of the filtered model */
123 invalidateFilter();
124 }
125
126 QString AccessModel::applicationId() const
127 {
128 Q_D(const AccessModel);
129 return d->m_applicationId;
130 }
131
132 QVariant AccessModel::get(int row, const QString &roleName) const
133 {
134 int role = roleNames().key(roleName.toLatin1(), -1);
135 return data(index(row, 0), role);
136 }
137
138 bool AccessModel::filterAcceptsRow(int sourceRow,
139 const QModelIndex &sourceParent) const
140 {
141 Q_D(const AccessModel);
142
143 Q_UNUSED(sourceParent);
144
145 if (d->m_applicationId.isEmpty()) return true;
146
147 /* We must avoid showing those accounts which have already been enabled for
148 * this application. */
149 d->ensureSupportedServices();
150 QVariant result;
151 bool ok = QMetaObject::invokeMethod(sourceModel(), "get",
152 Qt::DirectConnection,
153 Q_RETURN_ARG(QVariant, result),
154 Q_ARG(int, sourceRow),
155 Q_ARG(QString, "accountHandle"));
156 if (Q_UNLIKELY(!ok)) return false;
157
158 QObject *accountHandle = result.value<QObject*>();
159 Accounts::Account *account =
160 qobject_cast<Accounts::Account*>(accountHandle);
161 if (Q_UNLIKELY(!account)) return false;
162
163 bool allServicesEnabled = true;
164 Q_FOREACH(const Accounts::Service &service, d->m_supportedServices) {
165 account->selectService(service);
166 if (!account->isEnabled()) {
167 allServicesEnabled = false;
168 break;
169 }
170 }
171
172 DEBUG() << account->id() << "allServicesEnabled" << allServicesEnabled;
173 return !allServicesEnabled;
174 }