* Add a new dialog, Sort Dialog (WID-DIA021).
[modest] / src / widgets / modest-header-view.c
1 /* Copyright (c) 2006, 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 <glib/gi18n.h>
31 #include <tny-list.h>
32 #include <tny-simple-list.h>
33 #include <tny-folder-monitor.h>
34 #include <string.h>
35
36 #include <modest-header-view.h>
37 #include <modest-header-view-priv.h>
38 #include <modest-dnd.h>
39 #include <modest-tny-folder.h>
40
41 #include <modest-marshal.h>
42 #include <modest-text-utils.h>
43 #include <modest-icon-names.h>
44 #include <modest-runtime.h>
45
46 static void modest_header_view_class_init  (ModestHeaderViewClass *klass);
47 static void modest_header_view_init        (ModestHeaderView *obj);
48 static void modest_header_view_finalize    (GObject *obj);
49
50 static gboolean      on_header_clicked      (GtkWidget *widget, 
51                                             GdkEventButton *event, 
52                                             gpointer user_data);
53
54 static gint          cmp_rows               (GtkTreeModel *tree_model, 
55                                              GtkTreeIter *iter1, 
56                                              GtkTreeIter *iter2,
57                                              gpointer user_data);
58
59 static void          on_selection_changed   (GtkTreeSelection *sel, 
60                                              gpointer user_data);
61
62 static void          setup_drag_and_drop    (GtkTreeView *self);
63
64 static GtkTreePath * get_selected_row       (GtkTreeView *self, GtkTreeModel **model);
65
66 static gboolean      on_focus_in            (GtkWidget     *sef,
67                                              GdkEventFocus *event,
68                                              gpointer       user_data);
69
70 typedef struct _ModestHeaderViewPrivate ModestHeaderViewPrivate;
71 struct _ModestHeaderViewPrivate {
72         TnyFolder            *folder;
73         ModestHeaderViewStyle style;
74
75         TnyFolderMonitor     *monitor;
76         GMutex               *monitor_lock;
77         gint sort_colid[2][TNY_FOLDER_TYPE_NUM];
78         gint sort_type[2][TNY_FOLDER_TYPE_NUM];
79 };
80
81 #define MODEST_HEADER_VIEW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
82                                                 MODEST_TYPE_HEADER_VIEW, \
83                                                 ModestHeaderViewPrivate))
84
85
86
87 #define MODEST_HEADER_VIEW_PTR "modest-header-view"
88
89 enum {
90         HEADER_SELECTED_SIGNAL,
91         HEADER_ACTIVATED_SIGNAL,
92         ITEM_NOT_FOUND_SIGNAL,
93         STATUS_UPDATE_SIGNAL,
94         LAST_SIGNAL
95 };
96
97 /* globals */
98 static GObjectClass *parent_class = NULL;
99
100 /* uncomment the following if you have defined any signals */
101 static guint signals[LAST_SIGNAL] = {0};
102
103 GType
104 modest_header_view_get_type (void)
105 {
106         static GType my_type = 0;
107         if (!my_type) {
108                 static const GTypeInfo my_info = {
109                         sizeof(ModestHeaderViewClass),
110                         NULL,           /* base init */
111                         NULL,           /* base finalize */
112                         (GClassInitFunc) modest_header_view_class_init,
113                         NULL,           /* class finalize */
114                         NULL,           /* class data */
115                         sizeof(ModestHeaderView),
116                         1,              /* n_preallocs */
117                         (GInstanceInitFunc) modest_header_view_init,
118                         NULL
119                 };
120                 my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
121                                                   "ModestHeaderView",
122                                                   &my_info, 0);
123         }
124         return my_type;
125 }
126
127 static void
128 modest_header_view_class_init (ModestHeaderViewClass *klass)
129 {
130         GObjectClass *gobject_class;
131         gobject_class = (GObjectClass*) klass;
132
133         parent_class            = g_type_class_peek_parent (klass);
134         gobject_class->finalize = modest_header_view_finalize;
135         
136         g_type_class_add_private (gobject_class, sizeof(ModestHeaderViewPrivate));
137         
138         signals[HEADER_SELECTED_SIGNAL] = 
139                 g_signal_new ("header_selected",
140                               G_TYPE_FROM_CLASS (gobject_class),
141                               G_SIGNAL_RUN_FIRST,
142                               G_STRUCT_OFFSET (ModestHeaderViewClass,header_selected),
143                               NULL, NULL,
144                               g_cclosure_marshal_VOID__POINTER,
145                               G_TYPE_NONE, 1, G_TYPE_POINTER);
146
147         signals[HEADER_ACTIVATED_SIGNAL] = 
148                 g_signal_new ("header_activated",
149                               G_TYPE_FROM_CLASS (gobject_class),
150                               G_SIGNAL_RUN_FIRST,
151                               G_STRUCT_OFFSET (ModestHeaderViewClass,header_activated),
152                               NULL, NULL,
153                               g_cclosure_marshal_VOID__POINTER,
154                               G_TYPE_NONE, 1, G_TYPE_POINTER);
155         
156         
157         signals[ITEM_NOT_FOUND_SIGNAL] = 
158                 g_signal_new ("item_not_found",
159                               G_TYPE_FROM_CLASS (gobject_class),
160                               G_SIGNAL_RUN_FIRST,
161                               G_STRUCT_OFFSET (ModestHeaderViewClass,item_not_found),
162                               NULL, NULL,
163                               g_cclosure_marshal_VOID__INT,
164                               G_TYPE_NONE, 1, G_TYPE_INT);
165
166         signals[STATUS_UPDATE_SIGNAL] =
167                 g_signal_new ("status_update",
168                               G_TYPE_FROM_CLASS (gobject_class),
169                               G_SIGNAL_RUN_FIRST,
170                               G_STRUCT_OFFSET (ModestHeaderViewClass,status_update),
171                               NULL, NULL,
172                               modest_marshal_VOID__STRING_INT_INT,
173                               G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT);
174 }
175
176 static GtkTreeViewColumn*
177 get_new_column (const gchar *name, GtkCellRenderer *renderer,
178                 gboolean resizable, gint sort_col_id, gboolean show_as_text,
179                 GtkTreeCellDataFunc cell_data_func, gpointer user_data)
180 {
181         GtkTreeViewColumn *column;
182
183         column =  gtk_tree_view_column_new_with_attributes(name, renderer, NULL);
184         gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
185
186         gtk_tree_view_column_set_resizable (column, resizable);
187         if (resizable) 
188                 gtk_tree_view_column_set_expand (column, TRUE);
189         
190         if (show_as_text) 
191                 gtk_tree_view_column_add_attribute (column, renderer, "text",
192                                                     sort_col_id);
193         if (sort_col_id >= 0)
194                 gtk_tree_view_column_set_sort_column_id (column, sort_col_id);
195
196         gtk_tree_view_column_set_sort_indicator (column, FALSE);
197         gtk_tree_view_column_set_reorderable (column, TRUE);
198         
199         if (cell_data_func)
200                 gtk_tree_view_column_set_cell_data_func(column, renderer, cell_data_func,
201                                                         user_data, NULL);
202         return column;
203 }
204
205
206
207
208 static void
209 remove_all_columns (ModestHeaderView *obj)
210 {
211         GList *columns, *cursor;
212
213         columns = gtk_tree_view_get_columns (GTK_TREE_VIEW(obj));
214
215         for (cursor = columns; cursor; cursor = cursor->next)
216                 gtk_tree_view_remove_column (GTK_TREE_VIEW(obj),
217                                              GTK_TREE_VIEW_COLUMN(cursor->data));
218         g_list_free (columns);  
219 }
220
221 gboolean
222 modest_header_view_set_columns (ModestHeaderView *self, const GList *columns, TnyFolderType type)
223 {
224         GtkTreeModel *sortable;
225         GtkTreeViewColumn *column=NULL;
226         GtkTreeSelection *selection = NULL;
227         GtkCellRenderer *renderer_msgtype,*renderer_header,
228                 *renderer_attach,  *renderer_comptact_flag, 
229                 *renderer_compact_date;
230         ModestHeaderViewPrivate *priv;
231         const GList *cursor;
232         
233         priv = MODEST_HEADER_VIEW_GET_PRIVATE(self); 
234
235         /* FIXME: check whether these renderers need to be freed */
236         renderer_msgtype = gtk_cell_renderer_pixbuf_new ();
237         renderer_attach  = gtk_cell_renderer_pixbuf_new ();
238         renderer_header  = gtk_cell_renderer_text_new ();
239         renderer_comptact_flag  = gtk_cell_renderer_pixbuf_new ();
240         renderer_compact_date  = gtk_cell_renderer_text_new ();
241
242         g_object_set(G_OBJECT(renderer_header),
243                      "ellipsize", PANGO_ELLIPSIZE_END,
244                      NULL);
245         g_object_set(G_OBJECT(renderer_compact_date),
246                      "xalign", 1.0,
247                      NULL);
248         
249         remove_all_columns (self);
250
251         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
252         gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE);
253         sortable = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
254
255         /* Add new columns */
256         for (cursor = columns; cursor; cursor = g_list_next(cursor)) {
257                 ModestHeaderViewColumn col =
258                         (ModestHeaderViewColumn) GPOINTER_TO_INT(cursor->data);
259                 
260                 if (0> col || col >= MODEST_HEADER_VIEW_COLUMN_NUM) {
261                         g_printerr ("modest: invalid column %d in column list\n", col);
262                         continue;
263                 }
264                 
265                 switch (col) {
266                         
267                 case MODEST_HEADER_VIEW_COLUMN_MSGTYPE:
268                         column = get_new_column (_("M"), renderer_msgtype, FALSE,
269                                                  TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN,
270                                                  FALSE,
271                                                  (GtkTreeCellDataFunc)_modest_header_view_msgtype_cell_data,
272                                                  NULL);
273                         gtk_tree_view_column_set_fixed_width (column, 45);
274                         break;
275
276                 case MODEST_HEADER_VIEW_COLUMN_ATTACH:
277                         column = get_new_column (_("A"), renderer_attach, FALSE,
278                                                  TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN,
279                                                  FALSE,
280                                                  (GtkTreeCellDataFunc)_modest_header_view_attach_cell_data,
281                                                  NULL);
282                         gtk_tree_view_column_set_fixed_width (column, 45);
283                         break;
284
285                         
286                 case MODEST_HEADER_VIEW_COLUMN_FROM:
287                         column = get_new_column (_("From"), renderer_header, TRUE,
288                                                  TNY_GTK_HEADER_LIST_MODEL_FROM_COLUMN,
289                                                  TRUE,
290                                                  (GtkTreeCellDataFunc)_modest_header_view_sender_receiver_cell_data,
291                                                  GINT_TO_POINTER(TRUE));
292                         break;
293
294                 case MODEST_HEADER_VIEW_COLUMN_TO:
295                         column = get_new_column (_("To"), renderer_header, TRUE,
296                                                  TNY_GTK_HEADER_LIST_MODEL_TO_COLUMN,
297                                                  TRUE,
298                                                  (GtkTreeCellDataFunc)_modest_header_view_sender_receiver_cell_data,
299                                                  GINT_TO_POINTER(FALSE));
300                         break;
301                         
302                 case MODEST_HEADER_VIEW_COLUMN_COMPACT_FLAG:
303                         column = get_new_column (_("F"), renderer_comptact_flag, FALSE,
304                                                  TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN,
305                                                  FALSE,
306                                                  (GtkTreeCellDataFunc)_modest_header_view_compact_flag_cell_data,
307                                                  NULL);
308                         gtk_tree_view_column_set_fixed_width (column, 45);
309                         break;
310
311                 case MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_IN:
312                         column = get_new_column (_("Header"), renderer_header, TRUE,
313                                                  TNY_GTK_HEADER_LIST_MODEL_FROM_COLUMN,
314                                                  TRUE,
315                                                  (GtkTreeCellDataFunc)_modest_header_view_compact_header_cell_data,
316                                                  GINT_TO_POINTER(TRUE));
317                         break;
318
319                 case MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT:
320                         column = get_new_column (_("Header"), renderer_header, TRUE,
321                                                  TNY_GTK_HEADER_LIST_MODEL_FROM_COLUMN,
322                                                  TRUE,
323                                                  (GtkTreeCellDataFunc)_modest_header_view_compact_header_cell_data,
324                                                  GINT_TO_POINTER(FALSE));
325                         break;
326
327                         
328                 case MODEST_HEADER_VIEW_COLUMN_SUBJECT:
329                         column = get_new_column (_("Subject"), renderer_header, TRUE,
330                                                  TNY_GTK_HEADER_LIST_MODEL_SUBJECT_COLUMN,
331                                                  TRUE,
332                                                  (GtkTreeCellDataFunc)_modest_header_view_header_cell_data,
333                                                  NULL);
334                         break;
335                         
336                 case MODEST_HEADER_VIEW_COLUMN_RECEIVED_DATE:
337                         column = get_new_column (_("Received"), renderer_header, TRUE,
338                                                  TNY_GTK_HEADER_LIST_MODEL_DATE_RECEIVED_TIME_T_COLUMN,
339                                                  TRUE,
340                                                  (GtkTreeCellDataFunc)_modest_header_view_date_cell_data,
341                                                  GINT_TO_POINTER(TRUE));
342                         break;
343                         
344                 case MODEST_HEADER_VIEW_COLUMN_SENT_DATE:  
345                         column = get_new_column (_("Sent"), renderer_header, TRUE,
346                                                  TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN,
347                                                  TRUE,
348                                                  (GtkTreeCellDataFunc)_modest_header_view_date_cell_data,
349                                                  GINT_TO_POINTER(FALSE));
350                         break;
351                         
352                 case MODEST_HEADER_VIEW_COLUMN_COMPACT_RECEIVED_DATE:
353                         column = get_new_column (_("Received"), renderer_compact_date, FALSE,
354                                                  TNY_GTK_HEADER_LIST_MODEL_DATE_RECEIVED_TIME_T_COLUMN,
355                                                  TRUE,
356                                                  (GtkTreeCellDataFunc)_modest_header_view_compact_date_cell_data,
357                                                  GINT_TO_POINTER(TRUE));
358                         gtk_tree_view_column_set_fixed_width (column, 130);
359                         break;
360                         
361                 case MODEST_HEADER_VIEW_COLUMN_COMPACT_SENT_DATE:                                             
362                         column = get_new_column (_("Sent"), renderer_compact_date, FALSE,
363                                                  TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN,
364                                                  TRUE,
365                                                  (GtkTreeCellDataFunc)_modest_header_view_compact_date_cell_data,
366                                                  GINT_TO_POINTER(FALSE));
367                         gtk_tree_view_column_set_fixed_width (column, 130);
368                         break;
369                 case MODEST_HEADER_VIEW_COLUMN_SIZE:
370                         column = get_new_column (_("Size"), renderer_header, TRUE,
371                                                  TNY_GTK_HEADER_LIST_MODEL_MESSAGE_SIZE_COLUMN,
372                                                  FALSE,
373                                                  (GtkTreeCellDataFunc)_modest_header_view_size_cell_data,
374                                                  NULL); 
375                         break;
376                 case MODEST_HEADER_VIEW_COLUMN_STATUS:
377                         column = get_new_column (_("Status"), renderer_compact_date, TRUE,
378                                                  TNY_GTK_HEADER_LIST_MODEL_MESSAGE_SIZE_COLUMN,
379                                                  FALSE,
380                                                  (GtkTreeCellDataFunc)_modest_header_view_status_cell_data,
381                                                  NULL); 
382                         break;
383
384                 default:
385                         g_return_val_if_reached(FALSE);
386                 }
387
388                 /* we keep the column id around */
389                 g_object_set_data (G_OBJECT(column), MODEST_HEADER_VIEW_COLUMN,
390                                    GINT_TO_POINTER(col));
391                 
392                 /* we need this ptr when sorting the rows */
393                 g_object_set_data (G_OBJECT(column), MODEST_HEADER_VIEW_PTR,
394                                    self);
395                 gtk_tree_view_append_column (GTK_TREE_VIEW(self), column);              
396         }       
397         
398         
399         return TRUE;
400 }
401
402 static void
403 modest_header_view_init (ModestHeaderView *obj)
404 {
405         ModestHeaderViewPrivate *priv;
406         guint i, j;
407
408         priv = MODEST_HEADER_VIEW_GET_PRIVATE(obj); 
409
410         priv->folder  = NULL;
411
412         priv->monitor        = NULL;
413         priv->monitor_lock   = g_mutex_new ();
414
415         /* Sort parameters */
416         for (j=0; j < 2; j++) {
417                 for (i=0; i < TNY_FOLDER_TYPE_NUM; i++) {
418                         priv->sort_colid[j][i] = -1;
419                         priv->sort_type[j][i] = GTK_SORT_DESCENDING;
420                 }                       
421         }
422
423         setup_drag_and_drop (GTK_TREE_VIEW (obj));
424 }
425
426 static void
427 modest_header_view_finalize (GObject *obj)
428 {
429         ModestHeaderView        *self;
430         ModestHeaderViewPrivate *priv;
431         
432         self = MODEST_HEADER_VIEW(obj);
433         priv = MODEST_HEADER_VIEW_GET_PRIVATE(self);
434
435         g_mutex_lock (priv->monitor_lock);
436         if (priv->monitor) {
437                 tny_folder_monitor_stop (priv->monitor);
438                 g_object_unref (G_OBJECT (priv->monitor));
439         }
440         g_mutex_unlock (priv->monitor_lock);
441         g_mutex_free (priv->monitor_lock);
442
443         if (priv->folder) {
444                 g_object_unref (G_OBJECT (priv->folder));
445                 priv->folder   = NULL;
446         }
447         
448         G_OBJECT_CLASS(parent_class)->finalize (obj);
449 }
450
451
452 GtkWidget*
453 modest_header_view_new (TnyFolder *folder, ModestHeaderViewStyle style)
454 {
455         GObject *obj;
456         GtkTreeSelection *sel;
457         ModestHeaderView *self;
458         ModestHeaderViewPrivate *priv;
459         
460         g_return_val_if_fail (style >= 0 && style < MODEST_HEADER_VIEW_STYLE_NUM,
461                               NULL);
462         
463         obj  = G_OBJECT(g_object_new(MODEST_TYPE_HEADER_VIEW, NULL));
464         self = MODEST_HEADER_VIEW(obj);
465         priv = MODEST_HEADER_VIEW_GET_PRIVATE(self);
466         
467         modest_header_view_set_style   (self, style);
468         modest_header_view_set_folder (self, NULL);
469
470         gtk_tree_view_columns_autosize (GTK_TREE_VIEW(obj));
471         gtk_tree_view_set_fixed_height_mode (GTK_TREE_VIEW(obj),TRUE);
472         gtk_tree_view_set_enable_search (GTK_TREE_VIEW(obj), TRUE);
473         
474         gtk_tree_view_set_rules_hint (GTK_TREE_VIEW(obj),
475                                       TRUE); /* alternating row colors */
476         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
477         
478         g_signal_connect (sel, "changed",
479                           G_CALLBACK(on_selection_changed), self);
480         
481         g_signal_connect (self, "button-press-event",
482                           G_CALLBACK(on_header_clicked), NULL);
483
484         g_signal_connect (self, "focus-in-event",
485                           G_CALLBACK(on_focus_in), NULL);
486         
487         return GTK_WIDGET(self);
488 }
489
490
491 TnyList * 
492 modest_header_view_get_selected_headers (ModestHeaderView *self)
493 {
494         GtkTreeSelection *sel;
495         ModestHeaderViewPrivate *priv;
496         TnyList *header_list = NULL;
497         TnyHeader *header;
498         GList *list, *tmp = NULL;
499         GtkTreeModel *tree_model = NULL;
500         GtkTreeIter iter;
501
502         g_return_val_if_fail (self, NULL);
503         
504         priv = MODEST_HEADER_VIEW_GET_PRIVATE(self);
505
506         /* Get selected rows */
507         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
508         list = gtk_tree_selection_get_selected_rows (sel, &tree_model);
509
510         if (list) {
511                 header_list = tny_simple_list_new();
512
513                 list = g_list_reverse (list);
514                 tmp = list;
515                 while (tmp) {                   
516                         /* get header from selection */
517                         gtk_tree_model_get_iter (tree_model, &iter, (GtkTreePath *) (tmp->data));
518                         gtk_tree_model_get (tree_model, &iter,
519                                             TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN,
520                                             &header, -1);
521                         /* Prepend to list */
522                         tny_list_prepend (header_list, G_OBJECT (header));
523                         g_object_unref (G_OBJECT (header));
524
525                         tmp = g_list_next (tmp);
526                 }
527                 /* Clean up*/
528                 g_list_foreach (list, (GFunc) gtk_tree_path_free, NULL);
529                 g_list_free (list);
530         }
531         return header_list;
532 }
533
534
535 /* scroll our list view so the selected item is visible */
536 static void
537 scroll_to_selected (ModestHeaderView *self, GtkTreeIter *iter, gboolean up)
538 {
539 #ifdef MODEST_PLATFORM_GNOME 
540
541         GtkTreePath *selected_path;
542         GtkTreePath *start, *end;
543         
544         GtkTreeModel *model;
545         
546         model         = gtk_tree_view_get_model (GTK_TREE_VIEW(self));
547         selected_path = gtk_tree_model_get_path (model, iter);
548
549         start = gtk_tree_path_new ();
550         end   = gtk_tree_path_new ();
551
552         gtk_tree_view_get_visible_range (GTK_TREE_VIEW(self), &start, &end);
553
554         if (gtk_tree_path_compare (selected_path, start) < 0 ||
555             gtk_tree_path_compare (end, selected_path) < 0)
556                 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW(self),
557                                               selected_path, NULL, TRUE,
558                                               up ? 0.0 : 1.0,
559                                               up ? 0.0 : 1.0);
560         gtk_tree_path_free (selected_path);
561         gtk_tree_path_free (start);
562         gtk_tree_path_free (end);
563
564 #endif /* MODEST_PLATFORM_GNOME */
565 }
566
567
568 void 
569 modest_header_view_select_next (ModestHeaderView *self)
570 {
571         GtkTreeSelection *sel;
572         GtkTreeIter iter;
573         GtkTreeModel *model;
574         GtkTreePath *path;
575
576         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
577         path = get_selected_row (GTK_TREE_VIEW(self), &model);
578         if ((path != NULL) && (gtk_tree_model_get_iter(model, &iter, path))) {
579                 /* Unselect previous path */
580                 gtk_tree_selection_unselect_path (sel, path);
581                 
582                 /* Move path down and selects new one  */
583                 if (gtk_tree_model_iter_next (model, &iter)) {
584                         gtk_tree_selection_select_iter (sel, &iter);
585                         scroll_to_selected (self, &iter, FALSE);        
586                 }
587                 gtk_tree_path_free(path);
588         }
589         
590 }
591
592 void 
593 modest_header_view_select_prev (ModestHeaderView *self)
594 {
595         GtkTreeSelection *sel;
596         GtkTreeIter iter;
597         GtkTreeModel *model;
598         GtkTreePath *path;
599
600         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
601         path = get_selected_row (GTK_TREE_VIEW(self), &model);
602         if ((path != NULL) && (gtk_tree_model_get_iter(model, &iter, path))) {
603                 /* Unselect previous path */
604                 gtk_tree_selection_unselect_path (sel, path);
605
606                 /* Move path up */
607                 if (gtk_tree_path_prev (path)) {
608                         gtk_tree_model_get_iter (model, &iter, path);
609                         
610                         /* Select the new one */
611                         gtk_tree_selection_select_iter (sel, &iter);
612                         scroll_to_selected (self, &iter, TRUE); 
613
614                 }
615                 gtk_tree_path_free (path);
616         }
617 }
618
619 GList*
620 modest_header_view_get_columns (ModestHeaderView *self)
621 {
622         g_return_val_if_fail (self, FALSE);
623         return gtk_tree_view_get_columns (GTK_TREE_VIEW(self)); 
624 }
625
626
627 gboolean
628 modest_header_view_is_empty (ModestHeaderView *self)
629 {
630         g_return_val_if_fail (self, FALSE);
631         return FALSE; /* FIXME */
632 }
633
634
635 gboolean
636 modest_header_view_set_style (ModestHeaderView *self,
637                               ModestHeaderViewStyle style)
638 {
639         ModestHeaderViewPrivate *priv;
640         gboolean show_col_headers = FALSE;
641         ModestHeaderViewStyle old_style;
642         
643         g_return_val_if_fail (self, FALSE);
644         g_return_val_if_fail (style >= 0 && MODEST_HEADER_VIEW_STYLE_NUM,
645                               FALSE);
646
647         priv = MODEST_HEADER_VIEW_GET_PRIVATE(self);
648         if (priv->style == style)
649                 return TRUE; /* nothing to do */
650         
651         switch (style) {
652         case MODEST_HEADER_VIEW_STYLE_DETAILS:
653                 show_col_headers = TRUE;
654                 break;
655         case MODEST_HEADER_VIEW_STYLE_TWOLINES:
656                 break;
657         default:
658                 g_return_val_if_reached (FALSE);
659         }
660         gtk_tree_view_set_headers_visible   (GTK_TREE_VIEW(self), show_col_headers);
661         gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(self), show_col_headers);    
662
663         old_style   = priv->style;
664         priv->style = style;
665
666         return TRUE;
667 }
668
669
670 ModestHeaderViewStyle
671 modest_header_view_get_style (ModestHeaderView *self)
672 {
673         g_return_val_if_fail (self, FALSE);
674         return MODEST_HEADER_VIEW_GET_PRIVATE(self)->style;
675 }
676
677 /* 
678  * This function sets a sortable model in the header view. It's just
679  * used for developing purposes, because it only does a
680  * gtk_tree_view_set_model
681  */
682 static void
683 modest_header_view_set_model (GtkTreeView *header_view, GtkTreeModel *model)
684 {
685         GtkTreeModel *old_model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (header_view));
686
687         if (old_model_sort && GTK_IS_TREE_MODEL_SORT (old_model_sort)) { 
688                 GtkTreeModel *old_model;
689                 ModestHeaderViewPrivate *priv;
690
691                 priv = MODEST_HEADER_VIEW_GET_PRIVATE (header_view);
692                 old_model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (old_model_sort));
693
694                 /* Set new model */
695                 gtk_tree_view_set_model (header_view, model);
696         } else
697                 gtk_tree_view_set_model (header_view, model);
698
699         return;
700 }
701
702 static void
703 on_progress_changed (ModestMailOperation *mail_op,
704                      ModestHeaderView *self)
705 {
706         TnyFolderType type;
707         GtkTreeModel *sortable; 
708         ModestHeaderViewPrivate *priv;
709         GList *cols, *cursor;
710         TnyList *headers;
711         guint sort_colid;
712         GtkSortType sort_type;
713
714         if (!modest_mail_operation_is_finished (mail_op))
715                 return;
716
717         if (modest_mail_operation_get_error (mail_op))
718                 return;
719         
720         priv = MODEST_HEADER_VIEW_GET_PRIVATE(self);
721
722         headers = TNY_LIST (tny_gtk_header_list_model_new ());
723
724         tny_gtk_header_list_model_set_folder (TNY_GTK_HEADER_LIST_MODEL(headers),
725                                               priv->folder, TRUE);
726
727         sortable = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL(headers));
728         g_object_unref (G_OBJECT (headers));
729         
730         /* install our special sorting functions */
731         cursor = cols = gtk_tree_view_get_columns (GTK_TREE_VIEW(self));
732         while (cursor) {
733                 gint col_id = GPOINTER_TO_INT (g_object_get_data(G_OBJECT(cursor->data),
734                                                                  MODEST_HEADER_VIEW_COLUMN));
735                 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE(sortable),
736                                                  col_id,
737                                                  (GtkTreeIterCompareFunc) cmp_rows,
738                                                  cursor->data, NULL);
739                 cursor = g_list_next(cursor);
740         }
741         g_list_free (cols);
742
743         /* Restore sort column id */
744         type  = modest_tny_folder_guess_folder_type (priv->folder);
745         sort_colid = modest_header_view_get_sort_column_id (self, type); 
746         sort_type = modest_header_view_get_sort_type (self, type); 
747         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(sortable),
748                                               sort_colid,
749                                               sort_type);
750
751         /* Set new model */
752         modest_header_view_set_model (GTK_TREE_VIEW (self), sortable);
753         g_object_unref (G_OBJECT (sortable));
754
755         /* Add a folder observer */
756         g_mutex_lock (priv->monitor_lock);
757         priv->monitor = TNY_FOLDER_MONITOR (tny_folder_monitor_new (priv->folder));
758         tny_folder_monitor_add_list (priv->monitor, TNY_LIST (headers));
759         tny_folder_monitor_start (priv->monitor);
760         g_mutex_unlock (priv->monitor_lock);
761
762 }
763
764 TnyFolder*
765 modest_header_view_get_folder (ModestHeaderView *self)
766 {
767         ModestHeaderViewPrivate *priv;
768         priv = MODEST_HEADER_VIEW_GET_PRIVATE(self);
769
770         if (priv->folder)
771                 g_object_ref (priv->folder);
772
773         return priv->folder;
774 }
775
776 static void
777 modest_header_view_set_folder_intern (ModestHeaderView *self, TnyFolder *folder)
778 {
779         TnyFolderType type;
780         TnyList *headers;
781         ModestHeaderViewPrivate *priv;
782         GList *cols, *cursor;
783         GtkTreeModel *sortable; 
784         guint sort_colid;
785         GtkSortType sort_type;
786
787         priv = MODEST_HEADER_VIEW_GET_PRIVATE(self);
788
789         headers = TNY_LIST (tny_gtk_header_list_model_new ());
790
791         tny_gtk_header_list_model_set_folder (TNY_GTK_HEADER_LIST_MODEL(headers),
792                                               folder, FALSE);
793
794         /* Add a folder observer */
795         g_mutex_lock (priv->monitor_lock);
796         if (priv->monitor) {
797                 tny_folder_monitor_stop (priv->monitor);
798                 g_object_unref (G_OBJECT (priv->monitor));
799         }
800         priv->monitor = TNY_FOLDER_MONITOR (tny_folder_monitor_new (folder));
801         tny_folder_monitor_add_list (priv->monitor, TNY_LIST (headers));
802         tny_folder_monitor_start (priv->monitor);
803         g_mutex_unlock (priv->monitor_lock);
804
805
806         sortable = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL(headers));
807         g_object_unref (G_OBJECT (headers));
808
809         /* install our special sorting functions */
810         cursor = cols = gtk_tree_view_get_columns (GTK_TREE_VIEW(self));
811         while (cursor) {
812                 gint col_id = GPOINTER_TO_INT (g_object_get_data(G_OBJECT(cursor->data),
813                                                                  MODEST_HEADER_VIEW_COLUMN));
814                 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE(sortable),
815                                                  col_id,
816                                                  (GtkTreeIterCompareFunc) cmp_rows,
817                                                  cursor->data, NULL);
818                 cursor = g_list_next(cursor);
819         }
820         g_list_free (cols);
821
822         /* Restore sort column id */
823         type  = modest_tny_folder_guess_folder_type (folder);
824         sort_colid = modest_header_view_get_sort_column_id (self, type); 
825         sort_type = modest_header_view_get_sort_type (self, type); 
826         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(sortable),
827                                               sort_colid,
828                                               sort_type);
829
830         /* Set new model */
831         modest_header_view_set_model (GTK_TREE_VIEW (self), sortable);
832         g_object_unref (G_OBJECT (sortable));
833 }
834
835 void
836 modest_header_view_sort_by_column_id (ModestHeaderView *self, 
837                                       guint sort_colid,
838                                       GtkSortType sort_type)
839 {
840         ModestHeaderViewPrivate *priv = NULL;
841         GtkTreeModel *sortable = NULL; 
842         TnyFolderType type;
843
844         /* Get model and private data */
845         priv = MODEST_HEADER_VIEW_GET_PRIVATE(self);            
846         sortable = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
847         
848         /* Sort tree model */
849         type  = modest_tny_folder_guess_folder_type (priv->folder);
850         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(sortable),
851                                               sort_colid,
852                                               sort_type);
853         /* Store new sort parameters */
854         modest_header_view_set_sort_params (self, sort_colid, sort_type, type);
855
856         /* Save GConf parameters */
857 /*      modest_widget_memory_save (modest_runtime_get_conf(), */
858 /*                                 G_OBJECT(self), "header-view"); */
859         
860 }
861
862 void
863 modest_header_view_set_sort_params (ModestHeaderView *self, 
864                                     guint sort_colid, 
865                                     GtkSortType sort_type,
866                                     TnyFolderType type)
867 {
868         ModestHeaderViewPrivate *priv;
869         ModestHeaderViewStyle style;
870
871         style = modest_header_view_get_style   (self);
872         priv = MODEST_HEADER_VIEW_GET_PRIVATE(self);
873
874         priv->sort_colid[style][type] = sort_colid;
875         priv->sort_type[style][type] = sort_type;
876 }
877
878 gint
879 modest_header_view_get_sort_column_id (ModestHeaderView *self, 
880                                        TnyFolderType type)
881 {
882         ModestHeaderViewPrivate *priv;
883         ModestHeaderViewStyle style;
884
885         style = modest_header_view_get_style   (self);
886         priv = MODEST_HEADER_VIEW_GET_PRIVATE(self);
887
888         return priv->sort_colid[style][type];
889 }
890
891 GtkSortType
892 modest_header_view_get_sort_type (ModestHeaderView *self, 
893                                   TnyFolderType type)
894 {
895         ModestHeaderViewPrivate *priv;
896         ModestHeaderViewStyle style;
897
898         style = modest_header_view_get_style   (self);
899         priv = MODEST_HEADER_VIEW_GET_PRIVATE(self);
900
901         return priv->sort_type[style][type];
902 }
903
904 void
905 modest_header_view_set_folder (ModestHeaderView *self, TnyFolder *folder)
906 {
907         ModestHeaderViewPrivate *priv;
908
909         priv = MODEST_HEADER_VIEW_GET_PRIVATE(self);
910
911         if (priv->folder) {
912                 g_object_unref (priv->folder);
913                 priv->folder = NULL;
914         }
915
916         if (folder) {
917                 ModestMailOperation *mail_op;
918
919                 modest_header_view_set_folder_intern (self, folder);
920
921                 /* Pick my reference. Nothing to do with the mail operation */
922                 priv->folder = g_object_ref (folder);
923
924                 /* no message selected */
925                 g_signal_emit (G_OBJECT(self), signals[HEADER_SELECTED_SIGNAL], 0, NULL);
926
927                 /* Create the mail operation */
928                 mail_op = modest_mail_operation_new_with_id (MODEST_MAIL_OPERATION_ID_RECEIVE);
929                 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
930                                                  mail_op);
931
932                 /* Register a mail operation observer */
933                 g_signal_connect (mail_op, "progress-changed", 
934                                   G_CALLBACK (on_progress_changed), self);
935
936                 /* Refresh the folder asynchronously */
937                 modest_mail_operation_refresh_folder (mail_op, folder);
938
939                 /* Free */
940                 g_object_unref (mail_op);
941
942         } else {
943                 g_mutex_lock (priv->monitor_lock);
944                 modest_header_view_set_model (GTK_TREE_VIEW (self), NULL); 
945
946                 if (priv->monitor) {
947                         tny_folder_monitor_stop (priv->monitor);
948                         g_object_unref (G_OBJECT (priv->monitor));
949                         priv->monitor = NULL;
950                 }
951                 g_mutex_unlock (priv->monitor_lock);
952         }
953 }
954
955 static gboolean
956 on_header_clicked (GtkWidget *widget, GdkEventButton *event, gpointer user_data)
957 {
958         ModestHeaderView *self = NULL;
959         ModestHeaderViewPrivate *priv = NULL;
960         GtkTreePath *path = NULL;
961         GtkTreeIter iter;
962         GtkTreeModel *model = NULL;
963         TnyHeader *header;
964         /* ignore everything but doubleclick */
965         if (event->type != GDK_2BUTTON_PRESS)
966                 return FALSE;
967
968         self = MODEST_HEADER_VIEW (widget);
969         priv = MODEST_HEADER_VIEW_GET_PRIVATE(self);
970         
971         path = get_selected_row (GTK_TREE_VIEW(self), &model);
972         if ((path == NULL) || (!gtk_tree_model_get_iter(model, &iter, path))) 
973                 return FALSE;
974                         
975         /* get the first selected item */
976         gtk_tree_model_get (model, &iter,
977                             TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN,
978                             &header, -1);
979         /* Emit signal */
980         g_signal_emit (G_OBJECT(self), 
981                        signals[HEADER_ACTIVATED_SIGNAL], 
982                        0, header);
983
984         /* Free */
985         g_object_unref (G_OBJECT (header));
986         gtk_tree_path_free(path);
987
988         return TRUE;
989 }
990
991
992 static void
993 on_selection_changed (GtkTreeSelection *sel, gpointer user_data)
994 {
995         GtkTreeModel *model;
996         TnyHeader *header;
997         GtkTreePath *path = NULL;       
998         GtkTreeIter iter;
999         ModestHeaderView *self;
1000         ModestHeaderViewPrivate *priv;
1001         
1002         g_return_if_fail (sel);
1003         g_return_if_fail (user_data);
1004         
1005         self = MODEST_HEADER_VIEW (user_data);
1006         priv = MODEST_HEADER_VIEW_GET_PRIVATE(self);    
1007
1008         path = get_selected_row (GTK_TREE_VIEW(self), &model);
1009         if ((path == NULL) || (!gtk_tree_model_get_iter(model, &iter, path)))
1010                 return; /* msg was _un_selected */
1011
1012         gtk_tree_model_get (model, &iter,
1013                             TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN,
1014                             &header, -1);
1015
1016         /* Emit signal */
1017         g_signal_emit (G_OBJECT(self), 
1018                        signals[HEADER_SELECTED_SIGNAL], 
1019                        0, header);
1020
1021         g_object_unref (G_OBJECT (header));
1022         gtk_tree_path_free(path);
1023 }
1024
1025
1026 /* PROTECTED method. It's useful when we want to force a given
1027    selection to reload a msg. For example if we have selected a header
1028    in offline mode, when Modest become online, we want to reload the
1029    message automatically without an user click over the header */
1030 void 
1031 _modest_header_view_change_selection (GtkTreeSelection *selection,
1032                                       gpointer user_data)
1033 {
1034         g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1035         g_return_if_fail (MODEST_IS_HEADER_VIEW (user_data));
1036
1037         on_selection_changed (selection, user_data);
1038 }
1039
1040
1041 static gint
1042 cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2,
1043           gpointer user_data)
1044 {
1045         gint col_id;
1046         gint t1, t2;
1047         gint val1, val2;
1048         gchar *s1, *s2;
1049         gint cmp;       
1050         gint sort_colid;
1051         static int counter = 0;
1052
1053         g_return_val_if_fail (G_IS_OBJECT(user_data), 0);
1054         col_id = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(user_data), MODEST_HEADER_VIEW_COLUMN));
1055         
1056         if (!(++counter % 100)) {
1057                 GObject *header_view = g_object_get_data(G_OBJECT(user_data),
1058                                                          MODEST_HEADER_VIEW_PTR);
1059                 g_signal_emit (header_view,
1060                                signals[STATUS_UPDATE_SIGNAL],
1061                                0, _("Sorting..."), 0, 0);
1062         }       
1063         switch (col_id) {               
1064         case MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_IN:
1065         case MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT:
1066                 sort_colid = gtk_tree_view_column_get_sort_column_id (GTK_TREE_VIEW_COLUMN(user_data));
1067                 gtk_tree_model_get (tree_model, iter1,
1068                                     sort_colid, &s1,
1069                                     TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN, &t1,
1070                                     -1);
1071                 gtk_tree_model_get (tree_model, iter2,
1072                                     sort_colid, &s2,
1073                                     TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN, &t2,
1074                                     -1);
1075                 cmp = modest_text_utils_utf8_strcmp (s1, s2, TRUE);
1076                 g_free (s1);
1077                 g_free (s2);
1078                 
1079                 return cmp ? cmp : t1 - t2;
1080                 
1081                 break;
1082
1083         case MODEST_HEADER_VIEW_COLUMN_COMPACT_RECEIVED_DATE:
1084         case MODEST_HEADER_VIEW_COLUMN_RECEIVED_DATE:
1085
1086                 gtk_tree_model_get (tree_model, iter1,
1087                                     TNY_GTK_HEADER_LIST_MODEL_DATE_RECEIVED_TIME_T_COLUMN,
1088                                     &t1,-1);
1089                 gtk_tree_model_get (tree_model, iter2,
1090                                     TNY_GTK_HEADER_LIST_MODEL_DATE_RECEIVED_TIME_T_COLUMN,
1091                                     &t2,-1);
1092                 return t1 - t2;
1093
1094         case MODEST_HEADER_VIEW_COLUMN_COMPACT_SENT_DATE:
1095         case MODEST_HEADER_VIEW_COLUMN_SENT_DATE:
1096                 gtk_tree_model_get (tree_model, iter1,
1097                                     TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN,
1098                                     &t1,-1);
1099                 gtk_tree_model_get (tree_model, iter2,
1100                                     TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN,
1101                                     &t2,-1);
1102                 return t1 - t2;
1103
1104                 
1105                 /* next ones, we try the search criteria first, if they're the same, then we use 'sent date' */
1106                 /* FIXME: what about received-date? */
1107         case MODEST_HEADER_VIEW_COLUMN_SUBJECT: {
1108
1109                 gtk_tree_model_get (tree_model, iter1,
1110                                     TNY_GTK_HEADER_LIST_MODEL_SUBJECT_COLUMN, &s1,
1111                                     TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN, &t1,
1112                                     -1);
1113                 gtk_tree_model_get (tree_model, iter2,
1114                                     TNY_GTK_HEADER_LIST_MODEL_SUBJECT_COLUMN, &s2,
1115                                     TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN, &t2,
1116                                     -1);
1117
1118                 /* the prefix ('Re:', 'Fwd:' etc.) we ignore */ 
1119                 cmp = modest_text_utils_utf8_strcmp (s1 + modest_text_utils_get_subject_prefix_len(s1),
1120                                                      s2 + modest_text_utils_get_subject_prefix_len(s2),
1121                                                      TRUE);
1122                 g_free (s1);
1123                 g_free (s2);
1124                 
1125                 return cmp ? cmp : t1 - t2;
1126         }
1127                 
1128         case MODEST_HEADER_VIEW_COLUMN_FROM:
1129                 
1130                 gtk_tree_model_get (tree_model, iter1,
1131                                     TNY_GTK_HEADER_LIST_MODEL_FROM_COLUMN, &s1,
1132                                     TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN, &t1,
1133                                     -1);
1134                 gtk_tree_model_get (tree_model, iter2,
1135                                     TNY_GTK_HEADER_LIST_MODEL_FROM_COLUMN, &s2,
1136                                     TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN, &t2,
1137                                     -1);
1138                 cmp = modest_text_utils_utf8_strcmp (s1, s2, TRUE);
1139                 g_free (s1);
1140                 g_free (s2);
1141                 
1142                 return cmp ? cmp : t1 - t2;
1143                 
1144         case MODEST_HEADER_VIEW_COLUMN_TO: 
1145                 
1146                 gtk_tree_model_get (tree_model, iter1,
1147                                     TNY_GTK_HEADER_LIST_MODEL_TO_COLUMN, &s1,
1148                                     TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN, &t1,
1149                                     -1);
1150                 gtk_tree_model_get (tree_model, iter2,
1151                                     TNY_GTK_HEADER_LIST_MODEL_TO_COLUMN, &s2,
1152                                     TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN, &t2,
1153                                     -1);
1154                 cmp = modest_text_utils_utf8_strcmp (s1, s2, TRUE);
1155                 g_free (s1);
1156                 g_free (s2);
1157                 
1158                 return cmp ? cmp : t1 - t2;
1159
1160         case MODEST_HEADER_VIEW_COLUMN_ATTACH:
1161
1162                 gtk_tree_model_get (tree_model, iter1, TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN, &val1,
1163                                     TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN, &t1, -1);
1164                 gtk_tree_model_get (tree_model, iter2, TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN, &val2,
1165                                     TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN, &t2, -1);
1166                 
1167                 cmp = (val1 & TNY_HEADER_FLAG_ATTACHMENTS) -
1168                         (val2 & TNY_HEADER_FLAG_ATTACHMENTS);
1169
1170                 return cmp ? cmp : t1 - t2;
1171                 
1172         case MODEST_HEADER_VIEW_COLUMN_MSGTYPE:
1173                 gtk_tree_model_get (tree_model, iter1, TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN, &val1,
1174                                     TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN, &t1,-1);
1175                 gtk_tree_model_get (tree_model, iter2, TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN, &val2,
1176                                     TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN, &t2,-1);
1177                 cmp =  (val1 & TNY_HEADER_FLAG_SEEN) - (val2 & TNY_HEADER_FLAG_SEEN);
1178
1179                 return cmp ? cmp : t1 - t2;
1180
1181         case MODEST_HEADER_VIEW_COLUMN_COMPACT_FLAG:
1182                 gtk_tree_model_get (tree_model, iter1, TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN, &val1,
1183                                     TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN, &t1, -1);
1184                 gtk_tree_model_get (tree_model, iter2, TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN, &val2,
1185                                     TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN, &t2, -1);
1186
1187                 int flag_sort = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(user_data), MODEST_HEADER_VIEW_FLAG_SORT));
1188                 switch (flag_sort) {
1189                 case TNY_HEADER_FLAG_ATTACHMENTS:
1190                         cmp = (val1 & TNY_HEADER_FLAG_ATTACHMENTS) -
1191                                 (val2 & TNY_HEADER_FLAG_ATTACHMENTS);
1192                         break;
1193                 case TNY_HEADER_FLAG_PRIORITY:
1194                         cmp = (val1 & TNY_HEADER_FLAG_PRIORITY) -
1195                                 (val2 & TNY_HEADER_FLAG_PRIORITY);
1196                         break;
1197                 default:
1198                         cmp = (val1 & TNY_HEADER_FLAG_PRIORITY) -
1199                                 (val2 & TNY_HEADER_FLAG_PRIORITY);
1200                 }
1201                 
1202                 return cmp ? cmp : t1 - t2;
1203         default:
1204                 return &iter1 - &iter2; /* oughhhh  */
1205         }
1206 }
1207
1208 /* Drag and drop stuff */
1209 static void
1210 drag_data_get_cb (GtkWidget *widget, GdkDragContext *context, 
1211                   GtkSelectionData *selection_data, 
1212                   guint info,  guint time, gpointer data)
1213 {
1214         GtkTreeModel *model;
1215         GtkTreeIter iter;
1216         GtkTreePath *source_row;
1217         
1218         source_row = get_selected_row (GTK_TREE_VIEW(widget), &model);
1219         if ((source_row == NULL) || (!gtk_tree_model_get_iter(model, &iter, source_row))) return;
1220
1221         switch (info) {
1222         case MODEST_HEADER_ROW:
1223                 gtk_tree_set_row_drag_data (selection_data, model, source_row);
1224                 break;
1225         case MODEST_MSG: {
1226                 TnyHeader *hdr;
1227                 gtk_tree_model_get (model, &iter,
1228                                     TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN, &hdr,
1229                                     -1);
1230                 if (hdr) {
1231                         g_object_unref (G_OBJECT(hdr));
1232                 }
1233                 break;
1234         }
1235         default:
1236                 g_message ("default");
1237         }
1238
1239         gtk_tree_path_free (source_row);
1240 }
1241
1242 /* Header view drag types */
1243 const GtkTargetEntry header_view_drag_types[] = {
1244         { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_APP, MODEST_HEADER_ROW },
1245         { "text/uri-list",      0,                   MODEST_MSG },
1246  
1247 };
1248
1249 static void
1250 setup_drag_and_drop (GtkTreeView *self)
1251 {
1252         gtk_drag_source_set (GTK_WIDGET (self),
1253                              GDK_BUTTON1_MASK,
1254                              header_view_drag_types,
1255                              G_N_ELEMENTS (header_view_drag_types),
1256                              GDK_ACTION_MOVE | GDK_ACTION_COPY);
1257
1258         g_signal_connect(G_OBJECT (self), "drag_data_get",
1259                          G_CALLBACK(drag_data_get_cb), NULL);
1260 }
1261
1262 static GtkTreePath *
1263 get_selected_row (GtkTreeView *self, GtkTreeModel **model) 
1264 {
1265         GtkTreePath *path = NULL;
1266         GtkTreeSelection *sel = NULL;   
1267         GList *rows = NULL;
1268
1269         sel   = gtk_tree_view_get_selection(self);
1270         rows = gtk_tree_selection_get_selected_rows (sel, model);
1271         
1272         if ((rows == NULL) || (g_list_length(rows) != 1))
1273                 goto frees;
1274
1275         path = gtk_tree_path_copy(g_list_nth_data (rows, 0));
1276         
1277
1278         /* Free */
1279  frees:
1280         g_list_foreach(rows,(GFunc) gtk_tree_path_free, NULL);
1281         g_list_free(rows);
1282
1283         return path;
1284 }
1285
1286 /*
1287  * This function moves the tree view scroll to the current selected
1288  * row when the widget grabs the focus 
1289  */
1290 static gboolean 
1291 on_focus_in (GtkWidget     *self,
1292              GdkEventFocus *event,
1293              gpointer       user_data)
1294 {
1295         GtkTreeSelection *selection;
1296         GtkTreeModel *model;
1297         GList *selected = NULL;
1298         GtkTreePath *selected_path = NULL;
1299
1300         model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
1301         if (!model)
1302                 return FALSE;
1303
1304         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
1305         /* If none selected yet, pick the first one */
1306         if (gtk_tree_selection_count_selected_rows (selection) == 0) {
1307                 GtkTreeIter iter;
1308                 GtkTreePath *path;
1309
1310                 /* Return if the model is empty */
1311                 if (!gtk_tree_model_get_iter_first (model, &iter))
1312                         return FALSE;
1313
1314                 path = gtk_tree_model_get_path (model, &iter);
1315                 gtk_tree_selection_select_path (selection, path);
1316                 gtk_tree_path_free (path);
1317         }
1318
1319         /* Need to get the all the rows because is selection multiple */
1320         selected = gtk_tree_selection_get_selected_rows (selection, &model);
1321         selected_path = (GtkTreePath *) selected->data;
1322
1323         /* Check if we need to scroll */
1324         #if GTK_CHECK_VERSION(2, 8, 0) /* TODO: gtk_tree_view_get_visible_range() is only available in GTK+ 2.8 */
1325         GtkTreePath *start_path = NULL;
1326         GtkTreePath *end_path = NULL;
1327         if (gtk_tree_view_get_visible_range (GTK_TREE_VIEW (self),
1328                                              &start_path,
1329                                              &end_path)) {
1330
1331                 if ((gtk_tree_path_compare (start_path, selected_path) != -1) ||
1332                     (gtk_tree_path_compare (end_path, selected_path) != 1)) {
1333
1334                         /* Scroll to first path */
1335                         gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (self),
1336                                                       selected_path,
1337                                                       NULL,
1338                                                       TRUE,
1339                                                       0.5,
1340                                                       0.0);
1341                 }
1342         }
1343         #endif /* GTK_CHECK_VERSION */
1344
1345         /* Frees */     
1346         g_list_foreach (selected, (GFunc) gtk_tree_path_free, NULL);
1347         g_list_free (selected);
1348
1349         return FALSE;
1350 }