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