File: sessionmanagercontext.cpp

package info (click to toggle)
ukui-session-manager 4.0.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,176 kB
  • sloc: cpp: 9,043; xml: 102; python: 24; sh: 15; makefile: 15
file content (519 lines) | stat: -rw-r--r-- 18,411 bytes parent folder | download
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
 * Copyright (C) 2023, KylinSoft Co., Ltd.
 *               2014  Hong Jen Yee (PCMan) <pcman.tw@gmail.com>.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
#include "sessionmanagercontext.h"
#include "sessiondbusadaptor.h"
#include <QDBusMetaType>
#include <QDBusAbstractAdaptor>
#include <QDebug>

SessionManagerDBusContext::SessionManagerDBusContext(ModuleManager *manager, QObject *parent)
    : QObject(parent)
    , mManager(manager)
    , minhibit(new usminhibit())
    , m_systemdProvider(new SystemdProvider())
    , m_ukuiProvider(new UKUIProvider())
    , m_serviceWatcher(new QDBusServiceWatcher(this))
{
    new SessionDBusAdaptor(this);

    connect(mManager, &ModuleManager::moduleStateChanged, this , &SessionManagerDBusContext::moduleStateChanged);
    connect(mManager, &ModuleManager::finished, this, &SessionManagerDBusContext::emitPrepareForPhase2);
    connect(minhibit, &usminhibit::inhibitRemove, this, &SessionManagerDBusContext::simulateUserActivity);
    connect(minhibit, &usminhibit::inhibitAdd, this, &SessionManagerDBusContext::simulateUserActivity);

    m_serviceWatcher->setConnection(QDBusConnection::sessionBus());
    m_serviceWatcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration);
    connect(m_serviceWatcher, &QDBusServiceWatcher::serviceUnregistered, this, &SessionManagerDBusContext::on_serviceUnregistered);

    //注册类型
    qDBusRegisterMetaType<InhibitInfo::InhibitorInfo>();
    qDBusRegisterMetaType<QVector<InhibitInfo::InhibitorInfo>>();
    qDBusRegisterMetaType<SystemdInhibitor::Inhibitor>();
}

SessionManagerDBusContext::~SessionManagerDBusContext()
{
}


Q_NOREPLY void SessionManagerDBusContext::startupfinished(const QString &appName ,const QString &string)
{
    return mManager->startupfinished(appName, string);
}

bool SessionManagerDBusContext::canSwitch()
{
    if (Ukuilockinfo::getCfgValue(Ukuilockinfo::buttonType::switchuserBtn) && m_systemdProvider->canAction(UkuiPower::PowerSwitchUser)) {
        return true;
    }

    return false;
}

bool SessionManagerDBusContext::canLockscreen()
{
    if (Ukuilockinfo::getCfgValue(Ukuilockinfo::buttonType::lockscreenBtn)) {
        return true;
    }

    return false;
}

bool SessionManagerDBusContext::canHibernate()
{
    if (Ukuilockinfo::getCfgValue(Ukuilockinfo::buttonType::hibernateBtn) && m_systemdProvider->canAction(UkuiPower::PowerHibernate)) {
        return true;
    }

    return false;
}

bool SessionManagerDBusContext::canSuspend()
{
    if (Ukuilockinfo::getCfgValue(Ukuilockinfo::buttonType::suspendBtn) && m_systemdProvider->canAction(UkuiPower::PowerSuspend)) {
        return true;
    }

    return false;
}


bool SessionManagerDBusContext::canLogout()
{
    //暂时都返回true
    if (Ukuilockinfo::getCfgValue(Ukuilockinfo::buttonType::logoutBtn)) {
        return true;
    }

    return false;
}

bool SessionManagerDBusContext::canReboot()
{
    if (Ukuilockinfo::getCfgValue(Ukuilockinfo::buttonType::rebootBtn) && m_systemdProvider->canAction(UkuiPower::PowerReboot)) {
        return true;
    }

    return false;
}

bool SessionManagerDBusContext::canPowerOff()
{
    if (Ukuilockinfo::getCfgValue(Ukuilockinfo::buttonType::shutdownBtn) && m_systemdProvider->canAction(UkuiPower::PowerShutdown)) {
        return true;
    }

    return false;
}

Q_NOREPLY void SessionManagerDBusContext::switchUser()
{
    m_systemdProvider->doAction(UkuiPower::PowerSwitchUser);
}

Q_NOREPLY void SessionManagerDBusContext::hibernate()
{
    m_systemdProvider->doAction(UkuiPower::PowerHibernate);
}

Q_NOREPLY void SessionManagerDBusContext::suspend()
{
    m_systemdProvider->doAction(UkuiPower::PowerSuspend);
}

