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