19#include <QProcessEnvironment>
24 const QString CLICK_PACKAGE_PROPERTY =
"ClickPackage";
25 const QString SHOW_INDICATOR_PROPERTY =
"ShowInIndicator";
26 const QString TITLE_PROPERTY =
"Title";
31namespace DownloadManager {
33using namespace Logging;
35DownloadImpl::DownloadImpl(
const QDBusConnection& conn,
36 const QString& servicePath,
37 const QDBusObjectPath& objectPath,
40 _id(objectPath.path()),
42 _servicePath(servicePath) {
44 _dbusInterface =
new DownloadInterface(servicePath,
51 auto connected = connect(_dbusInterface, &DownloadInterface::canceled,
52 this, &Download::canceled);
55 Logger::log(Logger::Critical,
56 "Could not connect to signal DownloadInterface::canceled");
59 connected = connect(_dbusInterface, &DownloadInterface::finished,
60 this, &Download::finished);
62 Logger::log(Logger::Critical,
63 "Could not connect to signal &DownloadInterface::finished");
66 connected = connect(_dbusInterface, &DownloadInterface::finished,
67 this, &DownloadImpl::onFinished);
69 Logger::log(Logger::Critical,
70 "Could not connect to signal &DownloadInterface::finished");
73 connected = connect(_dbusInterface, &DownloadInterface::paused,
74 this, &Download::paused);
76 Logger::log(Logger::Critical,
77 "Could not connect to signal DownloadInterface::paused");
80 connected = connect(_dbusInterface, &DownloadInterface::processing,
81 this, &Download::processing);
83 Logger::log(Logger::Critical,
84 "Could not connect to signal DownloadInterface::processing");
87 connected = connect(_dbusInterface,
static_cast<void(DownloadInterface::*)
88 (qulonglong, qulonglong)
>(&DownloadInterface::progress),
89 this,
static_cast<void(Download::*)
90 (qulonglong, qulonglong)
>(&Download::progress));
92 Logger::log(Logger::Critical,
93 "Could not connect to signal &DownloadInterface::progress");
96 connected = connect(_dbusInterface, &DownloadInterface::resumed,
97 this, &Download::resumed);
99 Logger::log(Logger::Critical,
100 "Could not connect to signal &DownloadInterface::resumed");
103 connected = connect(_dbusInterface, &DownloadInterface::started,
104 this, &Download::started);
106 Logger::log(Logger::Critical,
107 "Could not connect to signal &DownloadInterface::started");
112 connected = connect(_dbusInterface, &DownloadInterface::httpError,
113 this, &DownloadImpl::onHttpError);
115 Logger::log(Logger::Critical,
116 "Could not connect to signal &DownloadInterface::httpError");
119 connected = connect(_dbusInterface, &DownloadInterface::networkError,
120 this, &DownloadImpl::onNetworkError);
122 Logger::log(Logger::Critical,
123 "Could not connect to signal &DownloadInterface::networkError");
126 connected = connect(_dbusInterface, &DownloadInterface::processError,
127 this, &DownloadImpl::onProcessError);
129 Logger::log(Logger::Critical,
130 "Could not connect to signal &DownloadInterface::processError");
133 connected = connect(_dbusInterface, &DownloadInterface::authError,
134 this, &DownloadImpl::onAuthError);
136 Logger::log(Logger::Critical,
137 "Could not connect to signal &DownloadInterface::authError");
140 connected = connect(_dbusInterface, &DownloadInterface::hashError,
141 this, &DownloadImpl::onHashError);
143 Logger::log(Logger::Critical,
144 "Could not connect to signal &DownloadInterface::authError");
148 this, &DownloadImpl::onPropertiesChanged);
150 Logger::log(Logger::Critical,
151 "Could not connect to signal &PropertiesInterface::PropertiesChanged");
155DownloadImpl::DownloadImpl(
const QDBusConnection& conn, Error* err,
QObject* parent)
162DownloadImpl::~DownloadImpl() {
164 delete _dbusInterface;
165 delete _propertiesInterface;
169DownloadImpl::setLastError(Error* err) {
170 Logger::log(Logger::Debug,
171 QString(
"Download{%1} setLastError(%2)").arg(_id).arg(
172 err->errorString()));
173 if (_lastError !=
nullptr) {
178 emit Download::error(err);
182DownloadImpl::setLastError(
const QDBusError& err) {
183 setLastError(
new DBusError(err,
this));
187DownloadImpl::start() {
188 if (_dbusInterface ==
nullptr || !_dbusInterface->isValid()) {
189 Logger::log(Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
192 Logger::log(Logger::Debug, QString(
"Download{%1} start())").arg(_id));
193 QDBusPendingCall call =
194 _dbusInterface->start();
195 auto watcher =
new DownloadPCW(_conn, _servicePath,
201DownloadImpl::pause() {
202 if (_dbusInterface ==
nullptr || !_dbusInterface->isValid()) {
203 Logger::log(Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
206 Logger::log(Logger::Debug, QString(
"Download{%1} pause())").arg(_id));
207 QDBusPendingCall call =
208 _dbusInterface->pause();
209 auto watcher =
new DownloadPCW(_conn, _servicePath,
215DownloadImpl::resume() {
216 if (_dbusInterface ==
nullptr || !_dbusInterface->isValid()) {
217 Logger::log(Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
220 Logger::log(Logger::Debug, QString(
"Download{%1} resume())").arg(_id));
221 QDBusPendingCall call =
222 _dbusInterface->resume();
223 auto watcher =
new DownloadPCW(_conn, _servicePath,
229DownloadImpl::cancel() {
230 if (_dbusInterface ==
nullptr || !_dbusInterface->isValid()) {
231 Logger::log(Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
234 Logger::log(Logger::Debug, QString(
"Download{%1} cancel())").arg(_id));
235 QDBusPendingCall call =
236 _dbusInterface->cancel();
237 auto watcher =
new DownloadPCW(_conn, _servicePath,
243DownloadImpl::collected() {
244 if (_dbusInterface ==
nullptr || !_dbusInterface->isValid()) {
245 Logger::log(Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
248 Logger::log(Logger::Debug, QString(
"Download{%1} collected()").arg(_id));
249 QDBusPendingReply<> reply =
250 _dbusInterface->collected();
252 reply.waitForFinished();
253 if (reply.isError()) {
254 Logger::log(Logger::Error,
"Error when setting download collected");
255 setLastError(reply.error());
260DownloadImpl::allowMobileDownload(
bool allowed) {
261 if (_dbusInterface ==
nullptr || !_dbusInterface->isValid()) {
262 Logger::log(Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
265 Logger::log(Logger::Debug,
266 QString(
"Download{%1} allowMobileDownload%2())").arg(_id).arg(allowed));
267 QDBusPendingReply<> reply =
268 _dbusInterface->allowGSMDownload(allowed);
270 reply.waitForFinished();
271 if (reply.isError()) {
272 Logger::log(Logger::Error,
"Error when setting mobile data usage");
273 setLastError(reply.error());
278DownloadImpl::isMobileDownloadAllowed() {
279 if (_dbusInterface ==
nullptr || !_dbusInterface->isValid()) {
280 Logger::log(Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
283 Logger::log(Logger::Debug,
284 QString(
"Download{%1} isMobileDownloadAllowed").arg(_id));
285 QDBusPendingReply<bool> reply =
286 _dbusInterface->isGSMDownloadAllowed();
288 reply.waitForFinished();
289 if (reply.isError()) {
290 Logger::log(Logger::Error,
"Error when querying mobile data usage");
291 setLastError(reply.error());
294 auto result = reply.value();
300DownloadImpl::setDestinationDir(
const QString& path) {
301 if (_dbusInterface ==
nullptr || !_dbusInterface->isValid()) {
302 Logger::log(Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
305 Logger::log(Logger::Debug, QString(
"Dowmload{%1} setDestinationDir(%2)")
306 .arg(_id).arg(path));
307 QDBusPendingReply<> reply =
308 _dbusInterface->setDestinationDir(path);
310 reply.waitForFinished();
311 if (reply.isError()) {
312 Logger::log(Logger::Error,
"Error setting the download directory");
313 setLastError(reply.error());
318DownloadImpl::setHeaders(QMap<QString, QString> headers) {
319 if (_dbusInterface ==
nullptr || !_dbusInterface->isValid()) {
320 Logger::log(Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
323 Logger::log(Logger::Debug,
324 QString(
"Download {%1} setHeaders(%2)").arg(_id), headers);
326 QDBusPendingReply<> reply =
327 _dbusInterface->setHeaders(headers);
329 reply.waitForFinished();
330 if (reply.isError()) {
331 Logger::log(Logger::Error,
"Error setting the download headers");
332 setLastError(reply.error());
337DownloadImpl::metadata() {
338 if (_dbusInterface ==
nullptr || !_dbusInterface->isValid()) {
339 Logger::log(Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
340 QVariantMap emptyResult;
343 Logger::log(Logger::Debug, QString(
"Download{%1} metadata()").arg(_id));
344 QDBusPendingReply<QVariantMap> reply =
345 _dbusInterface->metadata();
347 reply.waitForFinished();
348 if (reply.isError()) {
349 Logger::log(Logger::Error,
"Error querying the download metadata");
350 QVariantMap emptyResult;
351 setLastError(reply.error());
354 auto result = reply.value();
360DownloadImpl::setMetadata(QVariantMap map) {
361 if (_dbusInterface ==
nullptr || !_dbusInterface->isValid()) {
362 Logger::log(Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
365 Logger::log(Logger::Debug,
366 QString(
"Download {%1} setMetadata(%2)").arg(_id), map);
368 QDBusPendingReply<> reply =
369 _dbusInterface->setMetadata(map);
371 reply.waitForFinished();
372 if (reply.isError()) {
373 Logger::log(Logger::Error,
"Error setting the download metadata");
374 setLastError(reply.error());
378QMap<QString, QString>
379DownloadImpl::headers() {
380 if (_dbusInterface ==
nullptr || !_dbusInterface->isValid()) {
381 Logger::log(Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
382 QMap<QString, QString> empty;
385 Logger::log(Logger::Debug, QString(
"Download{%1} headers()").arg(_id));
386 QDBusPendingReply<QMap<QString, QString> > reply =
387 _dbusInterface->headers();
389 reply.waitForFinished();
390 if (reply.isError()) {
391 Logger::log(Logger::Error,
"Error querying the download headers");
392 setLastError(reply.error());
393 QMap<QString, QString> empty;
396 auto result = reply.value();
403DownloadImpl::setThrottle(qulonglong speed) {
404 if (_dbusInterface ==
nullptr || !_dbusInterface->isValid()) {
405 Logger::log(Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
408 Logger::log(Logger::Debug,
409 QString(
"Download{%1} setThrottle(%2)").arg(_id).arg(speed));
410 QDBusPendingReply<> reply =
411 _dbusInterface->setThrottle(speed);
413 reply.waitForFinished();
414 if (reply.isError()) {
415 Logger::log(Logger::Error,
"Error setting the download throttle");
416 setLastError(reply.error());
421DownloadImpl::throttle() {
422 if (_dbusInterface ==
nullptr || !_dbusInterface->isValid()) {
423 Logger::log(Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
426 Logger::log(Logger::Debug, QString(
"Download{%1} throttle()").arg(_id));
427 QDBusPendingReply<qulonglong> reply =
428 _dbusInterface->throttle();
430 reply.waitForFinished();
431 if (reply.isError()) {
432 Logger::log(Logger::Error,
"Error querying the download throttle");
433 setLastError(reply.error());
436 auto result = reply.value();
442DownloadImpl::filePath() {
443 if (_dbusInterface ==
nullptr || !_dbusInterface->isValid()) {
444 Logger::log(Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
447 Logger::log(Logger::Debug, QString(
"Download{%1} filePath()").arg(_id));
448 QDBusPendingReply<QString> reply =
449 _dbusInterface->filePath();
451 reply.waitForFinished();
452 if (reply.isError()) {
453 Logger::log(Logger::Error,
"Error querying the download file path");
454 setLastError(reply.error());
457 auto result = reply.value();
463DownloadImpl::state() {
464 if (_dbusInterface ==
nullptr || !_dbusInterface->isValid()) {
465 Logger::log(Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
466 return Download::ERROR;
468 Logger::log(Logger::Debug, QString(
"Download{%1} state()").arg(_id));
469 QDBusPendingReply<int> reply =
470 _dbusInterface->state();
472 reply.waitForFinished();
473 if (reply.isError()) {
474 Logger::log(Logger::Error,
"Error querying the download state");
475 setLastError(reply.error());
476 return Download::ERROR;
478 auto result =
static_cast<Download::State
>(reply.value());
484DownloadImpl::id()
const {
489DownloadImpl::progress() {
490 if (_dbusInterface ==
nullptr || !_dbusInterface->isValid()) {
491 Logger::log(Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
494 Logger::log(Logger::Debug, QString(
"Download{%1} progress()").arg(_id));
495 QDBusPendingReply<qulonglong> reply =
496 _dbusInterface->progress();
498 reply.waitForFinished();
499 if (reply.isError()) {
500 Logger::log(Logger::Error,
"Error querying the download progress");
501 setLastError(reply.error());
504 auto result = reply.value();
510DownloadImpl::totalSize() {
511 if (_dbusInterface ==
nullptr || !_dbusInterface->isValid()) {
512 Logger::log(Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
515 Logger::log(Logger::Debug, QString(
"Download{%1} totalSize()").arg(_id));
516 QDBusPendingReply<qulonglong> reply =
517 _dbusInterface->totalSize();
519 reply.waitForFinished();
520 if (reply.isError()) {
521 Logger::log(Logger::Error,
"Error querying the download size");
522 setLastError(reply.error());
525 auto result = reply.value();
531DownloadImpl::isError()
const {
536DownloadImpl::error()
const {
541DownloadImpl::clickPackage()
const {
542 if (_dbusInterface ==
nullptr || !_dbusInterface->isValid()) {
543 Logger::log(Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
546 return _dbusInterface->clickPackage();
550DownloadImpl::showInIndicator()
const {
551 if (_dbusInterface ==
nullptr || !_dbusInterface->isValid()) {
552 Logger::log(Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
555 return _dbusInterface->showInIndicator();
559DownloadImpl::title()
const {
560 if (_dbusInterface ==
nullptr || !_dbusInterface->isValid()) {
561 Logger::log(Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
564 return _dbusInterface->title();
568DownloadImpl::destinationApp()
const {
569 if (_dbusInterface ==
nullptr || !_dbusInterface->isValid()) {
570 Logger::log(Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
573 return _dbusInterface->destinationApp();
577DownloadImpl::onHttpError(HttpErrorStruct errStruct) {
578 auto err =
new HttpError(errStruct,
this);
583DownloadImpl::onNetworkError(NetworkErrorStruct errStruct) {
584 auto err =
new NetworkError(errStruct,
this);
589DownloadImpl::onProcessError(ProcessErrorStruct errStruct) {
590 auto err =
new ProcessError(errStruct,
this);
595DownloadImpl::onAuthError(AuthErrorStruct errStruct) {
596 auto err =
new AuthError(errStruct,
this);
601DownloadImpl::onHashError(HashErrorStruct errStruct) {
602 auto err =
new HashError(errStruct,
this);
607DownloadImpl::onPropertiesChanged(
const QString& interfaceName,
608 const QVariantMap& changedProperties,
609 const QStringList& invalidatedProperties) {
610 Q_UNUSED(invalidatedProperties);
612 if (interfaceName == DownloadInterface::staticInterfaceName()) {
613 if (changedProperties.contains(CLICK_PACKAGE_PROPERTY)) {
614 emit clickPackagedChanged();
617 if (changedProperties.contains(SHOW_INDICATOR_PROPERTY)) {
618 emit showInIndicatorChanged();
621 if (changedProperties.contains(TITLE_PROPERTY)) {
627void DownloadImpl::onFinished(
const QString &path) {
632 auto environment = QProcessEnvironment::systemEnvironment();
634 if (environment.contains(
"APP_ID")) {
635 appId = environment.value(
"APP_ID");
637 appId = QCoreApplication::applicationFilePath();
640 if (appId == metadata().value(
"app-id", appId)) {
void PropertiesChanged(const QString &interface_name, const QVariantMap &changed_properties, const QStringList &invalidated_properties)