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