Lomiri
Loading...
Searching...
No Matches
System.cpp
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 */
17
18#include "System.h"
19
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>
31#include <QStringBuilder>
32
33#include <glib.h>
34#include <libintl.h>
35
36System::System()
37 : QObject()
38{
39 // Register the argument needed for UpdateActivationEnvironment below
40 qDBusRegisterMetaType<QMap<QString,QString>>();
41
42 if(!wizardEnabled()) {
43 m_fsWatcher.addPath(wizardEnabledPath());
44 }
45 connect(&m_fsWatcher, &QFileSystemWatcher::fileChanged, this, &System::watcherFileChanged);
46}
47
48QString System::wizardEnabledPath()
49{
50 return QDir::home().filePath(QStringLiteral(".config/lomiri/wizard-has-run"));
51}
52
53QString System::currentFrameworkPath()
54{
55 QFileInfo f("/usr/share/click/frameworks/current");
56 return f.canonicalFilePath();
57}
58
59/*
60wizardEnabled and isUpdate logic
61
62if wizard-has-run does NOT exist == is new install
63if wizard-has-run exists but does NOT match current framework == is update
64if wizard-has-run exists but does match current framework == show no wizard
65*/
66
67bool System::wizardPathExists() {
68 return QFile::exists(wizardEnabledPath());
69}
70
71bool System::wizardEnabled() const
72{
73 if (!wizardPathExists()) {
74 return true;
75 }
76 return isUpdate();
77}
78
79QString System::readCurrentFramework()
80{
81 QFile f(currentFrameworkPath());
82 if (!f.open(QFile::ReadOnly | QFile::Text)) return "";
83 QTextStream in(&f);
84 return in.readAll();
85}
86
87QString System::readWizardEnabled()
88{
89 QFile f(wizardEnabledPath());
90 if (!f.open(QFile::ReadOnly | QFile::Text)) return "";
91 QTextStream in(&f);
92 return in.readAll();
93}
94
95QString System::version() const
96{
97 return readCurrentFramework();
98}
99
100bool System::isUpdate() const
101{
102 if (!wizardPathExists()) {
103 return false;
104 }
105
106 return readCurrentFramework() != readWizardEnabled();
107}
108
109void System::setWizardEnabled(bool enabled)
110{
111 if (wizardEnabled() == enabled && !isUpdate())
112 return;
113
114 if (enabled) {
115 QFile::remove(wizardEnabledPath());
116 } else {
117 QDir(wizardEnabledPath()).mkpath(QStringLiteral(".."));
118 if (QFile::exists(wizardEnabledPath())) {
119 QFile::remove(wizardEnabledPath());
120 }
121 // For special cases check if wizardEnabledPath is a folder
122 if (QDir(wizardEnabledPath()).exists()) {
123 QDir(wizardEnabledPath()).removeRecursively();
124 }
125 if (!QFile::copy(currentFrameworkPath(), wizardEnabledPath())) {
126 // Make en empty file if framework does not exist
127 QFile f(wizardEnabledPath());
128 f.open(QFile::WriteOnly);
129 }
130 m_fsWatcher.addPath(wizardEnabledPath());
131 Q_EMIT wizardEnabledChanged();
132 Q_EMIT isUpdateChanged();
133 }
134}
135
136void System::watcherFileChanged()
137{
138 Q_EMIT wizardEnabledChanged();
139 Q_EMIT isUpdateChanged();
140 m_fsWatcher.removePath(wizardEnabledPath());
141}
142
143void System::setSessionVariable(const QString &variable, const QString &value)
144{
145 // We need to update both systemd's and DBus's environment
146 QStringList vars = { variable % QChar('=') % value };
147 QDBusMessage systemdMsg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.systemd1"),
148 QStringLiteral("/org/freedesktop/systemd1"),
149 QStringLiteral("org.freedesktop.systemd1.Manager"),
150 QStringLiteral("SetEnvironment"));
151 systemdMsg << QVariant::fromValue(vars);
152 QDBusConnection::sessionBus().asyncCall(systemdMsg);
153
154 QMap<QString,QString> valueMap;
155 valueMap.insert(variable, value);
156
157 QDBusMessage dbusMsg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.DBus"),
158 QStringLiteral("/org/freedesktop/DBus"),
159 QStringLiteral("org.freedesktop.DBus"),
160 QStringLiteral("UpdateActivationEnvironment"));
161
162 dbusMsg << QVariant::fromValue(valueMap);
163 QDBusConnection::sessionBus().asyncCall(dbusMsg);
164}
165
166void System::restartUnit(const QString &unitName)
167{
168 QDBusMessage systemdMsg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.systemd1"),
169 QStringLiteral("/org/freedesktop/systemd1"),
170 QStringLiteral("org.freedesktop.systemd1.Manager"),
171 QStringLiteral("TryRestartUnit"));
172 systemdMsg << QVariant::fromValue(unitName);
173 systemdMsg << QVariant::fromValue(QStringLiteral("replace"));
174 QDBusConnection::sessionBus().asyncCall(systemdMsg);
175}
176
177void System::updateSessionLocale(const QString &locale)
178{
179 const QString language = locale.split(QStringLiteral("."))[0];
180
181 setSessionVariable(QStringLiteral("LANGUAGE"), language);
182 setSessionVariable(QStringLiteral("LANG"), locale);
183 setSessionVariable(QStringLiteral("LC_ALL"), locale);
184
185 // QLocale caches the default locale on startup, and Qt uses that cached
186 // copy when formatting dates. So manually update it here.
187 QLocale::setDefault(QLocale(locale));
188
189 // Restart bits of the session to pick up new language.
190 const QStringList units {
191 "lomiri-indicators.target",
192 "lomiri-location-service-trust-stored.service",
193 "pulseaudio-trust-stored.service",
194 "lomiri-sync-monitor.service",
195 "maliit-server.service",
196 "ciborium.service",
197 };
198 for (const QString& unit : units) {
199 restartUnit(unit);
200 }
201}
202
203void System::skipUntilFinishedPage()
204{
205 QSettings settings;
206 settings.setValue(QStringLiteral("Wizard/SkipUntilFinishedPage"), true);
207 settings.sync();
208}
209
210QString System::distroName() const
211{
212#ifdef LOMIRI_DISPLAYED_DISTRO_NAME
213 return QStringLiteral(LOMIRI_DISPLAYED_DISTRO_NAME);
214#else
215 g_autofree gchar * name = g_get_os_info(G_OS_INFO_KEY_NAME);
216 if (name)
217 return QString::fromUtf8(gettext("Lomiri on %1"))
218 .arg(QString::fromLocal8Bit(name));
219
220 return QStringLiteral("Lomiri");
221#endif
222}