on ded455e help sign in
unity8/plugins/Wizard/System.cpp
102 lines 3.6 kB history raw
1/*
2 * Copyright (C) 2014 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "System.h"
19#include <QDBusPendingCall>
20#include <QDBusMessage>
21#include <QDBusConnection>
22#include <QDBusMetaType>
23#include <QDir>
24#include <QFile>
25#include <QLocale>
26#include <QMap>
27#include <QProcess>
29System::System()
30 : QObject(),
31 m_fsWatcher()
33 // Register the argument needed for UpdateActivationEnvironment below
34 qDBusRegisterMetaType<QMap<QString,QString>>();
36 m_fsWatcher.addPath(wizardEnabledPath());
37 connect(&m_fsWatcher, &QFileSystemWatcher::fileChanged, this, &System::wizardEnabledChanged);
40QString System::wizardEnabledPath()
42 // Uses ubuntu-system-settings namespace for historic compatibility reasons
43 return QDir::home().filePath(".config/ubuntu-system-settings/wizard-has-run");
46bool System::wizardEnabled() const
48 return !QFile::exists(wizardEnabledPath());
51void System::setWizardEnabled(bool enabled)
53 if (wizardEnabled() == enabled)
54 return;
56 if (enabled) {
57 QFile::remove(wizardEnabledPath());
58 } else {
59 QDir(wizardEnabledPath()).mkpath("..");
60 QFile(wizardEnabledPath()).open(QIODevice::WriteOnly);
61 m_fsWatcher.addPath(wizardEnabledPath());
62 wizardEnabledChanged();
63 }
66void System::setSessionVariable(const QString &variable, const QString &value)
68 // We need to update both upstart's and DBus's environment
69 QProcess::startDetached(QStringLiteral("initctl set-env --global %1=%2").arg(variable, value));
71 QMap<QString,QString> valueMap;
72 valueMap.insert(variable, value);
74 QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.DBus",
75 "/org/freedesktop/DBus",
76 "org.freedesktop.DBus",
77 "UpdateActivationEnvironment");
79 msg << QVariant::fromValue(valueMap);
80 QDBusConnection::sessionBus().asyncCall(msg);
83void System::updateSessionLanguage(const QString &locale)
85 const QString language = locale.split(".")[0];
87 setSessionVariable("LANGUAGE", language);
88 setSessionVariable("LANG", locale);
89 setSessionVariable("LC_ALL", locale);
91 // QLocale caches the default locale on startup, and Qt uses that cached
92 // copy when formatting dates. So manually update it here.
93 QLocale::setDefault(QLocale(locale));
95 // Restart bits of the session to pick up new language.
96 QProcess::startDetached("sh -c \"initctl emit indicator-services-end; \
97 initctl stop scope-registry; \
98 initctl stop smart-scopes-proxy; \
99 initctl emit --no-wait indicator-services-start; \
100 initctl restart --no-wait maliit-server; \
101 initctl restart --no-wait unity8-dash\"");