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  if(!wizardEnabled()) {
37  m_fsWatcher.addPath(wizardEnabledPath());
38  }
39  connect(&m_fsWatcher, &QFileSystemWatcher::fileChanged, this, &System::watcherFileChanged);
40 }
41 
42 QString System::wizardEnabledPath()
43 {
44  // Uses ubuntu-system-settings namespace for historic compatibility reasons
45  return QDir::home().filePath(QStringLiteral(".config/ubuntu-system-settings/wizard-has-run"));
46 }
47 
48 bool System::wizardEnabled() const
49 {
50  return !QFile::exists(wizardEnabledPath());
51 }
52 
53 void System::setWizardEnabled(bool enabled)
54 {
55  if (wizardEnabled() == enabled)
56  return;
57 
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  }
66 }
67 
68 void System::watcherFileChanged()
69 {
70  Q_EMIT wizardEnabledChanged();
71  m_fsWatcher.removePath(wizardEnabledPath());
72 }
73 
74 void System::setSessionVariable(const QString &variable, const QString &value)
75 {
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));
78 
79  QMap<QString,QString> valueMap;
80  valueMap.insert(variable, value);
81 
82  QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.DBus"),
83  QStringLiteral("/org/freedesktop/DBus"),
84  QStringLiteral("org.freedesktop.DBus"),
85  QStringLiteral("UpdateActivationEnvironment"));
86 
87  msg << QVariant::fromValue(valueMap);
88  QDBusConnection::sessionBus().asyncCall(msg);
89 }
90 
91 void System::updateSessionLanguage(const QString &locale)
92 {
93  const QString language = locale.split(QStringLiteral("."))[0];
94 
95  setSessionVariable(QStringLiteral("LANGUAGE"), language);
96  setSessionVariable(QStringLiteral("LANG"), locale);
97  setSessionVariable(QStringLiteral("LC_ALL"), locale);
98 
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));
102 
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\""));
110 }