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