on d21d2ed help sign in
unity8/plugins/Wizard/System.cpp
172 lines 5.5 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 <QDir>
24#include <QFile>
25#include <QLocale>
26#include <QMap>
27#include <QProcess>
28#include <QDebug>
29#include <QSettings>
31System::System()
32 : QObject()
34 // Register the argument needed for UpdateActivationEnvironment below
35 qDBusRegisterMetaType<QMap<QString,QString>>();
37 if(!wizardEnabled()) {
38 m_fsWatcher.addPath(wizardEnabledPath());
39 }
40 connect(&m_fsWatcher, &QFileSystemWatcher::fileChanged, this, &System::watcherFileChanged);
43QString System::wizardEnabledPath()
45 // Uses ubuntu-system-settings namespace for historic compatibility reasons
46 return QDir::home().filePath(QStringLiteral(".config/ubuntu-system-settings/wizard-has-run"));
49QString System::currentFrameworkPath()
51 return QDir::home().filePath(QStringLiteral("/usr/share/click/frameworks/current"));
54/*
55wizardEnabled and isUpdate logic
57if wizard-has-run does NOT exists == is new install
58if wizard-has-run does exists but does NOT match current framework == is update
59if wizard-has-run does exists but does match current framework == show no wizard
60*/
62bool System::wizardPathExists() {
63 return QFile::exists(wizardEnabledPath());
66bool System::wizardEnabled() const
68 if (!wizardPathExists())
69 return true;
70 return isUpdate();
73QString System::readCurrentFramework()
75 QFile f(currentFrameworkPath());
76 if (!f.open(QFile::ReadOnly | QFile::Text)) return "";
77 QTextStream in(&f);
78 return in.readAll();
81QString System::readWizardEnabled()
83 QFile f(wizardEnabledPath());
84 if (!f.open(QFile::ReadOnly | QFile::Text)) return "";
85 QTextStream in(&f);
86 return in.readAll();
89QString System::version() const
91 return readCurrentFramework();
94bool System::isUpdate() const
96 if (!wizardPathExists())
97 return false;
99 return readCurrentFramework() != readWizardEnabled();
102void System::setWizardEnabled(bool enabled)
104 if (wizardEnabled() == enabled && !isUpdate())
105 return;
107 if (enabled) {
108 QFile::remove(wizardEnabledPath());
109 } else {
110 QDir(wizardEnabledPath()).mkpath(QStringLiteral(".."));
111 if (QFile::exists(wizardEnabledPath()))
112 QFile::remove(wizardEnabledPath());
113 QFile::copy(currentFrameworkPath(), wizardEnabledPath());
114 m_fsWatcher.addPath(wizardEnabledPath());
115 Q_EMIT wizardEnabledChanged();
116 Q_EMIT isUpdateChanged();
117 }
120void System::watcherFileChanged()
122 Q_EMIT wizardEnabledChanged();
123 Q_EMIT isUpdateChanged();
124 m_fsWatcher.removePath(wizardEnabledPath());
127void System::setSessionVariable(const QString &variable, const QString &value)
129 // We need to update both upstart's and DBus's environment
130 QProcess::startDetached(QStringLiteral("initctl set-env --global %1=%2").arg(variable, value));
132 QMap<QString,QString> valueMap;
133 valueMap.insert(variable, value);
135 QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.DBus"),
136 QStringLiteral("/org/freedesktop/DBus"),
137 QStringLiteral("org.freedesktop.DBus"),
138 QStringLiteral("UpdateActivationEnvironment"));
140 msg << QVariant::fromValue(valueMap);
141 QDBusConnection::sessionBus().asyncCall(msg);
144void System::updateSessionLocale(const QString &locale)
146 const QString language = locale.split(QStringLiteral("."))[0];
148 setSessionVariable(QStringLiteral("LANGUAGE"), language);
149 setSessionVariable(QStringLiteral("LANG"), locale);
150 setSessionVariable(QStringLiteral("LC_ALL"), locale);
152 // QLocale caches the default locale on startup, and Qt uses that cached
153 // copy when formatting dates. So manually update it here.
154 QLocale::setDefault(QLocale(locale));
156 // Restart bits of the session to pick up new language.
157 QProcess::startDetached(QStringLiteral("sh -c \"initctl emit indicator-services-end; \
158 initctl stop scope-registry; \
159 initctl stop smart-scopes-proxy; \
160 initctl emit --no-wait indicator-services-start; \
161 initctl restart --no-wait ubuntu-location-service-trust-stored; \
162 initctl restart --no-wait maliit-server; \
163 initctl restart --no-wait indicator-messages; \
164 initctl restart --no-wait unity8-dash\""));
167void System::skipUntilFinishedPage()
169 QSettings settings;
170 settings.setValue(QStringLiteral("Wizard/SkipUntilFinishedPage"), true);
171 settings.sync();