Skip to content

Commit 5c51030

Browse files
style: sonar fixes
1 parent fb69597 commit 5c51030

2 files changed

Lines changed: 100 additions & 116 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ endif()
5959

6060
add_library(${PROJECT_NAME} STATIC ${TRAY_SOURCES})
6161
set_property(TARGET ${PROJECT_NAME} PROPERTY C_STANDARD 99)
62-
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)
62+
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 14)
6363

6464
if(WIN32)
6565
list(APPEND TRAY_DEFINITIONS TRAY_WINAPI=1 WIN32_LEAN_AND_MEAN NOMINMAX)

src/tray_linux.cpp

Lines changed: 99 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -4,157 +4,141 @@
44
*/
55
// standard includes
66
#include <cstring>
7+
#include <memory>
78

89
// local includes
910
#include "tray.h"
1011

1112
// Qt includes
1213
#include <QApplication>
14+
#include <QCursor>
1315
#include <QIcon>
1416
#include <QMenu>
1517
#include <QSystemTrayIcon>
1618

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+
}
5148
}
5249
}
50+
return menu;
5351
}
54-
return menu;
55-
}
52+
} // namespace
5653

5754
extern "C" {
5855

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+
}
6562

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;
7466

75-
loop_result = 0;
67+
g_tray_icon = std::make_unique<QSystemTrayIcon>();
7668

77-
tray_icon = new QSystemTrayIcon();
69+
if (!QSystemTrayIcon::isSystemTrayAvailable()) {
70+
g_tray_icon.reset();
71+
return -1;
72+
}
7873

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;
8377
}
8478

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;
9586
}
96-
return loop_result;
97-
}
9887

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+
}
10392

104-
tray_icon->setIcon(QIcon(QString::fromUtf8(tray->icon)));
93+
g_tray_icon->setIcon(QIcon(QString::fromUtf8(tray->icon)));
10594

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+
}
10998

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);
115103
}
116-
tray_menu = new_menu;
117-
}
118104

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);
130119
}
131120
}
132-
}
133121

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+
}
138127
}
139-
}
140-
141-
void tray_exit(void) {
142-
loop_result = -1;
143128

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;
149131

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();
154137

155-
if (app_owned && app != nullptr) {
156-
app->quit();
138+
if (g_app_owned) {
139+
QApplication::quit();
140+
g_app_owned = false;
141+
}
157142
}
158-
}
159143

160144
} // extern "C"

0 commit comments

Comments
 (0)