@@ -1,5 +1,5 @@
11/*
2− * Copyright (C) 2014 Canonical, Ltd.
2+ * Copyright (C) 2014-2016 Canonical, Ltd.
33 *
44 * This program is free software; you can redistribute it and/or modify
55 * it under the terms of the GNU General Public License as published by
@@ -14,20 +14,99 @@
1414 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1515 */
1616
17−#include "SystemImage.h"
1817#include <QDBusConnection>
1918#include <QDBusPendingCall>
19+#include <QDBusReply>
20+#include <QDebug>
21+
22+#include "SystemImage.h"
23+
24+#define SYSTEMIMAGE_SERVICE QStringLiteral("com.canonical.SystemImage")
25+#define SYSTEMIMAGE_PATH QStringLiteral("/Service")
26+#define SYSTEMIMAGE_IFACE QStringLiteral("com.canonical.SystemImage")
2027
2128SystemImage::SystemImage(QObject *parent)
2229 : QObject(parent)
2330{
31+ QDBusConnection::systemBus().connect(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE, QStringLiteral("UpdateAvailableStatus"),
32+ this, SLOT(onUpdateAvailableStatus(bool,bool,QString,int,QString,QString)));
33+ QDBusConnection::systemBus().connect(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE, QStringLiteral("UpdateDownloaded"),
34+ this, SLOT(onUpdateDownloaded(bool)));
35+ QDBusConnection::systemBus().connect(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE, QStringLiteral("UpdateFailed"),
36+ this, SLOT(onUpdateFailed(int,QString)));
37+ QDBusConnection::systemBus().connect(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE, QStringLiteral("Applied"),
38+ this, SLOT(onUpdateApplied(bool)));
39+}
40+
41+void SystemImage::checkForUpdate()
42+{
43+ const QDBusMessage msg = QDBusMessage::createMethodCall(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE,
44+ QStringLiteral("CheckForUpdate"));
45+ QDBusConnection::systemBus().asyncCall(msg);
46+}
47+
48+void SystemImage::applyUpdate()
49+{
50+ const QDBusMessage msg = QDBusMessage::createMethodCall(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE,
51+ QStringLiteral("ApplyUpdate"));
52+ QDBusConnection::systemBus().asyncCall(msg);
2453}
2554
2655void SystemImage::factoryReset()
2756{
28− const QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("com.canonical.SystemImage"),
29− QStringLiteral("/Service"),
30− QStringLiteral("com.canonical.SystemImage"),
57+ const QDBusMessage msg = QDBusMessage::createMethodCall(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE,
3158 QStringLiteral("FactoryReset"));
3259 QDBusConnection::systemBus().asyncCall(msg);
3360}
61+
62+void SystemImage::onUpdateAvailableStatus(bool is_available, bool downloading, const QString &available_version, int update_size,
63+ const QString &last_update_date, const QString &error_reason)
64+{
65+ qDebug() << Q_FUNC_INFO << "A new update is" << (is_available ? "" : "NOT") << "available";
66+
67+ if (is_available == m_updateAvailable) {
68+ return;
69+ }
70+
71+ m_updateAvailable = is_available;
72+ m_downloading = downloading;
73+ m_availableVersion = available_version;
74+ m_updateSize = update_size;
75+ m_lastUpdateDate = last_update_date;
76+ m_errorReason = error_reason;
77+ Q_EMIT updateAvailableStatus();
78+}
79+
80+void SystemImage::onUpdateDownloaded(bool downloaded)
81+{
82+ if (downloaded != m_downloaded) {
83+ m_downloaded = downloaded;
84+ Q_EMIT updateDownloadedChanged();
85+ }
86+}
87+
88+void SystemImage::onUpdateFailed(int consecutive_failure_count, const QString &last_reason)
89+{
90+ Q_UNUSED(consecutive_failure_count)
91+ qWarning() << Q_FUNC_INFO << "System Update failed:" << last_reason;
92+}
93+
94+void SystemImage::onUpdateApplied(bool applied)
95+{
96+ qDebug() << Q_FUNC_INFO << "System Update applied with status:" << applied;
97+ if (applied) {
98+ resetUpdateStatus();
99+ Q_EMIT updateAvailableStatus();
100+ }
101+}
102+
103+void SystemImage::resetUpdateStatus()
104+{
105+ m_updateAvailable = false;
106+ m_downloading = false;
107+ m_downloaded = false;
108+ m_availableVersion.clear();
109+ m_updateSize = -1;
110+ m_lastUpdateDate.clear();
111+ m_errorReason.clear();
112+}