Dead code removal; also fixed Send button (at least a little; not tested yet.)
[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     private EventBox event_box;
42     private bool   is_selected = false;
43     
44     private Label label;
45
46     internal int _position = 0;
47     internal int position {
48         get {return _position;}
49         set {_position = value; set_idcard_color();}
50     }
51
52     public signal void expanded();
53     public signal void remove_id();
54     public signal void details_id();
55     public signal void send_id();
56
57     public void collapse()
58     {
59         is_selected = false;
60         update_id_card_label();
61
62         set_idcard_color();
63     }
64
65     public void expand()
66     {
67         is_selected = true;
68         update_id_card_label();
69
70         set_idcard_color();
71         this.expanded();
72     }
73
74     private bool button_press_cb()
75     {
76         if (is_selected)
77             collapse();
78         else
79             expand();
80
81         return false;
82     }
83
84     private void delete_button_cb()
85     {
86         this.remove_id();
87     }
88
89     private void details_button_cb()
90     {
91         this.details_id();
92     }
93
94     private void send_button_cb()
95     {
96         this.send_id();
97     }
98
99     private void set_idcard_color()
100     {
101         var color = Gdk.Color();
102
103         if (is_selected)
104         {
105                 color.red = 0xd9 << 8;
106                 color.green = 0xf7 << 8;
107                 color.blue = 65535;
108         }
109         else {
110             if (position % 2 == 0)
111             {
112                 color.red = color.green = color.blue = 0xf2 << 8;
113             }
114             else
115             {
116                 color.red = 65535;
117                 color.green = 65535;
118                 color.blue = 65535;
119
120             }
121         }
122         var state = this.get_state();
123         this.event_box.modify_bg(state, color);
124     }
125     
126     private void
127     update_id_card_label()
128     {
129         // !!TODO: Use a table to format the labels and values
130         string services_text = "Services:  ";
131         string service_spacer = "                ";
132
133         var label_text = Markup.printf_escaped("<big>%s</big>", this.id_card.display_name);
134
135         if (is_selected)
136         {
137             label_text += "\nUsername:  " + id_card.username;
138             label_text += "\nRealm:  " + id_card.issuer;
139
140             var sep = "";
141             for (int i = 0; i < id_card.services.length; i++)
142             {
143                 services_text += sep;
144                 services_text += id_card.services[i];
145
146                 sep = "\n" + service_spacer;
147             }
148             label_text += "\n" + services_text;
149         }
150
151         label.set_markup(label_text);
152     }
153
154     public IdCardWidget(IdCard id_card)
155     {
156         this.id_card = id_card;
157
158         var image = new Image.from_pixbuf(get_pixbuf(id_card));
159
160         label = new Label(null);
161         label.set_alignment((float) 0, (float) 0.5);
162         label.set_ellipsize(Pango.EllipsizeMode.END);
163         update_id_card_label();
164
165         table = new Gtk.HBox(false, 6);
166         table.pack_start(image, false, false, 0);
167         table.pack_start(label, true, true, 0);
168
169         this.main_vbox = new VBox(false, 12);
170         main_vbox.pack_start(table, true, true, 0);
171         main_vbox.set_border_width(12);
172
173         event_box = new EventBox();
174         event_box.add(main_vbox);
175         event_box.button_press_event.connect(button_press_cb);
176         event_box.set_visible(false);
177         this.pack_start(event_box, true, true);
178
179         this.show_all();
180
181         set_idcard_color();
182     }
183 }