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