Move the ListStore to the CustomVBox object
[moonshot-ui.git] / src / moonshot-window.vala
1 using Gtk;
2
3 class MainWindow : Window
4 {
5     private const int WINDOW_WIDTH = 400;
6     private const int WINDOW_HEIGHT = 500;
7
8     private Entry search_entry;
9     private VBox vbox_rigth;
10     private CustomVBox custom_vbox;
11
12     private Entry username_entry;
13     private Entry password_entry;
14
15     private ListStore listmodel;
16
17     private enum Columns
18     {
19         IDCARD_COL,
20         LOGO_COL,
21         ISSUER_COL,
22         USERNAME_COL,
23         PASSWORD_COL,
24         N_COLUMNS
25     }
26
27     public MainWindow()
28     {
29         this.title = "Moonshoot";
30         this.position = WindowPosition.CENTER;
31         set_default_size (WINDOW_WIDTH, WINDOW_HEIGHT);
32
33         build_ui();
34         setup_identities_list();
35         connect_signals();
36     }
37
38     private void search_entry_icon_press_cb (EntryIconPosition pos, Gdk.Event event)
39     {
40         if (pos == EntryIconPosition.PRIMARY)
41         {
42             print ("Search entry icon pressed\n");
43         }
44         else
45         {
46             this.search_entry.set_text ("");
47         }
48     }
49
50     private void search_entry_text_changed_cb ()
51     {
52         var has_text = this.search_entry.get_text_length () > 0;
53         this.search_entry.set_icon_sensitive (EntryIconPosition.PRIMARY, has_text);
54         this.search_entry.set_icon_sensitive (EntryIconPosition.SECONDARY, has_text);
55
56         this.vbox_rigth.set_visible (false);
57         this.resize (WINDOW_WIDTH, WINDOW_HEIGHT);
58     }
59
60     private bool search_entry_key_press_event_cb (Gdk.EventKey e)
61     {
62         if(Gdk.keyval_name(e.keyval) == "Escape")
63            this.search_entry.set_text("");
64
65         // Continue processing this event, since the
66         // text entry functionality needs to see it too.
67         return false;
68     }
69
70     private void setup_identities_list ()
71     {
72         this.listmodel = new ListStore (Columns.N_COLUMNS, typeof (IdCard),
73                                                            typeof (Gdk.Pixbuf),
74                                                            typeof (string),
75                                                            typeof (string),
76                                                            typeof (string));
77     }
78
79     private void fill_details (IdCardWidget id_card_widget)
80     {
81        var id_card = id_card_widget.id_card;
82        this.username_entry.set_text (id_card.username);
83        this.password_entry.set_text (id_card.password);
84     }
85
86     private void show_details (IdCard id_card)
87     {
88        this.vbox_rigth.set_visible (!vbox_rigth.get_visible ());
89
90        if (this.vbox_rigth.get_visible () == false)
91        {
92            this.resize (WINDOW_WIDTH, WINDOW_HEIGHT);
93        }
94     }
95
96     private void details_identity_cb (IdCardWidget id_card_widget)
97     {
98        fill_details (id_card_widget);
99        show_details (id_card_widget.id_card);
100     }
101
102     private IdCard get_id_card_data (AddIdentityDialog dialog)
103     {
104         var id_card = new IdCard ();
105
106         id_card.issuer = dialog.issuer;
107         id_card.username = dialog.username;
108         id_card.password = dialog.password;
109
110         var icon_theme = IconTheme.get_default ();
111         try
112         {
113             id_card.pixbuf = icon_theme.load_icon ("avatar-default",
114                                                    48,
115                                                    IconLookupFlags.FORCE_SIZE);
116         }
117         catch (Error e)
118         {
119             id_card.pixbuf = null;
120             stdout.printf("Error: %s\n", e.message);
121         }
122
123         id_card.services = {"email","jabber","irc"};
124
125         return id_card;
126     }
127
128     private void add_identity (AddIdentityDialog dialog)
129     {
130         var id_card = get_id_card_data (dialog);
131
132         var id_card_widget = new IdCardWidget (id_card);
133
134         this.custom_vbox.add_id_card_widget (id_card_widget);
135
136         id_card_widget.details_id.connect (details_identity_cb);
137         id_card_widget.remove_id.connect (remove_identity_cb);
138         id_card_widget.expanded.connect (this.custom_vbox.receive_expanded_event);
139         id_card_widget.expanded.connect (fill_details);
140     }
141
142     private void add_identity_cb ()
143     {
144         var dialog = new AddIdentityDialog ();
145         var result = dialog.run ();
146
147         switch (result) {
148         case ResponseType.OK:
149             add_identity (dialog);
150             break;
151         default:
152             break;
153         }
154         dialog.destroy ();
155     }
156
157     private void remove_identity (IdCardWidget id_card_widget)
158     {
159         this.custom_vbox.remove_id_card_widget (id_card_widget);
160     }
161
162     private void remove_identity_cb (IdCardWidget id_card_widget)
163     {
164         var id_card = id_card_widget.id_card;
165
166         var dialog = new MessageDialog (null,
167                                         DialogFlags.DESTROY_WITH_PARENT,
168                                         MessageType.INFO,
169                                         Gtk.ButtonsType.YES_NO,
170                                         _("Are you sure you want to delete %s ID Card?"), id_card.issuer);
171         var result = dialog.run ();
172         switch (result) {
173         case ResponseType.YES:
174             remove_identity (id_card_widget);
175             break;
176         default:
177             break;
178         }
179         dialog.destroy ();
180     }
181
182     private void label_make_bold (Label label)
183     {
184         var font_desc = new Pango.FontDescription ();
185
186         font_desc.set_weight (Pango.Weight.BOLD);
187
188         /* This will only affect the weight of the font, the rest is
189          * from the current state of the widget, which comes from the
190          * theme or user prefs, since the font desc only has the
191          * weight flag turned on.
192          */
193         label.modify_font (font_desc);
194     }
195
196     private void build_ui()
197     {
198         this.search_entry = new Entry();
199
200         this.search_entry.set_icon_from_icon_name (EntryIconPosition.PRIMARY,
201                                                    "edit-find-symbolic");
202         this.search_entry.set_icon_sensitive (EntryIconPosition.PRIMARY, false);
203         this.search_entry.set_icon_tooltip_text (EntryIconPosition.PRIMARY,
204                                                  _("Search identity or service"));
205
206         this.search_entry.set_icon_from_icon_name (EntryIconPosition.SECONDARY,
207                                                    "edit-clear-symbolic");
208         this.search_entry.set_icon_sensitive (EntryIconPosition.SECONDARY, false);
209         this.search_entry.set_icon_tooltip_text (EntryIconPosition.SECONDARY,
210                                                  _("Clear the current search"));
211
212         this.search_entry.icon_press.connect (search_entry_icon_press_cb);
213         this.search_entry.notify["text"].connect (search_entry_text_changed_cb);
214         this.search_entry.key_press_event.connect(search_entry_key_press_event_cb);
215
216         this.custom_vbox = new CustomVBox (false, 6);
217
218         var viewport = new Viewport (null, null);
219         viewport.set_border_width (6);
220         viewport.set_shadow_type (ShadowType.NONE);
221         viewport.add (custom_vbox);
222         var scroll = new ScrolledWindow (null, null);
223         scroll.set_policy (PolicyType.NEVER, PolicyType.AUTOMATIC);
224         scroll.set_shadow_type (ShadowType.IN);
225         scroll.add_with_viewport (viewport);
226
227         var button_add = new ToolButton (null, null);
228         button_add.set_icon_name ("list-add-symbolic");
229         button_add.clicked.connect (add_identity_cb);
230         var button_toolbar = new Toolbar ();
231         button_toolbar.insert (button_add, 0);
232
233         var vbox_left = new VBox (false, 0);
234         vbox_left.pack_start (search_entry, false, false, 6);
235         vbox_left.pack_start (scroll, true, true, 0);
236         vbox_left.pack_start (button_toolbar, false, false, 0);
237         vbox_left.set_size_request (WINDOW_WIDTH, 0);
238
239         var login_vbox_title = new Label (_("Login: "));
240         label_make_bold (login_vbox_title);
241         login_vbox_title.set_alignment (0, (float) 0.5);
242         var username_label = new Label (_("Username:"));
243         username_label.set_alignment (1, (float) 0.5);
244         this.username_entry = new Entry ();
245         var password_label = new Label (_("Password:"));
246         password_label.set_alignment (1, (float) 0.5);
247         this.password_entry = new Entry ();
248         password_entry.set_invisible_char ('*');
249         password_entry.set_visibility (false);
250         var remember_checkbutton = new CheckButton.with_label (_("Remember password"));
251         var login_table = new Table (3, 3, false);
252         login_table.set_col_spacings (10);
253         login_table.set_row_spacings (10);
254         login_table.attach_defaults (username_label, 0, 1, 0, 1);
255         login_table.attach_defaults (username_entry, 1, 2, 0, 1);
256         login_table.attach_defaults (password_label, 0, 1, 1, 2);
257         login_table.attach_defaults (password_entry, 1, 2, 1, 2);
258         login_table.attach_defaults (remember_checkbutton,  1, 2, 2, 3);
259         var login_vbox_alignment = new Alignment (0, 0, 0, 0);
260         login_vbox_alignment.set_padding (0, 0, 12, 0);
261         login_vbox_alignment.add (login_table);
262         var login_vbox = new VBox (false, 6);
263         login_vbox.pack_start (login_vbox_title, false, true, 0);
264         login_vbox.pack_start (login_vbox_alignment, false, true, 0);
265
266         var services_vbox_title = new Label (_("Services:"));
267         label_make_bold (services_vbox_title);
268         services_vbox_title.set_alignment (0, (float) 0.5);
269         var email_label = new Label (_("Email"));
270         var email_remove_button = new Button.from_stock (Stock.REMOVE);
271         var im_label = new Label (_("IM"));
272         var im_remove_button = new Button.from_stock (Stock.REMOVE);
273         var services_table = new Table (2, 2, false);
274         services_table.set_col_spacings (10);
275         services_table.set_row_spacings (10);
276         services_table.attach_defaults (email_label, 0, 1, 0, 1);
277         services_table.attach_defaults (email_remove_button, 1, 2, 0, 1);
278         services_table.attach_defaults (im_label, 0, 1, 1, 2);
279         services_table.attach_defaults (im_remove_button, 1, 2, 1, 2);
280         var services_vbox_alignment = new Alignment (0, 0, 0, 0);
281         services_vbox_alignment.set_padding (0, 0, 12, 0);
282         services_vbox_alignment.add (services_table);
283         var services_vbox = new VBox (false, 6);
284         services_vbox.pack_start (services_vbox_title, false, true, 0);
285         services_vbox.pack_start (services_vbox_alignment, false, true, 0);
286
287         this.vbox_rigth = new VBox (false, 18);
288         vbox_rigth.pack_start (login_vbox, false, true, 0);
289         vbox_rigth.pack_start (services_vbox, false, true, 0);
290
291         var hbox = new HBox (false, 12);
292         hbox.pack_start (vbox_left, false, false, 0);
293         hbox.pack_start (vbox_rigth, false, false, 0);
294
295         var main_vbox = new VBox (false, 12);
296         main_vbox.pack_start (hbox, true, true, 0);
297         main_vbox.set_border_width (12);
298         add (main_vbox);
299
300         main_vbox.show_all();
301         this.vbox_rigth.hide ();
302     }
303
304     private void connect_signals()
305     {
306         this.destroy.connect (Gtk.main_quit);
307     }
308
309     public static int main(string[] args)
310     {
311         Gtk.init(ref args);
312
313         Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
314         Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
315         Intl.textdomain (Config.GETTEXT_PACKAGE);
316
317         var window = new MainWindow();
318         window.show ();
319
320         Gtk.main();
321
322         return 0;
323     }
324 }