Set margins in account window.
[modest] / src / hildon2 / modest-accounts-window.c
1 /* Copyright (c) 2008, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <modest-accounts-window.h>
31 #include <modest-osso-state-saving.h>
32 #include <libosso.h>
33 #include <hildon/hildon-pannable-area.h>
34 #include <hildon/hildon-banner.h>
35 #include <modest-ui-actions.h>
36 #include <modest-window-mgr.h>
37 #include <modest-signal-mgr.h>
38 #include <modest-runtime.h>
39 #include <modest-platform.h>
40 #include <hildon/hildon-program.h>
41 #include <hildon/hildon-button.h>
42 #include <modest-maemo-utils.h>
43 #include <modest-icon-names.h>
44 #include <modest-defs.h>
45 #include <modest-folder-window.h>
46 #include <modest-ui-dimming-rules.h>
47 #include <modest-ui-dimming-manager.h>
48 #include <modest-window-priv.h>
49 #include <modest-ui-constants.h>
50 #include <modest-account-mgr-helpers.h>
51 #include <modest-mailboxes-window.h>
52
53
54 /* 'private'/'protected' functions */
55 static void modest_accounts_window_class_init  (ModestAccountsWindowClass *klass);
56 static void modest_accounts_window_instance_init (ModestAccountsWindow *obj);
57 static void modest_accounts_window_finalize    (GObject *obj);
58
59 static void connect_signals (ModestAccountsWindow *self);
60 static void modest_accounts_window_disconnect_signals (ModestWindow *self);
61
62 static void on_account_activated (GtkTreeView *treeview,
63                                   GtkTreePath *path,
64                                   GtkTreeViewColumn *column,
65                                   ModestAccountsWindow *accounts_window);
66 static void on_progress_list_changed (ModestWindowMgr *mgr,
67                                       ModestAccountsWindow *self);
68 static void setup_menu (ModestAccountsWindow *self);
69 static gboolean _modest_accounts_window_map_event (GtkWidget *widget,
70                                                    GdkEvent *event,
71                                                    gpointer userdata);
72 static void update_progress_hint (ModestAccountsWindow *self);
73 static void on_row_inserted (GtkTreeModel *tree_model,
74                              GtkTreePath  *path,
75                              GtkTreeIter  *iter,
76                              gpointer      user_data);
77 static void on_row_deleted (GtkTreeModel *tree_model,
78                             GtkTreePath  *path,
79                             gpointer      user_data);
80 static void row_count_changed (ModestAccountsWindow *self);
81
82 typedef struct _ModestAccountsWindowPrivate ModestAccountsWindowPrivate;
83 struct _ModestAccountsWindowPrivate {
84
85         GtkWidget *box;
86         GtkWidget *pannable;
87         GtkWidget *account_view;
88         GtkWidget *no_accounts_container;
89         GtkWidget *new_message_button;
90
91         /* signals */
92         GSList *sighandlers;
93
94         gboolean progress_hint;
95 };
96 #define MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE((o), \
97                                                                             MODEST_TYPE_ACCOUNTS_WINDOW, \
98                                                                             ModestAccountsWindowPrivate))
99
100 /* globals */
101 static GtkWindowClass *parent_class = NULL;
102
103 /************************************************************************/
104
105 GType
106 modest_accounts_window_get_type (void)
107 {
108         static GType my_type = 0;
109         if (!my_type) {
110                 static const GTypeInfo my_info = {
111                         sizeof(ModestAccountsWindowClass),
112                         NULL,           /* base init */
113                         NULL,           /* base finalize */
114                         (GClassInitFunc) modest_accounts_window_class_init,
115                         NULL,           /* class finalize */
116                         NULL,           /* class data */
117                         sizeof(ModestAccountsWindow),
118                         1,              /* n_preallocs */
119                         (GInstanceInitFunc) modest_accounts_window_instance_init,
120                         NULL
121                 };
122                 my_type = g_type_register_static (MODEST_TYPE_HILDON2_WINDOW,
123                                                   "ModestAccountsWindow",
124                                                   &my_info, 0);
125         }
126         return my_type;
127 }
128
129 static void
130 modest_accounts_window_class_init (ModestAccountsWindowClass *klass)
131 {
132         GObjectClass *gobject_class;
133         gobject_class = (GObjectClass*) klass;
134         ModestWindowClass *modest_window_class = (ModestWindowClass *) klass;
135
136         parent_class            = g_type_class_peek_parent (klass);
137         gobject_class->finalize = modest_accounts_window_finalize;
138
139         g_type_class_add_private (gobject_class, sizeof(ModestAccountsWindowPrivate));
140         
141         modest_window_class->disconnect_signals_func = modest_accounts_window_disconnect_signals;
142 }
143
144 static void
145 modest_accounts_window_instance_init (ModestAccountsWindow *obj)
146 {
147         ModestAccountsWindowPrivate *priv;
148
149         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(obj);
150
151         priv->sighandlers = NULL;
152         
153         priv->account_view = NULL;
154         priv->progress_hint = FALSE;
155         
156         modest_window_mgr_register_help_id (modest_runtime_get_window_mgr(),
157                                             GTK_WINDOW(obj),
158                                             "applications_email_accountsview");
159 }
160
161 static void
162 modest_accounts_window_finalize (GObject *obj)
163 {
164         ModestAccountsWindowPrivate *priv;
165
166         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(obj);
167
168         /* Sanity check: shouldn't be needed, the window mgr should
169            call this function before */
170         modest_accounts_window_disconnect_signals (MODEST_WINDOW (obj));        
171
172         G_OBJECT_CLASS(parent_class)->finalize (obj);
173 }
174
175 static void
176 modest_accounts_window_disconnect_signals (ModestWindow *self)
177 {       
178         ModestAccountsWindowPrivate *priv;      
179         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
180
181         modest_signal_mgr_disconnect_all_and_destroy (priv->sighandlers);
182         priv->sighandlers = NULL;       
183 }
184
185 static void
186 connect_signals (ModestAccountsWindow *self)
187 {
188         ModestAccountsWindowPrivate *priv;
189         GtkTreeModel *model;
190
191         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
192
193         /* accounts view */
194         priv->sighandlers = 
195                 modest_signal_mgr_connect (priv->sighandlers,
196                                            G_OBJECT (priv->account_view), "row-activated", 
197                                            G_CALLBACK (on_account_activated), self);
198
199         priv->sighandlers = 
200                 modest_signal_mgr_connect (priv->sighandlers,
201                                            G_OBJECT (modest_runtime_get_window_mgr ()),
202                                            "progress-list-changed",
203                                            G_CALLBACK (on_progress_list_changed), self);
204
205         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->account_view));
206
207         priv->sighandlers =
208                 modest_signal_mgr_connect (priv->sighandlers,
209                                            G_OBJECT (model),
210                                            "row-inserted",
211                                            G_CALLBACK (on_row_inserted), self);
212
213         priv->sighandlers =
214                 modest_signal_mgr_connect (priv->sighandlers,
215                                            G_OBJECT (model),
216                                            "row-deleted",
217                                            G_CALLBACK (on_row_deleted), self);
218
219         priv->sighandlers = 
220                 modest_signal_mgr_connect (priv->sighandlers,
221                                            G_OBJECT (priv->new_message_button),
222                                            "clicked",
223                                            G_CALLBACK (modest_ui_actions_on_new_msg), self);
224
225         /* we don't register this in sighandlers, as it should be run
226          * after disconnecting all signals, in destroy stage */
227 }
228
229 ModestWindow *
230 modest_accounts_window_new (void)
231 {
232         ModestAccountsWindow *self = NULL;
233         ModestAccountsWindowPrivate *priv = NULL;
234         HildonProgram *app;
235         GdkPixbuf *window_icon;
236         GdkPixbuf *new_message_pixbuf;
237         GtkWidget *action_area_box;
238         guint accel_key;
239         GdkModifierType accel_mods;
240         GtkAccelGroup *accel_group;
241         GtkWidget *no_accounts_label;
242         GtkWidget *empty_view_new_message_button;
243         GtkWidget *box_alignment;
244
245         self  = MODEST_ACCOUNTS_WINDOW(g_object_new(MODEST_TYPE_ACCOUNTS_WINDOW, NULL));
246         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
247
248         new_message_pixbuf = modest_platform_get_icon ("general_add", MODEST_ICON_SIZE_BIG);
249
250         box_alignment = gtk_alignment_new (0, 0, 1.0, 1.0);
251         gtk_alignment_set_padding (GTK_ALIGNMENT (box_alignment), 
252                                    0, 0,
253                                    HILDON_MARGIN_DOUBLE, HILDON_MARGIN_DOUBLE);
254         priv->box = gtk_vbox_new (FALSE, 0);
255
256         no_accounts_label = gtk_label_new (_("mcen_ia_noaccounts"));
257         gtk_misc_set_alignment (GTK_MISC (no_accounts_label), 0.5, 0.0);
258
259         empty_view_new_message_button = hildon_button_new (MODEST_EDITABLE_SIZE, HILDON_BUTTON_ARRANGEMENT_HORIZONTAL);
260         hildon_button_set_title (HILDON_BUTTON (empty_view_new_message_button), _("mcen_ti_new_message"));
261         hildon_button_set_image (HILDON_BUTTON (empty_view_new_message_button), gtk_image_new_from_pixbuf (new_message_pixbuf));
262
263
264         priv->no_accounts_container = gtk_vbox_new (FALSE, 0);
265         gtk_box_pack_start (GTK_BOX (priv->no_accounts_container), empty_view_new_message_button, FALSE, FALSE, 0);
266         gtk_widget_show_all (empty_view_new_message_button);
267         gtk_box_pack_end (GTK_BOX (priv->no_accounts_container), no_accounts_label, TRUE, TRUE, 0);
268         gtk_widget_show (no_accounts_label);
269         gtk_box_pack_start (GTK_BOX (priv->box), priv->no_accounts_container, TRUE, TRUE, 0);
270
271         g_signal_connect (G_OBJECT (empty_view_new_message_button),
272                           "clicked",
273                           G_CALLBACK (modest_ui_actions_on_new_msg), self);
274         
275         priv->pannable = hildon_pannable_area_new ();
276         priv->account_view  = GTK_WIDGET (modest_account_view_new (modest_runtime_get_account_mgr ()));
277
278         action_area_box = hildon_tree_view_get_action_area_box (GTK_TREE_VIEW (priv->account_view));
279         priv->new_message_button = hildon_button_new (0, HILDON_BUTTON_ARRANGEMENT_HORIZONTAL);
280
281         hildon_button_set_title (HILDON_BUTTON (priv->new_message_button), _("mcen_ti_new_message"));
282         hildon_button_set_image (HILDON_BUTTON (priv->new_message_button), gtk_image_new_from_pixbuf (new_message_pixbuf));
283
284         gtk_box_pack_start (GTK_BOX (action_area_box), priv->new_message_button, TRUE, TRUE, 0);
285         gtk_widget_show_all (priv->new_message_button);
286         hildon_tree_view_set_action_area_visible (GTK_TREE_VIEW (priv->account_view), TRUE);
287
288         g_object_unref (new_message_pixbuf);
289         setup_menu (self);
290
291         gtk_container_add (GTK_CONTAINER (priv->pannable), priv->account_view);
292         gtk_box_pack_start (GTK_BOX (priv->box), priv->pannable, TRUE, TRUE, 0);
293         gtk_container_add (GTK_CONTAINER (box_alignment), priv->box);
294         gtk_container_add (GTK_CONTAINER (self), box_alignment);
295
296         gtk_widget_show (priv->account_view);
297         gtk_widget_show (priv->pannable);
298         gtk_widget_show (priv->box);
299         gtk_widget_show (box_alignment);
300
301         connect_signals (MODEST_ACCOUNTS_WINDOW (self));
302
303         /* Load previous osso state, for instance if we are being restored from 
304          * hibernation:  */
305         modest_osso_load_state ();
306
307         /* Get device name */
308         modest_maemo_utils_get_device_name ();
309
310         app = hildon_program_get_instance ();
311         hildon_program_add_window (app, HILDON_WINDOW (self));
312         
313         /* Set window icon */
314         window_icon = modest_platform_get_icon (MODEST_APP_ICON, MODEST_ICON_SIZE_BIG);
315         if (window_icon) {
316                 gtk_window_set_icon (GTK_WINDOW (self), window_icon);
317                 g_object_unref (window_icon);
318         }
319
320         /* Dont't restore settings here, 
321          * because it requires a gtk_widget_show(), 
322          * and we don't want to do that until later,
323          * so that the UI is not visible for non-menu D-Bus activation.
324          */
325
326         g_signal_connect (G_OBJECT (self), "map-event",
327                           G_CALLBACK (_modest_accounts_window_map_event),
328                           G_OBJECT (self));
329         update_progress_hint (self);
330
331         row_count_changed (self);
332
333         accel_group = gtk_accel_group_new ();
334         gtk_accelerator_parse ("<Control>n", &accel_key, &accel_mods);
335         gtk_widget_add_accelerator (priv->new_message_button, "clicked", accel_group,
336                                     accel_key, accel_mods, 0);
337         gtk_window_add_accel_group (GTK_WINDOW (self), accel_group);
338
339         return MODEST_WINDOW(self);
340 }
341
342 ModestAccountView *
343 modest_accounts_window_get_account_view (ModestAccountsWindow *self)
344 {
345         ModestAccountsWindowPrivate *priv = NULL;
346
347         g_return_val_if_fail (MODEST_IS_ACCOUNTS_WINDOW(self), FALSE);
348
349         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
350         
351         return MODEST_ACCOUNT_VIEW (priv->account_view);
352 }
353
354 static void 
355 setup_menu (ModestAccountsWindow *self)
356 {
357         g_return_if_fail (MODEST_IS_ACCOUNTS_WINDOW(self));
358
359         /* Settings menu buttons */
360         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_new_account"), NULL, 
361                                            APP_MENU_CALLBACK (modest_ui_actions_on_new_account), 
362                                            NULL);
363         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_edit_accounts"), NULL,
364                                            APP_MENU_CALLBACK (modest_ui_actions_on_accounts), 
365                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_edit_accounts));
366         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_globalsmtpservers"), NULL,
367                                            APP_MENU_CALLBACK (modest_ui_actions_on_smtp_servers),
368                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_tools_smtp_servers));
369         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_sendandreceive"), NULL,
370                                            APP_MENU_CALLBACK (modest_ui_actions_on_send_receive),
371                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_send_receive_all));
372         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_outbox_cancelsend"), NULL,
373                                            APP_MENU_CALLBACK (modest_ui_actions_cancel_send),
374                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_cancel_sending_all));
375         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_options"), NULL,
376                                            APP_MENU_CALLBACK (modest_ui_actions_on_settings), 
377                                            NULL);
378 }
379
380
381 static void
382 on_account_activated (GtkTreeView *account_view,
383                       GtkTreePath *path,
384                       GtkTreeViewColumn *column,
385                       ModestAccountsWindow *self)
386 {
387         ModestAccountsWindowPrivate *priv;
388         gchar* account_name; 
389         GtkWidget *new_window;
390         gboolean registered;
391         ModestProtocolType store_protocol;
392         gboolean mailboxes_protocol;
393
394         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
395
396         account_name = modest_account_view_get_path_account (MODEST_ACCOUNT_VIEW (priv->account_view), path);
397         if (!account_name)
398                 return;
399
400         /* If it's a multimailbox container, we have to show the mailboxes window */
401         store_protocol = modest_account_mgr_get_store_protocol (modest_runtime_get_account_mgr (), 
402                                                                 account_name);
403         mailboxes_protocol = 
404                 modest_protocol_registry_protocol_type_has_tag (modest_runtime_get_protocol_registry (),
405                                                                 store_protocol,
406                                                                 MODEST_PROTOCOL_REGISTRY_MULTI_MAILBOX_PROVIDER_PROTOCOLS);
407         if (mailboxes_protocol) {
408                 new_window = GTK_WIDGET (modest_mailboxes_window_new (account_name));
409         } else {
410
411                 new_window = GTK_WIDGET (modest_folder_window_new (NULL));
412         }
413
414         registered = modest_window_mgr_register_window (modest_runtime_get_window_mgr (), 
415                                                         MODEST_WINDOW (new_window),
416                                                         MODEST_WINDOW (self));
417
418         if (!registered) {
419                 gtk_widget_destroy (new_window);
420                 new_window = NULL;
421         } else {
422                 if (!mailboxes_protocol) {
423                         modest_folder_window_set_account (MODEST_FOLDER_WINDOW (new_window), account_name);
424                 }
425                 gtk_widget_show (new_window);
426         }
427         g_free (account_name);
428 }
429
430 static gboolean
431 _modest_accounts_window_map_event (GtkWidget *widget,
432                                    GdkEvent *event,
433                                    gpointer userdata)
434 {
435         ModestAccountsWindow *self = (ModestAccountsWindow *) userdata;
436         ModestAccountsWindowPrivate *priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
437
438         if (priv->progress_hint) {
439                 hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), TRUE);
440         }
441
442         return FALSE;
443 }
444
445 static void
446 update_progress_hint (ModestAccountsWindow *self)
447 {
448         ModestAccountsWindowPrivate *priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
449
450         priv->progress_hint = modest_window_mgr_has_progress_operation (modest_runtime_get_window_mgr ());
451         
452         if (GTK_WIDGET_VISIBLE (self)) {
453                 hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), priv->progress_hint?1:0);
454         }
455 }
456
457 static void
458 on_progress_list_changed (ModestWindowMgr *mgr,
459                           ModestAccountsWindow *self)
460 {
461         update_progress_hint (self);
462 }
463
464 static void
465 on_row_inserted (GtkTreeModel *tree_model,
466                  GtkTreePath  *path,
467                  GtkTreeIter  *iter,
468                  gpointer      user_data)
469 {
470         ModestAccountsWindow *self;
471
472         self = (ModestAccountsWindow *) user_data;
473
474         row_count_changed (self);
475 }
476
477 static void
478 on_row_deleted (GtkTreeModel *tree_model,
479                 GtkTreePath  *path,
480                 gpointer      user_data)
481 {
482         ModestAccountsWindow *self;
483
484         self = (ModestAccountsWindow *) user_data;
485
486         row_count_changed (self);
487 }
488
489 static void row_count_changed (ModestAccountsWindow *self)
490 {
491         ModestAccountsWindowPrivate *priv;
492         GtkTreeModel *model;
493         gint count;
494         
495         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
496         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->account_view));
497
498         count = gtk_tree_model_iter_n_children (model, NULL);
499
500         if (count == 0) {
501                 gtk_widget_show (priv->no_accounts_container);
502         } else {
503                 gtk_widget_hide (priv->no_accounts_container);
504         }
505         gtk_container_child_set (GTK_CONTAINER(priv->box), priv->pannable, 
506                                  "expand", count > 0,
507                                  "fill", count > 0,
508                                  NULL);
509 }