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 <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>
28 
29 System::System()
30  : QObject(),
31  m_fsWatcher()
32 {
33  // Register the argument needed for UpdateActivationEnvironment below
34  qDBusRegisterMetaType<QMap<QString,QString>>();
35 
36  m_fsWatcher.addPath(wizardEnabledPath());
37  connect(&m_fsWatcher, &QFileSystemWatcher::fileChanged, this, &System::wizardEnabledChanged);
38 }
39 
40 QString System::wizardEnabledPath()
41 {
42  // Uses ubuntu-system-settings namespace for historic compatibility reasons
43  return QDir::home().filePath(".config/ubuntu-system-settings/wizard-has-run");
44 }
45 
46 bool System::wizardEnabled() const
47 {
48  return !QFile::exists(wizardEnabledPath());
49 }
50 
51 void System::setWizardEnabled(bool enabled)
52 {
53  if (wizardEnabled() == enabled)
54  return;
55 
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  }
64 }
65 
66 void System::setSessionVariable(const QString &variable, const QString &value)
67 {
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));
70 
71  QMap<QString,QString> valueMap;
72  valueMap.insert(variable, value);
73 
74  QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.DBus",
75  "/org/freedesktop/DBus",
76  "org.freedesktop.DBus",
77  "UpdateActivationEnvironment");
78 
79  msg << QVariant::fromValue(valueMap);
80  QDBusConnection::sessionBus().asyncCall(msg);
81 }
82 
83 void System::updateSessionLanguage(const QString &locale)
84 {
85  const QString language = locale.split(".")[0];
86 
87  setSessionVariable("LANGUAGE", language);
88  setSessionVariable("LANG", locale);
89  setSessionVariable("LC_ALL", locale);
90 
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));
94 
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\"");
102 }