Skip to content
API Documentation
BastionGuard™
SettingsStore Class Reference

#include <SettingsStore.hpp>

Static Public Member Functions

static void init ()
static void reload ()
static bool flush ()
static bool reset_to_defaults ()
static bool get_secure_payments_enabled ()
static void set_secure_payments_enabled (bool enabled)
static std::string get_language ()
static void set_language (const std::string &lang)
static void apply_language ()
static std::string get_string (const std::string &dotted_key, const std::string &fallback_default)
static void set_string (const std::string &dotted_key, const std::string &value)
static void ensure_payments_json_exists ()
static std::vector< std::string > get_payment_domains ()
static bool save_payment_domains_user (const std::vector< std::string > &domains)
static bool reset_payment_domains_from_system ()

Static Private Member Functions

static std::string settings_file_path ()
static nlohmann::json defaults ()
static void ensure_loaded_locked ()
static bool load_from_disk_locked ()
static bool write_to_disk_locked (const nlohmann::json &j)
static nlohmann::json * get_or_create_node_locked (nlohmann::json &root, const std::string &dotted_key)
static const nlohmann::json * get_node_locked (const nlohmann::json &root, const std::string &dotted_key)
static bool get_bool (const std::string &dotted_key, bool fallback_default)
static void set_bool (const std::string &dotted_key, bool value)
static int get_int (const std::string &dotted_key, int fallback_default)
static void set_int (const std::string &dotted_key, int value)
static std::string user_payments_path ()
static std::string system_payments_path ()
static nlohmann::json default_payments_seed ()
static bool read_json_file (const std::string &path, nlohmann::json &out)
static bool write_json_atomic (const std::string &path, const nlohmann::json &j)

Static Private Attributes

static std::mutex mtx_
static bool loaded_ = false
static nlohmann::json cache_ = nlohmann::json::object()

Detailed Description

Definition at line 27 of file SettingsStore.hpp.

Member Function Documentation

◆ apply_language()

void SettingsStore::apply_language ( )
static

Definition at line 293 of file SettingsStore.cpp.

293 {
294 // ── Fonte 1: settings.json ui.language ───────────────────────────────────
295 const std::string from_settings = get_language();
296
297 // ── Fonte 2: lang.conf ────────────────────────────────────────────────────
298 const std::string lang_conf =
299 Glib::get_home_dir() + "/.config/BastionGuard/lang.conf";
300
301 // Leggi la lingua da lang.conf (es. "it_IT" da LANG=it_IT.UTF-8)
302 std::string from_lang_conf;
303 {
304 std::ifstream f(lang_conf);
305 std::string line;
306 while (std::getline(f, line)) {
307 // Cerca LANG= o LANGUAGE= per estrarre il codice lingua
308 if (line.rfind("LANGUAGE=", 0) == 0) {
309 from_lang_conf = line.substr(9);
310 // Prendi solo la prima voce (es. "it_IT:it" → "it_IT")
311 const auto colon = from_lang_conf.find(':');
312 if (colon != std::string::npos)
313 from_lang_conf = from_lang_conf.substr(0, colon);
314 break;
315 }
316 }
317 }
318
319 // Determina la lingua effettiva da usare:
320 // Se settings.json è vuoto o è ancora il default "en_US" E lang.conf
321 // ha qualcosa di diverso → lang.conf vince (migrazione automatica).
322 std::string lang;
323 const bool settings_is_default =
324 from_settings.empty() || from_settings == "en_US";
325
326 if (settings_is_default && !from_lang_conf.empty()
327 && from_lang_conf != "en_US") {
328 lang = from_lang_conf;
329 // Scrivi in settings.json così i prossimi avvii usano direttamente quello
330 set_language(lang);
331 std::cerr << "[i18n] migrated language from lang.conf to settings.json: "
332 << lang << "\n";
333 } else if (!from_settings.empty()) {
334 lang = from_settings;
335 std::cerr << "[i18n] language from settings.json: " << lang << "\n";
336 } else if (!from_lang_conf.empty()) {
337 lang = from_lang_conf;
338 std::cerr << "[i18n] language from lang.conf: " << lang << "\n";
339 } else {
340 std::cerr << "[i18n] no language configured, using system locale\n";
341 setlocale(LC_ALL, "");
342 return;
343 }
344
345 setenv("LANGUAGE", lang.c_str(), 1);
346 setenv("LANG", (lang + ".UTF-8").c_str(), 1);
347 setenv("LC_ALL", (lang + ".UTF-8").c_str(), 1);
348 if (!setlocale(LC_ALL, (lang + ".UTF-8").c_str()))
349 if (!setlocale(LC_ALL, lang.c_str()))
350 setlocale(LC_ALL, "");
351}
static void set_language(const std::string &lang)
static std::string get_language()

