Skip to content
API Documentation
BastionGuard™
TeeJee.System Namespace Reference

Functions

bool user_is_admin ()
int get_user_id ()
int get_user_id_effective ()
string? get_username_from_uid (int user_id)
double get_system_uptime_seconds ()
static bool xdg_open (string file)
bool exo_open_folder (string dir_path, bool xdg_open_try_first=true)
bool using_efi_boot ()
GLib.Timer timer_start ()
ulong timer_elapsed (GLib.Timer timer, bool stop=true)
void sleep (int milliseconds)
string timer_elapsed_string (GLib.Timer timer, bool stop=true)
void set_numeric_locale (string type)

Variables

int euid = -1

Function Documentation

◆ exo_open_folder()

bool TeeJee.System.exo_open_folder ( string dir_path,
bool xdg_open_try_first = true )

Definition at line 100 of file TeeJee.System.vala.

100 {
101
102 /* Tries to open the given directory in a file manager */
103
104 /*
105 xdg-open is a desktop-independent tool for configuring the default applications of a user.
106 Inside a desktop environment (e.g. GNOME, KDE, Xfce), xdg-open simply passes the arguments
107 to that desktop environment's file-opener application (gvfs-open, kde-open, exo-open, respectively).
108 We will first try using xdg-open and then check for specific file managers if it fails.
109 */
110
111 bool xdgAvailable = cmd_exists("xdg-open");
112 string escaped_dir_path = escape_single_quote(dir_path);
113 int status = -1;
114
115 if (xdg_open_try_first && xdgAvailable){
116 //try using xdg-open
117 string cmd = "xdg-open '%s'".printf(escaped_dir_path);
118 status = exec_script_async (cmd);
119 return (status == 0);
120 }
121
122 foreach(string app_name in
123 new string[]{ "nemo", "nautilus", "thunar", "io.elementary.files", "pantheon-files", "marlin", "dolphin" }){
124 if(!cmd_exists(app_name)) {
125 continue;
126 }
127
128 string cmd = "%s '%s'".printf(app_name, escaped_dir_path);
129 status = exec_script_async (cmd);
130
131 if(status == 0) {
132 return true;
133 }
134 }
135
136 if (!xdg_open_try_first && xdgAvailable){
137 //try using xdg-open
138 string cmd = "xdg-open '%s'".printf(escaped_dir_path);
139 status = exec_script_async (cmd);
140 return (status == 0);
141 }
142
143 return false;
144 }
string escape_single_quote(string file_path)
int exec_script_async(string script)

References TeeJee.ProcessHelper.cmd_exists(), TeeJee.FileSystem.escape_single_quote(), TeeJee.ProcessHelper.exec_script_async(), and exo_open_folder().

Referenced by exo_open_folder().

◆ get_system_uptime_seconds()

double TeeJee.System.get_system_uptime_seconds ( )

Definition at line 79 of file TeeJee.System.vala.

79 {
80
81 /* Returns the system up-time in seconds */
82
83 string uptime = file_read("/proc/uptime").split(" ")[0];
84 double secs = double.parse(uptime);
85 return secs;
86 }
string? file_read(string file_path)

References TeeJee.FileSystem.file_read(), and get_system_uptime_seconds().

Referenced by Main.create_snapshot(), Main.create_snapshot_for_tag(), get_system_uptime_seconds(), and SnapshotRepo.load_snapshots().

◆ get_user_id()

int TeeJee.System.get_user_id ( )

Definition at line 39 of file TeeJee.System.vala.

39 {
40
41 // returns actual user id of current user (even for applications executed with sudo and pkexec)
42
43 string pkexec_uid = GLib.Environment.get_variable("PKEXEC_UID");
44
45 if (pkexec_uid != null){
46 return int.parse(pkexec_uid);
47 }
48
49 string sudo_user = GLib.Environment.get_variable("SUDO_UID");
50
51 if (sudo_user != null){
52 return int.parse(sudo_user);
53 }
54
55 return get_user_id_effective(); // normal user
56 }
int get_user_id_effective()

References get_user_id(), and get_user_id_effective().

Referenced by get_user_id().

◆ get_user_id_effective()

int TeeJee.System.get_user_id_effective ( )

Definition at line 59 of file TeeJee.System.vala.

