First pass at reskinning the main dialog (still need to update menu & detail views)
[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
40     private VBox main_vbox;
41     private HBox table;
42     public Button delete_button { get; private set; default = null; }
43     public Button details_button { get; private set; default = null; }
44     public Button send_button { get; private set; default = null; }
45 //    private HButtonBox hbutton_box;
46     private EventBox event_box;
47     private bool   is_selected = false;
48     
49     private Label label;
50
51     public signal void expanded();
52     public signal void remove_id();
53     public signal void details_id();
54     public signal void send_id();
55
56     public void collapse()
57     {
58 //        this.hbutton_box.set_visible(false);
59         is_selected = false;
60         update_id_card_label();
61
62         set_idcard_color();
63     }
64
65     public void expand()
66     {
67 //        this.hbutton_box.set_visible(true);
68         is_selected = true;
69         update_id_card_label();
70
71         set_idcard_color();
72         this.expanded();
73     }
74
75     private bool button_press_cb()
76     {
77         if (is_selected)
78             collapse();
79         else
80             expand();
81
82         return false;
83     }
84
85     private void delete_button_cb()
86     {
87         this.remove_id();
88     }
89
90     private void details_button_cb()
91     {
92         this.details_id();
93     }
94
95     private void send_button_cb()
96     {
97         this.send_id();
98     }
99
100     private void set_idcard_color()
101     {
102         var color = Gdk.Color();
103
104         if (!is_selected)
105         {
106             color.red = 65535;
107             color.green = 65535;
108             color.blue = 65535;
109         }
110         else
111         {
112
113             color.red = 0xd9 << 8;
114             color.green = 0xf7 << 8;
115             color.blue = 65535;
116         }
117         var state = this.get_state();
118         this.event_box.modify_bg(state, color);
119     }
120     
121     private void
122     update_id_card_label()
123     {
124         // !!TODO: Use a table to format the labels and values
125         string services_text = "Services:  ";
126         string service_spacer = "                ";
127
128         var label_text = Markup.printf_escaped("<big>%s</big>", this.id_card.display_name);
129
130         if (is_selected)
131         {
132             label_text += "\nUsername:  " + id_card.username;
133             label_text += "\nRealm:  " + id_card.issuer;
134
135             var sep = "";
136             for (int i = 0; i < id_card.services.length; i++)
137             {
138                 services_text += sep;
139                 services_text += id_card.services[i];
140
141                 sep = "\n" + service_spacer;
142             }
143             label_text += "\n" + services_text;
144         }
145
146         label.set_markup(label_text);
147     }
148
149     public IdCardWidget(IdCard id_card)
150     {
151         this.id_card = id_card;
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.delete_button = new Button.with_label(_("Delete"));
165         this.details_button = new Button.with_label(_("View details"));
166         this.send_button = new Button.with_label(_("Send"));
167         set_atk_name_description(delete_button, _("Delete"), _("Delete this ID Card"));
168         set_atk_name_description(details_button, _("Details"), _("View the details of this ID Card"));
169         set_atk_name_description(send_button, _("Send"), _("Send this ID Card"));
170         // this.hbutton_box = new HButtonBox();
171         // hbutton_box.pack_end(delete_button);
172         // hbutton_box.pack_end(details_button);
173         // hbutton_box.pack_end(send_button);
174         send_button.set_sensitive(false);
175
176         delete_button.clicked.connect(delete_button_cb);
177         details_button.clicked.connect(details_button_cb);
178         send_button.clicked.connect(send_button_cb);
179
180         this.main_vbox = new VBox(false, 12);
181         main_vbox.pack_start(table, true, true, 0);
182 //        main_vbox.pack_start(hbutton_box, false, false, 0);
183         main_vbox.set_border_width(12);
184
185         event_box = new EventBox();
186         event_box.add(main_vbox);
187         event_box.button_press_event.connect(button_press_cb);
188         this.pack_start(event_box, true, true);
189
190         this.show_all();
191 //        this.hbutton_box.hide();
192
193         set_idcard_color();
194     }
195
196     private void set_atk_name_description(Widget widget, string name, string description)
197     {
198         var atk_widget = widget.get_accessible();
199
200         atk_widget.set_name(name);
201         atk_widget.set_description(description);
202     }
203 }