5ed64c6ac17b99a6531dced7ae97ce90c8904458
[modest] / src / hildon2 / modest-header-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-header-window.h>
31 #include <modest-osso-state-saving.h>
32 #include <libosso.h>
33 #include <hildon/hildon-pannable-area.h>
34 #include <hildon/hildon-helper.h>
35 #include <modest-window-mgr.h>
36 #include <modest-window-priv.h>
37 #include <modest-signal-mgr.h>
38 #include <modest-runtime.h>
39 #include <modest-platform.h>
40 #include <modest-maemo-utils.h>
41 #include <modest-icon-names.h>
42 #include <modest-ui-constants.h>
43 #include <modest-account-mgr.h>
44 #include <modest-account-mgr-helpers.h>
45 #include <modest-defs.h>
46 #include <modest-widget-memory.h>
47 #include <modest-ui-actions.h>
48 #include <modest-platform.h>
49 #include <modest-text-utils.h>
50 #include <hildon/hildon-button.h>
51 #include <hildon/hildon-program.h>
52 #include <hildon/hildon-banner.h>
53 #include <modest-ui-dimming-rules.h>
54 #include <modest-tny-folder.h>
55 #include <tny-simple-list.h>
56
57 typedef enum {
58         CONTENTS_STATE_NONE = 0,
59         CONTENTS_STATE_EMPTY = 1,
60         CONTENTS_STATE_HEADERS = 2
61 } ContentsState;
62
63 typedef enum {
64         EDIT_MODE_COMMAND_MOVE = 1,
65         EDIT_MODE_COMMAND_DELETE = 2,
66 } EditModeCommand;
67
68 typedef struct _ModestHeaderWindowPrivate ModestHeaderWindowPrivate;
69 struct _ModestHeaderWindowPrivate {
70
71         GtkWidget *header_view;
72         GtkWidget *empty_view;
73         GtkWidget *contents_view;
74         GtkWidget *top_vbox;
75         GtkWidget *new_message_button;
76
77         /* state bar */
78         ContentsState contents_state;
79
80         TnyFolder *folder;
81
82         /* autoscroll */
83         gboolean autoscroll;
84
85         /* banners */
86         GtkWidget *updating_banner;
87         guint updating_banner_timeout;
88
89         /* signals */
90         GSList *sighandlers;
91         gulong queue_change_handler;
92
93         /* progress hint */
94         gboolean progress_hint;
95         gchar *current_store_account;
96
97         /* sort button */
98         GtkWidget *sort_button;
99
100         /* CSM menu */
101         GtkWidget *csm_menu;
102         gdouble x_coord;
103         gdouble y_coord;
104 };
105 #define MODEST_HEADER_WINDOW_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE((o), \
106                                                                           MODEST_TYPE_HEADER_WINDOW, \
107                                                                           ModestHeaderWindowPrivate))
108
109 /* 'private'/'protected' functions */
110 static void modest_header_window_class_init  (ModestHeaderWindowClass *klass);
111 static void modest_header_window_init        (ModestHeaderWindow *obj);
112 static void modest_header_window_finalize    (GObject *obj);
113
114 static void connect_signals (ModestHeaderWindow *self);
115 static void modest_header_window_disconnect_signals (ModestWindow *self);
116
117 static void setup_menu (ModestHeaderWindow *self);
118 static GtkWidget *create_empty_view (ModestWindow *self);
119 static GtkWidget *create_header_view (ModestWindow *progress_window,
120                                       TnyFolder *folder);
121
122 static void update_view (ModestHeaderWindow *self,
123                          TnyFolderChange *change);
124 static void set_contents_state (ModestHeaderWindow *window,
125                                 ContentsState state);
126
127 static void on_msg_count_changed (ModestHeaderView *header_view,
128                                   TnyFolder *folder,
129                                   TnyFolderChange *change,
130                                   ModestHeaderWindow *header_window);
131 static void on_header_activated (ModestHeaderView *header_view,
132                                  TnyHeader *header,
133                                  GtkTreePath *path,
134                                  ModestHeaderWindow *header_window);
135 static void on_updating_msg_list (ModestHeaderView *header_view,
136                                   gboolean starting,
137                                   gpointer user_data);
138 static void set_delete_edit_mode (GtkButton *button,
139                                   ModestHeaderWindow *self);
140 static void set_moveto_edit_mode (GtkButton *button,
141                                   ModestHeaderWindow *self);
142 static gboolean on_expose_event(GtkTreeView *header_view,
143                                 GdkEventExpose *event,
144                                 gpointer user_data);
145 static gboolean on_map_event (GtkWidget *widget,
146                               GdkEvent *event,
147                               gpointer userdata);
148 static void on_vertical_movement (HildonPannableArea *area,
149                                   HildonMovementDirection direction,
150                                   gdouble x, gdouble y, gpointer user_data);
151 static void on_queue_changed    (ModestMailOperationQueue *queue,
152                                  ModestMailOperation *mail_op,
153                                  ModestMailOperationQueueNotification type,
154                                  ModestHeaderWindow *self);
155 static void modest_header_window_pack_toolbar (ModestHildon2Window *self,
156                                                GtkPackType pack_type,
157                                                GtkWidget *toolbar);
158 static void edit_mode_changed (ModestHeaderWindow *header_window,
159                                gint edit_mode_id,
160                                gboolean enabled,
161                                ModestHeaderWindow *self);
162 static void on_progress_list_changed (ModestWindowMgr *mgr,
163                                       ModestHeaderWindow *self);
164 static void update_progress_hint (ModestHeaderWindow *self);
165 static void on_sort_column_changed (GtkTreeSortable *treesortable,
166                                     gpointer         user_data);
167 static void update_sort_button (ModestHeaderWindow *self);
168 static void on_horizontal_movement (HildonPannableArea *hildonpannable,
169                                     gint                direction,
170                                     gdouble             initial_x,
171                                     gdouble             initial_y,
172                                     gpointer            user_data);
173
174 /* globals */
175 static GtkWindowClass *parent_class = NULL;
176
177 #define EMPTYVIEW_XALIGN 0.5
178 #define EMPTYVIEW_YALIGN 0.5
179 #define EMPTYVIEW_XSPACE 1.0
180 #define EMPTYVIEW_YSPACE 1.0
181
182
183
184 /************************************************************************/
185
186 GType
187 modest_header_window_get_type (void)
188 {
189         static GType my_type = 0;
190         if (!my_type) {
191                 static const GTypeInfo my_info = {
192                         sizeof(ModestHeaderWindowClass),
193                         NULL,           /* base init */
194                         NULL,           /* base finalize */
195                         (GClassInitFunc) modest_header_window_class_init,
196                         NULL,           /* class finalize */
197                         NULL,           /* class data */
198                         sizeof(ModestHeaderWindow),
199                         1,              /* n_preallocs */
200                         (GInstanceInitFunc) modest_header_window_init,
201                         NULL
202                 };
203                 my_type = g_type_register_static (MODEST_TYPE_HILDON2_WINDOW,
204                                                   "ModestHeaderWindow",
205                                                   &my_info, 0);
206         }
207         return my_type;
208 }
209
210 static void
211 modest_header_window_class_init (ModestHeaderWindowClass *klass)
212 {
213         GObjectClass *gobject_class;
214         gobject_class = (GObjectClass*) klass;
215         ModestWindowClass *modest_window_class = (ModestWindowClass *) klass;
216         ModestHildon2WindowClass *modest_hildon2_window_class = (ModestHildon2WindowClass *) klass;
217
218         parent_class            = g_type_class_peek_parent (klass);
219         gobject_class->finalize = modest_header_window_finalize;
220
221         g_type_class_add_private (gobject_class, sizeof(ModestHeaderWindowPrivate));
222         
223         modest_window_class->disconnect_signals_func = modest_header_window_disconnect_signals;
224         modest_hildon2_window_class->pack_toolbar_func = modest_header_window_pack_toolbar;
225 }
226
227 static void
228 modest_header_window_init (ModestHeaderWindow *obj)
229 {
230         ModestHeaderWindowPrivate *priv;
231
232         priv = MODEST_HEADER_WINDOW_GET_PRIVATE(obj);
233
234         priv->sighandlers = NULL;
235
236         priv->header_view = NULL;
237         priv->empty_view = NULL;
238         priv->top_vbox = NULL;
239         priv->contents_view = NULL;
240         priv->contents_state = CONTENTS_STATE_NONE;
241         priv->folder = NULL;
242         priv->updating_banner = NULL;
243         priv->updating_banner_timeout = 0;
244         priv->autoscroll = TRUE;
245         priv->progress_hint = FALSE;
246         priv->queue_change_handler = 0;
247         priv->current_store_account = NULL;
248         priv->sort_button = NULL;
249         priv->new_message_button = NULL;
250         priv->x_coord = 0;
251         priv->y_coord = 0;
252
253         modest_window_mgr_register_help_id (modest_runtime_get_window_mgr(),
254                                             GTK_WINDOW(obj),
255                                             "applications_email_headerview");
256 }
257
258 static void
259 modest_header_window_finalize (GObject *obj)
260 {
261         ModestHeaderWindowPrivate *priv;
262
263         priv = MODEST_HEADER_WINDOW_GET_PRIVATE(obj);
264
265         tny_folder_sync_async (TNY_FOLDER (priv->folder),
266                                FALSE, NULL, NULL, NULL);
267
268         g_object_unref (priv->folder);
269         g_object_unref (priv->header_view);
270         g_object_unref (priv->empty_view);
271
272         if (priv->current_store_account) {
273                 g_free (priv->current_store_account);
274                 priv->current_store_account = NULL;
275         }
276
277         /* Sanity check: shouldn't be needed, the window mgr should
278            call this function before */
279         modest_header_window_disconnect_signals (MODEST_WINDOW (obj));  
280
281         if (priv->updating_banner_timeout > 0) {
282                 g_source_remove (priv->updating_banner_timeout);
283                 priv->updating_banner_timeout = 0;
284         }
285         if (priv->updating_banner) {
286                 gtk_widget_destroy (priv->updating_banner);
287                 priv->updating_banner = NULL;
288         }
289
290         G_OBJECT_CLASS(parent_class)->finalize (obj);
291 }
292
293 static void
294 modest_header_window_disconnect_signals (ModestWindow *self)
295 {
296         ModestHeaderWindowPrivate *priv;
297         priv = MODEST_HEADER_WINDOW_GET_PRIVATE(self);
298
299         if (g_signal_handler_is_connected (G_OBJECT (modest_runtime_get_mail_operation_queue ()), 
300                                            priv->queue_change_handler))
301                 g_signal_handler_disconnect (G_OBJECT (modest_runtime_get_mail_operation_queue ()), 
302                                              priv->queue_change_handler);
303
304         modest_signal_mgr_disconnect_all_and_destroy (priv->sighandlers);
305         priv->sighandlers = NULL;
306
307 }
308
309 static void
310 connect_signals (ModestHeaderWindow *self)
311 {       
312         ModestHeaderWindowPrivate *priv;
313         GtkTreeSortable *sortable;
314         
315         priv = MODEST_HEADER_WINDOW_GET_PRIVATE(self);
316
317         /* header view */
318
319         priv->sighandlers = 
320                 modest_signal_mgr_connect (priv->sighandlers,G_OBJECT(priv->header_view), 
321                                            "msg_count_changed",
322                                            G_CALLBACK(on_msg_count_changed), self);
323         priv->sighandlers =
324                 modest_signal_mgr_connect (priv->sighandlers, G_OBJECT (priv->header_view),
325                                            "header-activated",
326                                            G_CALLBACK (on_header_activated), self);
327         priv->sighandlers = 
328                 modest_signal_mgr_connect (priv->sighandlers,
329                                            G_OBJECT (priv->header_view), 
330                                            "updating-msg-list",
331                                            G_CALLBACK (on_updating_msg_list), 
332                                            self);
333         priv->sighandlers =
334                 modest_signal_mgr_connect (priv->sighandlers,
335                                            G_OBJECT (priv->header_view),
336                                            "expose-event",
337                                            G_CALLBACK (on_expose_event),
338                                            self);
339
340         sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (GTK_TREE_VIEW (priv->header_view)));
341
342         priv->sighandlers = 
343                 modest_signal_mgr_connect (priv->sighandlers,
344                                            G_OBJECT (sortable),
345                                            "sort-column-changed",
346                                            G_CALLBACK (on_sort_column_changed),
347                                            self);
348
349         priv->sighandlers =
350                 modest_signal_mgr_connect (priv->sighandlers,
351                                            G_OBJECT (self),
352                                            "map-event",
353                                            G_CALLBACK (on_map_event),
354                                            self);
355
356         priv->sighandlers =
357                 modest_signal_mgr_connect (priv->sighandlers,
358                                            G_OBJECT (priv->contents_view), 
359                                            "vertical-movement", 
360                                            G_CALLBACK (on_vertical_movement), 
361                                            self);
362
363         /* Mail Operation Queue */
364         priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
365                                                        G_OBJECT (modest_runtime_get_window_mgr ()),
366                                                        "progress-list-changed",
367                                                        G_CALLBACK (on_progress_list_changed), self);
368         priv->sighandlers =
369                 modest_signal_mgr_connect (priv->sighandlers,
370                                            G_OBJECT (priv->new_message_button),
371                                            "clicked",
372                                            G_CALLBACK (modest_ui_actions_on_new_msg), self);
373
374         /* Delete using horizontal gesture */
375         /* DISABLED because it's unreliabile */
376         if (FALSE) {
377                 priv->sighandlers =
378                         modest_signal_mgr_connect (priv->sighandlers,
379                                                    (GObject *) priv->contents_view,
380                                                    "horizontal-movement",
381                                                    G_CALLBACK (on_horizontal_movement),
382                                                    self);
383         }
384 }
385
386 static void
387 folder_refreshed_cb (ModestMailOperation *mail_op,
388                      TnyFolder *folder,
389                      gpointer user_data)
390 {
391         /* Update the view (folder could be empty) */
392         update_view (MODEST_HEADER_WINDOW (user_data), NULL);
393 }
394
395 static gboolean
396 tap_and_hold_query_cb (GtkWidget *header_view,
397                        GdkEvent *event,
398                        gpointer user_data)
399 {
400         ModestHeaderWindow *self;
401         ModestHeaderWindowPrivate *priv;
402
403         self = (ModestHeaderWindow *) user_data;
404         priv = MODEST_HEADER_WINDOW_GET_PRIVATE (self);
405
406         if (event->type == GDK_BUTTON_PRESS) {
407                 TnyHeader *header;
408
409                 priv->x_coord = ((GdkEventButton*)event)->x;
410                 priv->y_coord = ((GdkEventButton*)event)->y;
411
412                 /* Enable/Disable mark as (un)read */
413                 header = modest_header_view_get_header_at_pos ((ModestHeaderView *) header_view,
414                                                                priv->x_coord, priv->y_coord);
415                 if (header) {
416                         GList *children;
417                         GtkWidget *mark_read_item, *mark_unread_item;
418
419                         /* Show "mark as read" or "mark as unread" */
420                         children = gtk_container_get_children (GTK_CONTAINER (priv->csm_menu));
421                         mark_read_item = (GtkWidget *) g_list_nth_data (children, 1);
422                         mark_unread_item = (GtkWidget *) g_list_nth_data (children, 2);
423
424                         if (tny_header_get_flags (header) & TNY_HEADER_FLAG_SEEN) {
425                                 gtk_widget_show (mark_unread_item);
426                                 gtk_widget_hide (mark_read_item);
427                         } else {
428                                 gtk_widget_show (mark_read_item);
429                                 gtk_widget_hide (mark_unread_item);
430                         }
431                         g_object_unref (header);
432                 }
433         }
434
435         return FALSE;
436 }
437
438 static void
439 delete_header (GtkWindow *parent,
440                TnyHeader *header)
441 {
442         gint response;
443         gchar *subject, *msg;
444
445         subject = tny_header_dup_subject (header);
446         if (!subject)
447                 subject = g_strdup (_("mail_va_no_subject"));
448
449         msg = g_strdup_printf (ngettext("emev_nc_delete_message", "emev_nc_delete_messages", 1),
450                                subject);
451         g_free (subject);
452
453         /* Confirmation dialog */
454         response = modest_platform_run_confirmation_dialog (parent, msg);
455         g_free (msg);
456
457         if (response == GTK_RESPONSE_OK) {
458                 ModestMailOperation *mail_op;
459                 TnyList *header_list;
460
461                 header_list = tny_simple_list_new ();
462                 tny_list_append (header_list, (GObject *) header);
463                 mail_op = modest_mail_operation_new ((GObject *) parent);
464                 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
465                                                  mail_op);
466                 modest_mail_operation_remove_msgs (mail_op, header_list, FALSE);
467                 g_object_unref (mail_op);
468                 g_object_unref (header_list);
469         }
470 }
471
472
473 static void
474 on_delete_csm_activated (GtkMenuItem *item,
475                          gpointer user_data)
476 {
477         TnyHeader *header;
478         ModestHeaderWindow *self;
479         ModestHeaderWindowPrivate *priv;
480
481         self = (ModestHeaderWindow *) user_data;
482         priv = MODEST_HEADER_WINDOW_GET_PRIVATE (self);
483
484         header = modest_header_view_get_header_at_pos ((ModestHeaderView *) priv->header_view,
485                                                        priv->x_coord, priv->y_coord);
486         if (header) {
487                 delete_header ((GtkWindow *) self, header);
488                 g_object_unref (header);
489         }
490 }
491
492 static void
493 on_mark_read_csm_activated (GtkMenuItem *item,
494                             gpointer user_data)
495 {
496 }
497
498 static void
499 on_mark_unread_csm_activated (GtkMenuItem *item,
500                               gpointer user_data)
501 {
502 }
503
504 static GtkWidget *
505 create_header_view (ModestWindow *self, TnyFolder *folder)
506 {
507         GtkWidget *header_view;
508         GtkWidget *delete_item, *mark_read_item, *mark_unread_item;
509         ModestHeaderWindowPrivate *priv;
510
511         header_view  = modest_header_view_new (NULL, MODEST_HEADER_VIEW_STYLE_TWOLINES);
512         modest_header_view_set_folder (MODEST_HEADER_VIEW (header_view), folder,
513                                        TRUE, self, folder_refreshed_cb, self);
514         modest_header_view_set_filter (MODEST_HEADER_VIEW (header_view),
515                                        MODEST_HEADER_VIEW_FILTER_NONE);
516         modest_widget_memory_restore (modest_runtime_get_conf (), G_OBJECT(header_view),
517                                       MODEST_CONF_HEADER_VIEW_KEY);
518
519         /* Create CSM menu */
520         priv = MODEST_HEADER_WINDOW_GET_PRIVATE (self);
521         priv->csm_menu = gtk_menu_new ();
522         delete_item = gtk_menu_item_new_with_label (_HL("wdgt_bd_delete"));
523         mark_read_item = gtk_menu_item_new_with_label (_("mcen_me_inbox_mark_as_read"));
524         mark_unread_item = gtk_menu_item_new_with_label (_("mcen_me_inbox_mark_as_unread"));
525         gtk_menu_shell_append (GTK_MENU_SHELL (priv->csm_menu), delete_item);
526         gtk_menu_shell_append (GTK_MENU_SHELL (priv->csm_menu), mark_read_item);
527         gtk_menu_shell_append (GTK_MENU_SHELL (priv->csm_menu), mark_unread_item);
528         gtk_widget_show_all (priv->csm_menu);
529
530         /* Connect signals */
531         g_signal_connect ((GObject *) header_view, "tap-and-hold-query",
532                           G_CALLBACK (tap_and_hold_query_cb), self);
533         g_signal_connect ((GObject *) delete_item, "activate",
534                           G_CALLBACK (on_delete_csm_activated), self);
535         g_signal_connect ((GObject *) mark_read_item, "activate",
536                           G_CALLBACK (on_mark_read_csm_activated), self);
537         g_signal_connect ((GObject *) mark_unread_item, "activate",
538                           G_CALLBACK (on_mark_unread_csm_activated), self);
539
540         /* Add tap&hold handling */
541         gtk_widget_tap_and_hold_setup (header_view, priv->csm_menu, NULL, 0);
542
543         return header_view;
544 }
545
546 static GtkWidget *
547 create_empty_view (ModestWindow *self)
548 {
549         GtkWidget *viewport = NULL;
550         GtkWidget *label = NULL;
551         GtkWidget *align = NULL;
552         GtkWidget *vbox = NULL;
553         GtkWidget *button = NULL;
554         GdkPixbuf *new_message_pixbuf;
555
556         vbox = gtk_vbox_new (0, FALSE);
557
558         align = gtk_alignment_new(EMPTYVIEW_XALIGN, EMPTYVIEW_YALIGN, EMPTYVIEW_XSPACE, EMPTYVIEW_YSPACE);
559         label = gtk_label_new (_("mcen_ia_nomessages"));
560         hildon_helper_set_logical_font (label, "LargeSystemFont");
561         gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
562         gtk_widget_show (label);
563         gtk_widget_show (align);
564         gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_CENTER);  
565         gtk_container_add (GTK_CONTAINER (align), label);
566         gtk_box_pack_end (GTK_BOX (vbox), align, TRUE, TRUE, 0);
567
568         button = hildon_button_new (MODEST_EDITABLE_SIZE, 
569                                     HILDON_BUTTON_ARRANGEMENT_HORIZONTAL);
570
571         hildon_button_set_title (HILDON_BUTTON (button), _("mcen_ti_new_message"));
572         new_message_pixbuf = modest_platform_get_icon ("general_add", MODEST_ICON_SIZE_BIG);
573         hildon_button_set_image (HILDON_BUTTON (button), 
574                                  gtk_image_new_from_pixbuf (new_message_pixbuf));
575         g_object_unref (new_message_pixbuf);
576         gtk_widget_show_all (button);
577         gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
578
579         gtk_widget_show (vbox);
580
581         g_signal_connect (button,
582                           "clicked",
583                           G_CALLBACK (modest_ui_actions_on_new_msg), self);
584
585         viewport = gtk_viewport_new ((GtkAdjustment *) gtk_adjustment_new (0, 0, 0, 0, 0, 0), 
586                                      (GtkAdjustment *) gtk_adjustment_new (0, 0, 0, 0, 0, 0));
587         gtk_container_add (GTK_CONTAINER (viewport), vbox);
588
589         return viewport;
590 }
591
592 static void
593 on_vertical_movement (HildonPannableArea *area,
594                       HildonMovementDirection direction,
595                       gdouble x, gdouble y, gpointer user_data)
596 {
597         ModestHeaderWindow *self = (ModestHeaderWindow *) user_data;
598         ModestHeaderWindowPrivate *priv = MODEST_HEADER_WINDOW_GET_PRIVATE (self);
599
600         priv->autoscroll = FALSE;
601 }
602
603
604 ModestWindow *
605 modest_header_window_new (TnyFolder *folder, const gchar *account_name, const gchar *mailbox)
606 {
607         ModestHeaderWindow *self = NULL;        
608         ModestHeaderWindowPrivate *priv = NULL;
609         HildonProgram *app;
610         GdkPixbuf *window_icon;
611         ModestAccountMgr *mgr;
612         ModestAccountSettings *settings = NULL;
613         ModestServerAccountSettings *store_settings = NULL;
614         GtkWidget *action_area_box;
615         GdkPixbuf *new_message_pixbuf;
616         GtkWidget *alignment;
617         gchar *account_display_name = NULL;
618         
619         self  = MODEST_HEADER_WINDOW(g_object_new(MODEST_TYPE_HEADER_WINDOW, NULL));
620         priv = MODEST_HEADER_WINDOW_GET_PRIVATE(self);
621
622         priv->folder = g_object_ref (folder);
623
624         priv->contents_view = hildon_pannable_area_new ();
625         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
626         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
627                                    0, 0,
628                                    HILDON_MARGIN_DOUBLE, HILDON_MARGIN_DOUBLE);
629
630         /* We need to do this here to properly listen for mail
631            operations because create_header_view launches a mail
632            operation */
633         priv->queue_change_handler =
634                 g_signal_connect (G_OBJECT (modest_runtime_get_mail_operation_queue ()),
635                                   "queue-changed",
636                                   G_CALLBACK (on_queue_changed),
637                                   self);
638
639         priv->header_view  = create_header_view (MODEST_WINDOW (self), folder);
640         priv->empty_view = create_empty_view (MODEST_WINDOW (self));
641
642         /* Transform the floating reference in a "hard" reference. We
643            need to do this because the widgets could be added/removed
644            to containers many times so we always need to keep a
645            reference. It could happen also that some widget is never
646            added to any container */
647         g_object_ref_sink (priv->header_view);
648         g_object_ref_sink (priv->empty_view);
649
650         g_signal_connect (G_OBJECT (self), "edit-mode-changed",
651                           G_CALLBACK (edit_mode_changed), (gpointer) self);
652
653         action_area_box = hildon_tree_view_get_action_area_box (GTK_TREE_VIEW (priv->header_view));
654         priv->new_message_button = hildon_button_new (MODEST_EDITABLE_SIZE, HILDON_BUTTON_ARRANGEMENT_HORIZONTAL);
655
656         hildon_button_set_title (HILDON_BUTTON (priv->new_message_button), _("mcen_ti_new_message"));
657         new_message_pixbuf = modest_platform_get_icon ("general_add", MODEST_ICON_SIZE_BIG);
658         hildon_button_set_image (HILDON_BUTTON (priv->new_message_button), gtk_image_new_from_pixbuf (new_message_pixbuf));
659         g_object_unref (new_message_pixbuf);
660
661         gtk_box_pack_start (GTK_BOX (action_area_box), priv->new_message_button, TRUE, TRUE, 0);
662         gtk_widget_show_all (priv->new_message_button);
663         hildon_tree_view_set_action_area_visible (GTK_TREE_VIEW (priv->header_view), TRUE);
664         
665         setup_menu (self);
666
667         priv->top_vbox = gtk_vbox_new (FALSE, 0);
668         gtk_container_add (GTK_CONTAINER (alignment), priv->contents_view);
669         gtk_box_pack_end (GTK_BOX (priv->top_vbox), alignment, TRUE, TRUE, 0);
670
671         gtk_container_add (GTK_CONTAINER (self), priv->top_vbox);
672
673         gtk_widget_show (alignment);
674         gtk_widget_show (priv->contents_view);
675         gtk_widget_show (priv->top_vbox);
676
677         connect_signals (MODEST_HEADER_WINDOW (self));
678
679         update_view (self, NULL);
680
681         /* Load previous osso state, for instance if we are being restored from 
682          * hibernation:  */
683         modest_osso_load_state ();
684
685         /* Get device name */
686         modest_maemo_utils_get_device_name ();
687
688         app = hildon_program_get_instance ();
689         hildon_program_add_window (app, HILDON_WINDOW (self));
690
691         /* Set window icon */
692         window_icon = modest_platform_get_icon (MODEST_APP_ICON, MODEST_ICON_SIZE_BIG);
693         if (window_icon) {
694                 gtk_window_set_icon (GTK_WINDOW (self), window_icon);
695                 g_object_unref (window_icon);
696         }
697
698         /* Dont't restore settings here, 
699          * because it requires a gtk_widget_show(), 
700          * and we don't want to do that until later,
701          * so that the UI is not visible for non-menu D-Bus activation.
702          */
703
704         /* setup edit modes */
705         modest_hildon2_window_register_edit_mode (MODEST_HILDON2_WINDOW (self), EDIT_MODE_COMMAND_DELETE,
706                                                   _("mcen_ti_edit_delete"), _HL("wdgt_bd_delete"),
707                                                   GTK_TREE_VIEW (priv->header_view),
708                                                   GTK_SELECTION_MULTIPLE,
709                                                   EDIT_MODE_CALLBACK (modest_ui_actions_on_edit_mode_delete_message));
710         modest_hildon2_window_register_edit_mode (MODEST_HILDON2_WINDOW (self), EDIT_MODE_COMMAND_MOVE,
711                                                   _("mcen_ti_edit_move"), _HL("wdgt_bd_move"),
712                                                   GTK_TREE_VIEW (priv->header_view),
713                                                   GTK_SELECTION_MULTIPLE,
714                                                   EDIT_MODE_CALLBACK (modest_ui_actions_on_edit_mode_move_to));
715
716
717         modest_window_set_active_account (MODEST_WINDOW (self), account_name);
718         modest_window_set_active_mailbox (MODEST_WINDOW (self), mailbox);
719         mgr = modest_runtime_get_account_mgr ();
720         settings = modest_account_mgr_load_account_settings (mgr, account_name);
721         if (settings) {
722                 account_display_name = g_strdup (modest_account_settings_get_display_name (settings));
723                 store_settings = modest_account_settings_get_store_settings (settings);
724                 if (store_settings) {
725                         priv->current_store_account = 
726                                 g_strdup (modest_server_account_settings_get_account_name (store_settings));
727                         g_object_unref (store_settings);
728                 }
729                 g_object_unref (settings);
730         }
731         /* Set window title */
732         if (TNY_IS_FOLDER (folder)) {
733                 gchar *folder_name;
734
735                 if (tny_folder_get_folder_type (folder) == TNY_FOLDER_TYPE_INBOX) {
736                         const gchar *box_name;
737                         box_name = mailbox;
738                         if (box_name == NULL || box_name[0] == '\0') {
739                                 box_name = account_display_name;
740                         }
741                         folder_name = g_strconcat (_("mcen_me_folder_inbox"), " - ", box_name, NULL);
742                 } else {
743                         folder_name = modest_tny_folder_get_display_name (folder);
744                 }
745                 
746                 gtk_window_set_title (GTK_WINDOW (self), folder_name);
747                 g_free (folder_name);
748         }
749         g_free (account_display_name);
750
751
752         update_progress_hint (self);
753         update_sort_button (self);
754
755         return MODEST_WINDOW(self);
756 }
757
758 ModestHeaderView *
759 modest_header_window_get_header_view (ModestHeaderWindow *self)
760 {
761         ModestHeaderWindowPrivate *priv = NULL;
762
763         g_return_val_if_fail (MODEST_IS_HEADER_WINDOW(self), FALSE);
764
765         priv = MODEST_HEADER_WINDOW_GET_PRIVATE (self);
766         
767         return MODEST_HEADER_VIEW (priv->header_view);
768 }
769
770 static void setup_menu (ModestHeaderWindow *self)
771 {
772         ModestHeaderWindowPrivate *priv;
773
774         g_return_if_fail (MODEST_IS_HEADER_WINDOW(self));
775         priv = MODEST_HEADER_WINDOW_GET_PRIVATE (self);
776
777         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_new_message"), "<Control>n",
778                                            APP_MENU_CALLBACK (modest_ui_actions_on_new_msg),
779                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_new_msg));
780         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_move_messages"), NULL,
781                                            APP_MENU_CALLBACK (set_moveto_edit_mode),
782                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_move_to));
783         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_delete_messages"), NULL,
784                                            APP_MENU_CALLBACK (set_delete_edit_mode),
785                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_delete));
786         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_folder_details"), NULL,
787                                            APP_MENU_CALLBACK (modest_ui_actions_on_details),
788                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_details));
789         priv->sort_button = hildon_button_new (MODEST_EDITABLE_SIZE,
790                                                HILDON_BUTTON_ARRANGEMENT_VERTICAL);
791         hildon_button_set_title (HILDON_BUTTON (priv->sort_button), _("mcen_me_sort"));
792         g_signal_connect (G_OBJECT (priv->sort_button), "clicked",
793                           G_CALLBACK (modest_ui_actions_on_sort), (gpointer) self);
794         hildon_button_set_style(HILDON_BUTTON (priv->sort_button), HILDON_BUTTON_STYLE_PICKER);
795         hildon_button_set_title_alignment (HILDON_BUTTON (priv->sort_button), 0.5, 0.5);
796         hildon_button_set_value_alignment (HILDON_BUTTON (priv->sort_button), 0.5, 0.5);
797         modest_hildon2_window_add_button_to_menu (MODEST_HILDON2_WINDOW (self), GTK_BUTTON (priv->sort_button),
798                                                   modest_ui_dimming_rules_on_sort);
799         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_sendandreceive"), NULL,
800                                            APP_MENU_CALLBACK (modest_ui_actions_on_send_receive),
801                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_send_receive));
802         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_outbox_cancelsend"), NULL,
803                                            APP_MENU_CALLBACK (modest_ui_actions_cancel_send),
804                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_cancel_sending_all));
805 }
806
807 static void 
808 update_view (ModestHeaderWindow *self,
809              TnyFolderChange *change)
810 {
811         ModestHeaderWindowPrivate *priv = NULL;
812         gboolean refilter = FALSE;
813         gboolean folder_empty = FALSE;
814         gboolean all_marked_as_deleted = FALSE;
815
816         g_return_if_fail (MODEST_IS_HEADER_WINDOW(self));
817         priv = MODEST_HEADER_WINDOW_GET_PRIVATE (self);
818         g_return_if_fail (priv->folder);
819
820         if (change != NULL) {
821                 TnyFolderChangeChanged changed;
822
823                 changed = tny_folder_change_get_changed (change);
824                 /* If something changes */
825                 if ((changed) & TNY_FOLDER_CHANGE_CHANGED_ALL_COUNT)
826                         folder_empty = (((guint) tny_folder_change_get_new_all_count (change)) == 0);
827                 else
828                         folder_empty = (((guint) tny_folder_get_all_count (TNY_FOLDER (priv->folder))) == 0);
829
830                 if ((changed) & TNY_FOLDER_CHANGE_CHANGED_EXPUNGED_HEADERS)
831                         refilter = TRUE;
832         } else {
833                 folder_empty = (((guint) tny_folder_get_all_count (TNY_FOLDER (priv->folder))) == 0);
834         }
835
836         /* Check if all messages are marked to be deleted */
837         all_marked_as_deleted = modest_header_view_is_empty (MODEST_HEADER_VIEW (priv->header_view));
838         folder_empty = folder_empty || all_marked_as_deleted;
839
840         /* Set style of headers view */
841         set_contents_state (self, folder_empty?CONTENTS_STATE_EMPTY:CONTENTS_STATE_HEADERS);
842
843         if (refilter)
844                 modest_header_view_refilter (MODEST_HEADER_VIEW (priv->header_view));
845 }
846
847 static void 
848 set_contents_state (ModestHeaderWindow *self, 
849                     ContentsState state)
850 {
851         ModestHeaderWindowPrivate *priv = NULL;
852
853         g_return_if_fail (MODEST_IS_HEADER_WINDOW(self));
854         priv = MODEST_HEADER_WINDOW_GET_PRIVATE (self);
855
856         if (priv->contents_state == state)
857                 return;
858
859         /* Remove from container the old content */
860         switch (priv->contents_state) {
861         case CONTENTS_STATE_EMPTY:
862                 gtk_container_remove (GTK_CONTAINER (priv->contents_view), priv->empty_view);
863                 break;
864         case CONTENTS_STATE_HEADERS:
865                 gtk_container_remove (GTK_CONTAINER (priv->contents_view), priv->header_view);
866                 break;
867         case CONTENTS_STATE_NONE:
868                 break;
869         }
870
871         /* Add the new content */
872         switch (state) {
873         case CONTENTS_STATE_EMPTY:
874                 gtk_container_add (GTK_CONTAINER (priv->contents_view), priv->empty_view);
875                 gtk_widget_show (priv->empty_view);
876                 break;
877         case CONTENTS_STATE_HEADERS:
878                 gtk_container_add (GTK_CONTAINER (priv->contents_view), priv->header_view);
879                 gtk_widget_show (priv->header_view);
880                 break;
881         case CONTENTS_STATE_NONE:
882                 break;
883         }
884         priv->contents_state = state;
885 }
886
887 static void
888 on_msg_count_changed (ModestHeaderView *header_view,
889                       TnyFolder *folder,
890                       TnyFolderChange *change,
891                       ModestHeaderWindow *header_window)
892 {
893         g_return_if_fail (MODEST_IS_HEADER_WINDOW (header_window));
894
895         update_view (MODEST_HEADER_WINDOW (header_window), change);
896 }
897
898 static void 
899 on_header_activated (ModestHeaderView *header_view,
900                      TnyHeader *header,
901                      GtkTreePath *path,
902                      ModestHeaderWindow *header_window)
903 {
904         modest_ui_actions_on_header_activated (header_view, header, path, MODEST_WINDOW (header_window));
905 }
906
907 static void
908 updating_banner_destroyed (gpointer data,
909                            GObject *where_the_object_was)
910 {
911         ModestHeaderWindowPrivate *priv = NULL;
912
913         priv = MODEST_HEADER_WINDOW_GET_PRIVATE (data);
914
915         priv->updating_banner = NULL;
916 }
917
918 static gboolean
919 show_updating_banner (gpointer user_data)
920 {
921         ModestHeaderWindowPrivate *priv = NULL;
922
923         priv = MODEST_HEADER_WINDOW_GET_PRIVATE (user_data);
924
925         if (priv->updating_banner == NULL) {
926
927                 /* We're outside the main lock */
928                 gdk_threads_enter ();
929                 priv->updating_banner = 
930                         modest_platform_animation_banner (GTK_WIDGET (user_data), NULL,
931                                                           _CS ("ckdg_pb_updating"));
932
933                 /* We need this because banners in Maemo could be
934                    destroyed by dialogs so we need to properly update
935                    our reference to it */
936                 g_object_weak_ref (G_OBJECT (priv->updating_banner),
937                                    updating_banner_destroyed,
938                                    user_data);
939                 gdk_threads_leave ();
940         }
941
942         /* Remove timeout */
943         priv->updating_banner_timeout = 0;
944         return FALSE;
945 }
946
947 /**
948  * We use this function to show/hide a progress banner showing
949  * "Updating" while the header view is being filled. We're not showing
950  * it unless the update takes more than 2 seconds
951  *
952  * If starting = TRUE then the refresh is starting, otherwise it means
953  * that is has just finished
954  */
955 static void 
956 on_updating_msg_list (ModestHeaderView *header_view,
957                       gboolean starting,
958                       gpointer user_data)
959 {
960         ModestHeaderWindowPrivate *priv = NULL;
961
962         priv = MODEST_HEADER_WINDOW_GET_PRIVATE (user_data);
963         
964         /* Remove old timeout */
965         if (priv->updating_banner_timeout > 0) {
966                 g_source_remove (priv->updating_banner_timeout);
967                 priv->updating_banner_timeout = 0;
968         }
969
970         /* Create a new timeout */
971         if (starting) {
972                 priv->updating_banner_timeout = 
973                         g_timeout_add (2000, show_updating_banner, user_data);
974         } else {
975                 /* Remove the banner if exists */
976                 if (priv->updating_banner) {
977                         gtk_widget_destroy (priv->updating_banner);
978                         priv->updating_banner = NULL;
979                 }
980         }
981 }
982
983 static void
984 set_delete_edit_mode (GtkButton *button,
985                       ModestHeaderWindow *self)
986 {
987         modest_hildon2_window_set_edit_mode (MODEST_HILDON2_WINDOW (self), EDIT_MODE_COMMAND_DELETE);
988 }
989
990 static void
991 set_moveto_edit_mode (GtkButton *button,
992                     ModestHeaderWindow *self)
993 {
994         modest_hildon2_window_set_edit_mode (MODEST_HILDON2_WINDOW (self), EDIT_MODE_COMMAND_MOVE);
995 }
996
997 static gboolean 
998 on_expose_event(GtkTreeView *header_view,
999                 GdkEventExpose *event,
1000                 gpointer user_data)
1001 {
1002         ModestHeaderWindow *self = (ModestHeaderWindow *) user_data;
1003         ModestHeaderWindowPrivate *priv = MODEST_HEADER_WINDOW_GET_PRIVATE (self);
1004
1005         g_return_val_if_fail (MODEST_IS_HEADER_WINDOW (self), FALSE);
1006
1007         if (priv->autoscroll)
1008                 hildon_pannable_area_jump_to (HILDON_PANNABLE_AREA (priv->contents_view), 0.0, 0.0);
1009
1010         return FALSE;
1011 }
1012
1013 static gboolean 
1014 on_map_event(GtkWidget *widget,
1015              GdkEvent *event,
1016              gpointer user_data)
1017 {
1018         ModestHeaderWindow *self = (ModestHeaderWindow *) user_data;
1019         ModestHeaderWindowPrivate *priv = MODEST_HEADER_WINDOW_GET_PRIVATE (self);
1020
1021         g_return_val_if_fail (MODEST_IS_HEADER_WINDOW (self), FALSE);
1022
1023         if (priv->progress_hint) {
1024                 hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), TRUE);
1025         }
1026         return FALSE;
1027 }
1028
1029 static void
1030 on_progress_list_changed (ModestWindowMgr *mgr,
1031                           ModestHeaderWindow *self)
1032 {
1033         update_progress_hint (self);
1034 }
1035
1036 static gboolean
1037 has_active_operations (ModestHeaderWindow *self)
1038 {
1039         GSList *operations = NULL, *node;
1040         ModestMailOperationQueue *queue;
1041         gboolean has_active = FALSE;
1042
1043         queue = modest_runtime_get_mail_operation_queue ();
1044         operations = modest_mail_operation_queue_get_by_source (queue, G_OBJECT (self));
1045
1046         for (node = operations; node != NULL; node = g_slist_next (node)) {
1047                 if (!modest_mail_operation_is_finished (MODEST_MAIL_OPERATION (node->data))) {
1048                         has_active = TRUE;
1049                         break;
1050                 }
1051         }
1052
1053         if (operations) {
1054                 g_slist_foreach (operations, (GFunc) g_object_unref, NULL);
1055                 g_slist_free (operations);
1056         }
1057
1058         return has_active;
1059 }
1060
1061 static void
1062 update_progress_hint (ModestHeaderWindow *self)
1063 {
1064         ModestHeaderWindowPrivate *priv;
1065
1066         priv = MODEST_HEADER_WINDOW_GET_PRIVATE (self);
1067
1068         priv->progress_hint = FALSE;
1069
1070         if (has_active_operations (self)) {
1071                 priv->progress_hint = TRUE;
1072         } else {
1073                 priv->progress_hint = FALSE;
1074         }
1075
1076         if (!priv->progress_hint && priv->current_store_account) {
1077                 priv->progress_hint = 
1078                         modest_window_mgr_has_progress_operation_on_account (modest_runtime_get_window_mgr (),
1079                                                                              priv->current_store_account);
1080         }
1081
1082         modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (self));
1083
1084         if (GTK_WIDGET_VISIBLE (self)) {
1085                 hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), priv->progress_hint?1:0);
1086         }
1087 }
1088
1089 gboolean
1090 modest_header_window_toolbar_on_transfer_mode     (ModestHeaderWindow *self)
1091 {
1092         ModestHeaderWindowPrivate *priv= NULL; 
1093
1094         g_return_val_if_fail (MODEST_IS_HEADER_WINDOW (self), FALSE);
1095         priv = MODEST_HEADER_WINDOW_GET_PRIVATE (self);
1096
1097         return priv->progress_hint;
1098 }
1099
1100 gboolean 
1101 modest_header_window_transfer_mode_enabled (ModestHeaderWindow *self)
1102 {
1103         ModestHeaderWindowPrivate *priv;
1104         
1105         g_return_val_if_fail (MODEST_IS_HEADER_WINDOW (self), FALSE);   
1106         priv = MODEST_HEADER_WINDOW_GET_PRIVATE(self);
1107
1108         return priv->progress_hint;
1109 }
1110
1111 static void 
1112 on_mail_operation_started (ModestMailOperation *mail_op,
1113                            gpointer user_data)
1114 {
1115         ModestHeaderWindow *self;
1116         ModestMailOperationTypeOperation op_type;
1117         GObject *source = NULL;
1118
1119         self = MODEST_HEADER_WINDOW (user_data);
1120         op_type = modest_mail_operation_get_type_operation (mail_op);
1121         source = modest_mail_operation_get_source(mail_op);
1122         if (G_OBJECT (self) == source) {
1123                 update_progress_hint (self);
1124         }
1125         g_object_unref (source);
1126 }
1127
1128 static void 
1129 on_mail_operation_finished (ModestMailOperation *mail_op,
1130                             gpointer user_data)
1131 {
1132         ModestHeaderWindow *self;
1133
1134         self = MODEST_HEADER_WINDOW (user_data);
1135
1136         /* Don't disable the progress hint if there are more pending
1137            operations from this window */
1138         update_progress_hint (self);
1139
1140         modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (self));
1141 }
1142
1143 static void
1144 on_queue_changed (ModestMailOperationQueue *queue,
1145                   ModestMailOperation *mail_op,
1146                   ModestMailOperationQueueNotification type,
1147                   ModestHeaderWindow *self)
1148 {
1149         ModestHeaderWindowPrivate *priv;
1150
1151         priv = MODEST_HEADER_WINDOW_GET_PRIVATE (self);
1152
1153         /* If this operations was created by another window, do nothing */
1154         if (!modest_mail_operation_is_mine (mail_op, G_OBJECT(self))) 
1155                 return;
1156
1157         if (type == MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED) {
1158                 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
1159                                                                G_OBJECT (mail_op),
1160                                                                "operation-started",
1161                                                                G_CALLBACK (on_mail_operation_started),
1162                                                                self);
1163                 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
1164                                                                G_OBJECT (mail_op),
1165                                                                "operation-finished",
1166                                                                G_CALLBACK (on_mail_operation_finished),
1167                                                                self);
1168         } else if (type == MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED) {
1169                 priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
1170                                                                   G_OBJECT (mail_op),
1171                                                                   "operation-started");
1172                 priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
1173                                                                   G_OBJECT (mail_op),
1174                                                                   "operation-finished");
1175         }
1176 }
1177
1178 static void
1179 modest_header_window_pack_toolbar (ModestHildon2Window *self,
1180                                    GtkPackType pack_type,
1181                                    GtkWidget *toolbar)
1182 {
1183         ModestHeaderWindowPrivate *priv;
1184
1185         g_return_if_fail (MODEST_IS_HEADER_WINDOW (self));
1186         priv = MODEST_HEADER_WINDOW_GET_PRIVATE (self);
1187
1188         if (pack_type == GTK_PACK_START) {
1189                 gtk_box_pack_start (GTK_BOX (priv->top_vbox), toolbar, FALSE, FALSE, 0);
1190         } else {
1191                 gtk_box_pack_end (GTK_BOX (priv->top_vbox), toolbar, FALSE, FALSE, 0);
1192         }
1193 }
1194
1195 static void 
1196 edit_mode_changed (ModestHeaderWindow *header_window,
1197                    gint edit_mode_id,
1198                    gboolean enabled,
1199                    ModestHeaderWindow *self)
1200 {
1201         ModestHeaderWindowPrivate *priv;
1202         ModestHeaderViewFilter filter = MODEST_HEADER_VIEW_FILTER_NONE;
1203
1204         g_return_if_fail (MODEST_IS_HEADER_WINDOW (self));
1205         priv = MODEST_HEADER_WINDOW_GET_PRIVATE (self);
1206
1207         switch (edit_mode_id) {
1208         case EDIT_MODE_COMMAND_MOVE:
1209                 filter = MODEST_HEADER_VIEW_FILTER_MOVEABLE;
1210                 break;
1211         case EDIT_MODE_COMMAND_DELETE:
1212                 filter = MODEST_HEADER_VIEW_FILTER_DELETABLE;
1213                 break;
1214         case MODEST_HILDON2_WINDOW_EDIT_MODE_NONE:
1215                 filter = MODEST_HEADER_VIEW_FILTER_NONE;
1216                 break;
1217         }
1218
1219         hildon_tree_view_set_action_area_visible (GTK_TREE_VIEW (priv->header_view), !enabled);
1220         if (enabled) {
1221                 modest_header_view_set_filter (MODEST_HEADER_VIEW (priv->header_view), 
1222                                                filter);
1223         } else {
1224                 GtkTreeSelection *sel;
1225
1226                 /* Unselect all. This will prevent us from keeping a
1227                    reference to a TnyObject that we don't want to
1228                    have */
1229                 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->header_view));
1230                 gtk_tree_selection_unselect_all (sel);
1231
1232                 modest_header_view_unset_filter (MODEST_HEADER_VIEW (priv->header_view), 
1233                                                  filter);
1234         }
1235 }
1236
1237 static void 
1238 on_sort_column_changed (GtkTreeSortable *treesortable,
1239                         gpointer         user_data)
1240 {
1241         update_sort_button (MODEST_HEADER_WINDOW (user_data));
1242 }
1243
1244 static void
1245 update_sort_button (ModestHeaderWindow *self)
1246 {
1247         ModestHeaderWindowPrivate *priv;
1248         GtkTreeSortable *sortable;
1249         gint current_sort_colid = -1;
1250         GtkSortType current_sort_type;
1251         const gchar *value = NULL;
1252
1253         priv = MODEST_HEADER_WINDOW_GET_PRIVATE (self);
1254         sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (GTK_TREE_VIEW (priv->header_view)));
1255
1256         if (!gtk_tree_sortable_get_sort_column_id (sortable,
1257                                                    &current_sort_colid, &current_sort_type)) {
1258                 value =  _("mcen_li_sort_sender_date_newest");
1259         } else {
1260                 switch (current_sort_colid) {
1261                 case TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN:
1262                 {
1263                         GList *cols = NULL;
1264                         cols = modest_header_view_get_columns (MODEST_HEADER_VIEW (priv->header_view));
1265                         if (cols != NULL) {
1266                                 gpointer flags_sort_type_pointer;
1267                                 flags_sort_type_pointer = g_object_get_data (G_OBJECT (cols->data), 
1268                                                                              MODEST_HEADER_VIEW_FLAG_SORT);
1269                                 if (GPOINTER_TO_INT (flags_sort_type_pointer) == TNY_HEADER_FLAG_PRIORITY_MASK)
1270                                         value = _("mcen_li_sort_priority");
1271                                 else
1272                                         value = _("mcen_li_sort_attachment");
1273                                 g_list_free(cols);      
1274                         }
1275                 } 
1276                 break;
1277                 case TNY_GTK_HEADER_LIST_MODEL_TO_COLUMN:
1278                 case TNY_GTK_HEADER_LIST_MODEL_FROM_COLUMN:
1279                         if (current_sort_type == GTK_SORT_ASCENDING)
1280                                 value = _("mcen_li_sort_sender_recipient_az");
1281                         else
1282                                 value = _("mcen_li_sort_sender_recipient_za");
1283                         break;
1284                 case TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN:
1285                 case TNY_GTK_HEADER_LIST_MODEL_DATE_RECEIVED_TIME_T_COLUMN:
1286                         if (current_sort_type == GTK_SORT_ASCENDING)
1287                                 value = _("mcen_li_sort_date_oldest");
1288                         else
1289                                 value = _("mcen_li_sort_date_newest");
1290                         break;
1291                 case TNY_GTK_HEADER_LIST_MODEL_SUBJECT_COLUMN:
1292                         if (current_sort_type == GTK_SORT_ASCENDING)
1293                                 value = _("mcen_li_sort_subject_az");
1294                         else
1295                                 value = _("mcen_li_sort_subject_za");
1296                         break;
1297                 case TNY_GTK_HEADER_LIST_MODEL_MESSAGE_SIZE_COLUMN:
1298                         if (current_sort_type == GTK_SORT_ASCENDING)
1299                                 value = _("mcen_li_sort_size_smallest");
1300                         else
1301                                 value = _("mcen_li_sort_size_largest");
1302                         break;
1303                 } 
1304         }
1305
1306         hildon_button_set_value (HILDON_BUTTON (priv->sort_button), value?value:"");
1307 }
1308
1309 static void
1310 on_horizontal_movement (HildonPannableArea *hildonpannable,
1311                         gint                direction,
1312                         gdouble             initial_x,
1313                         gdouble             initial_y,
1314                         gpointer            user_data)
1315 {
1316         ModestHeaderWindowPrivate *priv;
1317         gint dest_x, dest_y;
1318         TnyHeader *header;
1319
1320         /* Ignore right to left movement */
1321         if (direction == HILDON_MOVEMENT_LEFT)
1322                 return;
1323
1324         /* Get the header to delete */
1325         priv = MODEST_HEADER_WINDOW_GET_PRIVATE (user_data);
1326
1327         /* Get tree view coordinates */
1328         if (!gtk_widget_translate_coordinates ((GtkWidget *) hildonpannable,
1329                                                priv->header_view,
1330                                                initial_x,
1331                                                initial_y,
1332                                                &dest_x,
1333                                                &dest_y))
1334             return;
1335
1336         header = modest_header_view_get_header_at_pos ((ModestHeaderView *) priv->header_view,
1337                                                        dest_x, dest_y);
1338         if (header) {
1339                 delete_header ((GtkWindow *) user_data, header);
1340                 g_object_unref (header);
1341         }
1342 }