|
4 | 4 | */ |
5 | 5 | // standard includes |
6 | 6 | #include <cstring> |
| 7 | +#include <memory> |
7 | 8 |
|
8 | 9 | // local includes |
9 | 10 | #include "tray.h" |
10 | 11 |
|
11 | 12 | // Qt includes |
12 | 13 | #include <QApplication> |
| 14 | +#include <QCursor> |
13 | 15 | #include <QIcon> |
14 | 16 | #include <QMenu> |
15 | 17 | #include <QSystemTrayIcon> |
16 | 18 |
|
17 | | -static QApplication *app = nullptr; |
18 | | -static QSystemTrayIcon *tray_icon = nullptr; |
19 | | -static QMenu *tray_menu = nullptr; |
20 | | -static int loop_result = 0; |
21 | | -static bool app_owned = false; |
22 | | - |
23 | | -static QMenu *_tray_menu(struct tray_menu *m) { |
24 | | - auto *menu = new QMenu(); |
25 | | - for (; m != nullptr && m->text != nullptr; m++) { |
26 | | - if (std::strcmp(m->text, "-") == 0) { |
27 | | - menu->addSeparator(); |
28 | | - } else if (m->submenu != nullptr) { |
29 | | - QMenu *sub = _tray_menu(m->submenu); |
30 | | - sub->setTitle(QString::fromUtf8(m->text)); |
31 | | - menu->addMenu(sub); |
32 | | - } else if (m->checkbox) { |
33 | | - auto *action = menu->addAction(QString::fromUtf8(m->text)); |
34 | | - action->setCheckable(true); |
35 | | - action->setChecked(m->checked != 0); |
36 | | - action->setEnabled(m->disabled == 0); |
37 | | - if (m->cb != nullptr) { |
38 | | - struct tray_menu *item = m; |
39 | | - QObject::connect(action, &QAction::triggered, [item]() { |
40 | | - item->cb(item); |
41 | | - }); |
42 | | - } |
43 | | - } else { |
44 | | - auto *action = menu->addAction(QString::fromUtf8(m->text)); |
45 | | - action->setEnabled(m->disabled == 0); |
46 | | - if (m->cb != nullptr) { |
47 | | - struct tray_menu *item = m; |
48 | | - QObject::connect(action, &QAction::triggered, [item]() { |
49 | | - item->cb(item); |
50 | | - }); |
| 19 | +namespace { |
| 20 | + std::unique_ptr<QApplication> g_app; |
| 21 | + std::unique_ptr<QSystemTrayIcon> g_tray_icon; |
| 22 | + std::unique_ptr<QMenu> g_tray_menu; |
| 23 | + int g_loop_result = 0; |
| 24 | + bool g_app_owned = false; |
| 25 | + |
| 26 | + QMenu *build_menu(struct tray_menu *m) { |
| 27 | + auto *menu = new QMenu(); // NOSONAR(cpp:S5025) - Qt parent-owned; transferred to unique_ptr or addMenu immediately |
| 28 | + for (; m != nullptr && m->text != nullptr; m++) { |
| 29 | + if (std::strcmp(m->text, "-") == 0) { |
| 30 | + menu->addSeparator(); |
| 31 | + } else if (m->submenu != nullptr) { |
| 32 | + QMenu *sub = build_menu(m->submenu); |
| 33 | + sub->setTitle(QString::fromUtf8(m->text)); |
| 34 | + menu->addMenu(sub); |
| 35 | + } else { |
| 36 | + auto *action = menu->addAction(QString::fromUtf8(m->text)); |
| 37 | + action->setEnabled(m->disabled == 0); |
| 38 | + if (m->checkbox) { |
| 39 | + action->setCheckable(true); |
| 40 | + action->setChecked(m->checked != 0); |
| 41 | + } |
| 42 | + if (m->cb != nullptr) { |
| 43 | + struct tray_menu *item = m; |
| 44 | + QObject::connect(action, &QAction::triggered, [item]() { |
| 45 | + item->cb(item); |
| 46 | + }); |
| 47 | + } |
51 | 48 | } |
52 | 49 | } |
| 50 | + return menu; |
53 | 51 | } |
54 | | - return menu; |
55 | | -} |
| 52 | +} // namespace |
56 | 53 |
|
57 | 54 | extern "C" { |
58 | 55 |
|
59 | | -int tray_init(struct tray *tray) { |
60 | | - if (QApplication::instance() == nullptr) { |
61 | | - static int argc = 0; |
62 | | - app = new QApplication(argc, nullptr); |
63 | | - app_owned = true; |
64 | | - } |
| 56 | + int tray_init(struct tray *tray) { |
| 57 | + if (QApplication::instance() == nullptr) { |
| 58 | + static int argc = 0; |
| 59 | + g_app = std::make_unique<QApplication>(argc, nullptr); |
| 60 | + g_app_owned = true; |
| 61 | + } |
65 | 62 |
|
66 | | - if (tray_icon != nullptr) { |
67 | | - delete tray_icon; |
68 | | - tray_icon = nullptr; |
69 | | - } |
70 | | - if (tray_menu != nullptr) { |
71 | | - delete tray_menu; |
72 | | - tray_menu = nullptr; |
73 | | - } |
| 63 | + g_tray_icon.reset(); |
| 64 | + g_tray_menu.reset(); |
| 65 | + g_loop_result = 0; |
74 | 66 |
|
75 | | - loop_result = 0; |
| 67 | + g_tray_icon = std::make_unique<QSystemTrayIcon>(); |
76 | 68 |
|
77 | | - tray_icon = new QSystemTrayIcon(); |
| 69 | + if (!QSystemTrayIcon::isSystemTrayAvailable()) { |
| 70 | + g_tray_icon.reset(); |
| 71 | + return -1; |
| 72 | + } |
78 | 73 |
|
79 | | - if (!QSystemTrayIcon::isSystemTrayAvailable()) { |
80 | | - delete tray_icon; |
81 | | - tray_icon = nullptr; |
82 | | - return -1; |
| 74 | + tray_update(tray); |
| 75 | + g_tray_icon->show(); |
| 76 | + return 0; |
83 | 77 | } |
84 | 78 |
|
85 | | - tray_update(tray); |
86 | | - tray_icon->show(); |
87 | | - return 0; |
88 | | -} |
89 | | - |
90 | | -int tray_loop(int blocking) { |
91 | | - if (blocking) { |
92 | | - QApplication::exec(); |
93 | | - } else { |
94 | | - QApplication::processEvents(); |
| 79 | + int tray_loop(int blocking) { |
| 80 | + if (blocking) { |
| 81 | + QApplication::exec(); |
| 82 | + } else { |
| 83 | + QApplication::processEvents(); |
| 84 | + } |
| 85 | + return g_loop_result; |
95 | 86 | } |
96 | | - return loop_result; |
97 | | -} |
98 | 87 |
|
99 | | -void tray_update(struct tray *tray) { |
100 | | - if (tray_icon == nullptr) { |
101 | | - return; |
102 | | - } |
| 88 | + void tray_update(struct tray *tray) { |
| 89 | + if (g_tray_icon == nullptr) { |
| 90 | + return; |
| 91 | + } |
103 | 92 |
|
104 | | - tray_icon->setIcon(QIcon(QString::fromUtf8(tray->icon))); |
| 93 | + g_tray_icon->setIcon(QIcon(QString::fromUtf8(tray->icon))); |
105 | 94 |
|
106 | | - if (tray->tooltip != nullptr) { |
107 | | - tray_icon->setToolTip(QString::fromUtf8(tray->tooltip)); |
108 | | - } |
| 95 | + if (tray->tooltip != nullptr) { |
| 96 | + g_tray_icon->setToolTip(QString::fromUtf8(tray->tooltip)); |
| 97 | + } |
109 | 98 |
|
110 | | - if (tray->menu != nullptr) { |
111 | | - QMenu *new_menu = _tray_menu(tray->menu); |
112 | | - tray_icon->setContextMenu(new_menu); |
113 | | - if (tray_menu != nullptr) { |
114 | | - delete tray_menu; |
| 99 | + if (tray->menu != nullptr) { |
| 100 | + auto *new_menu = build_menu(tray->menu); |
| 101 | + g_tray_icon->setContextMenu(new_menu); |
| 102 | + g_tray_menu.reset(new_menu); |
115 | 103 | } |
116 | | - tray_menu = new_menu; |
117 | | - } |
118 | 104 |
|
119 | | - if (tray->notification_text != nullptr && std::strlen(tray->notification_text) > 0) { |
120 | | - QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information; |
121 | | - const QString title = tray->notification_title != nullptr ? QString::fromUtf8(tray->notification_title) : QString(); |
122 | | - const QString text = QString::fromUtf8(tray->notification_text); |
123 | | - tray_icon->showMessage(title, text, icon, 5000); |
124 | | - |
125 | | - if (tray->notification_cb != nullptr) { |
126 | | - void (*cb)() = tray->notification_cb; |
127 | | - QObject::connect(tray_icon, &QSystemTrayIcon::messageClicked, [cb]() { |
128 | | - cb(); |
129 | | - }); |
| 105 | + const QString text = tray->notification_text != nullptr ? QString::fromUtf8(tray->notification_text) : QString(); |
| 106 | + if (!text.isEmpty()) { |
| 107 | + const QString title = tray->notification_title != nullptr ? QString::fromUtf8(tray->notification_title) : QString(); |
| 108 | + QObject::disconnect(g_tray_icon.get(), &QSystemTrayIcon::messageClicked, nullptr, nullptr); |
| 109 | + if (tray->notification_cb != nullptr) { |
| 110 | + void (*cb)() = tray->notification_cb; |
| 111 | + QObject::connect(g_tray_icon.get(), &QSystemTrayIcon::messageClicked, [cb]() { |
| 112 | + cb(); |
| 113 | + }); |
| 114 | + } |
| 115 | + g_tray_icon->showMessage(title, text, QSystemTrayIcon::Information, 5000); |
| 116 | + } else { |
| 117 | + QObject::disconnect(g_tray_icon.get(), &QSystemTrayIcon::messageClicked, nullptr, nullptr); |
| 118 | + g_tray_icon->showMessage(QString(), QString(), QSystemTrayIcon::NoIcon, 0); |
130 | 119 | } |
131 | 120 | } |
132 | | -} |
133 | 121 |
|
134 | | -void tray_show_menu(void) { |
135 | | - if (tray_icon != nullptr && tray_menu != nullptr) { |
136 | | - tray_menu->popup(QCursor::pos()); |
137 | | - QApplication::processEvents(); |
| 122 | + void tray_show_menu(void) { |
| 123 | + if (g_tray_icon != nullptr && g_tray_menu != nullptr) { |
| 124 | + g_tray_menu->popup(QCursor::pos()); |
| 125 | + QApplication::processEvents(); |
| 126 | + } |
138 | 127 | } |
139 | | -} |
140 | | - |
141 | | -void tray_exit(void) { |
142 | | - loop_result = -1; |
143 | 128 |
|
144 | | - if (tray_icon != nullptr) { |
145 | | - tray_icon->hide(); |
146 | | - delete tray_icon; |
147 | | - tray_icon = nullptr; |
148 | | - } |
| 129 | + void tray_exit(void) { |
| 130 | + g_loop_result = -1; |
149 | 131 |
|
150 | | - if (tray_menu != nullptr) { |
151 | | - delete tray_menu; |
152 | | - tray_menu = nullptr; |
153 | | - } |
| 132 | + if (g_tray_icon != nullptr) { |
| 133 | + g_tray_icon->hide(); |
| 134 | + } |
| 135 | + g_tray_icon.reset(); |
| 136 | + g_tray_menu.reset(); |
154 | 137 |
|
155 | | - if (app_owned && app != nullptr) { |
156 | | - app->quit(); |
| 138 | + if (g_app_owned) { |
| 139 | + QApplication::quit(); |
| 140 | + g_app_owned = false; |
| 141 | + } |
157 | 142 | } |
158 | | -} |
159 | 143 |
|
160 | 144 | } // extern "C" |
0 commit comments