Skip to content
API Documentation
BastionGuard™
TrayIcon Class Reference

#include <TrayIcon.hpp>

Public Member Functions

 TrayIcon ()
 ~TrayIcon ()
bool start ()
void stop ()
void set_icon (const std::string &icon_name)
void set_icon_file (const std::string &png_path, int target_px=22)
void set_tooltip (const std::string &text)
void set_title (const std::string &title)
void set_on_activate (std::function< void()> cb)
void set_on_settings (std::function< void()> cb)
void set_on_quit (std::function< void()> cb)
void bind_ui_context ()
 TrayIcon ()
 ~TrayIcon ()
bool start ()
void stop ()
void set_icon (const std::string &icon_name)
void set_icon_file (const std::string &png_path, int target_px=22)
void set_tooltip (const std::string &text)
void set_title (const std::string &title)
void set_on_activate (std::function< void()> cb)
void set_on_make_startup_persistent (std::function< void()> cb)
void set_on_quit (std::function< void()> cb)
void bind_ui_context ()

Private Attributes

std::string service_name_
std::string id_ = "bastionguard"
std::string title_ = "BastionGuard"
std::string icon_name_ = "security-high"
std::string tooltip_ = "BastionGuard in esecuzione"
std::string status_ = "Active"
GMainContext * ui_ctx_ = nullptr
bool have_pixmap_ = false
int pix_w_ = 0
int pix_h_ = 0
std::vector< uint8_t > pix_rgba_
std::function< void()> on_activate_cb_
std::function< void()> on_settings_cb_
std::function< void()> on_quit_cb_
std::thread bus_thread_
std::atomic_bool running_ {false}
std::function< void()> on_make_startup_persistent_cb_

Static Private Attributes

static constexpr int MENU_ROOT = 0
static constexpr int MENU_SHOW = 1
static constexpr int MENU_SETTINGS = 2
static constexpr int MENU_QUIT = 3
static constexpr int MENU_PERSIST_STARTUP = 2

Detailed Description

Definition at line 37 of file TrayIcon.hpp.

Constructor & Destructor Documentation

◆ TrayIcon() [1/2]

TrayIcon::TrayIcon ( )
default

Definition at line 118 of file TrayIcon.cpp.

118 {
119 service_name_ = make_service_name();
120 id_ = "BastionGuard";
121 title_ = "BastionGuard";
122 status_ = "Active";
123 icon_name_ = "security-high";
124 tooltip_ = "BastionGuard";
125}
std::string tooltip_
Definition TrayIcon.hpp:97
std::string service_name_
Definition TrayIcon.hpp:93
std::string status_
Definition TrayIcon.hpp:98
std::string title_
Definition TrayIcon.hpp:95
std::string id_
Definition TrayIcon.hpp:94
std::string icon_name_
Definition TrayIcon.hpp:96

References icon_name_, id_, service_name_, status_, title_, and tooltip_.

◆ ~TrayIcon() [1/2]

TrayIcon::~TrayIcon ( )

Definition at line 127 of file TrayIcon.cpp.

127 {
128 stop();
129 if (ui_ctx_) {
130 g_main_context_unref(ui_ctx_);
131 ui_ctx_ = nullptr;
132 }
133}
void stop()
Definition TrayIcon.cpp:282
GMainContext * ui_ctx_
Definition TrayIcon.hpp:99

References stop(), and ui_ctx_.

◆ TrayIcon() [2/2]

TrayIcon::TrayIcon ( )

◆ ~TrayIcon() [2/2]

TrayIcon::~TrayIcon ( )

Member Function Documentation

◆ bind_ui_context() [1/2]

void TrayIcon::bind_ui_context ( )

Definition at line 297 of file TrayIcon.cpp.

297 {
298 if (ui_ctx_) return;
299 ui_ctx_ = g_main_context_ref(g_main_context_default());
300}

References ui_ctx_.

◆ bind_ui_context() [2/2]

void TrayIcon::bind_ui_context ( )

◆ set_icon() [1/2]

void TrayIcon::set_icon ( const std::string & icon_name)

