19 #include <QProcessEnvironment>
24 const QString CLICK_PACKAGE_PROPERTY =
"ClickPackage";
25 const QString SHOW_INDICATOR_PROPERTY =
"ShowInIndicator";
26 const QString TITLE_PROPERTY =
"Title";
31 namespace DownloadManager {
33 using namespace Logging;
35 DownloadImpl::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,
56 "Could not connect to signal DownloadInterface::canceled");
59 connected = connect(_dbusInterface, &DownloadInterface::finished,
63 "Could not connect to signal &DownloadInterface::finished");
66 connected = connect(_dbusInterface, &DownloadInterface::finished,
67 this, &DownloadImpl::onFinished);
70 "Could not connect to signal &DownloadInterface::finished");
73 connected = connect(_dbusInterface, &DownloadInterface::paused,
77 "Could not connect to signal DownloadInterface::paused");
80 connected = connect(_dbusInterface, &DownloadInterface::processing,
84 "Could not connect to signal DownloadInterface::processing");
87 connected = connect(_dbusInterface,
static_cast<void(DownloadInterface::*)
88 (qulonglong, qulonglong)
>(&DownloadInterface::progress),
93 "Could not connect to signal &DownloadInterface::progress");
96 connected = connect(_dbusInterface, &DownloadInterface::resumed,
100 "Could not connect to signal &DownloadInterface::resumed");
103 connected = connect(_dbusInterface, &DownloadInterface::started,
107 "Could not connect to signal &DownloadInterface::started");
112 connected = connect(_dbusInterface, &DownloadInterface::httpError,
113 this, &DownloadImpl::onHttpError);
116 "Could not connect to signal &DownloadInterface::httpError");
119 connected = connect(_dbusInterface, &DownloadInterface::networkError,
120 this, &DownloadImpl::onNetworkError);
123 "Could not connect to signal &DownloadInterface::networkError");
126 connected = connect(_dbusInterface, &DownloadInterface::processError,
127 this, &DownloadImpl::onProcessError);
130 "Could not connect to signal &DownloadInterface::processError");
133 connected = connect(_dbusInterface, &DownloadInterface::authError,
134 this, &DownloadImpl::onAuthError);
137 "Could not connect to signal &DownloadInterface::authError");
140 connected = connect(_dbusInterface, &DownloadInterface::hashError,
141 this, &DownloadImpl::onHashError);
144 "Could not connect to signal &DownloadInterface::authError");
148 this, &DownloadImpl::onPropertiesChanged);
151 "Could not connect to signal &PropertiesInterface::PropertiesChanged");
155 DownloadImpl::DownloadImpl(
const QDBusConnection& conn, Error* err,
QObject* parent)
162 DownloadImpl::~DownloadImpl() {
164 delete _dbusInterface;
165 delete _propertiesInterface;
169 DownloadImpl::setLastError(Error* err) {
171 QString(
"Download{%1} setLastError(%2)").arg(_id).arg(
172 err->errorString()));
173 if (_lastError !=
nullptr) {
182 DownloadImpl::setLastError(
const QDBusError& err) {
183 setLastError(
new DBusError(err,
this));
187 DownloadImpl::start() {
188 Logger::log(
Logger::Debug, QString(
"Download{%1} start())").arg(_id));
189 QDBusPendingCall call =
190 _dbusInterface->start();
191 auto watcher =
new DownloadPCW(_conn, _servicePath,
197 DownloadImpl::pause() {
198 Logger::log(
Logger::Debug, QString(
"Download{%1} pause())").arg(_id));
199 QDBusPendingCall call =
200 _dbusInterface->pause();
201 auto watcher =
new DownloadPCW(_conn, _servicePath,
207 DownloadImpl::resume() {
208 Logger::log(
Logger::Debug, QString(
"Download{%1} resume())").arg(_id));
209 QDBusPendingCall call =
210 _dbusInterface->resume();
211 auto watcher =
new DownloadPCW(_conn, _servicePath,
217 DownloadImpl::cancel() {
218 Logger::log(
Logger::Debug, QString(
"Download{%1} cancel())").arg(_id));
219 QDBusPendingCall call =
220 _dbusInterface->cancel();
221 auto watcher =
new DownloadPCW(_conn, _servicePath,
227 DownloadImpl::collected() {
228 Logger::log(
Logger::Debug, QString(
"Download{%1} collected()").arg(_id));
229 QDBusPendingReply<> reply =
230 _dbusInterface->collected();
232 reply.waitForFinished();
233 if (reply.isError()) {
234 Logger::log(
Logger::Error,
"Error when setting download collected");
235 setLastError(reply.error());
240 DownloadImpl::allowMobileDownload(
bool allowed) {
242 QString(
"Download{%1} allowMobileDownload%2())").arg(_id).arg(allowed));
243 QDBusPendingReply<> reply =
244 _dbusInterface->allowGSMDownload(allowed);
246 reply.waitForFinished();
247 if (reply.isError()) {
248 Logger::log(
Logger::Error,
"Error when setting mobile data usage");
249 setLastError(reply.error());
254 DownloadImpl::isMobileDownloadAllowed() {
256 QString(
"Download{%1} isMobileDownloadAllowed").arg(_id));
257 QDBusPendingReply<bool> reply =
258 _dbusInterface->isGSMDownloadAllowed();
260 reply.waitForFinished();
261 if (reply.isError()) {
262 Logger::log(
Logger::Error,
"Error when querying mobile data usage");
263 setLastError(reply.error());
266 auto result = reply.value();
272 DownloadImpl::setDestinationDir(
const QString& path) {
273 Logger::log(
Logger::Debug, QString(
"Dowmload{%1} setDestinationDir(%2)")
274 .arg(_id).arg(path));
275 QDBusPendingReply<> reply =
276 _dbusInterface->setDestinationDir(path);
278 reply.waitForFinished();
279 if (reply.isError()) {
280 Logger::log(
Logger::Error,
"Error setting the download directory");
281 setLastError(reply.error());
286 DownloadImpl::setHeaders(QMap<QString, QString> headers) {
288 QString(
"Download {%1} setHeaders(%2)").arg(_id), headers);
290 QDBusPendingReply<> reply =
291 _dbusInterface->setHeaders(headers);
293 reply.waitForFinished();
294 if (reply.isError()) {
295 Logger::log(
Logger::Error,
"Error setting the download headers");
296 setLastError(reply.error());
301 DownloadImpl::metadata() {
302 Logger::log(
Logger::Debug, QString(
"Download{%1} metadata()").arg(_id));
303 QDBusPendingReply<QVariantMap> reply =
304 _dbusInterface->metadata();
306 reply.waitForFinished();
307 if (reply.isError()) {
308 Logger::log(
Logger::Error,
"Error querying the download metadata");
309 QVariantMap emptyResult;
310 setLastError(reply.error());
313 auto result = reply.value();
319 DownloadImpl::setMetadata(QVariantMap map) {
321 QString(
"Download {%1} setMetadata(%2)").arg(_id), map);
323 QDBusPendingReply<> reply =
324 _dbusInterface->setMetadata(map);
326 reply.waitForFinished();
327 if (reply.isError()) {
328 Logger::log(
Logger::Error,
"Error setting the download metadata");
329 setLastError(reply.error());
333 QMap<QString, QString>
334 DownloadImpl::headers() {
335 Logger::log(
Logger::Debug, QString(
"Download{%1} headers()").arg(_id));
336 QDBusPendingReply<QMap<QString, QString> > reply =
337 _dbusInterface->headers();
339 reply.waitForFinished();
340 if (reply.isError()) {
341 Logger::log(
Logger::Error,
"Error querying the download headers");
342 setLastError(reply.error());
343 QMap<QString, QString> empty;
346 auto result = reply.value();
353 DownloadImpl::setThrottle(qulonglong speed) {
355 QString(
"Download{%1} setThrottle(%2)").arg(_id).arg(speed));
356 QDBusPendingReply<> reply =
357 _dbusInterface->setThrottle(speed);
359 reply.waitForFinished();
360 if (reply.isError()) {
361 Logger::log(
Logger::Error,
"Error setting the download throttle");
362 setLastError(reply.error());
367 DownloadImpl::throttle() {
368 Logger::log(
Logger::Debug, QString(
"Download{%1} throttle()").arg(_id));
369 QDBusPendingReply<qulonglong> reply =
370 _dbusInterface->throttle();
372 reply.waitForFinished();
373 if (reply.isError()) {
374 Logger::log(
Logger::Error,
"Error querying the download throttle");
375 setLastError(reply.error());
378 auto result = reply.value();
384 DownloadImpl::filePath() {
385 Logger::log(
Logger::Debug, QString(
"Download{%1} filePath()").arg(_id));
386 QDBusPendingReply<QString> reply =
387 _dbusInterface->filePath();
389 reply.waitForFinished();
390 if (reply.isError()) {
391 Logger::log(
Logger::Error,
"Error querying the download file path");
392 setLastError(reply.error());
395 auto result = reply.value();
401 DownloadImpl::state() {
402 Logger::log(
Logger::Debug, QString(
"Download{%1} state()").arg(_id));
403 QDBusPendingReply<int> reply =
404 _dbusInterface->state();
406 reply.waitForFinished();
407 if (reply.isError()) {
408 Logger::log(
Logger::Error,
"Error querying the download state");
409 setLastError(reply.error());
418 DownloadImpl::id()
const {
423 DownloadImpl::progress() {
424 Logger::log(
Logger::Debug, QString(
"Download{%1} progress()").arg(_id));
425 QDBusPendingReply<qulonglong> reply =
426 _dbusInterface->progress();
428 reply.waitForFinished();
429 if (reply.isError()) {
430 Logger::log(
Logger::Error,
"Error querying the download progress");
431 setLastError(reply.error());
434 auto result = reply.value();
440 DownloadImpl::totalSize() {
441 Logger::log(
Logger::Debug, QString(
"Download{%1} totalSize()").arg(_id));
442 QDBusPendingReply<qulonglong> reply =
443 _dbusInterface->totalSize();
445 reply.waitForFinished();
446 if (reply.isError()) {
447 Logger::log(
Logger::Error,
"Error querying the download size");
448 setLastError(reply.error());
451 auto result = reply.value();
457 DownloadImpl::isError()
const {
462 DownloadImpl::error()
const {
467 DownloadImpl::clickPackage()
const {
468 return _dbusInterface->clickPackage();
472 DownloadImpl::showInIndicator()
const {
473 return _dbusInterface->showInIndicator();
477 DownloadImpl::title()
const {
478 return _dbusInterface->title();
482 DownloadImpl::destinationApp()
const {
483 return _dbusInterface->destinationApp();
487 DownloadImpl::onHttpError(HttpErrorStruct errStruct) {
488 auto err =
new HttpError(errStruct,
this);
493 DownloadImpl::onNetworkError(NetworkErrorStruct errStruct) {
494 auto err =
new NetworkError(errStruct,
this);
499 DownloadImpl::onProcessError(ProcessErrorStruct errStruct) {
500 auto err =
new ProcessError(errStruct,
this);
505 DownloadImpl::onAuthError(AuthErrorStruct errStruct) {
506 auto err =
new AuthError(errStruct,
this);
511 DownloadImpl::onHashError(HashErrorStruct errStruct) {
512 auto err =
new HashError(errStruct,
this);
517 DownloadImpl::onPropertiesChanged(
const QString& interfaceName,
518 const QVariantMap& changedProperties,
519 const QStringList& invalidatedProperties) {
520 Q_UNUSED(invalidatedProperties);
522 if (interfaceName == DownloadInterface::staticInterfaceName()) {
523 if (changedProperties.contains(CLICK_PACKAGE_PROPERTY)) {
524 emit clickPackagedChanged();
527 if (changedProperties.contains(SHOW_INDICATOR_PROPERTY)) {
528 emit showInIndicatorChanged();
531 if (changedProperties.contains(TITLE_PROPERTY)) {
537 void DownloadImpl::onFinished(
const QString &path) {
542 auto environment = QProcessEnvironment::systemEnvironment();
544 if (environment.contains(
"APP_ID")) {
545 appId = environment.value(
"APP_ID");
547 appId = QCoreApplication::applicationFilePath();
550 if (appId == metadata().value(
"app-id", appId)) {
void paused(bool success)
void finished(const QString &path)
void canceled(bool success)
void resumed(bool success)
Download(QObject *parent=0)
void PropertiesChanged(const QString &interface_name, const QVariantMap &changed_properties, const QStringList &invalidated_properties)
void processing(const QString &path)
void started(bool success)
virtual Error * error() const =0
virtual qulonglong progress()=0