References get_language(), and set_language().

Referenced by main().

◆ default_payments_seed()

nlohmann::json SettingsStore::default_payments_seed ( )
staticprivate

Definition at line 374 of file SettingsStore.cpp.

374 {
375 nlohmann::json j;
376 j["version"] = 1;
377 j["updated_at"] = "2026-01-13";
378 j["list"] = nlohmann::json::array({
379 "paypal.com",
380 "stripe.com",
381 "adyen.com",
382 "klarna.com",
383 "checkout.com",
384 "worldpay.com",
385 "braintreepayments.com",
386 "braintreegateway.com",
387 "amazonpay.com",
388 "pay.google.com",
389 "pay.apple.com",
390 "sisalpay.it",
391 "nexi.it",
392 "payments.amazon.com"
393 });
394 return j;
395}

Referenced by ensure_payments_json_exists().

◆ defaults()

nlohmann::json SettingsStore::defaults ( )
staticprivate

Definition at line 45 of file SettingsStore.cpp.

45 {
46 nlohmann::json j;
47 j["security"] = nlohmann::json::object();
48 j["security"]["secure_payments_enabled"] = false;
49
50 j["ui"] = nlohmann::json::object();
51 j["ui"]["language"] = ""; // vuoto = leggi da lang.conf
52
53 return j;
54}

Referenced by ensure_loaded_locked(), and reset_to_defaults().

◆ ensure_loaded_locked()

void SettingsStore::ensure_loaded_locked ( )
staticprivate

Definition at line 75 of file SettingsStore.cpp.

75 {
76 if (loaded_) return;
77
78 cache_ = defaults();
79
81
82 loaded_ = true;
83}
static nlohmann::json cache_
static nlohmann::json defaults()
static bool loaded_
static bool load_from_disk_locked()

References cache_, defaults(), load_from_disk_locked(), and loaded_.

Referenced by flush(), get_bool(), get_int(), get_string(), init(), reload(), set_bool(), set_int(), and set_string().

◆ ensure_payments_json_exists()

void SettingsStore::ensure_payments_json_exists ( )
static

Definition at line 439 of file SettingsStore.cpp.

439 {
440 std::lock_guard<std::mutex> lk(mtx_);
441
442 const auto up = user_payments_path();
443 const auto sp = system_payments_path();
444
445 if (fs::exists(up)) return;
446 if (fs::exists(sp)) {
447 try {
448 fs::create_directories(fs::path(up).parent_path());
449 std::ifstream in(sp, std::ios::binary);
450 std::ofstream out(up, std::ios::binary | std::ios::trunc);
451 if (in.good() && out.good()) {
452 out << in.rdbuf();
453 return;
454 }
455 } catch (...) {
456 }
457 }
458
460}
static std::string user_payments_path()
static nlohmann::json default_payments_seed()
static std::mutex mtx_
static std::string system_payments_path()
static bool write_json_atomic(const std::string &path, const nlohmann::json &j)

References default_payments_seed(), mtx_, system_payments_path(), user_payments_path(), and write_json_atomic().

Referenced by get_payment_domains().

◆ flush()

bool SettingsStore::flush ( )
static

Definition at line 139 of file SettingsStore.cpp.

