on 101c302 help sign in
unity8/plugins/Wizard/System.cpp
142 lines 4.8 kB history raw
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 */
17#include "System.h"
19#include <QDBusPendingCall>
20#include <QDBusMessage>
21#include <QDBusConnection>
22#include <QDBusMetaType>
23#include <QVersionNumber>
24#include <QDir>
25#include <QFile>
26#include <QLocale>
27#include <QMap>
28#include <QProcess>
29#include <QDebug>
30#include <QSettings>
32System::System()
33 : QObject()
35 // Register the argument needed for UpdateActivationEnvironment below
36 qDBusRegisterMetaType<QMap<QString,QString>>();
38 if(!wizardEnabled()) {
39 m_fsWatcher.addPath(wizardEnabledPath());
40 }
41 connect(&m_fsWatcher, &QFileSystemWatcher::fileChanged, this, &System::watcherFileChanged);
44QString System::wizardEnabledPath()
46 // Uses ubuntu-system-settings namespace for historic compatibility reasons
47 return QDir::home().filePath(QStringLiteral(".config/ubuntu-system-settings/wizard-has-run/"));
50bool System::wizardEnabled() const
52 return (versionToShow() >= 0);
55signed int System::versionToShow() const
57 if (!(QFile::exists(wizardEnabledPath()))) {
58 return 0;
59 }
61 QString pathToCheck = QDir(wizardEnabledPath()).filePath(QString::number(CURRENT_VERSION));
63 if (!(QFile::exists(pathToCheck))) {
64 return CURRENT_VERSION;
65 }
67 return -1;
71void System::setWizardEnabled(bool enabled)
73 if (wizardEnabled() == enabled)
74 return;
76 if (enabled) {
77 QDir(wizardEnabledPath()).removeRecursively();
78 } else {
79 QDir(wizardEnabledPath()).mkpath(QStringLiteral("."));
80 m_fsWatcher.addPath(wizardEnabledPath());
82 // Disable the newest non-first-run wizard, too
83 QFile(QDir(wizardEnabledPath()).filePath(QString::number(CURRENT_VERSION))).open(QIODevice::WriteOnly);
84 }
86 Q_EMIT wizardEnabledChanged();
87 Q_EMIT versionToShowChanged();
90void System::watcherFileChanged()
92 Q_EMIT wizardEnabledChanged();
93 Q_EMIT versionToShowChanged();
94 m_fsWatcher.removePath(wizardEnabledPath());
97void System::setSessionVariable(const QString &variable, const QString &value)
99 // We need to update both upstart's and DBus's environment
100 QProcess::startDetached(QStringLiteral("initctl set-env --global %1=%2").arg(variable, value));
102 QMap<QString,QString> valueMap;
103 valueMap.insert(variable, value);
105 QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.DBus"),
106 QStringLiteral("/org/freedesktop/DBus"),
107 QStringLiteral("org.freedesktop.DBus"),
108 QStringLiteral("UpdateActivationEnvironment"));
110 msg << QVariant::fromValue(valueMap);
111 QDBusConnection::sessionBus().asyncCall(msg);
114void System::updateSessionLocale(const QString &locale)
116 const QString language = locale.split(QStringLiteral("."))[0];
118 setSessionVariable(QStringLiteral("LANGUAGE"), language);
119 setSessionVariable(QStringLiteral("LANG"), locale);
120 setSessionVariable(QStringLiteral("LC_ALL"), locale);
122 // QLocale caches the default locale on startup, and Qt uses that cached
123 // copy when formatting dates. So manually update it here.
124 QLocale::setDefault(QLocale(locale));
126 // Restart bits of the session to pick up new language.
127 QProcess::startDetached(QStringLiteral("sh -c \"initctl emit indicator-services-end; \
128 initctl stop scope-registry; \
129 initctl stop smart-scopes-proxy; \
130 initctl emit --no-wait indicator-services-start; \
131 initctl restart --no-wait ubuntu-location-service-trust-stored; \
132 initctl restart --no-wait maliit-server; \
133 initctl restart --no-wait indicator-messages; \
134 initctl restart --no-wait unity8-dash\""));
137void System::skipUntilFinishedPage()
139 QSettings settings;
140 settings.setValue(QStringLiteral("Wizard/SkipUntilFinishedPage"), true);
141 settings.sync();