Definition at line 363 of file TrayIcon.cpp.

363 {
364 icon_name_ = icon_name.empty() ? "security-high" : icon_name;
365 emit_sni_signal("NewIcon");
366}

References icon_name_.

◆ set_icon() [2/2]

void TrayIcon::set_icon ( const std::string & icon_name)

◆ set_icon_file() [1/2]

void TrayIcon::set_icon_file ( const std::string & png_path,
int target_px = 22 )

Definition at line 302 of file TrayIcon.cpp.

302 {
303 GError* err = nullptr;
304 GdkPixbuf* pb = gdk_pixbuf_new_from_file(png_path.c_str(), &err);
305 if (!pb) {
306 std::fprintf(stderr, "[TrayIcon] cannot load icon '%s': %s\n",
307 png_path.c_str(), err ? err->message : "unknown error");
308 if (err) g_error_free(err);
309 have_pixmap_ = false;
310 emit_sni_signal("NewIcon");
311 return;
312 }
313
314 if (target_px > 0) {
315 int w = gdk_pixbuf_get_width(pb);
316 int h = gdk_pixbuf_get_height(pb);
317 if (w != target_px || h != target_px) {
318 GdkPixbuf* scaled = gdk_pixbuf_scale_simple(pb, target_px, target_px, GDK_INTERP_BILINEAR);
319 g_object_unref(pb);
320 pb = scaled;
321 }
322 }
323
324 GdkPixbuf* rgba = gdk_pixbuf_add_alpha(pb, FALSE, 0, 0, 0);
325 g_object_unref(pb);
326
327 pix_w_ = gdk_pixbuf_get_width(rgba);
328 pix_h_ = gdk_pixbuf_get_height(rgba);
329
330 const int rowstride = gdk_pixbuf_get_rowstride(rgba);
331 const int nchan = gdk_pixbuf_get_n_channels(rgba);
332 const guchar* pixels = gdk_pixbuf_get_pixels(rgba);
333
334 pix_rgba_.resize((size_t)pix_w_ * (size_t)pix_h_ * 4u);
335
336 for (int y = 0; y < pix_h_; ++y) {
337 const guchar* src = pixels + y * rowstride;
338 uint8_t* dst = pix_rgba_.data() + (size_t)y * (size_t)pix_w_ * 4u;
339
340 for (int x = 0; x < pix_w_; ++x) {
341 uint8_t r = src[x * nchan + 0];
342 uint8_t g = src[x * nchan + 1];
343 uint8_t b = src[x * nchan + 2];
344 uint8_t a = (nchan >= 4) ? src[x * nchan + 3] : 255u;
345
346 uint32_t argb = ((uint32_t)a << 24) |
347 ((uint32_t)r << 16) |
348 ((uint32_t)g << 8) |
349 (uint32_t)b;
350 argb = htobe32(argb);
351 std::memcpy(dst + x * 4, &argb, 4);
352 }
353 }
354
355 g_object_unref(rgba);
356
357 have_pixmap_ = true;
358 icon_name_.clear();
359
360 emit_sni_signal("NewIcon");
361}
bool have_pixmap_
Definition TrayIcon.hpp:101
std::vector< uint8_t > pix_rgba_
Definition TrayIcon.hpp:104

References have_pixmap_, icon_name_, pix_h_, pix_rgba_, and pix_w_.

◆ set_icon_file() [2/2]

void TrayIcon::set_icon_file ( const std::string & png_path,
int target_px = 22 )

◆ set_on_activate() [1/2]

void TrayIcon::set_on_activate ( std::function< void()> cb)
inline

Definition at line 54 of file TrayIcon.hpp.

54{ on_activate_cb_ = std::move(cb); }
std::function< void()> on_activate_cb_
Definition TrayIcon.hpp:107

References on_activate_cb_.

◆ set_on_activate() [2/2]

void TrayIcon::set_on_activate ( std::function< void()> cb)
inline

Definition at line 54 of file TrayIcon.hpp.

54{ on_activate_cb_ = std::move(cb); }

References on_activate_cb_.

◆ set_on_make_startup_persistent()

