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