Q_NOREPLY void SessionManagerDBusContext::logout()
{
    qDebug() << "ukui-session logout inteface called";
    if (mManager->getIsShutingDown()) {
        qDebug() << "already performing logout";
        return;
    }

    mManager->setIsShutingDown(true);

    QDBusInterface ukuismserverFace("org.ukui.ukuismserver",
                                    "/UKUISMServer",
                                    "org.ukui.ukuismserver",
                                    QDBusConnection::sessionBus());

    if (!ukuismserverFace.isValid()) {
        qDebug() << "ukuisminterface not valid";
        return;
    }

//    QDBusReply<bool> isCloseSession = ukuismserverFace.call("isCloseSession");
//    if (isCloseSession.value()) {
//        qDebug() << "already performing close session";
//        return;
//    }

//    OrgKdeKSMServerInterfaceInterface ksmserverIface(QStringLiteral("org.kde.ksmserver"), QStringLiteral("/KSMServer"), QDBusConnection::sessionBus());
    //QDBusAbstractInterface有超时机制,默认是25秒,25秒后没有返回值,会给出一个带有错误信息的返回值,供调用方判断做出下一步动作,也正是因此KDE桌面不会在某个应用的提醒保存页面一直
    //停留,而是会继续往下执行注销操作,可以通过setTimerout设置超时时间,实验证明确实如此
    ukuismserverFace.setTimeout(15000);
    QList<QVariant> argumentList;
    QDBusPendingReply<bool> closeSessionReply = ukuismserverFace.asyncCallWithArgumentList(QStringLiteral("closeSession"), argumentList);
    auto watcher = new QDBusPendingCallWatcher(closeSessionReply, this);
    connect(watcher, &QDBusPendingCallWatcher::finished, this, [closeSessionReply, watcher, this]() {
        watcher->deleteLater();
        if (closeSessionReply.isError()) {
            qDebug() << "ksmserver failed to complete close session";
        }
        if (closeSessionReply.value()) {
            qDebug() << "ukuismserver close session completed";
            logoutComplete();
        } else {
            mManager->setIsShutingDown(false);
            qDebug() << "logout cancelled";
        }
    });
}

Q_NOREPLY void SessionManagerDBusContext::reboot()
{
    qDebug() << "ukui-session reboot inteface called";
    if (mManager->getIsShutingDown()) {
        qDebug() << "already performing reboot";
        return;
    }

    mManager->setIsShutingDown(true);

    QDBusInterface ukuismserverFace("org.ukui.ukuismserver",
                                    "/UKUISMServer",
                                    "org.ukui.ukuismserver",
                                    QDBusConnection::sessionBus());

    if (!ukuismserverFace.isValid()) {
        qDebug() << "ukuisminterface not valid";
        return;
    }

//    OrgKdeKSMServerInterfaceInterface ksmserverIface(QStringLiteral("org.kde.ksmserver"), QStringLiteral("/KSMServer"), QDBusConnection::sessionBus());
    //QDBusAbstractInterface有超时机制,默认是25秒,25秒后没有返回值,会给出一个带有错误信息的返回值,供调用方判断做出下一步动作,也正是因此KDE桌面不会在某个应用的提醒保存页面一直
    //停留,而是会继续往下执行注销操作,可以通过setTimerout设置超时时间,实验证明确实如此
    ukuismserverFace.setTimeout(15000);
    QList<QVariant> argumentList;
    QDBusPendingReply<bool> closeSessionReply = ukuismserverFace.asyncCallWithArgumentList(QStringLiteral("closeSession"), argumentList);
    auto watcher = new QDBusPendingCallWatcher(closeSessionReply, this);
    connect(watcher, &QDBusPendingCallWatcher::finished, this, [closeSessionReply, watcher, this]() {
        watcher->deleteLater();
        if (closeSessionReply.isError()) {
            qDebug() << "ksmserver failed to complete close session";
        }
        if (closeSessionReply.value()) {
            qDebug() << "ukuismserver close session completed";
            rebootComplete();
        } else {
            mManager->setIsShutingDown(false);
            qDebug() << "logout cancelled";
        }
    });
}

