Fixed display of the no_identity widget, and don't allow it to be removed
[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 table;
45     private EventBox event_box;
46     private bool   is_selected = false;
47     private Arrow arrow;
48     
49     private Label label;
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     private void select()
61     {
62         expand();
63         this.expanded();
64     }
65
66     private void unselect()
67     {
68         collapse();
69         this.collapsed();
70     }
71
72     public void expand()
73     {
74         is_selected = true;
75         update_id_card_label();
76
77         set_idcard_color();
78         arrow.set(ArrowType.DOWN, ARROW_SHADOW);
79         this.expanded();
80     }
81
82     public void collapse()
83     {
84         is_selected = false;
85         update_id_card_label();
86
87         set_idcard_color();
88         arrow.set(ArrowType.RIGHT, ARROW_SHADOW);
89     }
90
91     private bool button_press_cb()
92     {
93         if (is_selected)
94             unselect();
95         else
96             select();
97
98         return false;
99     }
100
101     private void set_idcard_color()
102     {
103         var color = Gdk.Color();
104
105         if (is_selected)
106         {
107                 color.red = 0xd9 << 8;
108                 color.green = 0xf7 << 8;
109                 color.blue = 65535;
110         }
111         else {
112             if (position % 2 == 0)
113             {
114                 color.red = color.green = color.blue = 0xf2 << 8;
115             }
116             else
117             {
118                 color.red = 65535;
119                 color.green = 65535;
120                 color.blue = 65535;
121
122             }
123         }
124         this.event_box.modify_bg(StateType.NORMAL, color);
125         this.arrow.modify_bg(StateType.NORMAL, color);
126     }
127     
128     private void
129     update_id_card_label()
130     {
131         // !!TODO: Use a table to format the labels and values
132         string service_spacer = _("\n                ");
133
134         var display_name = (manager_view.selection_in_progress() && this.id_card.is_no_identity()
135                             ? "Do not use a Moonshot identity for this service" : this.id_card.display_name);
136         var label_text = Markup.printf_escaped(_("<big>%s</big>"), display_name);
137
138         if (is_selected)
139         {
140             if (!this.id_card.is_no_identity()) {
141                 label_text += "\nUsername:  " + id_card.username;
142                 label_text += "\nRealm:  " + id_card.issuer;
143                 if (!id_card.trust_anchor.is_empty()) {
144                     label_text += _("\nTrust anchor: Enterprise provisioned");
145                 }
146             }
147
148             string services_text = _("Services:  ") + this.id_card.get_services_string(service_spacer);
149             label_text += _("\n") + services_text;
150         }
151
152         label.set_markup(label_text);
153     }
154
155     public IdCardWidget(IdCard id_card, IdentityManagerView manager_view)
156     {
157         this.id_card = id_card;
158         this.manager_view = manager_view;
159
160         label = new Label(null);
161         label.set_alignment((float) 0, (float) 0.5);
162         label.set_ellipsize(Pango.EllipsizeMode.END);
163         update_id_card_label();
164
165         table = new Gtk.HBox(false, 6);
166         var image = new Image.from_pixbuf(get_pixbuf(id_card));
167         if (this.id_card.is_no_identity()) {
168             image.clear();
169             // Use padding to make the image size =  48x48 (size = 2x padding)
170             image.set_padding(24, 24);
171         }
172         table.pack_start(image, false, false, 0);
173         table.pack_start(label, true, true, 0);
174         this.arrow = new Arrow(ArrowType.RIGHT, ARROW_SHADOW);
175         table.pack_start(arrow, false, false);
176
177         this.main_vbox = new VBox(false, 12);
178         main_vbox.pack_start(table, true, true, 0);
179         main_vbox.set_border_width(12);
180
181         event_box = new EventBox();
182         event_box.add(main_vbox);
183         event_box.button_press_event.connect(button_press_cb);
184         event_box.set_visible(false);
185         this.pack_start(event_box, true, true);
186
187         this.show_all();
188
189         set_idcard_color();
190     }
191 }