139 {
140 std::lock_guard<std::mutex> lk(mtx_);
143}
static void ensure_loaded_locked()
static bool write_to_disk_locked(const nlohmann::json &j)

References cache_, ensure_loaded_locked(), mtx_, and write_to_disk_locked().

◆ get_bool()

bool SettingsStore::get_bool ( const std::string & dotted_key,
bool fallback_default )
staticprivate

Definition at line 187 of file SettingsStore.cpp.

187 {
188 std::lock_guard<std::mutex> lk(mtx_);
190
191 const auto* n = get_node_locked(cache_, dotted_key);
192 if (!n) return fallback_default;
193 if (n->is_boolean()) return n->get<bool>();
194 return fallback_default;
195}
static const nlohmann::json * get_node_locked(const nlohmann::json &root, const std::string &dotted_key)

References cache_, ensure_loaded_locked(), get_node_locked(), and mtx_.

Referenced by get_secure_payments_enabled().

◆ get_int()

int SettingsStore::get_int ( const std::string & dotted_key,
int fallback_default )
staticprivate

Definition at line 211 of file SettingsStore.cpp.

211 {
212 std::lock_guard<std::mutex> lk(mtx_);
214
215 const auto* n = get_node_locked(cache_, dotted_key);
216 if (!n) return fallback_default;
217 if (n->is_number_integer()) return n->get<int>();
218 if (n->is_number()) return static_cast<int>(n->get<double>());
219 return fallback_default;
220}

References cache_, ensure_loaded_locked(), get_node_locked(), and mtx_.

◆ get_language()

std::string SettingsStore::get_language ( )
static

Definition at line 270 of file SettingsStore.cpp.

270 {
271 return get_string("ui.language", "");
272}
static std::string get_string(const std::string &dotted_key, const std::string &fallback_default)

References get_string().

Referenced by apply_language().

◆ get_node_locked()

const nlohmann::json * SettingsStore::get_node_locked ( const nlohmann::json & root,
const std::string & dotted_key )
staticprivate

Definition at line 175 of file SettingsStore.cpp.

175 {
176 auto parts = split_dotted(dotted_key);
177 if (parts.empty()) return nullptr;
178
179 const nlohmann::json* node = &root;
180 for (const auto& k : parts) {
181 if (!node->is_object() || !node->contains(k)) return nullptr;
182 node = &(*node)[k];
183 }
184 return node;
185}
static std::vector< std::string > split_dotted(const std::string &dotted_key)

References split_dotted().

Referenced by get_bool(), get_int(), and get_string().

◆ get_or_create_node_locked()

nlohmann::json * SettingsStore::get_or_create_node_locked ( nlohmann::json & root,
const std::string & dotted_key )
staticprivate

Definition at line 160 of file SettingsStore.cpp.

160 {
161 auto parts = split_dotted(dotted_key);
162 if (parts.empty()) return nullptr;
163
164 nlohmann::json* node = &root;
165 for (size_t i = 0; i + 1 < parts.size(); ++i) {
166 const auto& k = parts[i];
167 if (!node->contains(k) || !(*node)[k].is_object()) {
168 (*node)[k] = nlohmann::json::object();
169 }
170 node = &(*node)[k];
171 }
172 return node;
173}

References split_dotted().

Referenced by set_bool(), set_int(), and set_string().

◆ get_payment_domains()

std::vector< std::string > SettingsStore::get_payment_domains ( )
static

Definition at line 480 of file SettingsStore.cpp.

480 {
482
483 std::lock_guard<std::mutex> lk(mtx_);
484
485 std::vector<std::string> tmp;
486 nlohmann::json jsys, jusr;
489
490 // dedup + sort
491 std::unordered_set<std::string> uniq;
492 std::vector<std::string> out;
493 out.reserve(tmp.size());
494
495 for (auto& d : tmp) {
496 d = normalize_domain(d);
497 if (d.empty()) continue;
498 if (uniq.insert(d).second) out.push_back(d);
499 }
500
501 std::sort(out.begin(), out.end());
502 return out;
503}
static std::string normalize_domain(std::string s)
static void extract_domains_from_payments_json(const nlohmann::json &j, std::vector< std::string > &out)
static void ensure_payments_json_exists()
static bool read_json_file(const std::string &path, nlohmann::json &out)

