-
Notifications
You must be signed in to change notification settings - Fork 6
/
systemtray.cpp
221 lines (180 loc) · 6.22 KB
/
systemtray.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "systemtray.h"
#include <QtWidgets/QApplication>
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QMenu>
#include <QtCore/QProcess>
#include <QtCore/QDebug>
#include <QtCore/QTimer>
#include <QtGui/QIcon>
#include <signal.h>
SystemTray::SystemTray()
{
_suspendTimer = new QTimer(this);
connect(_suspendTimer, &QTimer::timeout, this, &SystemTray::onTimeout);
}
void SystemTray::onClick(QSystemTrayIcon::ActivationReason reason)
{
switch (reason)
{
case QSystemTrayIcon::ActivationReason::Trigger:
case QSystemTrayIcon::ActivationReason::DoubleClick:
ToggleRedshift(!_enabled);
break;
case QSystemTrayIcon::ActivationReason::MiddleClick:
case QSystemTrayIcon::ActivationReason::Context:
case QSystemTrayIcon::ActivationReason::Unknown:
break;
}
}
void SystemTray::onQuit()
{
setIcon(_iconDisabled);
QApplication::quit();
}
void SystemTray::onRedshiftQuit(int exitCode, QProcess::ExitStatus exitStatus)
{
if (!_warnOnRedshiftQuit)
return;
if (exitStatus == QProcess::ExitStatus::CrashExit) {
_errText = "Redshift has crashed with exit code " + QString::number(exitCode) + ":\n";
} else {
_errText = "Redshift has terminated unexpectedly with exit code " + QString::number(exitCode) + ":\n";
}
_redshiftProcess->setReadChannel(QProcess::StandardError);
QTextStream stream(_redshiftProcess);
while (!stream.atEnd()) {
auto line = stream.readLine();
qInfo() << line;
_errText += line + "\n";
}
QMessageBox::critical(nullptr, QObject::tr("Fatal error"), _errText);
onQuit();
}
void SystemTray::onRedshiftOutput()
{
if (!_redshiftProcess)
return;
QTextStream stream(_redshiftProcess);
while (!stream.atEnd()) {
auto line = stream.readLine();
qInfo() << line;
if (line.startsWith("Color temperature"))
_colorTemp = line;
else if (line.startsWith("Period:"))
_period = line;
else if (!line.startsWith("Status:"))
_info += "\n" + line;
}
}
void SystemTray::onSuspend()
{
ToggleRedshift(!_enabled);
}
void SystemTray::onSuspend10minutes()
{
ToggleRedshift(false);
_suspendTimer->start(10*60*1000);
}
void SystemTray::onSuspend1hour()
{
ToggleRedshift(false);
_suspendTimer->start(60*60*1000);
}
void SystemTray::onSuspend2hours()
{
ToggleRedshift(false);
_suspendTimer->start(120*60*1000);
}
void SystemTray::onTimeout()
{
ToggleRedshift(true);
}
void SystemTray::onGetInfo()
{
QMessageBox::information(nullptr, "Redshift Information", _info
+ "\n" + _period
+ "\n" + _colorTemp, QMessageBox::Ok);
}
bool SystemTray::CreateIcon()
{
if (!QSystemTrayIcon::isSystemTrayAvailable()) {
qWarning() << "No system tray available";
}
CreateMenu();
_iconEnabled = QIcon::fromTheme("redshift-status-on", QIcon(":/icons/redshift-status-on.png"));
_iconDisabled = QIcon::fromTheme("redshift-status-off", QIcon(":/icons/redshift-status-off.png"));
setIcon(_iconEnabled);
connect(this, &QSystemTrayIcon::activated, this, &SystemTray::onClick);
show();
return true;
}
bool SystemTray::StartRedshift(QStringList argsl)
{
_redshiftProcess = new QProcess(this);
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("LC_ALL", "C");
_redshiftProcess->setProcessEnvironment(env);
_redshiftProcess->start("redshift", argsl);
connect(_redshiftProcess, static_cast<void (QProcess::*)(int,QProcess::ExitStatus)>(&QProcess::finished), this, &SystemTray::onRedshiftQuit);
connect(_redshiftProcess, &QProcess::readyRead, this, &SystemTray::onRedshiftOutput);
if (!_redshiftProcess->waitForStarted(5000))
{
QMessageBox::critical(nullptr, QObject::tr("Fatal error"), QObject::tr("Failed to start redshift"));
qFatal("Failed to start redshift");
}
_enabled = true;
return true;
}
void SystemTray::ToggleRedshift(bool enable)
{
if (!_redshiftProcess)
{
qFatal("QProcess pointer is null");
}
if (enable == _enabled)
return;
_enabled = enable;
_suspendMenu->setChecked(!enable);
setIcon(enable ? _iconEnabled : _iconDisabled);
qInfo() << "Redshift status change: " << (enable ? "enabled" : "disabled");
kill(static_cast<pid_t>(_redshiftProcess->pid()), SIGUSR1);
}
void SystemTray::StopRedshift()
{
if (_redshiftProcess && _redshiftProcess->pid())
{
_warnOnRedshiftQuit = false;
_redshiftProcess->terminate();
if (!_redshiftProcess->waitForFinished())
qCritical() << "Redshift process failed to terminate";
}
}
void SystemTray::CreateMenu()
{
// Lifetime of this menu is the same as the application's
auto trayIconMenu = new QMenu();
_suspendMenu = new QAction(QObject::tr("Suspended"), this);
_suspendMenu->setCheckable(true);
_suspendMenu->setChecked(false);
connect(_suspendMenu, &QAction::triggered, this, &SystemTray::onSuspend);
auto suspendAction_10m = new QAction(QObject::tr("Suspend for 10 minutes"), this);
auto suspendAction_1h = new QAction(QObject::tr("Suspend for 1 hour"), this);
auto suspendAction_2h = new QAction(QObject::tr("Suspend for 2 hours"), this);
connect(suspendAction_10m, &QAction::triggered, this, &SystemTray::onSuspend10minutes);
connect(suspendAction_1h, &QAction::triggered, this, &SystemTray::onSuspend1hour);
connect(suspendAction_2h, &QAction::triggered, this, &SystemTray::onSuspend2hours);
auto showInfoAction = new QAction(QObject::tr("Show Info"), this);
connect(showInfoAction, &QAction::triggered, this, &SystemTray::onGetInfo);
auto quitAction = new QAction(QObject::tr("&Quit"), nullptr);
connect(quitAction, &QAction::triggered, this, &SystemTray::onQuit);
trayIconMenu->addAction(_suspendMenu);
trayIconMenu->addSeparator();
trayIconMenu->addAction(suspendAction_10m);
trayIconMenu->addAction(suspendAction_1h);
trayIconMenu->addAction(suspendAction_2h);
trayIconMenu->addSeparator();
trayIconMenu->addAction(showInfoAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
setContextMenu(trayIconMenu);
}