Minor formatting changes
[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     public IdCard id_card { get; set; default = null; }
37
38     private VBox main_vbox;
39     private HBox table;
40     public Button delete_button { get; private set; default = null; }
41     public Button details_button { get; private set; default = null; }
42     public Button send_button { get; private set; default = null; }
43     private HButtonBox hbutton_box;
44     private EventBox event_box;
45     
46     private Label label;
47
48     public signal void expanded();
49     public signal void remove_id();
50     public signal void details_id();
51     public signal void send_id();
52
53     public void collapse()
54     {
55         this.hbutton_box.set_visible(false);
56
57         set_idcard_color();
58     }
59
60     public void expand()
61     {
62         this.hbutton_box.set_visible(true);
63
64         set_idcard_color();
65         this.expanded();
66     }
67
68     private bool button_press_cb()
69     {
70         if (hbutton_box.get_visible())
71             collapse();
72         else
73             expand();
74
75         return false;
76     }
77
78     private void delete_button_cb()
79     {
80         this.remove_id();
81     }
82
83     private void details_button_cb()
84     {
85         this.details_id();
86     }
87
88     private void send_button_cb()
89     {
90         this.send_id();
91     }
92
93     private void set_idcard_color()
94     {
95         var color = Gdk.Color();
96
97         if (hbutton_box.get_visible() == false)
98         {
99             color.red = 65535;
100             color.green = 65535;
101             color.blue = 65535;
102         }
103         else
104         {
105             color.red = 33333;
106             color.green = 33333;
107             color.blue = 60000;
108         }
109         var state = this.get_state();
110         this.event_box.modify_bg(state, color);
111     }
112     
113     public void
114     update_id_card_label()
115     {
116         string services_text = "";
117
118         var display_name = Markup.printf_escaped("<big>%s</big>", this.id_card.display_name);
119         for (int i = 0; i < id_card.services.length; i++)
120         {
121             var service = id_card.services[i];
122             
123             if (i == (id_card.services.length - 1))
124                 services_text = services_text + Markup.printf_escaped("<i>%s</i>", service);
125             else
126                 services_text = services_text + Markup.printf_escaped("<i>%s, </i>", service);
127         }
128         label.set_markup(display_name + "\n" + services_text);
129     }
130
131     public IdCardWidget(IdCard id_card)
132     {
133         this.id_card = id_card;
134
135         var image = new Image.from_pixbuf(get_pixbuf(id_card));
136
137         label = new Label(null);
138         label.set_alignment((float) 0, (float) 0.5);
139         label.set_ellipsize(Pango.EllipsizeMode.END);
140         update_id_card_label();
141
142         table = new Gtk.HBox(false, 6);
143         table.pack_start(image, false, false, 0);
144         table.pack_start(label, true, true, 0);
145
146         this.delete_button = new Button.with_label(_("Delete"));
147         this.details_button = new Button.with_label(_("View details"));
148         this.send_button = new Button.with_label(_("Send"));
149         set_atk_name_description(delete_button, _("Delete"), _("Delete this ID Card"));
150         set_atk_name_description(details_button, _("Details"), _("View the details of this ID Card"));
151         set_atk_name_description(send_button, _("Send"), _("Send this ID Card"));
152         this.hbutton_box = new HButtonBox();
153         hbutton_box.pack_end(delete_button);
154         hbutton_box.pack_end(details_button);
155         hbutton_box.pack_end(send_button);
156         send_button.set_sensitive(false);
157
158         delete_button.clicked.connect(delete_button_cb);
159         details_button.clicked.connect(details_button_cb);
160         send_button.clicked.connect(send_button_cb);
161
162         this.main_vbox = new VBox(false, 12);
163         main_vbox.pack_start(table, true, true, 0);
164         main_vbox.pack_start(hbutton_box, false, false, 0);
165         main_vbox.set_border_width(12);
166
167         event_box = new EventBox();
168         event_box.add(main_vbox);
169         event_box.button_press_event.connect(button_press_cb);
170         this.pack_start(event_box, true, true);
171
172         this.show_all();
173         this.hbutton_box.hide();
174
175         set_idcard_color();
176     }
177
178     private void set_atk_name_description(Widget widget, string name, string description)
179     {
180         var atk_widget = widget.get_accessible();
181
182         atk_widget.set_name(name);
183         atk_widget.set_description(description);
184     }
185 }