Ubuntu Download Manager  0.3.0
A session-wide downloading service
 All Classes Namespaces Files Functions Typedefs Enumerations Enumerator
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 <QDebug>
20 #include <QDBusPendingReply>
21 #include <QDBusObjectPath>
22 #include <glog/logging.h>
23 #include "download_impl.h"
24 #include "downloads_list_impl.h"
25 #include "error.h"
26 #include "group_download.h"
27 #include "manager.h"
29 
30 namespace Ubuntu {
31 
32 namespace DownloadManager {
33 
34 DownloadManagerPCW::DownloadManagerPCW(const QDBusConnection& conn,
35  const QString& servicePath,
36  const QDBusPendingCall& call,
37  DownloadCb cb,
38  DownloadCb errCb,
39  QObject* parent)
40  : PendingCallWatcher(conn, servicePath, call, parent),
41  _cb(cb),
42  _errCb(errCb) {
43  CHECK(connect(this, &QDBusPendingCallWatcher::finished,
44  this, &DownloadManagerPCW::onFinished))
45  << "Could not connect to signal";
46 }
47 
48 void
49 DownloadManagerPCW::onFinished(QDBusPendingCallWatcher* watcher) {
50  QDBusPendingReply<QDBusObjectPath> reply = *watcher;
51  auto man = static_cast<Manager*>(parent());
52  if (reply.isError()) {
53  qDebug() << "ERROR" << reply.error() << reply.error().type();
54  // create error and deal with it
55  auto err = new DBusError(reply.error());
56  auto down = new DownloadImpl(_conn, err);
57  _errCb(down);
58  emit man->downloadCreated(down);
59  } else {
60  qDebug() << "Success!";
61  auto path = reply.value();
62  auto down = new DownloadImpl(_conn, _servicePath, path);
63  emit man->downloadCreated(down);
64  _cb(down);
65  }
66  emit callbackExecuted();
67  watcher->deleteLater();
68 }
69 
70 DownloadsListManagerPCW::DownloadsListManagerPCW(const QDBusConnection& conn,
71  const QString& servicePath,
72  const QDBusPendingCall& call,
73  DownloadsListCb cb,
74  DownloadsListCb errCb,
75  QObject* parent)
76  : PendingCallWatcher(conn, servicePath, call, parent),
77  _cb(cb),
78  _errCb(errCb) {
79  connect(this, &QDBusPendingCallWatcher::finished,
80  this, &DownloadsListManagerPCW::onFinished);
81 }
82 
83 void
84 DownloadsListManagerPCW::onFinished(QDBusPendingCallWatcher* watcher) {
85  QDBusPendingReply<QList<QDBusObjectPath> > reply = *watcher;
86  DownloadsListImpl* list;
87  auto man = static_cast<Manager*>(parent());
88  if (reply.isError()) {
89  qDebug() << "ERROR" << reply.error() << reply.error().type();
90  // create error and deal with it
91  auto err = new DBusError(reply.error());
92  list = new DownloadsListImpl(err);
93  _errCb(list);
94  emit man->downloadsFound(list);
95  } else {
96  qDebug() << "Success!";
97  auto paths = reply.value();
98  QList<QSharedPointer<Download> > downloads;
99  list = new DownloadsListImpl();
100  foreach(const QDBusObjectPath& path, paths) {
101  QSharedPointer<Download> down =
102  QSharedPointer<Download>(new DownloadImpl(_conn,
103  _servicePath, path));
104  downloads.append(down);
105  }
106  list = new DownloadsListImpl(downloads);
107  emit man->downloadsFound(list);
108  _cb(list);
109  }
110  emit callbackExecuted();
111  watcher->deleteLater();
112 }
113 
114 MetadataDownloadsListManagerPCW::MetadataDownloadsListManagerPCW(
115  const QDBusConnection& conn,
116  const QString& servicePath,
117  const QDBusPendingCall& call,
118  const QString& key,
119  const QString& value,
122  QObject* parent)
123  : PendingCallWatcher(conn, servicePath, call, parent),
124  _key(key),
125  _value(value),
126  _cb(cb),
127  _errCb(errCb) {
128  connect(this, &QDBusPendingCallWatcher::finished,
129  this, &MetadataDownloadsListManagerPCW::onFinished);
130 }
131 
132 void
133 MetadataDownloadsListManagerPCW::onFinished(QDBusPendingCallWatcher* watcher) {
134  QDBusPendingReply<QList<QDBusObjectPath> > reply = *watcher;
135  DownloadsListImpl* list;
136  auto man = static_cast<Manager*>(parent());
137  if (reply.isError()) {
138  qDebug() << "ERROR" << reply.error() << reply.error().type();
139  // create error and deal with it
140  auto err = new DBusError(reply.error());
141  list = new DownloadsListImpl(err);
142  _errCb(_key, _value, list);
143  emit man->downloadsWithMetadataFound(_key, _value, list);
144  } else {
145  qDebug() << "Success!";
146  auto paths = reply.value();
147  QList<QSharedPointer<Download> > downloads;
148  list = new DownloadsListImpl();
149  foreach(const QDBusObjectPath& path, paths) {
150  QSharedPointer<Download> down =
151  QSharedPointer<Download>(new DownloadImpl(
152  _conn, _servicePath, path));
153  downloads.append(down);
154  }
155  list = new DownloadsListImpl(downloads);
156  emit man->downloadsWithMetadataFound(_key, _value, list);
157  _cb(_key, _value, list);
158  }
159  emit callbackExecuted();
160  watcher->deleteLater();
161 }
162 
163 GroupManagerPCW::GroupManagerPCW(const QDBusConnection& conn,
164  const QString& servicePath,
165  const QDBusPendingCall& call,
166  GroupCb cb,
167  GroupCb errCb,
168  QObject* parent)
169  : PendingCallWatcher(conn, servicePath, call, parent),
170  _cb(cb),
171  _errCb(errCb) {
172  CHECK(connect(this, &QDBusPendingCallWatcher::finished,
173  this, &GroupManagerPCW::onFinished))
174  << "Could not connect to signal";
175 }
176 
177 void
178 GroupManagerPCW::onFinished(QDBusPendingCallWatcher* watcher) {
179  QDBusPendingReply<QDBusObjectPath> reply = *watcher;
180  auto man = static_cast<Manager*>(parent());
181  if (reply.isError()) {
182  // creater error and deal with it
183  auto err = new DBusError(reply.error());
184  auto down = new GroupDownload(err);
185  _errCb(down);
186  emit man->groupCreated(down);
187  } else {
188  QDBusObjectPath path = reply.value();
189  auto down = new GroupDownload(path);
190  emit man->groupCreated(down);
191  _cb(down);
192  }
193  emit callbackExecuted();
194  watcher->deleteLater();
195 }
196 
197 } // DownloadManager
198 
199 } // Ubuntu
std::function< void(Download *)> DownloadCb
Definition: manager.h:37
std::function< void(const QString &, const QString &, DownloadsList *)> MetadataDownloadsListCb
Definition: manager.h:55
std::function< void(GroupDownload *)> GroupCb
Definition: manager.h:61
std::function< void(DownloadsList *)> DownloadsListCb
Definition: manager.h:49