Changeset View
Changeset View
Standalone View
Standalone View
logout-greeter/greeter.cpp
Show All 25 Lines | |||||
26 | #include "greeter.h" | 26 | #include "greeter.h" | ||
27 | 27 | | |||
28 | #include <QDebug> | 28 | #include <QDebug> | ||
29 | #include <QScreen> | 29 | #include <QScreen> | ||
30 | #include <QApplication> | 30 | #include <QApplication> | ||
31 | 31 | | |||
32 | #include "shutdowndlg.h" | 32 | #include "shutdowndlg.h" | ||
33 | 33 | | |||
34 | #include "logoutpromptadaptor.h" | ||||
35 | #include "ksmserveriface.h" | ||||
36 | | ||||
34 | #include <KQuickAddons/QtQuickSettings> | 37 | #include <KQuickAddons/QtQuickSettings> | ||
35 | #include <KWindowSystem> | 38 | #include <KWindowSystem> | ||
36 | 39 | | |||
37 | #include <KWayland/Client/connection_thread.h> | 40 | #include <KWayland/Client/connection_thread.h> | ||
38 | #include <KWayland/Client/registry.h> | 41 | #include <KWayland/Client/registry.h> | ||
39 | #include <KWayland/Client/plasmashell.h> | 42 | #include <KWayland/Client/plasmashell.h> | ||
40 | 43 | | |||
41 | #include <unistd.h> | 44 | Greeter::Greeter(bool shutdownAllowed) | ||
42 | | ||||
43 | Greeter::Greeter(int fd, bool shutdownAllowed, bool choose, KWorkSpace::ShutdownType type) | | |||
44 | : QObject() | 45 | : QObject() | ||
45 | , m_fd(fd) | | |||
46 | , m_shutdownAllowed(shutdownAllowed) | 46 | , m_shutdownAllowed(shutdownAllowed) | ||
47 | , m_choose(choose) | | |||
48 | , m_shutdownType(type) | | |||
49 | , m_waylandPlasmaShell(nullptr) | 47 | , m_waylandPlasmaShell(nullptr) | ||
50 | { | 48 | { | ||
49 | new LogoutPromptAdaptor(this); | ||||
50 | QDBusConnection::sessionBus().registerObject(QStringLiteral("/LogoutPrompt"), this); | ||||
51 | QDBusConnection::sessionBus().registerService(QStringLiteral("org.kde.LogoutPrompt")); | ||||
51 | } | 52 | } | ||
52 | 53 | | |||
53 | Greeter::~Greeter() | 54 | Greeter::~Greeter() | ||
54 | { | 55 | { | ||
55 | qDeleteAll(m_dialogs); | 56 | qDeleteAll(m_dialogs); | ||
56 | } | 57 | } | ||
57 | 58 | | |||
58 | void Greeter::setupWaylandIntegration() | 59 | void Greeter::setupWaylandIntegration() | ||
Show All 19 Lines | |||||
78 | 79 | | |||
79 | void Greeter::init() | 80 | void Greeter::init() | ||
80 | { | 81 | { | ||
81 | setupWaylandIntegration(); | 82 | setupWaylandIntegration(); | ||
82 | foreach (QScreen *screen, qApp->screens()) { | 83 | foreach (QScreen *screen, qApp->screens()) { | ||
83 | adoptScreen(screen); | 84 | adoptScreen(screen); | ||
84 | } | 85 | } | ||
85 | connect(qApp, &QGuiApplication::screenAdded, this, &Greeter::adoptScreen); | 86 | connect(qApp, &QGuiApplication::screenAdded, this, &Greeter::adoptScreen); | ||
87 | m_running = true; | ||||
86 | } | 88 | } | ||
87 | 89 | | |||
88 | void Greeter::adoptScreen(QScreen* screen) | 90 | void Greeter::adoptScreen(QScreen* screen) | ||
89 | { | 91 | { | ||
90 | // TODO: last argument is the theme, maybe add command line option for it? | 92 | // TODO: last argument is the theme, maybe add command line option for it? | ||
91 | KSMShutdownDlg *w = new KSMShutdownDlg(nullptr, m_shutdownAllowed, m_choose, m_shutdownType, m_waylandPlasmaShell); | 93 | KSMShutdownDlg *w = new KSMShutdownDlg(nullptr, m_shutdownAllowed, m_shutdownType, m_waylandPlasmaShell); | ||
92 | w->installEventFilter(this); | 94 | w->installEventFilter(this); | ||
93 | m_dialogs << w; | 95 | m_dialogs << w; | ||
94 | 96 | | |||
95 | QObject::connect(screen, &QObject::destroyed, w, [w, this] { | 97 | QObject::connect(screen, &QObject::destroyed, w, [w, this] { | ||
96 | m_dialogs.removeOne(w); | 98 | m_dialogs.removeOne(w); | ||
97 | w->deleteLater(); | 99 | w->deleteLater(); | ||
98 | }); | 100 | }); | ||
99 | connect(w, &KSMShutdownDlg::rejected, this, &Greeter::rejected); | 101 | connect(w, &KSMShutdownDlg::rejected, this, &Greeter::rejected); | ||
100 | connect(w, &KSMShutdownDlg::accepted, this, | 102 | connect(w, &KSMShutdownDlg::accepted, this, [w]() { | ||
101 | [w, this] { | 103 | OrgKdeKSMServerInterfaceInterface ksmserver(QStringLiteral("org.kde.ksmserver"), QStringLiteral("/KSMServer"), QDBusConnection::sessionBus()); | ||
102 | if (m_fd != -1) { | 104 | ksmserver.logout(KWorkSpace::ShutdownConfirmNo, w->shutdownType(), KWorkSpace::ShutdownModeDefault); | ||
103 | QFile f; | 105 | }); | ||
104 | if (f.open(m_fd, QFile::WriteOnly, QFile::AutoCloseHandle)) { | | |||
105 | f.write(QByteArray::number(int(w->shutdownType()))); | | |||
106 | f.close(); | | |||
107 | } | | |||
108 | } | | |||
109 | QApplication::quit(); | | |||
110 | } | | |||
111 | ); | | |||
112 | w->setScreen(screen); | 106 | w->setScreen(screen); | ||
113 | w->setGeometry(screen->geometry()); | 107 | w->setGeometry(screen->geometry()); | ||
114 | w->init(); | 108 | w->init(); | ||
115 | } | 109 | } | ||
116 | 110 | | |||
117 | void Greeter::rejected() | 111 | void Greeter::rejected() | ||
118 | { | 112 | { | ||
119 | if (m_fd != -1) { | | |||
120 | close(m_fd); | | |||
121 | } | | |||
122 | QApplication::exit(1); | 113 | QApplication::exit(1); | ||
123 | } | 114 | } | ||
124 | 115 | | |||
125 | bool Greeter::eventFilter(QObject *watched, QEvent *event) | 116 | bool Greeter::eventFilter(QObject *watched, QEvent *event) | ||
126 | { | 117 | { | ||
127 | if (qobject_cast<KSMShutdownDlg*>(watched)) { | 118 | if (qobject_cast<KSMShutdownDlg*>(watched)) { | ||
128 | if (event->type() == QEvent::MouseButtonPress) { | 119 | if (event->type() == QEvent::MouseButtonPress) { | ||
129 | // check that the position is on no window | 120 | // check that the position is on no window | ||
130 | QMouseEvent *me = static_cast<QMouseEvent*>(event); | 121 | QMouseEvent *me = static_cast<QMouseEvent*>(event); | ||
131 | for (auto it = m_dialogs.constBegin(); it != m_dialogs.constEnd(); ++it) { | 122 | for (auto it = m_dialogs.constBegin(); it != m_dialogs.constEnd(); ++it) { | ||
132 | if ((*it)->geometry().contains(me->globalPos())) { | 123 | if ((*it)->geometry().contains(me->globalPos())) { | ||
133 | return false; | 124 | return false; | ||
134 | } | 125 | } | ||
135 | } | 126 | } | ||
136 | // click outside, close | 127 | // click outside, close | ||
137 | rejected(); | 128 | rejected(); | ||
138 | } | 129 | } | ||
139 | } | 130 | } | ||
140 | return false; | 131 | return false; | ||
141 | } | 132 | } | ||
133 | | ||||
134 | void Greeter::promptLogout() | ||||
135 | { | ||||
136 | if (m_running) { | ||||
137 | return; | ||||
138 | } | ||||
139 | m_shutdownType = KWorkSpace::ShutdownTypeLogout; | ||||
140 | init(); | ||||
141 | } | ||||
142 | | ||||
143 | void Greeter::promptShutDown() | ||||
144 | { | ||||
145 | if (m_running) { | ||||
146 | return; | ||||
147 | } | ||||
148 | m_shutdownType = KWorkSpace::ShutdownTypeHalt; | ||||
149 | init(); | ||||
150 | } | ||||
151 | | ||||
152 | void Greeter::promptReboot() | ||||
153 | { | ||||
154 | if (m_running) { | ||||
155 | return; | ||||
156 | } | ||||
157 | m_shutdownType = KWorkSpace::ShutdownTypeReboot; | ||||
158 | init(); | ||||
159 | } | ||||
160 | | ||||
161 | |