~ci-train-bot/ubuntu-system-settings/ubuntu-system-settings-ubuntu-yakkety-landing-027

« back to all changes in this revision

Viewing changes to plugins/system-update/download_tracker.cpp

  • Committer: Bileto Bot
  • Author(s): jonas-drange
  • Date: 2016-08-17 11:18:05 UTC
  • mfrom: (1631.6.245 updates-rewrite)
  • Revision ID: ci-train-bot@canonical.com-20160817111805-moyvik9wloy79ul3
rewrite the system update panel

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2014-2015 - Canonical Ltd.
3
 
 *
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
7
 
 * of the License.
8
 
 *
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.
14
 
 *
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/>
17
 
 *
18
 
 * Authored by: Diego Sarmentero <diego.sarmentero@canonical.com>
19
 
 */
20
 
 
21
 
#include <QProcessEnvironment>
22
 
#include <ubuntu/download_manager/download_struct.h>
23
 
#include <ubuntu/download_manager/error.h>
24
 
 
25
 
#include "download_tracker.h"
26
 
#include "network.h"
27
 
 
28
 
namespace {
29
 
    const QString DOWNLOAD_COMMAND = "post-download-command";
30
 
    const QString PACKAGE_NAME = "package-name";
31
 
    const QString TITLE = "title";
32
 
    const QString SHOW_IN_INDICATOR = "indicator-shown";
33
 
    const QString PKCON_COMMAND = "pkcon";
34
 
    const QString DOWNLOAD_MANAGER_SHA512 = "sha512";
35
 
}
36
 
 
37
 
namespace UpdatePlugin {
38
 
 
39
 
DownloadTracker::DownloadTracker(QObject *parent) :
40
 
    QObject(parent),
41
 
    m_clickToken(QString::null),
42
 
    m_downloadUrl(QString::null),
43
 
    m_download(nullptr),
44
 
    m_manager(nullptr),
45
 