References ensure_payments_json_exists(), extract_domains_from_payments_json(), mtx_, normalize_domain(), read_json_file(), system_payments_path(), and user_payments_path().

◆ get_secure_payments_enabled()

bool SettingsStore::get_secure_payments_enabled ( )
static

Definition at line 261 of file SettingsStore.cpp.

261 {
262 // usa default ufficiale (false)
263 return get_bool("security.secure_payments_enabled", false);
264}
static bool get_bool(const std::string &dotted_key, bool fallback_default)

References get_bool().

Referenced by SettingsPage::build_secure_payments_tab(), BankOpener::is_payment_domain(), is_payment_domain(), and SecureList::is_payment_domain().

◆ get_string()

std::string SettingsStore::get_string ( const std::string & dotted_key,
const std::string & fallback_default )
static

Definition at line 236 of file SettingsStore.cpp.

236 {
237 std::lock_guard<std::mutex> lk(mtx_);
239
240 const auto* n = get_node_locked(cache_, dotted_key);
241 if (!n) return fallback_default;
242 if (n->is_string()) return n->get<std::string>();
243 return fallback_default;
244}

References cache_, ensure_loaded_locked(), get_node_locked(), and mtx_.

Referenced by get_language().

◆ init()

void SettingsStore::init ( )
static

Definition at line 56 of file SettingsStore.cpp.

56 {
57 std::lock_guard<std::mutex> lk(mtx_);
59}

References ensure_loaded_locked(), and mtx_.

◆ load_from_disk_locked()

bool SettingsStore::load_from_disk_locked ( )
staticprivate

Definition at line 85 of file SettingsStore.cpp.

85 {
86 const auto path = settings_file_path();
87
88 if (!fs::exists(path)) {
89 return true;
90 }
91
92 try {
93 std::ifstream in(path, std::ios::binary);
94 if (!in.good()) return false;
95
96 nlohmann::json disk;
97 in >> disk;
98 cache_.merge_patch(disk);
99
100 return true;
101 } catch (...) {
102 return false;
103 }
104}
static std::string settings_file_path()

References cache_, and settings_file_path().

Referenced by ensure_loaded_locked().

◆ read_json_file()

bool SettingsStore::read_json_file ( const std::string & path,
nlohmann::json & out )
staticprivate

Definition at line 397 of file SettingsStore.cpp.

397 {
398 try {
399 if (!fs::exists(path)) return false;
400 std::ifstream in(path, std::ios::binary);
401 if (!in.good()) return false;
402 in >> out;
403 return true;
404 } catch (...) {
405 return false;
406 }
407}

Referenced by get_payment_domains().

◆ reload()

void SettingsStore::reload ( )
static

Definition at line 61 of file SettingsStore.cpp.

61 {
62 std::lock_guard<std::mutex> lk(mtx_);
63 loaded_ = false;
64 cache_.clear();
66}

References cache_, ensure_loaded_locked(), loaded_, and mtx_.

◆ reset_payment_domains_from_system()

bool SettingsStore::reset_payment_domains_from_system ( )
static

Definition at line 523 of file SettingsStore.cpp.

523 {
524 std::lock_guard<std::mutex> lk(mtx_);
525
526 const auto sp = system_payments_path();
527 const auto up = user_payments_path();
528
529 if (!fs::exists(sp)) return false;
530
531 try {
532 fs::create_directories(fs::path(up).parent_path());
533 std::ifstream in(sp, std::ios::binary);
534 std::ofstream out(up, std::ios::binary | std::ios::trunc);
535 if (!in.good() || !out.good()) return false;
536 out << in.rdbuf();
537 return true;
538 } catch (...) {
539 return false;
540 }
541}

References mtx_, system_payments_path(), and user_payments_path().

◆ reset_to_defaults()

bool SettingsStore::reset_to_defaults ( )
static

Definition at line 68 of file SettingsStore.cpp.

