Checkpointing: ID Selector (main dialog) now looks as specified by reskinning wirefra...
[moonshot-ui.git] / src / moonshot-idcard-widget.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 using Gtk;
33
34 class IdCardWidget : Box
35 {
36     static MoonshotLogger logger = get_logger("IdCardWidget");
37
38     public IdCard id_card { get; set; default = null; }
39     private VBox main_vbox;
40     private HBox table;
41     // public Button delete_button { get; private set; default = null; }
42     // public Button details_button { get; private set; default = null; }
43     // public Button send_button { get; private set; default = null; }
44 //    private HButtonBox hbutton_box;
45     private EventBox event_box;
46     private bool   is_selected = false;
47     
48     private Label label;
49
50     internal int _position;
51     internal int position {
52         get {return _position;}
53         set {_position = value; set_idcard_color();}
54         default = 0;
55     }
56
57     public signal void expanded();
58     public signal void remove_id();
59     public signal void details_id();
60     public signal void send_id();
61
62     public void collapse()
63     {
64 //        this.hbutton_box.set_visible(false);
65         is_selected = false;
66         update_id_card_label();
67
68         set_idcard_color();
69     }
70
71     public void expand()
72     {
73 //        this.hbutton_box.set_visible(true);
74         is_selected = true;
75         update_id_card_label();
76
77         set_idcard_color();
78         this.expanded();
79     }
80
81     private bool button_press_cb()
82     {
83         if (is_selected)
84             collapse();
85         else
86             expand();
87
88         return false;
89     }
90
91     private void delete_button_cb()
92     {
93         this.remove_id();
94     }
95
96     private void details_button_cb()
97     {
98         this.details_id();
99     }
100
101     private void send_button_cb()
102     {
103         this.send_id();
104     }
105
106     private void set_idcard_color()
107     {
108         var color = Gdk.Color();
109
110         if (is_selected)
111         {
112                 color.red = 0xd9 << 8;
113                 color.green = 0xf7 << 8;
114                 color.blue = 65535;
115         }
116         else {
117             logger.trace("set_idcard_color: position=" + position.to_string());
118             if (position % 2 == 0)
119             {
120                 color.red = color.green = color.blue = 0xf2 << 8;
121             }
122             else
123             {
124                 color.red = 65535;
125                 color.green = 65535;
126                 color.blue = 65535;
127
128             }
129         }
130         var state = this.get_state();
131         this.event_box.modify_bg(state, color);
132     }
133     
134     private void
135     update_id_card_label()
136     {
137         // !!TODO: Use a table to format the labels and values
138         string services_text = "Services:  ";
139         string service_spacer = "                ";
140
141         var label_text = Markup.printf_escaped("<big>%s</big>", this.id_card.display_name);
142
143         if (is_selected)
144         {
145             label_text += "\nUsername:  " + id_card.username;
146             label_text += "\nRealm:  " + id_card.issuer;
147
148             var sep = "";
149             for (int i = 0; i < id_card.services.length; i++)
150             {
151                 services_text += sep;
152                 services_text += id_card.services[i];
153
154                 sep = "\n" + service_spacer;
155             }
156             label_text += "\n" + services_text;
157         }
158
159         label.set_markup(label_text);
160     }
161
162     public IdCardWidget(IdCard id_card)
163     {
164         this.id_card = id_card;
165
166         var image = new Image.from_pixbuf(get_pixbuf(id_card));
167
168         label = new Label(null);
169         label.set_alignment((float) 0, (float) 0.5);
170         label.set_ellipsize(Pango.EllipsizeMode.END);
171         update_id_card_label();
172
173         table = new Gtk.HBox(false, 6);
174         table.pack_start(image, false, false, 0);
175         table.pack_start(label, true, true, 0);
176
177         // this.delete_button = new Button.with_label(_("Delete"));
178         // this.details_button = new Button.with_label(_("View details"));
179         // this.send_button = new Button.with_label(_("Send"));
180         // set_atk_name_description(delete_button, _("Delete"), _("Delete this ID Card"));
181         // set_atk_name_description(details_button, _("Details"), _("View the details of this ID Card"));
182         // set_atk_name_description(send_button, _("Send"), _("Send this ID Card"));
183         // this.hbutton_box = new HButtonBox();
184         // hbutton_box.pack_end(delete_button);
185         // hbutton_box.pack_end(details_button);
186         // hbutton_box.pack_end(send_button);
187         // send_button.set_sensitive(false);
188
189         // delete_button.clicked.connect(delete_button_cb);
190         // details_button.clicked.connect(details_button_cb);
191         // send_button.clicked.connect(send_button_cb);
192
193         this.main_vbox = new VBox(false, 12);
194         main_vbox.pack_start(table, true, true, 0);
195 //        main_vbox.pack_start(hbutton_box, false, false, 0);
196         main_vbox.set_border_width(12);
197
198         event_box = new EventBox();
199         event_box.add(main_vbox);
200         event_box.button_press_event.connect(button_press_cb);
201         event_box.set_visible(false);
202         this.pack_start(event_box, true, true);
203
204         this.show_all();
205 //        this.hbutton_box.hide();
206
207         set_idcard_color();
208     }
209
210     // private void set_atk_name_description(Widget widget, string name, string description)
211     // {
212     //     if (widget == null)
213     //     {
214     //         logger.error("set_atk_name_description: widget is null for name=" + name + "; description=" + description);
215     //         return;
216     //     }
217
218     //     var atk_widget = widget.get_accessible();
219
220     //     if (atk_widget == null)
221     //     {
222     //         logger.error("set_atk_name_description: atk_widget is null for name=" + name + "; description=" + description);
223     //         return;
224     //     }
225     //     atk_widget.set_name(name);
226     //     atk_widget.set_description(description);
227     // }
228 }