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