2007-04-30 Murray Cumming <murrayc@murrayc.com>
[modest] / src / widgets / modest-folder-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 <string.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <tny-account-store-view.h>
34 #include <tny-gtk-account-list-model.h>
35 #include <tny-gtk-folder-store-tree-model.h>
36 #include <tny-gtk-header-list-model.h>
37 #include <tny-folder.h>
38 #include <tny-account-store.h>
39 #include <tny-account.h>
40 #include <tny-folder.h>
41 #include <tny-camel-folder.h>
42 #include <tny-simple-list.h>
43 #include <modest-tny-folder.h>
44 #include <modest-marshal.h>
45 #include <modest-icon-names.h>
46 #include <modest-tny-account-store.h>
47 #include <modest-text-utils.h>
48 #include <modest-runtime.h>
49 #include "modest-folder-view.h"
50 #include <modest-dnd.h>
51 #include <modest-platform.h>
52 #include <modest-account-mgr-helpers.h>
53
54 /* 'private'/'protected' functions */
55 static void modest_folder_view_class_init  (ModestFolderViewClass *klass);
56 static void modest_folder_view_init        (ModestFolderView *obj);
57 static void modest_folder_view_finalize    (GObject *obj);
58
59 static void         tny_account_store_view_init (gpointer g, 
60                                                  gpointer iface_data);
61
62 static void         modest_folder_view_set_account_store (TnyAccountStoreView *self, 
63                                                           TnyAccountStore     *account_store);
64
65 static gboolean     update_model           (ModestFolderView *self,
66                                             ModestTnyAccountStore *account_store);
67
68 static void         on_selection_changed   (GtkTreeSelection *sel, gpointer data);
69
70 static void         on_account_update      (TnyAccountStore *account_store, 
71                                             const gchar *account,
72                                             gpointer user_data);
73
74 static void         on_accounts_reloaded   (TnyAccountStore *store, 
75                                             gpointer user_data);
76
77 static gint         cmp_rows               (GtkTreeModel *tree_model, 
78                                             GtkTreeIter *iter1, 
79                                             GtkTreeIter *iter2,
80                                             gpointer user_data);
81
82 static gboolean     filter_row             (GtkTreeModel *model,
83                                             GtkTreeIter *iter,
84                                             gpointer data);
85
86 static gboolean     on_key_pressed         (GtkWidget *self,
87                                             GdkEventKey *event,
88                                             gpointer user_data);
89
90 static void         on_configuration_key_changed         (ModestConf* conf, 
91                                                           const gchar *key, 
92                                                           ModestConfEvent event, 
93                                                           ModestFolderView *self);
94
95 /* DnD functions */
96 static void         on_drag_data_get       (GtkWidget *widget, 
97                                             GdkDragContext *context, 
98                                             GtkSelectionData *selection_data, 
99                                             guint info, 
100                                             guint time, 
101                                             gpointer data);
102
103 static void         on_drag_data_received  (GtkWidget *widget, 
104                                             GdkDragContext *context, 
105                                             gint x, 
106                                             gint y, 
107                                             GtkSelectionData *selection_data, 
108                                             guint info, 
109                                             guint time, 
110                                             gpointer data);
111
112 static gboolean     on_drag_motion         (GtkWidget      *widget,
113                                             GdkDragContext *context,
114                                             gint            x,
115                                             gint            y,
116                                             guint           time,
117                                             gpointer        user_data);
118
119 static gint         expand_row_timeout     (gpointer data);
120
121 static void         setup_drag_and_drop    (GtkTreeView *self);
122
123 enum {
124         FOLDER_SELECTION_CHANGED_SIGNAL,
125         FOLDER_DISPLAY_NAME_CHANGED_SIGNAL,
126         LAST_SIGNAL
127 };
128
129 typedef struct _ModestFolderViewPrivate ModestFolderViewPrivate;
130 struct _ModestFolderViewPrivate {
131         TnyAccountStore      *account_store;
132         TnyFolderStore       *cur_folder_store;
133
134         gulong                account_update_signal;
135         gulong                changed_signal;
136         gulong                accounts_reloaded_signal;
137         
138         GtkTreeSelection     *cur_selection;
139         TnyFolderStoreQuery  *query;
140         guint                 timer_expander;
141
142         gchar                *local_account_name;
143         ModestFolderViewStyle style;
144 };
145 #define MODEST_FOLDER_VIEW_GET_PRIVATE(o)                       \
146         (G_TYPE_INSTANCE_GET_PRIVATE((o),                       \
147                                      MODEST_TYPE_FOLDER_VIEW,   \
148                                      ModestFolderViewPrivate))
149 /* globals */
150 static GObjectClass *parent_class = NULL;
151
152 static guint signals[LAST_SIGNAL] = {0}; 
153
154 GType
155 modest_folder_view_get_type (void)
156 {
157         static GType my_type = 0;
158         if (!my_type) {
159                 static const GTypeInfo my_info = {
160                         sizeof(ModestFolderViewClass),
161                         NULL,           /* base init */
162                         NULL,           /* base finalize */
163                         (GClassInitFunc) modest_folder_view_class_init,
164                         NULL,           /* class finalize */
165                         NULL,           /* class data */
166                         sizeof(ModestFolderView),
167                         1,              /* n_preallocs */
168                         (GInstanceInitFunc) modest_folder_view_init,
169                         NULL
170                 };
171
172                 static const GInterfaceInfo tny_account_store_view_info = {
173                         (GInterfaceInitFunc) tny_account_store_view_init, /* interface_init */
174                         NULL,         /* interface_finalize */
175                         NULL          /* interface_data */
176                 };
177
178                                 
179                 my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
180                                                   "ModestFolderView",
181                                                   &my_info, 0);
182
183                 g_type_add_interface_static (my_type, 
184                                              TNY_TYPE_ACCOUNT_STORE_VIEW, 
185                                              &tny_account_store_view_info);
186         }
187         return my_type;
188 }
189
190 static void
191 modest_folder_view_class_init (ModestFolderViewClass *klass)
192 {
193         GObjectClass *gobject_class;
194         gobject_class = (GObjectClass*) klass;
195
196         parent_class            = g_type_class_peek_parent (klass);
197         gobject_class->finalize = modest_folder_view_finalize;
198
199         g_type_class_add_private (gobject_class,
200                                   sizeof(ModestFolderViewPrivate));
201         
202         signals[FOLDER_SELECTION_CHANGED_SIGNAL] = 
203                 g_signal_new ("folder_selection_changed",
204                               G_TYPE_FROM_CLASS (gobject_class),
205                               G_SIGNAL_RUN_FIRST,
206                               G_STRUCT_OFFSET (ModestFolderViewClass,
207                                                folder_selection_changed),
208                               NULL, NULL,
209                               modest_marshal_VOID__POINTER_BOOLEAN,
210                               G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_BOOLEAN);
211
212         /*
213          * This signal is emitted whenever the currently selected
214          * folder display name is computed. Note that the name could
215          * be different to the folder name, because we could append
216          * the unread messages count to the folder name to build the
217          * folder display name
218          */
219         signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL] = 
220                 g_signal_new ("folder-display-name-changed",
221                               G_TYPE_FROM_CLASS (gobject_class),
222                               G_SIGNAL_RUN_FIRST,
223                               G_STRUCT_OFFSET (ModestFolderViewClass,
224                                                folder_display_name_changed),
225                               NULL, NULL,
226                               g_cclosure_marshal_VOID__STRING,
227                               G_TYPE_NONE, 1, G_TYPE_STRING);
228 }
229
230
231
232 static void
233 text_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
234                  GtkTreeModel *tree_model,  GtkTreeIter *iter,  gpointer data)
235 {
236         ModestFolderViewPrivate *priv;
237         GObject *rendobj;
238         gchar *fname = NULL;
239         gint unread, all;
240         TnyFolderType type;
241         GObject *instance = NULL;
242         
243         g_return_if_fail (column);
244         g_return_if_fail (tree_model);
245
246         gtk_tree_model_get (tree_model, iter,
247                             TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname,
248                             TNY_GTK_FOLDER_STORE_TREE_MODEL_ALL_COLUMN, &all,
249                             TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN, &unread,
250                             TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
251                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
252                             -1);
253         rendobj = G_OBJECT(renderer);
254  
255         if (!fname)
256                 return;
257
258         if (!instance) {
259                 g_free (fname);
260                 return;
261         }
262
263         
264         priv =  MODEST_FOLDER_VIEW_GET_PRIVATE (data);
265         
266         if (type != TNY_FOLDER_TYPE_ROOT) {
267                 gint number;
268
269                 if (modest_tny_folder_is_local_folder (TNY_FOLDER (instance))) {
270                         TnyFolderType type;
271                         type = modest_tny_folder_get_local_folder_type (TNY_FOLDER (instance));
272                         if (type != TNY_FOLDER_TYPE_UNKNOWN) {
273                                 g_free (fname);
274                                 fname = g_strdup(modest_local_folder_info_get_type_display_name (type));
275                         }
276                 }
277
278                 /* Select the number to show */
279                 if ((type == TNY_FOLDER_TYPE_DRAFTS) || (type == TNY_FOLDER_TYPE_OUTBOX))
280                         number = all;
281                 else
282                         number = unread;
283
284                 /* Use bold font style if there are unread messages */
285                 if (unread > 0) {
286                         gchar *folder_title = g_strdup_printf ("%s (%d)", fname, unread);
287                         g_object_set (rendobj,"text", folder_title,  "weight", 800, NULL);
288                         if (G_OBJECT (priv->cur_folder_store) == instance)
289                                 g_signal_emit (G_OBJECT(data),
290                                                signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
291                                                folder_title);
292                         g_free (folder_title);
293                 } else {
294                         g_object_set (rendobj,"text", fname, "weight", 400, NULL);
295                         if (G_OBJECT (priv->cur_folder_store) == instance)
296                                 g_signal_emit (G_OBJECT(data),
297                                                signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
298                                                fname);
299                 }
300
301         } else {
302                 const gchar *account_name = NULL;
303                 const gchar *account_id = NULL;
304
305                 /* If it's a server account */
306                 account_id = tny_account_get_id (TNY_ACCOUNT (instance));
307                 if (!strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID)) {
308                         account_name = priv->local_account_name;
309                 } else {
310                         if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID)) {
311                                 /* TODO: get MMC card name */
312                         } else {
313                                 account_name = fname;
314                         }
315                 }
316
317                 /* Notify display name observers */
318                 if (G_OBJECT (priv->cur_folder_store) == instance)
319                         g_signal_emit (G_OBJECT(data),
320                                        signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
321                                        account_name);
322
323                 /* Use bold font style */
324                 g_object_set (rendobj,"text", account_name, "weight", 800, NULL);
325         }
326         
327         g_object_unref (G_OBJECT (instance));
328         g_free (fname);
329 }
330
331
332
333 static void
334 icon_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
335                  GtkTreeModel *tree_model,  GtkTreeIter *iter, gpointer data)
336 {
337         GObject *rendobj, *instance;
338         GdkPixbuf *pixbuf;
339         TnyFolderType type;
340         gchar *fname = NULL;
341         const gchar *account_id = NULL;
342         gint unread;
343         
344         rendobj = G_OBJECT(renderer);
345         gtk_tree_model_get (tree_model, iter,
346                             TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
347                             TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname,
348                             TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN, &unread,
349                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
350                             -1);
351
352         if (!fname)
353                 return;
354
355         if (!instance) {
356                 g_free (fname);
357                 return;
358         }
359         
360         if (type == TNY_FOLDER_TYPE_NORMAL || type == TNY_FOLDER_TYPE_UNKNOWN) {
361                 type = modest_tny_folder_guess_folder_type_from_name (fname);
362         }
363
364         switch (type) {
365         case TNY_FOLDER_TYPE_ROOT:
366                 account_id = tny_account_get_id (TNY_ACCOUNT (instance));
367                 if (!strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID)) {
368                         pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_LOCAL_FOLDERS);
369                 } else {
370                         if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID))
371                                 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_MMC);
372                         else
373                                 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_ACCOUNT);
374                 }
375                 break;
376         case TNY_FOLDER_TYPE_INBOX:
377                 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_INBOX);
378                 break;
379         case TNY_FOLDER_TYPE_OUTBOX:
380                 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_OUTBOX);
381                 break;
382         case TNY_FOLDER_TYPE_JUNK:
383                 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_JUNK);
384                 break;
385         case TNY_FOLDER_TYPE_SENT:
386                 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_SENT);
387                 break;
388         case TNY_FOLDER_TYPE_TRASH:
389                 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_TRASH);
390                 break;
391         case TNY_FOLDER_TYPE_DRAFTS:
392                 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_DRAFTS);
393                 break;
394         case TNY_FOLDER_TYPE_NORMAL:
395         default:
396                 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_NORMAL);
397                 break;
398         }
399         g_object_unref (G_OBJECT (instance));
400         g_free (fname);
401
402         /* Set pixbuf */
403         g_object_set (rendobj, "pixbuf", pixbuf, NULL);
404 }
405
406 static void
407 add_columns (GtkWidget *treeview)
408 {
409         GtkTreeViewColumn *column;
410         GtkCellRenderer *renderer;
411         GtkTreeSelection *sel;
412
413         /* Create column */
414         column = gtk_tree_view_column_new ();   
415         
416         /* Set icon and text render function */
417         renderer = gtk_cell_renderer_pixbuf_new();
418         gtk_tree_view_column_pack_start (column, renderer, FALSE);
419         gtk_tree_view_column_set_cell_data_func(column, renderer,
420                                                 icon_cell_data, treeview, NULL);
421         
422         renderer = gtk_cell_renderer_text_new();
423         gtk_tree_view_column_pack_start (column, renderer, FALSE);
424         gtk_tree_view_column_set_cell_data_func(column, renderer,
425                                                 text_cell_data, treeview, NULL);
426         
427         /* Set selection mode */
428         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(treeview));
429         gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
430
431         /* Set treeview appearance */
432         gtk_tree_view_column_set_spacing (column, 2);
433         gtk_tree_view_column_set_resizable (column, TRUE);
434         gtk_tree_view_column_set_fixed_width (column, TRUE);            
435         gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(treeview), FALSE);
436         gtk_tree_view_set_enable_search (GTK_TREE_VIEW(treeview), FALSE);
437
438         /* Add column */
439         gtk_tree_view_append_column (GTK_TREE_VIEW(treeview),column);
440 }
441
442 static void
443 modest_folder_view_init (ModestFolderView *obj)
444 {
445         ModestFolderViewPrivate *priv;
446         ModestConf *conf;
447         
448         priv =  MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
449         
450         priv->timer_expander = 0;
451         priv->account_store  = NULL;
452         priv->cur_folder_store     = NULL;
453         priv->query          = NULL;
454         priv->style          = MODEST_FOLDER_VIEW_STYLE_SHOW_ALL;
455
456         /* Initialize the local account name */
457         conf = modest_runtime_get_conf();
458         priv->local_account_name = modest_conf_get_string (conf, MODEST_CONF_DEVICE_NAME, NULL);
459
460         /* Build treeview */
461         add_columns (GTK_WIDGET (obj));
462
463         /* Setup drag and drop */
464         setup_drag_and_drop (GTK_TREE_VIEW(obj));
465
466         /* Connect signals */
467         g_signal_connect (G_OBJECT (obj), 
468                           "key-press-event", 
469                           G_CALLBACK (on_key_pressed), NULL);
470
471         /*
472          * Track changes in the local account name (in the device it
473          * will be the device name)
474          */
475         g_signal_connect (G_OBJECT(conf), 
476                           "key_changed",
477                           G_CALLBACK(on_configuration_key_changed), obj);
478
479 }
480
481 static void
482 tny_account_store_view_init (gpointer g, gpointer iface_data)
483 {
484         TnyAccountStoreViewIface *klass = (TnyAccountStoreViewIface *)g;
485
486         klass->set_account_store_func = modest_folder_view_set_account_store;
487
488         return;
489 }
490
491 static void
492 modest_folder_view_finalize (GObject *obj)
493 {
494         ModestFolderViewPrivate *priv;
495         GtkTreeSelection    *sel;
496         
497         g_return_if_fail (obj);
498         
499         priv =  MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
500
501         if (priv->timer_expander != 0) {
502                 g_source_remove (priv->timer_expander);
503                 priv->timer_expander = 0;
504         }
505
506         if (priv->account_store) {
507                 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
508                                              priv->account_update_signal);
509                 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
510                                              priv->accounts_reloaded_signal);
511                 g_object_unref (G_OBJECT(priv->account_store));
512                 priv->account_store = NULL;
513         }
514
515         if (priv->query) {
516                 g_object_unref (G_OBJECT (priv->query));
517                 priv->query = NULL;
518         }
519
520         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(obj));
521         if (sel)
522                 g_signal_handler_disconnect (G_OBJECT(sel), priv->changed_signal);
523
524         g_free (priv->local_account_name);
525         
526         G_OBJECT_CLASS(parent_class)->finalize (obj);
527 }
528
529
530 static void
531 modest_folder_view_set_account_store (TnyAccountStoreView *self, TnyAccountStore *account_store)
532 {
533         ModestFolderViewPrivate *priv;
534         TnyDevice *device;
535
536         g_return_if_fail (MODEST_IS_FOLDER_VIEW (self));
537         g_return_if_fail (TNY_IS_ACCOUNT_STORE (account_store));
538
539         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
540         device = tny_account_store_get_device (account_store);
541
542         if (G_UNLIKELY (priv->account_store)) {
543
544                 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store), 
545                                                    priv->account_update_signal))
546                         g_signal_handler_disconnect (G_OBJECT (priv->account_store), 
547                                                      priv->account_update_signal);
548                 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store), 
549                                                    priv->accounts_reloaded_signal))
550                         g_signal_handler_disconnect (G_OBJECT (priv->account_store), 
551                                                      priv->accounts_reloaded_signal);
552
553                 g_object_unref (G_OBJECT (priv->account_store));
554         }
555
556         priv->account_store = g_object_ref (G_OBJECT (account_store));
557
558         priv->account_update_signal = 
559                 g_signal_connect (G_OBJECT(account_store), "account_update",
560                                   G_CALLBACK (on_account_update), self);
561
562         priv->accounts_reloaded_signal = 
563                 g_signal_connect (G_OBJECT(account_store), "accounts_reloaded",
564                                   G_CALLBACK (on_accounts_reloaded), self);
565         
566         if (!update_model (MODEST_FOLDER_VIEW (self),
567                            MODEST_TNY_ACCOUNT_STORE (priv->account_store)))
568                 g_printerr ("modest: failed to update model\n");
569
570         g_object_unref (G_OBJECT (device));
571 }
572
573 static void
574 on_account_update (TnyAccountStore *account_store, const gchar *account,
575                    gpointer user_data)
576 {
577         if (!update_model (MODEST_FOLDER_VIEW(user_data), 
578                            MODEST_TNY_ACCOUNT_STORE(account_store)))
579                 g_printerr ("modest: failed to update model for changes in '%s'",
580                             account);
581 }
582
583 static void 
584 on_accounts_reloaded   (TnyAccountStore *account_store, 
585                         gpointer user_data)
586 {
587         update_model (MODEST_FOLDER_VIEW (user_data), 
588                       MODEST_TNY_ACCOUNT_STORE(account_store));
589 }
590
591 void
592 modest_folder_view_set_title (ModestFolderView *self, const gchar *title)
593 {
594         GtkTreeViewColumn *col;
595         
596         g_return_if_fail (self);
597
598         col = gtk_tree_view_get_column (GTK_TREE_VIEW(self), 0);
599         if (!col) {
600                 g_printerr ("modest: failed get column for title\n");
601                 return;
602         }
603
604         gtk_tree_view_column_set_title (col, title);
605         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self),
606                                            title != NULL);
607 }
608
609 GtkWidget*
610 modest_folder_view_new (TnyFolderStoreQuery *query)
611 {
612         GObject *self;
613         ModestFolderViewPrivate *priv;
614         GtkTreeSelection *sel;
615         
616         self = G_OBJECT (g_object_new (MODEST_TYPE_FOLDER_VIEW, NULL));
617         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
618
619         if (query)
620                 priv->query = g_object_ref (query);
621         
622         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
623         priv->changed_signal = g_signal_connect (sel, "changed",
624                                                  G_CALLBACK (on_selection_changed), self);
625
626         return GTK_WIDGET(self);
627 }
628
629 /* this feels dirty; any other way to expand all the root items? */
630 static void
631 expand_root_items (ModestFolderView *self)
632 {
633         GtkTreePath *path;
634         path = gtk_tree_path_new_first ();
635
636         /* all folders should have child items, so.. */
637         while (gtk_tree_view_expand_row (GTK_TREE_VIEW(self), path, FALSE))
638                 gtk_tree_path_next (path);
639         
640         gtk_tree_path_free (path);
641 }
642
643 /*
644  * We use this function to implement the
645  * MODEST_FOLDER_VIEW_STYLE_SHOW_ONE style. We only show the default
646  * account in this case
647  */
648 static gboolean 
649 filter_row (GtkTreeModel *model,
650             GtkTreeIter *iter,
651             gpointer data)
652 {
653         gboolean retval = TRUE;
654         gint type;
655         GObject *instance = NULL;
656
657         gtk_tree_model_get (model, iter,
658                             TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
659                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
660                             -1);
661
662         if (type == TNY_FOLDER_TYPE_ROOT) {
663                 ModestAccountData *acc_data;
664                 TnyAccount *acc;
665                 const gchar *account_id;
666                 ModestAccountMgr *mgr;
667
668                 acc = TNY_ACCOUNT (instance);
669                 account_id = tny_account_get_id (acc); /* Non human-readable name, not the title */
670                 mgr = modest_runtime_get_account_mgr ();
671                 
672                 /* TODO: This does not work because acc is the _server_account, 
673                  * and this code assumes that it is the normal account, which uses a server account.
674                  * Maybe we need some way to get the parent account from the server account.
675                  */
676                 acc_data = modest_account_mgr_get_account_data (mgr, account_id);
677                 if (acc_data) {
678                         if (strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) &&
679                             strcmp (account_id, MODEST_MMC_ACCOUNT_ID)) {
680                                 /* Show only the default account */
681                                 if (!acc_data->is_default)
682                                         retval = FALSE;
683                         }
684                         modest_account_mgr_free_account_data (mgr, acc_data);
685                 }
686         }
687
688         g_object_unref (instance);
689
690         return retval;
691 }
692
693 static gboolean
694 update_model (ModestFolderView *self, ModestTnyAccountStore *account_store)
695 {
696         ModestFolderViewPrivate *priv;
697         TnyList          *account_list;
698         GtkTreeModel     *model;
699
700         g_return_val_if_fail (account_store, FALSE);
701
702         priv =  MODEST_FOLDER_VIEW_GET_PRIVATE(self);
703         
704         /* Notify that there is no folder selected */
705         g_signal_emit (G_OBJECT(self), 
706                        signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
707                        NULL, TRUE);
708         
709         /* FIXME: the local accounts are not shown when the query
710            selects only the subscribed folders. */
711 /*      model        = tny_gtk_folder_store_tree_model_new (TRUE, priv->query); */
712         model        = tny_gtk_folder_store_tree_model_new (TRUE, NULL);
713         account_list = TNY_LIST(model);
714
715         tny_account_store_get_accounts (TNY_ACCOUNT_STORE(account_store),
716                                         account_list,
717                                         TNY_ACCOUNT_STORE_STORE_ACCOUNTS);      
718         if (account_list) {
719                 GtkTreeModel *filter_model = NULL, *sortable = NULL;
720
721                 sortable = gtk_tree_model_sort_new_with_model (model);
722                 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(sortable),
723                                                       TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, 
724                                                       GTK_SORT_ASCENDING);
725                 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (sortable),
726                                                  TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
727                                                  cmp_rows, NULL, NULL);
728
729                 /* Create filter model */
730                 if (priv->style == MODEST_FOLDER_VIEW_STYLE_SHOW_ONE) {
731                         filter_model = gtk_tree_model_filter_new (sortable, NULL);
732                         gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (filter_model),
733                                                                 filter_row,
734                                                                 NULL,
735                                                                 NULL);
736                 }
737
738                 /* Set new model */
739                 gtk_tree_view_set_model (GTK_TREE_VIEW(self), 
740                                          (filter_model) ? filter_model : sortable);
741                 expand_root_items (self); /* expand all account folders */
742                 g_object_unref (account_list);
743         }
744         
745         return TRUE;
746 }
747
748
749 static void
750 on_selection_changed (GtkTreeSelection *sel, gpointer user_data)
751 {
752         GtkTreeModel            *model;
753         TnyFolderStore          *folder = NULL;
754         GtkTreeIter             iter;
755         ModestFolderView        *tree_view;
756         ModestFolderViewPrivate *priv;
757         gint                    type;
758
759         g_return_if_fail (sel);
760         g_return_if_fail (user_data);
761         
762         priv = MODEST_FOLDER_VIEW_GET_PRIVATE(user_data);
763         priv->cur_selection = sel;
764         
765         /* folder was _un_selected if true */
766         if (!gtk_tree_selection_get_selected (sel, &model, &iter)) {
767                 if (priv->cur_folder_store)
768                         g_object_unref (priv->cur_folder_store);
769                 priv->cur_folder_store = NULL;
770
771                 /* Notify the display name observers */
772                 g_signal_emit (G_OBJECT(user_data),
773                                signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
774                                NULL);
775                 return;
776         }
777
778         tree_view = MODEST_FOLDER_VIEW (user_data);
779
780         gtk_tree_model_get (model, &iter,
781                             TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
782                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder,
783                             -1);
784
785         /* If the folder is the same do not notify */
786         if (priv->cur_folder_store == folder) {
787                 g_object_unref (folder);
788                 return;
789         }
790         
791         /* Current folder was unselected */
792         if (priv->cur_folder_store) {
793                 g_signal_emit (G_OBJECT(tree_view), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
794                                priv->cur_folder_store, FALSE);
795                 g_object_unref (priv->cur_folder_store);
796         }
797
798         /* New current references */
799         priv->cur_folder_store = folder;
800
801         /* New folder has been selected */
802         g_signal_emit (G_OBJECT(tree_view),
803                        signals[FOLDER_SELECTION_CHANGED_SIGNAL],
804                        0, folder, TRUE);
805 }
806
807 TnyFolderStore *
808 modest_folder_view_get_selected (ModestFolderView *self)
809 {
810         ModestFolderViewPrivate *priv;
811
812         g_return_val_if_fail (self, NULL);
813         
814         priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
815         if (priv->cur_folder_store)
816                 g_object_ref (priv->cur_folder_store);
817
818         return priv->cur_folder_store;
819 }
820
821 /*
822  * This function orders the mail accounts following the next rules
823  * 1st - remote accounts
824  * 2nd - local account
825  * 3rd - MMC account
826  */
827 static gint
828 cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2,
829           gpointer user_data)
830 {
831         gint cmp;
832         gchar         *name1, *name2;
833         TnyFolderType type;
834         TnyFolder     *folder1, *folder2;
835
836         gtk_tree_model_get (tree_model, iter1,
837                             TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name1,
838                             TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
839                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder1,
840                             -1);
841         gtk_tree_model_get (tree_model, iter2,
842                             TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name2,
843                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder2,
844                             -1);
845
846         /* Order must be: Remote accounts -> Local account -> MMC account */
847         if (type == TNY_FOLDER_TYPE_ROOT) {
848                 const gchar *account_id = tny_account_get_id (TNY_ACCOUNT (folder1));
849                 const gchar *account_id2 = tny_account_get_id (TNY_ACCOUNT (folder2));
850
851                 if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID))
852                         cmp = +1;
853                 else {
854                         if (!strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID)) {
855                                 if (!strcmp (account_id2, MODEST_MMC_ACCOUNT_ID))
856                                         cmp = -1;
857                                 else
858                                         cmp = +1;
859                         } else {
860                                 if (!strcmp (account_id2, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) ||
861                                     !strcmp (account_id2, MODEST_MMC_ACCOUNT_ID))
862                                         cmp = -1;
863                                 else
864                                         cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
865                         }
866                 }
867         } else 
868                 cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
869         
870         if (folder1)
871                 g_object_unref(G_OBJECT(folder1));
872         if (folder2)
873                 g_object_unref(G_OBJECT(folder2));
874
875         g_free (name1);
876         g_free (name2);
877
878         return cmp;     
879 }
880
881 /*****************************************************************************/
882 /*                        DRAG and DROP stuff                                */
883 /*****************************************************************************/
884
885 /*
886  * This function fills the #GtkSelectionData with the row and the
887  * model that has been dragged. It's called when this widget is a
888  * source for dnd after the event drop happened
889  */
890 static void
891 on_drag_data_get (GtkWidget *widget, 
892                   GdkDragContext *context, 
893                   GtkSelectionData *selection_data, 
894                   guint info, 
895                   guint time, 
896                   gpointer data)
897 {
898         GtkTreeSelection *selection;
899         GtkTreeModel *model_sort, *model;
900         GtkTreeIter iter;
901         GtkTreePath *source_row_sort;
902         GtkTreePath *source_row;
903
904         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
905         gtk_tree_selection_get_selected (selection, &model_sort, &iter);
906         source_row_sort = gtk_tree_model_get_path (model_sort, &iter);
907
908         /* Get the unsorted path and model */
909         model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
910         source_row = gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
911                                                                      source_row_sort);
912
913         gtk_tree_set_row_drag_data (selection_data,
914                                     model,
915                                     source_row);
916
917         gtk_tree_path_free (source_row_sort);
918         gtk_tree_path_free (source_row);
919 }
920
921 typedef struct _DndHelper {
922         gboolean delete_source;
923         GtkTreePath *source_row;
924         GdkDragContext *context;
925         guint time;
926 } DndHelper;
927
928
929 /*
930  * This function is the callback of the
931  * modest_mail_operation_xfer_msgs () and
932  * modest_mail_operation_xfer_folder() calls. We check here if the
933  * message/folder was correctly asynchronously transferred. The reason
934  * to use the same callback is that the code is the same, it only has
935  * to check that the operation went fine and then finalize the drag
936  * and drop action
937  */
938 static void
939 on_progress_changed (ModestMailOperation *mail_op, gpointer user_data)
940 {
941         gboolean success;
942         DndHelper *helper;
943
944         helper = (DndHelper *) user_data;
945
946         if (!modest_mail_operation_is_finished (mail_op))
947                 return;
948
949         if (modest_mail_operation_get_status (mail_op) == 
950             MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
951                 success = TRUE;
952         } else {
953                 success = FALSE;
954         }
955
956         /* Notify the drag source. Never call delete, the monitor will
957            do the job if needed */
958         gtk_drag_finish (helper->context, success, FALSE, helper->time);
959
960         /* Free the helper */
961         gtk_tree_path_free (helper->source_row);        
962         g_slice_free (DndHelper, helper);
963 }
964
965 /*
966  * This function is used by drag_data_received_cb to manage drag and
967  * drop of a header, i.e, and drag from the header view to the folder
968  * view.
969  */
970 static void
971 drag_and_drop_from_header_view (GtkTreeModel *source_model,
972                                 GtkTreeModel *dest_model,
973                                 GtkTreePath  *dest_row,
974                                 DndHelper    *helper)
975 {
976         TnyList *headers;
977         TnyHeader *header;
978         TnyFolder *folder;
979         ModestMailOperation *mail_op;
980         GtkTreeIter source_iter, dest_iter;
981
982         /* Get header */
983         gtk_tree_model_get_iter (source_model, &source_iter, helper->source_row);
984         gtk_tree_model_get (source_model, &source_iter, 
985                             TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN, 
986                             &header, -1);
987
988         /* Get Folder */
989         gtk_tree_model_get_iter (dest_model, &dest_iter, dest_row);
990         gtk_tree_model_get (dest_model, &dest_iter, 
991                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, 
992                             &folder, -1);
993
994         /* Transfer message */
995         mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE);
996
997         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
998                                          mail_op);
999         g_signal_connect (G_OBJECT (mail_op), "progress-changed",
1000                           G_CALLBACK (on_progress_changed), helper);
1001
1002         /* FIXME: I replaced this because the API changed, but D&D
1003            should be reviewed in order to allow multiple drags*/
1004         headers = tny_simple_list_new ();
1005         tny_list_append (headers, G_OBJECT (header));
1006         g_object_unref (header);
1007         modest_mail_operation_xfer_msgs (mail_op, headers, folder, helper->delete_source);
1008
1009         /* Frees */
1010         g_object_unref (G_OBJECT (mail_op));
1011         g_object_unref (G_OBJECT (header));
1012         g_object_unref (G_OBJECT (folder));
1013 }
1014
1015 /*
1016  * This function is used by drag_data_received_cb to manage drag and
1017  * drop of a folder, i.e, and drag from the folder view to the same
1018  * folder view.
1019  */
1020 static void
1021 drag_and_drop_from_folder_view (GtkTreeModel     *source_model,
1022                                 GtkTreeModel     *dest_model,
1023                                 GtkTreePath      *dest_row,
1024                                 GtkSelectionData *selection_data,
1025                                 DndHelper        *helper)
1026 {
1027         ModestMailOperation *mail_op;
1028         GtkTreeIter parent_iter, iter;
1029         TnyFolderStore *parent_folder;
1030         TnyFolder *folder;
1031
1032         /* Check if the drag is possible */
1033         if (!gtk_tree_path_compare (helper->source_row, dest_row) ||
1034             !gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (dest_model),
1035                                                    dest_row,
1036                                                    selection_data)) {
1037
1038                 gtk_drag_finish (helper->context, FALSE, FALSE, helper->time);
1039                 gtk_tree_path_free (helper->source_row);        
1040                 g_slice_free (DndHelper, helper);
1041                 return;
1042         }
1043
1044         /* Get data */
1045         gtk_tree_model_get_iter (source_model, &parent_iter, dest_row);
1046         gtk_tree_model_get_iter (source_model, &iter, helper->source_row);
1047         gtk_tree_model_get (source_model, &parent_iter, 
1048                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, 
1049                             &parent_folder, -1);
1050         gtk_tree_model_get (source_model, &iter,
1051                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
1052                             &folder, -1);
1053
1054         /* Do the mail operation */
1055         mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE);
1056         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
1057                                          mail_op);
1058         g_signal_connect (G_OBJECT (mail_op), "progress-changed",
1059                           G_CALLBACK (on_progress_changed), helper);
1060
1061         modest_mail_operation_xfer_folder (mail_op, 
1062                                            folder, 
1063                                            parent_folder,
1064                                            helper->delete_source);
1065
1066         /* Frees */
1067         g_object_unref (G_OBJECT (parent_folder));
1068         g_object_unref (G_OBJECT (folder));
1069         g_object_unref (G_OBJECT (mail_op));
1070 }
1071
1072 /*
1073  * This function receives the data set by the "drag-data-get" signal
1074  * handler. This information comes within the #GtkSelectionData. This
1075  * function will manage both the drags of folders of the treeview and
1076  * drags of headers of the header view widget.
1077  */
1078 static void 
1079 on_drag_data_received (GtkWidget *widget, 
1080                        GdkDragContext *context, 
1081                        gint x, 
1082                        gint y, 
1083                        GtkSelectionData *selection_data, 
1084                        guint target_type, 
1085                        guint time, 
1086                        gpointer data)
1087 {
1088         GtkWidget *source_widget;
1089         GtkTreeModel *model_sort, *dest_model, *source_model;
1090         GtkTreePath *source_row, *dest_row, *child_dest_row;
1091         GtkTreeViewDropPosition pos;
1092         gboolean success = FALSE, delete_source = FALSE;
1093         DndHelper *helper;
1094
1095         /* Do not allow further process */
1096         g_signal_stop_emission_by_name (widget, "drag-data-received");
1097
1098         /* Get the action */
1099         if (context->action == GDK_ACTION_MOVE)
1100                 delete_source = TRUE;
1101
1102         /* Check if the get_data failed */
1103         if (selection_data == NULL || selection_data->length < 0)
1104                 gtk_drag_finish (context, success, FALSE, time);
1105
1106         /* Get the models */
1107         source_widget = gtk_drag_get_source_widget (context);
1108         gtk_tree_get_row_drag_data (selection_data,
1109                                     &source_model,
1110                                     &source_row);
1111
1112         model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
1113         /* Select the destination model */
1114         if (source_widget == widget) {
1115                 dest_model = source_model;
1116         } else {
1117                 dest_model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
1118         }
1119
1120         /* Get the path to the destination row. Can not call
1121            gtk_tree_view_get_drag_dest_row() because the source row
1122            is not selected anymore */
1123         gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget), x, y,
1124                                            &dest_row, &pos);
1125
1126         /* Only allow drops IN other rows */
1127         if (!dest_row || pos == GTK_TREE_VIEW_DROP_BEFORE || pos == GTK_TREE_VIEW_DROP_AFTER)
1128                 gtk_drag_finish (context, success, FALSE, time);
1129
1130         /* Create the helper */
1131         helper = g_slice_new0 (DndHelper);
1132         helper->delete_source = delete_source;
1133         helper->source_row = gtk_tree_path_copy (source_row);
1134         helper->context = context;
1135         helper->time = time;
1136
1137         /* Get path from the unsorted model */
1138         child_dest_row = 
1139                 gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
1140                                                                 dest_row);
1141         gtk_tree_path_free (dest_row);
1142
1143         /* Drags from the header view */
1144         if (source_widget != widget) {
1145
1146                 drag_and_drop_from_header_view (source_model,
1147                                                 dest_model,
1148                                                 child_dest_row,
1149                                                 helper);
1150         } else {
1151
1152
1153                 drag_and_drop_from_folder_view (source_model,
1154                                                 dest_model,
1155                                                 child_dest_row,
1156                                                 selection_data, 
1157                                                 helper);
1158         }
1159
1160         /* Frees */
1161         gtk_tree_path_free (source_row);
1162         gtk_tree_path_free (child_dest_row);
1163 }
1164
1165 /*
1166  * We define a "drag-drop" signal handler because we do not want to
1167  * use the default one, because the default one always calls
1168  * gtk_drag_finish and we prefer to do it in the "drag-data-received"
1169  * signal handler, because there we have all the information available
1170  * to know if the dnd was a success or not.
1171  */
1172 static gboolean
1173 drag_drop_cb (GtkWidget      *widget,
1174               GdkDragContext *context,
1175               gint            x,
1176               gint            y,
1177               guint           time,
1178               gpointer        user_data) 
1179 {
1180         gpointer target;
1181
1182         if (!context->targets)
1183                 return FALSE;
1184
1185         /* Check if we're dragging a folder row */
1186         target = gtk_drag_dest_find_target (widget, context, NULL);
1187
1188         /* Request the data from the source. */
1189         gtk_drag_get_data(widget, context, target, time);
1190
1191     return TRUE;
1192 }
1193
1194 /*
1195  * This function expands a node of a tree view if it's not expanded
1196  * yet. Not sure why it needs the threads stuff, but gtk+`example code
1197  * does that, so that's why they're here.
1198  */
1199 static gint
1200 expand_row_timeout (gpointer data)
1201 {
1202         GtkTreeView *tree_view = data;
1203         GtkTreePath *dest_path = NULL;
1204         GtkTreeViewDropPosition pos;
1205         gboolean result = FALSE;
1206         
1207         GDK_THREADS_ENTER ();
1208         
1209         gtk_tree_view_get_drag_dest_row (tree_view,
1210                                          &dest_path,
1211                                          &pos);
1212         
1213         if (dest_path &&
1214             (pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER ||
1215              pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)) {
1216                 gtk_tree_view_expand_row (tree_view, dest_path, FALSE);
1217                 gtk_tree_path_free (dest_path);
1218         }
1219         else {
1220                 if (dest_path)
1221                         gtk_tree_path_free (dest_path);
1222                 
1223                 result = TRUE;
1224         }
1225         
1226         GDK_THREADS_LEAVE ();
1227
1228         return result;
1229 }
1230
1231 /*
1232  * This function is called whenever the pointer is moved over a widget
1233  * while dragging some data. It installs a timeout that will expand a
1234  * node of the treeview if not expanded yet. This function also calls
1235  * gdk_drag_status in order to set the suggested action that will be
1236  * used by the "drag-data-received" signal handler to know if we
1237  * should do a move or just a copy of the data.
1238  */
1239 static gboolean
1240 on_drag_motion (GtkWidget      *widget,
1241                 GdkDragContext *context,
1242                 gint            x,
1243                 gint            y,
1244                 guint           time,
1245                 gpointer        user_data)  
1246 {
1247         GtkTreeViewDropPosition pos;
1248         GtkTreePath *dest_row;
1249         ModestFolderViewPrivate *priv;
1250         GdkDragAction suggested_action;
1251         gboolean valid_location = FALSE;
1252
1253         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (widget);
1254
1255         if (priv->timer_expander != 0) {
1256                 g_source_remove (priv->timer_expander);
1257                 priv->timer_expander = 0;
1258         }
1259
1260         gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
1261                                            x, y,
1262                                            &dest_row,
1263                                            &pos);
1264
1265         /* Do not allow drops between folders */
1266         if (!dest_row ||
1267             pos == GTK_TREE_VIEW_DROP_BEFORE || 
1268             pos == GTK_TREE_VIEW_DROP_AFTER) {
1269                 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW (widget), NULL, 0);
1270                 gdk_drag_status(context, 0, time);
1271                 valid_location = FALSE;
1272                 goto out;
1273         } else {
1274                 valid_location = TRUE;
1275         }
1276
1277         /* Expand the selected row after 1/2 second */
1278         if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), dest_row)) {
1279                 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), dest_row, pos);
1280                 priv->timer_expander = g_timeout_add (500, expand_row_timeout, widget);
1281         }
1282         gtk_tree_path_free (dest_row);
1283
1284         /* Select the desired action. By default we pick MOVE */
1285         suggested_action = GDK_ACTION_MOVE;
1286
1287         if (context->actions == GDK_ACTION_COPY)
1288             gdk_drag_status(context, GDK_ACTION_COPY, time);
1289         else if (context->actions == GDK_ACTION_MOVE)
1290             gdk_drag_status(context, GDK_ACTION_MOVE, time);
1291         else if (context->actions & suggested_action)
1292             gdk_drag_status(context, suggested_action, time);
1293         else
1294             gdk_drag_status(context, GDK_ACTION_DEFAULT, time);
1295
1296  out:
1297         g_signal_stop_emission_by_name (widget, "drag-motion");
1298         return valid_location;
1299 }
1300
1301
1302 /* Folder view drag types */
1303 const GtkTargetEntry folder_view_drag_types[] =
1304 {
1305         { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, MODEST_FOLDER_ROW },
1306         { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_APP,    MODEST_HEADER_ROW }
1307 };
1308
1309 /*
1310  * This function sets the treeview as a source and a target for dnd
1311  * events. It also connects all the requirede signals.
1312  */
1313 static void
1314 setup_drag_and_drop (GtkTreeView *self)
1315 {
1316         /* Set up the folder view as a dnd destination. Set only the
1317            highlight flag, otherwise gtk will have a different
1318            behaviour */
1319         gtk_drag_dest_set (GTK_WIDGET (self),
1320                            GTK_DEST_DEFAULT_HIGHLIGHT,
1321                            folder_view_drag_types,
1322                            G_N_ELEMENTS (folder_view_drag_types),
1323                            GDK_ACTION_MOVE | GDK_ACTION_COPY);
1324
1325         g_signal_connect (G_OBJECT (self),
1326                           "drag_data_received",
1327                           G_CALLBACK (on_drag_data_received),
1328                           NULL);
1329
1330
1331         /* Set up the treeview as a dnd source */
1332         gtk_drag_source_set (GTK_WIDGET (self),
1333                              GDK_BUTTON1_MASK,
1334                              folder_view_drag_types,
1335                              G_N_ELEMENTS (folder_view_drag_types),
1336                              GDK_ACTION_MOVE | GDK_ACTION_COPY);
1337
1338         g_signal_connect (G_OBJECT (self),
1339                           "drag_motion",
1340                           G_CALLBACK (on_drag_motion),
1341                           NULL);
1342         
1343         g_signal_connect (G_OBJECT (self),
1344                           "drag_data_get",
1345                           G_CALLBACK (on_drag_data_get),
1346                           NULL);
1347
1348         g_signal_connect (G_OBJECT (self),
1349                           "drag_drop",
1350                           G_CALLBACK (drag_drop_cb),
1351                           NULL);
1352 }
1353
1354 /*
1355  * This function manages the navigation through the folders using the
1356  * keyboard or the hardware keys in the device
1357  */
1358 static gboolean
1359 on_key_pressed (GtkWidget *self,
1360                 GdkEventKey *event,
1361                 gpointer user_data)
1362 {
1363         GtkTreeSelection *selection;
1364         GtkTreeIter iter;
1365         GtkTreeModel *model;
1366         gboolean retval = FALSE;
1367
1368         /* Up and Down are automatically managed by the treeview */
1369         if (event->keyval == GDK_Return) {
1370                 /* Expand/Collapse the selected row */
1371                 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
1372                 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
1373                         GtkTreePath *path;
1374
1375                         path = gtk_tree_model_get_path (model, &iter);
1376
1377                         if (gtk_tree_view_row_expanded (GTK_TREE_VIEW (self), path))
1378                                 gtk_tree_view_collapse_row (GTK_TREE_VIEW (self), path);
1379                         else
1380                                 gtk_tree_view_expand_row (GTK_TREE_VIEW (self), path, FALSE);
1381                         gtk_tree_path_free (path);
1382                 }
1383                 /* No further processing */
1384                 retval = TRUE;
1385         }
1386
1387         return retval;
1388 }
1389
1390 /*
1391  * We listen to the changes in the local folder account name key,
1392  * because we want to show the right name in the view. The local
1393  * folder account name corresponds to the device name in the Maemo
1394  * version. We do this because we do not want to query gconf on each
1395  * tree view refresh. It's better to cache it and change whenever
1396  * necessary.
1397  */
1398 static void 
1399 on_configuration_key_changed (ModestConf* conf, 
1400                               const gchar *key, 
1401                               ModestConfEvent event, 
1402                               ModestFolderView *self)
1403 {
1404         ModestFolderViewPrivate *priv;
1405
1406         if (!key || strcmp (key, MODEST_CONF_DEVICE_NAME))
1407                 return;
1408
1409         priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
1410
1411         g_free (priv->local_account_name);
1412
1413         if (event == MODEST_CONF_EVENT_KEY_UNSET)
1414                 priv->local_account_name = g_strdup (MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME);
1415         else
1416                 priv->local_account_name = modest_conf_get_string (modest_runtime_get_conf(),
1417                                                                    MODEST_CONF_DEVICE_NAME, NULL);
1418
1419         /* Force a redraw */
1420 #if GTK_CHECK_VERSION(2, 8, 0) /* gtk_tree_view_column_queue_resize is only available in GTK+ 2.8 */
1421         GtkTreeViewColumn * tree_column = gtk_tree_view_get_column (GTK_TREE_VIEW (self), 
1422                                                 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN);
1423         gtk_tree_view_column_queue_resize (tree_column);
1424 #endif
1425 }
1426
1427 void 
1428 modest_folder_view_set_style (ModestFolderView *self,
1429                               ModestFolderViewStyle style)
1430 {
1431         ModestFolderViewPrivate *priv;
1432
1433         g_return_if_fail (self);
1434         
1435         priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
1436
1437         priv->style = style;
1438 }