Add copyright to source code
[moonshot-ui.git] / src / moonshot-utils.vala
1 /*
2  * Copyright (c) 2011-2014, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31 */
32 #if OS_WIN32
33 extern string? g_win32_get_package_installation_directory_of_module (void *module);
34 #endif
35
36 public Gdk.Pixbuf? find_icon_sized (string name, Gtk.IconSize icon_size)
37 {
38     int width, height;
39     Gtk.icon_size_lookup (icon_size, out width, out height);
40     return find_icon (name, width);
41 }
42
43 /* Portability hack: making Gtk icon themes work on Windows is
44  * difficult; let's just bundle the icons that are necessary and
45  * load them manually.
46  */
47
48 public bool gtk_available = false;
49
50 public Gdk.Pixbuf? get_pixbuf(IdCard id)
51 {
52     return find_icon("avatar-default", 48);
53 }
54
55 public Gdk.Pixbuf? find_icon (string name, int size)
56 {
57     if (!gtk_available)
58         return null;
59     try
60     {
61 #if OS_WIN32
62         string? base_path = g_win32_get_package_installation_directory_of_module (null);
63
64         // Hack to allow running within the source tree
65         int last_dir_index = base_path.last_index_of_char ('\\');
66         if (base_path.substring (last_dir_index) == "\\.libs" || base_path.substring (last_dir_index) == "src")
67             base_path = base_path.slice(0, last_dir_index);
68
69         string? filename = Path.build_filename (base_path, "share", "icons", "%s.png".printf (name));
70         return new Gdk.Pixbuf.from_file_at_size (filename, size, size);
71
72 #else
73         var icon_theme = Gtk.IconTheme.get_default ();
74         return icon_theme.load_icon (name, size, Gtk.IconLookupFlags.FORCE_SIZE);
75 #endif
76     }
77     catch (Error e)
78     {
79         stdout.printf("Error loading icon '%s': %s\n", name, e.message);
80         return null;
81     }
82 }
83
84 public extern unowned string GetUserName();
85 public extern unowned string GetFlatStoreUsersFilePath();
86
87 public bool UserForcesFlatFileStore()
88 {
89     string username = GetUserName();
90     string flatstore_users_filename = GetFlatStoreUsersFilePath();
91     FileStream flatstore_users = FileStream.open(flatstore_users_filename, "r");
92     if (flatstore_users == null) {
93         return false;
94     }
95     string? flatstore_username = null;
96     while ((flatstore_username = flatstore_users.read_line()) != null) {
97         if (username == flatstore_username) {
98             return true;
99         }
100     }
101     return false;
102 }