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