Ubuntu Download Manager  0.3.0
A session-wide downloading service
 All Classes Namespaces Files Functions Typedefs Enumerations Enumerator
download_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 <glog/logging.h>
20 #include "download_impl.h"
21 
22 namespace Ubuntu {
23 
24 namespace DownloadManager {
25 
26 DownloadImpl::DownloadImpl(const QDBusConnection& conn,
27  const QString& servicePath,
28  const QDBusObjectPath& objectPath,
29  QObject* parent)
30  : Download(parent),
31  _id(objectPath.path()),
32  _conn(conn),
33  _servicePath(servicePath) {
34  _dbusInterface = new DownloadInterface(servicePath,
35  _id, conn);
36 
37  // fwd all the signals but the error one
38  CHECK(connect(_dbusInterface, &DownloadInterface::canceled,
39  this, &Download::canceled)) << "Could not connect to signal";
40  CHECK(connect(_dbusInterface, &DownloadInterface::finished,
41  this, &Download::finished)) << "Could not connect to signal";
42  CHECK(connect(_dbusInterface, &DownloadInterface::paused,
43  this, &Download::paused)) << "Could not connect to signal";
44  CHECK(connect(_dbusInterface, &DownloadInterface::processing,
45  this, &Download::processing)) << "Could not connect to signal";
46  CHECK(connect(_dbusInterface, static_cast<void(DownloadInterface::*)
47  (qulonglong, qulonglong)>(&DownloadInterface::progress),
48  this, static_cast<void(Download::*)
49  (qulonglong, qulonglong)>(&Download::progress)))
50  << "Could not connect to signal";
51  CHECK(connect(_dbusInterface, &DownloadInterface::resumed,
52  this, &Download::resumed)) << "Could not connect to signal";
53  CHECK(connect(_dbusInterface, &DownloadInterface::started,
54  this, &Download::started)) << "Could not connect to signal";
55 
56  // connect to the different type of errors that will later be converted to
57  // the error type to be used by the client.
58  CHECK(connect(_dbusInterface, &DownloadInterface::httpError,
59  this, &DownloadImpl::onHttpError)) << "Could not connect to signal";
60  CHECK(connect(_dbusInterface, &DownloadInterface::networkError,
61  this, &DownloadImpl::onNetworkError)) << "Could not connect to signal";
62  CHECK(connect(_dbusInterface, &DownloadInterface::processError,
63  this, &DownloadImpl::onProcessError)) << "Could not connect to signal";
64  CHECK(connect(_dbusInterface, &DownloadInterface::authError,
65  this, &DownloadImpl::onAuthError)) << "Could not connect to signal";
66 }
67 
68 DownloadImpl::DownloadImpl(const QDBusConnection& conn, Error* err, QObject* parent)
69  : Download(parent),
70  _isError(true),
71  _lastError(err),
72  _conn(conn) {
73 }
74 
75 DownloadImpl::~DownloadImpl() {
76  delete _lastError;
77  delete _dbusInterface;
78 }
79 
80 void
81 DownloadImpl::setLastError(Error* err) {
82  if (_lastError != nullptr) {
83  delete _lastError;
84  }
85  _lastError = err;
86  _isError = true;
87  emit Download::error(err);
88 }
89 
90 void
91 DownloadImpl::setLastError(const QDBusError& err) {
92  setLastError(new DBusError(err, this));
93 }
94 
95 void
96 DownloadImpl::start() {
97  QDBusPendingCall call =
98  _dbusInterface->start();
99  auto watcher = new DownloadPCW(_conn, _servicePath,
100  call, this);
101  Q_UNUSED(watcher);
102 }
103 
104 void
105 DownloadImpl::pause() {
106  QDBusPendingCall call =
107  _dbusInterface->pause();
108  auto watcher = new DownloadPCW(_conn, _servicePath,
109  call, this);
110  Q_UNUSED(watcher);
111 }
112 
113 void
114 DownloadImpl::resume() {
115  QDBusPendingCall call =
116  _dbusInterface->resume();
117  auto watcher = new DownloadPCW(_conn, _servicePath,
118  call, this);
119  Q_UNUSED(watcher);
120 }
121 
122 void
123 DownloadImpl::cancel() {
124  QDBusPendingCall call =
125  _dbusInterface->cancel();
126  auto watcher = new DownloadPCW(_conn, _servicePath,
127  call, this);
128  Q_UNUSED(watcher);
129 }
130 
131 void
132 DownloadImpl::allowMobileDownload(bool allowed) {
133  QDBusPendingReply<> reply =
134  _dbusInterface->allowGSMDownload(allowed);
135  // block, the call should be fast enough
136  reply.waitForFinished();
137  if (reply.isError()) {
138  setLastError(reply.error());
139  }
140 }
141 
142 bool
143 DownloadImpl::isMobileDownloadAllowed() {
144  QDBusPendingReply<bool> reply =
145  _dbusInterface->isGSMDownloadAllowed();
146  // block, the call should be fast enough
147  reply.waitForFinished();
148  if (reply.isError()) {
149  setLastError(reply.error());
150  return false;
151  } else {
152  auto result = reply.value();
153  return result;
154  }
155 }
156 
157 void
158 DownloadImpl::setDestinationDir(const QString& path) {
159  QDBusPendingReply<> reply =
160  _dbusInterface->setDestinationDir(path);
161  // block, the call should be fast enough
162  reply.waitForFinished();
163  if (reply.isError()) {
164  setLastError(reply.error());
165  }
166 }
167 
168 void
169 DownloadImpl::setThrottle(qulonglong speed) {
170  QDBusPendingReply<> reply =
171  _dbusInterface->setThrottle(speed);
172  // block, the call should be fast enough
173  reply.waitForFinished();
174  if (reply.isError()) {
175  setLastError(reply.error());
176  }
177 }
178 
179 qulonglong
180 DownloadImpl::throttle() {
181  QDBusPendingReply<qulonglong> reply =
182  _dbusInterface->throttle();
183  // block, the call is fast enough
184  reply.waitForFinished();
185  if (reply.isError()) {
186  setLastError(reply.error());
187  return 0;
188  } else {
189  auto result = reply.value();
190  return result;
191  }
192 }
193 
194 QString
195 DownloadImpl::id() const {
196  return _id;
197 }
198 
199 QVariantMap
200 DownloadImpl::metadata() {
201  QDBusPendingReply<QVariantMap> reply =
202  _dbusInterface->metadata();
203  // block the call is fast enough
204  reply.waitForFinished();
205  if (reply.isError()) {
206  QVariantMap emptyResult;
207  setLastError(reply.error());
208  return emptyResult;
209  } else {
210  auto result = reply.value();
211  return result;
212  }
213 }
214 
215 qulonglong
216 DownloadImpl::progress() {
217  QDBusPendingReply<qulonglong> reply =
218  _dbusInterface->progress();
219  // block call should be fast enough
220  reply.waitForFinished();
221  if (reply.isError()) {
222  setLastError(reply.error());
223  return 0;
224  } else {
225  auto result = reply.value();
226  return result;
227  }
228 }
229 
230 qulonglong
231 DownloadImpl::totalSize() {
232  QDBusPendingReply<qulonglong> reply =
233  _dbusInterface->totalSize();
234  // block call should b fast enough
235  reply.waitForFinished();
236  if (reply.isError()) {
237  setLastError(reply.error());
238  return 0;
239  } else {
240  auto result = reply.value();
241  return result;
242  }
243 }
244 
245 bool
246 DownloadImpl::isError() const {
247  return _isError;
248 }
249 
250 Error*
251 DownloadImpl::error() const {
252  return _lastError;
253 }
254 
255 void
256 DownloadImpl::onHttpError(HttpErrorStruct errStruct) {
257  auto err = new HttpError(errStruct, this);
258  setLastError(err);
259 }
260 
261 void
262 DownloadImpl::onNetworkError(NetworkErrorStruct errStruct) {
263  auto err = new NetworkError(errStruct, this);
264  setLastError(err);
265 }
266 
267 void
268 DownloadImpl::onProcessError(ProcessErrorStruct errStruct) {
269  auto err = new ProcessError(errStruct, this);
270  setLastError(err);
271 }
272 
273 void
274 DownloadImpl::onAuthError(AuthErrorStruct errStruct) {
275  auto err = new AuthError(errStruct, this);
276  setLastError(err);
277 }
278 
279 } // DownloadManager
280 
281 } // Ubuntu
void finished(const QString &path)
Download(QObject *parent=0)
Definition: download.h:53
void processing(const QString &path)
virtual Error * error() const =0
virtual qulonglong progress()=0