~unity-team/ubuntu-system-settings/welcome-wizard

« back to all changes in this revision

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

  • Committer: Michael Terry
  • Date: 2014-02-13 15:27:47 UTC
  • mfrom: (485.1.129 ubuntu-system-settings)
  • Revision ID: michael.terry@canonical.com-20140213152747-dwu4r5z4o5zesqvd
MergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2013 Canonical Ltd
 
2
 * Copyright (C) 2014 Canonical Ltd
3
3
 *
4
4
 * This program is free software: you can redistribute it and/or modify
5
5
 * it under the terms of the GNU General Public License version 3 as
14
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
15
 *
16
16
 * Authors:
17
 
 * Didier Roche <didier.roche@canonical.com>
 
17
 * Diego Sarmentero <diego.sarmentero@canonical.com>
18
18
 *
19
19
*/
20
20
 
21
21
#include "update.h"
22
 
#include <QDebug>
23
 
#include <QEvent>
24
 
#include <QDBusReply>
25
 
#include <unistd.h>
 
22
#include <QStringList>
 
23
#include <apt-pkg/debversion.h>
26
24
 
27
 
// FIXME: need to do this better including #include "../../src/i18n.h"
28
 
// and linking to it
29
 
#include <libintl.h>
30
 
QString _(const char *text)
31
 
{
32
 
    return QString::fromUtf8(dgettext(0, text));
33
 
}
 
25
namespace UpdatePlugin {
34
26
 
35
27
Update::Update(QObject *parent) :
36
28
    QObject(parent),
37
 
    m_infoMessage(""),
38
 
    m_downloadMode(-1),
39
 
    m_currentBuildNumber(-1),
40
 
    m_systemBusConnection (QDBusConnection::systemBus()),
41
 
