Unity 8
System.cpp
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  */
16 
17 #include "System.h"
18 
19 #include <QDBusInterface>
20 #include <QDBusMetaType>
21 #include <QDir>
22 #include <QFile>
23 #include <QLocale>
24 #include <QMap>
25 #include <QProcess>
26 
27 System::System()
28  : QObject(),
29  m_fsWatcher()
30 {
31  // Register the argument needed for UpdateActivationEnvironment below
32  qDBusRegisterMetaType<QMap<QString,QString>>();
33 
34  m_fsWatcher.addPath(wizardEnabledPath());
35  connect(&m_fsWatcher, SIGNAL(fileChanged(const QString &)),
36  this, SIGNAL(wizardEnabledChanged()));
37 }
38 
39 QString System::wizardEnabledPath()
40 {
41  // Uses ubuntu-system-settings namespace for historic compatibility reasons
42  return QDir::home().filePath(".config/ubuntu-system-settings/wizard-has-run");
43 }
44 
45 bool System::wizardEnabled() const
46 {
47  return !QFile::exists(wizardEnabledPath());
48 }
49 
50 void System::setWizardEnabled(bool enabled)
51 {
52  if (wizardEnabled() == enabled)
53  return;
54 
55  if (enabled) {
56  QFile::remove(wizardEnabledPath());
57  } else {
58  QDir(wizardEnabledPath()).mkpath("..");
59  QFile(wizardEnabledPath()).open(QIODevice::WriteOnly);
60  m_fsWatcher.addPath(wizardEnabledPath());
61  wizardEnabledChanged();
62  }
63 }
64 
65 void System::setSessionVariable(const QString &variable, const QString &value)
66 {
67  // We need to update both upstart's and DBus's environment
68  QProcess::execute(QString("initctl set-env --global %1=%2").arg(variable, value));
69 
70  QDBusInterface iface("org.freedesktop.DBus",
71  "/org/freedesktop/DBus",
72  "org.freedesktop.DBus",
73  QDBusConnection::sessionBus());
74 
75  QMap<QString,QString> valueMap;
76  valueMap.insert(variable, value);
77  iface.call("UpdateActivationEnvironment", QVariant::fromValue(valueMap));
78 }
79 
80 void System::updateSessionLanguage(const QString &locale)
81 {
82  QString language = locale.split(".")[0];
83 
84  setSessionVariable("LANGUAGE", language);
85  setSessionVariable("LANG", locale);
86  setSessionVariable("LC_ALL", locale);
87 
88  // QLocale caches the default locale on startup, and Qt uses that cached
89  // copy when formatting dates. So manually update it here.
90  QLocale::setDefault(QLocale(locale));
91 
92  // Restart bits of the session to pick up new language.
93  QProcess::startDetached("sh -c \"initctl emit indicator-services-end; \
94  initctl stop scope-registry; \
95  initctl stop smart-scopes-proxy; \
96  initctl emit --no-wait indicator-services-start; \
97  initctl restart --no-wait maliit-server; \
98  initctl restart --no-wait unity8-dash\"");
99 }