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