Use folder view activity feedback in folder window
[modest] / src / hildon2 / modest-folder-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-folder-window.h>
31 #include <modest-osso-state-saving.h>
32 #include <libosso.h>
33 #include <hildon/hildon-pannable-area.h>
34 #include <modest-window-mgr.h>
35 #include <modest-signal-mgr.h>
36 #include <modest-runtime.h>
37 #include <modest-platform.h>
38 #include <modest-maemo-utils.h>
39 #include <modest-icon-names.h>
40 #include <modest-ui-constants.h>
41 #include <modest-account-mgr.h>
42 #include <modest-account-mgr-helpers.h>
43 #include <modest-defs.h>
44 #include <modest-ui-actions.h>
45 #include <modest-window.h>
46 #include <hildon/hildon-program.h>
47 #include <hildon/hildon-banner.h>
48 #include <hildon/hildon-button.h>
49 #include <tny-account-store-view.h>
50 #include <modest-header-window.h>
51 #include <modest-ui-dimming-rules.h>
52 #include <modest-ui-dimming-manager.h>
53 #include <modest-window-priv.h>
54 #include "modest-text-utils.h"
55 #include "modest-tny-account.h"
56
57 typedef enum {
58         EDIT_MODE_COMMAND_MOVE = 1,
59         EDIT_MODE_COMMAND_DELETE = 2,
60         EDIT_MODE_COMMAND_RENAME = 3,
61 } EditModeCommand;
62
63 /* 'private'/'protected' functions */
64 static void modest_folder_window_class_init  (ModestFolderWindowClass *klass);
65 static void modest_folder_window_init        (ModestFolderWindow *obj);
66 static void modest_folder_window_finalize    (GObject *obj);
67
68 static void connect_signals (ModestFolderWindow *self);
69 static void modest_folder_window_disconnect_signals (ModestWindow *self);
70
71 static void on_folder_activated (ModestFolderView *folder_view,
72                                  TnyFolder *folder,
73                                  gpointer userdata);
74 static void setup_menu (ModestFolderWindow *self);
75
76 static void set_delete_edit_mode (GtkButton *button,
77                                   ModestFolderWindow *self);
78 static void set_moveto_edit_mode (GtkButton *button,
79                                   ModestFolderWindow *self);
80 static void set_rename_edit_mode (GtkButton *button,
81                                   ModestFolderWindow *self);
82 static void modest_folder_window_pack_toolbar (ModestHildon2Window *self,
83                                                GtkPackType pack_type,
84                                                GtkWidget *toolbar);
85 static void edit_mode_changed (ModestFolderWindow *folder_window,
86                                gint edit_mode_id,
87                                gboolean enabled,
88                                ModestFolderWindow *self);
89 static void on_progress_list_changed (ModestWindowMgr *mgr,
90                                       ModestFolderWindow *self);
91 static gboolean on_map_event (GtkWidget *widget,
92                               GdkEvent *event,
93                               gpointer userdata);
94 static void update_progress_hint (ModestFolderWindow *self);
95 static void on_queue_changed    (ModestMailOperationQueue *queue,
96                                  ModestMailOperation *mail_op,
97                                  ModestMailOperationQueueNotification type,
98                                  ModestFolderWindow *self);
99 static void on_activity_changed (ModestFolderView *view,
100                                  gboolean activity,
101                                  ModestFolderWindow *folder_window);
102
103 typedef struct _ModestFolderWindowPrivate ModestFolderWindowPrivate;
104 struct _ModestFolderWindowPrivate {
105
106         GtkWidget *folder_view;
107         GtkWidget *top_vbox;
108         GtkWidget *new_message_button;
109
110         /* signals */
111         GSList *sighandlers;
112
113         gchar *current_store_account;
114         gboolean progress_hint;
115         guint queue_change_handler;
116 };
117 #define MODEST_FOLDER_WINDOW_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE((o), \
118                                                                           MODEST_TYPE_FOLDER_WINDOW, \
119                                                                           ModestFolderWindowPrivate))
120
121 /* globals */
122 static GtkWindowClass *parent_class = NULL;
123
124 /************************************************************************/
125
126 GType
127 modest_folder_window_get_type (void)
128 {
129         static GType my_type = 0;
130         if (!my_type) {
131                 static const GTypeInfo my_info = {
132                         sizeof(ModestFolderWindowClass),
133                         NULL,           /* base init */
134                         NULL,           /* base finalize */
135                         (GClassInitFunc) modest_folder_window_class_init,
136                         NULL,           /* class finalize */
137                         NULL,           /* class data */
138                         sizeof(ModestFolderWindow),
139                         1,              /* n_preallocs */
140                         (GInstanceInitFunc) modest_folder_window_init,
141                         NULL
142                 };
143                 my_type = g_type_register_static (MODEST_TYPE_HILDON2_WINDOW,
144                                                   "ModestFolderWindow",
145                                                   &my_info, 0);
146         }
147         return my_type;
148 }
149
150 static void
151 modest_folder_window_class_init (ModestFolderWindowClass *klass)
152 {
153         GObjectClass *gobject_class;
154         gobject_class = (GObjectClass*) klass;
155         ModestWindowClass *modest_window_class = (ModestWindowClass *) klass;
156         ModestHildon2WindowClass *modest_hildon2_window_class = (ModestHildon2WindowClass *) klass;
157
158         parent_class            = g_type_class_peek_parent (klass);
159         gobject_class->finalize = modest_folder_window_finalize;
160
161         g_type_class_add_private (gobject_class, sizeof(ModestFolderWindowPrivate));
162         
163         modest_window_class->disconnect_signals_func = modest_folder_window_disconnect_signals;
164         modest_hildon2_window_class->pack_toolbar_func = modest_folder_window_pack_toolbar;
165 }
166
167 static void
168 modest_folder_window_init (ModestFolderWindow *obj)
169 {
170         ModestFolderWindowPrivate *priv;
171
172         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(obj);
173
174         priv->sighandlers = NULL;
175         
176         priv->folder_view = NULL;
177
178         priv->top_vbox = NULL;
179
180         modest_window_mgr_register_help_id (modest_runtime_get_window_mgr(),
181                                             GTK_WINDOW(obj),
182                                             "applications_email_folderview");
183         priv->progress_hint = FALSE;
184         priv->current_store_account = NULL;
185         priv->queue_change_handler = 0;
186 }
187
188 static void
189 modest_folder_window_finalize (GObject *obj)
190 {
191         ModestFolderWindowPrivate *priv;
192
193         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(obj);
194
195         if (priv->current_store_account) {
196                 g_free (priv->current_store_account);
197                 priv->current_store_account = NULL;
198         }
199
200         /* Sanity check: shouldn't be needed, the window mgr should
201            call this function before */
202         modest_folder_window_disconnect_signals (MODEST_WINDOW (obj));  
203
204         G_OBJECT_CLASS(parent_class)->finalize (obj);
205 }
206
207 static void
208 modest_folder_window_disconnect_signals (ModestWindow *self)
209 {
210         ModestFolderWindowPrivate *priv;
211         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(self);
212
213         if (g_signal_handler_is_connected (G_OBJECT (modest_runtime_get_mail_operation_queue ()), 
214                                            priv->queue_change_handler))
215                 g_signal_handler_disconnect (G_OBJECT (modest_runtime_get_mail_operation_queue ()), 
216                                              priv->queue_change_handler);
217
218         modest_signal_mgr_disconnect_all_and_destroy (priv->sighandlers);
219         priv->sighandlers = NULL;
220 }
221
222 static void
223 on_visible_account_changed (ModestFolderView *folder_view,
224                             const gchar *account_id,
225                             gpointer user_data)
226 {
227         if (account_id) {
228                 TnyAccount *acc = 
229                         modest_tny_account_store_get_tny_account_by (modest_runtime_get_account_store(),
230                                                                      MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
231                                                                      account_id);
232                 if (acc) {
233                         const gchar *name;
234                         const gchar *mailbox;
235                         gchar *title = NULL;
236                         ModestFolderWindowPrivate *priv;
237
238                         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (user_data);
239
240                         mailbox = modest_folder_view_get_mailbox (MODEST_FOLDER_VIEW (priv->folder_view));
241                         if (mailbox) {
242                                 title = g_strdup (mailbox);
243                         } else {
244                                 name = modest_tny_account_get_parent_modest_account_name_for_server_account (acc);
245                                 title = modest_account_mgr_get_display_name (modest_runtime_get_account_mgr(),
246                                                                              name);
247                         }
248                         if (title) {
249                                 gtk_window_set_title (GTK_WINDOW (user_data), title);
250                                 g_free (title);
251                         } else {
252                                 gtk_window_set_title (GTK_WINDOW (user_data), _("mcen_ap_name"));
253                         }
254                         g_object_unref (acc);
255                 }
256         } else {
257                 gtk_window_set_title (GTK_WINDOW (user_data), _("mcen_ap_name"));
258         }
259 }
260
261 static void
262 connect_signals (ModestFolderWindow *self)
263 {
264         ModestFolderWindowPrivate *priv;
265
266         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(self);
267
268         /* folder view */
269         priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers, 
270                                                        G_OBJECT (priv->folder_view), "folder-activated", 
271                                                        G_CALLBACK (on_folder_activated), self);
272
273         priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
274                                                        G_OBJECT (modest_runtime_get_window_mgr ()),
275                                                        "progress-list-changed",
276                                                        G_CALLBACK (on_progress_list_changed), self);
277
278         priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
279                                                        G_OBJECT (priv->folder_view),
280                                                        "visible-account-changed",
281                                                        G_CALLBACK (on_visible_account_changed), self);
282
283         priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
284                                                        G_OBJECT (priv->folder_view),
285                                                        "activity-changed",
286                                                        G_CALLBACK (on_activity_changed), self);
287
288         priv->sighandlers = 
289                 modest_signal_mgr_connect (priv->sighandlers,
290                                            G_OBJECT (priv->new_message_button),
291                                            "clicked",
292                                            G_CALLBACK (modest_ui_actions_on_new_msg), self);
293
294 }
295
296 ModestWindow *
297 modest_folder_window_new (TnyFolderStoreQuery *query)
298 {
299         ModestFolderWindow *self = NULL;        
300         ModestFolderWindowPrivate *priv = NULL;
301         HildonProgram *app;
302         GdkPixbuf *window_icon;
303         GtkWidget *pannable;
304         GtkWidget *action_area_box;
305         GdkPixbuf *new_message_pixbuf;
306         guint accel_key;
307         GdkModifierType accel_mods;
308         GtkAccelGroup *accel_group;
309         GtkWidget *top_alignment;
310         
311         self  = MODEST_FOLDER_WINDOW(g_object_new(MODEST_TYPE_FOLDER_WINDOW, NULL));
312         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(self);
313
314         pannable = hildon_pannable_area_new ();
315
316         priv->queue_change_handler =
317                 g_signal_connect (G_OBJECT (modest_runtime_get_mail_operation_queue ()),
318                                   "queue-changed",
319                                   G_CALLBACK (on_queue_changed),
320                                   self);
321
322         priv->folder_view  = modest_platform_create_folder_view (query);
323         modest_folder_view_set_cell_style (MODEST_FOLDER_VIEW (priv->folder_view),
324                                            MODEST_FOLDER_VIEW_CELL_STYLE_COMPACT);
325         modest_folder_view_set_filter (MODEST_FOLDER_VIEW (priv->folder_view), 
326                                        MODEST_FOLDER_VIEW_FILTER_HIDE_ACCOUNTS);
327
328         g_signal_connect (G_OBJECT (self), "edit-mode-changed",
329                           G_CALLBACK (edit_mode_changed), (gpointer) self);
330
331         action_area_box = hildon_tree_view_get_action_area_box (GTK_TREE_VIEW (priv->folder_view));
332         priv->new_message_button = hildon_button_new (0, HILDON_BUTTON_ARRANGEMENT_HORIZONTAL);
333
334         hildon_button_set_title (HILDON_BUTTON (priv->new_message_button), _("mcen_ti_new_message"));
335         new_message_pixbuf = modest_platform_get_icon ("general_add", MODEST_ICON_SIZE_BIG);
336         hildon_button_set_image (HILDON_BUTTON (priv->new_message_button), gtk_image_new_from_pixbuf (new_message_pixbuf));
337         g_object_unref (new_message_pixbuf);
338
339         gtk_box_pack_start (GTK_BOX (action_area_box), priv->new_message_button, TRUE, TRUE, 0);
340         gtk_widget_show_all (priv->new_message_button);
341         hildon_tree_view_set_action_area_visible (GTK_TREE_VIEW (priv->folder_view), TRUE);
342         
343         setup_menu (self);
344
345         /* Set account store */
346         tny_account_store_view_set_account_store (TNY_ACCOUNT_STORE_VIEW (priv->folder_view),
347                                                   TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()));
348
349         priv->top_vbox = gtk_vbox_new (0, FALSE);
350         top_alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
351         gtk_alignment_set_padding (GTK_ALIGNMENT (top_alignment),
352                                    0, 0,
353                                    HILDON_MARGIN_DOUBLE, HILDON_MARGIN_DOUBLE);
354
355         gtk_container_add (GTK_CONTAINER (pannable), priv->folder_view);
356         gtk_container_add (GTK_CONTAINER (top_alignment), pannable);
357         gtk_box_pack_end (GTK_BOX (priv->top_vbox), top_alignment, TRUE, TRUE, 0);
358         gtk_container_add (GTK_CONTAINER (self), priv->top_vbox);
359
360         gtk_widget_show (priv->folder_view);
361         gtk_widget_show (pannable);
362         gtk_widget_show (priv->top_vbox);
363         gtk_widget_show (top_alignment);
364
365         connect_signals (MODEST_FOLDER_WINDOW (self));
366
367         /* Load previous osso state, for instance if we are being restored from 
368          * hibernation:  */
369         modest_osso_load_state ();
370
371         /* Get device name */
372         modest_maemo_utils_get_device_name ();
373
374         app = hildon_program_get_instance ();
375         hildon_program_add_window (app, HILDON_WINDOW (self));
376         
377         /* Set window icon */
378         window_icon = modest_platform_get_icon (MODEST_APP_ICON, MODEST_ICON_SIZE_BIG);
379         if (window_icon) {
380                 gtk_window_set_icon (GTK_WINDOW (self), window_icon);
381                 g_object_unref (window_icon);
382         }
383
384         /* Dont't restore settings here, 
385          * because it requires a gtk_widget_show(), 
386          * and we don't want to do that until later,
387          * so that the UI is not visible for non-menu D-Bus activation.
388          */
389
390         /* Register edit modes */
391         modest_hildon2_window_register_edit_mode (MODEST_HILDON2_WINDOW (self), 
392                                                   EDIT_MODE_COMMAND_DELETE,
393                                                   _("mcen_ti_edit_folder_delete"), 
394                                                   _HL("wdgt_bd_delete"),
395                                                   GTK_TREE_VIEW (priv->folder_view),
396                                                   GTK_SELECTION_SINGLE,
397                                                   EDIT_MODE_CALLBACK (modest_ui_actions_on_edit_mode_delete_folder));
398         modest_hildon2_window_register_edit_mode (MODEST_HILDON2_WINDOW (self), 
399                                                   EDIT_MODE_COMMAND_MOVE,
400                                                   _("mcen_ti_edit_move_folder"), 
401                                                   _HL("wdgt_bd_move"),
402                                                   GTK_TREE_VIEW (priv->folder_view),
403                                                   GTK_SELECTION_SINGLE,
404                                                   EDIT_MODE_CALLBACK (modest_ui_actions_on_edit_mode_move_to)); 
405         modest_hildon2_window_register_edit_mode (MODEST_HILDON2_WINDOW (self), 
406                                                   EDIT_MODE_COMMAND_RENAME,
407                                                   _("mcen_ti_edit_rename_folder"), 
408                                                   _HL("wdgt_bd_rename"),
409                                                   GTK_TREE_VIEW (priv->folder_view),
410                                                   GTK_SELECTION_SINGLE,
411                                                   EDIT_MODE_CALLBACK (modest_ui_actions_on_edit_mode_rename_folder));
412         
413         g_signal_connect (G_OBJECT (self), "map-event",
414                           G_CALLBACK (on_map_event),
415                           G_OBJECT (self));
416         update_progress_hint (self);
417
418         accel_group = gtk_accel_group_new ();
419         gtk_accelerator_parse ("<Control>n", &accel_key, &accel_mods);
420         gtk_widget_add_accelerator (priv->new_message_button, "clicked", accel_group,
421                                     accel_key, accel_mods, 0);
422         gtk_window_add_accel_group (GTK_WINDOW (self), accel_group);
423
424         return MODEST_WINDOW(self);
425 }
426
427 ModestFolderView *
428 modest_folder_window_get_folder_view (ModestFolderWindow *self)
429 {
430         ModestFolderWindowPrivate *priv = NULL;
431
432         g_return_val_if_fail (MODEST_IS_FOLDER_WINDOW(self), FALSE);
433
434         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
435         
436         return MODEST_FOLDER_VIEW (priv->folder_view);
437 }
438
439 void
440 modest_folder_window_set_account (ModestFolderWindow *self,
441                                   const gchar *account_name)
442 {
443         ModestFolderWindowPrivate *priv = NULL;
444         ModestAccountMgr *mgr;
445         ModestAccountSettings *settings = NULL;
446         ModestServerAccountSettings *store_settings = NULL;
447
448         g_return_if_fail (MODEST_IS_FOLDER_WINDOW(self));
449
450         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
451
452         /* Get account data */
453         mgr = modest_runtime_get_account_mgr ();
454
455         settings = modest_account_mgr_load_account_settings (mgr, account_name);
456         if (!settings)
457                 goto free_refs;
458
459         store_settings = modest_account_settings_get_store_settings (settings);
460         if (!store_settings)
461                 goto free_refs;
462
463         if (priv->current_store_account != NULL)
464                 g_free (priv->current_store_account);
465         priv->current_store_account = g_strdup (modest_server_account_settings_get_account_name (store_settings));
466
467         modest_folder_view_set_account_id_of_visible_server_account
468                 (MODEST_FOLDER_VIEW (priv->folder_view),
469                  priv->current_store_account);
470
471         modest_window_set_active_account (MODEST_WINDOW (self), account_name);
472         update_progress_hint (self);
473
474 free_refs:
475         if (store_settings)
476                 g_object_unref (store_settings);
477         if (settings)
478                 g_object_unref (settings);
479
480 }
481
482 void
483 modest_folder_window_set_mailbox (ModestFolderWindow *self,
484                                   const gchar *mailbox)
485 {
486         ModestFolderWindowPrivate *priv = NULL;
487
488         g_return_if_fail (MODEST_IS_FOLDER_WINDOW(self));
489         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
490
491         modest_folder_view_set_mailbox (MODEST_FOLDER_VIEW (priv->folder_view), mailbox);
492
493 }
494
495 static void
496 setup_menu (ModestFolderWindow *self)
497 {
498         g_return_if_fail (MODEST_IS_FOLDER_WINDOW(self));
499
500         /* folders actions */
501         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_new_folder"), NULL,
502                                            APP_MENU_CALLBACK (modest_ui_actions_on_new_folder),
503                                            NULL);
504         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_rename_folder"), NULL,
505                                            APP_MENU_CALLBACK (set_rename_edit_mode),
506                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_rename_folder));
507         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_move_folder"), NULL,
508                                            APP_MENU_CALLBACK (set_moveto_edit_mode),
509                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_folder_window_move_to));
510         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_delete_folder"), NULL,
511                                            APP_MENU_CALLBACK (set_delete_edit_mode),
512                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_folder_window_delete));
513
514         /* send receive actions should be only one visible always */
515         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), 
516                                            _("mcen_me_inbox_sendandreceive"), 
517                                            NULL,
518                                            APP_MENU_CALLBACK (modest_ui_actions_on_send_receive),
519                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_send_receive));
520
521         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_outbox_cancelsend"), NULL,
522                                            APP_MENU_CALLBACK (modest_ui_actions_cancel_send),
523                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_cancel_sending_all));
524 }
525
526 static void
527 on_folder_activated (ModestFolderView *folder_view,
528                      TnyFolder *folder,
529                      gpointer userdata)
530 {
531         ModestFolderWindowPrivate *priv = NULL;
532         ModestWindow *headerwin;
533         ModestFolderWindow *self = (ModestFolderWindow *) userdata;
534
535         g_return_if_fail (MODEST_IS_FOLDER_WINDOW(self));
536
537         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
538
539         if (!folder)
540                 return;
541
542         if (!TNY_IS_FOLDER (folder))
543                 return;
544
545         /* We cannot open noselect folders (fake ones) */
546         if (tny_folder_get_caps (folder) & TNY_FOLDER_CAPS_NOSELECT)
547                 return;
548
549         headerwin = modest_header_window_new (folder, modest_window_get_active_account (MODEST_WINDOW (self)));
550
551         if (modest_window_mgr_register_window (modest_runtime_get_window_mgr (),
552                                                MODEST_WINDOW (headerwin),
553                                                MODEST_WINDOW (self))) {
554                 gtk_widget_show (GTK_WIDGET (headerwin));
555         } else {
556                 gtk_widget_destroy (GTK_WIDGET (headerwin));
557                 headerwin = NULL;
558         }
559 }
560
561 static void
562 set_delete_edit_mode (GtkButton *button,
563                       ModestFolderWindow *self)
564 {
565         modest_hildon2_window_set_edit_mode (MODEST_HILDON2_WINDOW (self), EDIT_MODE_COMMAND_DELETE);
566 }
567
568 static void
569 set_moveto_edit_mode (GtkButton *button,
570                       ModestFolderWindow *self)
571 {
572         modest_hildon2_window_set_edit_mode (MODEST_HILDON2_WINDOW (self), EDIT_MODE_COMMAND_MOVE);
573 }
574
575 static void
576 set_rename_edit_mode (GtkButton *button,
577                       ModestFolderWindow *self)
578 {
579         modest_hildon2_window_set_edit_mode (MODEST_HILDON2_WINDOW (self), EDIT_MODE_COMMAND_RENAME);
580 }
581
582 static void
583 modest_folder_window_pack_toolbar (ModestHildon2Window *self,
584                                    GtkPackType pack_type,
585                                    GtkWidget *toolbar)
586 {
587         ModestFolderWindowPrivate *priv;
588
589         g_return_if_fail (MODEST_IS_FOLDER_WINDOW (self));
590         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
591
592         if (pack_type == GTK_PACK_START) {
593                 gtk_box_pack_start (GTK_BOX (priv->top_vbox), toolbar, FALSE, FALSE, 0);
594         } else {
595                 gtk_box_pack_end (GTK_BOX (priv->top_vbox), toolbar, FALSE, FALSE, 0);
596         }
597 }
598
599 static void 
600 edit_mode_changed (ModestFolderWindow *folder_window,
601                    gint edit_mode_id,
602                    gboolean enabled,
603                    ModestFolderWindow *self)
604 {
605         ModestFolderWindowPrivate *priv;
606         ModestFolderViewFilter filter = MODEST_FOLDER_VIEW_FILTER_NONE;
607
608         g_return_if_fail (MODEST_IS_FOLDER_WINDOW (self));
609         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
610
611         switch (edit_mode_id) {
612         case EDIT_MODE_COMMAND_MOVE:
613                 filter = MODEST_FOLDER_VIEW_FILTER_MOVEABLE;
614                 break;
615         case EDIT_MODE_COMMAND_DELETE:
616                 filter = MODEST_FOLDER_VIEW_FILTER_DELETABLE;
617                 break;
618         case EDIT_MODE_COMMAND_RENAME:
619                 filter = MODEST_FOLDER_VIEW_FILTER_RENAMEABLE;
620                 break;
621         case MODEST_HILDON2_WINDOW_EDIT_MODE_NONE:
622                 filter = MODEST_FOLDER_VIEW_FILTER_NONE;
623                 break;
624         }
625
626         hildon_tree_view_set_action_area_visible (GTK_TREE_VIEW (priv->folder_view), !enabled);
627         if (enabled) {
628                 modest_folder_view_set_filter (MODEST_FOLDER_VIEW (priv->folder_view), 
629                                                filter);
630         } else {
631                 GtkTreeSelection *sel;
632
633                 /* Unselect all. This will prevent us from keeping a
634                    reference to a TnyObject that we don't want to
635                    have */
636                 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->folder_view));
637                 gtk_tree_selection_unselect_all (sel);
638
639                 modest_folder_view_unset_filter (MODEST_FOLDER_VIEW (priv->folder_view), 
640                                                  filter);
641         }
642 }
643
644 static gboolean 
645 on_map_event (GtkWidget *widget,
646               GdkEvent *event,
647               gpointer userdata)
648 {
649         ModestFolderWindow *self = (ModestFolderWindow *) userdata;
650         ModestFolderWindowPrivate *priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
651
652         if (priv->progress_hint) {
653                 hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), TRUE);
654         }
655
656         return FALSE;
657 }
658
659 static gboolean
660 has_active_operations (ModestFolderWindow *self)
661 {
662         GSList *operations = NULL, *node;
663         ModestMailOperationQueue *queue;
664         gboolean has_active = FALSE;
665
666         queue = modest_runtime_get_mail_operation_queue ();
667         operations = modest_mail_operation_queue_get_by_source (queue, G_OBJECT (self));
668
669         for (node = operations; node != NULL; node = g_slist_next (node)) {
670                 if (!modest_mail_operation_is_finished (MODEST_MAIL_OPERATION (node->data))) {
671                         has_active = TRUE;
672                         break;
673                 }
674         }
675
676         if (operations) {
677                 g_slist_foreach (operations, (GFunc) g_object_unref, NULL);
678                 g_slist_free (operations);
679         }
680
681         return has_active;
682 }
683
684 static void
685 update_progress_hint (ModestFolderWindow *self)
686 {
687         ModestFolderWindowPrivate *priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
688
689         if (has_active_operations (self)) {
690                 priv->progress_hint = TRUE;
691         } else {
692                 priv->progress_hint = FALSE;
693         }
694
695         if (!priv->progress_hint && priv->current_store_account) {
696                 priv->progress_hint = modest_window_mgr_has_progress_operation_on_account (modest_runtime_get_window_mgr (),
697                                                                                            priv->current_store_account);
698         }
699
700         if (!priv->progress_hint) {
701                 priv->progress_hint = modest_folder_view_get_activity (MODEST_FOLDER_VIEW (priv->folder_view));
702         }
703
704         modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (self));
705
706         if (GTK_WIDGET_VISIBLE (self)) {
707                 hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), priv->progress_hint ? 1:0);
708         }
709 }
710
711 static void
712 on_progress_list_changed (ModestWindowMgr *mgr,
713                           ModestFolderWindow *self)
714 {
715         update_progress_hint (self);
716 }
717
718 gboolean
719 modest_folder_window_transfer_mode_enabled (ModestFolderWindow *self)
720 {
721         ModestFolderWindowPrivate *priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
722
723         return priv->progress_hint;
724 }
725
726 static void 
727 on_mail_operation_started (ModestMailOperation *mail_op,
728                            gpointer user_data)
729 {
730         ModestFolderWindow *self;
731         ModestMailOperationTypeOperation op_type;
732         GObject *source = NULL;
733
734         self = MODEST_FOLDER_WINDOW (user_data);
735         op_type = modest_mail_operation_get_type_operation (mail_op);
736         source = modest_mail_operation_get_source(mail_op);
737         if (G_OBJECT (self) == source) {
738                 update_progress_hint (self);
739         }
740         g_object_unref (source);
741 }
742
743 static void 
744 on_mail_operation_finished (ModestMailOperation *mail_op,
745                             gpointer user_data)
746 {
747         ModestFolderWindow *self;
748
749         self = MODEST_FOLDER_WINDOW (user_data);
750
751         /* Don't disable the progress hint if there are more pending
752            operations from this window */
753         update_progress_hint (self);
754
755         modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (self));
756 }
757
758 static void
759 on_queue_changed (ModestMailOperationQueue *queue,
760                   ModestMailOperation *mail_op,
761                   ModestMailOperationQueueNotification type,
762                   ModestFolderWindow *self)
763 {
764         ModestFolderWindowPrivate *priv;
765
766         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
767
768         /* If this operations was created by another window, do nothing */
769         if (!modest_mail_operation_is_mine (mail_op, G_OBJECT(self))) 
770                 return;
771
772         if (type == MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED) {
773                 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
774                                                                G_OBJECT (mail_op),
775                                                                "operation-started",
776                                                                G_CALLBACK (on_mail_operation_started),
777                                                                self);
778                 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
779                                                                G_OBJECT (mail_op),
780                                                                "operation-finished",
781                                                                G_CALLBACK (on_mail_operation_finished),
782                                                                self);
783         } else if (type == MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED) {
784                 priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
785                                                                   G_OBJECT (mail_op),
786                                                                   "operation-started");
787                 priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
788                                                                   G_OBJECT (mail_op),
789                                                                   "operation-finished");
790         }
791 }
792
793 static void
794 on_activity_changed (ModestFolderView *view,
795                      gboolean activity,
796                      ModestFolderWindow *folder_window)
797 {
798         g_return_if_fail (MODEST_IS_FOLDER_WINDOW (folder_window));
799
800         update_progress_hint (folder_window);
801 }