Use the label text 'Do not use a Moonshot identity for this service' if displaying...
[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 IdentityManagerView manager_view;
39
40     public IdCard id_card { get; set; default = null; }
41     private VBox main_vbox;
42     private HBox table;
43     private EventBox event_box;
44     private bool   is_selected = false;
45     
46     private Label label;
47
48     internal int _position = 0;
49     internal int position {
50         get {return _position;}
51         set {_position = value; set_idcard_color();}
52     }
53
54     public signal void expanded();
55     public signal void collapsed();
56
57     private void select()
58     {
59         expand();
60         this.expanded();
61     }
62
63     private void unselect()
64     {
65         collapse();
66         this.collapsed();
67     }
68
69     public void expand()
70     {
71         is_selected = true;
72         update_id_card_label();
73
74         set_idcard_color();
75         this.expanded();
76     }
77
78     public void collapse()
79     {
80         is_selected = false;
81         update_id_card_label();
82
83         set_idcard_color();
84     }
85
86     private bool button_press_cb()
87     {
88         if (is_selected)
89             unselect();
90         else
91             select();
92
93         return false;
94     }
95
96     private void set_idcard_color()
97     {
98         var color = Gdk.Color();
99
100         if (is_selected)
101         {
102                 color.red = 0xd9 << 8;
103                 color.green = 0xf7 << 8;
104                 color.blue = 65535;
105         }
106         else {
107             if (position % 2 == 0)
108             {
109                 color.red = color.green = color.blue = 0xf2 << 8;
110             }
111             else
112             {
113                 color.red = 65535;
114                 color.green = 65535;
115                 color.blue = 65535;
116
117             }
118         }
119         var state = this.get_state();
120         this.event_box.modify_bg(state, color);
121     }
122     
123     private void
124     update_id_card_label()
125     {
126         // !!TODO: Use a table to format the labels and values
127         string services_text = _("Services:  ");
128         string service_spacer = _("\n                ");
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(_("<big>%s</big>"), display_name);
133
134         if (is_selected)
135         {
136             label_text += "\nUsername:  " + id_card.username;
137             label_text += "\nRealm:  " + id_card.issuer;
138             if (!id_card.trust_anchor.is_empty()) {
139                 label_text += _("\nTrust anchor: Enterprise provisioned");
140             }
141             services_text += this.id_card.get_services_string(service_spacer);
142             label_text += _("\n") + services_text;
143         }
144
145         label.set_markup(label_text);
146     }
147
148     public IdCardWidget(IdCard id_card, IdentityManagerView manager_view)
149     {
150         this.id_card = id_card;
151         this.manager_view = manager_view;
152
153         var image = new Image.from_pixbuf(get_pixbuf(id_card));
154
155         label = new Label(null);
156         label.set_alignment((float) 0, (float) 0.5);
157         label.set_ellipsize(Pango.EllipsizeMode.END);
158         update_id_card_label();
159
160         table = new Gtk.HBox(false, 6);
161         table.pack_start(image, false, false, 0);
162         table.pack_start(label, true, true, 0);
163
164         this.main_vbox = new VBox(false, 12);
165         main_vbox.pack_start(table, true, true, 0);
166         main_vbox.set_border_width(12);
167
168         event_box = new EventBox();
169         event_box.add(main_vbox);
170         event_box.button_press_event.connect(button_press_cb);
171         event_box.set_visible(false);
172         this.pack_start(event_box, true, true);
173
174         this.show_all();
175
176         set_idcard_color();
177     }
178 }