Skip to content
API Documentation
BastionGuard™
FirstRunServicesWindow Class Reference

#include <first-run-services-window.hpp>

Inheritance diagram for FirstRunServicesWindow:

Classes

struct  ServiceInfo

Public Member Functions

 FirstRunServicesWindow ()

Static Public Member Functions

static void maybe_show ()

Protected Member Functions

bool on_close_request () override

Private Member Functions

void build_headerbar ()
void build_ui ()
void load_current_states ()
void select_all ()
void select_minimal ()
void apply_and_close ()
void on_minimize_clicked ()
void on_close_clicked ()
bool is_user_service_enabled (const std::string &service_name)
bool enable_user_service (const std::string &service_name)
bool disable_user_service (const std::string &service_name)
bool is_system_service_enabled (const std::string &unit_name)
bool enable_system_service (const std::string &unit_name)
bool disable_system_service (const std::string &unit_name)

Private Attributes

std::vector< Gtk::CheckButton * > check_buttons
std::vector< std::string > service_names
Gtk::Button * btn_minimize = nullptr
Gtk::Button * btn_close = nullptr

Detailed Description

Definition at line 28 of file first-run-services-window.hpp.

Constructor & Destructor Documentation

◆ FirstRunServicesWindow()

FirstRunServicesWindow::FirstRunServicesWindow ( )

Definition at line 229 of file first-run-services-window.cpp.

230{
231 set_title("");
232 set_resizable(true);
233 set_default_size(720, 640);
234 set_size_request(680, 580);
235 set_modal(true);
236
238 build_ui();
240}

References build_headerbar(), build_ui(), and load_current_states().

Referenced by maybe_show().

Member Function Documentation

◆ apply_and_close()

void FirstRunServicesWindow::apply_and_close ( )
private

Definition at line 599 of file first-run-services-window.cpp.

600{
601 for (size_t i = 0; i < check_buttons.size(); ++i)
602 {
603 const auto& unit = service_names[i];
604 bool want = check_buttons[i]->get_active();
605
606 bool current = false;
607 if (is_system_unit(unit))
608 current = is_system_service_enabled(unit);
609 else
610 current = is_user_service_enabled(unit);
611
612 if (want == current)
613 continue;
614
615 if (is_system_unit(unit))
616 {
617 if (want) enable_system_service(unit);
618 else disable_system_service(unit);
619 }
620 else
621 {
622 if (want) enable_user_service(unit);
623 else disable_user_service(unit);
624 }
625 }
626
628
630
631 std::thread([]() { install_ca_nss_user(); }).detach();
632
633 hide();
634}
bool disable_user_service(const std::string &service_name)
bool enable_system_service(const std::string &unit_name)
std::vector< Gtk::CheckButton * > check_buttons
bool is_user_service_enabled(const std::string &service_name)
bool is_system_service_enabled(const std::string &unit_name)
bool disable_system_service(const std::string &unit_name)
bool enable_user_service(const std::string &service_name)
std::vector< std::string > service_names
static void install_ca_system()
static void save_config(const std::vector< std::string > &service_names, const std::vector< Gtk::CheckButton * > &check_buttons)
static void install_ca_nss_user()
static bool is_system_unit(const std::string &unit_name)

References check_buttons, disable_system_service(), disable_user_service(), enable_system_service(), enable_user_service(), install_ca_nss_user(), install_ca_system(), is_system_service_enabled(), is_user_service_enabled(), save_config(), and service_names.

Referenced by build_ui(), on_close_clicked(), and on_close_request().

◆ build_headerbar()

void FirstRunServicesWindow::build_headerbar ( )
private

Definition at line 257 of file first-run-services-window.cpp.

