-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
115 lines (102 loc) · 5.1 KB
/
main.cpp
File metadata and controls
115 lines (102 loc) · 5.1 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
#include <QApplication>
#include <QCommandLineParser>
#include <QDebug>
#include <QLibraryInfo>
#include <QLocale>
#include <QProcess>
#include <QSettings>
#include <QTimer>
#include <QTranslator>
#include "mainwindow.h"
#include "common.h"
#include <chrono>
using namespace std::chrono_literals;
int main(int argc, char *argv[])
{
// Set Qt platform to XCB (X11) if not already set and we're in X11 environment
if (qEnvironmentVariableIsEmpty("QT_QPA_PLATFORM")) {
if (!qEnvironmentVariableIsEmpty("DISPLAY") && qEnvironmentVariableIsEmpty("WAYLAND_DISPLAY")) {
qputenv("QT_QPA_PLATFORM", "xcb");
}
}
QApplication app(argc, argv);
QApplication::setOrganizationName("MX-Linux");
QTranslator qtTran;
if (qtTran.load("qt_" + QLocale().name(), QLibraryInfo::path(QLibraryInfo::TranslationsPath))) {
QApplication::installTranslator(&qtTran);
}
QTranslator qtBaseTran;
if (qtBaseTran.load("qtbase_" + QLocale().name(), QLibraryInfo::path(QLibraryInfo::TranslationsPath))) {
QApplication::installTranslator(&qtBaseTran);
}
QTranslator appTran;
if (appTran.load(QApplication::applicationName() + "_" + QLocale().name(),
DATA_DIR + "/locale")) {
QApplication::installTranslator(&appTran);
}
// Command line parser setup
QCommandLineParser parser;
parser.setApplicationDescription(QObject::tr("Pop-up with exit options for MX Fluxbox"));
parser.addOption({{"v", "vertical"}, QObject::tr("Display buttons in a vertical window")});
parser.addOption({{"h", "horizontal"}, QObject::tr("Display buttons in a horizontal window")});
parser.addOption({{"t", "timeout"}, QObject::tr("Timeout duration in seconds"), "seconds", "5"});
parser.addOption({"help", QObject::tr("use -v, --vertical to display buttons vertically\nuse "
"-h, --horizontal to display buttons horizontally.")});
parser.process(app);
if (parser.isSet("help")) {
qDebug().noquote() << QObject::tr("Pop-up with exit options for MX Fluxbox");
qDebug().noquote() << QObject::tr("Usage: exit-options [options]\n");
qDebug().noquote() << QObject::tr("Options:");
qDebug().noquote() << QObject::tr(" -h, --horizontal\t Option to display buttons horizontally");
qDebug().noquote() << QObject::tr(" -v, --vertical\t Option to display buttons vertically");
qDebug().noquote() << QObject::tr(" -t, --timeout <sec>\t Timeout duration in seconds\n");
qDebug().noquote() << QObject::tr(
"The display orientation option used will be remembered and used the next time you start the app");
qDebug().noquote() << QObject::tr("Alternatively, set the option 'layout=horizontal' or 'layout=vertical' in "
"~/.config/MX-Linux/exit-options.conf")
+ "\n";
qDebug().noquote() << QObject::tr(
"To set the timeout in exit-options.conf use 'timeout=X' where X is the timeout in seconds.");
qDebug().noquote() << QObject::tr("You can also use 'timeout=off' to turn the timeout off.") + "\n";
qDebug().noquote()
<< QObject::tr("You can define custom icons by adding IconName=/path/iconame.ext in the exit-options.conf "
"file. The names of the icons that you remap: %1")
.arg("LockIcon, LogoutIcon, SuspendIcon, RebootIcon, ShutdownIcon.")
+ "\n";
qDebug().noquote() << QObject::tr("Other options that can be set in the exit-options.conf file: %1")
.arg("IconSize=, Margin=, Spacing=");
exit(EXIT_SUCCESS);
}
// Allow informational invocations like --help to bypass single-instance toggle behavior.
QProcess proc;
proc.start("pgrep", {"--count", "--exact", QApplication::applicationName()});
proc.waitForFinished();
if (proc.exitCode() == 0 && proc.readAllStandardOutput().trimmed().toInt() > 1) {
QProcess::startDetached("pkill", {"--oldest", QApplication::applicationName()});
return EXIT_SUCCESS;
}
MainWindow mainWindow(parser);
mainWindow.show();
// Load timeout settings
const QSettings userSettings(QSettings::UserScope, QApplication::organizationName(), QApplication::applicationName());
const QSettings systemSettings(SYSTEM_CONFIG_PATH, QSettings::IniFormat);
QString timeout;
if (parser.isSet("timeout")) {
timeout = parser.value("timeout");
} else if (userSettings.contains("timeout")) {
timeout = userSettings.value("timeout").toString();
} else if (systemSettings.contains("timeout")) {
timeout = systemSettings.value("timeout").toString();
} else {
timeout = QStringLiteral("off");
}
// Set up timeout for quitting the application
if (timeout != QStringLiteral("off")) {
bool ok {false};
const auto timeoutDuration = std::chrono::seconds(timeout.toUInt(&ok));
if (ok) {
QTimer::singleShot(timeoutDuration, &app, &QApplication::quit);
}
}
return app.exec();
}