Do not assume that ModestWindow's are GtkWindow's
[modest] / src / widgets / 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-scrollable.h>
32 #include <modest-window-mgr.h>
33 #include <modest-signal-mgr.h>
34 #include <modest-runtime.h>
35 #include <modest-platform.h>
36 #include <modest-icon-names.h>
37 #include <modest-ui-constants.h>
38 #include <modest-account-mgr.h>
39 #include <modest-account-mgr-helpers.h>
40 #include <modest-defs.h>
41 #include <modest-ui-actions.h>
42 #include <modest-window.h>
43 #ifdef MODEST_TOOLKIT_HILDON2
44 #include <modest-maemo-utils.h>
45 #include <hildon/hildon-program.h>
46 #include <hildon/hildon-banner.h>
47 #include <hildon/hildon-button.h>
48 #endif
49 #include <tny-account-store-view.h>
50 #include <tny-gtk-folder-list-store.h>
51 #include <modest-header-window.h>
52 #include <modest-ui-dimming-rules.h>
53 #include <modest-ui-dimming-manager.h>
54 #include <modest-window-priv.h>
55 #include "modest-text-utils.h"
56 #include "modest-tny-account.h"
57 #include "modest-account-protocol.h"
58 #include <gdk/gdkkeysyms.h>
59
60 #ifdef MODEST_TOOLKIT_HILDON2
61 typedef enum {
62         EDIT_MODE_COMMAND_MOVE = 1,
63         EDIT_MODE_COMMAND_DELETE = 2,
64         EDIT_MODE_COMMAND_RENAME = 3,
65 } EditModeCommand;
66 #endif
67
68 /* 'private'/'protected' functions */
69 static void modest_folder_window_class_init  (ModestFolderWindowClass *klass);
70 static void modest_folder_window_init        (ModestFolderWindow *obj);
71 static void modest_folder_window_finalize    (GObject *obj);
72 static void modest_folder_window_dispose     (GObject *obj);
73
74 static void connect_signals (ModestFolderWindow *self);
75 static void modest_folder_window_disconnect_signals (ModestWindow *self);
76
77 static void on_folder_activated (ModestFolderView *folder_view,
78                                  TnyFolder *folder,
79                                  gpointer userdata);
80 static void setup_menu (ModestFolderWindow *self);
81
82 #ifdef MODEST_TOOLKIT_HILDON2
83 static void set_delete_edit_mode (GtkButton *button,
84                                   ModestFolderWindow *self);
85 static void set_moveto_edit_mode (GtkButton *button,
86                                   ModestFolderWindow *self);
87 static void set_rename_edit_mode (GtkButton *button,
88                                   ModestFolderWindow *self);
89 #endif
90 static void modest_folder_window_pack_toolbar (ModestWindow *self,
91                                                GtkPackType pack_type,
92                                                GtkWidget *toolbar);
93 #ifdef MODEST_TOOLKIT_HILDON2
94 static void edit_mode_changed (ModestFolderWindow *folder_window,
95                                gint edit_mode_id,
96                                gboolean enabled,
97                                ModestFolderWindow *self);
98 #endif
99 static void on_progress_list_changed (ModestWindowMgr *mgr,
100                                       ModestFolderWindow *self);
101 static gboolean on_map_event (GtkWidget *widget,
102                               GdkEvent *event,
103                               gpointer userdata);
104 static void update_progress_hint (ModestFolderWindow *self);
105 static void on_queue_changed    (ModestMailOperationQueue *queue,
106                                  ModestMailOperation *mail_op,
107                                  ModestMailOperationQueueNotification type,
108                                  ModestFolderWindow *self);
109 static void on_activity_changed (ModestFolderView *view,
110                                  gboolean activity,
111                                  ModestFolderWindow *folder_window);
112 static void on_visible_account_changed (ModestFolderView *folder_view,
113                                         const gchar *account_id,
114                                         gpointer user_data);
115 static void on_account_changed (TnyAccountStore *account_store,
116                                 TnyAccount *account,
117                                 gpointer user_data);
118 #ifdef MODEST_TOOLKIT_HILDON2
119 static gboolean on_key_press(GtkWidget *widget,
120                              GdkEventKey *event,
121                              gpointer user_data);
122 #endif
123
124 typedef struct _ModestFolderWindowPrivate ModestFolderWindowPrivate;
125 struct _ModestFolderWindowPrivate {
126
127         GtkWidget *folder_view;
128         GtkWidget *top_vbox;
129 #ifdef MODEST_TOOLKIT_HILDON2
130         GtkWidget *new_message_button;
131 #endif
132
133         /* signals */
134         GSList *sighandlers;
135
136         gchar *current_store_account;
137         gboolean progress_hint;
138         guint queue_change_handler;
139 };
140 #define MODEST_FOLDER_WINDOW_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE((o), \
141                                                                           MODEST_TYPE_FOLDER_WINDOW, \
142                                                                           ModestFolderWindowPrivate))
143
144 /* globals */
145 static GtkWindowClass *parent_class = NULL;
146
147 /************************************************************************/
148
149 GType
150 modest_folder_window_get_type (void)
151 {
152         static GType my_type = 0;
153         if (!my_type) {
154                 static const GTypeInfo my_info = {
155                         sizeof(ModestFolderWindowClass),
156                         NULL,           /* base init */
157                         NULL,           /* base finalize */
158                         (GClassInitFunc) modest_folder_window_class_init,
159                         NULL,           /* class finalize */
160                         NULL,           /* class data */
161                         sizeof(ModestFolderWindow),
162                         1,              /* n_preallocs */
163                         (GInstanceInitFunc) modest_folder_window_init,
164                         NULL
165                 };
166                 my_type = g_type_register_static (
167 #ifdef MODEST_TOOLKIT_HILDON2
168                                                   MODEST_TYPE_HILDON2_WINDOW,
169 #else
170                                                   MODEST_TYPE_SHELL_WINDOW,
171 #endif
172                                                   "ModestFolderWindow",
173                                                   &my_info, 0);
174         }
175         return my_type;
176 }
177
178 static void
179 modest_folder_window_class_init (ModestFolderWindowClass *klass)
180 {
181         GObjectClass *gobject_class;
182         gobject_class = (GObjectClass*) klass;
183         ModestWindowClass *modest_window_class = (ModestWindowClass *) klass;
184
185         parent_class            = g_type_class_peek_parent (klass);
186         gobject_class->finalize = modest_folder_window_finalize;
187         gobject_class->dispose = modest_folder_window_dispose;
188
189         g_type_class_add_private (gobject_class, sizeof(ModestFolderWindowPrivate));
190         
191         modest_window_class->disconnect_signals_func = modest_folder_window_disconnect_signals;
192         modest_window_class->pack_toolbar_func = modest_folder_window_pack_toolbar;
193 }
194
195 static void
196 modest_folder_window_init (ModestFolderWindow *obj)
197 {
198         ModestFolderWindowPrivate *priv;
199
200         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(obj);
201
202         priv->sighandlers = NULL;
203         
204         priv->folder_view = NULL;
205
206         priv->top_vbox = NULL;
207
208         priv->progress_hint = FALSE;
209         priv->current_store_account = NULL;
210         priv->queue_change_handler = 0;
211 }
212
213 static void
214 modest_folder_window_finalize (GObject *obj)
215 {
216         ModestFolderWindowPrivate *priv;
217
218         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(obj);
219
220         if (priv->current_store_account) {
221                 g_free (priv->current_store_account);
222                 priv->current_store_account = NULL;
223         }
224
225         /* Sanity check: shouldn't be needed, the window mgr should
226            call this function before */
227         modest_folder_window_disconnect_signals (MODEST_WINDOW (obj));  
228
229         G_OBJECT_CLASS(parent_class)->finalize (obj);
230 }
231
232 static void
233 modest_folder_window_dispose (GObject *obj)
234 {
235         ModestFolderWindowPrivate *priv;
236
237         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(obj);
238         if (priv->folder_view) {
239                 TnyList *list;
240
241                 list = modest_folder_view_get_model_tny_list (MODEST_FOLDER_VIEW (priv->folder_view));
242
243                 if (list) {
244                         TnyIterator *iter;
245
246                         iter = tny_list_create_iterator (list);
247                         while (!tny_iterator_is_done (iter)) {
248                                 GObject *item = tny_iterator_get_current (iter);
249
250                                 if (TNY_IS_ACCOUNT (item)) {
251                                         if (TNY_IS_FOLDER_STORE (item) && 
252                                             modest_tny_folder_store_is_remote (TNY_FOLDER_STORE (item))) {
253                                                 tny_account_cancel (TNY_ACCOUNT (item));
254                                         }
255                                 }
256                                 g_object_unref (item);
257                                 tny_iterator_next (iter);
258                         }
259                         g_object_unref (iter);
260                 }
261
262                 if (list && TNY_IS_GTK_FOLDER_LIST_STORE (list)) {
263                         g_object_run_dispose (G_OBJECT (list));
264                 }
265
266                 g_object_unref (list);
267                 priv->folder_view = NULL;
268         }       
269
270         G_OBJECT_CLASS(parent_class)->dispose (obj);
271 }
272
273 static void
274 modest_folder_window_disconnect_signals (ModestWindow *self)
275 {
276         ModestFolderWindowPrivate *priv;
277         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(self);
278
279         if (g_signal_handler_is_connected (G_OBJECT (modest_runtime_get_mail_operation_queue ()), 
280                                            priv->queue_change_handler))
281                 g_signal_handler_disconnect (G_OBJECT (modest_runtime_get_mail_operation_queue ()), 
282                                              priv->queue_change_handler);
283
284         modest_signal_mgr_disconnect_all_and_destroy (priv->sighandlers);
285         priv->sighandlers = NULL;
286 }
287
288 static void
289 connect_signals (ModestFolderWindow *self)
290 {
291         ModestFolderWindowPrivate *priv;
292
293         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(self);
294
295         /* folder view */
296         priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
297                                                        G_OBJECT (priv->folder_view), "folder-activated",
298                                                        G_CALLBACK (on_folder_activated), self);
299
300         priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
301                                                        G_OBJECT (modest_runtime_get_window_mgr ()),
302                                                        "progress-list-changed",
303                                                        G_CALLBACK (on_progress_list_changed), self);
304
305         priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
306                                                        G_OBJECT (priv->folder_view),
307                                                        "visible-account-changed",
308                                                        G_CALLBACK (on_visible_account_changed), self);
309
310         priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
311                                                        G_OBJECT (priv->folder_view),
312                                                        "activity-changed",
313                                                        G_CALLBACK (on_activity_changed), self);
314
315 #ifdef MODEST_TOOLKIT_HILDON2
316         priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
317                                                        G_OBJECT (priv->new_message_button),
318                                                        "clicked",
319                                                        G_CALLBACK (modest_ui_actions_on_new_msg), self);
320 #endif
321
322         priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
323                                                        G_OBJECT (modest_runtime_get_account_store()),
324                                                        "account-changed",
325                                                        G_CALLBACK (on_account_changed), self);
326
327
328 #ifdef MODEST_TOOLKIT_HILDON2
329         g_signal_connect(G_OBJECT(self), "key-press-event",
330                         G_CALLBACK(on_key_press), self);
331 #endif
332 }
333
334 ModestWindow *
335 modest_folder_window_new (TnyFolderStoreQuery *query)
336 {
337         ModestFolderWindow *self = NULL;        
338         ModestFolderWindowPrivate *priv = NULL;
339         GdkPixbuf *window_icon;
340         GtkWidget *scrollable;
341         GtkWidget *top_alignment;
342         
343         self  = MODEST_FOLDER_WINDOW(g_object_new(MODEST_TYPE_FOLDER_WINDOW, NULL));
344         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(self);
345
346         scrollable = modest_toolkit_factory_create_scrollable (modest_runtime_get_toolkit_factory ());
347
348         priv->queue_change_handler =
349                 g_signal_connect (G_OBJECT (modest_runtime_get_mail_operation_queue ()),
350                                   "queue-changed",
351                                   G_CALLBACK (on_queue_changed),
352                                   self);
353
354         priv->folder_view  = modest_platform_create_folder_view (query);
355         modest_folder_view_set_cell_style (MODEST_FOLDER_VIEW (priv->folder_view),
356                                            MODEST_FOLDER_VIEW_CELL_STYLE_COMPACT);
357         modest_folder_view_set_filter (MODEST_FOLDER_VIEW (priv->folder_view), 
358                                        MODEST_FOLDER_VIEW_FILTER_HIDE_ACCOUNTS);
359
360 #ifdef MODEST_TOOLKIT_HILDON2
361         GtkWidget *action_area_box;
362         GdkPixbuf *new_message_pixbuf;
363         g_signal_connect (G_OBJECT (self), "edit-mode-changed",
364                           G_CALLBACK (edit_mode_changed), (gpointer) self);
365
366         action_area_box = hildon_tree_view_get_action_area_box (GTK_TREE_VIEW (priv->folder_view));
367         priv->new_message_button = hildon_button_new (MODEST_EDITABLE_SIZE, HILDON_BUTTON_ARRANGEMENT_HORIZONTAL);
368
369         hildon_button_set_title (HILDON_BUTTON (priv->new_message_button), _("mcen_ti_new_message"));
370         new_message_pixbuf = modest_platform_get_icon ("general_add", MODEST_ICON_SIZE_BIG);
371         hildon_button_set_image (HILDON_BUTTON (priv->new_message_button), gtk_image_new_from_pixbuf (new_message_pixbuf));
372         g_object_unref (new_message_pixbuf);
373
374         gtk_box_pack_start (GTK_BOX (action_area_box), priv->new_message_button, TRUE, TRUE, 0);
375         gtk_widget_show_all (priv->new_message_button);
376         hildon_tree_view_set_action_area_visible (GTK_TREE_VIEW (priv->folder_view), TRUE);
377 #endif
378         
379         setup_menu (self);
380
381         /* Set account store */
382         tny_account_store_view_set_account_store (TNY_ACCOUNT_STORE_VIEW (priv->folder_view),
383                                                   TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()));
384
385         priv->top_vbox = gtk_vbox_new (0, FALSE);
386         top_alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
387         gtk_alignment_set_padding (GTK_ALIGNMENT (top_alignment),
388                                    MODEST_MARGIN_HALF, 0,
389                                    MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE);
390
391         gtk_container_add (GTK_CONTAINER (scrollable), priv->folder_view);
392         gtk_container_add (GTK_CONTAINER (top_alignment), scrollable);
393         gtk_box_pack_end (GTK_BOX (priv->top_vbox), top_alignment, TRUE, TRUE, 0);
394         gtk_container_add (GTK_CONTAINER (self), priv->top_vbox);
395
396         gtk_widget_show (priv->folder_view);
397         gtk_widget_show (scrollable);
398         gtk_widget_show (priv->top_vbox);
399         gtk_widget_show (top_alignment);
400
401         connect_signals (MODEST_FOLDER_WINDOW (self));
402
403         /* Get device name */
404 #ifdef MODEST_TOOLKIT_HILDON2
405         HildonProgram *app;
406         modest_maemo_utils_get_device_name ();
407
408         app = hildon_program_get_instance ();
409         hildon_program_add_window (app, HILDON_WINDOW (self));
410 #endif
411         
412         /* Set window icon */
413         window_icon = modest_platform_get_icon (MODEST_APP_ICON, MODEST_ICON_SIZE_BIG);
414         if (window_icon) {
415                 GtkWindow *toplevel = gtk_widget_get_toplevel ((GtkWidget *) self);
416                 gtk_window_set_icon (toplevel, window_icon);
417                 g_object_unref (window_icon);
418         }
419
420         /* Dont't restore settings here, 
421          * because it requires a gtk_widget_show(), 
422          * and we don't want to do that until later,
423          * so that the UI is not visible for non-menu D-Bus activation.
424          */
425
426         /* Register edit modes */
427 #ifdef MODEST_TOOLKIT_HILDON2
428         modest_hildon2_window_register_edit_mode (MODEST_HILDON2_WINDOW (self), 
429                                                   EDIT_MODE_COMMAND_DELETE,
430                                                   _("mcen_ti_edit_folder_delete"), 
431                                                   _HL_DELETE,
432                                                   GTK_TREE_VIEW (priv->folder_view),
433                                                   GTK_SELECTION_SINGLE,
434                                                   EDIT_MODE_CALLBACK (modest_ui_actions_on_edit_mode_delete_folder));
435         modest_hildon2_window_register_edit_mode (MODEST_HILDON2_WINDOW (self), 
436                                                   EDIT_MODE_COMMAND_MOVE,
437                                                   _("mcen_ti_edit_move_folder"), 
438                                                   _HL_MOVE,
439                                                   GTK_TREE_VIEW (priv->folder_view),
440                                                   GTK_SELECTION_SINGLE,
441                                                   EDIT_MODE_CALLBACK (modest_ui_actions_on_edit_mode_move_to)); 
442         modest_hildon2_window_register_edit_mode (MODEST_HILDON2_WINDOW (self), 
443                                                   EDIT_MODE_COMMAND_RENAME,
444                                                   _("mcen_ti_edit_rename_folder"), 
445                                                   _HL_RENAME,
446                                                   GTK_TREE_VIEW (priv->folder_view),
447                                                   GTK_SELECTION_SINGLE,
448                                                   EDIT_MODE_CALLBACK (modest_ui_actions_on_edit_mode_rename_folder));
449 #endif
450         
451         g_signal_connect (G_OBJECT (self), "map-event",
452                           G_CALLBACK (on_map_event),
453                           G_OBJECT (self));
454         update_progress_hint (self);
455
456 #ifdef MODEST_TOOLKIT_HILDON2
457         GtkAccelGroup *accel_group;
458         guint accel_key;
459         GdkModifierType accel_mods;
460
461         accel_group = gtk_accel_group_new ();
462         gtk_accelerator_parse ("<Control>n", &accel_key, &accel_mods);
463         gtk_widget_add_accelerator (priv->new_message_button, "clicked", accel_group,
464                                     accel_key, accel_mods, 0);
465         gtk_window_add_accel_group (GTK_WINDOW (self), accel_group);
466 #endif
467
468         return MODEST_WINDOW(self);
469 }
470
471 ModestFolderView *
472 modest_folder_window_get_folder_view (ModestFolderWindow *self)
473 {
474         ModestFolderWindowPrivate *priv = NULL;
475
476         g_return_val_if_fail (MODEST_IS_FOLDER_WINDOW(self), FALSE);
477
478         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
479         
480         return MODEST_FOLDER_VIEW (priv->folder_view);
481 }
482
483 void
484 modest_folder_window_set_account (ModestFolderWindow *self,
485                                   const gchar *account_name)
486 {
487         ModestFolderWindowPrivate *priv = NULL;
488         ModestAccountMgr *mgr;
489         ModestAccountSettings *settings = NULL;
490         ModestServerAccountSettings *store_settings = NULL;
491
492         g_return_if_fail (MODEST_IS_FOLDER_WINDOW(self));
493
494         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
495
496         /* Get account data */
497         mgr = modest_runtime_get_account_mgr ();
498
499         settings = modest_account_mgr_load_account_settings (mgr, account_name);
500         if (!settings)
501                 goto free_refs;
502
503         store_settings = modest_account_settings_get_store_settings (settings);
504         if (!store_settings)
505                 goto free_refs;
506
507         if (priv->current_store_account != NULL)
508                 g_free (priv->current_store_account);
509         priv->current_store_account = g_strdup (modest_server_account_settings_get_account_name (store_settings));
510
511         modest_folder_view_set_account_id_of_visible_server_account
512                 (MODEST_FOLDER_VIEW (priv->folder_view),
513                  priv->current_store_account);
514
515         modest_window_set_active_account (MODEST_WINDOW (self), account_name);
516         update_progress_hint (self);
517
518 free_refs:
519         if (store_settings)
520                 g_object_unref (store_settings);
521         if (settings)
522                 g_object_unref (settings);
523
524 }
525
526 void
527 modest_folder_window_set_mailbox (ModestFolderWindow *self,
528                                   const gchar *mailbox)
529 {
530         ModestFolderWindowPrivate *priv = NULL;
531
532         g_return_if_fail (MODEST_IS_FOLDER_WINDOW(self));
533         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
534
535         modest_folder_view_set_mailbox (MODEST_FOLDER_VIEW (priv->folder_view), mailbox);
536         modest_window_set_active_mailbox (MODEST_WINDOW (self), mailbox);
537 }
538
539 static void
540 edit_account (GtkButton *button,
541               ModestFolderWindow *self)
542 {
543         const gchar *account_name;
544
545         account_name = modest_window_get_active_account ((ModestWindow *) self);
546         if (modest_ui_actions_check_for_active_account ((ModestWindow *) self, account_name)) {
547                 /* Show the account settings dialog */
548                 ModestAccountProtocol *proto;
549                 ModestProtocolType proto_type;
550
551                 /* Get proto */
552                 proto_type = modest_account_mgr_get_store_protocol (modest_runtime_get_account_mgr (),
553                                                                     account_name);
554                 proto = (ModestAccountProtocol *)
555                         modest_protocol_registry_get_protocol_by_type (modest_runtime_get_protocol_registry (),
556                                                                        proto_type);
557
558                 /* Create and show the dialog */
559                 if (proto && MODEST_IS_ACCOUNT_PROTOCOL (proto)) {
560                         ModestAccountSettingsDialog *dialog =
561                                 modest_account_protocol_get_account_settings_dialog (proto, account_name);
562
563                         if (dialog) {
564                                 modest_window_mgr_set_modal (modest_runtime_get_window_mgr (),
565                                                              (GtkWindow *) dialog,
566                                                              (GtkWindow *) self);
567                                 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), FALSE);
568                                 gtk_widget_show (GTK_WIDGET (dialog));
569                         }
570                 }
571         }
572 }
573
574 static void
575 setup_menu (ModestFolderWindow *self)
576 {
577         g_return_if_fail (MODEST_IS_FOLDER_WINDOW(self));
578
579         /* folders actions*/
580         modest_window_add_to_menu (MODEST_WINDOW (self), _("mcen_me_new_folder"), NULL,
581                                    MODEST_WINDOW_MENU_CALLBACK (modest_ui_actions_on_new_folder),
582                                    NULL);
583         modest_window_add_to_menu (MODEST_WINDOW (self), _("mcen_me_inbox_sendandreceive"), NULL,
584                                    MODEST_WINDOW_MENU_CALLBACK (modest_ui_actions_on_send_receive),
585                                    MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_send_receive));
586 #ifdef MODEST_TOOLKIT_HILDON2
587         modest_window_add_to_menu (MODEST_WINDOW (self), _("mcen_me_rename_folder"), NULL,
588                                    MODEST_WINDOW_MENU_CALLBACK (set_rename_edit_mode),
589                                    MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_rename_folder));
590         modest_window_add_to_menu (MODEST_WINDOW (self), _("mcen_me_move_folder"), NULL,
591                                    MODEST_WINDOW_MENU_CALLBACK (set_moveto_edit_mode),
592                                    MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_folder_window_move_to));
593         modest_window_add_to_menu (MODEST_WINDOW (self), _("mcen_me_delete_folder"), NULL,
594                                    MODEST_WINDOW_MENU_CALLBACK (set_delete_edit_mode),
595                                    MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_folder_window_delete));
596 #endif
597
598         modest_window_add_to_menu (MODEST_WINDOW (self), _("mcen_me_outbox_cancelsend"), NULL,
599                                    MODEST_WINDOW_MENU_CALLBACK (modest_ui_actions_cancel_send),
600                                    MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_cancel_sending_all));
601
602         modest_window_add_to_menu (MODEST_WINDOW (self),
603                                    dngettext(GETTEXT_PACKAGE,
604                                              "mcen_me_edit_account",
605                                              "mcen_me_edit_accounts",
606                                              1),
607                                    NULL, MODEST_WINDOW_MENU_CALLBACK (edit_account),
608                                    MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_edit_accounts));
609 }
610
611 static void
612 on_folder_activated (ModestFolderView *folder_view,
613                      TnyFolder *folder,
614                      gpointer userdata)
615 {
616         ModestFolderWindowPrivate *priv = NULL;
617         ModestWindow *headerwin;
618         ModestFolderWindow *self = (ModestFolderWindow *) userdata;
619
620         g_return_if_fail (MODEST_IS_FOLDER_WINDOW(self));
621
622         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
623
624         if (!folder || !TNY_IS_FOLDER (folder))
625                 return;
626
627         /* We cannot open noselect folders (fake ones) */
628         if (tny_folder_get_caps (folder) & TNY_FOLDER_CAPS_NOSELECT)
629                 return;
630
631         headerwin = modest_header_window_new (folder, 
632                                               modest_window_get_active_account (MODEST_WINDOW (self)),
633                                               modest_window_get_active_mailbox (MODEST_WINDOW (self)));
634
635         if (modest_window_mgr_register_window (modest_runtime_get_window_mgr (),
636                                                MODEST_WINDOW (headerwin),
637                                                MODEST_WINDOW (self))) {
638                 gtk_widget_show (GTK_WIDGET (headerwin));
639         } else {
640                 gtk_widget_destroy (GTK_WIDGET (headerwin));
641                 headerwin = NULL;
642         }
643 }
644
645 #ifdef MODEST_TOOLKIT_HILDON2
646 static void
647 set_delete_edit_mode (GtkButton *button,
648                       ModestFolderWindow *self)
649 {
650         modest_hildon2_window_set_edit_mode (MODEST_HILDON2_WINDOW (self), EDIT_MODE_COMMAND_DELETE);
651 }
652
653 static void
654 set_moveto_edit_mode (GtkButton *button,
655                       ModestFolderWindow *self)
656 {
657         modest_hildon2_window_set_edit_mode (MODEST_HILDON2_WINDOW (self), EDIT_MODE_COMMAND_MOVE);
658 }
659
660 static void
661 set_rename_edit_mode (GtkButton *button,
662                       ModestFolderWindow *self)
663 {
664         modest_hildon2_window_set_edit_mode (MODEST_HILDON2_WINDOW (self), EDIT_MODE_COMMAND_RENAME);
665 }
666 #endif
667
668 static void
669 modest_folder_window_pack_toolbar (ModestWindow *self,
670                                    GtkPackType pack_type,
671                                    GtkWidget *toolbar)
672 {
673         ModestFolderWindowPrivate *priv;
674
675         g_return_if_fail (MODEST_IS_FOLDER_WINDOW (self));
676         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
677
678         if (pack_type == GTK_PACK_START) {
679                 gtk_box_pack_start (GTK_BOX (priv->top_vbox), toolbar, FALSE, FALSE, 0);
680         } else {
681                 gtk_box_pack_end (GTK_BOX (priv->top_vbox), toolbar, FALSE, FALSE, 0);
682         }
683 }
684
685 #ifdef MODEST_TOOLKIT_HILDON2
686 static void 
687 edit_mode_changed (ModestFolderWindow *folder_window,
688                    gint edit_mode_id,
689                    gboolean enabled,
690                    ModestFolderWindow *self)
691 {
692         ModestFolderWindowPrivate *priv;
693         ModestFolderViewFilter filter = MODEST_FOLDER_VIEW_FILTER_NONE;
694
695         g_return_if_fail (MODEST_IS_FOLDER_WINDOW (self));
696         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
697
698         switch (edit_mode_id) {
699         case EDIT_MODE_COMMAND_MOVE:
700                 filter = MODEST_FOLDER_VIEW_FILTER_MOVEABLE;
701                 break;
702         case EDIT_MODE_COMMAND_DELETE:
703                 filter = MODEST_FOLDER_VIEW_FILTER_DELETABLE;
704                 break;
705         case EDIT_MODE_COMMAND_RENAME:
706                 filter = MODEST_FOLDER_VIEW_FILTER_RENAMEABLE;
707                 break;
708         case MODEST_HILDON2_WINDOW_EDIT_MODE_NONE:
709                 filter = MODEST_FOLDER_VIEW_FILTER_NONE;
710                 break;
711         }
712
713         hildon_tree_view_set_action_area_visible (GTK_TREE_VIEW (priv->folder_view), !enabled);
714         if (enabled) {
715                 modest_folder_view_set_filter (MODEST_FOLDER_VIEW (priv->folder_view), 
716                                                filter);
717         } else {
718                 GtkTreeSelection *sel;
719
720                 /* Unselect all. This will prevent us from keeping a
721                    reference to a TnyObject that we don't want to
722                    have */
723                 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->folder_view));
724                 gtk_tree_selection_unselect_all (sel);
725
726                 modest_folder_view_unset_filter (MODEST_FOLDER_VIEW (priv->folder_view), 
727                                                  filter);
728         }
729 }
730 #endif
731
732 static gboolean 
733 on_map_event (GtkWidget *widget,
734               GdkEvent *event,
735               gpointer userdata)
736 {
737         ModestFolderWindow *self = (ModestFolderWindow *) userdata;
738         ModestFolderWindowPrivate *priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
739
740         if (priv->progress_hint) {
741                 modest_window_show_progress (MODEST_WINDOW (self), TRUE);
742         }
743
744         return FALSE;
745 }
746
747 static gboolean
748 has_active_operations (ModestFolderWindow *self)
749 {
750         GSList *operations = NULL, *node;
751         ModestMailOperationQueue *queue;
752         gboolean has_active = FALSE;
753
754         queue = modest_runtime_get_mail_operation_queue ();
755         operations = modest_mail_operation_queue_get_by_source (queue, G_OBJECT (self));
756
757         for (node = operations; node != NULL; node = g_slist_next (node)) {
758                 if (!modest_mail_operation_is_finished (MODEST_MAIL_OPERATION (node->data))) {
759                         has_active = TRUE;
760                         break;
761                 }
762         }
763
764         if (operations) {
765                 g_slist_foreach (operations, (GFunc) g_object_unref, NULL);
766                 g_slist_free (operations);
767         }
768
769         return has_active;
770 }
771
772 static void
773 update_progress_hint (ModestFolderWindow *self)
774 {
775         ModestFolderWindowPrivate *priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
776
777         if (has_active_operations (self)) {
778                 priv->progress_hint = TRUE;
779         } else {
780                 priv->progress_hint = FALSE;
781         }
782
783         if (!priv->progress_hint && priv->current_store_account) {
784                 priv->progress_hint = modest_window_mgr_has_progress_operation_on_account (modest_runtime_get_window_mgr (),
785                                                                                            priv->current_store_account);
786         }
787
788         if (!priv->progress_hint) {
789                 priv->progress_hint = modest_folder_view_get_activity (MODEST_FOLDER_VIEW (priv->folder_view));
790         }
791
792         modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (self));
793
794         if (GTK_WIDGET_VISIBLE (self)) {
795                 modest_window_show_progress (MODEST_WINDOW (self), priv->progress_hint ? 1:0);
796         }
797 }
798
799 static void
800 on_progress_list_changed (ModestWindowMgr *mgr,
801                           ModestFolderWindow *self)
802 {
803         update_progress_hint (self);
804 }
805
806 gboolean
807 modest_folder_window_transfer_mode_enabled (ModestFolderWindow *self)
808 {
809         ModestFolderWindowPrivate *priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
810
811         return priv->progress_hint;
812 }
813
814 static void 
815 on_mail_operation_started (ModestMailOperation *mail_op,
816                            gpointer user_data)
817 {
818         ModestFolderWindow *self;
819         ModestMailOperationTypeOperation op_type;
820         GObject *source = NULL;
821
822         self = MODEST_FOLDER_WINDOW (user_data);
823         op_type = modest_mail_operation_get_type_operation (mail_op);
824         source = modest_mail_operation_get_source(mail_op);
825         if (G_OBJECT (self) == source) {
826                 update_progress_hint (self);
827         }
828         g_object_unref (source);
829 }
830
831 static void 
832 on_mail_operation_finished (ModestMailOperation *mail_op,
833                             gpointer user_data)
834 {
835         ModestFolderWindow *self;
836
837         self = MODEST_FOLDER_WINDOW (user_data);
838
839         /* Don't disable the progress hint if there are more pending
840            operations from this window */
841         update_progress_hint (self);
842
843         modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (self));
844 }
845
846 static void
847 on_queue_changed (ModestMailOperationQueue *queue,
848                   ModestMailOperation *mail_op,
849                   ModestMailOperationQueueNotification type,
850                   ModestFolderWindow *self)
851 {
852         ModestFolderWindowPrivate *priv;
853
854         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
855
856         /* If this operations was created by another window, do nothing */
857         if (!modest_mail_operation_is_mine (mail_op, G_OBJECT(self))) 
858                 return;
859
860         if (type == MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED) {
861                 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
862                                                                G_OBJECT (mail_op),
863                                                                "operation-started",
864                                                                G_CALLBACK (on_mail_operation_started),
865                                                                self);
866                 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
867                                                                G_OBJECT (mail_op),
868                                                                "operation-finished",
869                                                                G_CALLBACK (on_mail_operation_finished),
870                                                                self);
871         } else if (type == MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED) {
872                 priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
873                                                                   G_OBJECT (mail_op),
874                                                                   "operation-started");
875                 priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
876                                                                   G_OBJECT (mail_op),
877                                                                   "operation-finished");
878         }
879 }
880
881 static void
882 on_activity_changed (ModestFolderView *view,
883                      gboolean activity,
884                      ModestFolderWindow *folder_window)
885 {
886         g_return_if_fail (MODEST_IS_FOLDER_WINDOW (folder_window));
887
888         update_progress_hint (folder_window);
889 }
890
891
892 static void
893 update_window_title (ModestFolderWindow *self,
894                      TnyAccount *account)
895 {
896         if (account) {
897                 const gchar *name;
898                 const gchar *mailbox;
899                 gchar *title = NULL;
900                 ModestFolderWindowPrivate *priv;
901
902                 priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
903
904                 mailbox = modest_folder_view_get_mailbox (MODEST_FOLDER_VIEW (priv->folder_view));
905                 if (mailbox) {
906                         title = g_strdup (mailbox);
907                 } else {
908                         name = modest_tny_account_get_parent_modest_account_name_for_server_account (account);
909                         title = modest_account_mgr_get_display_name (modest_runtime_get_account_mgr(),
910                                                                      name);
911                 }
912                 if (title) {
913                         modest_window_set_title (MODEST_WINDOW (self), title);
914                         g_free (title);
915                 } else {
916                         modest_window_set_title (MODEST_WINDOW (self), _("mcen_ap_name"));
917                 }
918         } else {
919                 modest_window_set_title (MODEST_WINDOW (self), _("mcen_ap_name"));
920         }
921 }
922
923 static void
924 on_visible_account_changed (ModestFolderView *folder_view,
925                             const gchar *account_id,
926                             gpointer user_data)
927 {
928         TnyAccount *account;
929
930         if (!account_id)
931                 return;
932
933         account = modest_tny_account_store_get_tny_account_by (modest_runtime_get_account_store(),
934                                                                MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
935                                                                account_id);
936
937         /* Update window title */
938         if (account) {
939                 update_window_title (MODEST_FOLDER_WINDOW (user_data), account);
940                 g_object_unref (account);
941         }
942 }
943
944 static void
945 on_account_changed (TnyAccountStore *account_store,
946                     TnyAccount *account,
947                     gpointer user_data)
948 {
949         const gchar *acc_id, *visible;
950         ModestFolderWindowPrivate *priv;
951
952         if (!TNY_IS_STORE_ACCOUNT (account))
953                 return;
954
955         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (user_data);
956
957         acc_id = tny_account_get_id (account);
958         visible = modest_folder_view_get_account_id_of_visible_server_account (MODEST_FOLDER_VIEW (priv->folder_view));
959
960         /* Update title if the visible account is the one that have just changed */
961         if (acc_id && visible && !g_utf8_collate (acc_id, visible))
962                 update_window_title (MODEST_FOLDER_WINDOW (user_data), account);
963 }
964
965 #ifdef MODEST_TOOLKIT_HILDON2
966 static gboolean
967 on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
968 {
969         ModestFolderWindowPrivate *priv;
970         ModestScrollable *scrollable;
971
972         if (event->type == GDK_KEY_RELEASE)
973                 return FALSE;
974
975         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (user_data);
976
977         scrollable = MODEST_SCROLLABLE (gtk_widget_get_parent (priv->folder_view));
978
979         switch (event->keyval) {
980
981         case GDK_Up:
982                 modest_scrollable_scroll (scrollable, 0, -1);
983                 break;
984
985         case GDK_Down:
986                 modest_scrollable_scroll (scrollable, 0, 1);
987                 break;
988         }
989
990         return FALSE;
991 }
992 #endif