258{
259 auto header = Gtk::make_managed<Gtk::HeaderBar>();
260 header->set_show_title_buttons(false);
261 header->set_decoration_layout("");
262 header->get_style_context()->add_class("custom-headerbar");
263
264 auto title_label = Gtk::make_managed<Gtk::Label>(_("BastionGuard - Configurazione iniziale"));
265 title_label->get_style_context()->add_class("header-title");
266 title_label->set_halign(Gtk::Align::CENTER);
267 title_label->set_valign(Gtk::Align::CENTER);
268 header->set_title_widget(*title_label);
269
270 auto header_right = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL);
271
272 try {
273 auto pix_min = Gdk::Pixbuf::create_from_file(resource("icon-minimize.png"), 32, 32, true);
274 btn_minimize = Gtk::make_managed<Gtk::Button>();
275 btn_minimize->set_child(*Gtk::make_managed<Gtk::Image>(pix_min));
276 btn_minimize->get_style_context()->add_class("header-button");
277 header_right->append(*btn_minimize);
278 btn_minimize->signal_clicked().connect(sigc::mem_fun(*this, &FirstRunServicesWindow::on_minimize_clicked));
279 } catch (...) {
280 std::cerr << _("⚠ Errore caricamento icona minimize\n");
281 }
282
283 try {
284 auto pix_close = Gdk::Pixbuf::create_from_file(resource("icon-close.png"), 32, 32, true);
285 btn_close = Gtk::make_managed<Gtk::Button>();
286 btn_close->set_child(*Gtk::make_managed<Gtk::Image>(pix_close));
287 btn_close->get_style_context()->add_class("header-button");
288 header_right->append(*btn_close);
289 btn_close->signal_clicked().connect(sigc::mem_fun(*this, &FirstRunServicesWindow::on_close_clicked));
290 } catch (...) {
291 std::cerr << _("⚠ Errore caricamento icona close\n");
292 }
293
294 header->pack_end(*header_right);
295 set_titlebar(*header);
296}
#define _(s)
std::string resource(const std::string &rel)
Definition Resource.hpp:31

References _, btn_close, btn_minimize, on_close_clicked(), on_minimize_clicked(), and resource().

Referenced by FirstRunServicesWindow().

◆ build_ui()

void FirstRunServicesWindow::build_ui ( )
private

Definition at line 298 of file first-run-services-window.cpp.

