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