Add/Edit cards is now functionally complete (still needs aesthetic cleanup)
[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             if (position % 2 == 0)
118             {
119                 color.red = color.green = color.blue = 0xf2 << 8;
120             }
121             else
122             {
123                 color.red = 65535;
124                 color.green = 65535;
125                 color.blue = 65535;
126
127             }
128         }
129         var state = this.get_state();
130         this.event_box.modify_bg(state, color);
131     }
132     
133     private void
134     update_id_card_label()
135     {
136         // !!TODO: Use a table to format the labels and values
137         string services_text = "Services:  ";
138         string service_spacer = "                ";
139
140         var label_text = Markup.printf_escaped("<big>%s</big>", this.id_card.display_name);
141
142         if (is_selected)
143         {
144             label_text += "\nUsername:  " + id_card.username;
145             label_text += "\nRealm:  " + id_card.issuer;
146
147             var sep = "";
148             for (int i = 0; i < id_card.services.length; i++)
149             {
150                 services_text += sep;
151                 services_text += id_card.services[i];
152
153                 sep = "\n" + service_spacer;
154             }
155             label_text += "\n" + services_text;
156         }
157
158         label.set_markup(label_text);
159     }
160
161     public IdCardWidget(IdCard id_card)
162     {
163         this.id_card = id_card;
164
165         var image = new Image.from_pixbuf(get_pixbuf(id_card));
166
167         label = new Label(null);
168         label.set_alignment((float) 0, (float) 0.5);
169         label.set_ellipsize(Pango.EllipsizeMode.END);
170         update_id_card_label();
171
172         table = new Gtk.HBox(false, 6);
173         table.pack_start(image, false, false, 0);
174         table.pack_start(label, true, true, 0);
175
176         // this.delete_button = new Button.with_label(_("Delete"));
177         // this.details_button = new Button.with_label(_("View details"));
178         // this.send_button = new Button.with_label(_("Send"));
179         // set_atk_name_description(delete_button, _("Delete"), _("Delete this ID Card"));
180         // set_atk_name_description(details_button, _("Details"), _("View the details of this ID Card"));
181         // set_atk_name_description(send_button, _("Send"), _("Send this ID Card"));
182         // this.hbutton_box = new HButtonBox();
183         // hbutton_box.pack_end(delete_button);
184         // hbutton_box.pack_end(details_button);
185         // hbutton_box.pack_end(send_button);
186         // send_button.set_sensitive(false);
187
188         // delete_button.clicked.connect(delete_button_cb);
189         // details_button.clicked.connect(details_button_cb);
190         // send_button.clicked.connect(send_button_cb);
191
192         this.main_vbox = new VBox(false, 12);
193         main_vbox.pack_start(table, true, true, 0);
194 //        main_vbox.pack_start(hbutton_box, false, false, 0);
195         main_vbox.set_border_width(12);
196
197         event_box = new EventBox();
198         event_box.add(main_vbox);
199         event_box.button_press_event.connect(button_press_cb);
200         event_box.set_visible(false);
201         this.pack_start(event_box, true, true);
202
203         this.show_all();
204 //        this.hbutton_box.hide();
205
206         set_idcard_color();
207     }
208
209     // private void set_atk_name_description(Widget widget, string name, string description)
210     // {
211     //     if (widget == null)
212     //     {
213     //         logger.error("set_atk_name_description: widget is null for name=" + name + "; description=" + description);
214     //         return;
215     //     }
216
217     //     var atk_widget = widget.get_accessible();
218
219     //     if (atk_widget == null)
220     //     {
221     //         logger.error("set_atk_name_description: atk_widget is null for name=" + name + "; description=" + description);
222     //         return;
223     //     }
224     //     atk_widget.set_name(name);
225     //     atk_widget.set_description(description);
226     // }
227 }