void TrayIcon::set_on_make_startup_persistent ( std::function< void()> cb)
inline

Definition at line 55 of file TrayIcon.hpp.

55{ on_make_startup_persistent_cb_ = std::move(cb); }
std::function< void()> on_make_startup_persistent_cb_
Definition TrayIcon.hpp:108

References on_make_startup_persistent_cb_.

◆ set_on_quit() [1/2]

void TrayIcon::set_on_quit ( std::function< void()> cb)
inline

Definition at line 56 of file TrayIcon.hpp.

56{ on_quit_cb_ = std::move(cb); }
std::function< void()> on_quit_cb_
Definition TrayIcon.hpp:109

References on_quit_cb_.

◆ set_on_quit() [2/2]

void TrayIcon::set_on_quit ( std::function< void()> cb)
inline

Definition at line 56 of file TrayIcon.hpp.

56{ on_quit_cb_ = std::move(cb); }

References on_quit_cb_.

◆ set_on_settings()

void TrayIcon::set_on_settings ( std::function< void()> cb)
inline

Definition at line 55 of file TrayIcon.hpp.

55{ on_settings_cb_ = std::move(cb); }
std::function< void()> on_settings_cb_
Definition TrayIcon.hpp:108

References on_settings_cb_.

◆ set_title() [1/2]

void TrayIcon::set_title ( const std::string & title)

Definition at line 373 of file TrayIcon.cpp.

373 {
374 title_ = title;
375 emit_sni_signal("NewTitle");
376}

References title_.

◆ set_title() [2/2]

void TrayIcon::set_title ( const std::string & title)

◆ set_tooltip() [1/2]

void TrayIcon::set_tooltip ( const std::string & text)

Definition at line 368 of file TrayIcon.cpp.

368 {
369 tooltip_ = text;
370 emit_sni_signal("NewToolTip");
371}

References tooltip_.

Referenced by MainWindow::onDaemonAlert(), MainWindow::onDaemonConnected(), and MainWindow::onDaemonDisconnected().

◆ set_tooltip() [2/2]

void TrayIcon::set_tooltip ( const std::string & text)

◆ start() [1/2]

bool TrayIcon::start ( )

Definition at line 135 of file TrayIcon.cpp.

135 {
136 if (running_.exchange(true)) return true;
137
138 if (!acquire_bus()) { running_ = false; return false; }
139 if (!request_name()) { release_bus(); running_ = false; return false; }
140 if (!export_objects()) { release_bus(); running_ = false; return false; }
141
142 bus_thread_ = std::thread([this]() {
143 bool ok = register_to_watcher();
144 std::fprintf(stderr, "[TrayIcon] register_to_watcher: %s (service: %s)\n",
145 ok ? "OK" : "FAILED", service_name_.c_str());
146
147 std::this_thread::sleep_for(std::chrono::milliseconds(150));
148 emit_dbusmenu_layout_updated();
149
150 bus_loop();
151 });
152
153 return true;
154}
std::thread bus_thread_
Definition TrayIcon.hpp:117
std::atomic_bool running_
Definition TrayIcon.hpp:118

References bus_thread_, running_, and service_name_.

◆ start() [2/2]

bool TrayIcon::start ( )

◆ stop() [1/2]

void TrayIcon::stop ( )

Definition at line 282 of file TrayIcon.cpp.

282 {
283 if (!running_.exchange(false)) return;
284
285 if (bus_thread_.joinable() &&
286 std::this_thread::get_id() == bus_thread_.get_id()) {
287 return;
288 }
289
290 if (bus_thread_.joinable())
291 bus_thread_.join();
292
293 unexport_objects();
294 release_bus();
295}

References bus_thread_, and running_.

Referenced by MainWindow::~MainWindow(), and ~TrayIcon().

◆ stop() [2/2]

void TrayIcon::stop ( )

Member Data Documentation

◆ bus_thread_

std::thread TrayIcon::bus_thread_
private

Definition at line 117 of file TrayIcon.hpp.

Referenced by start(), and stop().

◆ have_pixmap_

