on xenial help sign in
WIP: implement support for checking system updates
bd146a0Lukáš Tinkl
plugins/Ubuntu/SystemImage/SystemImage.cpp+84−5
@@ -1,5 +1,5 @@
1/*
1/*
2− * Copyright (C) 2014 Canonical, Ltd.
2+ * Copyright (C) 2014-2016 Canonical, Ltd.
3 *
3 *
4 * This program is free software; you can redistribute it and/or modify
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
5 * it under the terms of the GNU General Public License as published by
@@ -14,20 +14,99 @@
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
15 */
16
16
17−#include "SystemImage.h"
18#include <QDBusConnection>
17#include <QDBusConnection>
19#include <QDBusPendingCall>
18#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")
20
27
21SystemImage::SystemImage(QObject *parent)
28SystemImage::SystemImage(QObject *parent)
22 : QObject(parent)
29 : QObject(parent)
23{
30{
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);
24}
53}
25
54
26void SystemImage::factoryReset()
55void SystemImage::factoryReset()
27{
56{
28− const QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("com.canonical.SystemImage"),
57+ const QDBusMessage msg = QDBusMessage::createMethodCall(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE,
29− QStringLiteral("/Service"),
30− QStringLiteral("com.canonical.SystemImage"),
31 QStringLiteral("FactoryReset"));
58 QStringLiteral("FactoryReset"));
32 QDBusConnection::systemBus().asyncCall(msg);
59 QDBusConnection::systemBus().asyncCall(msg);
33}
60}
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+}
plugins/Ubuntu/SystemImage/SystemImage.h+44−2
@@ -1,5 +1,5 @@
1/*
1/*
2− * Copyright (C) 2014 Canonical, Ltd.
2+ * Copyright (C) 2014-2016 Canonical, Ltd.
3 *
3 *
4 * This program is free software; you can redistribute it and/or modify
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
5 * it under the terms of the GNU General Public License as published by
@@ -18,17 +18,59 @@
18#define SYSTEMIMAGE_H
18#define SYSTEMIMAGE_H
19
19
20#include <QObject>
20#include <QObject>
21+#include <QMap>
21
22
22class SystemImage : public QObject
23class SystemImage : public QObject
23{
24{
24 Q_OBJECT
25 Q_OBJECT
25 Q_DISABLE_COPY(SystemImage)
26 Q_DISABLE_COPY(SystemImage)
26
27
28+ Q_PROPERTY(bool updateAvailable READ updateAvailable NOTIFY updateAvailableStatus)
29+ Q_PROPERTY(bool updateDownloading READ updateDownloading NOTIFY updateAvailableStatus)
30+ Q_PROPERTY(QString availableVersion READ availableVersion NOTIFY updateAvailableStatus)
31+ Q_PROPERTY(int updateSize READ updateSize NOTIFY updateAvailableStatus)
32+
33+ Q_PROPERTY(bool updateDownloaded READ updateDownloaded NOTIFY updateDownloadedChanged)
34+
27public:
35public:
28− explicit SystemImage(QObject *parent = 0);
36+ explicit SystemImage(QObject *parent = nullptr);
29 ~SystemImage() = default;
37 ~SystemImage() = default;
30
38
39+ bool updateAvailable() const { return m_updateAvailable; }
40+ bool updateDownloading() const { return m_downloading; }
41+ QString availableVersion() const { return m_availableVersion; }
42+ int updateSize() const { return m_updateSize; }
43+
44+ bool updateDownloaded() const { return m_downloaded; }
45+
46+public Q_SLOTS:
47+ Q_INVOKABLE void checkForUpdate();
48+ Q_INVOKABLE void applyUpdate();
31 Q_INVOKABLE void factoryReset();
49 Q_INVOKABLE void factoryReset();
50+
51+private Q_SLOTS:
52+ void onUpdateAvailableStatus(bool is_available, bool updateDownloading, const QString &available_version,
53+ int update_size, const QString &last_update_date, const QString &error_reason);
54+ void onUpdateDownloaded(bool downloaded);
55+ void onUpdateFailed(int consecutive_failure_count, const QString & last_reason);
56+ void onUpdateApplied(bool applied);
57+
58+Q_SIGNALS:
59+ void updateAvailableStatus();
60+ void updateDownloadedChanged();
61+
62+private:
63+ void resetUpdateStatus();
64+
65+private:
66+ bool m_updateAvailable = false;
67+ bool m_downloading = false;
68+ bool m_downloaded = false;
69+ QString m_availableVersion;
70+ int m_updateSize = -1;
71+ QString m_lastUpdateDate;
72+ QString m_errorReason;
73+ bool m_updateDownloaded;
32};
74};
33
75
34#endif // SYSTEMIMAGE_H
76#endif // SYSTEMIMAGE_H
plugins/Ubuntu/SystemImage/plugin.cpp+2−8
@@ -19,16 +19,10 @@
19
19
20#include <QtQml>
20#include <QtQml>
21
21
22−static QObject *service_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
23−{
24− Q_UNUSED(engine)
25− Q_UNUSED(scriptEngine)
26− return new SystemImage();
27−}
28−
29void BackendPlugin::registerTypes(const char *uri)
22void BackendPlugin::registerTypes(const char *uri)
30{
23{
31 Q_ASSERT(uri == QLatin1String("Ubuntu.SystemImage"));
24 Q_ASSERT(uri == QLatin1String("Ubuntu.SystemImage"));
32
25
33− qmlRegisterSingletonType<SystemImage>(uri, 0, 1, "SystemImage", service_provider);
26+ qmlRegisterSingletonType<SystemImage>(uri, 0, 1, "SystemImage",
27+ [](QQmlEngine*, QJSEngine*) -> QObject* { return new SystemImage; });
34}
28}
plugins/Ubuntu/SystemImage/plugin.h+1−1
@@ -22,7 +22,7 @@
22class BackendPlugin : public QQmlExtensionPlugin
22class BackendPlugin : public QQmlExtensionPlugin
23{
23{
24 Q_OBJECT
24 Q_OBJECT
25− Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
25+ Q_PLUGIN_METADATA(IID QQmlExtensionInterface_iid)
26
26
27public:
27public:
28 void registerTypes(const char *uri) override;
28 void registerTypes(const char *uri) override;