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