bool TrayIcon::have_pixmap_ = false
private

Definition at line 101 of file TrayIcon.hpp.

Referenced by bind_ui_context(), and set_icon_file().

◆ icon_name_

std::string TrayIcon::icon_name_ = "security-high"
private

Definition at line 96 of file TrayIcon.hpp.

Referenced by bind_ui_context(), set_icon(), set_icon_file(), and TrayIcon().

◆ id_

std::string TrayIcon::id_ = "bastionguard"
private

Definition at line 94 of file TrayIcon.hpp.

Referenced by bind_ui_context(), and TrayIcon().

◆ MENU_PERSIST_STARTUP

int TrayIcon::MENU_PERSIST_STARTUP = 2
staticconstexprprivate

Definition at line 122 of file TrayIcon.hpp.

◆ MENU_QUIT

constexpr int TrayIcon::MENU_QUIT = 3
staticconstexprprivate

Definition at line 123 of file TrayIcon.hpp.

◆ MENU_ROOT

constexpr int TrayIcon::MENU_ROOT = 0
staticconstexprprivate

Definition at line 120 of file TrayIcon.hpp.

◆ MENU_SETTINGS

int TrayIcon::MENU_SETTINGS = 2
staticconstexprprivate

Definition at line 122 of file TrayIcon.hpp.

◆ MENU_SHOW

constexpr int TrayIcon::MENU_SHOW = 1
staticconstexprprivate

Definition at line 121 of file TrayIcon.hpp.

◆ on_activate_cb_

std::function< void()> TrayIcon::on_activate_cb_
private

Definition at line 107 of file TrayIcon.hpp.

Referenced by bind_ui_context(), and set_on_activate().

◆ on_make_startup_persistent_cb_

std::function<void()> TrayIcon::on_make_startup_persistent_cb_
private

Definition at line 108 of file TrayIcon.hpp.

Referenced by set_on_make_startup_persistent().

◆ on_quit_cb_

std::function< void()> TrayIcon::on_quit_cb_
private

Definition at line 109 of file TrayIcon.hpp.

Referenced by set_on_quit().

◆ on_settings_cb_

std::function<void()> TrayIcon::on_settings_cb_
private

Definition at line 108 of file TrayIcon.hpp.

Referenced by set_on_settings().

◆ pix_h_

int TrayIcon::pix_h_ = 0
private

Definition at line 103 of file TrayIcon.hpp.

Referenced by bind_ui_context(), and set_icon_file().

◆ pix_rgba_

std::vector< uint8_t > TrayIcon::pix_rgba_
private

Definition at line 104 of file TrayIcon.hpp.

Referenced by bind_ui_context(), and set_icon_file().

◆ pix_w_

int TrayIcon::pix_w_ = 0
private

Definition at line 102 of file TrayIcon.hpp.

Referenced by bind_ui_context(), and set_icon_file().

◆ running_

std::atomic_bool TrayIcon::running_ {false}
private

Definition at line 118 of file TrayIcon.hpp.

Referenced by start(), and stop().

◆ service_name_

std::string TrayIcon::service_name_
private

Definition at line 93 of file TrayIcon.hpp.

Referenced by bind_ui_context(), start(), and TrayIcon().

◆ status_

std::string TrayIcon::status_ = "Active"
private

Definition at line 98 of file TrayIcon.hpp.

Referenced by bind_ui_context(), and TrayIcon().

◆ title_

std::string TrayIcon::title_ = "BastionGuard"
private

Definition at line 95 of file TrayIcon.hpp.

Referenced by bind_ui_context(), set_title(), and TrayIcon().

◆ tooltip_

std::string TrayIcon::tooltip_ = "BastionGuard in esecuzione"
private

Definition at line 97 of file TrayIcon.hpp.

Referenced by bind_ui_context(), set_tooltip(), and TrayIcon().

◆ ui_ctx_

GMainContext * TrayIcon::ui_ctx_ = nullptr
private

Definition at line 99 of file TrayIcon.hpp.

Referenced by bind_ui_context(), and ~TrayIcon().


The documentation for this class was generated from the following files: