Unity 8
System.cpp
1 /*
2  * Copyright (C) 2014-2016 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 #include <QDebug>
29 #include <QSettings>
30 
31 System::System()
32  : QObject()
33 {
34  // Register the argument needed for UpdateActivationEnvironment below
35  qDBusRegisterMetaType<QMap<QString,QString>>();
36 
37  if(!wizardEnabled()) {
38  m_fsWatcher.addPath(wizardEnabledPath());
39  }
40  connect(&m_fsWatcher, &QFileSystemWatcher::fileChanged, this, &System::watcherFileChanged);
41 }
42 
43 QString System::wizardEnabledPath()
44 {
45  // Uses ubuntu-system-settings namespace for historic compatibility reasons
46  return QDir::home().filePath(QStringLiteral(".config/ubuntu-system-settings/wizard-has-run"));
47 }
48 
49 bool System::wizardEnabled() const
50 {
51  return !QFile::exists(wizardEnabledPath());
52 }
53 
54 void System::setWizardEnabled(bool enabled)
55 {
56  if (wizardEnabled() == enabled)
57  return;
58 
59  if (enabled) {
60  QFile::remove(wizardEnabledPath());
61  } else {
62  QDir(wizardEnabledPath()).mkpath(QStringLiteral(".."));
63  QFile(wizardEnabledPath()).open(QIODevice::WriteOnly);
64  m_fsWatcher.addPath(wizardEnabledPath());
65  Q_EMIT wizardEnabledChanged();
66  }
67 }
68 
69 void System::watcherFileChanged()
70 {
71  Q_EMIT wizardEnabledChanged();
72  m_fsWatcher.removePath(wizardEnabledPath());
73 }
74 
75 void System::setSessionVariable(const QString &variable, const QString &value)
76 {
77  // We need to update both upstart's and DBus's environment
78  QProcess::startDetached(QStringLiteral("initctl set-env --global %1=%2").arg(variable, value));
79 
80  QMap<QString,QString> valueMap;
81  valueMap.insert(variable, value);
82 
83  QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.DBus"),
84  QStringLiteral("/org/freedesktop/DBus"),
85  QStringLiteral("org.freedesktop.DBus"),
86  QStringLiteral("UpdateActivationEnvironment"));
87 
88  msg << QVariant::fromValue(valueMap);
89  QDBusConnection::sessionBus().asyncCall(msg);
90 }
91 
92 void System::updateSessionLocale(const QString &locale)
93 {
94  const QString language = locale.split(QStringLiteral("."))[0];
95 
96  setSessionVariable(QStringLiteral("LANGUAGE"), language);
97  setSessionVariable(QStringLiteral("LANG"), locale);
98  setSessionVariable(QStringLiteral("LC_ALL"), locale);
99 
100  // QLocale caches the default locale on startup, and Qt uses that cached
101  // copy when formatting dates. So manually update it here.
102  QLocale::setDefault(QLocale(locale));
103 
104  // Restart bits of the session to pick up new language.
105  QProcess::startDetached(QStringLiteral("sh -c \"initctl emit indicator-services-end; \
106  initctl stop scope-registry; \
107  initctl stop smart-scopes-proxy; \
108  initctl emit --no-wait indicator-services-start; \
109  initctl restart --no-wait ubuntu-location-service-trust-stored; \
110  initctl restart --no-wait maliit-server; \
111  initctl restart --no-wait indicator-messages; \
112  initctl restart --no-wait unity8-dash\""));
113 }
114 
115 void System::skipUntilFinishedPage()
116 {
117  QSettings settings;
118  settings.setValue(QStringLiteral("Wizard/SkipUntilFinishedPage"), true);
119  settings.sync();
120 }