Skip to content
API Documentation
BastionGuard™
first-run-main.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 <gtkmm.h>
22#include <glibmm/i18n.h>
23#include <clocale>
24#include <fstream>
26#include "StyleProvider.hpp"
27#include "Resource.hpp"
28
29#ifndef LOCALEDIR
30#error "LOCALEDIR non definita per questo target"
31#endif
32
33namespace fs = std::filesystem;
34
35static bool load_lang_conf()
36{
37 const std::string lang_conf = Glib::get_home_dir() + "/.config/BastionGuard/lang.conf";
38 std::ifstream f(lang_conf);
39 if (!f.is_open())
40 return false;
41
42 std::string line;
43 bool any = false;
44
45 while (std::getline(f, line)) {
46 if (line.empty() || line[0] == '#')
47 continue;
48
49 auto pos = line.find('=');
50 if (pos == std::string::npos)
51 continue;
52
53 std::string key = line.substr(0, pos);
54 std::string val = line.substr(pos + 1);
55
56 auto ltrim = [](std::string& s){
57 s.erase(0, s.find_first_not_of(" \t\r\n"));
58 };
59 auto rtrim = [](std::string& s){
60 s.erase(s.find_last_not_of(" \t\r\n") + 1);
61 };
62 ltrim(key); rtrim(key);
63 ltrim(val); rtrim(val);
64
65 if (!key.empty() && !val.empty()) {
66 setenv(key.c_str(), val.c_str(), 1);
67 any = true;
68 }
69 }
70
71 return any;
72}
73
74int main(int argc, char* argv[])
75{
76
77 const std::string lang_conf = Glib::get_home_dir() + "/.config/BastionGuard/lang.conf";
78 {
79 std::error_code ec;
80 if (!fs::exists(lang_conf, ec) || ec) {
81 std::string cfgdir = Glib::get_home_dir() + "/.config/BastionGuard";
82 fs::create_directories(cfgdir, ec);
83 std::ofstream out(lang_conf, std::ios::out | std::ios::trunc);
84 if (out.is_open()) {
85 out << "LANG=en_US.UTF-8\n";
86 out << "LC_ALL=en_US.UTF-8\n";
87 out << "LANGUAGE=en_US\n";
88 out.close();
89 std::cout << "[Lang] ✅ Creato lang.conf di default: " << lang_conf << std::endl;
90 }
91 }
92 }
93
94 std::ifstream f(lang_conf);
95 if (f.is_open()) {
96 std::string line;
97 while (std::getline(f, line)) {
98 auto pos = line.find('=');
99 if (pos != std::string::npos) {
100 std::string key = line.substr(0, pos);
101 std::string val = line.substr(pos + 1);
102 if (!key.empty() && !val.empty())
103 setenv(key.c_str(), val.c_str(), 1);
104 }
105 }
106 f.close();
107 std::cout << "[Lang] Config caricata da " << lang_conf << std::endl;
108 } else {
109 std::cerr << "[Lang] ⚠️ Impossibile aprire lang.conf, forzo inglese di default" << std::endl;
110 }
111
112
113 const char* lang_env = std::getenv("LANG");
114 if (!lang_env || std::string(lang_env).empty())
115 setenv("LANG", "en_US.UTF-8", 1);
116
117 const char* lc_all_env = std::getenv("LC_ALL");
118 if (!lc_all_env || std::string(lc_all_env).empty())
119 setenv("LC_ALL", "en_US.UTF-8", 1);
120
121 const char* language_env = std::getenv("LANGUAGE");
122 if (!language_env || std::string(language_env).empty())
123 setenv("LANGUAGE", "en_US", 1);
124
125
126 const char* applied = setlocale(LC_ALL, "");
127 if (!applied) applied = setlocale(LC_ALL, "en_US.UTF-8");
128 if (!applied) applied = setlocale(LC_ALL, "en_US");
129 if (!applied) applied = setlocale(LC_ALL, "C.UTF-8");
130 if (!applied) applied = setlocale(LC_ALL, "C");
131
132 bindtextdomain("BastionGuard", LOCALEDIR);
133 bind_textdomain_codeset("BastionGuard", "UTF-8");
134 textdomain("BastionGuard");
135
136 auto app = Gtk::Application::create("org.bastionguard.first-run-test");
137 load_style();
138 return app->make_window_and_run<FirstRunServicesWindow>(argc, argv);
139}
int main(int argc, char *argv[])
static bool load_lang_conf()
#define LOCALEDIR
void load_style()