on 669c2af help sign in
unity8/plugins/Ubuntu/SystemImage/SystemImage.cpp
155 lines 5.6 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")
30Q_LOGGING_CATEGORY(SYSTEMIMAGEPLUGIN, "unity8.systemimage", QtWarningMsg)
32#define DEBUG_MSG qCDebug(SYSTEMIMAGEPLUGIN).nospace().noquote() << Q_FUNC_INFO
33#define WARNING_MSG qCWarning(SYSTEMIMAGEPLUGIN).nospace().noquote() << Q_FUNC_INFO
35SystemImage::SystemImage(QObject *parent)
36 : QObject(parent)
38 QDBusConnection::systemBus().connect(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE, QStringLiteral("UpdateAvailableStatus"),
39 this, SLOT(onUpdateAvailableStatus(bool,bool,QString,int,QString,QString)));
40 QDBusConnection::systemBus().connect(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE, QStringLiteral("UpdateDownloaded"),
41 this, SLOT(onUpdateDownloaded()));
42 QDBusConnection::systemBus().connect(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE, QStringLiteral("UpdateFailed"),
43 this, SLOT(onUpdateFailed(int,QString)));
44 QDBusConnection::systemBus().connect(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE, QStringLiteral("Applied"),
45 this, SLOT(onUpdateApplied(bool)));
46 QDBusConnection::systemBus().connect(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE, QStringLiteral("Rebooting"),
47 this, SLOT(onRebooting(bool)));
50void SystemImage::checkForUpdate()
52 DEBUG_MSG << "Checking for update";
53 const QDBusMessage msg = QDBusMessage::createMethodCall(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE,
54 QStringLiteral("CheckForUpdate"));
55 QDBusConnection::systemBus().asyncCall(msg);
58void SystemImage::applyUpdate()
60 DEBUG_MSG << "Applying update";
61 setUpdateApplying(true);
63 const QDBusMessage msg = QDBusMessage::createMethodCall(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE,
64 QStringLiteral("ApplyUpdate"));
65 QDBusConnection::systemBus().asyncCall(msg);
68void SystemImage::factoryReset()
70 const QDBusMessage msg = QDBusMessage::createMethodCall(SYSTEMIMAGE_SERVICE, SYSTEMIMAGE_PATH, SYSTEMIMAGE_IFACE,
71 QStringLiteral("FactoryReset"));
72 QDBusConnection::systemBus().asyncCall(msg);
75void SystemImage::onUpdateAvailableStatus(bool is_available, bool downloading, const QString &available_version, int update_size,
76 const QString &last_update_date, const QString &error_reason)
78 DEBUG_MSG << "A new update is " << (is_available ? "" : "NOT") << "available";
80 if (is_available == m_updateAvailable) {
81 return;
82 }
84 m_updateAvailable = is_available;
85 m_downloading = downloading;
86 m_availableVersion = available_version;
87 m_updateSize = formatSize(update_size);
88 m_lastUpdateDate = last_update_date;
89 m_errorReason = error_reason;
90 Q_EMIT updateAvailableStatus();
92 DEBUG_MSG << "Downloading: " << downloading << ", version: " << available_version << ", last update: " << last_update_date <<
93 ", size: " << m_updateSize;
96void SystemImage::onUpdateDownloaded()
98 DEBUG_MSG << "Update downloaded";
100 m_downloaded = true;
101 Q_EMIT updateDownloadedChanged();
104void SystemImage::onUpdateFailed(int /*consecutive_failure_count*/, const QString &last_reason)
106 WARNING_MSG << "System Update failed: " << last_reason;
107 setUpdateApplying(false);
110void SystemImage::onUpdateApplied(bool applied)
112 DEBUG_MSG << "System Update applied with status: " << applied;
113 setUpdateApplying(false);
114 if (applied) {
115 resetUpdateStatus();
116 Q_EMIT updateAvailableStatus();
117 }
120void SystemImage::onRebooting(bool status)
122 setUpdateApplying(false);
123 DEBUG_MSG << "Rebooting: " << status;
126void SystemImage::setUpdateApplying(bool status)
128 if (status != m_updateApplying) {
129 m_updateApplying = status;
130 Q_EMIT updateApplyingChanged();
131 }
134void SystemImage::resetUpdateStatus()
136 m_updateAvailable = false;
137 m_updateApplying = false;
138 m_downloading = false;
139 m_downloaded = false;
140 m_availableVersion.clear();
141 m_updateSize.clear();
142 m_lastUpdateDate.clear();
143 m_errorReason.clear();
146QString SystemImage::formatSize(quint64 size) const
148 guint64 g_size = size;
150 gchar * formatted_size = g_format_size(g_size);
151 QString q_formatted_size = QString::fromLocal8Bit(formatted_size);
152 g_free(formatted_size);
154 return q_formatted_size;