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