-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVncConfiguration.cpp
257 lines (212 loc) · 6.8 KB
/
VncConfiguration.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include "VncConfiguration.h"
#include <strings.h>
#include <unistd.h>
#include <iostream>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xvnc.h>
// We don't need this macros and they polute our namespace.
#undef None
#undef Bool
#undef Status
#include <QtNetwork/QLocalSocket>
#include <QtCore/QProcessEnvironment>
#include <QtCore/QSettings>
VncConfiguration::VncConfiguration(Display *display)
: m_display(display)
, m_managerConnection(display)
{}
bool VncConfiguration::connect()
{
return m_managerConnection.connect();
}
void VncConfiguration::read()
{
int length;
char *value;
// Persistence
if (XVncExtGetParam(m_display, "MaxDisconnectionTime", &value, &length)) {
if (length == 1 && value[0] == '0') {
m_persistence = Persistence::Persistent;
} else {
m_persistence = Persistence::NonPersistent;
}
XFree(value);
}
// Sharing
bool NeverShared = false;
if (XVncExtGetParam(m_display, "NeverShared", &value, &length)) {
NeverShared = (length == 1 && value[0] == '1');
XFree(value);
}
bool DisconnectClients = false;
if (XVncExtGetParam(m_display, "DisconnectClients", &value, &length)) {
DisconnectClients = (length == 1 && value[0] == '1');
XFree(value);
}
if (NeverShared && DisconnectClients) {
m_sharing = Sharing::OneClient;
} else if (!NeverShared && !DisconnectClients) {
m_sharing = Sharing::MultipleClients;
} else {
m_sharing = Sharing::Unknown;
}
// Security
m_plainUsers.clear();
if (XVncExtGetParam(m_display, "SecurityTypes", &value, &length)) {
if (strcasecmp(value, "None") == 0) {
m_security = Security::None;
} else if (strcasecmp(value, "VncAuth") == 0) {
m_security = Security::VncAuth;
} else if (strcasecmp(value, "Plain") == 0) {
m_security = Security::Plain;
} else {
m_security = Security::Unknown;
}
XFree(value);
}
// Reading this in no matter what is the current security type
if (XVncExtGetParam(m_display, "PasswordFile", &value, &length)) {
m_passwordFile = QString::fromLatin1(value, length); // TODO: Verify if it's ascii
XFree(value);
}
// Reading this in no matter what is the current security type
if (XVncExtGetParam(m_display, "PlainUsers", &value, &length)) {
m_plainUsers = QString::fromLatin1(value, length).split(',', QString::SkipEmptyParts); // TODO: Verify if it's ascii
XFree(value);
}
// Name
if (XVncExtGetParam(m_display, "Desktop", &value, &length)) {
m_sessionName = QString::fromLatin1(value, length);
XFree(value);
}
}
void VncConfiguration::normalizeSettings()
{
// Persistence
if (m_persistence == Persistence::Unknown) {
m_persistence = Persistence::NonPersistent;
}
// Sharing
if (m_sharing == Sharing::Unknown) {
m_sharing = Sharing::OneClient;
}
// Security
QString possibleUsername = queryUserName();
if (!possibleUsername.isEmpty()) {
if (m_security == Security::Unknown || m_security == Security::None) {
m_security = Security::Plain;
m_plainUsers.clear();
m_plainUsers.append(possibleUsername);
}
}
// Name
QString possibleName = querySessionName();
if (!possibleName.isEmpty()) {
m_sessionName = possibleName;
}
}
void VncConfiguration::apply()
{
// Sharing 1
switch (m_sharing) {
case Sharing::OneClient:
XVncExtSetParam(m_display, "NeverShared=1");
XVncExtSetParam(m_display, "DisconnectClients=1");
break;
case Sharing::MultipleClients:
// Will be done after setting security
break;
}
// Security
switch (m_security) {
case Security::None:
XVncExtSetParam(m_display, "SecurityTypes=None");
break;
case Security::VncAuth:
XVncExtSetParam(m_display, "SecurityTypes=VncAuth");
XVncExtSetParam(m_display, (QString("PasswordFile=") + m_passwordFile).toLatin1());
break;
case Security::Plain:
XVncExtSetParam(m_display, "SecurityTypes=Plain");
XVncExtSetParam(m_display, (QString("PlainUsers=") + m_plainUsers.join(",")).toLatin1());
break;
}
// Sharing 2
switch (m_sharing) {
case Sharing::OneClient:
// Was done before setting security
break;
case Sharing::MultipleClients:
XVncExtSetParam(m_display, "DisconnectClients=0");
XVncExtSetParam(m_display, "NeverShared=0");
break;
}
// Persistence
switch (m_persistence) {
case Persistence::Persistent:
XVncExtSetParam(m_display, "MaxDisconnectionTime=0");
break;
case Persistence::NonPersistent:
XVncExtSetParam(m_display, "MaxDisconnectionTime=5"); // XXX: Too hardcoded
break;
}
// Name
XVncExtSetParam(m_display, ("Desktop=" + m_sessionName).toLatin1());
if (m_managerConnection.connected()) {
// Send session info
m_managerConnection.sendSessionInfo(m_persistence == Persistence::Persistent);
}
}
void VncConfiguration::setSessionName(QString sessionName)
{
m_sessionName = sessionName;
}
void VncConfiguration::setPersistence(VncConfiguration::Persistence persistence)
{
m_persistence = persistence;
}
void VncConfiguration::setSharing(VncConfiguration::Sharing sharing)
{
m_sharing = sharing;
}
void VncConfiguration::setSecurityNone()
{
m_security = Security::None;
}
void VncConfiguration::setSecurityVncAuth(QString password, QString viewonlyPassword)
{
m_security = Security::VncAuth;
throw std::runtime_error("Not implemented.");
}
void VncConfiguration::setSecurityVncAuth(QString authFilename)
{
m_security = Security::VncAuth;
throw std::runtime_error("Not implemented.");
}
void VncConfiguration::setSecurityPlain()
{
m_security = Security::Plain;
}
void VncConfiguration::setSecurityPlain(QStringList plainUsers)
{
m_security = Security::Plain;
m_plainUsers = plainUsers;
}
QString VncConfiguration::querySessionName()
{
QString desktopSession = QProcessEnvironment::systemEnvironment().value("DESKTOP_SESSION");
if (!desktopSession.isEmpty()) {
// XXX: Misusing QSettings to read desktop file
QSettings desktopFile("/usr/share/xsessions/" + desktopSession + ".desktop", QSettings::IniFormat);
if (desktopFile.contains("Desktop Entry/Name")) {
return desktopFile.value("Desktop Entry/Name").toString();
}
}
QString xdg_current_desktop = QProcessEnvironment::systemEnvironment().value("XDG_CURRENT_DESKTOP");
return xdg_current_desktop;
}
QString VncConfiguration::queryUserName()
{
return QProcessEnvironment::systemEnvironment().value("USER");
}