-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·370 lines (312 loc) · 12.7 KB
/
main.cpp
File metadata and controls
executable file
·370 lines (312 loc) · 12.7 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
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
#include <QApplication>
#include <QCommandLineParser>
#include <QtDBus>
#include <QDebug>
#include <QMessageBox>
#include <QTimer>
#include "version.h"
#include "src/HeadsetManager.h"
#include "src/TrayIconController.h"
#include "src/NotificationManager.h"
#include "src/ConfigManager.h"
#include "src/SettingsDialog.h"
/**
* @class DBusListener
* @brief Listens for D-Bus property changes from UPower
*/
class DBusListener : public QObject {
Q_OBJECT
public:
explicit DBusListener(QObject *parent = nullptr) : QObject(parent) {}
signals:
void statusRelevantEvent();
public slots:
void propertiesChanged(const QString& interfaceName,
const QVariantMap& changedProperties,
const QStringList& invalidatedProperties) {
Q_UNUSED(invalidatedProperties)
if (interfaceName != "org.freedesktop.UPower.Device") {
return;
}
if (changedProperties.contains("Percentage") ||
changedProperties.contains("IsCharging") ||
changedProperties.contains("IsPresent")) {
emit statusRelevantEvent();
}
}
void deviceAdded(const QDBusObjectPath&) { emit statusRelevantEvent(); }
void deviceRemoved(const QDBusObjectPath&) { emit statusRelevantEvent(); }
};
/**
* @class HeadsetStatusApp
* @brief Main application class coordinating all components
*
* Supports both GUI mode (system tray) and headless mode (notifications only).
*/
class HeadsetStatusApp : public QObject {
Q_OBJECT
public:
explicit HeadsetStatusApp(bool headless = false, bool debug = false)
: m_headless(headless), m_debug(debug) {
// Initialize managers
configManager = new ConfigManager(this);
headsetManager = new HeadsetManager(this);
notificationManager = new NotificationManager(this);
listener = new DBusListener(this);
// Apply config to notification manager
notificationManager->setNotificationsEnabled(configManager->notificationsEnabled());
notificationManager->setLowBatteryThreshold(configManager->lowBatteryThreshold());
// Only create tray controller in GUI mode
if (!m_headless) {
trayController = new TrayIconController(this);
trayController->setLowBatteryThreshold(configManager->lowBatteryThreshold());
// Connect tray signals
connect(trayController, &TrayIconController::informationRequested, this, &HeadsetStatusApp::showInformation);
connect(trayController, &TrayIconController::settingsRequested, this, &HeadsetStatusApp::showSettings);
connect(trayController, &TrayIconController::aboutRequested, this, &HeadsetStatusApp::showAbout);
connect(trayController, &TrayIconController::deviceDetailsRequested, this, &HeadsetStatusApp::showDeviceDetails);
}
// Connect to D-Bus for property changes
bool connected = QDBusConnection::systemBus().connect(
"org.freedesktop.UPower", QString(),
"org.freedesktop.DBus.Properties",
"PropertiesChanged",
listener,
SLOT(propertiesChanged(QString,QVariantMap,QStringList))
);
if (!connected) {
qWarning() << "Failed to connect to D-Bus PropertiesChanged signal";
}
bool addedConnected = QDBusConnection::systemBus().connect(
"org.freedesktop.UPower", "/org/freedesktop/UPower",
"org.freedesktop.UPower", "DeviceAdded",
listener, SLOT(deviceAdded(QDBusObjectPath))
);
if (!addedConnected) {
qWarning() << "Failed to connect to UPower DeviceAdded signal";
}
bool removedConnected = QDBusConnection::systemBus().connect(
"org.freedesktop.UPower", "/org/freedesktop/UPower",
"org.freedesktop.UPower", "DeviceRemoved",
listener, SLOT(deviceRemoved(QDBusObjectPath))
);
if (!removedConnected) {
qWarning() << "Failed to connect to UPower DeviceRemoved signal";
}
m_updateDebounceTimer = new QTimer(this);
m_updateDebounceTimer->setSingleShot(true);
m_updateDebounceTimer->setInterval(120);
connect(m_updateDebounceTimer, &QTimer::timeout, this, &HeadsetStatusApp::updateStatus);
m_fallbackPollTimer = new QTimer(this);
m_fallbackPollTimer->setSingleShot(false);
connect(m_fallbackPollTimer, &QTimer::timeout, this, &HeadsetStatusApp::scheduleStatusUpdate);
applyPollingInterval(configManager->updateInterval());
// Connect signals
connect(listener, &DBusListener::statusRelevantEvent, this, &HeadsetStatusApp::scheduleStatusUpdate);
connect(configManager, &ConfigManager::configChanged, this, &HeadsetStatusApp::onConfigChanged);
if (m_debug) {
qDebug() << "HeadsetStatus started in" << (m_headless ? "headless" : "GUI") << "mode";
}
// Initial status update
updateStatus();
}
private slots:
void scheduleStatusUpdate() {
if (!m_updateDebounceTimer->isActive()) {
m_updateDebounceTimer->start();
}
}
void updateStatus() {
QList<HeadsetDevice> currentDevices = headsetManager->getDevices();
QSet<QString> currentPaths;
currentPaths.reserve(currentDevices.size());
for (const HeadsetDevice& device : currentDevices) {
currentPaths.insert(device.dbusPath);
}
if (m_debug) {
qDebug() << "Status update: found" << currentDevices.size() << "devices";
}
// Check for disconnected devices
if (configManager->notifyOnDisconnect()) {
for (auto it = m_knownDevices.constBegin(); it != m_knownDevices.constEnd(); ++it) {
if (!currentPaths.contains(it.key())) {
notificationManager->notifyDeviceDisconnected(it.value());
}
}
}
for (auto it = m_lowBatteryNotified.begin(); it != m_lowBatteryNotified.end();) {
if (!currentPaths.contains(*it)) {
it = m_lowBatteryNotified.erase(it);
} else {
++it;
}
}
for (auto it = m_previouslyCharging.begin(); it != m_previouslyCharging.end();) {
if (!currentPaths.contains(*it)) {
it = m_previouslyCharging.erase(it);
} else {
++it;
}
}
// Update device cache
m_knownDevices.clear();
m_knownDevices.reserve(currentDevices.size());
for (const HeadsetDevice& device : currentDevices) {
m_knownDevices[device.dbusPath] = device;
}
// Check for low battery and send notifications
if (configManager->notifyOnLowBattery()) {
for (const HeadsetDevice& device : currentDevices) {
if (device.isPresent && !device.isCharging &&
device.battery <= configManager->lowBatteryThreshold()) {
if (!m_lowBatteryNotified.contains(device.dbusPath)) {
notificationManager->notifyLowBattery(device);
m_lowBatteryNotified.insert(device.dbusPath);
}
} else {
m_lowBatteryNotified.remove(device.dbusPath);
}
}
}
// Check for charging complete
if (configManager->notifyOnChargingComplete()) {
for (const HeadsetDevice& device : currentDevices) {
if (device.battery >= 95 && !device.isCharging) {
if (m_previouslyCharging.contains(device.dbusPath)) {
notificationManager->notifyChargingComplete(device);
m_previouslyCharging.remove(device.dbusPath);
}
} else if (device.isCharging) {
m_previouslyCharging.insert(device.dbusPath);
}
}
}
// Update tray icon (GUI mode only)
if (trayController) {
trayController->updateIcon(currentDevices);
}
}
void showInformation() {
if (trayController) {
QMessageBox::information(nullptr, "Headset Information",
trayController->trayIcon()->toolTip());
}
}
void onConfigChanged() {
notificationManager->setNotificationsEnabled(configManager->notificationsEnabled());
notificationManager->setLowBatteryThreshold(configManager->lowBatteryThreshold());
applyPollingInterval(configManager->updateInterval());
if (trayController) {
trayController->setLowBatteryThreshold(configManager->lowBatteryThreshold());
}
}
void applyPollingInterval(int intervalMs) {
if (intervalMs <= 0) {
m_fallbackPollTimer->stop();
return;
}
if (m_fallbackPollTimer->interval() != intervalMs) {
m_fallbackPollTimer->setInterval(intervalMs);
}
if (!m_fallbackPollTimer->isActive()) {
m_fallbackPollTimer->start();
}
}
void showSettings() {
SettingsDialog dialog(configManager, nullptr);
dialog.exec();
}
void showDeviceDetails(const QString& dbusPath) {
QList<HeadsetDevice> devices = headsetManager->getDevices();
HeadsetDevice targetDevice;
bool found = false;
for (const HeadsetDevice& device : devices) {
if (device.dbusPath == dbusPath) {
targetDevice = device;
found = true;
break;
}
}
if (!found) {
QMessageBox::warning(nullptr, "Device Not Found",
"The selected device is no longer connected.");
return;
}
QString details = QString(
"<b>%1</b><br><br>"
"<b>Connection Type:</b> %2<br>"
"<b>Battery Level:</b> %3%<br>"
"<b>Charging:</b> %4<br>"
"<b>Present:</b> %5<br>"
"<br><b>Technical Details:</b><br>"
"<small>D-Bus Path: %6<br>"
"Native Path: %7</small>")
.arg(targetDevice.model)
.arg(targetDevice.connectionType)
.arg(int(targetDevice.battery))
.arg(targetDevice.isCharging ? "Yes" : "No")
.arg(targetDevice.isPresent ? "Yes" : "No")
.arg(targetDevice.dbusPath)
.arg(targetDevice.nativePath);
QMessageBox::information(nullptr,
QString("Device Details: %1").arg(targetDevice.model),
details);
}
void showAbout() {
QMessageBox aboutBox;
aboutBox.setWindowTitle("About HeadsetStatus");
aboutBox.setTextFormat(Qt::RichText);
aboutBox.setText(QString("<b>HeadsetStatus</b><br>"
"Version %1<br>"
"A fast Linux tray app for headset battery and connection status.<br>"
"<a href='https://github.com/mewset/headsetstatus'>GitHub</a><br>"
"License: MIT<br><br>"
"© 2025 mewset").arg(HEADSETSTATUS_VERSION));
aboutBox.setStandardButtons(QMessageBox::Ok);
if (trayController) {
aboutBox.installEventFilter(trayController);
}
aboutBox.exec();
}
private:
bool m_headless;
bool m_debug;
HeadsetManager *headsetManager;
TrayIconController *trayController = nullptr;
NotificationManager *notificationManager;
ConfigManager *configManager;
DBusListener *listener;
QTimer *m_updateDebounceTimer = nullptr;
QTimer *m_fallbackPollTimer = nullptr;
// Track device and notification states
QHash<QString, HeadsetDevice> m_knownDevices;
QSet<QString> m_lowBatteryNotified;
QSet<QString> m_previouslyCharging;
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
app.setApplicationName("HeadsetStatus");
app.setApplicationVersion(HEADSETSTATUS_VERSION);
app.setOrganizationName("mewset");
QCommandLineParser parser;
parser.setApplicationDescription("Headset battery status monitor for Linux");
parser.addHelpOption();
parser.addVersionOption();
QCommandLineOption noTrayOption(
QStringList() << "n" << "no-tray",
"Run in headless mode without system tray icon");
parser.addOption(noTrayOption);
QCommandLineOption debugOption(
QStringList() << "d" << "debug",
"Enable debug output");
parser.addOption(debugOption);
parser.process(app);
bool headless = parser.isSet(noTrayOption);
bool debug = parser.isSet(debugOption);
if (debug) {
qDebug() << "HeadsetStatus" << HEADSETSTATUS_VERSION;
}
HeadsetStatusApp headsetStatus(headless, debug);
return app.exec();
}
#include "main.moc"