Ubuntu Download Manager  0.3.0
A session-wide downloading service
 All Classes Namespaces Files Functions Typedefs Enumerations Enumerator
manager_impl.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2013-2014 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 <glog/logging.h>
21 #include "download_impl.h"
22 #include "downloads_list.h"
23 #include "manager_impl.h"
24 
25 namespace {
26  const QString MANAGER_PATH = "/";
27 }
28 
29 namespace Ubuntu {
30 
31 namespace DownloadManager {
32 
33 ManagerImpl::ManagerImpl(const QDBusConnection& conn,
34  const QString& path,
35  QObject* parent)
36  : Manager(parent),
37  _conn(conn),
38  _servicePath(path) {
39  _dbusInterface = new ManagerInterface(path, MANAGER_PATH, conn);
40  init();
41 }
42 
43 // used for testing purposes
44 ManagerImpl::ManagerImpl(const QDBusConnection& conn,
45  const QString& path,
46  ManagerInterface* interface,
47  QObject* parent)
48  : Manager(parent),
49  _conn(conn),
50  _servicePath(path),
51  _dbusInterface(interface) {
52  init();
53 }
54 
55 ManagerImpl::~ManagerImpl() {
56  delete _lastError;
57  delete _dbusInterface;
58 }
59 
60 void
61 ManagerImpl::init() {
62  qRegisterMetaType<Download*>("Download*");
63  qRegisterMetaType<GroupDownload*>("GroupDownload*");
64  qRegisterMetaType<Error*>("Error*");
65  qRegisterMetaType<DBusError*>("DBusError*");
66  qRegisterMetaType<HttpError*>("HttpError*");
67  qRegisterMetaType<NetworkError*>("NetworkError*");
68  qRegisterMetaType<AuthError*>("AuthError*");
69  qRegisterMetaType<ProcessError*>("ProcessError*");
70  qRegisterMetaType<DownloadsList*>("DownloadsList*");
71  qDBusRegisterMetaType<StringMap>();
72  qDBusRegisterMetaType<DownloadStruct>();
73  qDBusRegisterMetaType<GroupDownloadStruct>();
74  qDBusRegisterMetaType<StructList>();
75  qDBusRegisterMetaType<AuthErrorStruct>();
76  qDBusRegisterMetaType<HttpErrorStruct>();
77  qDBusRegisterMetaType<NetworkErrorStruct>();
78  qDBusRegisterMetaType<ProcessErrorStruct>();
79 }
80 
81 Download*
82 ManagerImpl::getDownloadForId(const QString& id) {
83  auto down = new DownloadImpl(_conn, _servicePath, QDBusObjectPath(id));
84  return down;
85 }
86 
87 void
88 ManagerImpl::createDownload(DownloadStruct downStruct) {
89  DownloadCb cb = [](Download*) {};
90  createDownload(downStruct, cb, cb);
91 }
92 
93 void
94 ManagerImpl::createDownload(DownloadStruct downStruct,
95  DownloadCb cb,
96  DownloadCb errCb) {
97  QDBusPendingCall call =
98  _dbusInterface->createDownload(downStruct);
99  auto watcher = new DownloadManagerPCW(_conn,
100  _servicePath, call, cb, errCb, this);
101  CHECK(connect(watcher, &DownloadManagerPCW::callbackExecuted,
102  this, &ManagerImpl::onWatcherDone)) << "Could not connect to signal";
103 }
104 
105 void
106 ManagerImpl::createDownload(StructList downs,
107  const QString& algorithm,
108  bool allowed3G,
109  const QVariantMap& metadata,
110  StringMap headers) {
111  GroupCb cb = [](GroupDownload*) {};
112  createDownload(downs, algorithm, allowed3G, metadata, headers, cb, cb);
113 }
114 
115 void
116 ManagerImpl::createDownload(StructList downs,
117  const QString& algorithm,
118  bool allowed3G,
119  const QVariantMap& metadata,
120  StringMap headers,
121  GroupCb cb,
122  GroupCb errCb) {
123  QDBusPendingCall call =
124  _dbusInterface->createDownloadGroup(downs,
125  algorithm, allowed3G, metadata, headers);
126  auto watcher = new GroupManagerPCW(_conn, _servicePath,
127  call, cb, errCb, this);
128  CHECK(connect(watcher, &GroupManagerPCW::callbackExecuted,
129  this, &ManagerImpl::onWatcherDone)) << "Could not connect to signal";
130 }
131 
132 void
133 ManagerImpl::getAllDownloads() {
134  DownloadsListCb cb = [](DownloadsList*){};
135  getAllDownloads(cb, cb);
136 }
137 
138 void
139 ManagerImpl::getAllDownloads(DownloadsListCb cb, DownloadsListCb errCb) {
140  QDBusPendingCall call = _dbusInterface->getAllDownloads();
141  auto watcher = new DownloadsListManagerPCW(
142  _conn, _servicePath, call, cb, errCb, this);
143  CHECK(connect(watcher, &GroupManagerPCW::callbackExecuted,
144  this, &ManagerImpl::onWatcherDone)) << "Could not connect to signal";
145 }
146 
147 void
148 ManagerImpl::getAllDownloadsWithMetadata(const QString &name,
149  const QString &value) {
151  [](const QString&, const QString&, DownloadsList*){};
152  getAllDownloadsWithMetadata(name, value, cb, cb);
153 }
154 
155 void
156 ManagerImpl::getAllDownloadsWithMetadata(const QString &name,
157  const QString &value,
159  MetadataDownloadsListCb errCb) {
160  QDBusPendingCall call = _dbusInterface->getAllDownloadsWithMetadata(
161  name, value);
162  auto watcher = new MetadataDownloadsListManagerPCW(
163  _conn, _servicePath, call, name, value, cb, errCb, this);
164  CHECK(connect(watcher, &GroupManagerPCW::callbackExecuted,
165  this, &ManagerImpl::onWatcherDone)) << "Could not connect to signal";
166 }
167 
168 bool
169 ManagerImpl::isError() const {
170  return _isError;
171 }
172 
173 Error*
174 ManagerImpl::lastError() const {
175  return _lastError;
176 }
177 
178 void
179 ManagerImpl::setLastError(const QDBusError& err) {
180  // delete the last if error if present to keep mem to a minimum
181  if (_lastError != nullptr) {
182  delete _lastError;
183  }
184  _lastError = new DBusError(err);
185  _isError = true;
186 }
187 
188 void
189 ManagerImpl::allowMobileDataDownload(bool allowed) {
190  QDBusPendingReply<> reply =
191  _dbusInterface->allowGSMDownload(allowed);
192  // we block but because we expect it to be fast
193  reply.waitForFinished();
194  if (reply.isError()) {
195  auto err = reply.error();
196  qCritical() << "Error setting mobile data" << err;
197  setLastError(err);
198  }
199 }
200 
201 bool
202 ManagerImpl::isMobileDataDownload() {
203  QDBusPendingReply<bool> reply =
204  _dbusInterface->isGSMDownloadAllowed();
205  // we block but because we expect it to be fast
206  reply.waitForFinished();
207  if (reply.isError()) {
208  auto err = reply.error();
209  qCritical() << "Error getting if mobile data is enabled"
210  << err;
211  setLastError(err);
212  return false;
213  } else {
214  return reply.value();
215  }
216 }
217 
218 qulonglong
219 ManagerImpl::defaultThrottle() {
220  QDBusPendingReply<qulonglong> reply =
221  _dbusInterface->defaultThrottle();
222  // we block but because we expect it to be fast
223  reply.waitForFinished();
224  if (reply.isError()) {
225  auto err = reply.error();
226  qCritical() << "Error getting the default throttle" << err;
227  setLastError(err);
228  return 0;
229  } else {
230  return reply.value();
231  }
232 }
233 
234 void
235 ManagerImpl::setDefaultThrottle(qulonglong speed) {
236  QDBusPendingReply<> reply =
237  _dbusInterface->setDefaultThrottle(speed);
238  // we block but because we expect it to be fast
239  reply.waitForFinished();
240  if (reply.isError()) {
241  auto err = reply.error();
242  qCritical() << "Error setting default throttle" << err;
243  setLastError(err);
244  }
245 }
246 
247 void
248 ManagerImpl::exit() {
249  QDBusPendingReply<> reply =
250  _dbusInterface->exit();
251  // we block but because we expect it to be fast
252  reply.waitForFinished();
253  if (reply.isError()) {
254  auto err = reply.error();
255  qCritical() << "Error setting killing the daemon" << err;
256  setLastError(err);
257  }
258 }
259 
260 void
261 ManagerImpl::onWatcherDone() {
262  auto senderObj = sender();
263  senderObj->deleteLater();
264 }
265 
266 } // DownloadManager
267 
268 } // 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