-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Utils.vala
86 lines (73 loc) · 2.69 KB
/
Utils.vala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* Copyright 2018 elementary, Inc. (https://elementary.io)
*
* This program is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see http://www.gnu.org/licenses/.
*/
namespace Utils {
public void shuffle_array<T> (T[] array) {
// https://gitlab.gnome.org/GNOME/vala/issues/7
var rand = new GLib.Rand ();
for (int i = 0; i < array.length * 3; i++) {
swap (&array[0], &array[rand.int_range (1, array.length)]);
}
}
public void swap<T> (T *x, T *y) {
var tmp = *x;
*x = *y;
*y = tmp;
}
public static uint get_file_age (GLib.File file) {
FileInfo info;
try {
info = file.query_info (FileAttribute.TIME_MODIFIED, FileQueryInfoFlags.NONE);
} catch (Error e) {
warning ("Error while getting file age: %s", e.message);
return uint.MAX;
}
if (info == null) {
return uint.MAX;
}
uint64 mtime = info.get_attribute_uint64 (FileAttribute.TIME_MODIFIED);
uint64 now = (uint64) time_t ();
if (mtime > now) {
return uint.MAX;
}
if (now - mtime > uint.MAX) {
return uint.MAX;
}
return (uint) (now - mtime);
}
public static string unescape_markup (string escaped_text) {
return escaped_text.replace ("&", "&")
.replace ("<", "<")
.replace (">", ">")
.replace ("'", "'");
}
public static bool is_running_in_demo_mode () {
var proc_cmdline = File.new_for_path ("/proc/cmdline");
try {
var @is = proc_cmdline.read ();
var dis = new DataInputStream (@is);
var line = dis.read_line ();
if ("boot=casper" in line || "boot=live" in line || "rd.live.image" in line) {
return true;
}
} catch (Error e) {
critical ("Couldn't detect if running in Demo Mode: %s", e.message);
}
return false;
}
public static bool is_running_in_guest_session () {
return Environment.get_user_name ().has_prefix ("guest-");
}
}