-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandLine.cpp
255 lines (222 loc) · 5.97 KB
/
CommandLine.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
#include "CommandLine.h"
#ifndef DEBUG_MODE
#include <SessionLockQt/command.h>
#endif
#include <QDBusInterface>
#include <QDate>
#include <QDebug>
#include <QFile>
#include <QGuiApplication>
#include <QJsonDocument>
#include <QLocale>
#include <QSettings>
#include <QTimer>
using namespace Qt::StringLiterals;
CommandLine::CommandLine(QObject *parent)
: QObject(parent)
, m_currentDate(QLocale().toString(QDate::currentDate()))
, m_userName(QString())
, m_backgroundImagePath(QUrl("qrc:/image/gangdamu.png"))
, m_opacity(0.6)
, m_greetd(nullptr)
, m_status(LoginStatus::Start)
, m_userIcon(QUrl("qrc:/image/account.svg"))
, m_command(QString())
, m_isAuthing(false)
, m_env(QStringList())
{
QSettings setting;
QString user = setting.value("user").toString();
if (!user.isEmpty()) {
setUserName(user);
}
connectToGreetd();
}
void
CommandLine::setPassword(const QString &password)
{
m_password = password;
Q_EMIT passwordChanged();
}
void
CommandLine::setUserName(const QString &userName)
{
if (m_userName == userName) {
return;
}
m_userName = userName;
if (auto iconP = QString("/var/lib/AccountsService/icons/%1").arg(m_userName);
QFile(iconP).exists()) {
m_userIcon = QUrl::fromLocalFile(iconP);
} else {
m_userIcon = QUrl("qrc:/image/account.svg");
}
Q_EMIT userIconChanged();
Q_EMIT userNameChanged();
}
void
CommandLine::setCommand(const QString &command)
{
m_command = command;
Q_EMIT commandChanged();
}
void
CommandLine::setEnv(const QStringList &env)
{
m_env = env;
Q_EMIT envChanged();
}
void
CommandLine::connectToGreetd()
{
QString path = qgetenv("GREETD_SOCK");
if (path.isEmpty()) {
qWarning() << "Unable to retrieve GREETD_SOCK";
m_errorMessage = "Cannot connect to greetd";
Q_EMIT errorMessageChanged();
return;
}
m_greetd = new QLocalSocket(this);
m_greetd->connectToServer(path, QIODevice::ReadWrite | QIODevice::Unbuffered);
m_greetd->waitForConnected();
connect(m_greetd, &QLocalSocket::readyRead, this, &CommandLine::handleDataRead);
}
void
CommandLine::handleDataRead()
{
QByteArray data = m_greetd->readAll();
data.remove(0, 4);
QJsonObject document = QJsonDocument::fromJson(data).object();
QString authtype = document["type"].toString();
if (authtype == "auth_message") {
QString auth_message_type = document["auth_message_type"].toString();
if (auth_message_type == "secret") {
handleAuthPasswordMessage();
return;
} else {
// NOTE: I DO NOT CARE
handleAuthMessageNone();
return;
}
} else if (authtype == "error") {
QString error_type = document["error_type"].toString();
QString description = document["description"].toString();
m_errorMessage = QString("%1: %2").arg(error_type).arg(description);
Q_EMIT errorMessageChanged();
handleAuthError();
return;
} else if (authtype == "success") {
handleSuccessded();
}
}
void
CommandLine::handleAuthMessageNone()
{
QVariantMap request;
request["type"] = "post_auth_message_response";
QJsonDocument json;
json.setObject(QJsonObject::fromVariantMap(request));
roundtrip(json.toJson().simplified());
}
void
CommandLine::handleSuccessded()
{
switch (m_status) {
case Errored: {
m_status = CancelSessionSuccessded;
break;
}
case TryToLoginSession: {
m_status = TryToStartSession;
QVariantMap request;
request["type"] = "start_session";
request["cmd"] = m_command.split(' ');
request["env"] = m_env;
QJsonDocument json;
json.setObject(QJsonObject::fromVariantMap(request));
roundtrip(json.toJson().simplified());
break;
}
case TryToStartSession: {
m_status = LoginSuccessded;
}
default:
break;
}
}
void
CommandLine::handleAuthError()
{
m_status = LoginStatus::Errored;
QVariantMap request;
request["type"] = "cancel_session";
QJsonDocument json;
json.setObject(QJsonObject::fromVariantMap(request));
roundtrip(json.toJson().simplified());
m_isAuthing = false;
Q_EMIT isAuthingChanged();
}
void
CommandLine::handleAuthPasswordMessage()
{
m_status = LoginStatus::TryToLoginSession;
m_isAuthing = true;
Q_EMIT isAuthingChanged();
QVariantMap request;
request["type"] = "post_auth_message_response";
request["response"] = m_password;
QJsonDocument json;
json.setObject(QJsonObject::fromVariantMap(request));
roundtrip(json.toJson().simplified());
}
void
CommandLine::UnLock()
{
#ifndef DEBUG_MODE
ExtSessionLockV1Qt::Command::instance()->unLockScreen();
#endif
QTimer::singleShot(0, qApp, [] { QGuiApplication::quit(); });
}
void
CommandLine::tryLogin()
{
if (!m_greetd) {
return;
}
QVariantMap request;
request["type"] = "create_session";
request["username"] = m_userName;
QJsonDocument json;
json.setObject(QJsonObject::fromVariantMap(request));
roundtrip(json.toJson().simplified());
}
void
CommandLine::roundtrip(const QString &payload)
{
qint32 length = payload.length();
m_greetd->write((const char *)&length, sizeof(length));
m_greetd->write(payload.toUtf8());
}
void
CommandLine::RequestLogin()
{
if (m_password.isEmpty()) {
m_errorMessage = "password is needed";
Q_EMIT errorMessageChanged();
return;
}
tryLogin();
}
void
CommandLine::RequestShutDown()
{
QDBusInterface login1{"org.freedesktop.login1"_L1,
"/org/freedesktop/login1"_L1,
"org.freedesktop.login1.Manager"_L1,
QDBusConnection::systemBus(),
this};
auto result = login1.asyncCallWithArgumentList("PowerOff", {true});
if (result.isError()) {
qWarning() << "Cannot PowerOff" << result.error();
}
}