Enable live search in accounts window
[modest] / src / hildon2 / modest-accounts-window.c
1 /* Copyright (c) 2008, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <modest-accounts-window.h>
31 #include <hildon/hildon-pannable-area.h>
32 #include <hildon/hildon-banner.h>
33 #include <hildon/hildon-helper.h>
34 #include <modest-ui-actions.h>
35 #include <modest-window-mgr.h>
36 #include <modest-signal-mgr.h>
37 #include <modest-runtime.h>
38 #include <modest-platform.h>
39 #include <hildon/hildon.h>
40 #include <modest-maemo-utils.h>
41 #include <modest-icon-names.h>
42 #include <modest-defs.h>
43 #include <modest-folder-window.h>
44 #include <modest-ui-dimming-rules.h>
45 #include <modest-ui-dimming-manager.h>
46 #include <modest-window-priv.h>
47 #include <modest-ui-constants.h>
48 #include <modest-account-mgr-helpers.h>
49 #include <modest-mailboxes-window.h>
50 #include <modest-text-utils.h>
51
52
53 /* 'private'/'protected' functions */
54 static void modest_accounts_window_class_init  (ModestAccountsWindowClass *klass);
55 static void modest_accounts_window_instance_init (ModestAccountsWindow *obj);
56 static void modest_accounts_window_finalize    (GObject *obj);
57
58 static void connect_signals (ModestAccountsWindow *self);
59 static void modest_accounts_window_disconnect_signals (ModestWindow *self);
60
61 static void on_account_activated (GtkTreeView *treeview,
62                                   GtkTreePath *path,
63                                   GtkTreeViewColumn *column,
64                                   ModestAccountsWindow *accounts_window);
65 static void on_progress_list_changed (ModestWindowMgr *mgr,
66                                       ModestAccountsWindow *self);
67 static void setup_menu (ModestAccountsWindow *self);
68 static gboolean _modest_accounts_window_map_event (GtkWidget *widget,
69                                                    GdkEvent *event,
70                                                    gpointer userdata);
71 static void update_progress_hint (ModestAccountsWindow *self);
72 static void on_queue_changed    (ModestMailOperationQueue *queue,
73                                  ModestMailOperation *mail_op,
74                                  ModestMailOperationQueueNotification type,
75                                  ModestAccountsWindow *self);
76 static void on_row_inserted (GtkTreeModel *tree_model,
77                              GtkTreePath  *path,
78                              GtkTreeIter  *iter,
79                              gpointer      user_data);
80 static void on_row_deleted (GtkTreeModel *tree_model,
81                             GtkTreePath  *path,
82                             gpointer      user_data);
83 static void row_count_changed (ModestAccountsWindow *self);
84 static gboolean on_key_press(GtkWidget *widget,
85                                 GdkEventKey *event,
86                                 gpointer user_data);
87 static gboolean on_delete_event (GtkWidget *widget,
88                                  GdkEvent *event,
89                                  gpointer userdata);
90
91 typedef struct _ModestAccountsWindowPrivate ModestAccountsWindowPrivate;
92 struct _ModestAccountsWindowPrivate {
93
94         GtkWidget *box;
95         GtkWidget *pannable;
96         GtkWidget *account_view;
97         GtkWidget *no_accounts_container;
98         GtkWidget *new_message_button;
99
100         /* signals */
101         GSList *sighandlers;
102
103         gboolean progress_hint;
104         guint queue_change_handler;
105 };
106 #define MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE((o), \
107                                                                             MODEST_TYPE_ACCOUNTS_WINDOW, \
108                                                                             ModestAccountsWindowPrivate))
109
110 /* globals */
111 static GtkWindowClass *parent_class = NULL;
112 static GtkWidget *pre_created_accounts_window = NULL;
113
114 /************************************************************************/
115
116 GType
117 modest_accounts_window_get_type (void)
118 {
119         static GType my_type = 0;
120         if (!my_type) {
121                 static const GTypeInfo my_info = {
122                         sizeof(ModestAccountsWindowClass),
123                         NULL,           /* base init */
124                         NULL,           /* base finalize */
125                         (GClassInitFunc) modest_accounts_window_class_init,
126                         NULL,           /* class finalize */
127                         NULL,           /* class data */
128                         sizeof(ModestAccountsWindow),
129                         1,              /* n_preallocs */
130                         (GInstanceInitFunc) modest_accounts_window_instance_init,
131                         NULL
132                 };
133                 my_type = g_type_register_static (MODEST_TYPE_HILDON2_WINDOW,
134                                                   "ModestAccountsWindow",
135                                                   &my_info, 0);
136         }
137         return my_type;
138 }
139
140 static void
141 modest_accounts_window_class_init (ModestAccountsWindowClass *klass)
142 {
143         GObjectClass *gobject_class;
144         gobject_class = (GObjectClass*) klass;
145         ModestWindowClass *modest_window_class = (ModestWindowClass *) klass;
146
147         parent_class            = g_type_class_peek_parent (klass);
148         gobject_class->finalize = modest_accounts_window_finalize;
149
150         g_type_class_add_private (gobject_class, sizeof(ModestAccountsWindowPrivate));
151         
152         modest_window_class->disconnect_signals_func = modest_accounts_window_disconnect_signals;
153 }
154
155 static void
156 modest_accounts_window_instance_init (ModestAccountsWindow *obj)
157 {
158         ModestAccountsWindowPrivate *priv;
159
160         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(obj);
161
162         priv->sighandlers = NULL;
163         
164         priv->account_view = NULL;
165         priv->progress_hint = FALSE;
166         
167         modest_window_mgr_register_help_id (modest_runtime_get_window_mgr(),
168                                             GTK_WINDOW(obj),
169                                             "applications_email_accountsview");
170 }
171
172 static void
173 modest_accounts_window_finalize (GObject *obj)
174 {
175         ModestAccountsWindowPrivate *priv;
176
177         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(obj);
178
179         /* Sanity check: shouldn't be needed, the window mgr should
180            call this function before */
181         modest_accounts_window_disconnect_signals (MODEST_WINDOW (obj));        
182
183         G_OBJECT_CLASS(parent_class)->finalize (obj);
184 }
185
186 static void
187 modest_accounts_window_disconnect_signals (ModestWindow *self)
188 {       
189         ModestAccountsWindowPrivate *priv;      
190         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
191
192         if (g_signal_handler_is_connected (G_OBJECT (modest_runtime_get_mail_operation_queue ()), 
193                                            priv->queue_change_handler))
194                 g_signal_handler_disconnect (G_OBJECT (modest_runtime_get_mail_operation_queue ()), 
195                                              priv->queue_change_handler);
196
197         modest_signal_mgr_disconnect_all_and_destroy (priv->sighandlers);
198         priv->sighandlers = NULL;       
199 }
200
201 static void
202 connect_signals (ModestAccountsWindow *self)
203 {
204         ModestAccountsWindowPrivate *priv;
205         GtkTreeModel *model;
206
207         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
208
209         /* accounts view */
210         priv->sighandlers = 
211                 modest_signal_mgr_connect (priv->sighandlers,
212                                            G_OBJECT (priv->account_view), "row-activated", 
213                                            G_CALLBACK (on_account_activated), self);
214
215         priv->sighandlers = 
216                 modest_signal_mgr_connect (priv->sighandlers,
217                                            G_OBJECT (modest_runtime_get_window_mgr ()),
218                                            "progress-list-changed",
219                                            G_CALLBACK (on_progress_list_changed), self);
220
221         model = modest_account_view_get_model (MODEST_ACCOUNT_VIEW (priv->account_view));
222
223         priv->sighandlers =
224                 modest_signal_mgr_connect (priv->sighandlers,
225                                            G_OBJECT (model),
226                                            "row-inserted",
227                                            G_CALLBACK (on_row_inserted), self);
228
229         priv->sighandlers =
230                 modest_signal_mgr_connect (priv->sighandlers,
231                                            G_OBJECT (model),
232                                            "row-deleted",
233                                            G_CALLBACK (on_row_deleted), self);
234
235         priv->sighandlers = 
236                 modest_signal_mgr_connect (priv->sighandlers,
237                                            G_OBJECT (priv->new_message_button),
238                                            "clicked",
239                                            G_CALLBACK (modest_ui_actions_on_new_msg), self);
240
241         /* we don't register this in sighandlers, as it should be run
242          * after disconnecting all signals, in destroy stage */
243
244
245         g_signal_connect(G_OBJECT(self), "key-press-event",
246                         G_CALLBACK(on_key_press), self);
247 }
248
249 static ModestWindow *
250 modest_accounts_window_new_real (void)
251 {
252         ModestAccountsWindow *self = NULL;
253         ModestAccountsWindowPrivate *priv = NULL;
254         GdkPixbuf *window_icon;
255         GdkPixbuf *new_message_pixbuf;
256         guint accel_key;
257         GdkModifierType accel_mods;
258         GtkAccelGroup *accel_group;
259         GtkWidget *no_accounts_label;
260         GtkWidget *empty_view_new_message_button;
261         GtkWidget *box_alignment;
262
263         self  = MODEST_ACCOUNTS_WINDOW(g_object_new(MODEST_TYPE_ACCOUNTS_WINDOW, NULL));
264         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
265
266         new_message_pixbuf = modest_platform_get_icon ("general_add", MODEST_ICON_SIZE_BIG);
267
268         box_alignment = gtk_alignment_new (0, 0, 1.0, 1.0);
269         gtk_alignment_set_padding (GTK_ALIGNMENT (box_alignment), 
270                                    HILDON_MARGIN_HALF, 0,
271                                    HILDON_MARGIN_DOUBLE, HILDON_MARGIN_DOUBLE);
272         priv->box = gtk_vbox_new (FALSE, 0);
273
274         no_accounts_label = gtk_label_new (_("mcen_ia_noaccounts"));
275         
276         gtk_misc_set_alignment (GTK_MISC (no_accounts_label), 0.5, 0.5);
277         hildon_helper_set_logical_font (no_accounts_label, "LargeSystemFont");
278
279         empty_view_new_message_button = hildon_button_new (MODEST_EDITABLE_SIZE, HILDON_BUTTON_ARRANGEMENT_HORIZONTAL);
280         hildon_button_set_title (HILDON_BUTTON (empty_view_new_message_button), _("mcen_ti_new_message"));
281         hildon_button_set_image (HILDON_BUTTON (empty_view_new_message_button), gtk_image_new_from_pixbuf (new_message_pixbuf));
282
283
284         priv->no_accounts_container = gtk_vbox_new (FALSE, 0);
285         gtk_box_pack_start (GTK_BOX (priv->no_accounts_container), empty_view_new_message_button, FALSE, FALSE, 0);
286         gtk_widget_show_all (empty_view_new_message_button);
287         gtk_box_pack_end (GTK_BOX (priv->no_accounts_container), no_accounts_label, TRUE, TRUE, 0);
288         gtk_widget_show (no_accounts_label);
289         gtk_box_pack_start (GTK_BOX (priv->box), priv->no_accounts_container, TRUE, TRUE, 0);
290
291         g_signal_connect (G_OBJECT (empty_view_new_message_button),
292                           "clicked",
293                           G_CALLBACK (modest_ui_actions_on_new_msg), self);
294         
295         priv->pannable = hildon_pannable_area_new ();
296
297         priv->queue_change_handler =
298                 g_signal_connect (G_OBJECT (modest_runtime_get_mail_operation_queue ()),
299                                   "queue-changed",
300                                   G_CALLBACK (on_queue_changed),
301                                   self);
302
303         priv->new_message_button = hildon_button_new (MODEST_EDITABLE_SIZE,
304                                                       HILDON_BUTTON_ARRANGEMENT_HORIZONTAL);
305
306         hildon_button_set_title (HILDON_BUTTON (priv->new_message_button), _("mcen_ti_new_message"));
307         hildon_button_set_image (HILDON_BUTTON (priv->new_message_button), gtk_image_new_from_pixbuf (new_message_pixbuf));
308
309         gtk_widget_show_all (priv->new_message_button);
310
311         g_object_unref (new_message_pixbuf);
312         setup_menu (self);
313
314         gtk_box_pack_start (GTK_BOX (priv->box), priv->pannable, TRUE, TRUE, 0);
315         gtk_container_add (GTK_CONTAINER (box_alignment), priv->box);
316         gtk_container_add (GTK_CONTAINER (self), box_alignment);
317
318         gtk_widget_show (priv->pannable);
319         gtk_widget_show (priv->box);
320         gtk_widget_show (box_alignment);
321
322         /* Get device name */
323         modest_maemo_utils_get_device_name ();
324
325         /* Set window icon */
326         window_icon = modest_platform_get_icon (MODEST_APP_ICON, MODEST_ICON_SIZE_BIG);
327         if (window_icon) {
328                 gtk_window_set_icon (GTK_WINDOW (self), window_icon);
329                 g_object_unref (window_icon);
330         }
331
332         accel_group = gtk_accel_group_new ();
333         gtk_accelerator_parse ("<Control>n", &accel_key, &accel_mods);
334         gtk_widget_add_accelerator (priv->new_message_button, "clicked", accel_group,
335                                     accel_key, accel_mods, 0);
336         gtk_window_add_accel_group (GTK_WINDOW (self), accel_group);
337
338         return MODEST_WINDOW(self);
339 }
340
341 static gboolean
342 live_search_visible_func (GtkTreeModel *model,
343                           GtkTreeIter  *iter,
344                           gchar        *text,
345                           gpointer      data)
346 {
347         gchar *display_name;
348         gboolean result;
349         gtk_tree_model_get(model, iter, 
350                            1, 
351                            &display_name, -1);
352
353         result = modest_text_utils_live_search_find (display_name, text);
354
355         g_free (display_name);
356
357         return result;
358 }
359
360
361 ModestWindow *
362 modest_accounts_window_new (void)
363 {
364         GtkWidget *action_area_box;
365         ModestWindow *self;
366         ModestAccountsWindowPrivate *priv = NULL;
367         HildonProgram *app;
368 #ifdef MODEST_TOOLKIT_HILDON2
369         GtkWidget *live_search;
370 #endif
371
372         if (pre_created_accounts_window) {
373                 self = MODEST_WINDOW (pre_created_accounts_window);
374                 pre_created_accounts_window = NULL;
375         } else {
376                 self = modest_accounts_window_new_real ();
377         }
378         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
379         priv->account_view  = GTK_WIDGET (modest_account_view_new (modest_runtime_get_account_mgr ()));
380 #ifdef MODEST_TOOLKIT_HILDON2
381         live_search = hildon_live_search_new ();
382         hildon_live_search_set_filter (HILDON_LIVE_SEARCH (live_search),
383                                        GTK_TREE_MODEL_FILTER (modest_account_view_get_filter (MODEST_ACCOUNT_VIEW (priv->account_view))));
384         hildon_live_search_set_visible_func (HILDON_LIVE_SEARCH (live_search), live_search_visible_func, self, NULL);
385         hildon_live_search_set_text_column (HILDON_LIVE_SEARCH (live_search),
386                                             1);
387         hildon_live_search_widget_hook (HILDON_LIVE_SEARCH (live_search), GTK_WIDGET (self), priv->account_view);
388         gtk_box_pack_start (GTK_BOX (priv->box), live_search, FALSE, FALSE, 0);
389 #endif
390
391         action_area_box = hildon_tree_view_get_action_area_box (GTK_TREE_VIEW (priv->account_view));
392         gtk_box_pack_start (GTK_BOX (action_area_box), priv->new_message_button, TRUE, TRUE, 0);
393         hildon_tree_view_set_action_area_visible (GTK_TREE_VIEW (priv->account_view), TRUE);
394         gtk_container_add (GTK_CONTAINER (priv->pannable), priv->account_view);
395
396         connect_signals (MODEST_ACCOUNTS_WINDOW (self));
397
398         app = hildon_program_get_instance ();
399         hildon_program_add_window (app, HILDON_WINDOW (self));
400         
401         /* Dont't restore settings here, 
402          * because it requires a gtk_widget_show(), 
403          * and we don't want to do that until later,
404          * so that the UI is not visible for non-menu D-Bus activation.
405          */
406
407         g_signal_connect (G_OBJECT (self), "map-event",
408                           G_CALLBACK (_modest_accounts_window_map_event),
409                           G_OBJECT (self));
410         g_signal_connect (G_OBJECT (self), "delete-event",
411                           G_CALLBACK (on_delete_event), self);
412         update_progress_hint (MODEST_ACCOUNTS_WINDOW (self));
413
414         row_count_changed (MODEST_ACCOUNTS_WINDOW (self));
415
416         gtk_window_set_title (GTK_WINDOW (self), _("mcen_ap_name"));
417
418         return self;
419 }
420
421
422 ModestAccountView *
423 modest_accounts_window_get_account_view (ModestAccountsWindow *self)
424 {
425         ModestAccountsWindowPrivate *priv = NULL;
426
427         g_return_val_if_fail (MODEST_IS_ACCOUNTS_WINDOW(self), FALSE);
428
429         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
430         
431         return MODEST_ACCOUNT_VIEW (priv->account_view);
432 }
433
434 static void 
435 setup_menu (ModestAccountsWindow *self)
436 {
437         g_return_if_fail (MODEST_IS_ACCOUNTS_WINDOW(self));
438
439         /* Settings menu buttons */
440         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_new_account"), NULL, 
441                                            APP_MENU_CALLBACK (modest_ui_actions_on_new_account), 
442                                            NULL);
443         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_sendandreceive"), NULL,
444                                            APP_MENU_CALLBACK (modest_ui_actions_on_send_receive),
445                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_send_receive_all));
446         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self),
447                                            dngettext(GETTEXT_PACKAGE,
448                                                      "mcen_me_edit_account",
449                                                      "mcen_me_edit_accounts",
450                                                      2),
451                                            NULL,
452                                            APP_MENU_CALLBACK (modest_ui_actions_on_accounts), 
453                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_edit_accounts));
454         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_globalsmtpservers"), NULL,
455                                            APP_MENU_CALLBACK (modest_ui_actions_on_smtp_servers),
456                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_tools_smtp_servers));
457         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_outbox_cancelsend"), NULL,
458                                            APP_MENU_CALLBACK (modest_ui_actions_cancel_send),
459                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_cancel_sending_all));
460         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_options"), NULL,
461                                            APP_MENU_CALLBACK (modest_ui_actions_on_settings), 
462                                            NULL);
463 }
464
465
466 static void
467 on_account_activated (GtkTreeView *account_view,
468                       GtkTreePath *path,
469                       GtkTreeViewColumn *column,
470                       ModestAccountsWindow *self)
471 {
472         ModestAccountsWindowPrivate *priv;
473         gchar* account_name; 
474         GtkWidget *new_window;
475         gboolean registered;
476         ModestProtocolType store_protocol;
477         gboolean mailboxes_protocol;
478
479         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
480
481         account_name = modest_account_view_get_path_account (MODEST_ACCOUNT_VIEW (priv->account_view), path);
482         if (!account_name)
483                 return;
484
485         /* If it's a multimailbox container, we have to show the mailboxes window */
486         store_protocol = modest_account_mgr_get_store_protocol (modest_runtime_get_account_mgr (), 
487                                                                 account_name);
488         mailboxes_protocol = 
489                 modest_protocol_registry_protocol_type_has_tag (modest_runtime_get_protocol_registry (),
490                                                                 store_protocol,
491                                                                 MODEST_PROTOCOL_REGISTRY_MULTI_MAILBOX_PROVIDER_PROTOCOLS);
492         if (mailboxes_protocol) {
493                 new_window = GTK_WIDGET (modest_mailboxes_window_new (account_name));
494         } else {
495
496                 new_window = GTK_WIDGET (modest_folder_window_new (NULL));
497                 modest_window_set_active_account (MODEST_WINDOW (new_window), account_name);
498         }
499
500         registered = modest_window_mgr_register_window (modest_runtime_get_window_mgr (), 
501                                                         MODEST_WINDOW (new_window),
502                                                         MODEST_WINDOW (self));
503
504         if (!registered) {
505                 gtk_widget_destroy (new_window);
506                 new_window = NULL;
507         } else {
508                 if (!mailboxes_protocol) {
509                         modest_folder_window_set_account (MODEST_FOLDER_WINDOW (new_window), account_name);
510                 }
511                 gtk_widget_show (new_window);
512         }
513         g_free (account_name);
514 }
515
516 static gboolean
517 _modest_accounts_window_map_event (GtkWidget *widget,
518                                    GdkEvent *event,
519                                    gpointer userdata)
520 {
521         ModestAccountsWindow *self = (ModestAccountsWindow *) userdata;
522         ModestAccountsWindowPrivate *priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
523
524         if (priv->progress_hint) {
525                 hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), TRUE);
526         }
527
528         return FALSE;
529 }
530
531 static gboolean
532 has_active_operations (ModestAccountsWindow *self)
533 {
534         GSList *operations = NULL, *node;
535         ModestMailOperationQueue *queue;
536         gboolean has_active = FALSE;
537
538         queue = modest_runtime_get_mail_operation_queue ();
539         operations = modest_mail_operation_queue_get_by_source (queue, G_OBJECT (self));
540
541         for (node = operations; node != NULL; node = g_slist_next (node)) {
542                 if (!modest_mail_operation_is_finished (MODEST_MAIL_OPERATION (node->data))) {
543                         has_active = TRUE;
544                         break;
545                 }
546         }
547
548         if (operations) {
549                 g_slist_foreach (operations, (GFunc) g_object_unref, NULL);
550                 g_slist_free (operations);
551         }
552
553         return has_active;
554 }
555
556 static void
557 update_progress_hint (ModestAccountsWindow *self)
558 {
559         ModestAccountsWindowPrivate *priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
560
561         if (has_active_operations (self)) {
562                 priv->progress_hint = TRUE;
563         } else {
564                 priv->progress_hint = FALSE;
565         }
566
567         if (!priv->progress_hint) {
568                 priv->progress_hint = modest_window_mgr_has_progress_operation (modest_runtime_get_window_mgr ());
569         }
570         
571         if (GTK_WIDGET_VISIBLE (self)) {
572                 hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), priv->progress_hint?1:0);
573         }
574 }
575
576 static void
577 on_progress_list_changed (ModestWindowMgr *mgr,
578                           ModestAccountsWindow *self)
579 {
580         update_progress_hint (self);
581 }
582
583 static void
584 on_row_inserted (GtkTreeModel *tree_model,
585                  GtkTreePath  *path,
586                  GtkTreeIter  *iter,
587                  gpointer      user_data)
588 {
589         ModestAccountsWindow *self;
590
591         self = (ModestAccountsWindow *) user_data;
592
593         row_count_changed (self);
594 }
595
596 static void
597 on_row_deleted (GtkTreeModel *tree_model,
598                 GtkTreePath  *path,
599                 gpointer      user_data)
600 {
601         ModestAccountsWindow *self;
602
603         self = (ModestAccountsWindow *) user_data;
604
605         row_count_changed (self);
606 }
607
608 static void 
609 row_count_changed (ModestAccountsWindow *self)
610 {
611         ModestAccountsWindowPrivate *priv;
612         GtkTreeModel *model;
613         gint count;
614
615         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
616         model = modest_account_view_get_model (MODEST_ACCOUNT_VIEW (priv->account_view));
617
618         count = gtk_tree_model_iter_n_children (model, NULL);
619
620         if (count == 0) {
621                 gtk_widget_hide (priv->account_view);
622                 gtk_widget_show (priv->no_accounts_container);
623                 g_debug ("%s: hiding accounts view", __FUNCTION__);
624         } else {
625                 gtk_widget_hide (priv->no_accounts_container);
626                 gtk_widget_show (priv->account_view);
627                 g_debug ("%s: showing accounts view", __FUNCTION__);
628         }
629         gtk_container_child_set (GTK_CONTAINER(priv->box), priv->pannable, 
630                                  "expand", count > 0,
631                                  "fill", count > 0,
632                                  NULL);
633 }
634
635 static void 
636 on_mail_operation_started (ModestMailOperation *mail_op,
637                            gpointer user_data)
638 {
639         ModestAccountsWindow *self;
640         ModestMailOperationTypeOperation op_type;
641         GObject *source = NULL;
642
643         self = MODEST_ACCOUNTS_WINDOW (user_data);
644         op_type = modest_mail_operation_get_type_operation (mail_op);
645         source = modest_mail_operation_get_source(mail_op);
646         if (G_OBJECT (self) == source) {
647                 update_progress_hint (self);
648         }
649         g_object_unref (source);
650 }
651
652 static void 
653 on_mail_operation_finished (ModestMailOperation *mail_op,
654                             gpointer user_data)
655 {
656         ModestAccountsWindow *self;
657
658         self = MODEST_ACCOUNTS_WINDOW (user_data);
659
660         /* Don't disable the progress hint if there are more pending
661            operations from this window */
662         update_progress_hint (self);
663
664         modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (self));
665 }
666
667 static void
668 on_queue_changed (ModestMailOperationQueue *queue,
669                   ModestMailOperation *mail_op,
670                   ModestMailOperationQueueNotification type,
671                   ModestAccountsWindow *self)
672 {
673         ModestAccountsWindowPrivate *priv;
674
675         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
676
677         /* If this operations was created by another window, do nothing */
678         if (!modest_mail_operation_is_mine (mail_op, G_OBJECT(self))) 
679                 return;
680
681         if (type == MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED) {
682                 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
683                                                                G_OBJECT (mail_op),
684                                                                "operation-started",
685                                                                G_CALLBACK (on_mail_operation_started),
686                                                                self);
687                 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
688                                                                G_OBJECT (mail_op),
689                                                                "operation-finished",
690                                                                G_CALLBACK (on_mail_operation_finished),
691                                                                self);
692         } else if (type == MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED) {
693                 priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
694                                                                   G_OBJECT (mail_op),
695                                                                   "operation-started");
696                 priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
697                                                                   G_OBJECT (mail_op),
698                                                                   "operation-finished");
699         }
700 }
701
702 void 
703 modest_accounts_window_pre_create (void)
704 {
705         static gboolean pre_created = FALSE;
706         if (!pre_created) {
707                 pre_created = TRUE;
708                 pre_created_accounts_window = GTK_WIDGET (modest_accounts_window_new_real ());
709         }
710 }
711
712
713 static gboolean
714 on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
715 {
716         ModestAccountsWindowPrivate *priv;
717         HildonPannableArea *pannable;
718
719         if (event->type == GDK_KEY_RELEASE)
720                 return FALSE;
721
722         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(user_data);
723
724         pannable = HILDON_PANNABLE_AREA (priv->pannable);
725
726         switch (event->keyval) {
727
728         case GDK_Up:
729                 modest_maemo_utils_scroll_pannable(pannable, 0, -1);
730                 break;
731
732         case GDK_Down:
733                 modest_maemo_utils_scroll_pannable(pannable, 0, 1);
734                 break;
735         }
736
737         return FALSE;
738 }
739
740 static gboolean
741 on_delete_event (GtkWidget *widget,
742                  GdkEvent *event,
743                  gpointer userdata)
744 {
745         ModestAccountsWindowPrivate *priv;
746
747         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (widget);
748
749         modest_account_view_set_show_last_update (MODEST_ACCOUNT_VIEW (priv->account_view), FALSE);
750
751         gtk_widget_queue_resize (widget);
752
753         gdk_window_process_updates (priv->account_view->window, TRUE);
754
755         hildon_gtk_window_take_screenshot (GTK_WINDOW (widget), TRUE);
756
757         modest_account_view_set_show_last_update (MODEST_ACCOUNT_VIEW (priv->account_view), TRUE);
758
759         return FALSE;
760
761 }
762