Squashed merge of many commits, including (but not limited to) :
[moonshot-ui.git] / src / moonshot-utils.vala
1 /*
2  * Copyright (c) 2011-2016, 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
33 using Gtk;
34 using Pango;
35
36 #if OS_WIN32
37 extern string? g_win32_get_package_installation_directory_of_module(void *module);
38 #endif
39
40 public Gdk.Pixbuf? find_icon_sized(string name, Gtk.IconSize icon_size)
41 {
42     int width, height;
43     Gtk.icon_size_lookup(icon_size, out width, out height);
44     return find_icon(name, width);
45 }
46
47 /* Portability hack: making Gtk icon themes work on Windows is
48  * difficult; let's just bundle the icons that are necessary and
49  * load them manually.
50  */
51
52 public bool gtk_available = false;
53
54 public Gdk.Pixbuf? get_pixbuf(IdCard id)
55 {
56     return find_icon("avatar-default", 48);
57 }
58
59 public Gdk.Pixbuf? find_icon(string name, int size)
60 {
61     if (!gtk_available)
62         return null;
63     try
64     {
65         #if OS_WIN32
66         string? base_path = g_win32_get_package_installation_directory_of_module(null);
67
68         // Hack to allow running within the source tree
69         int last_dir_index = base_path.last_index_of_char('\\');
70         if (base_path.substring(last_dir_index) == "\\.libs" || base_path.substring(last_dir_index) == "src")
71             base_path = base_path.slice(0, last_dir_index);
72
73         string? filename = Path.build_filename(base_path, "share", "icons", "%s.png".printf(name));
74         return new Gdk.Pixbuf.from_file_at_size(filename, size, size);
75
76         #else
77         var icon_theme = Gtk.IconTheme.get_default();
78         return icon_theme.load_icon(name, size, Gtk.IconLookupFlags.FORCE_SIZE);
79         #endif
80     }
81     catch (Error e)
82     {
83         stdout.printf("Error loading icon '%s': %s\n", name, e.message);
84         return null;
85     }
86 }
87
88 public extern unowned string GetUserName();
89 public extern unowned string GetFlatStoreUsersFilePath();
90
91 public bool UserForcesFlatFileStore()
92 {
93     string username = GetUserName();
94     string flatstore_users_filename = GetFlatStoreUsersFilePath();
95     FileStream flatstore_users = FileStream.open(flatstore_users_filename, "r");
96     if (flatstore_users == null) {
97         return false;
98     }
99     string? flatstore_username = null;
100     while ((flatstore_username = flatstore_users.read_line()) != null) {
101         if (username == flatstore_username) {
102             return true;
103         }
104     }
105     return false;
106 }
107
108 internal Gdk.Color make_color(uint16 red, uint16 green, uint16 blue)
109 {
110     Gdk.Color color = Gdk.Color();
111     color.red = red;
112     color.green = green;
113     color.blue = blue;
114
115     return color;
116 }
117
118 internal void set_atk_relation(Widget widget, Widget target_widget, Atk.RelationType relationship)
119 {
120     var atk_widget = widget.get_accessible();
121     var atk_target_widget = target_widget.get_accessible();
122
123     atk_widget.add_relationship(relationship, atk_target_widget);
124 }
125
126
127 internal Widget make_ta_fingerprint_widget(string server_cert)
128 {
129         var fingerprint_label = new Label(_("SHA-256 fingerprint:"));
130         fingerprint_label.set_alignment(0, 0.5f);
131
132         var fingerprint = new TextView();
133         var fontdesc = FontDescription.from_string("monospace 10");
134         fingerprint.modify_font(fontdesc);
135         fingerprint.set_editable(false);
136         fingerprint.set_left_margin(3);
137         var buffer = fingerprint.get_buffer();
138         buffer.set_text(colonize(server_cert, 16), -1);
139         fingerprint.wrap_mode = Gtk.WrapMode.WORD_CHAR;
140
141         set_atk_relation(fingerprint_label, fingerprint, Atk.RelationType.LABEL_FOR);
142
143         var fingerprint_width_constraint = new ScrolledWindow(null, null);
144         fingerprint_width_constraint.set_policy(PolicyType.NEVER, PolicyType.NEVER);
145         fingerprint_width_constraint.set_shadow_type(ShadowType.IN);
146         fingerprint_width_constraint.set_size_request(360, 60);
147         fingerprint_width_constraint.add_with_viewport(fingerprint);
148
149         var vbox = new VBox(false, 0);
150         vbox.pack_start(fingerprint_label, true, true, 2);
151         vbox.pack_start(fingerprint_width_constraint, true, true, 2);
152         return vbox;
153 }
154
155     // Yeah, it doesn't mean "colonize" the way you might think... :-)
156 internal static string colonize(string input, int bytes_per_line) {
157     return_if_fail(input.length % 2 == 0);
158
159     string result = "";
160     int i = 0;
161     int line_bytes = 0;
162     while (i < input.length) {
163         if (line_bytes == bytes_per_line) {
164             result += "\n";
165             line_bytes = 0;
166         }
167         else if (i > 0) {
168             result += ":";
169         }
170         result += input[i : i + 2];
171         i += 2;
172         line_bytes++;
173     }
174     return result;
175 }
176
177 static Gdk.Color white;
178 static void set_bg_color(Widget w)
179 {
180 #if OS_WIN32
181
182     if (white == null) {
183         white = make_color(65535, 65535, 65535);
184     }
185
186     w.modify_bg(StateType.NORMAL, white);
187
188 #endif
189 }