1919
2020namespace tray_qt {
2121 /* *
22- * QtTrayMenu instance
22+ * @brief Process-wide state backing the C tray API.
2323 */
24- std::unique_ptr<QtTrayMenu> qt_tray_menu = nullptr ; // NOSONAR(cpp:S5421): mutable state, not const
25- /* *
26- * Logging callback for qt_message_handler
27- */
28- void (*log_callback)(int , const char *) = nullptr ; // NOSONAR(cpp:S5421): mutable state, not const
29- /* *
30- * Explicit Qt application metadata configured through the C API.
31- */
32- bool app_info_configured = false ; // NOSONAR(cpp:S5421): mutable state, not const
33- /* *
34- * Qt application name configured through the C API.
35- */
36- QString app_name; // NOSONAR(cpp:S5421): mutable state, not const
37- /* *
38- * Qt application display name configured through the C API.
39- */
40- QString app_display_name; // NOSONAR(cpp:S5421): mutable state, not const
24+ struct State {
25+ std::unique_ptr<QtTrayMenu> trayMenu; // /< Active tray menu instance.
26+ void (*logCallback)(int , const char *) = nullptr ; // /< Registered C logging callback.
27+ bool appInfoConfigured = false ; // /< Whether application metadata was explicitly configured.
28+ QString appName; // /< Configured application name.
29+ QString appDisplayName; // /< Configured application display name.
30+ QString desktopName; // /< Configured desktop file name.
31+ };
32+
4133 /* *
42- * Qt desktop file name configured through the C API.
34+ * @brief Access the process-wide tray API state.
35+ * @return Mutable tray API state.
4336 */
44- QString desktop_name; // NOSONAR(cpp:S5421): mutable state, not const
37+ State &state () {
38+ static State instance;
39+ return instance;
40+ }
4541
4642 /* *
4743 * @brief Acknowledge/click current notification.
4844 */
4945 void acknowledge_notification () {
50- if (qt_tray_menu != nullptr && QtTrayMenu::supportsMessages ()) {
51- qt_tray_menu ->clickMessage ();
46+ if (state (). trayMenu != nullptr && QtTrayMenu::supportsMessages ()) {
47+ state (). trayMenu ->clickMessage ();
5248 }
5349 }
5450
5551 /* *
5652 * @brief Clear current notification state without invoking callbacks.
5753 */
5854 void clear_notification () {
59- if (qt_tray_menu != nullptr ) {
60- qt_tray_menu ->clearMessageCallback ();
55+ if (state (). trayMenu != nullptr ) {
56+ state (). trayMenu ->clearMessageCallback ();
6157 }
6258 }
6359
@@ -70,11 +66,11 @@ namespace tray_qt {
7066 clear_notification ();
7167 return ;
7268 }
73- if (qt_tray_menu != nullptr && QtTrayMenu::supportsMessages ()) {
69+ if (state (). trayMenu != nullptr && QtTrayMenu::supportsMessages ()) {
7470 if (tray->notification_icon != nullptr ) {
75- qt_tray_menu ->showMessage (tray->notification_title , tray->notification_text , tray->notification_icon , tray->notification_cb );
71+ state (). trayMenu ->showMessage (tray->notification_title , tray->notification_text , tray->notification_icon , tray->notification_cb );
7672 } else {
77- qt_tray_menu ->showMessage (tray->notification_title , tray->notification_text , tray->notification_cb );
73+ state (). trayMenu ->showMessage (tray->notification_title , tray->notification_text , tray->notification_cb );
7874 }
7975 }
8076 }
@@ -84,14 +80,15 @@ namespace tray_qt {
8480 * @param allow_defaults Whether empty app info values should apply fallback defaults.
8581 */
8682 void apply_app_info (const bool allow_defaults = true ) {
87- if (!app_info_configured || qt_tray_menu == nullptr ) {
83+ const auto ¤t_state = state ();
84+ if (!current_state.appInfoConfigured || current_state.trayMenu == nullptr ) {
8885 return ;
8986 }
90- if (!allow_defaults && app_name. isEmpty () && app_display_name .isEmpty ()) {
87+ if (!allow_defaults && current_state. appName . isEmpty () && current_state. appDisplayName .isEmpty ()) {
9188 return ;
9289 }
9390
94- qt_tray_menu ->configureAppMetadata (app_name, app_display_name, desktop_name );
91+ current_state. trayMenu ->configureAppMetadata (current_state. appName , current_state. appDisplayName , current_state. desktopName );
9592 }
9693
9794 /* *
@@ -114,7 +111,7 @@ namespace tray_qt {
114111 * @param msg The message string.
115112 */
116113 void qt_message_handler (QtMsgType type, const QMessageLogContext &, const QString &msg) {
117- if (log_callback == nullptr ) {
114+ if (state (). logCallback == nullptr ) {
118115 return ;
119116 }
120117 int level;
@@ -132,29 +129,31 @@ namespace tray_qt {
132129 level = 3 ;
133130 break ;
134131 }
135- log_callback (level, msg.toUtf8 ().constData ());
132+ state (). logCallback (level, msg.toUtf8 ().constData ());
136133 }
137134} // namespace tray_qt
138135
139136extern " C" {
140137 void tray_set_app_info (const char *app_name, const char *app_display_name, const char *desktop_name) {
141- tray_qt::app_info_configured = true ;
142- tray_qt::app_name = app_name != nullptr ? QString::fromUtf8 (app_name) : QString ();
143- tray_qt::app_display_name = app_display_name != nullptr ? QString::fromUtf8 (app_display_name) : QString ();
144- tray_qt::desktop_name = desktop_name != nullptr ? QString::fromUtf8 (desktop_name) : QString ();
138+ auto &state = tray_qt::state ();
139+ state.appInfoConfigured = true ;
140+ state.appName = app_name != nullptr ? QString::fromUtf8 (app_name) : QString ();
141+ state.appDisplayName = app_display_name != nullptr ? QString::fromUtf8 (app_display_name) : QString ();
142+ state.desktopName = desktop_name != nullptr ? QString::fromUtf8 (desktop_name) : QString ();
145143
146144 tray_qt::apply_app_info ();
147145 }
148146
149147 int tray_init (struct tray *tray) {
150- if (tray_qt::qt_tray_menu == nullptr ) {
148+ auto &state = tray_qt::state ();
149+ if (state.trayMenu == nullptr ) {
151150 tray_qt::configure_platform ();
152151 // Create a new unique pointer to QtTrayMenu instance
153- tray_qt::qt_tray_menu = std::make_unique<QtTrayMenu>();
152+ state. trayMenu = std::make_unique<QtTrayMenu>();
154153 tray_qt::apply_app_info (false );
155154 }
156155
157- if (const auto result = tray_qt::qt_tray_menu ->init (tray, false ); result < 0 ) {
156+ if (const auto result = state. trayMenu ->init (tray, false ); result < 0 ) {
158157 // Tray init failed. Clean up and return error.
159158 tray_exit ();
160159 return result;
@@ -173,18 +172,18 @@ extern "C" {
173172 }
174173
175174 int tray_loop (int blocking) {
176- if (tray_qt::qt_tray_menu == nullptr ) {
175+ if (tray_qt::state (). trayMenu == nullptr ) {
177176 return -1 ;
178177 }
179- return tray_qt::qt_tray_menu ->loop (blocking);
178+ return tray_qt::state (). trayMenu ->loop (blocking);
180179 }
181180
182181 void tray_update (struct tray *tray) { // NOSONAR(cpp:S995): C API requires this exact mutable-pointer signature
183- if (tray_qt::qt_tray_menu == nullptr ) {
182+ if (tray_qt::state (). trayMenu == nullptr ) {
184183 return ;
185184 }
186185
187- auto *const tray_menu = tray_qt::qt_tray_menu .get ();
186+ auto *const tray_menu = tray_qt::state (). trayMenu .get ();
188187 const auto apply_update = [tray_menu, tray]() {
189188 tray_menu->update (tray, false );
190189 tray_qt::notify (tray);
@@ -200,14 +199,14 @@ extern "C" {
200199 }
201200
202201 void tray_exit (void ) {
203- if (tray_qt::qt_tray_menu == nullptr ) {
202+ if (tray_qt::state (). trayMenu == nullptr ) {
204203 return ;
205204 }
206- tray_qt::qt_tray_menu ->exit ();
205+ tray_qt::state (). trayMenu ->exit ();
207206 }
208207
209208 void tray_set_log_callback (void (*cb)(int level, const char *msg)) { // NOSONAR(cpp:S5205): C API requires a plain function pointer callback type
210- tray_qt::log_callback = cb;
209+ tray_qt::state (). logCallback = cb;
211210 if (cb != nullptr ) {
212211 qInstallMessageHandler (tray_qt::qt_message_handler);
213212 } else {
@@ -216,17 +215,17 @@ extern "C" {
216215 }
217216
218217 void tray_show_menu (void ) {
219- if (tray_qt::qt_tray_menu == nullptr ) {
218+ if (tray_qt::state (). trayMenu == nullptr ) {
220219 return ;
221220 }
222- tray_qt::qt_tray_menu ->showMenu ();
221+ tray_qt::state (). trayMenu ->showMenu ();
223222 }
224223
225224 void tray_simulate_menu_item_click (int index) {
226- if (tray_qt::qt_tray_menu == nullptr ) {
225+ if (tray_qt::state (). trayMenu == nullptr ) {
227226 return ;
228227 }
229- tray_qt::qt_tray_menu ->clickMenuItem (index);
228+ tray_qt::state (). trayMenu ->clickMenuItem (index);
230229 }
231230
232231 void tray_simulate_notification_click (void ) {
0 commit comments