Add flatstore-users
[moonshot-ui.git] / src / moonshot-utils.vala
1 #if OS_WIN32
2 extern string? g_win32_get_package_installation_directory_of_module (void *module);
3 #endif
4
5 public Gdk.Pixbuf? find_icon_sized (string name, Gtk.IconSize icon_size)
6 {
7     int width, height;
8     Gtk.icon_size_lookup (icon_size, out width, out height);
9     return find_icon (name, width);
10 }
11
12 /* Portability hack: making Gtk icon themes work on Windows is
13  * difficult; let's just bundle the icons that are necessary and
14  * load them manually.
15  */
16
17 public bool gtk_available = false;
18
19 public Gdk.Pixbuf? get_pixbuf(IdCard id)
20 {
21     return find_icon("avatar-default", 48);
22 }
23
24 public Gdk.Pixbuf? find_icon (string name, int size)
25 {
26     if (!gtk_available)
27         return null;
28     try
29     {
30 #if OS_WIN32
31         string? base_path = g_win32_get_package_installation_directory_of_module (null);
32
33         // Hack to allow running within the source tree
34         int last_dir_index = base_path.last_index_of_char ('\\');
35         if (base_path.substring (last_dir_index) == "\\.libs" || base_path.substring (last_dir_index) == "src")
36             base_path = base_path.slice(0, last_dir_index);
37
38         string? filename = Path.build_filename (base_path, "share", "icons", "%s.png".printf (name));
39         return new Gdk.Pixbuf.from_file_at_size (filename, size, size);
40
41 #else
42         var icon_theme = Gtk.IconTheme.get_default ();
43         return icon_theme.load_icon (name, size, Gtk.IconLookupFlags.FORCE_SIZE);
44 #endif
45     }
46     catch (Error e)
47     {
48         stdout.printf("Error loading icon '%s': %s\n", name, e.message);
49         return null;
50     }
51 }
52
53 public extern unowned string GetUserName();
54 public extern unowned string GetFlatStoreUsersFilePath();
55
56 public bool UserForcesFlatFileStore()
57 {
58     string username = GetUserName();
59     string flatstore_users_filename = GetFlatStoreUsersFilePath();
60     FileStream flatstore_users = FileStream.open(flatstore_users_filename, "r");
61     if (flatstore_users == null) {
62         return false;
63     }
64     string? flatstore_username = null;
65     while ((flatstore_username = flatstore_users.read_line()) != null) {
66         if (username == flatstore_username) {
67             return true;
68         }
69     }
70     return false;
71 }