299{
300 auto main_box = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 0);
301 main_box->set_vexpand(false);
302 main_box->set_hexpand(true);
303 set_child(*main_box);
304
305 auto sidebar = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 10);
306 sidebar->set_size_request(240, -1);
307 sidebar->set_vexpand(false);
308 sidebar->get_style_context()->add_class("sidebar");
309
310 auto header_sidebar = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 8);
311 header_sidebar->get_style_context()->add_class("sidebar-header");
312
313 try {
314 auto pixbuf = Gdk::Pixbuf::create_from_file(resource("logo-sidebar.png"), 96, 96, true);
315 auto logo = Gtk::make_managed<Gtk::Image>(pixbuf);
316 header_sidebar->append(*logo);
317 } catch (...) {
318 std::cerr << _("⚠ Impossibile caricare logo-sidebar.png\n");
319 }
320
321 auto lbl = Gtk::make_managed<Gtk::Label>("BastionGuard");
322 lbl->set_halign(Gtk::Align::START);
323 lbl->get_style_context()->add_class("sidebar-title");
324 header_sidebar->append(*lbl);
325
326 sidebar->append(*header_sidebar);
327
328 auto spacer = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL);
329 spacer->set_vexpand(true);
330 sidebar->append(*spacer);
331
332 main_box->append(*sidebar);
333
334 auto content_box = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 12);
335 content_box->set_margin(24);
336 content_box->set_hexpand(true);
337 content_box->set_vexpand(false);
338 content_box->get_style_context()->add_class("content");
339 main_box->append(*content_box);
340
341 auto title = Gtk::make_managed<Gtk::Label>(_("Scegli quali servizi di protezione attivare"));
342 title->get_style_context()->add_class("title");
343 content_box->append(*title);
344
345 auto subtitle = Gtk::make_managed<Gtk::Label>(
346 _("I servizi selezionati verranno avviati automaticamente al login.\n"
347 "Potrai modificarli in qualsiasi momento dalle impostazioni."));
348 subtitle->set_wrap();
349 subtitle->add_css_class("alert-info");
350 content_box->append(*subtitle);
351
352 auto grid = Gtk::make_managed<Gtk::Grid>();
353 grid->set_column_spacing(20);
354 grid->set_row_spacing(10);
355 grid->set_margin_top(12);
356 grid->set_margin_bottom(12);
357 grid->set_vexpand(false);
358 grid->set_valign(Gtk::Align::START);
359 content_box->append(*grid);
360
361 std::vector<ServiceInfo> services = {
362 {"BastionGuard-privacyd.service", _("Privacy Guard"), _("Blocco microfono e webcam quando non in uso")},
363 {"BastionGuard-ransomware-alert.service", _("Ransomware Alert"), _("Notifiche su possibili attività ransomware")},
364 {"BastionGuard-ransomware-realtime-alert.service", _("Ransomware Realtime Alert"), _("Avvisi in tempo reale su comportamenti sospetti")},
365 {"BastionGuard-ransomware-scanner.service", _("Ransomware Scanner"), _("Scansione periodica alla ricerca di comportamenti malevoli")},
366 {"BastionGuard-useragent.service", _("User-Agent Randomizer"), _("Modifica user-agent per aumentare anonimato")},
367 {"BastionGuard-pacd.service", _("PAC Daemon"), _("Gestione automatica di proxy PAC sicuri")},
368 {"BastionGuard-phishing-scanner.service", _("Phishing Scanner (System)"), _("Scansione anti-phishing a livello di sistema")},
369 {"BastionGuard-mailproxy.service", _("Mail Proxy"), _("Proxy SMTP locale per protezione email in uscita")},
370 {"BastionGuard-usbd.service", _("USB Protection (System)"), _("Monitoraggio e blocco dispositivi USB sospetti")},
371 {"BastionGuard-ransomware-realtime.service", _("Ransomware Realtime (System)"), _("Monitor realtime ransomware a livello di sistema")},
372 };
373
375 services.push_back({"BastionGuard-cef.service", _("CEF Sandbox Service"), _("Supporto in background per pagamenti sicuri con CEF")});
376 }
377
378 int row = 0;
379 for (const auto& svc : services)
380 {
381 auto check = Gtk::make_managed<Gtk::CheckButton>(svc.label);
382 check->set_active(true);
383
384 auto desc = Gtk::make_managed<Gtk::Label>(svc.description);
385 desc->add_css_class("label");
386 desc->set_wrap();
387 desc->set_halign(Gtk::Align::START);
388 desc->set_margin_start(24);
389 desc->set_max_width_chars(50);
390 desc->set_ellipsize(Pango::EllipsizeMode::END);
391
392 grid->attach(*check, 0, row, 1, 1);
393 grid->attach(*desc, 1, row, 1, 1);
394
395 check_buttons.push_back(check);
396 service_names.push_back(svc.name);
397
398 row++;
399 }
400
401 auto btn_box = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 12);
402 btn_box->set_halign(Gtk::Align::END);
403 btn_box->set_margin_top(16);
404 btn_box->set_vexpand(false);
405
406 auto btn_minimal = Gtk::make_managed<Gtk::Button>(_("Configurazione minima"));
407 btn_minimal->get_style_context()->add_class("btn-warning");
408 btn_minimal->signal_clicked().connect(sigc::mem_fun(*this, &FirstRunServicesWindow::select_minimal));
409
410 auto btn_all = Gtk::make_managed<Gtk::Button>(_("Attiva tutto"));
411 btn_all->get_style_context()->add_class("btn-primary");
412 btn_all->signal_clicked().connect(sigc::mem_fun(*this, &FirstRunServicesWindow::select_all));
413
414 auto btn_apply = Gtk::make_managed<Gtk::Button>(_("Applica e continua"));
415 btn_apply->get_style_context()->add_class("btn-success");
416 btn_apply->signal_clicked().connect(sigc::mem_fun(*this, &FirstRunServicesWindow::apply_and_close));
417
418 btn_box->append(*btn_minimal);
419 btn_box->append(*btn_all);
420 btn_box->append(*btn_apply);
421
422 content_box->append(*btn_box);
423}

