567489c6adbc0904916b9dd82596ecc3e18eb826
[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     private bool button_press_cb ()
30     {
31         this.hbutton_box.set_visible (!hbutton_box.get_visible ());
32
33         set_idcard_color ();
34
35         if (hbutton_box.get_visible () == true)
36           this.expanded ();
37
38         return false;
39     }
40
41     private void delete_button_cb ()
42     {
43        this.remove_id ();
44     }
45
46     private void details_button_cb ()
47     {
48        this.details_id ();
49     }
50
51     private void send_button_cb ()
52     {
53        this.send_id ();
54     }
55
56     private void set_idcard_color ()
57     {
58         var color = Gdk.Color ();
59
60         if (hbutton_box.get_visible () == false)
61         {
62             color.red = 65535;
63             color.green = 65535;
64             color.blue = 65535;
65         }
66         else
67         {
68             color.red = 33333;
69             color.green = 33333;
70             color.blue = 60000;
71         }
72         var state = this.get_state ();
73         this.event_box.modify_bg (state, color);
74     }
75     
76     public void
77     update_id_card_label ()
78     {
79         string services_text = "";
80
81         var display_name = Markup.printf_escaped ("<big><b>%s</b></big>", this.id_card.display_name);
82         var issued_by =  Markup.printf_escaped ("<i>%s</i>", _("Issued by: "));
83         var issuer_name = Markup.printf_escaped ("<i><b>%s</b></i>", this.id_card.issuer);
84         for (int i=0; i<id_card.services.length; i++)
85         {
86             var service = id_card.services[i];
87             
88             if (i == (id_card.services.length - 1))
89               services_text = services_text + Markup.printf_escaped ("<i>%s</i>", service);
90             else
91               services_text = services_text + Markup.printf_escaped ("<i>%s, </i>", service);
92         }
93         label.set_markup (display_name + "\n" + issued_by + issuer_name + "\n" + services_text);
94     }
95
96     public IdCardWidget (IdCard id_card)
97     {
98         this.id_card = id_card;
99
100         var image = new Image.from_pixbuf (id_card.get_data ("pixbuf"));
101
102         label = new Label (null);
103         label.set_alignment ((float) 0.5, (float) 0.5);
104         label.set_justify(Gtk.Justification.CENTER);
105         label.set_ellipsize (Pango.EllipsizeMode.END);
106         update_id_card_label();
107
108         table = new Gtk.HBox (false, 6);
109         table.pack_start (image, false, false, 0);
110         table.pack_start (label, true, true, 0);
111
112         this.delete_button = new Button.with_label (_("Delete"));
113         this.details_button = new Button.with_label (_("View details"));
114         this.send_button = new Button.with_label (_("Send"));
115         set_atk_name_description (delete_button, _("Delete"), _("Delete this ID Card"));
116         set_atk_name_description (details_button, _("Details"), _("View the details of this ID Card"));
117         set_atk_name_description (send_button, _("Send"), _("Send this ID Card"));
118         this.hbutton_box = new HButtonBox ();
119         hbutton_box.pack_end (delete_button);
120         hbutton_box.pack_end (details_button);
121         hbutton_box.pack_end (send_button);
122         send_button.set_sensitive (false);
123
124         delete_button.clicked.connect (delete_button_cb);
125         details_button.clicked.connect (details_button_cb);
126         send_button.clicked.connect (send_button_cb);
127
128         this.main_vbox = new VBox (false, 12);
129         main_vbox.pack_start (table, true, true, 0);
130         main_vbox.pack_start (hbutton_box, false, false, 0);
131         main_vbox.set_border_width (12);
132
133         event_box = new EventBox ();
134         event_box.add (main_vbox);
135         event_box.button_press_event.connect (button_press_cb);
136         this.pack_start (event_box, true, true);
137
138         this.show_all ();
139         this.hbutton_box.hide ();
140
141         set_idcard_color ();
142     }
143
144     private void set_atk_name_description (Widget widget, string name, string description)
145     {
146        var atk_widget = widget.get_accessible ();
147
148        atk_widget.set_name (name);
149        atk_widget.set_description (description);
150     }
151 }