Reselect current id card by nai when cards are reloaded.
[moonshot-ui.git] / src / moonshot-idcard-widget.vala
1 using Gtk;
2
3 class IdCardWidget : Box
4 {
5     public IdCard id_card { get; set; default = null; }
6
7     private VBox main_vbox;
8     private HBox table;
9     public Button delete_button { get; private set; default = null; }
10     public Button details_button { get; private set; default = null; }
11     public Button send_button { get; private set; default = null; }
12     private HButtonBox hbutton_box;
13     private EventBox event_box;
14     
15     private Label label;
16
17     public signal void expanded ();
18     public signal void remove_id ();
19     public signal void details_id ();
20     public signal void send_id ();
21
22     public void collapse ()
23     {
24         this.hbutton_box.set_visible (false);
25
26         set_idcard_color ();
27     }
28
29     public void expand ()
30     {
31         this.hbutton_box.set_visible (true);
32
33         set_idcard_color ();
34         this.expanded ();
35     }
36
37     private bool button_press_cb ()
38     {
39         if (hbutton_box.get_visible ())
40             collapse ();
41         else
42             expand ();
43
44         return false;
45     }
46
47     private void delete_button_cb ()
48     {
49        this.remove_id ();
50     }
51
52     private void details_button_cb ()
53     {
54        this.details_id ();
55     }
56
57     private void send_button_cb ()
58     {
59        this.send_id ();
60     }
61
62     private void set_idcard_color ()
63     {
64         var color = Gdk.Color ();
65
66         if (hbutton_box.get_visible () == false)
67         {
68             color.red = 65535;
69             color.green = 65535;
70             color.blue = 65535;
71         }
72         else
73         {
74             color.red = 33333;
75             color.green = 33333;
76             color.blue = 60000;
77         }
78         var state = this.get_state ();
79         this.event_box.modify_bg (state, color);
80     }
81     
82     public void
83     update_id_card_label ()
84     {
85         string services_text = "";
86
87         var display_name = Markup.printf_escaped ("<big>%s</big>", this.id_card.display_name);
88         for (int i=0; i<id_card.services.length; i++)
89         {
90             var service = id_card.services[i];
91             
92             if (i == (id_card.services.length - 1))
93               services_text = services_text + Markup.printf_escaped ("<i>%s</i>", service);
94             else
95               services_text = services_text + Markup.printf_escaped ("<i>%s, </i>", service);
96         }
97         label.set_markup (display_name + "\n" + services_text);
98     }
99
100     public IdCardWidget (IdCard id_card)
101     {
102         this.id_card = id_card;
103
104         var image = new Image.from_pixbuf (get_pixbuf(id_card));
105
106         label = new Label (null);
107         label.set_alignment ((float) 0, (float) 0.5);
108         label.set_ellipsize (Pango.EllipsizeMode.END);
109         update_id_card_label();
110
111         table = new Gtk.HBox (false, 6);
112         table.pack_start (image, false, false, 0);
113         table.pack_start (label, true, true, 0);
114
115         this.delete_button = new Button.with_label (_("Delete"));
116         this.details_button = new Button.with_label (_("View details"));
117         this.send_button = new Button.with_label (_("Send"));
118         set_atk_name_description (delete_button, _("Delete"), _("Delete this ID Card"));
119         set_atk_name_description (details_button, _("Details"), _("View the details of this ID Card"));
120         set_atk_name_description (send_button, _("Send"), _("Send this ID Card"));
121         this.hbutton_box = new HButtonBox ();
122         hbutton_box.pack_end (delete_button);
123         hbutton_box.pack_end (details_button);
124         hbutton_box.pack_end (send_button);
125         send_button.set_sensitive (false);
126
127         delete_button.clicked.connect (delete_button_cb);
128         details_button.clicked.connect (details_button_cb);
129         send_button.clicked.connect (send_button_cb);
130
131         this.main_vbox = new VBox (false, 12);
132         main_vbox.pack_start (table, true, true, 0);
133         main_vbox.pack_start (hbutton_box, false, false, 0);
134         main_vbox.set_border_width (12);
135
136         event_box = new EventBox ();
137         event_box.add (main_vbox);
138         event_box.button_press_event.connect (button_press_cb);
139         this.pack_start (event_box, true, true);
140
141         this.show_all ();
142         this.hbutton_box.hide ();
143
144         set_idcard_color ();
145     }
146
147     private void set_atk_name_description (Widget widget, string name, string description)
148     {
149        var atk_widget = widget.get_accessible ();
150
151        atk_widget.set_name (name);
152        atk_widget.set_description (description);
153     }
154 }