345ee437e15abd137cb1c61b797a81a80b189aa2
[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_queue_changed    (ModestMailOperationQueue *queue,
75                                  ModestMailOperation *mail_op,
76                                  ModestMailOperationQueueNotification type,
77                                  ModestAccountsWindow *self);
78 static void on_row_inserted (GtkTreeModel *tree_model,
79                              GtkTreePath  *path,
80                              GtkTreeIter  *iter,
81                              gpointer      user_data);
82 static void on_row_deleted (GtkTreeModel *tree_model,
83                             GtkTreePath  *path,
84                             gpointer      user_data);
85 static void row_count_changed (ModestAccountsWindow *self);
86 static gboolean on_key_press(GtkWidget *widget,
87                                 GdkEventKey *event,
88                                 gpointer user_data);
89
90 typedef struct _ModestAccountsWindowPrivate ModestAccountsWindowPrivate;
91 struct _ModestAccountsWindowPrivate {
92
93         GtkWidget *box;
94         GtkWidget *pannable;
95         GtkWidget *account_view;
96         GtkWidget *no_accounts_container;
97         GtkWidget *new_message_button;
98
99         /* signals */
100         GSList *sighandlers;
101
102         gboolean progress_hint;
103         guint queue_change_handler;
104 };
105 #define MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE((o), \
106                                                                             MODEST_TYPE_ACCOUNTS_WINDOW, \
107                                                                             ModestAccountsWindowPrivate))
108
109 /* globals */
110 static GtkWindowClass *parent_class = NULL;
111 static GtkWidget *pre_created_accounts_window = NULL;
112
113 /************************************************************************/
114
115 GType
116 modest_accounts_window_get_type (void)
117 {
118         static GType my_type = 0;
119         if (!my_type) {
120                 static const GTypeInfo my_info = {
121                         sizeof(ModestAccountsWindowClass),
122                         NULL,           /* base init */
123                         NULL,           /* base finalize */
124                         (GClassInitFunc) modest_accounts_window_class_init,
125                         NULL,           /* class finalize */
126                         NULL,           /* class data */
127                         sizeof(ModestAccountsWindow),
128                         1,              /* n_preallocs */
129                         (GInstanceInitFunc) modest_accounts_window_instance_init,
130                         NULL
131                 };
132                 my_type = g_type_register_static (MODEST_TYPE_HILDON2_WINDOW,
133                                                   "ModestAccountsWindow",
134                                                   &my_info, 0);
135         }
136         return my_type;
137 }
138
139 static void
140 modest_accounts_window_class_init (ModestAccountsWindowClass *klass)
141 {
142         GObjectClass *gobject_class;
143         gobject_class = (GObjectClass*) klass;
144         ModestWindowClass *modest_window_class = (ModestWindowClass *) klass;
145
146         parent_class            = g_type_class_peek_parent (klass);
147         gobject_class->finalize = modest_accounts_window_finalize;
148
149         g_type_class_add_private (gobject_class, sizeof(ModestAccountsWindowPrivate));
150         
151         modest_window_class->disconnect_signals_func = modest_accounts_window_disconnect_signals;
152 }
153
154 static void
155 modest_accounts_window_instance_init (ModestAccountsWindow *obj)
156 {
157         ModestAccountsWindowPrivate *priv;
158
159         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(obj);
160
161         priv->sighandlers = NULL;
162         
163         priv->account_view = NULL;
164         priv->progress_hint = FALSE;
165         
166         modest_window_mgr_register_help_id (modest_runtime_get_window_mgr(),
167                                             GTK_WINDOW(obj),
168                                             "applications_email_accountsview");
169 }
170
171 static void
172 modest_accounts_window_finalize (GObject *obj)
173 {
174         ModestAccountsWindowPrivate *priv;
175
176         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(obj);
177
178         /* Sanity check: shouldn't be needed, the window mgr should
179            call this function before */
180         modest_accounts_window_disconnect_signals (MODEST_WINDOW (obj));        
181
182         G_OBJECT_CLASS(parent_class)->finalize (obj);
183 }
184
185 static void
186 modest_accounts_window_disconnect_signals (ModestWindow *self)
187 {       
188         ModestAccountsWindowPrivate *priv;      
189         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
190
191         if (g_signal_handler_is_connected (G_OBJECT (modest_runtime_get_mail_operation_queue ()), 
192                                            priv->queue_change_handler))
193                 g_signal_handler_disconnect (G_OBJECT (modest_runtime_get_mail_operation_queue ()), 
194                                              priv->queue_change_handler);
195
196         modest_signal_mgr_disconnect_all_and_destroy (priv->sighandlers);
197         priv->sighandlers = NULL;       
198 }
199
200 static void
201 connect_signals (ModestAccountsWindow *self)
202 {
203         ModestAccountsWindowPrivate *priv;
204         GtkTreeModel *model;
205
206         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
207
208         /* accounts view */
209         priv->sighandlers = 
210                 modest_signal_mgr_connect (priv->sighandlers,
211                                            G_OBJECT (priv->account_view), "row-activated", 
212                                            G_CALLBACK (on_account_activated), self);
213
214         priv->sighandlers = 
215                 modest_signal_mgr_connect (priv->sighandlers,
216                                            G_OBJECT (modest_runtime_get_window_mgr ()),
217                                            "progress-list-changed",
218                                            G_CALLBACK (on_progress_list_changed), self);
219
220         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->account_view));
221
222         priv->sighandlers =
223                 modest_signal_mgr_connect (priv->sighandlers,
224                                            G_OBJECT (model),
225                                            "row-inserted",
226                                            G_CALLBACK (on_row_inserted), self);
227
228         priv->sighandlers =
229                 modest_signal_mgr_connect (priv->sighandlers,
230                                            G_OBJECT (model),
231                                            "row-deleted",
232                                            G_CALLBACK (on_row_deleted), self);
233
234         priv->sighandlers = 
235                 modest_signal_mgr_connect (priv->sighandlers,
236                                            G_OBJECT (priv->new_message_button),
237                                            "clicked",
238                                            G_CALLBACK (modest_ui_actions_on_new_msg), self);
239
240         /* we don't register this in sighandlers, as it should be run
241          * after disconnecting all signals, in destroy stage */
242
243
244         g_signal_connect(G_OBJECT(self), "key-press-event",
245                         G_CALLBACK(on_key_press), self);
246 }
247
248 static ModestWindow *
249 modest_accounts_window_new_real (void)
250 {
251         ModestAccountsWindow *self = NULL;
252         ModestAccountsWindowPrivate *priv = NULL;
253         GdkPixbuf *window_icon;
254         GdkPixbuf *new_message_pixbuf;
255         guint accel_key;
256         GdkModifierType accel_mods;
257         GtkAccelGroup *accel_group;
258         GtkWidget *no_accounts_label;
259         GtkWidget *empty_view_new_message_button;
260         GtkWidget *box_alignment;
261
262         self  = MODEST_ACCOUNTS_WINDOW(g_object_new(MODEST_TYPE_ACCOUNTS_WINDOW, NULL));
263         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
264
265         new_message_pixbuf = modest_platform_get_icon ("general_add", MODEST_ICON_SIZE_BIG);
266
267         box_alignment = gtk_alignment_new (0, 0, 1.0, 1.0);
268         gtk_alignment_set_padding (GTK_ALIGNMENT (box_alignment), 
269                                    HILDON_MARGIN_HALF, 0,
270                                    HILDON_MARGIN_DOUBLE, HILDON_MARGIN_DOUBLE);
271         priv->box = gtk_vbox_new (FALSE, 0);
272
273         no_accounts_label = gtk_label_new (_("mcen_ia_noaccounts"));
274         
275         gtk_misc_set_alignment (GTK_MISC (no_accounts_label), 0.5, 0.5);
276         hildon_helper_set_logical_font (no_accounts_label, "LargeSystemFont");
277
278         empty_view_new_message_button = hildon_button_new (MODEST_EDITABLE_SIZE, HILDON_BUTTON_ARRANGEMENT_HORIZONTAL);
279         hildon_button_set_title (HILDON_BUTTON (empty_view_new_message_button), _("mcen_ti_new_message"));
280         hildon_button_set_image (HILDON_BUTTON (empty_view_new_message_button), gtk_image_new_from_pixbuf (new_message_pixbuf));
281
282
283         priv->no_accounts_container = gtk_vbox_new (FALSE, 0);
284         gtk_box_pack_start (GTK_BOX (priv->no_accounts_container), empty_view_new_message_button, FALSE, FALSE, 0);
285         gtk_widget_show_all (empty_view_new_message_button);
286         gtk_box_pack_end (GTK_BOX (priv->no_accounts_container), no_accounts_label, TRUE, TRUE, 0);
287         gtk_widget_show (no_accounts_label);
288         gtk_box_pack_start (GTK_BOX (priv->box), priv->no_accounts_container, TRUE, TRUE, 0);
289
290         g_signal_connect (G_OBJECT (empty_view_new_message_button),
291                           "clicked",
292                           G_CALLBACK (modest_ui_actions_on_new_msg), self);
293         
294         priv->pannable = hildon_pannable_area_new ();
295
296         priv->queue_change_handler =
297                 g_signal_connect (G_OBJECT (modest_runtime_get_mail_operation_queue ()),
298                                   "queue-changed",
299                                   G_CALLBACK (on_queue_changed),
300                                   self);
301
302         priv->new_message_button = hildon_button_new (MODEST_EDITABLE_SIZE,
303                                                       HILDON_BUTTON_ARRANGEMENT_HORIZONTAL);
304
305         hildon_button_set_title (HILDON_BUTTON (priv->new_message_button), _("mcen_ti_new_message"));
306         hildon_button_set_image (HILDON_BUTTON (priv->new_message_button), gtk_image_new_from_pixbuf (new_message_pixbuf));
307
308         gtk_widget_show_all (priv->new_message_button);
309
310         g_object_unref (new_message_pixbuf);
311         setup_menu (self);
312
313         gtk_box_pack_start (GTK_BOX (priv->box), priv->pannable, TRUE, TRUE, 0);
314         gtk_container_add (GTK_CONTAINER (box_alignment), priv->box);
315         gtk_container_add (GTK_CONTAINER (self), box_alignment);
316
317         gtk_widget_show (priv->pannable);
318         gtk_widget_show (priv->box);
319         gtk_widget_show (box_alignment);
320
321         /* Get device name */
322         modest_maemo_utils_get_device_name ();
323
324         /* Set window icon */
325         window_icon = modest_platform_get_icon (MODEST_APP_ICON, MODEST_ICON_SIZE_BIG);
326         if (window_icon) {
327                 gtk_window_set_icon (GTK_WINDOW (self), window_icon);
328                 g_object_unref (window_icon);
329         }
330
331         accel_group = gtk_accel_group_new ();
332         gtk_accelerator_parse ("<Control>n", &accel_key, &accel_mods);
333         gtk_widget_add_accelerator (priv->new_message_button, "clicked", accel_group,
334                                     accel_key, accel_mods, 0);
335         gtk_window_add_accel_group (GTK_WINDOW (self), accel_group);
336
337         return MODEST_WINDOW(self);
338 }
339
340 ModestWindow *
341 modest_accounts_window_new (void)
342 {
343         GtkWidget *action_area_box;
344         ModestWindow *self;
345         ModestAccountsWindowPrivate *priv = NULL;
346         HildonProgram *app;
347
348         if (pre_created_accounts_window) {
349                 self = MODEST_WINDOW (pre_created_accounts_window);
350                 pre_created_accounts_window = NULL;
351         } else {
352                 self = modest_accounts_window_new_real ();
353         }
354         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
355         priv->account_view  = GTK_WIDGET (modest_account_view_new (modest_runtime_get_account_mgr ()));
356
357         action_area_box = hildon_tree_view_get_action_area_box (GTK_TREE_VIEW (priv->account_view));
358         gtk_box_pack_start (GTK_BOX (action_area_box), priv->new_message_button, TRUE, TRUE, 0);
359         hildon_tree_view_set_action_area_visible (GTK_TREE_VIEW (priv->account_view), TRUE);
360         gtk_container_add (GTK_CONTAINER (priv->pannable), priv->account_view);
361
362         connect_signals (MODEST_ACCOUNTS_WINDOW (self));
363
364         /* Load previous osso state, for instance if we are being restored from 
365          * hibernation:  */
366         modest_osso_load_state ();
367
368         app = hildon_program_get_instance ();
369         hildon_program_add_window (app, HILDON_WINDOW (self));
370         
371         /* Dont't restore settings here, 
372          * because it requires a gtk_widget_show(), 
373          * and we don't want to do that until later,
374          * so that the UI is not visible for non-menu D-Bus activation.
375          */
376
377         g_signal_connect (G_OBJECT (self), "map-event",
378                           G_CALLBACK (_modest_accounts_window_map_event),
379                           G_OBJECT (self));
380         update_progress_hint (MODEST_ACCOUNTS_WINDOW (self));
381
382         row_count_changed (MODEST_ACCOUNTS_WINDOW (self));
383
384         gtk_window_set_title (GTK_WINDOW (self), _("mcen_ap_name"));
385
386         return self;
387 }
388
389
390 ModestAccountView *
391 modest_accounts_window_get_account_view (ModestAccountsWindow *self)
392 {
393         ModestAccountsWindowPrivate *priv = NULL;
394
395         g_return_val_if_fail (MODEST_IS_ACCOUNTS_WINDOW(self), FALSE);
396
397         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
398         
399         return MODEST_ACCOUNT_VIEW (priv->account_view);
400 }
401
402 static void 
403 setup_menu (ModestAccountsWindow *self)
404 {
405         g_return_if_fail (MODEST_IS_ACCOUNTS_WINDOW(self));
406
407         /* Settings menu buttons */
408         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_new_account"), NULL, 
409                                            APP_MENU_CALLBACK (modest_ui_actions_on_new_account), 
410                                            NULL);
411         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self),
412                                            dngettext(GETTEXT_PACKAGE,
413                                                      "mcen_me_edit_account",
414                                                      "mcen_me_edit_accounts",
415                                                      2),
416                                            NULL,
417                                            APP_MENU_CALLBACK (modest_ui_actions_on_accounts), 
418                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_edit_accounts));
419         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_globalsmtpservers"), NULL,
420                                            APP_MENU_CALLBACK (modest_ui_actions_on_smtp_servers),
421                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_tools_smtp_servers));
422         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_sendandreceive"), NULL,
423                                            APP_MENU_CALLBACK (modest_ui_actions_on_send_receive),
424                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_send_receive_all));
425         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_outbox_cancelsend"), NULL,
426                                            APP_MENU_CALLBACK (modest_ui_actions_cancel_send),
427                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_cancel_sending_all));
428         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_options"), NULL,
429                                            APP_MENU_CALLBACK (modest_ui_actions_on_settings), 
430                                            NULL);
431 }
432
433
434 static void
435 on_account_activated (GtkTreeView *account_view,
436                       GtkTreePath *path,
437                       GtkTreeViewColumn *column,
438                       ModestAccountsWindow *self)
439 {
440         ModestAccountsWindowPrivate *priv;
441         gchar* account_name; 
442         GtkWidget *new_window;
443         gboolean registered;
444         ModestProtocolType store_protocol;
445         gboolean mailboxes_protocol;
446
447         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
448
449         account_name = modest_account_view_get_path_account (MODEST_ACCOUNT_VIEW (priv->account_view), path);
450         if (!account_name)
451                 return;
452
453         /* If it's a multimailbox container, we have to show the mailboxes window */
454         store_protocol = modest_account_mgr_get_store_protocol (modest_runtime_get_account_mgr (), 
455                                                                 account_name);
456         mailboxes_protocol = 
457                 modest_protocol_registry_protocol_type_has_tag (modest_runtime_get_protocol_registry (),
458                                                                 store_protocol,
459                                                                 MODEST_PROTOCOL_REGISTRY_MULTI_MAILBOX_PROVIDER_PROTOCOLS);
460         if (mailboxes_protocol) {
461                 new_window = GTK_WIDGET (modest_mailboxes_window_new (account_name));
462         } else {
463
464                 new_window = GTK_WIDGET (modest_folder_window_new (NULL));
465         }
466
467         registered = modest_window_mgr_register_window (modest_runtime_get_window_mgr (), 
468                                                         MODEST_WINDOW (new_window),
469                                                         MODEST_WINDOW (self));
470
471         if (!registered) {
472                 gtk_widget_destroy (new_window);
473                 new_window = NULL;
474         } else {
475                 if (!mailboxes_protocol) {
476                         modest_folder_window_set_account (MODEST_FOLDER_WINDOW (new_window), account_name);
477                 }
478                 gtk_widget_show (new_window);
479         }
480         g_free (account_name);
481 }
482
483 static gboolean
484 _modest_accounts_window_map_event (GtkWidget *widget,
485                                    GdkEvent *event,
486                                    gpointer userdata)
487 {
488         ModestAccountsWindow *self = (ModestAccountsWindow *) userdata;
489         ModestAccountsWindowPrivate *priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
490
491         if (priv->progress_hint) {
492                 hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), TRUE);
493         }
494
495         return FALSE;
496 }
497
498 static gboolean
499 has_active_operations (ModestAccountsWindow *self)
500 {
501         GSList *operations = NULL, *node;
502         ModestMailOperationQueue *queue;
503         gboolean has_active = FALSE;
504
505         queue = modest_runtime_get_mail_operation_queue ();
506         operations = modest_mail_operation_queue_get_by_source (queue, G_OBJECT (self));
507
508         for (node = operations; node != NULL; node = g_slist_next (node)) {
509                 if (!modest_mail_operation_is_finished (MODEST_MAIL_OPERATION (node->data))) {
510                         has_active = TRUE;
511                         break;
512                 }
513         }
514
515         if (operations) {
516                 g_slist_foreach (operations, (GFunc) g_object_unref, NULL);
517                 g_slist_free (operations);
518         }
519
520         return has_active;
521 }
522
523 static void
524 update_progress_hint (ModestAccountsWindow *self)
525 {
526         ModestAccountsWindowPrivate *priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
527
528         if (has_active_operations (self)) {
529                 priv->progress_hint = TRUE;
530         } else {
531                 priv->progress_hint = FALSE;
532         }
533
534         if (!priv->progress_hint) {
535                 priv->progress_hint = modest_window_mgr_has_progress_operation (modest_runtime_get_window_mgr ());
536         }
537         
538         if (GTK_WIDGET_VISIBLE (self)) {
539                 hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), priv->progress_hint?1:0);
540         }
541 }
542
543 static void
544 on_progress_list_changed (ModestWindowMgr *mgr,
545                           ModestAccountsWindow *self)
546 {
547         update_progress_hint (self);
548 }
549
550 static void
551 on_row_inserted (GtkTreeModel *tree_model,
552                  GtkTreePath  *path,
553                  GtkTreeIter  *iter,
554                  gpointer      user_data)
555 {
556         ModestAccountsWindow *self;
557
558         self = (ModestAccountsWindow *) user_data;
559
560         row_count_changed (self);
561 }
562
563 static void
564 on_row_deleted (GtkTreeModel *tree_model,
565                 GtkTreePath  *path,
566                 gpointer      user_data)
567 {
568         ModestAccountsWindow *self;
569
570         self = (ModestAccountsWindow *) user_data;
571
572         row_count_changed (self);
573 }
574
575 static void 
576 row_count_changed (ModestAccountsWindow *self)
577 {
578         ModestAccountsWindowPrivate *priv;
579         GtkTreeModel *model;
580         gint count;
581
582         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
583         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->account_view));
584
585         count = gtk_tree_model_iter_n_children (model, NULL);
586
587         if (count == 0) {
588                 gtk_widget_hide (priv->account_view);
589                 gtk_widget_show (priv->no_accounts_container);
590                 g_debug ("%s: hiding accounts view", __FUNCTION__);
591         } else {
592                 gtk_widget_hide (priv->no_accounts_container);
593                 gtk_widget_show (priv->account_view);
594                 g_debug ("%s: showing accounts view", __FUNCTION__);
595         }
596         gtk_container_child_set (GTK_CONTAINER(priv->box), priv->pannable, 
597                                  "expand", count > 0,
598                                  "fill", count > 0,
599                                  NULL);
600 }
601
602 static void 
603 on_mail_operation_started (ModestMailOperation *mail_op,
604                            gpointer user_data)
605 {
606         ModestAccountsWindow *self;
607         ModestMailOperationTypeOperation op_type;
608         GObject *source = NULL;
609
610         self = MODEST_ACCOUNTS_WINDOW (user_data);
611         op_type = modest_mail_operation_get_type_operation (mail_op);
612         source = modest_mail_operation_get_source(mail_op);
613         if (G_OBJECT (self) == source) {
614                 update_progress_hint (self);
615         }
616         g_object_unref (source);
617 }
618
619 static void 
620 on_mail_operation_finished (ModestMailOperation *mail_op,
621                             gpointer user_data)
622 {
623         ModestAccountsWindow *self;
624
625         self = MODEST_ACCOUNTS_WINDOW (user_data);
626
627         /* Don't disable the progress hint if there are more pending
628            operations from this window */
629         update_progress_hint (self);
630
631         modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (self));
632 }
633
634 static void
635 on_queue_changed (ModestMailOperationQueue *queue,
636                   ModestMailOperation *mail_op,
637                   ModestMailOperationQueueNotification type,
638                   ModestAccountsWindow *self)
639 {
640         ModestAccountsWindowPrivate *priv;
641
642         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
643
644         /* If this operations was created by another window, do nothing */
645         if (!modest_mail_operation_is_mine (mail_op, G_OBJECT(self))) 
646                 return;
647
648         if (type == MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED) {
649                 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
650                                                                G_OBJECT (mail_op),
651                                                                "operation-started",
652                                                                G_CALLBACK (on_mail_operation_started),
653                                                                self);
654                 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
655                                                                G_OBJECT (mail_op),
656                                                                "operation-finished",
657                                                                G_CALLBACK (on_mail_operation_finished),
658                                                                self);
659         } else if (type == MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED) {
660                 priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
661                                                                   G_OBJECT (mail_op),
662                                                                   "operation-started");
663                 priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
664                                                                   G_OBJECT (mail_op),
665                                                                   "operation-finished");
666         }
667 }
668
669 void 
670 modest_accounts_window_pre_create (void)
671 {
672         static gboolean pre_created = FALSE;
673         if (!pre_created) {
674                 pre_created = TRUE;
675                 pre_created_accounts_window = GTK_WIDGET (modest_accounts_window_new_real ());
676         }
677 }
678
679
680 static gboolean
681 on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
682 {
683         ModestAccountsWindowPrivate *priv;
684         HildonPannableArea *pannable;
685
686         if (event->type == GDK_KEY_RELEASE)
687                 return FALSE;
688
689         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(user_data);
690
691         pannable = HILDON_PANNABLE_AREA (priv->pannable);
692
693         switch (event->keyval) {
694
695         case GDK_Up:
696                 modest_maemo_utils_scroll_pannable(pannable, 0, -1);
697                 break;
698
699         case GDK_Down:
700                 modest_maemo_utils_scroll_pannable(pannable, 0, 1);
701                 break;
702         }
703
704         return FALSE;
705 }