References _, apply_and_close(), BastionGuard::Platform::cef_available(), check_buttons, resource(), select_all(), select_minimal(), and service_names.

Referenced by FirstRunServicesWindow().

◆ disable_system_service()

bool FirstRunServicesWindow::disable_system_service ( const std::string & unit_name)
private

Definition at line 732 of file first-run-services-window.cpp.

733{
734 pkexec_servicectl({"stop", unit_name}, false);
735 return pkexec_servicectl({"disable", "--now", unit_name}, false);
736}
static bool pkexec_servicectl(const std::vector< std::string > &args, bool silence_stderr=false)

Referenced by apply_and_close().

◆ disable_user_service()

bool FirstRunServicesWindow::disable_user_service ( const std::string & service_name)
private

Definition at line 690 of file first-run-services-window.cpp.

691{
692 run_cmd({"/usr/libexec/bastionguard/bastionguard-service", "--user", "stop", service_name});
693 return run_cmd({"/usr/libexec/bastionguard/bastionguard-service", "--user", "disable", "--now", service_name});
694}
static bool run_cmd(const std::vector< std::string > &argv, bool silence_stderr=true, bool capture_stdout=false, std::string *out_stdout=nullptr)

Referenced by apply_and_close().

◆ enable_system_service()

bool FirstRunServicesWindow::enable_system_service ( const std::string & unit_name)
private

Definition at line 723 of file first-run-services-window.cpp.

724{
725 if (!pkexec_servicectl({"enable", "--now", unit_name}, false))
726 return false;
727
728 pkexec_servicectl({"start", unit_name}, false);
729 return true;
730}

Referenced by apply_and_close().

◆ enable_user_service()

bool FirstRunServicesWindow::enable_user_service ( const std::string & service_name)
private

Definition at line 681 of file first-run-services-window.cpp.

682{
683 if (!run_cmd({"/usr/libexec/bastionguard/bastionguard-service", "--user", "enable", "--now", service_name}))
684 return false;
685
686 run_cmd({"/usr/libexec/bastionguard/bastionguard-service", "--user", "start", service_name});
687 return true;
688}

Referenced by apply_and_close().

◆ is_system_service_enabled()

bool FirstRunServicesWindow::is_system_service_enabled ( const std::string & unit_name)
private

Definition at line 696 of file first-run-services-window.cpp.

697{
698 try
699 {
700 auto proc = Gio::Subprocess::create(
701 {"/usr/libexec/bastionguard/bastionguard-service", "--system", "is-enabled", unit_name},
702 Gio::Subprocess::Flags::STDOUT_PIPE |
703 Gio::Subprocess::Flags::STDERR_PIPE
704 );
705
706 auto [stdout_str, stderr_str] = proc->communicate_utf8("");
707
708 if (!proc->get_successful())
709 return false;
710
711 std::string result = stdout_str.raw();
712 result.erase(std::remove(result.begin(), result.end(), '\n'), result.end());
713 result.erase(std::remove(result.begin(), result.end(), '\r'), result.end());
714
715 return result == "enabled";
716 }
717 catch (...)
718 {
719 return false;
720 }
721}

Referenced by apply_and_close(), and load_current_states().

◆ is_user_service_enabled()

bool FirstRunServicesWindow::is_user_service_enabled ( const std::string & service_name)
private

Definition at line 654 of file first-run-services-window.cpp.

