Cleaning up compilation warnings
[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     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         update_id_card_label();
76
77         set_idcard_color();
78         arrow.set(ArrowType.DOWN, ARROW_SHADOW);
79     }
80
81     public void collapse()
82     {
83         is_selected = false;
84         update_id_card_label();
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     update_id_card_label()
129     {
130         // !!TODO: Use a table to format the labels and values
131         string service_spacer = _("\n                ");
132
133         var display_name = (manager_view.selection_in_progress() && this.id_card.is_no_identity()
134                             ? "Do not use a Moonshot identity for this service" : this.id_card.display_name);
135         var label_text = Markup.printf_escaped(_("<span rise='8000'><big>%s</big></span>"), display_name);
136
137         if (is_selected)
138         {
139             if (!this.id_card.is_no_identity()) {
140                 label_text += "\nUsername:  " + id_card.username;
141                 label_text += "\nRealm:  " + id_card.issuer;
142                 if (!id_card.trust_anchor.is_empty()) {
143                     label_text += _("\nTrust anchor: Enterprise provisioned");
144                 }
145             }
146
147             string services_text = _("Services:  ") + this.id_card.get_services_string(service_spacer);
148             label_text += _("\n") + services_text;
149         }
150
151         label.set_markup(label_text);
152     }
153
154     public IdCardWidget(IdCard id_card, IdentityManagerView manager_view)
155     {
156         this.id_card = id_card;
157         this.manager_view = manager_view;
158
159         label = new Label(null);
160         label.set_alignment((float) 0, (float) 0.5);
161         label.set_ellipsize(Pango.EllipsizeMode.END);
162         update_id_card_label();
163
164         table = new Gtk.HBox(false, 6);
165         var image = new Image.from_pixbuf(get_pixbuf(id_card));
166         if (this.id_card.is_no_identity()) {
167             image.clear();
168             // Use padding to make the image size =  48x48 (size = 2x padding)
169             image.set_padding(24, 24);
170         }
171         table.pack_start(image, false, false, 0);
172         table.pack_start(label, true, true, 0);
173         this.arrow = new Arrow(ArrowType.RIGHT, ARROW_SHADOW);
174         table.pack_start(arrow, false, false);
175
176         this.main_vbox = new VBox(false, 12);
177         main_vbox.pack_start(table, true, true, 0);
178         main_vbox.set_border_width(12);
179
180         event_box = new EventBox();
181         event_box.add(main_vbox);
182         event_box.button_press_event.connect(button_press_cb);
183         event_box.set_visible(false);
184         this.pack_start(event_box, true, true);
185
186         this.show_all();
187
188         set_idcard_color();
189     }
190 }