68 {
69 std::lock_guard<std::mutex> lk(mtx_);
70 cache_ = defaults();
71 loaded_ = true;
73}

References cache_, defaults(), loaded_, mtx_, and write_to_disk_locked().

◆ save_payment_domains_user()

bool SettingsStore::save_payment_domains_user ( const std::vector< std::string > & domains)
static

Definition at line 505 of file SettingsStore.cpp.

505 {
506 std::lock_guard<std::mutex> lk(mtx_);
507
508 nlohmann::json j;
509 j["version"] = 1;
510 j["updated_at"] = "2026-01-13";
511 j["list"] = nlohmann::json::array();
512
513 std::unordered_set<std::string> uniq;
514 for (auto d : domains) {
515 d = normalize_domain(d);
516 if (d.empty()) continue;
517 if (uniq.insert(d).second) j["list"].push_back(d);
518 }
519
521}

References mtx_, normalize_domain(), user_payments_path(), and write_json_atomic().

◆ set_bool()

void SettingsStore::set_bool ( const std::string & dotted_key,
bool value )
staticprivate

Definition at line 197 of file SettingsStore.cpp.

197 {
198 std::lock_guard<std::mutex> lk(mtx_);
200
201 auto parts = split_dotted(dotted_key);
202 if (parts.empty()) return;
203
204 nlohmann::json* parent = get_or_create_node_locked(cache_, dotted_key);
205 if (!parent) return;
206
207 (*parent)[parts.back()] = value;
209}
static nlohmann::json * get_or_create_node_locked(nlohmann::json &root, const std::string &dotted_key)

References cache_, ensure_loaded_locked(), get_or_create_node_locked(), mtx_, split_dotted(), and write_to_disk_locked().

Referenced by set_secure_payments_enabled().

◆ set_int()

void SettingsStore::set_int ( const std::string & dotted_key,
int value )
staticprivate

Definition at line 222 of file SettingsStore.cpp.

222 {
223 std::lock_guard<std::mutex> lk(mtx_);
225
226 auto parts = split_dotted(dotted_key);
227 if (parts.empty()) return;
228
229 nlohmann::json* parent = get_or_create_node_locked(cache_, dotted_key);
230 if (!parent) return;
231
232 (*parent)[parts.back()] = value;
234}

References cache_, ensure_loaded_locked(), get_or_create_node_locked(), mtx_, split_dotted(), and write_to_disk_locked().

◆ set_language()

void SettingsStore::set_language ( const std::string & lang)
static

Definition at line 274 of file SettingsStore.cpp.

274 {
275 set_string("ui.language", lang);
276
277 // Mantieni lang.conf sincronizzato — usato dai binari che girano
278 // prima che SettingsStore sia inizializzato (es. script di sistema).
279 const std::string lang_conf =
280 Glib::get_home_dir() + "/.config/BastionGuard/lang.conf";
281 try {
282 std::filesystem::create_directories(
283 std::filesystem::path(lang_conf).parent_path());
284 std::ofstream f(lang_conf, std::ios::trunc);
285 if (f.is_open()) {
286 f << "LANG=" << lang << ".UTF-8\n"
287 << "LC_ALL=" << lang << ".UTF-8\n"
288 << "LANGUAGE=" << lang << ":" << lang.substr(0, 2) << "\n";
289 }
290 } catch (...) {}
291}
static void set_string(const std::string &dotted_key, const std::string &value)

References set_string().

Referenced by apply_language().

◆ set_secure_payments_enabled()

void SettingsStore::set_secure_payments_enabled ( bool enabled)
static

Definition at line 266 of file SettingsStore.cpp.

266 {
267 set_bool("security.secure_payments_enabled", enabled);
268}
static void set_bool(const std::string &dotted_key, bool value)

References set_bool().

Referenced by SettingsPage::build_secure_payments_tab().

◆ set_string()

void SettingsStore::set_string ( const std::string & dotted_key,
const std::string & value )
static

Definition at line 246 of file SettingsStore.cpp.