Q_NOREPLY void SessionManagerDBusContext::powerOff()
{
    qDebug() << "ukui-session reboot inteface called";
    if (mManager->getIsShutingDown()) {
        qDebug() << "already performing poweroff";
        return;
    }

    mManager->setIsShutingDown(true);

    QDBusInterface ukuismserverFace("org.ukui.ukuismserver",
                                    "/UKUISMServer",
                                    "org.ukui.ukuismserver",
                                    QDBusConnection::sessionBus());

    if (!ukuismserverFace.isValid()) {
        qDebug() << "ukuisminterface not valid";
        return;
    }

//    OrgKdeKSMServerInterfaceInterface ksmserverIface(QStringLiteral("org.kde.ksmserver"), QStringLiteral("/KSMServer"), QDBusConnection::sessionBus());
    //QDBusAbstractInterface有超时机制,默认是25秒,25秒后没有返回值,会给出一个带有错误信息的返回值,供调用方判断做出下一步动作,也正是因此KDE桌面不会在某个应用的提醒保存页面一直
    //停留,而是会继续往下执行注销操作,可以通过setTimerout设置超时时间,实验证明确实如此
    ukuismserverFace.setTimeout(15000);
    QList<QVariant> argumentList;
    QDBusPendingReply<bool> closeSessionReply = ukuismserverFace.asyncCallWithArgumentList(QStringLiteral("closeSession"), argumentList);
    auto watcher = new QDBusPendingCallWatcher(closeSessionReply, this);
    connect(watcher, &QDBusPendingCallWatcher::finished, this, [closeSessionReply, watcher, this]() {
        watcher->deleteLater();
        if (closeSessionReply.isError()) {
            qDebug() << "ksmserver failed to complete close session";
        }
        if (closeSessionReply.value()) {
            qDebug() << "ukuismserver close session completed";
            shutdownComplete();
        } else {
            mManager->setIsShutingDown(false);
            qDebug() << "logout cancelled";
        }
    });
}

Q_NOREPLY void SessionManagerDBusContext::startModule(const QString& name)
{
    mManager->startProcess(name, true);
}

Q_NOREPLY void SessionManagerDBusContext::stopModule(const QString& name)
{
    mManager->stopProcess(name);
}

bool SessionManagerDBusContext::startApp(const QString &name, const QStringList &args)
{
    return mManager->startProcess(name, args);
}

uint SessionManagerDBusContext::Inhibit(QString app_id, quint32 toplevel_xid, QString reason, quint32 flags)
{
    uint result = minhibit->addInhibit(app_id, toplevel_xid, reason, flags);
    if (result < 0) {
        return 0;
    }

    QString dbusService = message().service();
    qDebug() << "SessionManagerDBusContext message().service():" << dbusService;

    QStringList keys = m_hashInhibitionServices.keys();
    if (!keys.contains(dbusService)) {
        QList<quint32> cookies;
        cookies.append(result);
        m_hashInhibitionServices.insert(dbusService, cookies);
        m_serviceWatcher->addWatchedService(dbusService);
        qDebug() << "m_serviceWatcher services:..." << m_serviceWatcher->watchedServices();
    } else {
        for (auto iter = m_hashInhibitionServices.begin(); iter != m_hashInhibitionServices.end(); iter++) {
            qDebug() << "Inhibit iter.key()" << iter.key() << dbusService;

            if (iter.key() == dbusService) {
                QList<quint32> cookies = iter.value();
                if (!cookies.contains(result)) {
                    cookies.append(result);
                    m_hashInhibitionServices[dbusService] = cookies;
                    break;
                }
            }
        }
    }
    qDebug() << "Inhibit m_hashInhibitionServices..." << m_hashInhibitionServices;
    emit this->inhibitadded(flags);
    return result;
}

Q_NOREPLY void SessionManagerDBusContext::Uninhibit(uint cookie)
{
    uint result = minhibit->unInhibit(cookie);
    if (result > 0) {
        emit inhibitremove(result);
    }
    for (auto iter = m_hashInhibitionServices.begin(); iter != m_hashInhibitionServices.end(); iter++) {
        QList<quint32> cookies = iter.value();
        for (int i = 0; i < cookies.length(); i++) {
            if (cookie == cookies[i]) {
                qDebug() << "Uninhibit cookies:" << cookies << "cookie:" << cookie;
                if (cookies.length() > 1) {
                    cookies.removeAt(i);
                    m_hashInhibitionServices[iter.key()] = cookies;
                } else {
                    m_serviceWatcher->removeWatchedService(iter.key());
                    m_hashInhibitionServices.remove(iter.key());
                    qDebug() << "Uninhibit m_hashInhibitionServices...:" << m_hashInhibitionServices;
                }
                qDebug() << "Uninhibit m_hashInhibitionServices:" << m_hashInhibitionServices;
                return;
            }
        }
    }
}

QStringList SessionManagerDBusContext::GetInhibitors()
{
    return minhibit->getInhibitor();
}

QVector<InhibitInfo::InhibitorInfo> SessionManagerDBusContext::ListInhibitor(QString type)
{
    QVector<InhibitInfo::InhibitorInfo> result;

    if (type == QString("logout")) {
        result = Ukuilockinfo::listInhibitorInfo(Ukuilockinfo::InhibitorType::logout);
    } else if (type == QString("sleep")) {
        result = Ukuilockinfo::listInhibitorInfo(Ukuilockinfo::InhibitorType::suspend);
    } else if (type == QString("shutdown")) {
        result = Ukuilockinfo::listInhibitorInfo(Ukuilockinfo::InhibitorType::shutdown);
    }

    return result;
}

