* Rewritten main window title handling
[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
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         /* local_folders should be the last one */
764         if (type == TNY_FOLDER_TYPE_ROOT) {
765                 /* the account name is also the name of the root folder
766                  * in case of local folders */
767                 if (name1 && strcmp (name1, MODEST_LOCAL_FOLDERS_ACCOUNT_NAME) == 0)
768                         cmp = +1;
769                 else if (name2 && strcmp (name2, MODEST_LOCAL_FOLDERS_ACCOUNT_NAME) == 0)
770                         cmp = -1;
771                 else 
772                         cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
773         } else 
774                 cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
775
776         
777         if (folder1)
778                 g_object_unref(G_OBJECT(folder1));
779         if (folder2)
780                 g_object_unref(G_OBJECT(folder2));
781         
782         g_free (name1);
783         g_free (name2);
784
785         return cmp;     
786 }
787
788 /*****************************************************************************/
789 /*                        DRAG and DROP stuff                                */
790 /*****************************************************************************/
791
792 /*
793  * This function fills the #GtkSelectionData with the row and the
794  * model that has been dragged. It's called when this widget is a
795  * source for dnd after the event drop happened
796  */
797 static void
798 on_drag_data_get (GtkWidget *widget, 
799                   GdkDragContext *context, 
800                   GtkSelectionData *selection_data, 
801                   guint info, 
802                   guint time, 
803                   gpointer data)
804 {
805         GtkTreeSelection *selection;
806         GtkTreeModel *model_sort, *model;
807         GtkTreeIter iter;
808         GtkTreePath *source_row_sort;
809         GtkTreePath *source_row;
810
811         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
812         gtk_tree_selection_get_selected (selection, &model_sort, &iter);
813         source_row_sort = gtk_tree_model_get_path (model_sort, &iter);
814
815         /* Get the unsorted path and model */
816         model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
817         source_row = gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
818                                                                      source_row_sort);
819
820         gtk_tree_set_row_drag_data (selection_data,
821                                     model,
822                                     source_row);
823
824         gtk_tree_path_free (source_row_sort);
825         gtk_tree_path_free (source_row);
826 }
827
828 typedef struct _DndHelper {
829         gboolean delete_source;
830         GtkTreePath *source_row;
831         GdkDragContext *context;
832         guint time;
833 } DndHelper;
834
835
836 /*
837  * This function is the callback of the
838  * modest_mail_operation_xfer_msg() and
839  * modest_mail_operation_xfer_folder() calls. We check here if the
840  * message/folder was correctly asynchronously transferred. The reason
841  * to use the same callback is that the code is the same, it only has
842  * to check that the operation went fine and then finalize the drag
843  * and drop action
844  */
845 static void
846 on_progress_changed (ModestMailOperation *mail_op, gpointer user_data)
847 {
848         gboolean success;
849         DndHelper *helper;
850
851         helper = (DndHelper *) user_data;
852
853         if (!modest_mail_operation_is_finished (mail_op))
854                 return;
855
856         if (modest_mail_operation_get_status (mail_op) == 
857             MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
858                 success = TRUE;
859         } else {
860                 success = FALSE;
861         }
862
863         /* Notify the drag source. Never call delete, the monitor will
864            do the job if needed */
865         gtk_drag_finish (helper->context, success, FALSE, helper->time);
866
867         /* Free the helper */
868         gtk_tree_path_free (helper->source_row);        
869         g_slice_free (DndHelper, helper);
870 }
871
872 /*
873  * This function is used by drag_data_received_cb to manage drag and
874  * drop of a header, i.e, and drag from the header view to the folder
875  * view.
876  */
877 static void
878 drag_and_drop_from_header_view (GtkTreeModel *source_model,
879                                 GtkTreeModel *dest_model,
880                                 GtkTreePath  *dest_row,
881                                 DndHelper    *helper)
882 {
883         TnyHeader *header;
884         TnyFolder *folder;
885         ModestMailOperation *mail_op;
886         GtkTreeIter source_iter, dest_iter;
887
888         /* Get header */
889         gtk_tree_model_get_iter (source_model, &source_iter, helper->source_row);
890         gtk_tree_model_get (source_model, &source_iter, 
891                             TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN, 
892                             &header, -1);
893
894         /* Get Folder */
895         gtk_tree_model_get_iter (dest_model, &dest_iter, dest_row);
896         gtk_tree_model_get (dest_model, &dest_iter, 
897                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, 
898                             &folder, -1);
899
900         /* Transfer message */
901         mail_op = modest_mail_operation_new ();
902         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
903                                          mail_op);
904         g_signal_connect (G_OBJECT (mail_op), "progress-changed",
905                           G_CALLBACK (on_progress_changed), helper);
906
907         modest_mail_operation_xfer_msg (mail_op, header, folder, helper->delete_source);
908
909         /* Frees */
910         g_object_unref (G_OBJECT (mail_op));
911         g_object_unref (G_OBJECT (header));
912         g_object_unref (G_OBJECT (folder));
913 }
914
915 /*
916  * This function is used by drag_data_received_cb to manage drag and
917  * drop of a folder, i.e, and drag from the folder view to the same
918  * folder view.
919  */
920 static void
921 drag_and_drop_from_folder_view (GtkTreeModel     *source_model,
922                                 GtkTreeModel     *dest_model,
923                                 GtkTreePath      *dest_row,
924                                 GtkSelectionData *selection_data,
925                                 DndHelper        *helper)
926 {
927         ModestMailOperation *mail_op;
928         GtkTreeIter parent_iter, iter;
929         TnyFolderStore *parent_folder;
930         TnyFolder *folder;
931
932         /* Check if the drag is possible */
933         if (!gtk_tree_path_compare (helper->source_row, dest_row) ||
934             !gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (dest_model),
935                                                    dest_row,
936                                                    selection_data)) {
937
938                 gtk_drag_finish (helper->context, FALSE, FALSE, helper->time);
939                 gtk_tree_path_free (helper->source_row);        
940                 g_slice_free (DndHelper, helper);
941                 return;
942         }
943
944         /* Get data */
945         gtk_tree_model_get_iter (source_model, &parent_iter, dest_row);
946         gtk_tree_model_get_iter (source_model, &iter, helper->source_row);
947         gtk_tree_model_get (source_model, &parent_iter, 
948                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, 
949                             &parent_folder, -1);
950         gtk_tree_model_get (source_model, &iter,
951                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
952                             &folder, -1);
953
954         /* Do the mail operation */
955         mail_op = modest_mail_operation_new ();
956         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
957                                          mail_op);
958         g_signal_connect (G_OBJECT (mail_op), "progress-changed",
959                           G_CALLBACK (on_progress_changed), helper);
960
961         modest_mail_operation_xfer_folder (mail_op, 
962                                            folder, 
963                                            parent_folder,
964                                            helper->delete_source);
965
966         /* Frees */
967         g_object_unref (G_OBJECT (parent_folder));
968         g_object_unref (G_OBJECT (folder));
969         g_object_unref (G_OBJECT (mail_op));
970 }
971
972 /*
973  * This function receives the data set by the "drag-data-get" signal
974  * handler. This information comes within the #GtkSelectionData. This
975  * function will manage both the drags of folders of the treeview and
976  * drags of headers of the header view widget.
977  */
978 static void 
979 on_drag_data_received (GtkWidget *widget, 
980                        GdkDragContext *context, 
981                        gint x, 
982                        gint y, 
983                        GtkSelectionData *selection_data, 
984                        guint target_type, 
985                        guint time, 
986                        gpointer data)
987 {
988         GtkWidget *source_widget;
989         GtkTreeModel *model_sort, *dest_model, *source_model;
990         GtkTreePath *source_row, *dest_row, *child_dest_row;
991         GtkTreeViewDropPosition pos;
992         gboolean success = FALSE, delete_source = FALSE;
993         DndHelper *helper;
994
995         /* Do not allow further process */
996         g_signal_stop_emission_by_name (widget, "drag-data-received");
997
998         /* Get the action */
999         if (context->action == GDK_ACTION_MOVE)
1000                 delete_source = TRUE;
1001
1002         /* Check if the get_data failed */
1003         if (selection_data == NULL || selection_data->length < 0)
1004                 gtk_drag_finish (context, success, FALSE, time);
1005
1006         /* Get the models */
1007         source_widget = gtk_drag_get_source_widget (context);
1008         gtk_tree_get_row_drag_data (selection_data,
1009                                     &source_model,
1010                                     &source_row);
1011
1012         model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
1013         /* Select the destination model */
1014         if (source_widget == widget) {
1015                 dest_model = source_model;
1016         } else {
1017                 dest_model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
1018         }
1019
1020         /* Get the path to the destination row. Can not call
1021            gtk_tree_view_get_drag_dest_row() because the source row
1022            is not selected anymore */
1023         gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget), x, y,
1024                                            &dest_row, &pos);
1025
1026         /* Only allow drops IN other rows */
1027         if (!dest_row || pos == GTK_TREE_VIEW_DROP_BEFORE || pos == GTK_TREE_VIEW_DROP_AFTER)
1028                 gtk_drag_finish (context, success, FALSE, time);
1029
1030         /* Create the helper */
1031         helper = g_slice_new0 (DndHelper);
1032         helper->delete_source = delete_source;
1033         helper->source_row = gtk_tree_path_copy (source_row);
1034         helper->context = context;
1035         helper->time = time;
1036
1037         /* Get path from the unsorted model */
1038         child_dest_row = 
1039                 gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
1040                                                                 dest_row);
1041         gtk_tree_path_free (dest_row);
1042
1043         /* Drags from the header view */
1044         if (source_widget != widget) {
1045
1046                 drag_and_drop_from_header_view (source_model,
1047                                                 dest_model,
1048                                                 child_dest_row,
1049                                                 helper);
1050         } else {
1051
1052
1053                 drag_and_drop_from_folder_view (source_model,
1054                                                 dest_model,
1055                                                 child_dest_row,
1056                                                 selection_data, 
1057                                                 helper);
1058         }
1059
1060         /* Frees */
1061         gtk_tree_path_free (source_row);
1062         gtk_tree_path_free (child_dest_row);
1063 }
1064
1065 /*
1066  * We define a "drag-drop" signal handler because we do not want to
1067  * use the default one, because the default one always calls
1068  * gtk_drag_finish and we prefer to do it in the "drag-data-received"
1069  * signal handler, because there we have all the information available
1070  * to know if the dnd was a success or not.
1071  */
1072 static gboolean
1073 drag_drop_cb (GtkWidget      *widget,
1074               GdkDragContext *context,
1075               gint            x,
1076               gint            y,
1077               guint           time,
1078               gpointer        user_data) 
1079 {
1080         gpointer target;
1081
1082         if (!context->targets)
1083                 return FALSE;
1084
1085         /* Check if we're dragging a folder row */
1086         target = gtk_drag_dest_find_target (widget, context, NULL);
1087
1088         /* Request the data from the source. */
1089         gtk_drag_get_data(widget, context, target, time);
1090
1091     return TRUE;
1092 }
1093
1094 /*
1095  * This function expands a node of a tree view if it's not expanded
1096  * yet. Not sure why it needs the threads stuff, but gtk+`example code
1097  * does that, so that's why they're here.
1098  */
1099 static gint
1100 expand_row_timeout (gpointer data)
1101 {
1102         GtkTreeView *tree_view = data;
1103         GtkTreePath *dest_path = NULL;
1104         GtkTreeViewDropPosition pos;
1105         gboolean result = FALSE;
1106         
1107         GDK_THREADS_ENTER ();
1108         
1109         gtk_tree_view_get_drag_dest_row (tree_view,
1110                                          &dest_path,
1111                                          &pos);
1112         
1113         if (dest_path &&
1114             (pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER ||
1115              pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)) {
1116                 gtk_tree_view_expand_row (tree_view, dest_path, FALSE);
1117                 gtk_tree_path_free (dest_path);
1118         }
1119         else {
1120                 if (dest_path)
1121                         gtk_tree_path_free (dest_path);
1122                 
1123                 result = TRUE;
1124         }
1125         
1126         GDK_THREADS_LEAVE ();
1127
1128         return result;
1129 }
1130
1131 /*
1132  * This function is called whenever the pointer is moved over a widget
1133  * while dragging some data. It installs a timeout that will expand a
1134  * node of the treeview if not expanded yet. This function also calls
1135  * gdk_drag_status in order to set the suggested action that will be
1136  * used by the "drag-data-received" signal handler to know if we
1137  * should do a move or just a copy of the data.
1138  */
1139 static gboolean
1140 on_drag_motion (GtkWidget      *widget,
1141                 GdkDragContext *context,
1142                 gint            x,
1143                 gint            y,
1144                 guint           time,
1145                 gpointer        user_data)  
1146 {
1147         GtkTreeViewDropPosition pos;
1148         GtkTreePath *dest_row;
1149         ModestFolderViewPrivate *priv;
1150         GdkDragAction suggested_action;
1151         gboolean valid_location = FALSE;
1152
1153         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (widget);
1154
1155         if (priv->timer_expander != 0) {
1156                 g_source_remove (priv->timer_expander);
1157                 priv->timer_expander = 0;
1158         }
1159
1160         gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
1161                                            x, y,
1162                                            &dest_row,
1163                                            &pos);
1164
1165         /* Do not allow drops between folders */
1166         if (!dest_row ||
1167             pos == GTK_TREE_VIEW_DROP_BEFORE || 
1168             pos == GTK_TREE_VIEW_DROP_AFTER) {
1169                 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW (widget), NULL, 0);
1170                 gdk_drag_status(context, 0, time);
1171                 valid_location = FALSE;
1172                 goto out;
1173         } else {
1174                 valid_location = TRUE;
1175         }
1176
1177         /* Expand the selected row after 1/2 second */
1178         if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), dest_row)) {
1179                 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), dest_row, pos);
1180                 priv->timer_expander = g_timeout_add (500, expand_row_timeout, widget);
1181         }
1182         gtk_tree_path_free (dest_row);
1183
1184         /* Select the desired action. By default we pick MOVE */
1185         suggested_action = GDK_ACTION_MOVE;
1186
1187         if (context->actions == GDK_ACTION_COPY)
1188             gdk_drag_status(context, GDK_ACTION_COPY, time);
1189         else if (context->actions == GDK_ACTION_MOVE)
1190             gdk_drag_status(context, GDK_ACTION_MOVE, time);
1191         else if (context->actions & suggested_action)
1192             gdk_drag_status(context, suggested_action, time);
1193         else
1194             gdk_drag_status(context, GDK_ACTION_DEFAULT, time);
1195
1196  out:
1197         g_signal_stop_emission_by_name (widget, "drag-motion");
1198         return valid_location;
1199 }
1200
1201
1202 /* Folder view drag types */
1203 const GtkTargetEntry folder_view_drag_types[] =
1204 {
1205         { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, MODEST_FOLDER_ROW },
1206         { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_APP,    MODEST_HEADER_ROW }
1207 };
1208
1209 /*
1210  * This function sets the treeview as a source and a target for dnd
1211  * events. It also connects all the requirede signals.
1212  */
1213 static void
1214 setup_drag_and_drop (GtkTreeView *self)
1215 {
1216         /* Set up the folder view as a dnd destination. Set only the
1217            highlight flag, otherwise gtk will have a different
1218            behaviour */
1219         gtk_drag_dest_set (GTK_WIDGET (self),
1220                            GTK_DEST_DEFAULT_HIGHLIGHT,
1221                            folder_view_drag_types,
1222                            G_N_ELEMENTS (folder_view_drag_types),
1223                            GDK_ACTION_MOVE | GDK_ACTION_COPY);
1224
1225         g_signal_connect (G_OBJECT (self),
1226                           "drag_data_received",
1227                           G_CALLBACK (on_drag_data_received),
1228                           NULL);
1229
1230
1231         /* Set up the treeview as a dnd source */
1232         gtk_drag_source_set (GTK_WIDGET (self),
1233                              GDK_BUTTON1_MASK,
1234                              folder_view_drag_types,
1235                              G_N_ELEMENTS (folder_view_drag_types),
1236                              GDK_ACTION_MOVE | GDK_ACTION_COPY);
1237
1238         g_signal_connect (G_OBJECT (self),
1239                           "drag_motion",
1240                           G_CALLBACK (on_drag_motion),
1241                           NULL);
1242         
1243         g_signal_connect (G_OBJECT (self),
1244                           "drag_data_get",
1245                           G_CALLBACK (on_drag_data_get),
1246                           NULL);
1247
1248         g_signal_connect (G_OBJECT (self),
1249                           "drag_drop",
1250                           G_CALLBACK (drag_drop_cb),
1251                           NULL);
1252 }
1253
1254 /*
1255  * This function manages the navigation through the folders using the
1256  * keyboard or the hardware keys in the device
1257  */
1258 static gboolean
1259 on_key_pressed (GtkWidget *self,
1260                 GdkEventKey *event,
1261                 gpointer user_data)
1262 {
1263         GtkTreeSelection *selection;
1264         GtkTreeIter iter;
1265         GtkTreeModel *model;
1266         gboolean retval = FALSE;
1267
1268         /* Up and Down are automatically managed by the treeview */
1269         if (event->keyval == GDK_Return) {
1270                 /* Expand/Collapse the selected row */
1271                 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
1272                 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
1273                         GtkTreePath *path;
1274
1275                         path = gtk_tree_model_get_path (model, &iter);
1276
1277                         if (gtk_tree_view_row_expanded (GTK_TREE_VIEW (self), path))
1278                                 gtk_tree_view_collapse_row (GTK_TREE_VIEW (self), path);
1279                         else
1280                                 gtk_tree_view_expand_row (GTK_TREE_VIEW (self), path, FALSE);
1281                         gtk_tree_path_free (path);
1282                 }
1283                 /* No further processing */
1284                 retval = TRUE;
1285         }
1286
1287         return retval;
1288 }
1289
1290 /*
1291  * We listen to the changes in the local folder account name key,
1292  * because we want to show the right name in the view. The local
1293  * folder account name corresponds to the device name in the Maemo
1294  * version. We do this because we do not want to query gconf on each
1295  * tree view refresh. It's better to cache it and change whenever
1296  * necessary.
1297  */
1298 static void 
1299 on_configuration_key_changed (ModestConf* conf, 
1300                               const gchar *key, 
1301                               ModestConfEvent event, 
1302                               ModestFolderView *self)
1303 {
1304         ModestFolderViewPrivate *priv;
1305
1306         if (!key || strcmp (key, MODEST_CONF_DEVICE_NAME))
1307                 return;
1308
1309         priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
1310
1311         g_free (priv->local_account_name);
1312
1313         if (event == MODEST_CONF_EVENT_KEY_UNSET)
1314                 priv->local_account_name = g_strdup (MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME);
1315         else
1316                 priv->local_account_name = modest_conf_get_string (modest_runtime_get_conf(),
1317                                                                    MODEST_CONF_DEVICE_NAME, NULL);
1318
1319         /* Force a redraw */
1320 #if GTK_CHECK_VERSION(2, 8, 0) /* gtk_tree_view_column_queue_resize is only available in GTK+ 2.8 */
1321         GtkTreeViewColumn * tree_column = gtk_tree_view_get_column (GTK_TREE_VIEW (self), 
1322                                                 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN);
1323         gtk_tree_view_column_queue_resize (tree_column);
1324 #endif
1325 }