Launch mailboxes window on multimailboxes accounts
[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
244         self  = MODEST_ACCOUNTS_WINDOW(g_object_new(MODEST_TYPE_ACCOUNTS_WINDOW, NULL));
245         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
246
247         new_message_pixbuf = modest_platform_get_icon ("general_add", MODEST_ICON_SIZE_BIG);
248
249         priv->box = gtk_vbox_new (FALSE, 0);
250
251         no_accounts_label = gtk_label_new (_("mcen_ia_noaccounts"));
252         gtk_misc_set_alignment (GTK_MISC (no_accounts_label), 0.5, 0.0);
253
254         empty_view_new_message_button = hildon_button_new (MODEST_EDITABLE_SIZE, HILDON_BUTTON_ARRANGEMENT_HORIZONTAL);
255         hildon_button_set_title (HILDON_BUTTON (empty_view_new_message_button), _("mcen_ti_new_message"));
256         hildon_button_set_image (HILDON_BUTTON (empty_view_new_message_button), gtk_image_new_from_pixbuf (new_message_pixbuf));
257
258
259         priv->no_accounts_container = gtk_vbox_new (FALSE, 0);
260         gtk_box_pack_start (GTK_BOX (priv->no_accounts_container), empty_view_new_message_button, FALSE, FALSE, 0);
261         gtk_widget_show_all (empty_view_new_message_button);
262         gtk_box_pack_end (GTK_BOX (priv->no_accounts_container), no_accounts_label, TRUE, TRUE, 0);
263         gtk_widget_show (no_accounts_label);
264         gtk_box_pack_start (GTK_BOX (priv->box), priv->no_accounts_container, TRUE, TRUE, 0);
265
266         g_signal_connect (G_OBJECT (empty_view_new_message_button),
267                           "clicked",
268                           G_CALLBACK (modest_ui_actions_on_new_msg), self);
269         
270         priv->pannable = hildon_pannable_area_new ();
271         priv->account_view  = GTK_WIDGET (modest_account_view_new (modest_runtime_get_account_mgr ()));
272
273         action_area_box = hildon_tree_view_get_action_area_box (GTK_TREE_VIEW (priv->account_view));
274         priv->new_message_button = hildon_button_new (0, HILDON_BUTTON_ARRANGEMENT_HORIZONTAL);
275
276         hildon_button_set_title (HILDON_BUTTON (priv->new_message_button), _("mcen_ti_new_message"));
277         hildon_button_set_image (HILDON_BUTTON (priv->new_message_button), gtk_image_new_from_pixbuf (new_message_pixbuf));
278
279         gtk_box_pack_start (GTK_BOX (action_area_box), priv->new_message_button, TRUE, TRUE, 0);
280         gtk_widget_show_all (priv->new_message_button);
281         hildon_tree_view_set_action_area_visible (GTK_TREE_VIEW (priv->account_view), TRUE);
282
283         g_object_unref (new_message_pixbuf);
284         setup_menu (self);
285
286         gtk_container_add (GTK_CONTAINER (priv->pannable), priv->account_view);
287         gtk_box_pack_start (GTK_BOX (priv->box), priv->pannable, TRUE, TRUE, 0);
288         gtk_container_add (GTK_CONTAINER (self), priv->box);
289
290         gtk_widget_show (priv->account_view);
291         gtk_widget_show (priv->pannable);
292         gtk_widget_show (priv->box);
293
294         connect_signals (MODEST_ACCOUNTS_WINDOW (self));
295
296         /* Load previous osso state, for instance if we are being restored from 
297          * hibernation:  */
298         modest_osso_load_state ();
299
300         /* Get device name */
301         modest_maemo_utils_get_device_name ();
302
303         app = hildon_program_get_instance ();
304         hildon_program_add_window (app, HILDON_WINDOW (self));
305         
306         /* Set window icon */
307         window_icon = modest_platform_get_icon (MODEST_APP_ICON, MODEST_ICON_SIZE_BIG);
308         if (window_icon) {
309                 gtk_window_set_icon (GTK_WINDOW (self), window_icon);
310                 g_object_unref (window_icon);
311         }
312
313         /* Dont't restore settings here, 
314          * because it requires a gtk_widget_show(), 
315          * and we don't want to do that until later,
316          * so that the UI is not visible for non-menu D-Bus activation.
317          */
318
319         g_signal_connect (G_OBJECT (self), "map-event",
320                           G_CALLBACK (_modest_accounts_window_map_event),
321                           G_OBJECT (self));
322         update_progress_hint (self);
323
324         row_count_changed (self);
325
326         accel_group = gtk_accel_group_new ();
327         gtk_accelerator_parse ("<Control>n", &accel_key, &accel_mods);
328         gtk_widget_add_accelerator (priv->new_message_button, "clicked", accel_group,
329                                     accel_key, accel_mods, 0);
330         gtk_window_add_accel_group (GTK_WINDOW (self), accel_group);
331
332         return MODEST_WINDOW(self);
333 }
334
335 ModestAccountView *
336 modest_accounts_window_get_account_view (ModestAccountsWindow *self)
337 {
338         ModestAccountsWindowPrivate *priv = NULL;
339
340         g_return_val_if_fail (MODEST_IS_ACCOUNTS_WINDOW(self), FALSE);
341
342         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
343         
344         return MODEST_ACCOUNT_VIEW (priv->account_view);
345 }
346
347 static void 
348 setup_menu (ModestAccountsWindow *self)
349 {
350         g_return_if_fail (MODEST_IS_ACCOUNTS_WINDOW(self));
351
352         /* Settings menu buttons */
353         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_new_account"), NULL, 
354                                            APP_MENU_CALLBACK (modest_ui_actions_on_new_account), 
355                                            NULL);
356         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_edit_accounts"), NULL,
357                                            APP_MENU_CALLBACK (modest_ui_actions_on_accounts), 
358                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_edit_accounts));
359         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_globalsmtpservers"), NULL,
360                                            APP_MENU_CALLBACK (modest_ui_actions_on_smtp_servers),
361                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_tools_smtp_servers));
362         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_sendandreceive"), NULL,
363                                            APP_MENU_CALLBACK (modest_ui_actions_on_send_receive),
364                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_send_receive_all));
365         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_outbox_cancelsend"), NULL,
366                                            APP_MENU_CALLBACK (modest_ui_actions_cancel_send),
367                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_cancel_sending_all));
368         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_options"), NULL,
369                                            APP_MENU_CALLBACK (modest_ui_actions_on_settings), 
370                                            NULL);
371 }
372
373
374 static void
375 on_account_activated (GtkTreeView *account_view,
376                       GtkTreePath *path,
377                       GtkTreeViewColumn *column,
378                       ModestAccountsWindow *self)
379 {
380         ModestAccountsWindowPrivate *priv;
381         gchar* account_name; 
382         GtkWidget *new_window;
383         gboolean registered;
384         ModestProtocolType store_protocol;
385         gboolean mailboxes_protocol;
386
387         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
388
389         account_name = modest_account_view_get_path_account (MODEST_ACCOUNT_VIEW (priv->account_view), path);
390         if (!account_name)
391                 return;
392
393         /* If it's a multimailbox container, we have to show the mailboxes window */
394         store_protocol = modest_account_mgr_get_store_protocol (modest_runtime_get_account_mgr (), 
395                                                                 account_name);
396         mailboxes_protocol = 
397                 modest_protocol_registry_protocol_type_has_tag (modest_runtime_get_protocol_registry (),
398                                                                 store_protocol,
399                                                                 MODEST_PROTOCOL_REGISTRY_MULTI_MAILBOX_PROVIDER_PROTOCOLS);
400         if (mailboxes_protocol) {
401                 new_window = GTK_WIDGET (modest_mailboxes_window_new (account_name));
402         } else {
403
404                 new_window = GTK_WIDGET (modest_folder_window_new (NULL));
405         }
406
407         registered = modest_window_mgr_register_window (modest_runtime_get_window_mgr (), 
408                                                         MODEST_WINDOW (new_window),
409                                                         MODEST_WINDOW (self));
410
411         if (!registered) {
412                 gtk_widget_destroy (new_window);
413                 new_window = NULL;
414         } else {
415                 if (!mailboxes_protocol) {
416                         modest_folder_window_set_account (MODEST_FOLDER_WINDOW (new_window), account_name);
417                 }
418                 gtk_widget_show (new_window);
419         }
420         g_free (account_name);
421 }
422
423 static gboolean
424 _modest_accounts_window_map_event (GtkWidget *widget,
425                                    GdkEvent *event,
426                                    gpointer userdata)
427 {
428         ModestAccountsWindow *self = (ModestAccountsWindow *) userdata;
429         ModestAccountsWindowPrivate *priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
430
431         if (priv->progress_hint) {
432                 hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), TRUE);
433         }
434
435         return FALSE;
436 }
437
438 static void
439 update_progress_hint (ModestAccountsWindow *self)
440 {
441         ModestAccountsWindowPrivate *priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
442
443         priv->progress_hint = modest_window_mgr_has_progress_operation (modest_runtime_get_window_mgr ());
444         
445         if (GTK_WIDGET_VISIBLE (self)) {
446                 hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), priv->progress_hint?1:0);
447         }
448 }
449
450 static void
451 on_progress_list_changed (ModestWindowMgr *mgr,
452                           ModestAccountsWindow *self)
453 {
454         update_progress_hint (self);
455 }
456
457 static void
458 on_row_inserted (GtkTreeModel *tree_model,
459                  GtkTreePath  *path,
460                  GtkTreeIter  *iter,
461                  gpointer      user_data)
462 {
463         ModestAccountsWindow *self;
464
465         self = (ModestAccountsWindow *) user_data;
466
467         row_count_changed (self);
468 }
469
470 static void
471 on_row_deleted (GtkTreeModel *tree_model,
472                 GtkTreePath  *path,
473                 gpointer      user_data)
474 {
475         ModestAccountsWindow *self;
476
477         self = (ModestAccountsWindow *) user_data;
478
479         row_count_changed (self);
480 }
481
482 static void row_count_changed (ModestAccountsWindow *self)
483 {
484         ModestAccountsWindowPrivate *priv;
485         GtkTreeModel *model;
486         gint count;
487         
488         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
489         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->account_view));
490
491         count = gtk_tree_model_iter_n_children (model, NULL);
492
493         if (count == 0) {
494                 gtk_widget_show (priv->no_accounts_container);
495         } else {
496                 gtk_widget_hide (priv->no_accounts_container);
497         }
498         gtk_container_child_set (GTK_CONTAINER(priv->box), priv->pannable, 
499                                  "expand", count > 0,
500                                  "fill", count > 0,
501                                  NULL);
502 }