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