Skip to content
API Documentation
BastionGuard™
StyleProvider.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
21#include "StyleProvider.hpp"
22#include <gtkmm/stylecontext.h>
23#include <gdkmm/display.h>
24#include <glibmm/miscutils.h>
25#include <glibmm/i18n.h>
26#include <iostream>
27#include <fstream>
28#include <filesystem>
29
30#ifndef DATA_DIR
31#define DATA_DIR "/usr/share/BastionGuard/data"
32#endif
33
34Glib::RefPtr<Gtk::CssProvider> global_css;
35
36
37static std::string read_saved_theme() {
38 const std::string path =
39 Glib::get_home_dir() + "/.config/BastionGuard/theme.conf";
40 std::ifstream f(path);
41 if (!f.is_open()) return "light";
42 std::string val;
43 f >> val;
44 return (val == "dark") ? "dark" : "light";
45}
46
48{
49 global_css = Gtk::CssProvider::create();
50
51 const std::string theme = read_saved_theme();
52 const std::string cssName = (theme == "dark")
53 ? "BastionGuard-dark.css"
54 : "BastionGuard.css";
55
56 const std::string local = "data/" + cssName;
57 const std::string installed = std::string(DATA_DIR) + "/" + cssName;
58
59 std::string used;
60
61 try {
62 if (std::filesystem::exists(local)) {
63 global_css->load_from_path(local);
64 used = local;
65 } else if (std::filesystem::exists(installed)) {
66 global_css->load_from_path(installed);
67 used = installed;
68 } else {
69 const std::string fallback_local = "data/BastionGuard.css";
70 const std::string fallback_installed = std::string(DATA_DIR) + "/BastionGuard.css";
71 if (std::filesystem::exists(fallback_local)) {
72 global_css->load_from_path(fallback_local);
73 used = fallback_local;
74 } else if (std::filesystem::exists(fallback_installed)) {
75 global_css->load_from_path(fallback_installed);
76 used = fallback_installed;
77 } else {
78 std::cerr << "⚠ Nessun file CSS trovato:\n"
79 << " " << local << "\n " << installed
80 << std::endl;
81 return;
82 }
83 }
84
85 Gtk::StyleContext::add_provider_for_display(
86 Gdk::Display::get_default(),
88 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
89
90 std::cout << _("[CSS] Tema '") << theme
91 << _("' caricato da: ") << used << std::endl;
92
93 } catch (const Glib::Error& ex) {
94 std::cerr << _("❌ Errore CSS: ") << ex.what() << std::endl;
95 }
96}
#define _(s)
#define DATA_DIR
Definition Resource.hpp:28
void load_style()
Glib::RefPtr< Gtk::CssProvider > global_css
static std::string read_saved_theme()