-
Notifications
You must be signed in to change notification settings - Fork 4
/
mainwindow.cpp
198 lines (168 loc) · 7.19 KB
/
mainwindow.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
#include <QApplication>
#include <QFileInfo>
#include <QLayout>
#include <QProcess>
#include <QPushButton>
#include <QSettings>
#include <QStandardPaths>
#include "mainwindow.h"
MainWindow::MainWindow(const QCommandLineParser &parser, QWidget *parent)
: QDialog(parent),
horizontal {parser.isSet("horizontal")},
defaultIconSize {50},
defaultSpacing {3}
{
// Load icons from settings
QSettings userSettings(QSettings::UserScope, QApplication::organizationName(), QApplication::applicationName());
QSettings systemSettings("/etc/exit-options.conf", QSettings::IniFormat);
iconSize = userSettings.value("IconSize", systemSettings.value("IconSize", defaultIconSize).toUInt()).toInt();
QString sessionDesktop = qgetenv("XDG_SESSION_DESKTOP").toLower();
QString labelRestartDE;
if (sessionDesktop == "fluxbox") {
labelRestartDE = tr("Restart Fluxbox");
} else if (sessionDesktop == "icewm-session") {
labelRestartDE = tr("Restart IceWM");
}
auto *pushRestartDE = createButton("RestartFluxbox", "/usr/share/exit-options/awesome/refresh.png", labelRestartDE,
on_pushRestartDE);
auto *pushExit
= createButton("LogoutIcon", "/usr/share/exit-options/awesome/logout.png", tr("Log Out"), on_pushExit);
auto *pushLock
= createButton("LockIcon", "/usr/share/exit-options/awesome/lock.png", tr("Lock Screen"), on_pushLock);
auto *pushRestart
= createButton("RebootIcon", "/usr/share/exit-options/awesome/reboot.png", tr("Reboot"), on_pushRestart);
auto *pushShutdown
= createButton("ShutdownIcon", "/usr/share/exit-options/awesome/shutdown.png", tr("Shutdown"), on_pushShutdown);
auto *pushSleep
= createButton("SuspendIcon", "/usr/share/exit-options/awesome/suspend.png", tr("Suspend"), on_pushSleep);
if (!horizontal && !parser.isSet("vertical")) {
horizontal = userSettings.value("layout", systemSettings.value("layout").toString()).toString() == "horizontal";
}
auto *layout
= horizontal ? static_cast<QLayout *>(new QHBoxLayout(this)) : static_cast<QLayout *>(new QVBoxLayout(this));
// Add pushRestartDE?
if (sessionDesktop == "fluxbox" || sessionDesktop == "icewm-session") {
layout->addWidget(pushRestartDE);
}
layout->addWidget(pushLock);
layout->addWidget(pushExit);
// Add pushSleep?
if (!isRaspberryPi()) {
layout->addWidget(pushSleep);
}
layout->addWidget(pushRestart);
layout->addWidget(pushShutdown);
// Set layout margins and spacing
layout->setMargin(userSettings.value("Margin", systemSettings.value("Margin", defaultSpacing).toInt()).toInt());
layout->setSpacing(userSettings.value("Spacing", systemSettings.value("Spacing", defaultSpacing).toInt()).toInt());
setLayout(layout);
// Set window features
setSizeGripEnabled(true);
setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
connect(QApplication::instance(), &QApplication::aboutToQuit, this, &MainWindow::saveSettings);
// Restore or reset geometry
show(); // Can't save geometry if not shown
if (userSettings.contains("Geometry") || userSettings.contains("geometry")) {
const QByteArray geometry = saveGeometry();
restoreGeometry(userSettings.value("Geometry", userSettings.value("geometry").toByteArray()).toByteArray());
// If too wide/tall reset geometry
const auto factor = 0.6;
if ((horizontal && size().height() >= size().width() * factor)
|| (!horizontal && size().width() >= size().height() * factor)) {
restoreGeometry(geometry);
}
}
}
void MainWindow::on_pushLock()
{
QProcess::startDetached("dm-tool", {"switch-to-greeter"});
}
void MainWindow::on_pushExit()
{
QString sessionDesktop = qgetenv("XDG_SESSION_DESKTOP").toLower();
bool commandExecuted = false;
auto executeCommand = [&](const QString &program, const QStringList &arguments) -> bool {
if (QStandardPaths::findExecutable(program).isEmpty()) {
return false;
}
return QProcess::execute(program, arguments);
};
// Map format: {desktop, {program, {arguments}}
static const QMap<QString, QPair<QString, QStringList>> desktopCommands
= {{"fluxbox", {"fluxbox-remote", {"exit"}}},
{"xfce", {"xfce4-session-logout", {"--logout"}}},
{"kde", {"qdbus", {"org.kde.ksmserver", "/KSMServer", "logout", "0", "0", "0"}}},
{"i3", {"i3-msg", {"exit"}}}};
if (desktopCommands.contains(sessionDesktop)) {
const QPair<QString, QStringList> &commandPair = desktopCommands.value(sessionDesktop);
const QString &program = commandPair.first;
const QStringList arguments = commandPair.second;
if (sessionDesktop == "fluxbox") {
if (executeCommand(program, arguments)) {
executeCommand("killall", {"fluxbox"});
commandExecuted = true;
}
} else {
commandExecuted = executeCommand(program, arguments);
}
}
if (!commandExecuted) {
const QString user = qgetenv("USER");
if (!executeCommand("loginctl", {"terminate-user", user})) {
executeCommand("pkill", {"-KILL", "-u", user});
}
}
}
void MainWindow::on_pushSleep()
{
QProcess::startDetached("sudo", {"-n", "pm-suspend"});
}
void MainWindow::saveSettings()
{
QSettings userSettings(QSettings::UserScope, QApplication::organizationName(), QApplication::applicationName());
userSettings.setValue("geometry", saveGeometry());
userSettings.setValue("layout", horizontal ? "horizontal" : "vertical");
}
void MainWindow::reject()
{
QApplication::quit();
}
QPushButton *MainWindow::createButton(const QString &iconName, const QString &iconLocation, const QString &toolTip,
const std::function<void()> &action)
{
QSettings userSettings(QSettings::UserScope, QApplication::organizationName(), QApplication::applicationName());
QSettings systemSettings("/etc/exit-options.conf", QSettings::IniFormat);
QString icon = userSettings.value(iconName, systemSettings.value(iconName).toString()).toString();
if (!QFileInfo::exists(icon)) {
icon = iconLocation;
}
auto *btn = new QPushButton(QIcon(icon), QString());
btn->setToolTip(toolTip);
btn->setFlat(true);
btn->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
btn->setIconSize(QSize(iconSize, iconSize));
connect(btn, &QPushButton::clicked, this, action);
return btn;
}
void MainWindow::on_pushRestart()
{
QProcess::startDetached("sudo", {"-n", "reboot"});
}
void MainWindow::on_pushRestartDE()
{
QString sessionDesktop = qgetenv("XDG_SESSION_DESKTOP").toLower();
if (sessionDesktop == "fluxbox") {
QProcess::startDetached("fluxbox-remote", {"restart"});
QProcess::startDetached("idesktoggle", {"idesk", "refresh"});
} else if (sessionDesktop == "icewm-session") {
QProcess::execute("icewm", {"-r"});
}
}
void MainWindow::on_pushShutdown()
{
QProcess::startDetached("sudo", {"-n", "/sbin/halt", "-p"});
}
bool MainWindow::isRaspberryPi()
{
return QFile::exists("/etc/rpi-issue");
}