Ubuntu Download Manager  1.2.0
A session-wide downloading service
manager_pendingcall_watcher.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2013 Canonical Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of version 3 of the GNU Lesser General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the
15  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  * Boston, MA 02110-1301, USA.
17  */
18 
19 #include <QDBusPendingReply>
20 #include <QDBusObjectPath>
21 
22 #include <boost/log/sources/record_ostream.hpp>
23 #include <boost/log/sources/severity_feature.hpp>
24 
31 
33 
34 namespace Ubuntu {
35 
36 namespace DownloadManager {
37 
38 using namespace Logging;
39 
40 DownloadManagerPCW::DownloadManagerPCW(const QDBusConnection& conn,
41  const QString& servicePath,
42  const QDBusPendingCall& call,
43  DownloadCb cb,
44  DownloadCb errCb,
45  QObject* parent)
46  : PendingCallWatcher(conn, servicePath, call, parent),
47  _cb(cb),
48  _errCb(errCb) {
49  auto connected = connect(this, &QDBusPendingCallWatcher::finished,
50  this, &DownloadManagerPCW::onFinished);
51  if (!connected) {
52  Logger::log(Logger::Critical,
53  "Could not connect to signal &QDBusPendingCallWatcher::finished");
54  }
55 }
56 
57 void
58 DownloadManagerPCW::onFinished(QDBusPendingCallWatcher* watcher) {
59  QDBusPendingReply<QDBusObjectPath> reply = *watcher;
60  auto man = static_cast<Manager*>(parent());
61  if (reply.isError()) {
62  auto dbusErr = reply.error();
63  Logger::log(Logger::Error,
64  QString("%1 %2").arg(dbusErr.name()).arg(dbusErr.message()));
65  auto err = new DBusError(reply.error());
66  auto down = new DownloadImpl(_conn, err);
67  _errCb(down);
68  emit man->downloadCreated(down);
69  } else {
70  auto path = reply.value();
71  auto down = new DownloadImpl(_conn, _servicePath, path);
72  emit man->downloadCreated(down);
73  _cb(down);
74  }
75  emit callbackExecuted();
76  watcher->deleteLater();
77 }
78 
79 DownloadsListManagerPCW::DownloadsListManagerPCW(const QDBusConnection& conn,
80  const QString& servicePath,
81  const QDBusPendingCall& call,
82  DownloadsListCb cb,
83  DownloadsListCb errCb,
84  QObject* parent)
85  : PendingCallWatcher(conn, servicePath, call, parent),
86  _cb(cb),
87  _errCb(errCb) {
88  auto connected = connect(this, &QDBusPendingCallWatcher::finished,
89  this, &DownloadsListManagerPCW::onFinished);
90  if (!connected) {
91  Logger::log(Logger::Critical,
92  "Could not connect to signal &QDBusPendingCallWatcher::finished");
93  }
94 }
95 
96 void
97 DownloadsListManagerPCW::onFinished(QDBusPendingCallWatcher* watcher) {
98  QDBusPendingReply<QList<QDBusObjectPath> > reply = *watcher;
99  DownloadsListImpl* list;
100  auto man = static_cast<Manager*>(parent());
101  if (reply.isError()) {
102  auto dbusErr = reply.error();
103  Logger::log(Logger::Error,
104  QString("%1 %2").arg(dbusErr.name()).arg(dbusErr.message()));
105  auto err = new DBusError(reply.error());
106  list = new DownloadsListImpl(err);
107  _errCb(list);
108  emit man->downloadsFound(list);
109  } else {
110  auto paths = reply.value();
111  QList<QSharedPointer<Download> > downloads;
112  list = new DownloadsListImpl();
113  foreach(const QDBusObjectPath& path, paths) {
114  QSharedPointer<Download> down =
115  QSharedPointer<Download>(new DownloadImpl(_conn,
116  _servicePath, path));
117  downloads.append(down);
118  }
119  list = new DownloadsListImpl(downloads);
120  emit man->downloadsFound(list);
121  _cb(list);
122  }
123  emit callbackExecuted();
124  watcher->deleteLater();
125 }
126 
127 MetadataDownloadsListManagerPCW::MetadataDownloadsListManagerPCW(
128  const QDBusConnection& conn,
129  const QString& servicePath,
130  const QDBusPendingCall& call,
131  const QString& key,
132  const QString& value,
135  QObject* parent)
136  : PendingCallWatcher(conn, servicePath, call, parent),
137  _key(key),
138  _value(value),
139  _cb(cb),
140  _errCb(errCb) {
141  auto connected = connect(this, &QDBusPendingCallWatcher::finished,
142  this, &MetadataDownloadsListManagerPCW::onFinished);
143  if (!connected) {
144  Logger::log(Logger::Critical,
145  "Could not connect to signal &QDBusPendingCallWatcher::finished");
146  }
147 }
148 
149 void
150 MetadataDownloadsListManagerPCW::onFinished(QDBusPendingCallWatcher* watcher) {
151  QDBusPendingReply<QList<QDBusObjectPath> > reply = *watcher;
152  DownloadsListImpl* list;
153  auto man = static_cast<Manager*>(parent());
154  if (reply.isError()) {
155  auto dbusErr = reply.error();
156  Logger::log(Logger::Error,
157  QString("%1 %2").arg(dbusErr.name()).arg(dbusErr.message()));
158  auto err = new DBusError(reply.error());
159  list = new DownloadsListImpl(err);
160  _errCb(_key, _value, list);
161  emit man->downloadsWithMetadataFound(_key, _value, list);
162  } else {
163  auto paths = reply.value();
164  QList<QSharedPointer<Download> > downloads;
165  list = new DownloadsListImpl();
166  foreach(const QDBusObjectPath& path, paths) {
167  QSharedPointer<Download> down =
168  QSharedPointer<Download>(new DownloadImpl(
169  _conn, _servicePath, path));
170  downloads.append(down);
171  }
172  list = new DownloadsListImpl(downloads);
173  emit man->downloadsWithMetadataFound(_key, _value, list);
174  _cb(_key, _value, list);
175  }
176  emit callbackExecuted();
177  watcher->deleteLater();
178 }
179 
180 GroupManagerPCW::GroupManagerPCW(const QDBusConnection& conn,
181  const QString& servicePath,
182  const QDBusPendingCall& call,
183  GroupCb cb,
184  GroupCb errCb,
185  QObject* parent)
186  : PendingCallWatcher(conn, servicePath, call, parent),
187  _cb(cb),
188  _errCb(errCb) {
189  auto connected = connect(this, &QDBusPendingCallWatcher::finished,
190  this, &GroupManagerPCW::onFinished);
191  if (!connected) {
192  Logger::log(Logger::Critical,
193  "Could not connect to signal &DownloadPCW::finished");
194  }
195 }
196 
197 void
198 GroupManagerPCW::onFinished(QDBusPendingCallWatcher* watcher) {
199  QDBusPendingReply<QDBusObjectPath> reply = *watcher;
200  auto man = static_cast<Manager*>(parent());
201  if (reply.isError()) {
202  auto dbusErr = reply.error();
203  Logger::log(Logger::Error,
204  QString("%1 %2").arg(dbusErr.name()).arg(dbusErr.message()));
205  auto err = new DBusError(reply.error());
206  auto down = new GroupDownload(err);
207  _errCb(down);
208  emit man->groupCreated(down);
209  } else {
210  QDBusObjectPath path = reply.value();
211  auto down = new GroupDownload(path);
212  emit man->groupCreated(down);
213  _cb(down);
214  }
215  emit callbackExecuted();
216  watcher->deleteLater();
217 }
218 
219 } // DownloadManager
220 
221 } // Ubuntu
std::function< void(Download *)> DownloadCb
Definition: manager.h:36
std::function< void(const QString &, const QString &, DownloadsList *)> MetadataDownloadsListCb
Definition: manager.h:54
std::function< void(GroupDownload *)> GroupCb
Definition: manager.h:60
std::function< void(DownloadsList *)> DownloadsListCb
Definition: manager.h:48