-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathwarningcontent.cpp
More file actions
471 lines (399 loc) · 19.2 KB
/
warningcontent.cpp
File metadata and controls
471 lines (399 loc) · 19.2 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// SPDX-FileCopyrightText: 2015 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "dconfig_helper.h"
#include "warningcontent.h"
#include "fullscreenbackground.h"
#include "lockcontent.h"
#include "keyboardmonitor.h"
#include <DDBusSender>
#include "dbusconstant.h"
WarningContent::WarningContent(QWidget *parent)
: SessionBaseWindow(parent)
, m_model(nullptr)
, m_login1Inter(new DBusLogin1Manager("org.freedesktop.login1", "/org/freedesktop/login1", QDBusConnection::systemBus(), this))
, m_powerAction(SessionBaseModel::PowerAction::None)
, m_failures(0)
{
setAccessibleName("WarningContent");
m_inhibitorBlacklists << "NetworkManager" << "ModemManager" << DSS_DBUS::sessionPowerService;
setTopFrameVisible(false);
setBottomFrameVisible(false);
}
WarningContent::~WarningContent()
{
}
WarningContent *WarningContent::instance()
{
static WarningContent *warningContent = nullptr;
if (!warningContent) {
warningContent = new WarningContent;
}
return warningContent;
}
void WarningContent::setModel(SessionBaseModel * const model)
{
m_model = model;
connect(m_model, &SessionBaseModel::shutdownInhibit, this, &WarningContent::shutdownInhibit);
}
QList<InhibitWarnView::InhibitorData> WarningContent::listInhibitors(const SessionBaseModel::PowerAction action)
{
std::shared_ptr<User> currentUser = m_model->currentUser();
if (!currentUser)
return QList<InhibitWarnView::InhibitorData>();
QList<InhibitWarnView::InhibitorData> inhibitorList;
if (m_login1Inter->isValid()) {
QDBusPendingReply<InhibitorsList> reply = m_login1Inter->ListInhibitors();
reply.waitForFinished();
if (!reply.isError()) {
InhibitorsList inhibitList = qdbus_cast<InhibitorsList>(reply.argumentAt(0));
qCDebug(DDE_SHELL) << "Inhibitors list count: " << inhibitList.count();
QString type;
switch (action) {
case SessionBaseModel::PowerAction::RequireShutdown:
case SessionBaseModel::PowerAction::RequireUpdateShutdown:
case SessionBaseModel::PowerAction::RequireRestart:
case SessionBaseModel::PowerAction::RequireUpdateRestart:
case SessionBaseModel::PowerAction::RequireSwitchSystem:
case SessionBaseModel::PowerAction::RequireLogout:
type = "shutdown";
break;
case SessionBaseModel::PowerAction::RequireSuspend:
case SessionBaseModel::PowerAction::RequireHibernate:
type = "sleep";
break;
default:
return {};
}
QStringList delayInhibitIgnoreList = DConfigHelper::instance()->getConfig("delayInhibitIgnoreList", QStringList()).toStringList();
for (int i = 0; i < inhibitList.count(); i++) {
// Just take care of DStore's inhibition, ignore others'.
const Inhibit &inhibitor = inhibitList.at(i);
if (inhibitor.uid != currentUser->uid() && inhibitor.uid != 0)
continue;
#ifndef ENABLE_DSS_SNIPE
if (inhibitor.what.split(':', QString::SkipEmptyParts).contains(type)
#else
if (inhibitor.what.split(':', Qt::SkipEmptyParts).contains(type)
#endif
&& !m_inhibitorBlacklists.contains(inhibitor.who)) {
// 待机时,非block暂不处理,因为目前没有倒计时待机功能
if (type == "sleep" && inhibitor.mode != "block")
continue;
if (inhibitor.mode == "delay" && delayInhibitIgnoreList.contains(inhibitor.who))
continue;
InhibitWarnView::InhibitorData inhibitData;
inhibitData.who = inhibitor.who;
inhibitData.why = inhibitor.why;
inhibitData.mode = inhibitor.mode;
inhibitData.pid = inhibitor.pid;
if(action == SessionBaseModel::PowerAction::RequireLogout && inhibitor.uid != m_model->currentUser()->uid())
continue;
// 读取翻译后的文本,读取应用图标
QDBusConnection connection = QDBusConnection::sessionBus();
if (!inhibitor.uid) {
connection = QDBusConnection::systemBus();
}
if (connection.interface()->isServiceRegistered(inhibitor.who)) {
QDBusInterface ifc(inhibitor.who, DSS_DBUS::inhibitHintPath, DSS_DBUS::inhibitHintService, connection);
QDBusMessage msg = ifc.call("Get", qgetenv("LANG"), inhibitor.why);
if (msg.type() == QDBusMessage::ReplyMessage) {
InhibitHint inhibitHint = qdbus_cast<InhibitHint>(msg.arguments().at(0).value<QDBusArgument>());
if (!inhibitHint.why.isEmpty()) {
inhibitData.who = inhibitHint.name;
inhibitData.why = inhibitHint.why;
inhibitData.icon = inhibitHint.icon;
}
}
}
inhibitorList.append(inhibitData);
}
}
for (const InhibitWarnView::InhibitorData &data : inhibitorList) {
qCDebug(DDE_SHELL) << "Inhibitor list detail: who:" << data.who
<< ", why:" << data.why
<< ", pid:" << data.pid;
}
} else {
qCWarning(DDE_SHELL) << "DBus request reply error:" << reply.error().message();
}
} else {
qCWarning(DDE_SHELL) << "Login1 interface is invalid";
}
return inhibitorList;
}
void WarningContent::doCancelShutdownInhibit()
{
InhibitWarnView *view = qobject_cast<InhibitWarnView *>(m_warningView);
if (view && view->delayView())
return;
qCInfo(DDE_SHELL) << "Cancel shutdown inhibit";
m_model->setPowerAction(SessionBaseModel::PowerAction::None);
FullScreenBackground::setContent(LockContent::instance());
m_model->setCurrentContentType(SessionBaseModel::LockContent);
// 从lock点的power btn,不能隐藏锁屏而进入桌面
if (m_model->currentModeState() == SessionBaseModel::ModeStatus::ShutDownMode) {
m_model->setVisible(false);
m_model->setCurrentModeState(SessionBaseModel::PasswordMode);
}
}
void WarningContent::doAcceptShutdownInhibit()
{
qCInfo(DDE_SHELL) << "Accept shutdown inhibit, power action: " << m_powerAction
<< ", current mode: " << m_model->currentModeState();
InhibitWarnView *view = qobject_cast<InhibitWarnView *>(sender());
if (view) {
view->setDelayView(false);
if (view->hasInhibit()) {
switch (m_powerAction) {
case SessionBaseModel::PowerAction::RequireShutdown:
case SessionBaseModel::PowerAction::RequireUpdateShutdown:
view->setDelayView(true);
view->setInhibitConfirmMessage(tr("Closing the programs and shutting down, please wait..."), true);
break;
case SessionBaseModel::PowerAction::RequireRestart:
case SessionBaseModel::PowerAction::RequireUpdateRestart:
view->setDelayView(true);
view->setInhibitConfirmMessage(tr("Closing the programs and rebooting, please wait..."), true);
break;
case SessionBaseModel::PowerAction::RequireLogout:
view->setDelayView(true);
view->setInhibitConfirmMessage(tr("Closing the programs and logging out, please wait..."), true);
break;
default:
break;
}
}
}
#ifndef ENABLE_DSS_SNIPE
m_model->setPowerAction(m_powerAction);
#else
m_model->setPowerAction(m_powerAction, true);
#endif
if (m_model->currentModeState() != SessionBaseModel::ModeStatus::ShutDownMode
&& m_model->currentModeState() != SessionBaseModel::ModeStatus::PowerMode
&& m_powerAction != SessionBaseModel::RequireUpdateShutdown
&& m_powerAction != SessionBaseModel::RequireUpdateRestart
&& m_powerAction != SessionBaseModel::RequireShutdown
&& m_powerAction != SessionBaseModel::RequireRestart ) {
FullScreenBackground::setContent(LockContent::instance());
m_model->setCurrentContentType(SessionBaseModel::LockContent);
}
}
void WarningContent::beforeInvokeAction(bool needConfirm)
{
const QList<InhibitWarnView::InhibitorData> inhibitors = listInhibitors(m_powerAction);
const QList<std::shared_ptr<User>> &loginUsers = m_model->loginedUserList();
if (m_warningView != nullptr) {
qCInfo(DDE_SHELL) << "Before invoke action, delete warning view: " << m_warningView;
m_warningView->deleteLater();
m_warningView = nullptr;
}
// change ui
if (!inhibitors.isEmpty()) {
InhibitWarnView *view = new InhibitWarnView(m_powerAction, this);
view->setInhibitorList(inhibitors);
switch (m_powerAction) {
case SessionBaseModel::PowerAction::RequireShutdown:
case SessionBaseModel::PowerAction::RequireUpdateShutdown:
view->setInhibitConfirmMessage(tr("The programs are preventing the computer from shutting down, and forcing shut down may cause data loss.") + "\n" +
tr("To close the program, click Cancel, and then close the program."));
break;
case SessionBaseModel::PowerAction::RequireSwitchSystem:
case SessionBaseModel::PowerAction::RequireRestart:
case SessionBaseModel::PowerAction::RequireUpdateRestart:
view->setInhibitConfirmMessage(tr("The programs are preventing the computer from reboot, and forcing reboot may cause data loss.") + "\n" +
tr("To close the program, click Cancel, and then close the program."));
break;
case SessionBaseModel::PowerAction::RequireSuspend:
view->setInhibitConfirmMessage(tr("The programs are preventing the computer from suspend, and forcing suspend may cause data loss.") + "\n" +
tr("To close the program, click Cancel, and then close the program."));
break;
case SessionBaseModel::PowerAction::RequireHibernate:
view->setInhibitConfirmMessage(tr("The programs are preventing the computer from hibernate, and forcing hibernate may cause data loss.") + "\n" +
tr("To close the program, click Cancel, and then close the program."));
break;
case SessionBaseModel::PowerAction::RequireLogout:
view->setInhibitConfirmMessage(tr("The programs are preventing the computer from log out, and forcing log out may cause data loss.") + "\n" +
tr("To close the program, click Cancel, and then close the program."));
break;
default:
return;
}
// 如果有阻止关机、重启、待机或休眠的进程,则不允许手动强制执行
bool isBlock = std::any_of(inhibitors.begin(), inhibitors.end(),
[](const InhibitWarnView::InhibitorData &inhib) { return inhib.mode.compare("block") == 0; });
if (m_powerAction == SessionBaseModel::PowerAction::RequireShutdown) {
view->setAcceptReason(tr("Shut down"));
view->setAcceptVisible(!isBlock);
} else if (m_powerAction == SessionBaseModel::PowerAction::RequireRestart
|| m_powerAction == SessionBaseModel::PowerAction::RequireSwitchSystem) {
view->setAcceptReason(tr("Reboot"));
view->setAcceptVisible(!isBlock);
} else if (m_powerAction == SessionBaseModel::PowerAction::RequireSuspend) {
view->setAcceptReason(tr("Suspend"));
view->setAcceptVisible(!isBlock);
} else if (m_powerAction == SessionBaseModel::PowerAction::RequireHibernate) {
view->setAcceptReason(tr("Hibernate"));
view->setAcceptVisible(!isBlock);
} else if (m_powerAction == SessionBaseModel::PowerAction::RequireLogout) {
view->setAcceptReason(tr("Log out"));
view->setAcceptVisible(!isBlock);
} else if (m_powerAction == SessionBaseModel::PowerAction::RequireUpdateShutdown) {
view->setAcceptReason(tr("Update and Shut Down"));
view->setAcceptVisible(!isBlock);
} else if (m_powerAction == SessionBaseModel::PowerAction::RequireUpdateRestart) {
view->setAcceptReason(tr("Update and Reboot"));
view->setAcceptVisible(!isBlock);
}
m_warningView = view;
setCenterContent(m_warningView);
qCInfo(DDE_SHELL) << "Before invoke action, warning view: " << m_warningView;
connect(view, &InhibitWarnView::cancelled, this, &WarningContent::doCancelShutdownInhibit);
connect(view, &InhibitWarnView::actionInvoked, this, &WarningContent::doAcceptShutdownInhibit);
return;
}
if (loginUsers.length() > 1
&& (m_powerAction == SessionBaseModel::PowerAction::RequireShutdown
|| m_powerAction == SessionBaseModel::PowerAction::RequireRestart
|| m_powerAction == SessionBaseModel::PowerAction::RequireUpdateShutdown
|| m_powerAction == SessionBaseModel::PowerAction::RequireUpdateRestart)) {
QList<std::shared_ptr<User>> tmpList = loginUsers;
for (auto it = tmpList.begin(); it != tmpList.end();) {
if ((*it)->uid() == m_model->currentUser()->uid()) {
it = tmpList.erase(it);
continue;
}
++it;
}
MultiUsersWarningView *view = new MultiUsersWarningView(m_powerAction, this);
view->setUsers(tmpList);
if (m_powerAction == SessionBaseModel::PowerAction::RequireShutdown)
view->setAcceptReason(tr("Shut down"));
else if (m_powerAction == SessionBaseModel::PowerAction::RequireRestart)
view->setAcceptReason(tr("Reboot"));
else if (m_powerAction == SessionBaseModel::PowerAction::RequireUpdateRestart)
view->setAcceptReason(tr("Update and Reboot"));
else if (m_powerAction == SessionBaseModel::PowerAction::RequireUpdateShutdown)
view->setAcceptReason(tr("Update and Shut Down"));
m_warningView = view;
m_warningView->setFixedSize(getCenterContentSize());
setCenterContent(m_warningView);
connect(view, &MultiUsersWarningView::cancelled, this, &WarningContent::doCancelShutdownInhibit);
connect(view, &MultiUsersWarningView::actionInvoked, this, &WarningContent::doAcceptShutdownInhibit);
return;
}
if (needConfirm && (m_powerAction == SessionBaseModel::PowerAction::RequireShutdown ||
m_powerAction == SessionBaseModel::PowerAction::RequireRestart ||
m_powerAction == SessionBaseModel::PowerAction::RequireLogout)) {
InhibitWarnView *view = new InhibitWarnView(m_powerAction, this);
if (m_powerAction == SessionBaseModel::PowerAction::RequireShutdown
|| m_powerAction == SessionBaseModel::PowerAction::RequireUpdateShutdown) {
view->setAcceptReason(tr("Shut down"));
view->setInhibitConfirmMessage(tr("Are you sure you want to shut down?"));
} else if (m_powerAction == SessionBaseModel::PowerAction::RequireRestart
|| m_powerAction == SessionBaseModel::PowerAction::RequireUpdateRestart) {
view->setAcceptReason(tr("Reboot"));
view->setInhibitConfirmMessage(tr("Are you sure you want to reboot?"));
} else if (m_powerAction == SessionBaseModel::PowerAction::RequireLogout) {
view->setAcceptReason(tr("Log out"));
view->setInhibitConfirmMessage(tr("Are you sure you want to log out?"));
}
m_warningView = view;
m_warningView->setFixedSize(getCenterContentSize());
setCenterContent(m_warningView);
connect(view, &InhibitWarnView::cancelled, this, &WarningContent::doCancelShutdownInhibit);
connect(view, &InhibitWarnView::actionInvoked, this, &WarningContent::doAcceptShutdownInhibit);
return;
}
doAcceptShutdownInhibit();
}
/**
* @brief 抓取键盘,失败后继续抓取,最多尝试15次.
*
* @param exitIfFailed true: 发送失败通知,并隐藏锁屏。 false:不做任何处理。
*/
void WarningContent::tryGrabKeyboard(bool exitIfFailed)
{
#ifndef QT_DEBUG
if (!isVisible()) {
return;
}
if (m_model->isUseWayland()) {
static QDBusInterface *kwinInter = new QDBusInterface("org.kde.KWin","/KWin","org.kde.KWin", QDBusConnection::sessionBus());
if (!kwinInter || !kwinInter->isValid()) {
qCWarning(DDE_SHELL) << "Kwin interface is invalid";
m_failures = 0;
return;
}
// wayland下判断是否有应用发起grab,如果有就不锁屏
QDBusReply<bool> reply = kwinInter->call("xwaylandGrabed");
if (!reply.isValid() || !reply.value()) {
m_failures = 0;
return;
}
} else {
if (window()->windowHandle() && window()->windowHandle()->setKeyboardGrabEnabled(true)) {
m_failures = 0;
return;
}
}
if (m_failures > 5) {
qCWarning(DDE_SHELL) << "Trying ungrab keyboard in warning content";
KeyboardMonitor::instance()->ungrabKeyboard();
}
m_failures++;
if (m_failures == 15) {
qCWarning(DDE_SHELL) << "Trying to grab keyboard has exceeded the upper limit. dde-lock will quit.";
m_failures = 0;
if (!exitIfFailed) {
return;
}
qCInfo(DDE_SHELL) << "Request hide lock frame";
emit requestLockFrameHide();
return;
}
QTimer::singleShot(100, this, [this, exitIfFailed] {
tryGrabKeyboard(exitIfFailed);
});
#endif
}
void WarningContent::showEvent(QShowEvent *event)
{
tryGrabKeyboard();
QFrame::showEvent(event);
}
void WarningContent::setPowerAction(const SessionBaseModel::PowerAction action)
{
if (m_powerAction == action)
return;
m_powerAction = action;
}
bool WarningContent::supportDelayOrWait() const
{
InhibitWarnView *view = qobject_cast<InhibitWarnView *>(m_warningView);
return (view && view->hasInhibit() && view->delayView());
}
void WarningContent::mouseReleaseEvent(QMouseEvent *event)
{
doCancelShutdownInhibit();
return SessionBaseWindow::mouseReleaseEvent(event);
}
void WarningContent::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Escape:
doCancelShutdownInhibit();
break;
}
QWidget::keyPressEvent(event);
}
void WarningContent::shutdownInhibit(const SessionBaseModel::PowerAction action, bool needConfirm)
{
qCInfo(DDE_SHELL) << "Shutdown inhibit, action: " << action;
setPowerAction(action);
//检查是否允许关机
FullScreenBackground::setContent(WarningContent::instance());
m_model->setCurrentContentType(SessionBaseModel::WarningContent);
beforeInvokeAction(needConfirm);
}