2
* Copyright (C) 2014 - Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License, as
6
* published by the Free Software Foundation; either version 2.1 or 3.0
9
* This program is distributed in the hope that it will be useful, but
10
* WITHOUT ANY WARRANTY; without even the implied warranties of
11
* MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
12
* PURPOSE. See the applicable version of the GNU Lesser General Public
13
* License for more details.
15
* You should have received a copy of both the GNU Lesser General Public
16
* License along with this program. If not, see <http://www.gnu.org/licenses/>
18
* Authored by: Diego Sarmentero <diego.sarmentero@canonical.com>
21
#include "download_tracker.h"
22
#include "network/network.h"
23
#include <ubuntu/download_manager/download_struct.h>
24
#include <ubuntu/download_manager/error.h>
25
#include <QProcessEnvironment>
27
#define DOWNLOAD_COMMAND "post-download-command"
28
#define APP_ID "app_id"
29
#define PKCON_COMMAND "pkcon"
31
namespace UpdatePlugin {
33
DownloadTracker::DownloadTracker(QObject *parent) :
43
void DownloadTracker::setDownload(const QString& url)
51
void DownloadTracker::setClickToken(const QString& token)
59
void DownloadTracker::setPackageName(const QString& package)
62
m_packageName = package;
67
void DownloadTracker::startService()
69
if (!m_clickToken.isEmpty() && !m_downloadUrl.isEmpty() && !m_packageName.isEmpty()) {
70
if (m_manager == nullptr) {
71
m_manager = Manager::createSessionManager("", this);
73
QObject::connect(m_manager, SIGNAL(downloadCreated(Download*)),
74
this, SLOT(bindDownload(Download*)));
78
QString command = getPkconCommand();
79
args << command << "-p" << "install-local" << "$file";
80
vmap[DOWNLOAD_COMMAND] = args;
81
vmap[APP_ID] = m_packageName;
83
map[X_CLICK_TOKEN] = m_clickToken;
84
DownloadStruct dstruct = DownloadStruct(m_downloadUrl, vmap, map);
85
m_manager->createDownload(dstruct);
89
void DownloadTracker::bindDownload(Download* download)
91
m_download = download;
92
connect(m_download, SIGNAL(finished(const QString &)), this,
93
SIGNAL(finished(const QString &)));
94
connect(m_download, SIGNAL(error(Error*)), this,
95
SLOT(registerError(Error*)));
96
connect(m_download, SIGNAL(progress(qulonglong, qulonglong)), this,
97
SLOT(setProgress(qulonglong, qulonglong)));
102
void DownloadTracker::registerError(Error* error)
104
Q_EMIT errorFound(error->errorString());
107
void DownloadTracker::pause()
109
if (m_download != nullptr) {
114
void DownloadTracker::resume()
116
if (m_download != nullptr) {
117
m_download->resume();
121
QString DownloadTracker::getPkconCommand()
123
QProcessEnvironment environment = QProcessEnvironment::systemEnvironment();
124
QString command = environment.value("PKCON_COMMAND", QString(PKCON_COMMAND));
128
void DownloadTracker::setProgress(qulonglong received, qulonglong total)
131
qulonglong result = (received * 100);
132
m_progress = static_cast<int>(result / total);
133
emit progressChanged();