bool SessionManagerDBusContext::IsSessionRunning()
{
    QString xdg_session_desktop = qgetenv("XDG_SESSION_DESKTOP").toLower();
    if (xdg_session_desktop == "ukui" || xdg_session_desktop == "ukui-wayland") {
        return true;
    }
    return false;
}

QString SessionManagerDBusContext::GetSessionName()
{
    QString xdg_session_desktop = qgetenv("XDG_SESSION_DESKTOP").toLower();
    if (xdg_session_desktop == "ukui" || xdg_session_desktop == "ukui-wayland") {
        return "ukui-session";
    }

    return "error";
}

bool SessionManagerDBusContext::IsInhibited(quint32 flags)
{
    return minhibit->isInhibited(flags);
}

void SessionManagerDBusContext::setSessionEnv(const QString &key, const QString &value)
{
    qDebug() << "set env " << key << " as " << value;
    qputenv(key.toLatin1(), value.toLatin1());
}

Q_NOREPLY void SessionManagerDBusContext::emitStartLogout()
{
    qDebug() << "emit  StartLogout";
    emit StartLogout();
}

Q_NOREPLY void SessionManagerDBusContext::emitPrepareForSwitchuser()
{
    qDebug() << "emit  PrepareForSwitchuser";
    emit PrepareForSwitchuser();
}

Q_NOREPLY void SessionManagerDBusContext::emitPrepareForPhase2()
{
    qDebug() << "emit  PrepareForPhase2";
    emit PrepareForPhase2();
}

Q_NOREPLY void SessionManagerDBusContext::simulateUserActivity()
{
    qDebug() << "simulate User Activity";
    KIdleTime::instance()->simulateUserActivity();
}

void SessionManagerDBusContext::on_serviceUnregistered(const QString &serviceName)
{
    qDebug() << "onServiceUnregistered..." << serviceName;

    for (auto iter = m_hashInhibitionServices.begin(); iter != m_hashInhibitionServices.end(); iter++) {
        if (iter.key() == serviceName) {
            QList<quint32> cookies = iter.value();
            for (auto i = 0; i < cookies.length(); i++) {
                quint32 cookie = cookies[i];
                qDebug() << "on_serviceUnregistered cookie:" << cookie;
                Uninhibit(cookie);
            }
            return;
        }
    }
    qDebug() << "on_serviceUnregistered unfind serviceName:" << serviceName;
}

void SessionManagerDBusContext::logoutComplete()
{
    //注销前执行指定目录脚本
    QStringList list = listFileList();
    if (list.isEmpty()) {
        qDebug() << "logouting file list is empty or dir is not exits.";
    } else {
        execPro(list);
    }

    //调用systemd的注销
    QDBusInterface face("org.freedesktop.login1",
                        "/org/freedesktop/login1/session/self",
                        "org.freedesktop.login1.Session",
                        QDBusConnection::systemBus());

    qDebug() << "call sytemd login1 teminate session";
    face.call("Terminate");
}

void SessionManagerDBusContext::rebootComplete()
{
    //注销前执行指定目录脚本
    QStringList list = listFileList();
    if (list.isEmpty()) {
        qDebug() << "logouting file list is empty or dir is not exits.";
    } else {
        execPro(list);
    }

    //调用systemd的reboot
    qDebug() << "complete xsmp logout, call Reboot";
    this->m_systemdProvider->doAction(UkuiPower::PowerReboot);
}

void SessionManagerDBusContext::shutdownComplete()
{
    //注销前执行指定目录脚本
    QStringList list = listFileList();
    if (list.isEmpty()) {
        qDebug() << "logouting file list is empty or dir is not exits.";
    } else {
        execPro(list);
    }

    //调用systemd的shutdown
    qDebug() << "complete xsmp logout, call powerOff";
    this->m_systemdProvider->doAction(UkuiPower::PowerShutdown);
}

QStringList SessionManagerDBusContext::listFileList()
{
    QDir dir("/etc/ukui/ukui-session/logout/");
    if (!dir.exists()) {
          qWarning("Cannot find the example directory");
          return QStringList();
    }
    dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
    dir.setSorting(QDir::Size | QDir::Reversed);

    QFileInfoList list = dir.entryInfoList();
    QStringList filelist;

    for (int i = list.size() - 1 ; i >= 0 ; --i) {
        QFileInfo fileInfo = list.at(i);
        filelist<<fileInfo.fileName();
    }
    return filelist;
}

void SessionManagerDBusContext::execPro(QStringList proList)
{
    for (int i = 0 ; i < proList.size(); ++i){
        qDebug() << "script name is " << proList.at(i);
        QString pro = "/etc/ukui/ukui-session/logout/" + proList.at(i);
        QProcess::startDetached(pro ,QStringList());
    }
}