~cimi/ubuntu-system-settings/wizard.privacy

« back to all changes in this revision

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

  • Committer: Andrea Cimitan
  • Date: 2014-04-11 12:07:23 UTC
  • mfrom: (662.2.47 wizard.wifi)
  • Revision ID: andrea.cimitan@gmail.com-20140411120723-hzbv5ekjurprc6uw
* Bump version for new package ubuntu-system-settings-wizard
[ Andrea Cimitan ]
* Detect SIM and if negative show no sim page
[ Victor R. Ruiz ]
* Autopilot: Move launch_system_settings to a helper module.
[ Iain Lane ]
* Hide 'Serial' and 'IMEI' entries when the info isn't available
* Don't look for PkgConfig explicitly, breaks with Multi-Arch.
  FindPkgConfig.cmake does this for us anyway.
[ CI bot ]
* Resync trunk
[ Diego Sarmentero ]
* Add click updates support. .

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2014 - 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 "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>
 
26
 
 
27
#define DOWNLOAD_COMMAND "post-download-command"
 
28
#define APP_ID "app_id"
 
29
#define PKCON_COMMAND "pkcon"
 
30
 
 
31
namespace UpdatePlugin {
 
32
 
 
33
DownloadTracker::DownloadTracker(QObject *parent) :
 
34
    QObject(parent),
 
35
    m_clickToken(""),
 
36
    m_downloadUrl(""),
 
37
    m_download(nullptr),
 
38
    m_manager(nullptr),
 
39
    m_progress(0)
 
40
{
 
41
}
 
42
 
 
43
void DownloadTracker::setDownload(const QString& url)
 
44
{
 
45
    if (url != "") {
 
46
        m_downloadUrl = url;
 
47
        startService();
 
48
    }
 
49
}
 
50
 
 
51
void DownloadTracker::setClickToken(const QString& token)
 
52
{
 
53
    if (token != "") {
 
54
        m_clickToken = token;
 
55
        startService();
 
56
    }
 
57
}
 
58
 
 
59
void DownloadTracker::setPackageName(const QString& package)
 
60
{
 
61
    if (package != "") {
 
62
        m_packageName = package;
 
63
        startService();
 
64
    }
 
65
}
 
66
 
 
67
void DownloadTracker::startService()
 
68
{
 
69
    if (!m_clickToken.isEmpty() && !m_downloadUrl.isEmpty() && !m_packageName.isEmpty()) {
 
70
        if (m_manager == nullptr) {
 
71
            m_manager = Manager::createSessionManager("", this);
 
72
 
 
73
            QObject::connect(m_manager, SIGNAL(downloadCreated(Download*)),
 
74
                             this, SLOT(bindDownload(Download*)));
 
75
        }
 
76
        QVariantMap vmap;
 
77
        QStringList args;
 
78
        QString command = getPkconCommand();
 
79
        args << command << "-p" << "install-local" << "$file";
 
80
        vmap[DOWNLOAD_COMMAND] = args;
 
81
        vmap[APP_ID] = m_packageName;
 
82
        StringMap map;
 
83
        map[X_CLICK_TOKEN] = m_clickToken;
 
84
        DownloadStruct dstruct = DownloadStruct(m_downloadUrl, vmap, map);
 
85
        m_manager->createDownload(dstruct);
 
86
    }
 
87
}
 
88
 
 
89
void DownloadTracker::bindDownload(Download* download)
 
90
{
 
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)));
 
98
 
 
99
    m_download->start();
 
100
}
 
101
 
 
102
void DownloadTracker::registerError(Error* error)
 
103
{
 
104
    Q_EMIT errorFound(error->errorString());
 
105
}
 
106
 
 
107
void DownloadTracker::pause()
 
108
{
 
109
    if (m_download != nullptr) {
 
110
        m_download->pause();
 
111
    }
 
112
}
 
113
 
 
114
void DownloadTracker::resume()
 
115
{
 
116
    if (m_download != nullptr) {
 
117
        m_download->resume();
 
118
    }
 
119
}
 
120
 
 
121
QString DownloadTracker::getPkconCommand()
 
122
{
 
123
    QProcessEnvironment environment = QProcessEnvironment::systemEnvironment();
 
124
    QString command = environment.value("PKCON_COMMAND", QString(PKCON_COMMAND));
 
125
    return command;
 
126
}
 
127
 
 
128
void DownloadTracker::setProgress(qulonglong received, qulonglong total)
 
129
{
 
130
    if (total > 0) {
 
131
        qulonglong result = (received * 100);
 
132
        m_progress = static_cast<int>(result / total);
 
133
        emit progressChanged();
 
134
    }
 
135
}
 
136
 
 
137
}