File: System.cpp

package info (click to toggle)
lomiri 0.5.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 24,088 kB
  • sloc: cpp: 39,498; python: 2,559; javascript: 1,426; ansic: 1,012; sh: 289; xml: 252; makefile: 69
file content (220 lines) | stat: -rw-r--r-- 7,003 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * Copyright (C) 2018 The UBports project
 * Copyright (C) 2014-2016 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "System.h"

#include <QDBusPendingCall>
#include <QDBusMessage>
#include <QDBusConnection>
#include <QDBusMetaType>
#include <QDir>
#include <QFile>
#include <QLocale>
#include <QMap>
#include <QProcess>
#include <QDebug>
#include <QSettings>
#include <QStringBuilder>

#include <glib.h>

System::System()
    : QObject()
{
    // Register the argument needed for UpdateActivationEnvironment below
    qDBusRegisterMetaType<QMap<QString,QString>>();

    if(!wizardEnabled()) {
        m_fsWatcher.addPath(wizardEnabledPath());
    }
    connect(&m_fsWatcher, &QFileSystemWatcher::fileChanged, this, &System::watcherFileChanged);
}

QString System::wizardEnabledPath()
{
    return QDir::home().filePath(QStringLiteral(".config/lomiri/wizard-has-run"));
}

QString System::currentFrameworkPath()
{
    QFileInfo f("/usr/share/click/frameworks/current");
    return f.canonicalFilePath();
}

/*
wizardEnabled and isUpdate logic

if wizard-has-run does NOT exist == is new install
if wizard-has-run exists but does NOT match current framework == is update
if wizard-has-run exists but does match current framework == show no wizard
*/

bool System::wizardPathExists() {
  return QFile::exists(wizardEnabledPath());
}

bool System::wizardEnabled() const
{
    if (!wizardPathExists()) {
        return true;
    }
    return isUpdate();
}

QString System::readCurrentFramework()
{
    QFile f(currentFrameworkPath());
    if (!f.open(QFile::ReadOnly | QFile::Text)) return "";
    QTextStream in(&f);
    return in.readAll();
}

QString System::readWizardEnabled()
{
    QFile f(wizardEnabledPath());
    if (!f.open(QFile::ReadOnly | QFile::Text)) return "";
    QTextStream in(&f);
    return in.readAll();
}

QString System::version() const
{
    return readCurrentFramework();
}

bool System::isUpdate() const
{
    if (!wizardPathExists()) {
        return false;
    }

    return readCurrentFramework() != readWizardEnabled();
}

void System::setWizardEnabled(bool enabled)
{
    if (wizardEnabled() == enabled && !isUpdate())
        return;

    if (enabled) {
        QFile::remove(wizardEnabledPath());
    } else {
        QDir(wizardEnabledPath()).mkpath(QStringLiteral(".."));
        if (QFile::exists(wizardEnabledPath())) {
            QFile::remove(wizardEnabledPath());
        }
        // For special cases check if wizardEnabledPath is a folder
        if (QDir(wizardEnabledPath()).exists()) {
            QDir(wizardEnabledPath()).removeRecursively();
        }
        if (!QFile::copy(currentFrameworkPath(), wizardEnabledPath())) {
            // Make en empty file if framework does not exist
            QFile f(wizardEnabledPath());
            f.open(QFile::WriteOnly);
        }
        m_fsWatcher.addPath(wizardEnabledPath());
        Q_EMIT wizardEnabledChanged();
        Q_EMIT isUpdateChanged();
    }
}

void System::watcherFileChanged()
{
    Q_EMIT wizardEnabledChanged();
    Q_EMIT isUpdateChanged();
    m_fsWatcher.removePath(wizardEnabledPath());
}

void System::setSessionVariable(const QString &variable, const QString &value)
{
    // We need to update both systemd's and DBus's environment
    QStringList vars = { variable % QChar('=') % value };
    QDBusMessage systemdMsg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.systemd1"),
                                                             QStringLiteral("/org/freedesktop/systemd1"),
                                                             QStringLiteral("org.freedesktop.systemd1.Manager"),
                                                             QStringLiteral("SetEnvironment"));
    systemdMsg << QVariant::fromValue(vars);
    QDBusConnection::sessionBus().asyncCall(systemdMsg);

    QMap<QString,QString> valueMap;
    valueMap.insert(variable, value);

    QDBusMessage dbusMsg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.DBus"),
                                                      QStringLiteral("/org/freedesktop/DBus"),
                                                      QStringLiteral("org.freedesktop.DBus"),
                                                      QStringLiteral("UpdateActivationEnvironment"));

    dbusMsg << QVariant::fromValue(valueMap);
    QDBusConnection::sessionBus().asyncCall(dbusMsg);
}

void System::restartUnit(const QString &unitName)
{
    QDBusMessage systemdMsg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.systemd1"),
                                                             QStringLiteral("/org/freedesktop/systemd1"),
                                                             QStringLiteral("org.freedesktop.systemd1.Manager"),
                                                             QStringLiteral("TryRestartUnit"));
    systemdMsg << QVariant::fromValue(unitName);
    systemdMsg << QVariant::fromValue(QStringLiteral("replace"));
    QDBusConnection::sessionBus().asyncCall(systemdMsg);
}

void System::updateSessionLocale(const QString &locale)
{
    const QString language = locale.split(QStringLiteral("."))[0];

    setSessionVariable(QStringLiteral("LANGUAGE"), language);
    setSessionVariable(QStringLiteral("LANG"), locale);
    setSessionVariable(QStringLiteral("LC_ALL"), locale);

    // QLocale caches the default locale on startup, and Qt uses that cached
    // copy when formatting dates.  So manually update it here.
    QLocale::setDefault(QLocale(locale));

    // Restart bits of the session to pick up new language.
    const QStringList units {
        "lomiri-indicators.target",
        "lomiri-location-service-trust-stored.service",
        "pulseaudio-trust-stored.service",
        "sync-monitor.service",
        "maliit-server.service",
        "ciborium.service",
    };
    for (const QString& unit : units) {
        restartUnit(unit);
    }
}

void System::skipUntilFinishedPage()
{
    QSettings settings;
    settings.setValue(QStringLiteral("Wizard/SkipUntilFinishedPage"), true);
    settings.sync();
}

QString System::distroName() const
{
#ifdef LOMIRI_DISPLAYED_DISTRO_NAME
    return QStringLiteral(LOMIRI_DISPLAYED_DISTRO_NAME);
#else
    g_autofree gchar * name = g_get_os_info(G_OS_INFO_KEY_NAME);
    if (name)
        return QString::fromLocal8Bit(name);

    return QStringLiteral("Lomiri");
#endif
}