61c0154d2a963c6998fd7e7d6ba9096ca8c0456b
[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 <modest-maemo-utils.h>
48 #include <hildon/hildon.h>
49 #endif
50
51
52 /* 'private'/'protected' functions */
53 static void modest_accounts_window_class_init  (ModestAccountsWindowClass *klass);
54 static void modest_accounts_window_instance_init (ModestAccountsWindow *obj);
55 static void modest_accounts_window_finalize    (GObject *obj);
56
57 static void connect_signals (ModestAccountsWindow *self);
58 static void modest_accounts_window_disconnect_signals (ModestWindow *self);
59
60 static void on_account_activated (GtkTreeView *treeview,
61                                   GtkTreePath *path,
62                                   GtkTreeViewColumn *column,
63                                   ModestAccountsWindow *accounts_window);
64 static void on_progress_list_changed (ModestWindowMgr *mgr,
65                                       ModestAccountsWindow *self);
66 static void setup_menu (ModestAccountsWindow *self);
67 static gboolean _modest_accounts_window_map_event (GtkWidget *widget,
68                                                    GdkEvent *event,
69                                                    gpointer userdata);
70 static void update_progress_hint (ModestAccountsWindow *self);
71 static void on_queue_changed    (ModestMailOperationQueue *queue,
72                                  ModestMailOperation *mail_op,
73                                  ModestMailOperationQueueNotification type,
74                                  ModestAccountsWindow *self);
75 static void on_row_inserted (GtkTreeModel *tree_model,
76                              GtkTreePath  *path,
77                              GtkTreeIter  *iter,
78                              gpointer      user_data);
79 static void on_row_deleted (GtkTreeModel *tree_model,
80                             GtkTreePath  *path,
81                             gpointer      user_data);
82 static void row_count_changed (ModestAccountsWindow *self);
83 static gboolean on_key_press(GtkWidget *widget,
84                                 GdkEventKey *event,
85                                 gpointer user_data);
86 static gboolean on_delete_event (GtkWidget *widget,
87                                  GdkEvent *event,
88                                  gpointer userdata);
89
90 typedef struct _ModestAccountsWindowPrivate ModestAccountsWindowPrivate;
91 struct _ModestAccountsWindowPrivate {
92
93         GtkWidget *box;
94         GtkWidget *scrollable;
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->scrollable = modest_toolkit_factory_create_scrollable (modest_runtime_get_toolkit_factory ());
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->scrollable, 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->scrollable);
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->scrollable), priv->account_view);
361
362         connect_signals (MODEST_ACCOUNTS_WINDOW (self));
363
364         app = hildon_program_get_instance ();
365         hildon_program_add_window (app, HILDON_WINDOW (self));
366         
367         /* Dont't restore settings here, 
368          * because it requires a gtk_widget_show(), 
369          * and we don't want to do that until later,
370          * so that the UI is not visible for non-menu D-Bus activation.
371          */
372
373         g_signal_connect (G_OBJECT (self), "map-event",
374                           G_CALLBACK (_modest_accounts_window_map_event),
375                           G_OBJECT (self));
376         g_signal_connect (G_OBJECT (self), "delete-event",
377                           G_CALLBACK (on_delete_event), self);
378         update_progress_hint (MODEST_ACCOUNTS_WINDOW (self));
379
380         row_count_changed (MODEST_ACCOUNTS_WINDOW (self));
381
382         modest_window_set_title (MODEST_WINDOW (self), _("mcen_ap_name"));
383
384         return self;
385 }
386
387
388 ModestAccountView *
389 modest_accounts_window_get_account_view (ModestAccountsWindow *self)
390 {
391         ModestAccountsWindowPrivate *priv = NULL;
392
393         g_return_val_if_fail (MODEST_IS_ACCOUNTS_WINDOW(self), FALSE);
394
395         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
396         
397         return MODEST_ACCOUNT_VIEW (priv->account_view);
398 }
399
400 static void 
401 setup_menu (ModestAccountsWindow *self)
402 {
403         g_return_if_fail (MODEST_IS_ACCOUNTS_WINDOW(self));
404
405         /* Settings menu buttons */
406         modest_window_add_to_menu (MODEST_WINDOW (self), _("mcen_me_new_account"), NULL, 
407                                    MODEST_WINDOW_MENU_CALLBACK (modest_ui_actions_on_new_account), 
408                                    NULL);
409         modest_window_add_to_menu (MODEST_WINDOW (self), _("mcen_me_inbox_sendandreceive"), NULL,
410                                    MODEST_WINDOW_MENU_CALLBACK (modest_ui_actions_on_send_receive),
411                                    MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_send_receive_all));
412         modest_window_add_to_menu (MODEST_WINDOW (self),
413                                    dngettext(GETTEXT_PACKAGE,
414                                              "mcen_me_edit_account",
415                                              "mcen_me_edit_accounts",
416                                              2),
417                                    NULL,
418                                    MODEST_WINDOW_MENU_CALLBACK (modest_ui_actions_on_accounts), 
419                                    MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_edit_accounts));
420         modest_window_add_to_menu (MODEST_WINDOW (self), _("mcen_me_inbox_globalsmtpservers"), NULL,
421                                    MODEST_WINDOW_MENU_CALLBACK (modest_ui_actions_on_smtp_servers),
422                                    MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_tools_smtp_servers));
423         modest_window_add_to_menu (MODEST_WINDOW (self), _("mcen_me_outbox_cancelsend"), NULL,
424                                    MODEST_WINDOW_MENU_CALLBACK (modest_ui_actions_cancel_send),
425                                    MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_cancel_sending_all));
426         modest_window_add_to_menu (MODEST_WINDOW (self), _("mcen_me_inbox_options"), NULL,
427                                    MODEST_WINDOW_MENU_CALLBACK (modest_ui_actions_on_settings), 
428                                    NULL);
429 }
430
431
432 static void
433 on_account_activated (GtkTreeView *account_view,
434                       GtkTreePath *path,
435                       GtkTreeViewColumn *column,
436                       ModestAccountsWindow *self)
437 {
438         ModestAccountsWindowPrivate *priv;
439         gchar* account_name; 
440         GtkWidget *new_window;
441         gboolean registered;
442         ModestProtocolType store_protocol;
443         gboolean mailboxes_protocol;
444
445         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
446
447         account_name = modest_account_view_get_path_account (MODEST_ACCOUNT_VIEW (priv->account_view), path);
448         if (!account_name)
449                 return;
450
451         /* If it's a multimailbox container, we have to show the mailboxes window */
452         store_protocol = modest_account_mgr_get_store_protocol (modest_runtime_get_account_mgr (), 
453                                                                 account_name);
454         mailboxes_protocol = 
455                 modest_protocol_registry_protocol_type_has_tag (modest_runtime_get_protocol_registry (),
456                                                                 store_protocol,
457                                                                 MODEST_PROTOCOL_REGISTRY_MULTI_MAILBOX_PROVIDER_PROTOCOLS);
458         if (mailboxes_protocol) {
459                 new_window = GTK_WIDGET (modest_mailboxes_window_new (account_name));
460         } else {
461
462                 new_window = GTK_WIDGET (modest_folder_window_new (NULL));
463         }
464
465         registered = modest_window_mgr_register_window (modest_runtime_get_window_mgr (), 
466                                                         MODEST_WINDOW (new_window),
467                                                         MODEST_WINDOW (self));
468
469         if (!registered) {
470                 gtk_widget_destroy (new_window);
471                 new_window = NULL;
472         } else {
473                 if (!mailboxes_protocol) {
474                         modest_folder_window_set_account (MODEST_FOLDER_WINDOW (new_window), account_name);
475                 }
476                 gtk_widget_show (new_window);
477         }
478         g_free (account_name);
479 }
480
481 static gboolean
482 _modest_accounts_window_map_event (GtkWidget *widget,
483                                    GdkEvent *event,
484                                    gpointer userdata)
485 {
486         ModestAccountsWindow *self = (ModestAccountsWindow *) userdata;
487         ModestAccountsWindowPrivate *priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
488
489         if (priv->progress_hint) {
490                 hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), TRUE);
491         }
492
493         return FALSE;
494 }
495
496 static gboolean
497 has_active_operations (ModestAccountsWindow *self)
498 {
499         GSList *operations = NULL, *node;
500         ModestMailOperationQueue *queue;
501         gboolean has_active = FALSE;
502
503         queue = modest_runtime_get_mail_operation_queue ();
504         operations = modest_mail_operation_queue_get_by_source (queue, G_OBJECT (self));
505
506         for (node = operations; node != NULL; node = g_slist_next (node)) {
507                 if (!modest_mail_operation_is_finished (MODEST_MAIL_OPERATION (node->data))) {
508                         has_active = TRUE;
509                         break;
510                 }
511         }
512
513         if (operations) {
514                 g_slist_foreach (operations, (GFunc) g_object_unref, NULL);
515                 g_slist_free (operations);
516         }
517
518         return has_active;
519 }
520
521 static void
522 update_progress_hint (ModestAccountsWindow *self)
523 {
524         ModestAccountsWindowPrivate *priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
525
526         if (has_active_operations (self)) {
527                 priv->progress_hint = TRUE;
528         } else {
529                 priv->progress_hint = FALSE;
530         }
531
532         if (!priv->progress_hint) {
533                 priv->progress_hint = modest_window_mgr_has_progress_operation (modest_runtime_get_window_mgr ());
534         }
535         
536         if (GTK_WIDGET_VISIBLE (self)) {
537                 hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), priv->progress_hint?1:0);
538         }
539 }
540
541 static void
542 on_progress_list_changed (ModestWindowMgr *mgr,
543                           ModestAccountsWindow *self)
544 {
545         update_progress_hint (self);
546 }
547
548 static void
549 on_row_inserted (GtkTreeModel *tree_model,
550                  GtkTreePath  *path,
551                  GtkTreeIter  *iter,
552                  gpointer      user_data)
553 {
554         ModestAccountsWindow *self;
555
556         self = (ModestAccountsWindow *) user_data;
557
558         row_count_changed (self);
559 }
560
561 static void
562 on_row_deleted (GtkTreeModel *tree_model,
563                 GtkTreePath  *path,
564                 gpointer      user_data)
565 {
566         ModestAccountsWindow *self;
567
568         self = (ModestAccountsWindow *) user_data;
569
570         row_count_changed (self);
571 }
572
573 static void 
574 row_count_changed (ModestAccountsWindow *self)
575 {
576         ModestAccountsWindowPrivate *priv;
577         GtkTreeModel *model;
578         gint count;
579
580         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
581         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->account_view));
582
583         count = gtk_tree_model_iter_n_children (model, NULL);
584
585         if (count == 0) {
586                 gtk_widget_hide (priv->account_view);
587                 gtk_widget_show (priv->no_accounts_container);
588                 g_debug ("%s: hiding accounts view", __FUNCTION__);
589         } else {
590                 gtk_widget_hide (priv->no_accounts_container);
591                 gtk_widget_show (priv->account_view);
592                 g_debug ("%s: showing accounts view", __FUNCTION__);
593         }
594         gtk_container_child_set (GTK_CONTAINER(priv->box), priv->scrollable, 
595                                  "expand", count > 0,
596                                  "fill", count > 0,
597                                  NULL);
598 }
599
600 static void 
601 on_mail_operation_started (ModestMailOperation *mail_op,
602                            gpointer user_data)
603 {
604         ModestAccountsWindow *self;
605         ModestMailOperationTypeOperation op_type;
606         GObject *source = NULL;
607
608         self = MODEST_ACCOUNTS_WINDOW (user_data);
609         op_type = modest_mail_operation_get_type_operation (mail_op);
610         source = modest_mail_operation_get_source(mail_op);
611         if (G_OBJECT (self) == source) {
612                 update_progress_hint (self);
613         }
614         g_object_unref (source);
615 }
616
617 static void 
618 on_mail_operation_finished (ModestMailOperation *mail_op,
619                             gpointer user_data)
620 {
621         ModestAccountsWindow *self;
622
623         self = MODEST_ACCOUNTS_WINDOW (user_data);
624
625         /* Don't disable the progress hint if there are more pending
626            operations from this window */
627         update_progress_hint (self);
628
629         modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (self));
630 }
631
632 static void
633 on_queue_changed (ModestMailOperationQueue *queue,
634                   ModestMailOperation *mail_op,
635                   ModestMailOperationQueueNotification type,
636                   ModestAccountsWindow *self)
637 {
638         ModestAccountsWindowPrivate *priv;
639
640         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
641
642         /* If this operations was created by another window, do nothing */
643         if (!modest_mail_operation_is_mine (mail_op, G_OBJECT(self))) 
644                 return;
645
646         if (type == MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED) {
647                 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
648                                                                G_OBJECT (mail_op),
649                                                                "operation-started",
650                                                                G_CALLBACK (on_mail_operation_started),
651                                                                self);
652                 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
653                                                                G_OBJECT (mail_op),
654                                                                "operation-finished",
655                                                                G_CALLBACK (on_mail_operation_finished),
656                                                                self);
657         } else if (type == MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED) {
658                 priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
659                                                                   G_OBJECT (mail_op),
660                                                                   "operation-started");
661                 priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
662                                                                   G_OBJECT (mail_op),
663                                                                   "operation-finished");
664         }
665 }
666
667 void 
668 modest_accounts_window_pre_create (void)
669 {
670         static gboolean pre_created = FALSE;
671         if (!pre_created) {
672                 pre_created = TRUE;
673                 pre_created_accounts_window = GTK_WIDGET (modest_accounts_window_new_real ());
674         }
675 }
676
677
678 static gboolean
679 on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
680 {
681         ModestAccountsWindowPrivate *priv;
682         ModestScrollable *scrollable;
683
684         if (event->type == GDK_KEY_RELEASE)
685                 return FALSE;
686
687         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(user_data);
688
689         scrollable = MODEST_SCROLLABLE (priv->scrollable);
690
691         switch (event->keyval) {
692
693         case GDK_Up:
694                 modest_scrollable_scroll (scrollable, 0, -1);
695                 break;
696
697         case GDK_Down:
698                 modest_scrollable_scroll (scrollable, 0, 1);
699                 break;
700         }
701
702         return FALSE;
703 }
704
705 static gboolean
706 on_delete_event (GtkWidget *widget,
707                  GdkEvent *event,
708                  gpointer userdata)
709 {
710         ModestAccountsWindowPrivate *priv;
711
712         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (widget);
713
714         modest_account_view_set_show_last_update (MODEST_ACCOUNT_VIEW (priv->account_view), FALSE);
715
716         gtk_widget_queue_resize (widget);
717
718         gdk_window_process_updates (priv->account_view->window, TRUE);
719         hildon_gtk_window_take_screenshot (GTK_WINDOW (widget), TRUE);
720
721         modest_account_view_set_show_last_update (MODEST_ACCOUNT_VIEW (priv->account_view), TRUE);
722
723         return FALSE;
724
725 }