on 700e2bd help sign in
unity8/plugins/Wizard/System.cpp
182 lines 5.7 kB history raw
1/*
2 * Copyright (C) 2018 The UBports project
3 * Copyright (C) 2014-2016 Canonical Ltd.
4 *
5 * This program is free software: you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 3, as published
7 * by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranties of
11 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12 * PURPOSE. See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
18#include "System.h"
20#include <QDBusPendingCall>
21#include <QDBusMessage>
22#include <QDBusConnection>
23#include <QDBusMetaType>
24#include <QDir>
25#include <QFile>
26#include <QLocale>
27#include <QMap>
28#include <QProcess>
29#include <QDebug>
30#include <QSettings>
32System::System()
33 : QObject()
35 // Register the argument needed for UpdateActivationEnvironment below
36 qDBusRegisterMetaType<QMap<QString,QString>>();
38 if(!wizardEnabled()) {
39 m_fsWatcher.addPath(wizardEnabledPath());
40 }
41 connect(&m_fsWatcher, &QFileSystemWatcher::fileChanged, this, &System::watcherFileChanged);
44QString System::wizardEnabledPath()
46 // Uses ubuntu-system-settings namespace for historic compatibility reasons
47 return QDir::home().filePath(QStringLiteral(".config/ubuntu-system-settings/wizard-has-run"));
50QString System::currentFrameworkPath()
52 QFileInfo f("/usr/share/click/frameworks/current");
53 return f.canonicalFilePath();
56/*
57wizardEnabled and isUpdate logic
59if wizard-has-run does NOT exist == is new install
60if wizard-has-run exists but does NOT match current framework == is update
61if wizard-has-run exists but does match current framework == show no wizard
62*/
64bool System::wizardPathExists() {
65 return QFile::exists(wizardEnabledPath());
68bool System::wizardEnabled() const
70 if (!wizardPathExists()) {
71 return true;
72 }
73 return isUpdate();
76QString System::readCurrentFramework()
78 QFile f(currentFrameworkPath());
79 if (!f.open(QFile::ReadOnly | QFile::Text)) return "";
80 QTextStream in(&f);
81 return in.readAll();
84QString System::readWizardEnabled()
86 QFile f(wizardEnabledPath());
87 if (!f.open(QFile::ReadOnly | QFile::Text)) return "";
88 QTextStream in(&f);
89 return in.readAll();
92QString System::version() const
94 return readCurrentFramework();
97bool System::isUpdate() const
99 if (!wizardPathExists()) {
100 return false;
101 }
103 return readCurrentFramework() != readWizardEnabled();
106void System::setWizardEnabled(bool enabled)
108 if (wizardEnabled() == enabled && !isUpdate())
109 return;
111 if (enabled) {
112 QFile::remove(wizardEnabledPath());
113 } else {
114 QDir(wizardEnabledPath()).mkpath(QStringLiteral(".."));
115 if (QFile::exists(wizardEnabledPath())) {
116 QFile::remove(wizardEnabledPath());
117 }
118 // For special cases check if wizardEnabledPath is a folder
119 if (QDir(wizardEnabledPath()).exists()) {
120 QDir(wizardEnabledPath()).removeRecursively();
121 }
122 if (!QFile::copy(currentFrameworkPath(), wizardEnabledPath())) {
123 // Make en empty file if framework does not exist
124 QFile f(wizardEnabledPath());
125 f.open(QFile::WriteOnly);
126 }
127 m_fsWatcher.addPath(wizardEnabledPath());
128 Q_EMIT wizardEnabledChanged();
129 Q_EMIT isUpdateChanged();
130 }
133void System::watcherFileChanged()
135 Q_EMIT wizardEnabledChanged();
136 Q_EMIT isUpdateChanged();
137 m_fsWatcher.removePath(wizardEnabledPath());
140void System::setSessionVariable(const QString &variable, const QString &value)
142 // We need to update both upstart's and DBus's environment
143 QProcess::startDetached(QStringLiteral("initctl set-env --global %1=%2").arg(variable, value));
145 QMap<QString,QString> valueMap;
146 valueMap.insert(variable, value);
148 QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.DBus"),
149 QStringLiteral("/org/freedesktop/DBus"),
150 QStringLiteral("org.freedesktop.DBus"),
151 QStringLiteral("UpdateActivationEnvironment"));
153 msg << QVariant::fromValue(valueMap);
154 QDBusConnection::sessionBus().asyncCall(msg);
157void System::updateSessionLocale(const QString &locale)
159 const QString language = locale.split(QStringLiteral("."))[0];
161 setSessionVariable(QStringLiteral("LANGUAGE"), language);
162 setSessionVariable(QStringLiteral("LANG"), locale);
163 setSessionVariable(QStringLiteral("LC_ALL"), locale);
165 // QLocale caches the default locale on startup, and Qt uses that cached
166 // copy when formatting dates. So manually update it here.
167 QLocale::setDefault(QLocale(locale));
169 // Restart bits of the session to pick up new language.
170 QProcess::startDetached(QStringLiteral("sh -c \"initctl emit indicator-services-end; \
171 initctl emit --no-wait indicator-services-start; \
172 initctl restart --no-wait ubuntu-location-service-trust-stored; \
173 initctl restart --no-wait maliit-server; \
174 initctl restart --no-wait indicator-messages"));
177void System::skipUntilFinishedPage()
179 QSettings settings;
180 settings.setValue(QStringLiteral("Wizard/SkipUntilFinishedPage"), true);
181 settings.sync();