    m_SystemServiceIface ("com.canonical.SystemImage",
42
 
                         "/Service",
43
 
                         "com.canonical.SystemImage",
44
 
                         m_systemBusConnection)
45
 
{
46
 
 
47
 
    connect(&m_SystemServiceIface, SIGNAL(UpdateAvailableStatus(bool, bool, QString, int, QString, QString)),
48
 
               this, SLOT(ProcessAvailableStatus(bool, bool, QString, int, QString, QString)));
49
 
    // signals to forward directly to QML
50
 
    connect(&m_SystemServiceIface, SIGNAL(UpdateProgress(int, double)),
51
 
                this, SIGNAL(updateProgress(int, double)));
52
 
    connect(&m_SystemServiceIface, SIGNAL(UpdatePaused(int)),
53
 
                this, SIGNAL(updatePaused(int)));
54
 
    connect(&m_SystemServiceIface, SIGNAL(UpdateDownloaded()),
55
 
                this, SIGNAL(updateDownloaded()));
56
 
    connect(&m_SystemServiceIface, SIGNAL(UpdateFailed(int, QString)),
57
 
                this, SIGNAL(updateFailed(int, QString)));
58
 
    connect(&m_SystemServiceIface, SIGNAL(SettingChanged(QString, QString)),
59
 
                this, SLOT(ProcessSettingChanged(QString, QString)));
60
 
 
61
 
    this->CheckForUpdate();
62
 
 
63
 
}
64
 
 
65
 
Update::~Update() {
66
 
}
67
 
 
68
 
void Update::CheckForUpdate() {
69
 
    m_SystemServiceIface.asyncCall("CheckForUpdate");
70
 
}
71
 
 
72
 
void Update::DownloadUpdate() {
73
 
    m_SystemServiceIface.asyncCall("DownloadUpdate");
74
 
}
75
 
 
76
 
QString Update::ApplyUpdate() {
77
 
    QDBusReply<QString> reply = m_SystemServiceIface.call("ApplyUpdate");
78
 
    if (reply.isValid())
79
 
        return reply.value();
80
 
    return _("Can't apply the current update (can't contact service)");
81
 
}
82
 
 
83
 
QString Update::CancelUpdate() {
84
 
    QDBusReply<QString> reply = m_SystemServiceIface.call("CancelUpdate");
85
 
    if (reply.isValid())
86
 
        return reply.value();
87
 
    return _("Can't cancel current request (can't contact service)");
88
 
}
89
 
 
90
 
QString Update::PauseDownload() {
91
 
    QDBusReply<QString> reply = m_SystemServiceIface.call("PauseDownload");
92
 
    if (reply.isValid())
93
 
        return reply.value();
94
 
    return _("Can't pause current request (can't contact service)");
95
 
}
96
 
 
97
 
QString Update::InfoMessage() {
98
 
    return m_infoMessage;
99
 
}
100
 
 
101
 
void Update::SetInfoMessage(QString infoMessage) {
102
 
    m_infoMessage = infoMessage;
103
 
    Q_EMIT infoMessageChanged();
104
 
}
105
 
 
106
 
int Update::currentBuildNumber() {
107
 
    if (m_currentBuildNumber != -1)
108
 
        return m_currentBuildNumber;
109
 
 
110
 
    QDBusReply<int> reply = m_SystemServiceIface.call("Info");
111
 
    if (reply.isValid())
112
 
        m_currentBuildNumber = reply.value();
113
 
 
114
 
    return m_currentBuildNumber;
115
 
}
116
 
 
117
 
int Update::DownloadMode() {
118
 
    if (m_downloadMode != -1)
119
 
        return m_downloadMode;
120
 
 
121
 
    QDBusReply<QString> reply = m_SystemServiceIface.call("GetSetting", "auto_download");
122
 
    int default_mode = 1;
123
 
    if (reply.isValid()) {
124
 
        bool ok;
125
 
        int result;
126
 
        result = reply.value().toInt(&ok);
127
 
        if (ok)
128
 
            m_downloadMode = result;
129
 
        else
130
 
            m_downloadMode = default_mode;
131
 
    }
132
 
    else
133
 
        m_downloadMode = default_mode;
134
 
    return m_downloadMode;
135
 
}
136
 
 
137
 
void Update::SetDownloadMode(int value) {
138
 
    if (m_downloadMode == value)
139
 
        return;
140
 
 
141
 
    m_downloadMode = value;
142
 
    m_SystemServiceIface.asyncCall("SetSetting", "auto_download", QString::number(value));
143
 
}
144
 
 
145
 
void Update::ProcessSettingChanged(QString key, QString newvalue) {
146
 
    if(key == "auto_download") {
147
 
        bool ok;
148
 
        int newintValue;
149
 
        newintValue = newvalue.toInt(&ok);
150
 
        if (ok) {
151
 
            m_downloadMode = newintValue;
152
 
            Q_EMIT downloadModeChanged();
153
 
        }
154
 
    }
155
 
}
156
 
 
157
 
QString Update::TranslateFromBackend(QString msg) {
158
 
    // TODO: load in the backend context .mo file
159
 
    return msg;
160
 
}
161
 
 
162
 
void Update::ProcessAvailableStatus(bool isAvailable, bool downloading, QString availableVersion, int updateSize, QString lastUpdateDate, QString errorReason)
163
 
{
164
 
    //TODO: bind with the real description once we can listen to the signal
165
 
    QStringList descriptions;
166
 
    descriptions.append("Description for update1");
167
 
    descriptions.append("Description for update2");
168
 
    descriptions.append("Description for update3");
169
 
 
170
 
    Q_EMIT updateAvailableStatus(isAvailable, downloading, availableVersion, updateSize, lastUpdateDate, descriptions, errorReason);
171
 
}
172
 
 
 
29
    m_binary_filesize(0),
 
30
    m_download_progress(0),
 
31
    m_error(""),
 
32
    m_icon_url(""),
 
33
    m_lastUpdateDate(""),
 
34
    m_local_version(""),
 
35
    m_packagename(""),
 
36
    m_remote_version(""),
 
37
    m_selected(false),
 
38
    m_systemUpdate(false),
 
39
    m_title(""),
 
40
    m_update(false),
 
41
    m_update_ready(false),
 
42
    m_update_state(false)
 
43
{
 
44
}
 
45
 
 
46
Update::~Update()
 
47
{
 
48
}
 
49
 
 
50
void Update::initializeApplication(QString packagename, QString title,
 
51
                                   QString version)
 
52
{
 
53
    m_packagename = packagename;
 
54
    m_title = title;
 
55
    m_local_version = version;
 
56
}
 
57
 
 
58
void Update::setRemoteVersion(QString& version)
 
59
{
 
60
    m_remote_version = version;
 
61
 
 
62
    int result = debVS.CmpVersion(m_local_version.toUtf8().data(),
 
63
                                  m_remote_version.toUtf8().data());
 
64
 
 
65
    m_update = result < 0;
 
66
}
 
67
 
 
68
void Update::setError(QString error)
 
69
{
 
70
    m_error = error;
 
71
    if (!m_error.isEmpty()) {
 
72
        Q_EMIT errorChanged();
 
73
    }
 
74
}
 
75
 
 
76
void Update::setSystemUpdate(bool isSystem)
 
77
{
 
78
    m_systemUpdate = isSystem;
 
79
    Q_EMIT systemUpdateChanged();
 
80
}
 
81
 
 
82
void Update::setUpdateState(bool state)
 
83
{
 
84
    m_update_state = state;
 
85
    Q_EMIT updatesStateChanged();
 
86
}
 
87
 
 
88
void Update::setUpdateReady(bool ready)
 
89
{
 
90
    m_update_ready = ready;
 
91
    Q_EMIT updatesReadyChanged();
 
92
}
 
93
 
 
94
void Update::setSelected(bool value)
 
95
{
 
96
    m_selected = value;
 
97
    Q_EMIT selectedChanged();
 
98
}
 
99
 
 
100
void Update::setBinaryFilesize(int size)
 
101
{
 
102
    m_binary_filesize = size;
 
103
    Q_EMIT binaryFilesizeChanged();
 
104
}
 
105
 
 
106
void Update::setIconUrl(QString icon) {
 
107
    m_icon_url = icon;
 
108
    Q_EMIT iconUrlChanged();
 
109
}
 
110
 
 
111
void Update::setLastUpdateDate(const QString date)
 
112
{
 
113
    m_lastUpdateDate = date;
 
114
    Q_EMIT lastUpdateDateChanged();
 
115
}
 
116
 
 
117
void Update::setDownloadProgress(int progress)
 
118
{
 
119
    m_download_progress = progress;
 
120
    Q_EMIT downloadProgressChanged();
 
121
}
 
122
 
 
123
}