-
Notifications
You must be signed in to change notification settings - Fork 327
/
Session.cpp
218 lines (180 loc) · 6.07 KB
/
Session.cpp
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
/***************************************************************************
* Copyright (c) 2015 Pier Luigi Fiorini <pierluigi.fiorini@gmail.com>
* Copyright (c) 2013 Abdurrahman AVCI <abdurrahmanavci@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY 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, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
***************************************************************************/
#include <QFile>
#include <QFileInfo>
#include <QTextStream>
#include <QSettings>
#include <QLocale>
#include <QRegularExpression>
#include "Configuration.h"
#include "Session.h"
const QString s_entryExtention = QStringLiteral(".desktop");
namespace SDDM {
Session::Session()
: m_valid(false)
, m_type(UnknownSession)
, m_isHidden(false)
, m_isNoDisplay(false)
{
}
Session::Session(Type type, const QString &fileName)
: Session()
{
setTo(type, fileName);
}
bool Session::isValid() const
{
return m_valid;
}
Session::Type Session::type() const
{
return m_type;
}
int Session::vt() const
{
return m_vt;
}
void Session::setVt(int vt)
{
m_vt = vt;
}
QString Session::xdgSessionType() const
{
return m_xdgSessionType;
}
QDir Session::directory() const
{
return m_dir;
}
QString Session::fileName() const
{
return m_fileName;
}
QString Session::displayName() const
{
return m_displayName;
}
QString Session::comment() const
{
return m_comment;
}
QString Session::exec() const
{
return m_exec;
}
QString Session::tryExec() const
{
return m_tryExec;
}
QString Session::desktopSession() const
{
return QFileInfo(m_fileName).completeBaseName();
}
QString Session::desktopNames() const
{
return m_desktopNames;
}
bool Session::isHidden() const
{
return m_isHidden;
}
bool Session::isNoDisplay() const
{
return m_isNoDisplay;
}
QProcessEnvironment Session::additionalEnv() const {
return m_additionalEnv;
}
void Session::setTo(Type type, const QString &_fileName)
{
QString fileName(_fileName);
if (!fileName.endsWith(s_entryExtention))
fileName += s_entryExtention;
QFileInfo info(fileName);
m_type = UnknownSession;
m_valid = false;
m_desktopNames.clear();
switch (type) {
case WaylandSession:
m_dir = QDir(mainConfig.Wayland.SessionDir.get());
m_xdgSessionType = QStringLiteral("wayland");
break;
case X11Session:
m_dir = QDir(mainConfig.X11.SessionDir.get());
m_xdgSessionType = QStringLiteral("x11");
break;
default:
m_xdgSessionType.clear();
break;
}
m_fileName = m_dir.absoluteFilePath(fileName);
qDebug() << "Reading from" << m_fileName;
QSettings settings(m_fileName, QSettings::IniFormat);
QStringList locales = { QLocale().name() };
if (auto clean = QLocale().name().remove(QRegularExpression(QLatin1String("_.*"))); clean != locales.constFirst()) {
locales << clean;
}
if (settings.status() != QSettings::NoError)
return;
settings.beginGroup(QLatin1String("Desktop Entry"));
auto localizedValue = [&] (const QLatin1String &key) {
for (QString locale : qAsConst(locales)) {
QString localizedValue = settings.value(key + QLatin1Char('[') + locale + QLatin1Char(']'), QString()).toString();
if (!localizedValue.isEmpty()) {
return localizedValue;
}
}
return settings.value(key).toString();
};
m_displayName = localizedValue(QLatin1String("Name"));
m_comment = localizedValue(QLatin1String("Comment"));
m_exec = settings.value(QLatin1String("Exec"), QString()).toString();
m_tryExec = settings.value(QLatin1String("TryExec"), QString()).toString();
m_desktopNames = settings.value(QLatin1String("DesktopNames"), QString()).toString().replace(QLatin1Char(';'), QLatin1Char(':'));
QString hidden = settings.value(QLatin1String("Hidden"), QString()).toString();
m_isHidden = hidden.toLower() == QLatin1String("true");
QString noDisplay = settings.value(QLatin1String("NoDisplay"), QString()).toString();
m_isNoDisplay = noDisplay.toLower() == QLatin1String("true");
QString additionalEnv = settings.value(QLatin1String("X-SDDM-Env"), QString()).toString();
m_additionalEnv = parseEnv(additionalEnv);
settings.endGroup();
m_type = type;
m_valid = true;
}
Session &Session::operator=(const Session &other)
{
setTo(other.type(), other.fileName());
return *this;
}
QProcessEnvironment SDDM::Session::parseEnv(const QString &list)
{
QProcessEnvironment env;
const QVector<QStringRef> entryList = list.splitRef(QLatin1Char(','));
for (const auto &entry: entryList) {
int midPoint = entry.indexOf(QLatin1Char('='));
if (midPoint < 0) {
qWarning() << "Malformed entry in" << fileName() << ":" << entry;
continue;
}
env.insert(entry.left(midPoint).toString(), entry.mid(midPoint+1).toString());
}
return env;
}
}