246 {
247 std::lock_guard<std::mutex> lk(mtx_);
249
250 auto parts = split_dotted(dotted_key);
251 if (parts.empty()) return;
252
253 nlohmann::json* parent = get_or_create_node_locked(cache_, dotted_key);
254 if (!parent) return;
255
256 (*parent)[parts.back()] = value;
258}

References cache_, ensure_loaded_locked(), get_or_create_node_locked(), mtx_, split_dotted(), and write_to_disk_locked().

Referenced by set_language().

◆ settings_file_path()

std::string SettingsStore::settings_file_path ( )
staticprivate

Definition at line 41 of file SettingsStore.cpp.

41 {
42 return config_dir_path() + "/settings.json";
43}
static std::string config_dir_path()

References config_dir_path().

Referenced by load_from_disk_locked(), and write_to_disk_locked().

◆ system_payments_path()

std::string SettingsStore::system_payments_path ( )
staticprivate

Definition at line 357 of file SettingsStore.cpp.

357 {
358 return "/usr/share/BastionGuard/data/payments/payments.json";
359}

Referenced by ensure_payments_json_exists(), get_payment_domains(), and reset_payment_domains_from_system().

◆ user_payments_path()

std::string SettingsStore::user_payments_path ( )
staticprivate

Definition at line 353 of file SettingsStore.cpp.

353 {
354 return Glib::get_home_dir() + "/.config/BastionGuard/payments.json";
355}

Referenced by ensure_payments_json_exists(), get_payment_domains(), reset_payment_domains_from_system(), and save_payment_domains_user().

◆ write_json_atomic()

bool SettingsStore::write_json_atomic ( const std::string & path,
const nlohmann::json & j )
staticprivate

Definition at line 409 of file SettingsStore.cpp.

409 {
410 try {
411 fs::create_directories(fs::path(path).parent_path());
412 const auto tmp = path + ".tmp";
413
414 {
415 std::ofstream out(tmp, std::ios::binary | std::ios::trunc);
416 if (!out.good()) return false;
417 out << j.dump(2);
418 out.flush();
419 if (!out.good()) return false;
420 }
421
422 std::error_code ec;
423 fs::rename(tmp, path, ec);
424 if (ec) {
425 fs::remove(path, ec);
426 ec.clear();
427 fs::rename(tmp, path, ec);
428 if (ec) {
429 fs::remove(tmp, ec);
430 return false;
431 }
432 }
433 return true;
434 } catch (...) {
435 return false;
436 }
437}

Referenced by ensure_payments_json_exists(), and save_payment_domains_user().

◆ write_to_disk_locked()

bool SettingsStore::write_to_disk_locked ( const nlohmann::json & j)
staticprivate

Definition at line 106 of file SettingsStore.cpp.

106 {
107 const auto dir = config_dir_path();
108 const auto path = settings_file_path();
109 const auto tmp = path + ".tmp";
110
111 try {
112 fs::create_directories(dir);
113 {
114 std::ofstream out(tmp, std::ios::binary | std::ios::trunc);
115 if (!out.good()) return false;
116 out << j.dump(2);
117 out.flush();
118 if (!out.good()) return false;
119 }
120
121 std::error_code ec;
122 fs::rename(tmp, path, ec);
123 if (ec) {
124 fs::remove(path, ec);
125 ec.clear();
126 fs::rename(tmp, path, ec);
127 if (ec) {
128 fs::remove(tmp, ec);
129 return false;
130 }
131 }
132
133 return true;
134 } catch (...) {
135 return false;
136 }
137}

References config_dir_path(), and settings_file_path().

Referenced by flush(), reset_to_defaults(), set_bool(), set_int(), and set_string().

Member Data Documentation

◆ cache_

nlohmann::json SettingsStore::cache_ = nlohmann::json::object()
staticprivate

◆ loaded_

bool SettingsStore::loaded_ = false
staticprivate

Definition at line 75 of file SettingsStore.hpp.

Referenced by ensure_loaded_locked(), reload(), and reset_to_defaults().

◆ mtx_


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