* Added ModestFolderViewStyle
[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 modest_folder_view_init (ModestFolderView *obj)
407 {
408         ModestFolderViewPrivate *priv;
409         GtkTreeViewColumn *column;
410         GtkCellRenderer *renderer;
411         GtkTreeSelection *sel;
412         ModestConf *conf;
413         
414         priv =  MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
415         
416         priv->timer_expander = 0;
417         priv->account_store  = NULL;
418         priv->cur_folder_store     = NULL;
419         priv->query          = NULL;
420         priv->style          = MODEST_FOLDER_VIEW_STYLE_SHOW_ALL;
421
422         /* Initialize the local account name */
423         conf = modest_runtime_get_conf();
424         priv->local_account_name = modest_conf_get_string (conf, MODEST_CONF_DEVICE_NAME, NULL);
425
426         /* Build treeview */
427         column = gtk_tree_view_column_new ();   
428         
429         renderer = gtk_cell_renderer_pixbuf_new();
430         gtk_tree_view_column_pack_start (column, renderer, FALSE);
431         gtk_tree_view_column_set_cell_data_func(column, renderer,
432                                                 icon_cell_data, obj, NULL);
433         
434         renderer = gtk_cell_renderer_text_new();
435         gtk_tree_view_column_pack_start (column, renderer, FALSE);
436         gtk_tree_view_column_set_cell_data_func(column, renderer,
437                                                 text_cell_data, obj, NULL);
438         
439         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(obj));
440         gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
441
442         gtk_tree_view_column_set_spacing (column, 2);
443         gtk_tree_view_column_set_resizable (column, TRUE);
444         gtk_tree_view_column_set_fixed_width (column, TRUE);            
445         gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(obj), FALSE);
446         gtk_tree_view_set_enable_search     (GTK_TREE_VIEW(obj), FALSE);
447
448         gtk_tree_view_append_column (GTK_TREE_VIEW(obj),column);
449
450         /* Setup drag and drop */
451         setup_drag_and_drop (GTK_TREE_VIEW(obj));
452
453         /* Connect signals */
454         g_signal_connect (G_OBJECT (obj), 
455                           "key-press-event", 
456                           G_CALLBACK (on_key_pressed), NULL);
457
458         /*
459          * Track changes in the local account name (in the device it
460          * will be the device name)
461          */
462         g_signal_connect (G_OBJECT(conf), 
463                           "key_changed",
464                           G_CALLBACK(on_configuration_key_changed), obj);
465
466 }
467
468 static void
469 tny_account_store_view_init (gpointer g, gpointer iface_data)
470 {
471         TnyAccountStoreViewIface *klass = (TnyAccountStoreViewIface *)g;
472
473         klass->set_account_store_func = modest_folder_view_set_account_store;
474
475         return;
476 }
477
478 static void
479 modest_folder_view_finalize (GObject *obj)
480 {
481         ModestFolderViewPrivate *priv;
482         GtkTreeSelection    *sel;
483         
484         g_return_if_fail (obj);
485         
486         priv =  MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
487
488         if (priv->timer_expander != 0) {
489                 g_source_remove (priv->timer_expander);
490                 priv->timer_expander = 0;
491         }
492
493         if (priv->account_store) {
494                 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
495                                              priv->account_update_signal);
496                 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
497                                              priv->accounts_reloaded_signal);
498                 g_object_unref (G_OBJECT(priv->account_store));
499                 priv->account_store = NULL;
500         }
501
502         if (priv->query) {
503                 g_object_unref (G_OBJECT (priv->query));
504                 priv->query = NULL;
505         }
506
507         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(obj));
508         if (sel)
509                 g_signal_handler_disconnect (G_OBJECT(sel), priv->changed_signal);
510
511         g_free (priv->local_account_name);
512         
513         G_OBJECT_CLASS(parent_class)->finalize (obj);
514 }
515
516
517 static void
518 modest_folder_view_set_account_store (TnyAccountStoreView *self, TnyAccountStore *account_store)
519 {
520         ModestFolderViewPrivate *priv;
521         TnyDevice *device;
522
523         g_return_if_fail (MODEST_IS_FOLDER_VIEW (self));
524         g_return_if_fail (TNY_IS_ACCOUNT_STORE (account_store));
525
526         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
527         device = tny_account_store_get_device (account_store);
528
529         if (G_UNLIKELY (priv->account_store)) {
530
531                 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store), 
532                                                    priv->account_update_signal))
533                         g_signal_handler_disconnect (G_OBJECT (priv->account_store), 
534                                                      priv->account_update_signal);
535                 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store), 
536                                                    priv->accounts_reloaded_signal))
537                         g_signal_handler_disconnect (G_OBJECT (priv->account_store), 
538                                                      priv->accounts_reloaded_signal);
539
540                 g_object_unref (G_OBJECT (priv->account_store));
541         }
542
543         priv->account_store = g_object_ref (G_OBJECT (account_store));
544
545         priv->account_update_signal = 
546                 g_signal_connect (G_OBJECT(account_store), "account_update",
547                                   G_CALLBACK (on_account_update), self);
548
549         priv->accounts_reloaded_signal = 
550                 g_signal_connect (G_OBJECT(account_store), "accounts_reloaded",
551                                   G_CALLBACK (on_accounts_reloaded), self);
552         
553         if (!update_model (MODEST_FOLDER_VIEW (self),
554                            MODEST_TNY_ACCOUNT_STORE (priv->account_store)))
555                 g_printerr ("modest: failed to update model\n");
556
557         g_object_unref (G_OBJECT (device));
558 }
559
560 static void
561 on_account_update (TnyAccountStore *account_store, const gchar *account,
562                    gpointer user_data)
563 {
564         if (!update_model (MODEST_FOLDER_VIEW(user_data), 
565                            MODEST_TNY_ACCOUNT_STORE(account_store)))
566                 g_printerr ("modest: failed to update model for changes in '%s'",
567                             account);
568 }
569
570 static void 
571 on_accounts_reloaded   (TnyAccountStore *account_store, 
572                         gpointer user_data)
573 {
574         update_model (MODEST_FOLDER_VIEW (user_data), 
575                       MODEST_TNY_ACCOUNT_STORE(account_store));
576 }
577
578 void
579 modest_folder_view_set_title (ModestFolderView *self, const gchar *title)
580 {
581         GtkTreeViewColumn *col;
582         
583         g_return_if_fail (self);
584
585         col = gtk_tree_view_get_column (GTK_TREE_VIEW(self), 0);
586         if (!col) {
587                 g_printerr ("modest: failed get column for title\n");
588                 return;
589         }
590
591         gtk_tree_view_column_set_title (col, title);
592         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self),
593                                            title != NULL);
594 }
595
596 GtkWidget*
597 modest_folder_view_new (TnyFolderStoreQuery *query)
598 {
599         GObject *self;
600         ModestFolderViewPrivate *priv;
601         GtkTreeSelection *sel;
602         
603         self = G_OBJECT (g_object_new (MODEST_TYPE_FOLDER_VIEW, NULL));
604         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
605
606         priv->query = g_object_ref (query);
607         
608         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
609         priv->changed_signal = g_signal_connect (sel, "changed",
610                                                  G_CALLBACK (on_selection_changed), self);
611
612         return GTK_WIDGET(self);
613 }
614
615 /* this feels dirty; any other way to expand all the root items? */
616 static void
617 expand_root_items (ModestFolderView *self)
618 {
619         GtkTreePath *path;
620         path = gtk_tree_path_new_first ();
621
622         /* all folders should have child items, so.. */
623         while (gtk_tree_view_expand_row (GTK_TREE_VIEW(self), path, FALSE))
624                 gtk_tree_path_next (path);
625         
626         gtk_tree_path_free (path);
627 }
628
629 /*
630  * HACK: we use this function to implement the
631  * MODEST_FOLDER_VIEW_STYLE_SHOW_ONE style. This implementation
632  * assumes that the model has the following order (which is currently
633  * true) Remote folders->Local folders->MMC folders, so the rows of
634  * the first level (that represent the accounts) are received in the
635  * same order. So basically it uses a static variable to register that
636  * a remote account has already being shown to hide the others
637  * (returning NULL). When the function evaluates the local or the MMC
638  * accounts is time to reset the static variable in order to get it
639  * ready for the next time the tree model needs to be shown.
640  */
641 static gboolean 
642 filter_row (GtkTreeModel *model,
643             GtkTreeIter *iter,
644             gpointer data)
645 {
646         static gboolean found = FALSE;
647         gboolean retval;
648         gint type;
649         GObject *instance;
650
651         gtk_tree_model_get (model, iter,
652                             TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
653                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
654                             -1);
655
656         if (type == TNY_FOLDER_TYPE_ROOT) {
657                 const gchar *account_id = tny_account_get_id (TNY_ACCOUNT (instance));
658
659                 if (strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) &&
660                     strcmp (account_id, MODEST_MMC_ACCOUNT_ID)) {
661
662                         if (!found) {
663                                 found = TRUE;
664                                 retval = TRUE;
665                         } else
666                                 retval = FALSE;
667                 } else {
668                         found = FALSE;
669                         retval = TRUE;
670                 }
671         } else
672                 retval = TRUE;
673
674         return retval;
675 }
676
677 static gboolean
678 update_model (ModestFolderView *self, ModestTnyAccountStore *account_store)
679 {
680         ModestFolderViewPrivate *priv;
681         TnyList          *account_list;
682         GtkTreeModel     *model;
683
684         g_return_val_if_fail (account_store, FALSE);
685
686         priv =  MODEST_FOLDER_VIEW_GET_PRIVATE(self);
687         
688         /* Notify that there is no folder selected */
689         g_signal_emit (G_OBJECT(self), 
690                        signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
691                        NULL, TRUE);
692         
693         /* FIXME: the local accounts are not shown when the query
694            selects only the subscribed folders. */
695 /*      model        = tny_gtk_folder_store_tree_model_new (TRUE, priv->query); */
696         model        = tny_gtk_folder_store_tree_model_new (TRUE, NULL);
697         account_list = TNY_LIST(model);
698
699         tny_account_store_get_accounts (TNY_ACCOUNT_STORE(account_store),
700                                         account_list,
701                                         TNY_ACCOUNT_STORE_STORE_ACCOUNTS);      
702         if (account_list) {
703                 GtkTreeModel *filter_model = NULL, *sortable = NULL;
704
705                 sortable = gtk_tree_model_sort_new_with_model (model);
706                 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(sortable),
707                                                       TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, 
708                                                       GTK_SORT_ASCENDING);
709                 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (sortable),
710                                                  TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
711                                                  cmp_rows, NULL, NULL);
712
713                 /* Create filter model */
714                 if (priv->style == MODEST_FOLDER_VIEW_STYLE_SHOW_ONE) {
715                         filter_model = gtk_tree_model_filter_new (sortable, NULL);
716                         gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (filter_model),
717                                                                 filter_row,
718                                                                 NULL,
719                                                                 NULL);
720                 }
721
722                 /* Set new model */
723                 gtk_tree_view_set_model (GTK_TREE_VIEW(self), 
724                                          (filter_model) ? filter_model : sortable);
725                 expand_root_items (self); /* expand all account folders */
726                 g_object_unref (account_list);
727         }
728         
729         return TRUE;
730 }
731
732
733 static void
734 on_selection_changed (GtkTreeSelection *sel, gpointer user_data)
735 {
736         GtkTreeModel            *model;
737         TnyFolderStore          *folder = NULL;
738         GtkTreeIter             iter;
739         ModestFolderView        *tree_view;
740         ModestFolderViewPrivate *priv;
741         gint                    type;
742
743         g_return_if_fail (sel);
744         g_return_if_fail (user_data);
745         
746         priv = MODEST_FOLDER_VIEW_GET_PRIVATE(user_data);
747         priv->cur_selection = sel;
748         
749         /* folder was _un_selected if true */
750         if (!gtk_tree_selection_get_selected (sel, &model, &iter)) {
751                 if (priv->cur_folder_store)
752                         g_object_unref (priv->cur_folder_store);
753                 priv->cur_folder_store = NULL;
754
755                 /* Notify the display name observers */
756                 g_signal_emit (G_OBJECT(user_data), 
757                                signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
758                                NULL);
759                 return; 
760         }
761
762         tree_view = MODEST_FOLDER_VIEW (user_data);
763
764         gtk_tree_model_get (model, &iter,
765                             TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
766                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder,
767                             -1);
768
769         /* If the folder is the same do not notify */
770         if (priv->cur_folder_store == folder) {
771                 g_object_unref (folder);
772                 return;
773         }
774         
775         /* Current folder was unselected */
776         if (priv->cur_folder_store) {
777                 g_signal_emit (G_OBJECT(tree_view), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
778                                priv->cur_folder_store, FALSE);
779                 g_object_unref (priv->cur_folder_store);
780         }
781
782         /* New current references */
783         priv->cur_folder_store = folder;
784
785         /* New folder has been selected */
786         g_signal_emit (G_OBJECT(tree_view), 
787                        signals[FOLDER_SELECTION_CHANGED_SIGNAL], 
788                        0, folder, TRUE); 
789 }
790
791 TnyFolderStore *
792 modest_folder_view_get_selected (ModestFolderView *self)
793 {
794         ModestFolderViewPrivate *priv;
795
796         g_return_val_if_fail (self, NULL);
797         
798         priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
799         if (priv->cur_folder_store)
800                 g_object_ref (priv->cur_folder_store);
801
802         return priv->cur_folder_store;
803 }
804
805 /*
806  * This function orders the mail accounts following the next rules
807  * 1st - remote accounts
808  * 2nd - local account
809  * 3rd - MMC account
810  */
811 static gint
812 cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2,
813           gpointer user_data)
814 {
815         gint cmp;
816         gchar         *name1, *name2;
817         TnyFolderType type;
818         TnyFolder     *folder1, *folder2;
819
820         gtk_tree_model_get (tree_model, iter1,
821                             TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name1,
822                             TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
823                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder1,
824                             -1);
825         gtk_tree_model_get (tree_model, iter2,
826                             TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name2,
827                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder2,
828                             -1);
829
830         /* Order must be: Remote accounts -> Local account -> MMC account */
831         if (type == TNY_FOLDER_TYPE_ROOT) {
832                 const gchar *account_id = tny_account_get_id (TNY_ACCOUNT (folder1));
833                 const gchar *account_id2 = tny_account_get_id (TNY_ACCOUNT (folder2));
834
835                 if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID))
836                         cmp = +1;
837                 else {
838                         if (!strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID)) {
839                                 if (!strcmp (account_id2, MODEST_MMC_ACCOUNT_ID))
840                                         cmp = -1;
841                                 else
842                                         cmp = +1;
843                         } else {
844                                 if (!strcmp (account_id2, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) ||
845                                     !strcmp (account_id2, MODEST_MMC_ACCOUNT_ID))
846                                         cmp = -1;
847                                 else
848                                         cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
849                         }
850                 }
851         } else 
852                 cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
853         
854         if (folder1)
855                 g_object_unref(G_OBJECT(folder1));
856         if (folder2)
857                 g_object_unref(G_OBJECT(folder2));
858
859         g_free (name1);
860         g_free (name2);
861
862         return cmp;     
863 }
864
865 /*****************************************************************************/
866 /*                        DRAG and DROP stuff                                */
867 /*****************************************************************************/
868
869 /*
870  * This function fills the #GtkSelectionData with the row and the
871  * model that has been dragged. It's called when this widget is a
872  * source for dnd after the event drop happened
873  */
874 static void
875 on_drag_data_get (GtkWidget *widget, 
876                   GdkDragContext *context, 
877                   GtkSelectionData *selection_data, 
878                   guint info, 
879                   guint time, 
880                   gpointer data)
881 {
882         GtkTreeSelection *selection;
883         GtkTreeModel *model_sort, *model;
884         GtkTreeIter iter;
885         GtkTreePath *source_row_sort;
886         GtkTreePath *source_row;
887
888         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
889         gtk_tree_selection_get_selected (selection, &model_sort, &iter);
890         source_row_sort = gtk_tree_model_get_path (model_sort, &iter);
891
892         /* Get the unsorted path and model */
893         model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
894         source_row = gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
895                                                                      source_row_sort);
896
897         gtk_tree_set_row_drag_data (selection_data,
898                                     model,
899                                     source_row);
900
901         gtk_tree_path_free (source_row_sort);
902         gtk_tree_path_free (source_row);
903 }
904
905 typedef struct _DndHelper {
906         gboolean delete_source;
907         GtkTreePath *source_row;
908         GdkDragContext *context;
909         guint time;
910 } DndHelper;
911
912
913 /*
914  * This function is the callback of the
915  * modest_mail_operation_xfer_msg() and
916  * modest_mail_operation_xfer_folder() calls. We check here if the
917  * message/folder was correctly asynchronously transferred. The reason
918  * to use the same callback is that the code is the same, it only has
919  * to check that the operation went fine and then finalize the drag
920  * and drop action
921  */
922 static void
923 on_progress_changed (ModestMailOperation *mail_op, gpointer user_data)
924 {
925         gboolean success;
926         DndHelper *helper;
927
928         helper = (DndHelper *) user_data;
929
930         if (!modest_mail_operation_is_finished (mail_op))
931                 return;
932
933         if (modest_mail_operation_get_status (mail_op) == 
934             MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
935                 success = TRUE;
936         } else {
937                 success = FALSE;
938         }
939
940         /* Notify the drag source. Never call delete, the monitor will
941            do the job if needed */
942         gtk_drag_finish (helper->context, success, FALSE, helper->time);
943
944         /* Free the helper */
945         gtk_tree_path_free (helper->source_row);        
946         g_slice_free (DndHelper, helper);
947 }
948
949 /*
950  * This function is used by drag_data_received_cb to manage drag and
951  * drop of a header, i.e, and drag from the header view to the folder
952  * view.
953  */
954 static void
955 drag_and_drop_from_header_view (GtkTreeModel *source_model,
956                                 GtkTreeModel *dest_model,
957                                 GtkTreePath  *dest_row,
958                                 DndHelper    *helper)
959 {
960         TnyHeader *header;
961         TnyFolder *folder;
962         ModestMailOperation *mail_op;
963         GtkTreeIter source_iter, dest_iter;
964
965         /* Get header */
966         gtk_tree_model_get_iter (source_model, &source_iter, helper->source_row);
967         gtk_tree_model_get (source_model, &source_iter, 
968                             TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN, 
969                             &header, -1);
970
971         /* Get Folder */
972         gtk_tree_model_get_iter (dest_model, &dest_iter, dest_row);
973         gtk_tree_model_get (dest_model, &dest_iter, 
974                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, 
975                             &folder, -1);
976
977         /* Transfer message */
978         mail_op = modest_mail_operation_new ();
979         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
980                                          mail_op);
981         g_signal_connect (G_OBJECT (mail_op), "progress-changed",
982                           G_CALLBACK (on_progress_changed), helper);
983
984         modest_mail_operation_xfer_msg (mail_op, header, folder, helper->delete_source);
985
986         /* Frees */
987         g_object_unref (G_OBJECT (mail_op));
988         g_object_unref (G_OBJECT (header));
989         g_object_unref (G_OBJECT (folder));
990 }
991
992 /*
993  * This function is used by drag_data_received_cb to manage drag and
994  * drop of a folder, i.e, and drag from the folder view to the same
995  * folder view.
996  */
997 static void
998 drag_and_drop_from_folder_view (GtkTreeModel     *source_model,
999                                 GtkTreeModel     *dest_model,
1000                                 GtkTreePath      *dest_row,
1001                                 GtkSelectionData *selection_data,
1002                                 DndHelper        *helper)
1003 {
1004         ModestMailOperation *mail_op;
1005         GtkTreeIter parent_iter, iter;
1006         TnyFolderStore *parent_folder;
1007         TnyFolder *folder;
1008
1009         /* Check if the drag is possible */
1010         if (!gtk_tree_path_compare (helper->source_row, dest_row) ||
1011             !gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (dest_model),
1012                                                    dest_row,
1013                                                    selection_data)) {
1014
1015                 gtk_drag_finish (helper->context, FALSE, FALSE, helper->time);
1016                 gtk_tree_path_free (helper->source_row);        
1017                 g_slice_free (DndHelper, helper);
1018                 return;
1019         }
1020
1021         /* Get data */
1022         gtk_tree_model_get_iter (source_model, &parent_iter, dest_row);
1023         gtk_tree_model_get_iter (source_model, &iter, helper->source_row);
1024         gtk_tree_model_get (source_model, &parent_iter, 
1025                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, 
1026                             &parent_folder, -1);
1027         gtk_tree_model_get (source_model, &iter,
1028                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
1029                             &folder, -1);
1030
1031         /* Do the mail operation */
1032         mail_op = modest_mail_operation_new ();
1033         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
1034                                          mail_op);
1035         g_signal_connect (G_OBJECT (mail_op), "progress-changed",
1036                           G_CALLBACK (on_progress_changed), helper);
1037
1038         modest_mail_operation_xfer_folder (mail_op, 
1039                                            folder, 
1040                                            parent_folder,
1041                                            helper->delete_source);
1042
1043         /* Frees */
1044         g_object_unref (G_OBJECT (parent_folder));
1045         g_object_unref (G_OBJECT (folder));
1046         g_object_unref (G_OBJECT (mail_op));
1047 }
1048
1049 /*
1050  * This function receives the data set by the "drag-data-get" signal
1051  * handler. This information comes within the #GtkSelectionData. This
1052  * function will manage both the drags of folders of the treeview and
1053  * drags of headers of the header view widget.
1054  */
1055 static void 
1056 on_drag_data_received (GtkWidget *widget, 
1057                        GdkDragContext *context, 
1058                        gint x, 
1059                        gint y, 
1060                        GtkSelectionData *selection_data, 
1061                        guint target_type, 
1062                        guint time, 
1063                        gpointer data)
1064 {
1065         GtkWidget *source_widget;
1066         GtkTreeModel *model_sort, *dest_model, *source_model;
1067         GtkTreePath *source_row, *dest_row, *child_dest_row;
1068         GtkTreeViewDropPosition pos;
1069         gboolean success = FALSE, delete_source = FALSE;
1070         DndHelper *helper;
1071
1072         /* Do not allow further process */
1073         g_signal_stop_emission_by_name (widget, "drag-data-received");
1074
1075         /* Get the action */
1076         if (context->action == GDK_ACTION_MOVE)
1077                 delete_source = TRUE;
1078
1079         /* Check if the get_data failed */
1080         if (selection_data == NULL || selection_data->length < 0)
1081                 gtk_drag_finish (context, success, FALSE, time);
1082
1083         /* Get the models */
1084         source_widget = gtk_drag_get_source_widget (context);
1085         gtk_tree_get_row_drag_data (selection_data,
1086                                     &source_model,
1087                                     &source_row);
1088
1089         model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
1090         /* Select the destination model */
1091         if (source_widget == widget) {
1092                 dest_model = source_model;
1093         } else {
1094                 dest_model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
1095         }
1096
1097         /* Get the path to the destination row. Can not call
1098            gtk_tree_view_get_drag_dest_row() because the source row
1099            is not selected anymore */
1100         gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget), x, y,
1101                                            &dest_row, &pos);
1102
1103         /* Only allow drops IN other rows */
1104         if (!dest_row || pos == GTK_TREE_VIEW_DROP_BEFORE || pos == GTK_TREE_VIEW_DROP_AFTER)
1105                 gtk_drag_finish (context, success, FALSE, time);
1106
1107         /* Create the helper */
1108         helper = g_slice_new0 (DndHelper);
1109         helper->delete_source = delete_source;
1110         helper->source_row = gtk_tree_path_copy (source_row);
1111         helper->context = context;
1112         helper->time = time;
1113
1114         /* Get path from the unsorted model */
1115         child_dest_row = 
1116                 gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
1117                                                                 dest_row);
1118         gtk_tree_path_free (dest_row);
1119
1120         /* Drags from the header view */
1121         if (source_widget != widget) {
1122
1123                 drag_and_drop_from_header_view (source_model,
1124                                                 dest_model,
1125                                                 child_dest_row,
1126                                                 helper);
1127         } else {
1128
1129
1130                 drag_and_drop_from_folder_view (source_model,
1131                                                 dest_model,
1132                                                 child_dest_row,
1133                                                 selection_data, 
1134                                                 helper);
1135         }
1136
1137         /* Frees */
1138         gtk_tree_path_free (source_row);
1139         gtk_tree_path_free (child_dest_row);
1140 }
1141
1142 /*
1143  * We define a "drag-drop" signal handler because we do not want to
1144  * use the default one, because the default one always calls
1145  * gtk_drag_finish and we prefer to do it in the "drag-data-received"
1146  * signal handler, because there we have all the information available
1147  * to know if the dnd was a success or not.
1148  */
1149 static gboolean
1150 drag_drop_cb (GtkWidget      *widget,
1151               GdkDragContext *context,
1152               gint            x,
1153               gint            y,
1154               guint           time,
1155               gpointer        user_data) 
1156 {
1157         gpointer target;
1158
1159         if (!context->targets)
1160                 return FALSE;
1161
1162         /* Check if we're dragging a folder row */
1163         target = gtk_drag_dest_find_target (widget, context, NULL);
1164
1165         /* Request the data from the source. */
1166         gtk_drag_get_data(widget, context, target, time);
1167
1168     return TRUE;
1169 }
1170
1171 /*
1172  * This function expands a node of a tree view if it's not expanded
1173  * yet. Not sure why it needs the threads stuff, but gtk+`example code
1174  * does that, so that's why they're here.
1175  */
1176 static gint
1177 expand_row_timeout (gpointer data)
1178 {
1179         GtkTreeView *tree_view = data;
1180         GtkTreePath *dest_path = NULL;
1181         GtkTreeViewDropPosition pos;
1182         gboolean result = FALSE;
1183         
1184         GDK_THREADS_ENTER ();
1185         
1186         gtk_tree_view_get_drag_dest_row (tree_view,
1187                                          &dest_path,
1188                                          &pos);
1189         
1190         if (dest_path &&
1191             (pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER ||
1192              pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)) {
1193                 gtk_tree_view_expand_row (tree_view, dest_path, FALSE);
1194                 gtk_tree_path_free (dest_path);
1195         }
1196         else {
1197                 if (dest_path)
1198                         gtk_tree_path_free (dest_path);
1199                 
1200                 result = TRUE;
1201         }
1202         
1203         GDK_THREADS_LEAVE ();
1204
1205         return result;
1206 }
1207
1208 /*
1209  * This function is called whenever the pointer is moved over a widget
1210  * while dragging some data. It installs a timeout that will expand a
1211  * node of the treeview if not expanded yet. This function also calls
1212  * gdk_drag_status in order to set the suggested action that will be
1213  * used by the "drag-data-received" signal handler to know if we
1214  * should do a move or just a copy of the data.
1215  */
1216 static gboolean
1217 on_drag_motion (GtkWidget      *widget,
1218                 GdkDragContext *context,
1219                 gint            x,
1220                 gint            y,
1221                 guint           time,
1222                 gpointer        user_data)  
1223 {
1224         GtkTreeViewDropPosition pos;
1225         GtkTreePath *dest_row;
1226         ModestFolderViewPrivate *priv;
1227         GdkDragAction suggested_action;
1228         gboolean valid_location = FALSE;
1229
1230         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (widget);
1231
1232         if (priv->timer_expander != 0) {
1233                 g_source_remove (priv->timer_expander);
1234                 priv->timer_expander = 0;
1235         }
1236
1237         gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
1238                                            x, y,
1239                                            &dest_row,
1240                                            &pos);
1241
1242         /* Do not allow drops between folders */
1243         if (!dest_row ||
1244             pos == GTK_TREE_VIEW_DROP_BEFORE || 
1245             pos == GTK_TREE_VIEW_DROP_AFTER) {
1246                 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW (widget), NULL, 0);
1247                 gdk_drag_status(context, 0, time);
1248                 valid_location = FALSE;
1249                 goto out;
1250         } else {
1251                 valid_location = TRUE;
1252         }
1253
1254         /* Expand the selected row after 1/2 second */
1255         if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), dest_row)) {
1256                 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), dest_row, pos);
1257                 priv->timer_expander = g_timeout_add (500, expand_row_timeout, widget);
1258         }
1259         gtk_tree_path_free (dest_row);
1260
1261         /* Select the desired action. By default we pick MOVE */
1262         suggested_action = GDK_ACTION_MOVE;
1263
1264         if (context->actions == GDK_ACTION_COPY)
1265             gdk_drag_status(context, GDK_ACTION_COPY, time);
1266         else if (context->actions == GDK_ACTION_MOVE)
1267             gdk_drag_status(context, GDK_ACTION_MOVE, time);
1268         else if (context->actions & suggested_action)
1269             gdk_drag_status(context, suggested_action, time);
1270         else
1271             gdk_drag_status(context, GDK_ACTION_DEFAULT, time);
1272
1273  out:
1274         g_signal_stop_emission_by_name (widget, "drag-motion");
1275         return valid_location;
1276 }
1277
1278
1279 /* Folder view drag types */
1280 const GtkTargetEntry folder_view_drag_types[] =
1281 {
1282         { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, MODEST_FOLDER_ROW },
1283         { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_APP,    MODEST_HEADER_ROW }
1284 };
1285
1286 /*
1287  * This function sets the treeview as a source and a target for dnd
1288  * events. It also connects all the requirede signals.
1289  */
1290 static void
1291 setup_drag_and_drop (GtkTreeView *self)
1292 {
1293         /* Set up the folder view as a dnd destination. Set only the
1294            highlight flag, otherwise gtk will have a different
1295            behaviour */
1296         gtk_drag_dest_set (GTK_WIDGET (self),
1297                            GTK_DEST_DEFAULT_HIGHLIGHT,
1298                            folder_view_drag_types,
1299                            G_N_ELEMENTS (folder_view_drag_types),
1300                            GDK_ACTION_MOVE | GDK_ACTION_COPY);
1301
1302         g_signal_connect (G_OBJECT (self),
1303                           "drag_data_received",
1304                           G_CALLBACK (on_drag_data_received),
1305                           NULL);
1306
1307
1308         /* Set up the treeview as a dnd source */
1309         gtk_drag_source_set (GTK_WIDGET (self),
1310                              GDK_BUTTON1_MASK,
1311                              folder_view_drag_types,
1312                              G_N_ELEMENTS (folder_view_drag_types),
1313                              GDK_ACTION_MOVE | GDK_ACTION_COPY);
1314
1315         g_signal_connect (G_OBJECT (self),
1316                           "drag_motion",
1317                           G_CALLBACK (on_drag_motion),
1318                           NULL);
1319         
1320         g_signal_connect (G_OBJECT (self),
1321                           "drag_data_get",
1322                           G_CALLBACK (on_drag_data_get),
1323                           NULL);
1324
1325         g_signal_connect (G_OBJECT (self),
1326                           "drag_drop",
1327                           G_CALLBACK (drag_drop_cb),
1328                           NULL);
1329 }
1330
1331 /*
1332  * This function manages the navigation through the folders using the
1333  * keyboard or the hardware keys in the device
1334  */
1335 static gboolean
1336 on_key_pressed (GtkWidget *self,
1337                 GdkEventKey *event,
1338                 gpointer user_data)
1339 {
1340         GtkTreeSelection *selection;
1341         GtkTreeIter iter;
1342         GtkTreeModel *model;
1343         gboolean retval = FALSE;
1344
1345         /* Up and Down are automatically managed by the treeview */
1346         if (event->keyval == GDK_Return) {
1347                 /* Expand/Collapse the selected row */
1348                 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
1349                 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
1350                         GtkTreePath *path;
1351
1352                         path = gtk_tree_model_get_path (model, &iter);
1353
1354                         if (gtk_tree_view_row_expanded (GTK_TREE_VIEW (self), path))
1355                                 gtk_tree_view_collapse_row (GTK_TREE_VIEW (self), path);
1356                         else
1357                                 gtk_tree_view_expand_row (GTK_TREE_VIEW (self), path, FALSE);
1358                         gtk_tree_path_free (path);
1359                 }
1360                 /* No further processing */
1361                 retval = TRUE;
1362         }
1363
1364         return retval;
1365 }
1366
1367 /*
1368  * We listen to the changes in the local folder account name key,
1369  * because we want to show the right name in the view. The local
1370  * folder account name corresponds to the device name in the Maemo
1371  * version. We do this because we do not want to query gconf on each
1372  * tree view refresh. It's better to cache it and change whenever
1373  * necessary.
1374  */
1375 static void 
1376 on_configuration_key_changed (ModestConf* conf, 
1377                               const gchar *key, 
1378                               ModestConfEvent event, 
1379                               ModestFolderView *self)
1380 {
1381         ModestFolderViewPrivate *priv;
1382
1383         if (!key || strcmp (key, MODEST_CONF_DEVICE_NAME))
1384                 return;
1385
1386         priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
1387
1388         g_free (priv->local_account_name);
1389
1390         if (event == MODEST_CONF_EVENT_KEY_UNSET)
1391                 priv->local_account_name = g_strdup (MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME);
1392         else
1393                 priv->local_account_name = modest_conf_get_string (modest_runtime_get_conf(),
1394                                                                    MODEST_CONF_DEVICE_NAME, NULL);
1395
1396         /* Force a redraw */
1397 #if GTK_CHECK_VERSION(2, 8, 0) /* gtk_tree_view_column_queue_resize is only available in GTK+ 2.8 */
1398         GtkTreeViewColumn * tree_column = gtk_tree_view_get_column (GTK_TREE_VIEW (self), 
1399                                                 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN);
1400         gtk_tree_view_column_queue_resize (tree_column);
1401 #endif
1402 }
1403
1404 void 
1405 modest_folder_view_set_style (ModestFolderView *self,
1406                               ModestFolderViewStyle style)
1407 {
1408         ModestFolderViewPrivate *priv;
1409
1410         g_return_if_fail (self);
1411         
1412         priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
1413
1414         priv->style = style;
1415 }