    m_progress(0)
46
 
{
47
 
}
48
 
 
49
 
void DownloadTracker::setDownload(const QString& url)
50
 
{
51
 
    if (!url.isEmpty()) {
52
 
        m_downloadUrl = url;
53
 
        startService();
54
 
    }
55
 
}
56
 
 
57
 
void DownloadTracker::setClickToken(const QString& token)
58
 
{
59
 
    if (!token.isEmpty()) {
60
 
        m_clickToken = token;
61
 
        startService();
62
 
    }
63
 
}
64
 
 
65
 
void DownloadTracker::setPackageName(const QString& package)
66
 
{
67
 
    if (!package.isEmpty()) {
68
 
        m_packageName = package;
69
 
        startService();
70
 
    }
71
 
}
72
 
 
73
 
void DownloadTracker::setTitle(const QString& title)
74
 
{
75
 
    if (!title.isEmpty()) {
76
 
        m_title = title;
77
 
        startService();
78
 
    }
79
 
}
80
 
 
81
 
void DownloadTracker::startService()
82
 
{
83
 
    if (!m_clickToken.isEmpty() && !m_downloadUrl.isEmpty() && !m_packageName.isEmpty() && !m_title.isEmpty()) {
84
 
        if (m_manager == nullptr) {
85
 
            m_manager = Manager::createSessionManager("", this);
86
 
 
87
 
            if (!connect(m_manager, &Manager::downloadCreated,
88
 
                this, &DownloadTracker::bindDownload)) {
89
 
                qWarning() << "Could not connect to Manager::downloadCreated!";
90
 
            }
91
 
        }
92
 
        QVariantMap vmap;
93
 
        QStringList args;
94
 
        QString command = getPkconCommand();
95
 
        args << command << "-p" << "install-local" << "$file";
96
 
        vmap[DOWNLOAD_COMMAND] = args;
97
 
        vmap[PACKAGE_NAME] = m_packageName;
98
 
        vmap[TITLE] = m_title;
99
 
        vmap[SHOW_IN_INDICATOR] = m_show_in_indicator;
100
 
        StringMap map;
101
 
        map[X_CLICK_TOKEN] = m_clickToken;
102
 
        DownloadStruct dstruct(m_downloadUrl, m_download_sha512, DOWNLOAD_MANAGER_SHA512, vmap, map);
103
 
        m_manager->createDownload(dstruct);
104
 
    }
105
 
}
106
 
 
107
 
void DownloadTracker::bindDownload(Download* download)
108
 
{
109
 
    m_download = download;
110
 
    if (!connect(m_download, &Download::finished,
111
 
            this, &DownloadTracker::onDownloadFinished)) {
112
 
        qWarning() << "Could not connect to Download::finished";
113
 
    }
114
 
    if (!connect(m_download, &Download::canceled,
115
 
            this, &DownloadTracker::onDownloadCanceled)) {
116
 
        qWarning() << "Could not connect to Download::canceled";
117
 
    }
118
 
    if (!connect(m_download, &Download::paused,
119
 
            this, &DownloadTracker::paused)) {
120
 
        qWarning() << "Could not connect to Download::paused";
121
 
    }
122
 
    if (!connect(m_download, &Download::resumed,
123
 
            this, &DownloadTracker::resumed)) {
124
 
        qWarning() << "Could not connect to Download::resumed";
125
 
    }
126
 
    if (!connect(m_download, &Download::started,
127
 
            this, &DownloadTracker::started)) {
128
 
        qWarning() << "Could not connect to Download::started";
129
 
    }
130
 
    if (!connect(m_download, static_cast<void(Download::*)(Error*)>(&Download::error),
131
 
            this, &DownloadTracker::registerError)) {
132
 
        qWarning() << "Could not connect to Download::error";
133
 
    }
134
 
 
135
 
    if (!connect(m_download, static_cast<void(Download::*)(qulonglong, qulonglong)>(&Download::progress),
136
 
            this, &DownloadTracker::setProgress)) {
137
 
        qWarning() << "Could not connect to Download::progress";
138
 
    }
139
 
 
140
 
    if (!connect(m_download, &Download::processing,
141
 
            this, &DownloadTracker::processing)) {
142
 
        qWarning() << "Could not connect to Download::processing";
143
 
    }
144
 
 
145
 
    m_download->start();
146
 
}
147
 
 
148
 
void DownloadTracker::registerError(Error* error)
149
 
{
150
 
    Q_EMIT errorFound(error->errorString());
151
 
 
152
 
    // we need to ensure that the resources are cleaned
153
 
    m_download->deleteLater();
154
 
    m_download = nullptr;
155
 
}
156
 
 
157
 
void DownloadTracker::onDownloadFinished(const QString& path)
158
 
{
159
 
    // once a download is finished we need to clean the resources
160
 
    m_download->deleteLater();
161
 
    m_download = nullptr;
162
 
    Q_EMIT finished(path);
163
 
}
164
 
 
165
 
void DownloadTracker::onDownloadCanceled(bool wasCanceled)
166
 
{
167
 
    if (wasCanceled) {
168
 
        m_download->deleteLater();
169
 
        m_download = nullptr;
170
 
    }
171
 
    Q_EMIT canceled(wasCanceled);
172
 
}
173
 
 
174
 
void DownloadTracker::pause()
175
 
{
176
 
    if (m_download != nullptr) {
177
 
        m_download->pause();
178
 
    }
179
 
}
180
 
 
181
 
void DownloadTracker::resume()
182
 
{
183
 
    if (m_download != nullptr) {
184
 
        m_download->resume();
185
 
    }
186
 
}
187
 
 
188
 
QString DownloadTracker::getPkconCommand()
189
 
{
190
 
    QProcessEnvironment environment = QProcessEnvironment::systemEnvironment();
191
 
    QString command = environment.value("PKCON_COMMAND", QString(PKCON_COMMAND));
192
 
    return command;
193
 
}
194
 
 
195
 
void DownloadTracker::setProgress(qulonglong received, qulonglong total)
196
 
{
197
 
    if (total > 0) {
198
 
        qulonglong result = (received * 100);
199
 
        m_progress = static_cast<int>(result / total);
200
 
        emit progressChanged();
201
 
    }
202
 
}
203
 
 
204
 
}