forked from dmikushin/tray
-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathQtTrayMenu.cpp
More file actions
286 lines (249 loc) · 8.34 KB
/
Copy pathQtTrayMenu.cpp
File metadata and controls
286 lines (249 loc) · 8.34 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
#include "QtTrayMenu.h"
#include <QApplication>
#include <QCursor>
#include <QDebug>
#include <QMouseEvent>
namespace {
int defaultArgc = 1;
char defaultArgv0[] = "TrayMenuApp";
char *defaultArgv[] = {defaultArgv0, nullptr};
} // namespace
QtTrayMenu::QtTrayMenu(QObject *parent):
QtTrayMenu(-1, nullptr, false, parent) {
};
QtTrayMenu::QtTrayMenu(int argc, char **argv, const bool debug, QObject *parent):
QObject(parent) {
if (QApplication::instance()) {
app = dynamic_cast<QApplication *>(QApplication::instance());
if (!app) {
qDebug() << "QCoreApplication is not a QApplication, please contact support.";
}
} else {
// Note: The following is ugly but QApplication requires an argv containing the application name.
// We might not have access to the real argc/argv here due to being called/pulled as a dependency.
if (argc < 0 && argv == nullptr) {
app = new QApplication(defaultArgc, defaultArgv); // NOSONAR(cpp:S5025) - Qt has its own integrated memory management
} else {
app = new QApplication(argc, argv); // NOSONAR(cpp:S5025) - Qt has its own integrated memory management
}
}
if (debug) {
app->installEventFilter(this);
}
}
QtTrayMenu::~QtTrayMenu() {
// Quit QApplication
QApplication::quit();
// Cleanup app only if it was created within this class
if (app && app != QApplication::instance()) {
// Delete app and clear references
delete app; // NOSONAR(cpp:S5025) - Qt has its own integrated memory management
app = nullptr; // Set to nullptr after deletion
}
}
int QtTrayMenu::init(struct tray *tray) {
if (trayIcon) {
// Running tray is initialized again. Fail with error.
return -1;
}
this->trayStruct = tray;
this->running = true;
if (QApplication::applicationName().isEmpty() || QApplication::applicationName() == "TrayMenuApp") {
QApplication::setApplicationName(tray->tooltip);
}
trayIcon = new QSystemTrayIcon(QIcon(tray->icon), this);
trayIcon->setToolTip(QString::fromUtf8(tray->tooltip));
connect(trayIcon, &QSystemTrayIcon::activated, this, &QtTrayMenu::onTrayActivated);
connect(trayIcon, &QSystemTrayIcon::messageClicked, this, &QtTrayMenu::onMessageClicked);
trayTopMenu = new QMenu(); // NOSONAR(cpp:S5025) - Qt has its own integrated memory management
createMenu(tray->menu, trayTopMenu);
trayIcon->setContextMenu(trayTopMenu);
trayIcon->show();
createNotification();
return 0;
}
void QtTrayMenu::update(struct tray *tray) {
if (!trayIcon) {
return;
}
this->trayStruct = tray;
if (const auto newIcon = QIcon(tray->icon); !newIcon.isNull()) {
trayIcon->setIcon(newIcon);
}
trayIcon->setToolTip(QString::fromUtf8(tray->tooltip));
if (auto *existingMenu = trayIcon->contextMenu()) {
existingMenu->clear(); // Remove all actions
createMenu(tray->menu, existingMenu);
}
createNotification();
}
int QtTrayMenu::loop(int blocking) const {
if (!running) {
return -1;
}
if (!app || QApplication::closingDown()) {
qDebug() << "Application is not in a valid state or is closing down.";
return -1;
}
if (blocking) {
QApplication::exec();
return -1;
} else {
QApplication::processEvents();
return 0;
}
}
void QtTrayMenu::exit() {
running = false;
// Remove tray menu references
if (trayTopMenu) {
trayTopMenu->hide();
if (trayIcon) {
trayIcon->setContextMenu(nullptr);
}
delete trayTopMenu; // NOSONAR(cpp:S5025) - Qt has its own integrated memory management
trayTopMenu = nullptr; // Set to nullptr after deletion
}
// Remove tray icon references;
if (trayIcon) {
trayIcon->hide();
delete trayIcon; // NOSONAR(cpp:S5025) - Qt has its own integrated memory management
trayIcon = nullptr; // Set to nullptr after deletion
}
// Unset tray structure
trayStruct = nullptr;
}
void QtTrayMenu::createMenu(struct tray_menu *items, QMenu *menu) {
while (items && items->text) {
if (strcmp(items->text, "-") == 0) {
menu->addSeparator();
} else {
auto *action = new QAction(QString::fromUtf8(items->text), menu);
action->setDisabled(items->disabled == 1);
action->setCheckable(items->checkbox == 1);
action->setChecked(items->checked == 1);
action->setProperty("tray_menu_item", QVariant::fromValue((void *) items));
connect(action, &QAction::triggered, this, &QtTrayMenu::onMenuItemTriggered);
if (items->submenu) {
const auto submenu = new QMenu(menu);
createMenu(items->submenu, submenu);
action->setMenu(submenu);
}
menu->addAction(action);
}
items++;
}
}
void QtTrayMenu::createNotification() const {
if (trayStruct && trayStruct->notification_title && trayStruct->notification_text) {
const auto title = QString::fromUtf8(trayStruct->notification_title);
const auto text = QString::fromUtf8(trayStruct->notification_text);
if (trayStruct->notification_icon) {
showMessage(title, text, QIcon(trayStruct->notification_icon));
} else {
showMessage(title, text);
}
}
}
bool QtTrayMenu::eventFilter(QObject *watched, QEvent *event) {
qDebug() << "Event Type:" << event->type();
return QObject::eventFilter(watched, event);
}
void QtTrayMenu::onTrayActivated(QSystemTrayIcon::ActivationReason reason) {
if (reason != QSystemTrayIcon::Trigger) {
return;
}
if (trayStruct && trayStruct->cb) {
trayStruct->cb(trayStruct);
} else {
showMenu();
}
}
void QtTrayMenu::onMenuItemTriggered() {
auto *action = qobject_cast<QAction *>(sender());
struct tray_menu *menuItem = getTrayMenuItem(action);
if (menuItem && menuItem->cb) {
menuItem->cb(menuItem);
}
}
struct tray_menu *QtTrayMenu::getTrayMenuItem(QAction *action) { // NOSONAR(cpp:S995) - Use as defined in function interface
return static_cast<struct tray_menu *>(action->property("tray_menu_item").value<void *>());
}
void QtTrayMenu::onMessageClicked() const {
if (trayStruct && trayStruct->notification_cb) {
trayStruct->notification_cb();
}
}
void QtTrayMenu::configureAppMetadata(const QString &appName, const QString &appDisplayName, const QString &desktopName) const {
const QString effective_name = !appName.isEmpty() ? appName : QStringLiteral("tray");
if (QApplication::applicationName().isEmpty()) {
QApplication::setApplicationName(effective_name);
}
if (QApplication::applicationDisplayName().isEmpty()) {
if (!appDisplayName.isEmpty()) {
QApplication::setApplicationDisplayName(appDisplayName);
} else {
const QString display_name =
(trayStruct && trayStruct->tooltip) ? QString::fromUtf8(trayStruct->tooltip) : effective_name;
QApplication::setApplicationDisplayName(display_name);
}
}
if (!QApplication::desktopFileName().isEmpty()) {
return;
}
if (!desktopName.isEmpty()) {
QApplication::setDesktopFileName(desktopName);
return;
}
QString desktop_name = QApplication::applicationName();
if (!desktop_name.endsWith(QStringLiteral(".desktop"))) {
desktop_name += QStringLiteral(".desktop");
}
QApplication::setDesktopFileName(desktop_name);
}
void QtTrayMenu::showMenu() const {
if (!trayIcon) {
return;
}
if (QMenu *menu = trayIcon->contextMenu(); menu != nullptr) {
// Due to QTBUG-139921 this is currently not working on Linux/Wayland
// with Qt-6.9+ unless menu has a transient parent (which we do not have here).
menu->popup(QCursor::pos());
}
}
void QtTrayMenu::showMessage(const QString &title, const QString &msg, const QSystemTrayIcon::MessageIcon icon, const int msecs) const {
if (!trayIcon) {
return;
}
trayIcon->showMessage(title, msg, icon, msecs);
}
void QtTrayMenu::showMessage(const QString &title, const QString &msg, const QIcon &icon, const int msecs) const {
if (!trayIcon) {
return;
}
trayIcon->showMessage(title, msg, icon, msecs);
}
void QtTrayMenu::clickMenuItem(int index) const {
if (!trayIcon) {
return;
}
const QMenu *menu = trayIcon->contextMenu();
if (!menu) {
return;
}
const QList<QAction *> actions = menu->actions();
if (index < 0 || index >= actions.size()) {
return;
}
QAction *action = actions.at(index);
if (!action || action->isSeparator() || action->menu() != nullptr || !action->isEnabled()) {
return;
}
action->trigger();
}
void QtTrayMenu::clickMessage() const {
if (!trayIcon) {
return;
}
emit trayIcon->messageClicked();
}