Squashed merge of many commits, including (but not limited to) :
[moonshot-ui.git] / src / moonshot-idcard-widget.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 using Gtk;
33
34 class IdCardWidget : Box
35 {
36     // static MoonshotLogger logger = get_logger("IdCardWidget");
37
38     private static const ShadowType ARROW_SHADOW = ShadowType.NONE;
39
40     private IdentityManagerView manager_view;
41
42     public IdCard id_card { get; set; default = null; }
43     private VBox main_vbox;
44     private HBox hbox;
45     private EventBox event_box;
46     private bool   is_selected = false;
47     private Arrow arrow;
48     
49     private VBox details;
50
51     internal int _position = 0;
52     internal int position {
53         get {return _position;}
54         set {_position = value; set_idcard_color();}
55     }
56
57     public signal void expanded();
58     public signal void collapsed();
59
60     internal void select()
61     {
62         expand();
63         this.expanded();
64     }
65
66     internal void unselect()
67     {
68         collapse();
69         this.collapsed();
70     }
71
72     public void expand()
73     {
74         is_selected = true;
75         details.show_all();
76
77         set_idcard_color();
78         arrow.set(ArrowType.DOWN, ARROW_SHADOW);
79     }
80
81     public void collapse()
82     {
83         is_selected = false;
84         details.hide();
85
86         set_idcard_color();
87         arrow.set(ArrowType.RIGHT, ARROW_SHADOW);
88     }
89
90     private bool button_press_cb()
91     {
92         if (is_selected)
93             unselect();
94         else
95             select();
96
97         return false;
98     }
99
100     private void set_idcard_color()
101     {
102         var color = Gdk.Color();
103
104         if (is_selected)
105         {
106                 color.red = 0xd9 << 8;
107                 color.green = 0xf7 << 8;
108                 color.blue = 65535;
109         }
110         else {
111             if (position % 2 == 0)
112             {
113                 color.red = color.green = color.blue = 0xf2 << 8;
114             }
115             else
116             {
117                 color.red = 65535;
118                 color.green = 65535;
119                 color.blue = 65535;
120
121             }
122         }
123         this.event_box.modify_bg(StateType.NORMAL, color);
124         this.arrow.modify_bg(StateType.NORMAL, color);
125     }
126     
127     private void
128     make_id_card_label(Label label)
129     {
130         var display_name = (manager_view.selection_in_progress() && this.id_card.is_no_identity()
131                             ? _("Do not use a Moonshot identity for this service") : this.id_card.display_name);
132         var label_text = Markup.printf_escaped("<span rise='8000'><big>%s</big></span>", display_name);
133
134         label.set_markup(label_text);
135     }
136
137     public IdCardWidget(IdCard id_card, IdentityManagerView manager_view)
138     {
139         this.id_card = id_card;
140         this.manager_view = manager_view;
141
142         var display_name_label = new Label(null);
143         display_name_label.set_alignment((float) 0, (float) 0.5);
144         display_name_label.set_ellipsize(Pango.EllipsizeMode.END);
145         make_id_card_label(display_name_label);
146
147         var details_wrapper = new VBox(false, 0);
148         details_wrapper.pack_start(display_name_label, false, false, 0);
149         this.details = new VBox(false, 0);
150         details_wrapper.pack_start(details, false, false, 0);
151
152         if (!this.id_card.is_no_identity()) {
153             var upper_details_text = _("Username") + ":  " + id_card.username;
154             upper_details_text += "\n" + _("Realm:") + "  " + id_card.issuer;
155             if (!id_card.trust_anchor.is_empty()) {
156                 upper_details_text += "\n" + _("Trust anchor: Enterprise provisioned");
157             }
158             Label upper_details = new Label(upper_details_text);
159             upper_details.set_alignment(0, 0);
160             details.pack_start(upper_details);
161         }
162         var services_hbox = new HBox(false, 6);
163         Label services_label = new Label(_("Services: "));
164         services_label.set_alignment(0, 0);
165
166         string services_text = this.id_card.get_services_string("\n");
167         Label service_list = new Label(services_text);
168         service_list.set_alignment(0, 0);
169         service_list.set_ellipsize(Pango.EllipsizeMode.END);
170         service_list.set_max_width_chars(50);
171         services_hbox.pack_start(services_label, false, false, 0);
172         services_hbox.pack_start(service_list, false, false, 0);
173         details.pack_start(services_hbox);
174
175         hbox = new Gtk.HBox(false, 6);
176         var image = new Image.from_pixbuf(get_pixbuf(id_card));
177         if (this.id_card.is_no_identity()) {
178             image.clear();
179             // Use padding to make the image size =  48x48 (size = 2x padding)
180             image.set_padding(24, 24);
181         }
182         hbox.pack_start(image, false, false, 0);
183         hbox.pack_start(details_wrapper, true, true, 0);
184         this.arrow = new Arrow(ArrowType.RIGHT, ARROW_SHADOW);
185         this.arrow.set_alignment((float) 0.5, (float) 0);
186         hbox.pack_start(arrow, false, false);
187
188         this.main_vbox = new VBox(false, 12);
189         main_vbox.pack_start(hbox, true, true, 0);
190         main_vbox.set_border_width(12);
191
192         event_box = new EventBox();
193         event_box.add(main_vbox);
194         event_box.button_press_event.connect(button_press_cb);
195         event_box.set_visible(false);
196         this.pack_start(event_box, true, true);
197
198         this.show_all();
199         details.hide();
200
201         set_idcard_color();
202     }
203 }