on bcbf76a help sign in
unity8/plugins/Wizard/System.cpp
110 lines 3.9 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 if(!wizardEnabled()) {
37 m_fsWatcher.addPath(wizardEnabledPath());
38 }
39 connect(&m_fsWatcher, &QFileSystemWatcher::fileChanged, this, &System::watcherFileChanged);
42QString System::wizardEnabledPath()
44 // Uses ubuntu-system-settings namespace for historic compatibility reasons
45 return QDir::home().filePath(QStringLiteral(".config/ubuntu-system-settings/wizard-has-run"));
48bool System::wizardEnabled() const
50 return !QFile::exists(wizardEnabledPath());
53void System::setWizardEnabled(bool enabled)
55 if (wizardEnabled() == enabled)
56 return;
58 if (enabled) {
59 QFile::remove(wizardEnabledPath());
60 } else {
61 QDir(wizardEnabledPath()).mkpath(QStringLiteral(".."));
62 QFile(wizardEnabledPath()).open(QIODevice::WriteOnly);
63 m_fsWatcher.addPath(wizardEnabledPath());
64 Q_EMIT wizardEnabledChanged();
65 }
68void System::watcherFileChanged()
70 Q_EMIT wizardEnabledChanged();
71 m_fsWatcher.removePath(wizardEnabledPath());
74void System::setSessionVariable(const QString &variable, const QString &value)
76 // We need to update both upstart's and DBus's environment
77 QProcess::startDetached(QStringLiteral("initctl set-env --global %1=%2").arg(variable, value));
79 QMap<QString,QString> valueMap;
80 valueMap.insert(variable, value);
82 QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.DBus"),
83 QStringLiteral("/org/freedesktop/DBus"),
84 QStringLiteral("org.freedesktop.DBus"),
85 QStringLiteral("UpdateActivationEnvironment"));
87 msg << QVariant::fromValue(valueMap);
88 QDBusConnection::sessionBus().asyncCall(msg);
91void System::updateSessionLanguage(const QString &locale)
93 const QString language = locale.split(QStringLiteral("."))[0];
95 setSessionVariable(QStringLiteral("LANGUAGE"), language);
96 setSessionVariable(QStringLiteral("LANG"), locale);
97 setSessionVariable(QStringLiteral("LC_ALL"), locale);
99 // QLocale caches the default locale on startup, and Qt uses that cached
100 // copy when formatting dates. So manually update it here.
101 QLocale::setDefault(QLocale(locale));
103 // Restart bits of the session to pick up new language.
104 QProcess::startDetached(QStringLiteral("sh -c \"initctl emit indicator-services-end; \
105 initctl stop scope-registry; \
106 initctl stop smart-scopes-proxy; \
107 initctl emit --no-wait indicator-services-start; \
108 initctl restart --no-wait maliit-server; \
109 initctl restart --no-wait unity8-dash\""));