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