on bc75a12 help sign in
unity8/plugins/Ubuntu/SystemImage/SystemImage.cpp
151 lines 5.5 kB history raw
1/*
2 * Copyright (C) 2014-2016 Canonical, Ltd.
3 *
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
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include <QDBusConnection>
18#include <QDBusPendingCall>
19#include <QDBusReply>
20#include <QDebug>
22#include <glib.h>
24#include "SystemImage.h"
26#define SYSTEMIMAGE_SERVICE QStringLiteral("com.canonical.SystemImage")
27#define SYSTEMIMAGE_PATH QStringLiteral("/Service")
28#define SYSTEMIMAGE_IFACE QStringLiteral("com.canonical.SystemImage")
30SystemImage::SystemImage(QObject *parent)
31 : QObject(parent)
33 QDBusConnection::systemBus().connect(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE, QStringLiteral("UpdateAvailableStatus"),
34 this, SLOT(onUpdateAvailableStatus(bool,bool,QString,int,QString,QString)));
35 QDBusConnection::systemBus().connect(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE, QStringLiteral("UpdateDownloaded"),
36 this, SLOT(onUpdateDownloaded()));
37 QDBusConnection::systemBus().connect(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE, QStringLiteral("UpdateFailed"),
38 this, SLOT(onUpdateFailed(int,QString)));
39 QDBusConnection::systemBus().connect(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE, QStringLiteral("Applied"),
40 this, SLOT(onUpdateApplied(bool)));
41 QDBusConnection::systemBus().connect(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE, QStringLiteral("Rebooting"),
42 this, SLOT(onRebooting(bool)));
45void SystemImage::checkForUpdate()
47 qDebug() << "!!! Checking for update";
48 const QDBusMessage msg = QDBusMessage::createMethodCall(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE,
49 QStringLiteral("CheckForUpdate"));
50 QDBusConnection::systemBus().asyncCall(msg);
53void SystemImage::applyUpdate()
55 qDebug() << "!!! Applying update";
56 setUpdateApplying(true);
58 const QDBusMessage msg = QDBusMessage::createMethodCall(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE,
59 QStringLiteral("ApplyUpdate"));
60 QDBusConnection::systemBus().asyncCall(msg);
63void SystemImage::factoryReset()
65 const QDBusMessage msg = QDBusMessage::createMethodCall(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE,
66 QStringLiteral("FactoryReset"));
67 QDBusConnection::systemBus().asyncCall(msg);
70void SystemImage::onUpdateAvailableStatus(bool is_available, bool downloading, const QString &available_version, int update_size,
71 const QString &last_update_date, const QString &error_reason)
73 qDebug() << Q_FUNC_INFO << "A new update is" << (is_available ? "" : "NOT") << "available";
75 if (is_available == m_updateAvailable) {
76 return;
77 }
79 m_updateAvailable = is_available;
80 m_downloading = downloading;
81 m_availableVersion = available_version;
82 m_updateSize = formatSize(update_size);
83 m_lastUpdateDate = last_update_date;
84 m_errorReason = error_reason;
85 Q_EMIT updateAvailableStatus();
87 qDebug() << "!!! Downloading:" << downloading << ", version:" << available_version << ", last update:" << last_update_date <<
88 ", size:" << m_updateSize;
91void SystemImage::onUpdateDownloaded()
93 qDebug() << "!!! Update downloaded";
95 m_downloaded = true;
96 Q_EMIT updateDownloadedChanged();
99void SystemImage::onUpdateFailed(int consecutive_failure_count, const QString &last_reason)
101 Q_UNUSED(consecutive_failure_count)
102 qWarning() << Q_FUNC_INFO << "System Update failed:" << last_reason;
103 setUpdateApplying(false);
106void SystemImage::onUpdateApplied(bool applied)
108 qDebug() << Q_FUNC_INFO << "System Update applied with status:" << applied;
109 setUpdateApplying(false);
110 if (applied) {
111 resetUpdateStatus();
112 Q_EMIT updateAvailableStatus();
113 }
116void SystemImage::onRebooting(bool status)
118 setUpdateApplying(false);
119 qDebug() << "!!! Rebooting:" << status;
122void SystemImage::setUpdateApplying(bool status)
124 if (status != m_updateApplying) {
125 m_updateApplying = status;
126 Q_EMIT updateApplyingChanged();
127 }
130void SystemImage::resetUpdateStatus()
132 m_updateAvailable = false;
133 m_updateApplying = false;
134 m_downloading = false;
135 m_downloaded = false;
136 m_availableVersion.clear();
137 m_updateSize.clear();
138 m_lastUpdateDate.clear();
139 m_errorReason.clear();
142QString SystemImage::formatSize(quint64 size) const
144 guint64 g_size = size;
146 gchar * formatted_size = g_format_size(g_size);
147 QString q_formatted_size = QString::fromLocal8Bit(formatted_size);
148 g_free(formatted_size);
150 return q_formatted_size;