Skip to content
API Documentation
BastionGuard™
StyleProvider.cpp
Go to the documentation of this file.
1/*
2 * BastionGuard™ Recovery Data
3 * Copyright (C) 2025–2026 Calogero Scarnà
4 *
5 * SPDX-License-Identifier: GPL-3.0-or-later
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include "StyleProvider.hpp"
19#include <gtkmm/stylecontext.h>
20#include <gdkmm/display.h>
21#include <glibmm/miscutils.h>
22#include <glibmm/i18n.h>
23#include <iostream>
24#include <fstream>
25#include <filesystem>
26#include "i18n.hpp"
27#ifndef DATA_DIR
28#define DATA_DIR "/usr/share/bastionguard-recovery-data/data"
29#endif
30
31Glib::RefPtr<Gtk::CssProvider> global_css;
32
33
34static std::string read_saved_theme() {
35 const std::string path =
36 Glib::get_home_dir() + "/.config/bastionguard-recovery-data/theme.conf";
37 std::ifstream f(path);
38 if (!f.is_open()) return "light";
39 std::string val;
40 f >> val;
41 return (val == "dark") ? "dark" : "light";
42}
43
45{
46 global_css = Gtk::CssProvider::create();
47
48 const std::string theme = read_saved_theme();
49 const std::string cssName = (theme == "dark")
50 ? "BastionGuard-dark.css"
51 : "BastionGuard.css";
52
53 const std::string local = "data/" + cssName;
54 const std::string installed = std::string(DATA_DIR) + "/" + cssName;
55
56 std::string used;
57
58 try {
59 if (std::filesystem::exists(local)) {
60 global_css->load_from_path(local);
61 used = local;
62 } else if (std::filesystem::exists(installed)) {
63 global_css->load_from_path(installed);
64 used = installed;
65 } else {
66 const std::string fallback_local = "data/BastionGuard.css";
67 const std::string fallback_installed = std::string(DATA_DIR) + "/BastionGuard.css";
68 if (std::filesystem::exists(fallback_local)) {
69 global_css->load_from_path(fallback_local);
70 used = fallback_local;
71 } else if (std::filesystem::exists(fallback_installed)) {
72 global_css->load_from_path(fallback_installed);
73 used = fallback_installed;
74 } else {
75 std::cerr << trs("⚠ No CSS file found:\n")
76 << " " << local << "\n " << installed
77 << std::endl;
78 return;
79 }
80 }
81
82 auto display = Gdk::Display::get_default();
83 if (display) {
84 gtk_style_context_add_provider_for_display(
85 display->gobj(),
86 GTK_STYLE_PROVIDER(global_css->gobj()),
87 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
88 );
89 }
90
91 std::cout << _("[CSS] Tema '") << theme
92 << _("' caricato da: ") << used << std::endl;
93
94 } catch (const Glib::Error& ex) {
95 std::cerr << _("❌ Errore CSS: ") << ex.what() << std::endl;
96 }
97}
#define _(s)
std::string trs(const char *msgid)
Definition i18n.cpp:22
#define DATA_DIR
Definition Resource.hpp:28
void load_style()
Glib::RefPtr< Gtk::CssProvider > global_css
static std::string read_saved_theme()