Add services to ID Card
[moonshot-ui.git] / src / moonshot-window.vala
1 using Gtk;
2
3 class MainWindow : Window
4 {
5
6     private Entry search_entry;
7     private TreeView identities_list;
8
9     private enum Columns
10     {
11         IDCARD_COL,
12         LOGO_COL,
13         NAME_COL,
14         N_COLUMNS
15     }
16
17     public MainWindow()
18     {
19         this.title = "Moonshoot";
20         this.position = WindowPosition.CENTER;
21         set_default_size (400, 300);
22
23         build_ui();
24         connect_signals();
25     }
26
27     private void search_entry_icon_press_cb ()
28     {
29         print ("Search entry icon pressed\n");
30     }
31
32     private void search_entry_text_changed_cb ()
33     {
34         var has_text = this.search_entry.get_text_length () > 0;
35         this.search_entry.set_icon_sensitive (EntryIconPosition.SECONDARY, has_text);
36     }
37
38     private void setup_identities_list ()
39     {
40         var listmodel = new ListStore (Columns.N_COLUMNS, typeof (IdCard),
41                                                           typeof (Gdk.Pixbuf),
42                                                           typeof (string));
43         this.identities_list.set_model (listmodel);
44
45         var column = new TreeViewColumn ();
46         var cell_logo = new CellRendererPixbuf ();
47         column.pack_start (cell_logo, false);
48         column.add_attribute (cell_logo, "pixbuf", Columns.LOGO_COL);
49         cell_logo.set ("stock-size", IconSize.MENU);
50
51         var cell_name = new CellRendererText ();
52         column.pack_start (cell_name, false);
53         column.add_attribute (cell_name, "text", Columns.NAME_COL);
54         cell_name.set ("ellipsize", Pango.EllipsizeMode.END,
55                        "width-chars", 18);
56
57         this.identities_list.append_column (column);
58     }
59
60     private Dialog add_identity_dialog ()
61     {
62         var dialog = new Dialog.with_buttons (_("Add ID Card"),
63                                               null,
64                                               DialogFlags.MODAL | DialogFlags.DESTROY_WITH_PARENT,
65                                               _("Add ID Card"), ResponseType.OK,
66                                               Stock.CANCEL, ResponseType.CANCEL);
67
68         var content_area = dialog.get_content_area ();
69         ((Box) content_area).set_spacing (12);
70
71         var issuer_label = new Label (_("Issuer:"));
72         issuer_label.set_alignment (1, (float) 0.5);
73         var issuer_entry = new Entry ();
74         var username_label = new Label (_("Username:"));
75         username_label.set_alignment (1, (float) 0.5);
76         var username_entry = new Entry ();
77         var password_label = new Label (_("Password:"));
78         password_label.set_alignment (1, (float) 0.5);
79         var password_entry = new Entry ();
80         password_entry.set_invisible_char ('*');
81         password_entry.set_visibility (false);
82         var remember_checkbutton = new CheckButton.with_label (_("Remember password"));
83         var table = new Table (4, 4, false);
84         table.set_col_spacings (10);
85         table.set_row_spacings (10);
86         table.attach_defaults (issuer_label, 0, 1, 0, 1);
87         table.attach_defaults (issuer_entry, 1, 2, 0, 1);
88         table.attach_defaults (username_label, 0, 1, 1, 2);
89         table.attach_defaults (username_entry, 1, 2, 1, 2);
90         table.attach_defaults (password_label, 0, 1, 2, 3);
91         table.attach_defaults (password_entry, 1, 2, 2, 3);
92         table.attach_defaults (remember_checkbutton,  1, 2, 3, 4);
93
94         var vbox = new VBox (false, 0);
95         vbox.set_border_width (6);
96         vbox.pack_start (table, false, false, 0);
97
98         ((Container) content_area).add (vbox);
99
100         dialog.set_border_width (6);
101         dialog.set_resizable (false);
102         dialog.show_all ();
103
104         return dialog;
105     }
106
107     private void add_identity ()
108     {
109         ListStore listmodel;
110         TreeIter iter;
111         string services = "";
112
113         var icon_theme = IconTheme.get_default ();
114
115         var pixbuf = icon_theme.load_icon ("avatar-default",
116                                            48,
117                                            IconLookupFlags.FORCE_SIZE);
118
119         var id_card = new IdCard ();
120         id_card.name = "University";
121         id_card.services = new string[3];
122         id_card.services[0] = "Sending emails";
123         id_card.services[1] = "Connect to IRC";
124         id_card.services[2] = "Connect to jabber";
125         id_card.number = 123;
126
127         for (int i = 0; i < id_card.services.length - 1; i++)
128         {
129             services = services + "<i>" + id_card.services[i] + "</i>, ";
130         }
131         services = services + "<i>" + id_card.services[id_card.services.length - 1] + "</i>";
132         var text = "<b>" + id_card.name + "</b>\n" + services;
133
134         listmodel = (ListStore) this.identities_list.get_model ();
135         listmodel.append (out iter);
136         listmodel.set (iter,
137                        Columns.IDCARD_COL, id_card,
138                        Columns.LOGO_COL, pixbuf,
139                        Columns.NAME_COL, text);
140     }
141
142     private void add_identity_cb ()
143     {
144         var dialog = add_identity_dialog ();
145         var result = dialog.run ();
146
147         switch (result) {
148         case ResponseType.OK:
149             add_identity ();
150             break;
151         default:
152             break;
153         }
154         dialog.destroy ();
155     }
156
157     private IdCard* get_selected_idcard ()
158     {
159         TreeModel model;
160         TreeIter iter;
161         IdCard id_card;
162
163         var selection = this.identities_list.get_selection ();
164
165         if (selection.get_selected (out model, out iter))
166         {
167             model.get (iter, Columns.IDCARD_COL, out id_card);
168             return id_card;
169         }
170
171         return null;
172     }
173
174     private void remove_identity (IdCard id_card)
175     {
176         TreeModel model;
177         TreeIter iter;
178         IdCard id_card_list;
179
180         var selection = this.identities_list.get_selection ();
181
182         if (selection.get_selected (out model, out iter))
183         {
184             model.get (iter, Columns.IDCARD_COL, out id_card_list);
185             if (id_card_list != null && id_card_list.name == id_card.name)
186             {
187                 ((ListStore) model).remove (iter);
188             }
189         }
190     }
191
192     private void remove_identity_cb ()
193     {
194         var id_card = get_selected_idcard ();
195         if (id_card == null)
196         {
197             return;
198         }
199         else
200         {
201             var dialog = new MessageDialog (null,
202                                             DialogFlags.DESTROY_WITH_PARENT,
203                                             MessageType.INFO,
204                                             Gtk.ButtonsType.YES_NO,
205                                             _("Are you sure you want to delete this ID Card?"));
206             var result = dialog.run ();
207             switch (result) {
208             case ResponseType.YES:
209                 remove_identity (id_card);
210                 break;
211             default:
212                 break;
213             }
214             dialog.destroy ();
215         }
216     }
217
218     private void label_make_bold (Label label)
219     {
220         var font_desc = new Pango.FontDescription ();
221
222         font_desc.set_weight (Pango.Weight.BOLD);
223
224         /* This will only affect the weight of the font, the rest is
225          * from the current state of the widget, which comes from the
226          * theme or user prefs, since the font desc only has the
227          * weight flag turned on.
228          */
229         label.modify_font (font_desc);
230     }
231
232     private void build_ui()
233     {
234         var toolbar = new Toolbar ();
235         var open_button = new ToolButton (null, "Open"); //.from_stock (Stock.OPEN);
236         open_button.is_important = true;
237         toolbar.add (open_button);
238         //open_button.clicked.connect (on_open_clicked);
239
240         this.search_entry = new Entry();
241         this.search_entry.set_icon_from_icon_name (EntryIconPosition.SECONDARY,
242                                                    "edit-find-symbolic");
243         this.search_entry.set_icon_sensitive (EntryIconPosition.SECONDARY, false);
244         this.search_entry.set_icon_tooltip_text (EntryIconPosition.SECONDARY,
245                                                  _("Search identity or service"));
246         this.search_entry.icon_press.connect (search_entry_icon_press_cb);
247         this.search_entry.notify["text"].connect (search_entry_text_changed_cb);
248
249         this.identities_list = new TreeView ();
250         this.identities_list.set_headers_visible (false);
251         setup_identities_list ();
252
253         var scroll = new ScrolledWindow (null, null);
254         scroll.set_policy (PolicyType.NEVER, PolicyType.AUTOMATIC);
255         scroll.set_shadow_type (ShadowType.IN);
256         scroll.add (this.identities_list);
257
258         var button_add = new ToolButton (null, null);
259         button_add.set_icon_name ("list-add-symbolic");
260         button_add.clicked.connect (add_identity_cb);
261         var button_remove = new ToolButton (null, null);
262         button_remove.set_icon_name ("list-remove-symbolic");
263         button_remove.clicked.connect (remove_identity_cb);
264         var button_toolbar = new Toolbar ();
265         button_toolbar.insert (button_add, 0);
266         button_toolbar.insert (button_remove, 1);
267
268         var vbox_left = new VBox (false, 0);
269         vbox_left.pack_start (search_entry, false, false, 6);
270         vbox_left.pack_start (scroll, true, true, 0);
271         vbox_left.pack_start (button_toolbar, false, false, 0);
272
273         var login_vbox_title = new Label (_("Login: "));
274         label_make_bold (login_vbox_title);
275         login_vbox_title.set_alignment (0, (float) 0.5);
276         var username_label = new Label (_("Username:"));
277         username_label.set_alignment (1, (float) 0.5);
278         var username_entry = new Entry ();
279         var password_label = new Label (_("Password:"));
280         password_label.set_alignment (1, (float) 0.5);
281         var password_entry = new Entry ();
282         password_entry.set_invisible_char ('*');
283         password_entry.set_visibility (false);
284         var remember_checkbutton = new CheckButton.with_label (_("Remember password"));
285         var login_table = new Table (3, 3, false);
286         login_table.set_col_spacings (10);
287         login_table.set_row_spacings (10);
288         login_table.attach_defaults (username_label, 0, 1, 0, 1);
289         login_table.attach_defaults (username_entry, 1, 2, 0, 1);
290         login_table.attach_defaults (password_label, 0, 1, 1, 2);
291         login_table.attach_defaults (password_entry, 1, 2, 1, 2);
292         login_table.attach_defaults (remember_checkbutton,  1, 2, 2, 3);
293         var login_vbox_alignment = new Alignment (0, 0, 0, 0);
294         login_vbox_alignment.set_padding (0, 0, 12, 0);
295         login_vbox_alignment.add (login_table);
296         var login_vbox = new VBox (false, 6);
297         login_vbox.pack_start (login_vbox_title, false, true, 0);
298         login_vbox.pack_start (login_vbox_alignment, false, true, 0);
299
300         var services_vbox_title = new Label (_("Services:"));
301         label_make_bold (services_vbox_title);
302         services_vbox_title.set_alignment (0, (float) 0.5);
303         var email_label = new Label (_("Email"));
304         var email_remove_button = new Button.from_stock (Stock.REMOVE);
305         var im_label = new Label (_("IM"));
306         var im_remove_button = new Button.from_stock (Stock.REMOVE);
307         var services_table = new Table (2, 2, false);
308         services_table.set_col_spacings (10);
309         services_table.set_row_spacings (10);
310         services_table.attach_defaults (email_label, 0, 1, 0, 1);
311         services_table.attach_defaults (email_remove_button, 1, 2, 0, 1);
312         services_table.attach_defaults (im_label, 0, 1, 1, 2);
313         services_table.attach_defaults (im_remove_button, 1, 2, 1, 2);
314         var services_vbox_alignment = new Alignment (0, 0, 0, 0);
315         services_vbox_alignment.set_padding (0, 0, 12, 0);
316         services_vbox_alignment.add (services_table);
317         var services_vbox = new VBox (false, 6);
318         services_vbox.pack_start (services_vbox_title, false, true, 0);
319         services_vbox.pack_start (services_vbox_alignment, false, true, 0);
320
321         var vbox_rigth = new VBox (false, 18);
322         vbox_rigth.pack_start (login_vbox, false, true, 0);
323         vbox_rigth.pack_start (services_vbox, false, true, 0);
324
325         var hbox = new HBox (false, 12);
326         hbox.pack_start (vbox_left, true, true, 0);
327         hbox.pack_start (vbox_rigth, true, true, 0);
328
329         var send_button = new Button.with_label ("Send");
330         var hbox_send_button = new HBox (false, 0);
331         hbox_send_button.pack_end (send_button, false, false, 0);
332
333         var main_vbox = new VBox (false, 12);
334         main_vbox.pack_start (toolbar, false, true, 0);
335         main_vbox.pack_start (hbox, true, true, 0);
336         main_vbox.pack_start (hbox_send_button, false, false, 0);
337         main_vbox.set_border_width (12);
338         add (main_vbox);
339     }
340
341     private void connect_signals()
342     {
343         this.destroy.connect (Gtk.main_quit);
344     }
345
346     public static int main(string[] args)
347     {
348         Gtk.init(ref args);
349
350         Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
351         Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
352         Intl.textdomain (Config.GETTEXT_PACKAGE);
353
354         var window = new MainWindow();
355         window.show_all();
356
357         Gtk.main();
358
359         return 0;
360     }
361 }