Ubuntu Download Manager: /build/ubuntu-download-manager-git/src/ubuntu-download-manager/src/downloads/client/ubuntu/download_manager/manager_impl.cpp Source File

Ubuntu Download Manager 1.2.0 A session-wide downloading service |

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