Skip to content
API Documentation
BastionGuard™
FirewallChoiceDialog.cpp
Go to the documentation of this file.
1/*
2 * BastionGuard™
3 * Copyright (C) 2025–2026 Calogero Scarnà
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * BastionGuard™ is a trademark of Calogero Scarnà.
18 * The BastionGuard™ name and branding are not licensed under the GPL.
19 */
20
22#include "Utils.hpp"
23#include <iostream>
24#include <filesystem>
25#include <fstream>
26#include <glib/gi18n.h>
27#include <glibmm/miscutils.h>
28
29// Sceglie il CSS in base al tema salvato in ~/.config/BastionGuard/theme.conf.
30// Stessa logica di StyleProvider::load_style(), così la finestra di
31// configurazione del firewall IP segue il tema (dark/light) impostato
32// dall'utente in Settings.
34 const std::string conf =
35 Glib::get_home_dir() + "/.config/BastionGuard/theme.conf";
36 std::ifstream tf(conf);
37 std::string theme;
38 if (tf >> theme && theme == "dark")
39 return "BastionGuard-dark.css";
40 return "BastionGuard.css";
41}
42
44 : vbox(Gtk::Orientation::VERTICAL, 15),
45 titleLabel(_("Protezione Firewall")),
46 descriptionLabel(_("Scegli quale firewall usare per bloccare gli IP malevoli:")),
47 firewalldButton(_("Usa firewalld")),
48 ufwButton(_("Usa UFW")),
49 installButton(_("Installa firewall")),
50 cancelButton(_("Annulla"))
51{
54
55
56 set_title("");
57 set_default_size(520, 260);
58 set_resizable(false);
59 set_modal(true);
60
61 auto ctx = get_style_context();
62 ctx->add_class("app-dialog");
63 ctx->add_class("main-window");
64 ctx->add_class("app-window");
65
66
67 try {
68 auto css = Gtk::CssProvider::create();
69 css->load_from_path(resource(firewall_choice_resolve_theme_css()));
70 Gtk::StyleContext::add_provider_for_display(
71 Gdk::Display::get_default(), css,
72 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
73 );
74 } catch (...) {
75 std::cerr << _("Errore caricamento CSS\n");
76 }
77
78 auto *header = Gtk::make_managed<Gtk::HeaderBar>();
79 header->set_show_title_buttons(false);
80 header->add_css_class("custom-headerbar");
81
82 auto *close_box = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL);
83 close_box->set_margin_end(6);
84
85 try {
86 auto px = Gdk::Pixbuf::create_from_file(resource("icon-close.png"), 28, 28, true);
87 auto *btn = Gtk::make_managed<Gtk::Button>();
88 btn->set_child(*Gtk::make_managed<Gtk::Image>(px));
89 btn->add_css_class("header-button");
90 btn->signal_clicked().connect([this]() { hide(); });
91 close_box->append(*btn);
92 } catch (...) {
93 std::cerr << _("Icona X non trovata\n");
94 }
95
96 header->pack_end(*close_box);
97
98 auto *header_wrapper = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL);
99 header_wrapper->append(*header);
100 set_titlebar(*header_wrapper);
101
102 titleLabel.set_halign(Gtk::Align::CENTER);
103 titleLabel.add_css_class("title");
104
105 descriptionLabel.set_wrap(true);
106 descriptionLabel.set_halign(Gtk::Align::CENTER);
107 descriptionLabel.set_margin_bottom(10);
108
109 firewalldButton.add_css_class("btn-success");
110 ufwButton.add_css_class("btn-success");
111 installButton.add_css_class("btn-warning");
112 cancelButton.add_css_class("btn-danger");
113
114 auto *buttonBox = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 10);
115 buttonBox->set_halign(Gtk::Align::CENTER);
116
118 buttonBox->append(firewalldButton);
119
121 buttonBox->append(ufwButton);
122
125
126 if (!cmd.empty()) {
127 installButton.set_label(
128 Glib::ustring::compose(
129 _("Installa firewall (%1)"),
130 cmd
131 )
132 );
133 buttonBox->append(installButton);
134 }
135 }
136
137 buttonBox->append(cancelButton);
138
139 set_child(vbox);
140 vbox.set_margin(25);
141 vbox.set_halign(Gtk::Align::CENTER);
142
143 vbox.append(titleLabel);
144 vbox.append(descriptionLabel);
145 vbox.append(*buttonBox);
146
147
148 firewalldButton.signal_clicked().connect([this]() {
150 hide();
151 });
152
153 ufwButton.signal_clicked().connect([this]() {
155 hide();
156 });
157
158 installButton.signal_clicked().connect([this]() {
160 this,
161 _("Per installare il firewall esegui il comando:\n\n") +
163 Gtk::MessageType::INFO
164 );
165 });
166
167 cancelButton.signal_clicked().connect([this]() {
168 hide();
169 });
170
171}
#define _(s)
static std::string firewall_choice_resolve_theme_css()
sigc::signal< void(FirewallType)> signal_firewall_selected
static bool isUfwPresent()
static bool isFirewalldPresent()
static FirewallType detectFirewall()
static std::string detectDistro()
static std::string getInstallCommand(const std::string &distro, bool preferFirewalld=true)
static void show_message(Gtk::Window *parent, const std::string &message, Gtk::MessageType type=Gtk::MessageType::INFO, const std::string &secondary=std::string())
Definition Utils.cpp:33
std::string resource(const std::string &rel)
Definition Resource.hpp:31