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