Merge lp:~ralsina/ubuntu-push-qml/async-rtm into lp:ubuntu-push-qml/rtm

Proposed by Roberto Alsina
Status: Merged
Approved by: Roberto Alsina
Approved revision: 13
Merged at revision: 12
Proposed branch: lp:~ralsina/ubuntu-push-qml/async-rtm
Merge into: lp:ubuntu-push-qml/rtm
Diff against target: 170 lines (+72/-28)
2 files modified
src/Ubuntu/PushNotifications/pushclient.cpp (+64/-28)
src/Ubuntu/PushNotifications/pushclient.h (+8/-0)
To merge this branch: bzr merge lp:~ralsina/ubuntu-push-qml/async-rtm
Reviewer Review Type Date Requested Status
Roberto Alsina (community) Approve
Review via email: mp+244591@code.launchpad.net

Commit message

Make registration async

Description of the change

Make registration async

To post a comment you must log in.
Revision history for this message
Roberto Alsina (ralsina) :
review: Approve
13. By Roberto Alsina

link bug

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/Ubuntu/PushNotifications/pushclient.cpp'
2--- src/Ubuntu/PushNotifications/pushclient.cpp 2014-10-23 19:15:41 +0000
3+++ src/Ubuntu/PushNotifications/pushclient.cpp 2014-12-12 14:29:37 +0000
4@@ -18,6 +18,8 @@
5 #include "pushclient.h"
6 #include <QtDBus/QDBusConnection>
7 #include <QtDBus/QDBusMessage>
8+#include <QtDBus/QDBusPendingCall>
9+#include <QtDBus/QDBusPendingReply>
10 #include <QTimer>
11
12 #define PUSH_SERVICE "com.ubuntu.PushNotifications"
13@@ -50,24 +52,31 @@
14 // Register to the push client
15 QDBusMessage message = QDBusMessage::createMethodCall(PUSH_SERVICE, register_path , PUSH_IFACE, "Register");
16 message << appId;
17- QDBusMessage token = bus.call(message);
18- if (token.type() == QDBusMessage::ErrorMessage) {
19- status = token.errorMessage();
20- emit statusChanged(status);
21- // This has to be delayed because the error signal is not connected yet
22- QTimer::singleShot(200, this, SLOT(emitError()));
23- return;
24- }
25- this->token = token.arguments()[0].toStringList()[0];
26+ QDBusPendingCall pcall = bus.asyncCall(message);
27+ QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this);
28+ QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
29+ this, SLOT(registerFinished(QDBusPendingCallWatcher*)));
30
31 // Connect to the notification signal
32 QString postal_path(POSTAL_PATH);
33 postal_path += "/" + pkgname;
34 bus.connect(POSTAL_SERVICE, postal_path, POSTAL_IFACE, "Post", "s", this, SLOT(notified(QString)));
35+}
36
37- // Do an initial fetch
38- QTimer::singleShot(200, this, SLOT(getNotifications()));
39- emit tokenChanged(this->token);
40+void PushClient::registerFinished(QDBusPendingCallWatcher *watcher) {
41+ QDBusPendingReply<QString> reply = *watcher;
42+ if (reply.isError()) {
43+ status = reply.error().message();
44+ emit statusChanged(status);
45+ // This has to be delayed because the error signal is not connected yet
46+ QTimer::singleShot(200, this, SLOT(emitError()));
47+ }
48+ else {
49+ this->token = reply.value();
50+ // Do an initial fetch
51+ QTimer::singleShot(200, this, SLOT(getNotifications()));
52+ emit tokenChanged(this->token);
53+ }
54 }
55
56 QString PushClient::getAppId() {
57@@ -90,20 +99,28 @@
58
59 void PushClient::getNotifications() {
60 QDBusConnection bus = QDBusConnection::sessionBus();
61-
62- // FIXME: make async using http://qt-project.org/doc/qt-4.8/qdbusconnection.html#asyncCall
63 QString path(POSTAL_PATH);
64 path += "/" + pkgname;
65 QDBusMessage message = QDBusMessage::createMethodCall(POSTAL_SERVICE, path, POSTAL_IFACE, "PopAll");
66 message << this->appId;
67- QDBusMessage reply = bus.call(message);
68- if (reply.type() == QDBusMessage::ErrorMessage) {
69- emit error(reply.errorMessage());
70- }
71- emit notificationsChanged(reply.arguments()[0].toStringList());
72+ QDBusPendingCall pcall = bus.asyncCall(message);
73+ QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this);
74+ QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
75+ this, SLOT(popAllFinished(QDBusPendingCallWatcher*)));
76+}
77+
78+void PushClient::popAllFinished(QDBusPendingCallWatcher *watcher) {
79+ QDBusPendingReply<QStringList> reply = *watcher;
80+ if (reply.isError()) {
81+ emit error(reply.error().message());
82+ }
83+ else {
84+ emit notificationsChanged(reply.value());
85+ }
86 }
87
88 QStringList PushClient::getPersistent() {
89+ // FIXME: this is blocking, but making it async would change the API
90 QDBusConnection bus = QDBusConnection::sessionBus();
91 QString path(POSTAL_PATH);
92 path += "/" + pkgname;
93@@ -125,11 +142,21 @@
94 for (int i = 0; i < tags.size(); ++i) {
95 message << tags.at(i);
96 }
97- QDBusMessage reply = bus.call(message);
98- if (reply.type() == QDBusMessage::ErrorMessage) {
99- emit error(reply.errorMessage());
100+ QDBusPendingCall pcall = bus.asyncCall(message);
101+ QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this);
102+ QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
103+ this, SLOT(clearPersistentFinished(QDBusPendingCallWatcher*)));
104+}
105+
106+void PushClient::clearPersistentFinished(QDBusPendingCallWatcher *watcher) {
107+ QDBusPendingReply<void> reply = *watcher;
108+
109+ if (reply.isError()) {
110+ emit error(reply.error().message());
111+ } else {
112+ // FIXME: this is blocking
113+ emit persistentChanged(getPersistent());
114 }
115- emit persistentChanged(getPersistent());
116 }
117
118 void PushClient::setCount(int count) {
119@@ -140,11 +167,20 @@
120 path += "/" + pkgname;
121 QDBusMessage message = QDBusMessage::createMethodCall(POSTAL_SERVICE, path, POSTAL_IFACE, "setCounter");
122 message << this->appId << count << visible;
123- QDBusMessage reply = bus.call(message);
124- if (reply.type() == QDBusMessage::ErrorMessage) {
125- emit error(reply.errorMessage());
126- }
127- emit countChanged(counter);
128+ QDBusPendingCall pcall = bus.asyncCall(message);
129+ QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this);
130+ QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
131+ this, SLOT(setCounterFinished(QDBusPendingCallWatcher*)));
132+}
133+
134+void PushClient::setCounterFinished(QDBusPendingCallWatcher *watcher) {
135+ QDBusPendingReply<void> reply = *watcher;
136+ if (reply.isError()) {
137+ emit error(reply.error().message());
138+ }
139+ else {
140+ emit countChanged(counter);
141+ }
142 }
143
144 int PushClient::getCount() {
145
146=== modified file 'src/Ubuntu/PushNotifications/pushclient.h'
147--- src/Ubuntu/PushNotifications/pushclient.h 2014-09-05 12:33:24 +0000
148+++ src/Ubuntu/PushNotifications/pushclient.h 2014-12-12 14:29:37 +0000
149@@ -22,6 +22,8 @@
150 #include <QString>
151 #include <QStringList>
152
153+class QDBusPendingCallWatcher;
154+
155 class PushClient : public QObject
156 {
157 Q_OBJECT
158@@ -57,6 +59,12 @@
159 void emitError();
160 void clearPersistent(QStringList tags);
161
162+private slots:
163+ void registerFinished(QDBusPendingCallWatcher *watcher);
164+ void popAllFinished(QDBusPendingCallWatcher *watcher);
165+ void setCounterFinished(QDBusPendingCallWatcher *watcher);
166+ void clearPersistentFinished(QDBusPendingCallWatcher *watcher);
167+
168 private:
169 QString appId;
170 QString pkgname;

Subscribers

People subscribed via source and target branches