2 * Copyright (c) 2011-2016, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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
37 extern string? g_win32_get_package_installation_directory_of_module(void *module);
40 public Gdk.Pixbuf? find_icon_sized(string name, Gtk.IconSize icon_size)
43 Gtk.icon_size_lookup(icon_size, out width, out height);
44 return find_icon(name, width);
47 /* Portability hack: making Gtk icon themes work on Windows is
48 * difficult; let's just bundle the icons that are necessary and
52 public bool gtk_available = false;
54 public Gdk.Pixbuf? get_pixbuf(IdCard id)
56 return find_icon("avatar-default", 48);
59 public Gdk.Pixbuf? find_icon(string name, int size)
66 string? base_path = g_win32_get_package_installation_directory_of_module(null);
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);
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);
77 var icon_theme = Gtk.IconTheme.get_default();
78 return icon_theme.load_icon(name, size, Gtk.IconLookupFlags.FORCE_SIZE);
83 stdout.printf("Error loading icon '%s': %s\n", name, e.message);
88 public extern unowned string GetUserName();
89 public extern unowned string GetFlatStoreUsersFilePath();
91 public bool UserForcesFlatFileStore()
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) {
99 string? flatstore_username = null;
100 while ((flatstore_username = flatstore_users.read_line()) != null) {
101 if (username == flatstore_username) {
108 internal Gdk.Color make_color(uint16 red, uint16 green, uint16 blue)
110 Gdk.Color color = Gdk.Color();
118 internal void set_atk_relation(Widget widget, Widget target_widget, Atk.RelationType relationship)
120 var atk_widget = widget.get_accessible();
121 var atk_target_widget = target_widget.get_accessible();
123 atk_widget.add_relationship(relationship, atk_target_widget);
127 internal Widget make_ta_fingerprint_widget(string server_cert, string? label_text = null)
129 var fingerprint_label = new Label(label_text ?? _("SHA-256 fingerprint:"));
130 fingerprint_label.set_alignment(0, 0.5f);
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;
141 set_atk_relation(fingerprint_label, fingerprint, Atk.RelationType.LABEL_FOR);
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);
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);
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);
162 while (i < input.length) {
163 if (line_bytes == bytes_per_line) {
170 result += input[i : i + 2];
177 internal static void clear_password_entry(Entry entry) {
179 // Overwrite the entry with random data
180 var len = entry.get_text().length;
181 var random_chars = new char[len + 1];
182 for (int i = 0; i < len; i++) {
183 random_chars[i] = (char) Random.int_range(40, 127);
185 random_chars[len] = 0;
186 string r = (string) random_chars;
187 var buf = entry.get_buffer();
188 buf.set_text(r.data);
190 // Now delete the data
191 buf.delete_text(0, len);
194 static Gdk.Color white;
195 static void set_bg_color(Widget w)
200 white = make_color(65535, 65535, 65535);
203 w.modify_bg(StateType.NORMAL, white);