Merge lp:~mardy/accounts-qml-module/applications into lp:accounts-qml-module

Proposed by Alberto Mardegan
Status: Merged
Approved by: Ken VanDine
Approved revision: 29
Merged at revision: 28
Proposed branch: lp:~mardy/accounts-qml-module/applications
Merge into: lp:accounts-qml-module
Diff against target: 505 lines (+410/-0)
9 files modified
src/application-model.cpp (+156/-0)
src/application-model.h (+76/-0)
src/application.cpp (+66/-0)
src/application.h (+43/-0)
src/plugin.cpp (+2/-0)
src/src.pro (+4/-0)
tests/data/mailer.application (+12/-0)
tests/tst_plugin.cpp (+50/-0)
tests/tst_plugin.pro (+1/-0)
To merge this branch: bzr merge lp:~mardy/accounts-qml-module/applications
Reviewer Review Type Date Requested Status
Ken VanDine Approve
PS Jenkins bot (community) continuous-integration Approve
Review via email: mp+169134@code.launchpad.net

Commit message

Add ApplicationModel

Description of the change

Add ApplicationModel

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Ken VanDine (ken-vandine) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'src/application-model.cpp'
--- src/application-model.cpp 1970-01-01 00:00:00 +0000
+++ src/application-model.cpp 2013-06-13 08:40:35 +0000
@@ -0,0 +1,156 @@
1/*
2 * Copyright (C) 2013 Canonical Ltd.
3 *
4 * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; version 2.1.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "debug.h"
20#include "application-model.h"
21#include "application.h"
22
23#include <Accounts/Manager>
24
25using namespace OnlineAccounts;
26
27/*!
28 * \qmltype ApplicationModel
29 * \inqmlmodule Ubuntu.OnlineAccounts 0.1
30 * \ingroup Ubuntu
31 *
32 * \brief A model of the applications using online accounts.
33 *
34 * The ApplicationModel is a model representing the applications using online
35 * accounts installed on the system.
36 *
37 * In the current implementation, the model is valid only if its \l
38 * ApplicationModel::service property is set to a valid service ID.
39 *
40 * The model defines the following roles:
41 * \list
42 * \li \c applicationId is the unique identifier of the application
43 * \li \c displayName is the application display name
44 * \li \c iconName is the name of the application icon
45 * \li \c serviceUsage is a description of how the application uses the
46 * service; this is set to a valid value only if the \l
47 * ApplicationModel::service property is set to a valid service ID.
48 * \li \c application is the Application object
49 * \endlist
50 */
51
52ApplicationModel::ApplicationModel(QObject *parent):
53 QAbstractListModel(parent),
54 manager(SharedManager::instance())
55{
56}
57
58ApplicationModel::~ApplicationModel()
59{
60}
61
62/*
63 * \qmlproperty int ApplicationModel::count
64 * The number of items in the model.
65 */
66int ApplicationModel::rowCount(const QModelIndex &parent) const
67{
68 Q_UNUSED(parent);
69 return applications.count();
70}
71
72/*!
73 * \qmlproperty string ApplicationModel::service
74 * If set, the model will list only those accounts services for this
75 * specific service.
76 */
77void ApplicationModel::setService(const QString &serviceId)
78{
79 if (serviceId == m_service.name()) return;
80 m_service = manager->service(serviceId);
81
82 beginResetModel();
83 qDeleteAll(applications);
84 applications.clear();
85
86 computeApplicationList();
87 endResetModel();
88 Q_EMIT serviceChanged();
89}
90
91QString ApplicationModel::service() const
92{
93 return m_service.name();
94}
95
96/*
97 * \qmlmethod variant ApplicationModel::get(int row, string roleName)
98 *
99 * Returns the data at \a row for the role \a roleName.
100 */
101QVariant ApplicationModel::get(int row, const QString &roleName) const
102{
103 int role = roleNames().key(roleName.toLatin1(), -1);
104 return data(index(row), role);
105}
106
107QVariant ApplicationModel::data(const QModelIndex &index, int role) const
108{
109 if (index.row() < 0 || index.row() >= applications.count())
110 return QVariant();
111
112 Application *application = applications.at(index.row());
113 QVariant ret;
114
115 switch (role) {
116 case Qt::DisplayRole:
117 case ApplicationIdRole:
118 ret = application->name();
119 break;
120 case DisplayNameRole:
121 case IconNameRole:
122 // FIXME: implement when libaccounts-qt 1.8 is released
123 break;
124 case ServiceUsageRole:
125 ret = application->serviceUsage(m_service);
126 break;
127 case ApplicationRole:
128 ret = QVariant::fromValue<QObject*>(application);
129 break;
130 }
131
132 return ret;
133}
134
135QHash<int, QByteArray> ApplicationModel::roleNames() const
136{
137 static QHash<int, QByteArray> roles;
138 if (roles.isEmpty()) {
139 roles[ApplicationIdRole] = "applicationId";
140 roles[DisplayNameRole] = "displayName";
141 roles[IconNameRole] = "iconName";
142 roles[ServiceUsageRole] = "serviceUsage";
143 roles[ApplicationRole] = "application";
144 }
145 return roles;
146}
147
148void ApplicationModel::computeApplicationList()
149{
150 if (!m_service.isValid()) return;
151
152 Q_FOREACH(const Accounts::Application &app,
153 manager->applicationList(m_service)) {
154 applications.append(new Application(app, this));
155 }
156}
0157
=== added file 'src/application-model.h'
--- src/application-model.h 1970-01-01 00:00:00 +0000
+++ src/application-model.h 2013-06-13 08:40:35 +0000
@@ -0,0 +1,76 @@
1/*
2 * Copyright (C) 2013 Canonical Ltd.
3 *
4 * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; version 2.1.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef ONLINE_ACCOUNTS_APPLICATION_MODEL_H
20#define ONLINE_ACCOUNTS_APPLICATION_MODEL_H
21
22#include "manager.h"
23#include <Accounts/Service>
24#include <QAbstractListModel>
25#include <QList>
26#include <QSharedPointer>
27
28namespace OnlineAccounts {
29
30class Application;
31
32class ApplicationModel: public QAbstractListModel
33{
34 Q_OBJECT
35 Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
36 Q_PROPERTY(QString service READ service WRITE setService \
37 NOTIFY serviceChanged)
38
39public:
40 ApplicationModel(QObject *parent = 0);
41 ~ApplicationModel();
42
43 enum Roles {
44 ApplicationIdRole = Qt::UserRole + 1,
45 DisplayNameRole,
46 IconNameRole,
47 ServiceUsageRole,
48 ApplicationRole,
49 };
50
51 void setService(const QString &serviceId);
52 QString service() const;
53
54 Q_INVOKABLE QVariant get(int row, const QString &roleName) const;
55
56 // reimplemented virtual methods
57 int rowCount(const QModelIndex &parent = QModelIndex()) const;
58 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
59 QHash<int, QByteArray> roleNames() const;
60
61Q_SIGNALS:
62 void countChanged();
63 void serviceChanged();
64
65private:
66 void computeApplicationList();
67
68private:
69 QSharedPointer<Accounts::Manager> manager;
70 QList<Application*> applications;
71 Accounts::Service m_service;
72};
73
74}; // namespace
75
76#endif // ONLINE_ACCOUNTS_APPLICATION_MODEL_H
077
=== added file 'src/application.cpp'
--- src/application.cpp 1970-01-01 00:00:00 +0000
+++ src/application.cpp 2013-06-13 08:40:35 +0000
@@ -0,0 +1,66 @@
1/*
2 * Copyright (C) 2013 Canonical Ltd.
3 *
4 * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; version 2.1.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20#include "application.h"
21#include "debug.h"
22
23using namespace OnlineAccounts;
24
25/*!
26 * \qmltype Account
27 * \inqmlmodule Ubuntu.OnlineAccounts 0.1
28 * \ingroup Ubuntu
29 *
30 * \brief Represents an instance of an online account.
31 *
32 * The Account element represents an online account. Currently, instances of
33 * this object cannot be created directly, but are instantiated by the \l
34 * ApplicationModel element.
35 */
36Application::Application(const Accounts::Application &application,
37 QObject *parent):
38 QObject(parent),
39 Accounts::Application(application)
40{
41}
42
43Application::~Application()
44{
45}
46
47/*!
48 * \qmlproperty string Application::applicationId
49 * Unique identifier for this application.
50 */
51
52/*!
53 * \qmlproperty string Application::description
54 * Description of the application.
55 */
56
57/*!
58 * \qmlmethod string Application::serviceUsage(Service service)
59 *
60 * Returns a textual description of how the application can make use of \a
61 * service.
62 */
63QString Application::serviceUsage(const Accounts::Service &service)
64{
65 return Accounts::Application::serviceUsage(service);
66}
067
=== added file 'src/application.h'
--- src/application.h 1970-01-01 00:00:00 +0000
+++ src/application.h 2013-06-13 08:40:35 +0000
@@ -0,0 +1,43 @@
1/*
2 * Copyright (C) 2013 Canonical Ltd.
3 *
4 * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; version 2.1.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef ONLINE_ACCOUNTS_APPLICATION_H
20#define ONLINE_ACCOUNTS_APPLICATION_H
21
22#include <QObject>
23
24#include <Accounts/Application>
25
26namespace OnlineAccounts {
27
28class Application: public QObject, public Accounts::Application
29{
30 Q_OBJECT
31 Q_PROPERTY(QString applicationId READ name CONSTANT)
32 Q_PROPERTY(QString description READ description CONSTANT)
33
34public:
35 Application(const Accounts::Application &application, QObject *parent = 0);
36 ~Application();
37
38 Q_INVOKABLE QString serviceUsage(const Accounts::Service &service);
39};
40
41}; // namespace
42
43#endif // ONLINE_ACCOUNTS_APPLICATION_H
044
=== modified file 'src/plugin.cpp'
--- src/plugin.cpp 2013-05-31 06:46:13 +0000
+++ src/plugin.cpp 2013-06-13 08:40:35 +0000
@@ -19,6 +19,7 @@
19#include "account-service-model.h"19#include "account-service-model.h"
20#include "account-service.h"20#include "account-service.h"
21#include "account.h"21#include "account.h"
22#include "application-model.h"
22#include "credentials.h"23#include "credentials.h"
23#include "manager.h"24#include "manager.h"
24#include "plugin.h"25#include "plugin.h"
@@ -43,6 +44,7 @@
43 qmlRegisterType<AccountServiceModel>(uri, 0, 1, "AccountServiceModel");44 qmlRegisterType<AccountServiceModel>(uri, 0, 1, "AccountServiceModel");
44 qmlRegisterType<AccountService>(uri, 0, 1, "AccountService");45 qmlRegisterType<AccountService>(uri, 0, 1, "AccountService");
45 qmlRegisterType<Account>(uri, 0, 1, "Account");46 qmlRegisterType<Account>(uri, 0, 1, "Account");
47 qmlRegisterType<ApplicationModel>(uri, 0, 1, "ApplicationModel");
46 qmlRegisterType<Credentials>(uri, 0, 1, "Credentials");48 qmlRegisterType<Credentials>(uri, 0, 1, "Credentials");
47 qmlRegisterType<ProviderModel>(uri, 0, 1, "ProviderModel");49 qmlRegisterType<ProviderModel>(uri, 0, 1, "ProviderModel");
48 qmlRegisterSingletonType<Manager>(uri, 0, 1, "Manager", createManager);50 qmlRegisterSingletonType<Manager>(uri, 0, 1, "Manager", createManager);
4951
=== modified file 'src/src.pro'
--- src/src.pro 2013-06-03 10:30:40 +0000
+++ src/src.pro 2013-06-13 08:40:35 +0000
@@ -30,6 +30,8 @@
30 account-service-model.cpp \30 account-service-model.cpp \
31 account-service.cpp \31 account-service.cpp \
32 account.cpp \32 account.cpp \
33 application-model.cpp \
34 application.cpp \
33 credentials.cpp \35 credentials.cpp \
34 manager.cpp \36 manager.cpp \
35 plugin.cpp \37 plugin.cpp \
@@ -39,6 +41,8 @@
39 account-service-model.h \41 account-service-model.h \
40 account-service.h \42 account-service.h \
41 account.h \43 account.h \
44 application-model.h \
45 application.h \
42 credentials.h \46 credentials.h \
43 debug.h \47 debug.h \
44 manager.h \48 manager.h \
4549
=== added file 'tests/data/mailer.application'
--- tests/data/mailer.application 1970-01-01 00:00:00 +0000
+++ tests/data/mailer.application 2013-06-13 08:40:35 +0000
@@ -0,0 +1,12 @@
1<?xml version="1.0" encoding="UTF-8" ?>
2<application id="mailer">
3 <description>Mailer application</description>
4 <translations>mailer-catalog</translations>
5
6 <service-types>
7 <service-type id="e-mail">
8 <description>Mailer can retrieve your e-mails</description>
9 </service-type>
10 </service-types>
11
12</application>
013
=== modified file 'tests/tst_plugin.cpp'
--- tests/tst_plugin.cpp 2013-06-03 10:30:40 +0000
+++ tests/tst_plugin.cpp 2013-06-13 08:40:35 +0000
@@ -54,6 +54,7 @@
54 void testAccountCredentialsRemoval_data();54 void testAccountCredentialsRemoval_data();
55 void testAccountCredentialsRemoval();55 void testAccountCredentialsRemoval();
56 void testAccountServiceCredentials();56 void testAccountServiceCredentials();
57 void testApplicationModel();
5758
58private:59private:
59 void clearDb();60 void clearDb();
@@ -84,6 +85,7 @@
84{85{
85 qputenv("QML2_IMPORT_PATH", "../src");86 qputenv("QML2_IMPORT_PATH", "../src");
86 qputenv("ACCOUNTS", "/tmp/");87 qputenv("ACCOUNTS", "/tmp/");
88 qputenv("AG_APPLICATIONS", APPLICATIONS_DIR);
87 qputenv("AG_SERVICES", SERVICES_DIR);89 qputenv("AG_SERVICES", SERVICES_DIR);
88 qputenv("AG_SERVICE_TYPES", SERVICE_TYPES_DIR);90 qputenv("AG_SERVICE_TYPES", SERVICE_TYPES_DIR);
89 qputenv("AG_PROVIDERS", PROVIDERS_DIR);91 qputenv("AG_PROVIDERS", PROVIDERS_DIR);
@@ -1278,5 +1280,53 @@
1278 delete accountService;1280 delete accountService;
1279}1281}
12801282
1283void PluginTest::testApplicationModel()
1284{
1285 QQmlEngine engine;
1286 QQmlComponent component(&engine);
1287 component.setData("import Ubuntu.OnlineAccounts 0.1\n"
1288 "ApplicationModel {}",
1289 QUrl());
1290 QObject *qmlModel = component.create();
1291 QVERIFY(qmlModel != 0);
1292 QAbstractListModel *model = qobject_cast<QAbstractListModel*>(qmlModel);
1293 QVERIFY(model != 0);
1294
1295 QCOMPARE(model->rowCount(), 0);
1296 /* Retrieve a couple of invalid indexes */
1297 QVERIFY(!get(model, 0, "applicationId").isValid());
1298 QVERIFY(!get(model, -1, "applicationId").isValid());
1299
1300 /* Set a valid service on the model */
1301 qmlModel->setProperty("service", QString("badmail"));
1302 QCOMPARE(model->property("service").toString(), QString("badmail"));
1303
1304 QCOMPARE(model->rowCount(), 1);
1305 QCOMPARE(model->property("count").toInt(), 1);
1306
1307 QCOMPARE(get(model, 0, "applicationId").toString(), QString("mailer"));
1308 QCOMPARE(get(model, 0, "serviceUsage").toString(),
1309 QString("Mailer can retrieve your e-mails"));
1310
1311
1312 QVariant value;
1313 QVERIFY(QMetaObject::invokeMethod(model, "get",
1314 Q_RETURN_ARG(QVariant, value),
1315 Q_ARG(int, 0),
1316 Q_ARG(QString, "applicationId")));
1317 QCOMPARE(value.toString(), QString("mailer"));
1318
1319 /* Get an Application from the model */
1320 QObject *application = get(model, 0, "application").value<QObject*>();
1321 QCOMPARE(application->metaObject()->className(),
1322 "OnlineAccounts::Application");
1323
1324 /* Reset the model to an invalid service */
1325 qmlModel->setProperty("service", QString());
1326 QCOMPARE(model->rowCount(), 0);
1327
1328 delete qmlModel;
1329}
1330
1281QTEST_MAIN(PluginTest);1331QTEST_MAIN(PluginTest);
1282#include "tst_plugin.moc"1332#include "tst_plugin.moc"
12831333
=== modified file 'tests/tst_plugin.pro'
--- tests/tst_plugin.pro 2013-04-10 11:42:03 +0000
+++ tests/tst_plugin.pro 2013-06-13 08:40:35 +0000
@@ -25,6 +25,7 @@
25DATA_PATH = $${TOP_SRC_DIR}/tests/data/25DATA_PATH = $${TOP_SRC_DIR}/tests/data/
2626
27DEFINES += \27DEFINES += \
28 APPLICATIONS_DIR=\\\"$$DATA_PATH\\\" \
28 SERVICES_DIR=\\\"$$DATA_PATH\\\" \29 SERVICES_DIR=\\\"$$DATA_PATH\\\" \
29 SERVICE_TYPES_DIR=\\\"$$DATA_PATH\\\" \30 SERVICE_TYPES_DIR=\\\"$$DATA_PATH\\\" \
30 PROVIDERS_DIR=\\\"$$DATA_PATH\\\"31 PROVIDERS_DIR=\\\"$$DATA_PATH\\\"

Subscribers

People subscribed via source and target branches