655{
656 try
657 {
658 auto proc = Gio::Subprocess::create(
659 {"/usr/libexec/bastionguard/bastionguard-service", "--user", "is-enabled", service_name},
660 Gio::Subprocess::Flags::STDOUT_PIPE |
661 Gio::Subprocess::Flags::STDERR_PIPE
662 );
663
664 auto [stdout_str, stderr_str] = proc->communicate_utf8("");
665
666 if (!proc->get_successful())
667 return false;
668
669 std::string result = stdout_str.raw();
670 result.erase(std::remove(result.begin(), result.end(), '\n'), result.end());
671 result.erase(std::remove(result.begin(), result.end(), '\r'), result.end());
672
673 return result == "enabled";
674 }
675 catch (...)
676 {
677 return false;
678 }
679}

Referenced by apply_and_close(), and load_current_states().

◆ load_current_states()

void FirstRunServicesWindow::load_current_states ( )
private

Definition at line 425 of file first-run-services-window.cpp.

426{
427 for (size_t i = 0; i < check_buttons.size(); ++i)
428 {
429 const auto& unit = service_names[i];
430
431 bool enabled = false;
432 if (is_system_unit(unit))
433 enabled = is_system_service_enabled(unit);
434 else
435 enabled = is_user_service_enabled(unit);
436
437 check_buttons[i]->set_active(enabled);
438 }
439}

References check_buttons, is_system_service_enabled(), is_user_service_enabled(), and service_names.

Referenced by FirstRunServicesWindow().

◆ maybe_show()

void FirstRunServicesWindow::maybe_show ( )
static

Definition at line 242 of file first-run-services-window.cpp.

243{
244 if (Glib::file_test(FLAG_FILE, Glib::FileTest::EXISTS))
245 return;
246
247 auto* win = new FirstRunServicesWindow();
248
249 win->signal_hide().connect([win]() {
251 delete win;
252 });
253
254 win->show();
255}
static void touch_flag_file()

References FirstRunServicesWindow(), and touch_flag_file().

Referenced by ClamApp::on_activate().

◆ on_close_clicked()

void FirstRunServicesWindow::on_close_clicked ( )
private

Definition at line 643 of file first-run-services-window.cpp.

644{
646}

References apply_and_close().

Referenced by build_headerbar().

◆ on_close_request()

bool FirstRunServicesWindow::on_close_request ( )
overrideprotected

Definition at line 648 of file first-run-services-window.cpp.

649{
651 return false;
652}

References apply_and_close().

◆ on_minimize_clicked()

void FirstRunServicesWindow::on_minimize_clicked ( )
private

Definition at line 636 of file first-run-services-window.cpp.

637{
638#if GTKMM_CHECK_VERSION(4,10,0)
639 hide();
640#endif
641}

Referenced by build_headerbar().

◆ select_all()

void FirstRunServicesWindow::select_all ( )
private

Definition at line 441 of file first-run-services-window.cpp.

442{
443 for (auto* ch : check_buttons)
444 ch->set_active(true);
445}

References check_buttons.

Referenced by build_ui().

◆ select_minimal()

void FirstRunServicesWindow::select_minimal ( )
private

Definition at line 447 of file first-run-services-window.cpp.

448{
449 std::set<std::string> minimal = {
450 "BastionGuard-usbd.service",
451 "BastionGuard-privacyd.service",
452 "BastionGuard-ransomware-realtime-alert.service"
453 };
454
455 for (size_t i = 0; i < check_buttons.size(); ++i)
456 {
457 check_buttons[i]->set_active(minimal.contains(service_names[i]));
458 }
459}

References check_buttons, and service_names.

Referenced by build_ui().

Member Data Documentation

◆ btn_close

Gtk::Button* FirstRunServicesWindow::btn_close = nullptr
private

Definition at line 63 of file first-run-services-window.hpp.

Referenced by build_headerbar().

◆ btn_minimize

Gtk::Button* FirstRunServicesWindow::btn_minimize = nullptr
private

Definition at line 62 of file first-run-services-window.hpp.

Referenced by build_headerbar().

◆ check_buttons

std::vector<Gtk::CheckButton*> FirstRunServicesWindow::check_buttons
private

◆ service_names

std::vector<std::string> FirstRunServicesWindow::service_names
private

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