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