59 {
60 // returns effective user id (0 for applications executed with sudo and pkexec)
61 if (euid < 0) {
62 euid = (int) Posix.geteuid();
63 }
64
65 return euid;
66 }

References euid, and get_user_id_effective().

Referenced by get_user_id(), get_user_id_effective(), and user_is_admin().

◆ get_username_from_uid()

string? TeeJee.System.get_username_from_uid ( int user_id)

Definition at line 68 of file TeeJee.System.vala.

68 {
69 unowned Posix.Passwd? pw = Posix.getpwuid(user_id);
70 string? outvalue = pw?.pw_name;
71 if(null == outvalue) {
72 log_error("Could not get username for uid %d".printf(user_id));
73 }
74 return outvalue;
75 }
void log_error(string message, bool highlight=false, bool is_warning=false)

References get_username_from_uid(), and TeeJee.Logging.log_error().

Referenced by get_username_from_uid().

◆ set_numeric_locale()

void TeeJee.System.set_numeric_locale ( string type)

Definition at line 186 of file TeeJee.System.vala.

186 {
187 Intl.setlocale(GLib.LocaleCategory.NUMERIC, type);
188 Intl.setlocale(GLib.LocaleCategory.COLLATE, type);
189 Intl.setlocale(GLib.LocaleCategory.TIME, type);
190 }

References TeeJee.Misc.set_numeric_locale().

◆ sleep()

◆ timer_elapsed()

ulong TeeJee.System.timer_elapsed ( GLib.Timer timer,
bool stop = true )

Definition at line 162 of file TeeJee.System.vala.

162 {
163 ulong microseconds;
164 double seconds;
165 seconds = timer.elapsed (out microseconds);
166 if (stop){
167 timer.stop();
168 }
169 return (ulong)((seconds * 1000 ) + (microseconds / 1000));
170 }

References timer_elapsed().

Referenced by timer_elapsed().

◆ timer_elapsed_string()

string TeeJee.System.timer_elapsed_string ( GLib.Timer timer,
bool stop = true )

Definition at line 176 of file TeeJee.System.vala.

176 {
177 ulong microseconds;
178 double seconds;
179 seconds = timer.elapsed (out microseconds);
180 if (stop){
181 timer.stop();
182 }
183 return "%.0f ms".printf((seconds * 1000 ) + microseconds/1000);
184 }

References timer_elapsed_string().

Referenced by timer_elapsed_string().

◆ timer_start()

GLib.Timer TeeJee.System.timer_start ( )

Definition at line 156 of file TeeJee.System.vala.

156 {
157 var timer = new GLib.Timer();
158 timer.start();
159 return timer;
160 }

References timer_start().

Referenced by timer_start().

◆ user_is_admin()

bool TeeJee.System.user_is_admin ( )

Definition at line 34 of file TeeJee.System.vala.

34 {
35
36 return (get_user_id_effective() == 0);
37 }

References get_user_id_effective(), and user_is_admin().

Referenced by AppConsole.check_if_admin(), AppGtk.check_if_admin(), and user_is_admin().

◆ using_efi_boot()

bool TeeJee.System.using_efi_boot ( )

Definition at line 146 of file TeeJee.System.vala.

146 {
147
148 /* Returns true if the system was booted in EFI mode
149 * and false for BIOS mode */
150
151 return dir_exists("/sys/firmware/efi");
152 }
bool dir_exists(string dir_path)

References TeeJee.FileSystem.dir_exists(), and using_efi_boot().

Referenced by using_efi_boot().

◆ xdg_open()

bool TeeJee.System.xdg_open ( string file)
static

Definition at line 90 of file TeeJee.System.vala.

90 {
91 if (!TeeJee.ProcessHelper.cmd_exists("xdg-open")) {
92 return false;
93 }
94
95 string cmd = "xdg-open '%s'".printf(escape_single_quote(file));
96
97 return exec_user_async(cmd) == 0;
98 }

References TeeJee.FileSystem.escape_single_quote(), TeeJee.ProcessHelper.exec_user_async(), and xdg_open().

Referenced by xdg_open().

Variable Documentation

◆ euid

int TeeJee.System.euid = -1
private

Definition at line 58 of file TeeJee.System.vala.

Referenced by get_user_id_effective().