Skip to content
API Documentation
BastionGuard™
AppGtk.vala
Go to the documentation of this file.
1/*
2 * AppGtk.vala
3 *
4 * Copyright 2012-2018 Tony George <teejeetech@gmail.com>
5 * Copyright 2025-2026 Calogero Scarnà — BastionGuard Backup
6 *
7 * GPL-2.0-or-later
8 */
9
10using GLib;
11using Gtk;
12using Gee;
13using Json;
14
15using TeeJee.Logging;
20using TeeJee.System;
21using TeeJee.Misc;
22
23public Main App;
24public const string AppName = "BastionGuard Backup-gtk";
25public const string AppShortName = "bastionguard-backup";
26public const string AppVersion = Constants.VERSION;
27public const string AppAuthor = "BastionGuard™";
28public const string AppAuthorEmail = "info@bastionguard.eu";
29
30const string GETTEXT_PACKAGE = "bastionguard-backup";
31const string BASTIONGUARD_RECOVERY_FLAG = "bastionguard.recovery=1";
32const string LOCALE_DIR = "/usr/share/locale";
33
34extern void exit(int exit_code);
35
36public class AppGtk : GLib.Object {
37
38 public static int main (string[] args) {
39
40 set_locale();
41
42 // Rileva --recovery o parametro kernel bastionguard.recovery=1
43 bool recovery_mode = false;
44 foreach (var arg in args) {
45 if (arg == "--recovery") { recovery_mode = true; break; }
46 }
47 if (!recovery_mode && is_kernel_recovery_boot()) {
48 recovery_mode = true;
49 }
50
51 if (args.length > 1) {
52 switch (args[1].down()) {
53 case "--help":
54 case "--h":
55 case "-h":
56 stdout.printf(help_message());
57 return 0;
58 case "--version":
59 stdout.printf(version_message());
60 return 0;
61 }
62 }
63
65
66 Gtk.init();
67
68 GTK_INITIALIZED = true;
69
70 init_tmp();
71
73
74 App = new Main(args, true);
75
76 // In recovery mode: imposta app_mode così live_system() ritorna false
77 // e tutte le funzioni (Crea, Ripristina, Wizard) restano disponibili
78 if (recovery_mode) {
79 App.app_mode = "recovery";
80 }
81
82 parse_arguments(args);
84 start_application(recovery_mode);
85
86 App.exit_app();
87
88 return 0;
89 }
90
91 private static void set_locale() {
92 log_debug("setting locale...");
93 Intl.setlocale(GLib.LocaleCategory.MESSAGES, "bastionguard-backup");
94 Intl.textdomain(GETTEXT_PACKAGE);
95 Intl.bind_textdomain_codeset(GETTEXT_PACKAGE, "utf-8");
96 Intl.bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
97 }
98
99 public static bool parse_arguments(string[] args) {
100 for (int k = 1; k < args.length; k++) {
101 switch (args[k].down()) {
102 case "--debug":
103 LOG_DEBUG = true;
104 break;
105 case "--recovery":
106 // già gestito sopra
107 break;
108 default:
109 log_error(_("Unknown option") + ": %s".printf(args[k]));
111 App.exit_app(1);
112 return false;
113 }
114 }
115 return true;
116 }
117
118 private static string version_message() {
119 return "%s %s\n".printf(AppName, AppVersion);
120 }
121
122 public static string help_message() {
123 string msg = "\n%s v%s by BastionGuard (%s)\n".printf(AppName, AppVersion, AppAuthorEmail);
124 msg += "\n";
125 msg += _("Syntax") + ": bastionguard-backup-gtk [options]\n";
126 msg += "\n";
127 msg += _("Options") + ":\n";
128 msg += "\n";
129 msg += " --debug " + _("Print debug information") + "\n";
130 msg += " --recovery " + _("Start in recovery mode") + "\n";
131 msg += " --h[elp] " + _("Show all options") + "\n";
132 msg += " --version " + _("Print version number") + "\n";
133 msg += "\n\n";
134 msg += "\n%s\n".printf(_("Run 'bastionguard-backup' for the command-line version of this tool"));
135 return msg;
136 }
137
138 public static void check_if_admin() {
139 if (!user_is_admin()) {
140 var msg = _("Admin access is required to backup and restore system files.") + "\n";
141 msg += _("Please re-run the application as admin (using 'sudo' or 'su')");
142 log_error(msg);
143 string title = _("Admin Access Required");
144 gtk_messagebox(title, msg, null, true);
145 exit(1);
146 }
147 }
148
149 public static void start_application(bool recovery_mode = false) {
150
151 var window = new MainWindow();
152 var loop = new GLib.MainLoop(null, false);
153 window.close_request.connect(() => { loop.quit(); return false; });
154
155 // Mostra dialog recovery DOPO che la finestra è pronta
156 if (recovery_mode) {
157 window.present();
158 BastionGuardRecovery.show_recovery_banner(window);
159 } else {
160 // Controlla se c'è BastionGuardBackup da importare
162 window.present();
163 }
164
165 loop.run();
166 }
167
168 // Controlla bastionguard.recovery=1 in /proc/cmdline
169 private static bool is_kernel_recovery_boot() {
170 try {
171 string cmdline;
172 FileUtils.get_contents("/proc/cmdline", out cmdline);
173 return BASTIONGUARD_RECOVERY_FLAG in cmdline;
174 } catch {
175 return false;
176 }
177 }
178}
void exit(int exit_code)
Main App
const string GETTEXT_PACKAGE
Definition AppGtk.vala:30
const string BASTIONGUARD_RECOVERY_FLAG
Definition AppGtk.vala:31
const string LOCALE_DIR
Definition AppGtk.vala:32
void exit(int exit_code)
const string AppAuthor
Definition AppGtk.vala:27
const string AppShortName
Definition AppGtk.vala:25
Main App
Definition AppGtk.vala:23
const string AppName
Definition AppGtk.vala:24
const string AppVersion
Definition AppGtk.vala:26
const string AppAuthorEmail
Definition AppGtk.vala:28
#define _(s)
bool GTK_INITIALIZED
Definition Main.vala:37
static int main(string[] args)
Definition AppGtk.vala:38
static string help_message()
Definition AppGtk.vala:122
static bool is_kernel_recovery_boot()
Definition AppGtk.vala:169
static void set_locale()
Definition AppGtk.vala:91
static void check_if_admin()
Definition AppGtk.vala:138
static void start_application(bool recovery_mode=false)
Definition AppGtk.vala:149
static bool parse_arguments(string[] args)
Definition AppGtk.vala:99
static string version_message()
Definition AppGtk.vala:118
static void check_and_show_if_needed(Gtk.Window parent)
Definition Main.vala:39
Main(string[] args, bool gui_mode)
Definition Main.vala:186
static void setup_env()
Definition Main.vala:366
void initialize()
Definition Main.vala:337
void exit_app(int exit_code=0)
Definition Main.vala:4574
void log_debug(string message)
void log_error(string message, bool highlight=false, bool is_warning=false)
void log_msg(string message, bool highlight=false)