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     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,
 
  201 DownloadImpl::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,
 
  215 DownloadImpl::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,
 
  229 DownloadImpl::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,
 
  243 DownloadImpl::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());
 
  260 DownloadImpl::allowMobileDownload(
bool allowed) {
 
  261     if (_dbusInterface == 
nullptr || !_dbusInterface->isValid()) {
 
  262         Logger::log(
Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
 
  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());
 
  278 DownloadImpl::isMobileDownloadAllowed() {
 
  279     if (_dbusInterface == 
nullptr || !_dbusInterface->isValid()) {
 
  280         Logger::log(
Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
 
  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();
 
  300 DownloadImpl::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());
 
  318 DownloadImpl::setHeaders(QMap<QString, QString> headers) {
 
  319     if (_dbusInterface == 
nullptr || !_dbusInterface->isValid()) {
 
  320         Logger::log(
Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
 
  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());
 
  337 DownloadImpl::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();
 
  360 DownloadImpl::setMetadata(QVariantMap map) {
 
  361     if (_dbusInterface == 
nullptr || !_dbusInterface->isValid()) {
 
  362         Logger::log(
Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
 
  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());
 
  378 QMap<QString, QString>
 
  379 DownloadImpl::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();
 
  403 DownloadImpl::setThrottle(qulonglong speed) {
 
  404     if (_dbusInterface == 
nullptr || !_dbusInterface->isValid()) {
 
  405         Logger::log(
Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
 
  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());
 
  421 DownloadImpl::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();
 
  442 DownloadImpl::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();
 
  463 DownloadImpl::state() {
 
  464     if (_dbusInterface == 
nullptr || !_dbusInterface->isValid()) {
 
  465         Logger::log(
Logger::Error, QString(
"Invalid dbus interface: %1").arg(_lastError->errorString()));
 
  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());
 
  484 DownloadImpl::id()
 const {
 
  489 DownloadImpl::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();
 
  510 DownloadImpl::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();
 
  531 DownloadImpl::isError()
 const {
 
  536 DownloadImpl::error()
 const {
 
  541 DownloadImpl::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();
 
  550 DownloadImpl::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();
 
  559 DownloadImpl::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();
 
  568 DownloadImpl::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();
 
  577 DownloadImpl::onHttpError(HttpErrorStruct errStruct) {
 
  578     auto err = 
new HttpError(errStruct, 
this);
 
  583 DownloadImpl::onNetworkError(NetworkErrorStruct errStruct) {
 
  584     auto err = 
new NetworkError(errStruct, 
this);
 
  589 DownloadImpl::onProcessError(ProcessErrorStruct errStruct) {
 
  590     auto err = 
new ProcessError(errStruct, 
this);
 
  595 DownloadImpl::onAuthError(AuthErrorStruct errStruct) {
 
  596     auto err = 
new AuthError(errStruct, 
this);
 
  601 DownloadImpl::onHashError(HashErrorStruct errStruct) {
 
  602     auto err = 
new HashError(errStruct, 
this);
 
  607 DownloadImpl::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)) {
 
  627 void 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)) {