e04d46874929817e6dfd7e3a31ab163e16d24093
[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-folder-store-observer.h>
39 #include <tny-account-store.h>
40 #include <tny-account.h>
41 #include <tny-folder.h>
42 #include <tny-camel-folder.h>
43 #include <tny-simple-list.h>
44 #include <tny-camel-account.h>
45 #include <modest-tny-account.h>
46 #include <modest-tny-folder.h>
47 #include <modest-tny-local-folders-account.h>
48 #include <modest-tny-outbox-account.h>
49 #include <modest-marshal.h>
50 #include <modest-icon-names.h>
51 #include <modest-tny-account-store.h>
52 #include <modest-text-utils.h>
53 #include <modest-runtime.h>
54 #include "modest-folder-view.h"
55 #include <modest-platform.h>
56 #include <modest-widget-memory.h>
57 #include <modest-ui-actions.h>
58 #include "modest-dnd.h"
59 #include "widgets/modest-window.h"
60
61 /* Folder view drag types */
62 const GtkTargetEntry folder_view_drag_types[] =
63 {
64         { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, MODEST_FOLDER_ROW },
65         { GTK_TREE_PATH_AS_STRING_LIST, GTK_TARGET_SAME_APP, MODEST_HEADER_ROW }
66 };
67
68 /* 'private'/'protected' functions */
69 static void modest_folder_view_class_init  (ModestFolderViewClass *klass);
70 static void modest_folder_view_init        (ModestFolderView *obj);
71 static void modest_folder_view_finalize    (GObject *obj);
72
73 static void         tny_account_store_view_init (gpointer g, 
74                                                  gpointer iface_data);
75
76 static void         modest_folder_view_set_account_store (TnyAccountStoreView *self, 
77                                                           TnyAccountStore     *account_store);
78
79 static void         on_selection_changed   (GtkTreeSelection *sel, 
80                                             gpointer data);
81
82 static void         on_account_removed     (TnyAccountStore *self, 
83                                             TnyAccount *account,
84                                             gpointer user_data);
85
86 static void         on_account_inserted    (TnyAccountStore *self, 
87                                             TnyAccount *account,
88                                             gpointer user_data);
89
90 static void         on_account_changed    (TnyAccountStore *self, 
91                                             TnyAccount *account,
92                                             gpointer user_data);
93
94 static gint         cmp_rows               (GtkTreeModel *tree_model, 
95                                             GtkTreeIter *iter1, 
96                                             GtkTreeIter *iter2,
97                                             gpointer user_data);
98
99 static gboolean     filter_row             (GtkTreeModel *model,
100                                             GtkTreeIter *iter,
101                                             gpointer data);
102
103 static gboolean     on_key_pressed         (GtkWidget *self,
104                                             GdkEventKey *event,
105                                             gpointer user_data);
106
107 static void         on_configuration_key_changed  (ModestConf* conf, 
108                                                    const gchar *key, 
109                                                    ModestConfEvent event,
110                                                    ModestConfNotificationId notification_id, 
111                                                    ModestFolderView *self);
112
113 /* DnD functions */
114 static void         on_drag_data_get       (GtkWidget *widget, 
115                                             GdkDragContext *context, 
116                                             GtkSelectionData *selection_data, 
117                                             guint info, 
118                                             guint time, 
119                                             gpointer data);
120
121 static void         on_drag_data_received  (GtkWidget *widget, 
122                                             GdkDragContext *context, 
123                                             gint x, 
124                                             gint y, 
125                                             GtkSelectionData *selection_data, 
126                                             guint info, 
127                                             guint time, 
128                                             gpointer data);
129
130 static gboolean     on_drag_motion         (GtkWidget      *widget,
131                                             GdkDragContext *context,
132                                             gint            x,
133                                             gint            y,
134                                             guint           time,
135                                             gpointer        user_data);
136
137 static void         expand_root_items (ModestFolderView *self);
138
139 static gint         expand_row_timeout     (gpointer data);
140
141 static void         setup_drag_and_drop    (GtkTreeView *self);
142
143 static gboolean     _clipboard_set_selected_data (ModestFolderView *folder_view, 
144                                                   gboolean delete);
145
146 static void         _clear_hidding_filter (ModestFolderView *folder_view);
147
148 static void         on_row_inserted_maybe_select_folder (GtkTreeModel     *tree_model, 
149                                                          GtkTreePath      *path, 
150                                                          GtkTreeIter      *iter,
151                                                          ModestFolderView *self);
152
153 static void         on_display_name_changed (ModestAccountMgr *self, 
154                                              const gchar *account,
155                                              gpointer user_data);
156
157 enum {
158         FOLDER_SELECTION_CHANGED_SIGNAL,
159         FOLDER_DISPLAY_NAME_CHANGED_SIGNAL,
160         LAST_SIGNAL
161 };
162
163 typedef struct _ModestFolderViewPrivate ModestFolderViewPrivate;
164 struct _ModestFolderViewPrivate {
165         TnyAccountStore      *account_store;
166         TnyFolderStore       *cur_folder_store;
167
168         TnyFolder            *folder_to_select; /* folder to select after the next update */
169
170         gulong                changed_signal;
171         gulong                account_inserted_signal;
172         gulong                account_removed_signal;
173         gulong                account_changed_signal;
174         gulong                conf_key_signal;
175         gulong                display_name_changed_signal;
176         
177         /* not unref this object, its a singlenton */
178         ModestEmailClipboard *clipboard;
179
180         /* Filter tree model */
181         gchar **hidding_ids;
182         guint n_selected;
183
184         TnyFolderStoreQuery  *query;
185         guint                 timer_expander;
186
187         gchar                *local_account_name;
188         gchar                *visible_account_id;
189         ModestFolderViewStyle style;
190
191         gboolean  reselect; /* we use this to force a reselection of the INBOX */
192         gboolean  show_non_move;
193         gboolean  reexpand; /* next time we expose, we'll expand all root folders */
194 };
195 #define MODEST_FOLDER_VIEW_GET_PRIVATE(o)                       \
196         (G_TYPE_INSTANCE_GET_PRIVATE((o),                       \
197                                      MODEST_TYPE_FOLDER_VIEW,   \
198                                      ModestFolderViewPrivate))
199 /* globals */
200 static GObjectClass *parent_class = NULL;
201
202 static guint signals[LAST_SIGNAL] = {0}; 
203
204 GType
205 modest_folder_view_get_type (void)
206 {
207         static GType my_type = 0;
208         if (!my_type) {
209                 static const GTypeInfo my_info = {
210                         sizeof(ModestFolderViewClass),
211                         NULL,           /* base init */
212                         NULL,           /* base finalize */
213                         (GClassInitFunc) modest_folder_view_class_init,
214                         NULL,           /* class finalize */
215                         NULL,           /* class data */
216                         sizeof(ModestFolderView),
217                         1,              /* n_preallocs */
218                         (GInstanceInitFunc) modest_folder_view_init,
219                         NULL
220                 };
221
222                 static const GInterfaceInfo tny_account_store_view_info = {
223                         (GInterfaceInitFunc) tny_account_store_view_init, /* interface_init */
224                         NULL,         /* interface_finalize */
225                         NULL          /* interface_data */
226                 };
227
228                                 
229                 my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
230                                                   "ModestFolderView",
231                                                   &my_info, 0);
232
233                 g_type_add_interface_static (my_type, 
234                                              TNY_TYPE_ACCOUNT_STORE_VIEW, 
235                                              &tny_account_store_view_info);
236         }
237         return my_type;
238 }
239
240 static void
241 modest_folder_view_class_init (ModestFolderViewClass *klass)
242 {
243         GObjectClass *gobject_class;
244         GtkTreeViewClass *treeview_class;
245         gobject_class = (GObjectClass*) klass;
246         treeview_class = (GtkTreeViewClass*) klass;
247
248         parent_class            = g_type_class_peek_parent (klass);
249         gobject_class->finalize = modest_folder_view_finalize;
250
251         g_type_class_add_private (gobject_class,
252                                   sizeof(ModestFolderViewPrivate));
253         
254         signals[FOLDER_SELECTION_CHANGED_SIGNAL] = 
255                 g_signal_new ("folder_selection_changed",
256                               G_TYPE_FROM_CLASS (gobject_class),
257                               G_SIGNAL_RUN_FIRST,
258                               G_STRUCT_OFFSET (ModestFolderViewClass,
259                                                folder_selection_changed),
260                               NULL, NULL,
261                               modest_marshal_VOID__POINTER_BOOLEAN,
262                               G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_BOOLEAN);
263
264         /*
265          * This signal is emitted whenever the currently selected
266          * folder display name is computed. Note that the name could
267          * be different to the folder name, because we could append
268          * the unread messages count to the folder name to build the
269          * folder display name
270          */
271         signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL] = 
272                 g_signal_new ("folder-display-name-changed",
273                               G_TYPE_FROM_CLASS (gobject_class),
274                               G_SIGNAL_RUN_FIRST,
275                               G_STRUCT_OFFSET (ModestFolderViewClass,
276                                                folder_display_name_changed),
277                               NULL, NULL,
278                               g_cclosure_marshal_VOID__STRING,
279                               G_TYPE_NONE, 1, G_TYPE_STRING);
280
281         treeview_class->select_cursor_parent = NULL;
282
283 }
284
285 /* Simplify checks for NULLs: */
286 static gboolean
287 strings_are_equal (const gchar *a, const gchar *b)
288 {
289         if (!a && !b)
290                 return TRUE;
291         if (a && b)
292         {
293                 return (strcmp (a, b) == 0);
294         }
295         else
296                 return FALSE;
297 }
298
299 static gboolean
300 on_model_foreach_set_name(GtkTreeModel *model, GtkTreePath *path,  GtkTreeIter *iter, gpointer data)
301 {
302         GObject *instance = NULL;
303         
304         gtk_tree_model_get (model, iter,
305                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
306                             -1);
307                             
308         if (!instance)
309                 return FALSE; /* keep walking */
310                         
311         if (!TNY_IS_ACCOUNT (instance)) {
312                 g_object_unref (instance);
313                 return FALSE; /* keep walking */        
314         }    
315         
316         /* Check if this is the looked-for account: */
317         TnyAccount *this_account = TNY_ACCOUNT (instance);
318         TnyAccount *account = TNY_ACCOUNT (data);
319         
320         const gchar *this_account_id = tny_account_get_id(this_account);
321         const gchar *account_id = tny_account_get_id(account);
322         g_object_unref (instance);
323         instance = NULL;
324
325         /* printf ("DEBUG: %s: this_account_id=%s, account_id=%s\n", __FUNCTION__, this_account_id, account_id); */
326         if (strings_are_equal(this_account_id, account_id)) {
327                 /* Tell the model that the data has changed, so that
328                  * it calls the cell_data_func callbacks again: */
329                 /* TODO: This does not seem to actually cause the new string to be shown: */
330                 gtk_tree_model_row_changed (model, path, iter);
331                 
332                 return TRUE; /* stop walking */
333         }
334         
335         return FALSE; /* keep walking */
336 }
337
338 typedef struct 
339 {
340         ModestFolderView *self;
341         gchar *previous_name;
342 } GetMmcAccountNameData;
343
344 static void
345 on_get_mmc_account_name (TnyStoreAccount* account, gpointer user_data)
346 {
347         /* printf ("DEBU1G: %s: account name=%s\n", __FUNCTION__, tny_account_get_name (TNY_ACCOUNT(account))); */
348
349         GetMmcAccountNameData *data = (GetMmcAccountNameData*)user_data;
350         
351         if (!strings_are_equal (
352                 tny_account_get_name(TNY_ACCOUNT(account)), 
353                 data->previous_name)) {
354         
355                 /* Tell the model that the data has changed, so that 
356                  * it calls the cell_data_func callbacks again: */
357                 ModestFolderView *self = data->self;
358                 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
359                 if (model)
360                         gtk_tree_model_foreach(model, on_model_foreach_set_name, account);
361         }
362
363         g_free (data->previous_name);
364         g_slice_free (GetMmcAccountNameData, data);
365 }
366
367 static void
368 text_cell_data  (GtkTreeViewColumn *column,
369                  GtkCellRenderer *renderer,
370                  GtkTreeModel *tree_model,
371                  GtkTreeIter *iter,
372                  gpointer data)
373 {
374         ModestFolderViewPrivate *priv;
375         GObject *rendobj = (GObject *) renderer;
376         gchar *fname = NULL;
377         TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
378         GObject *instance = NULL;
379
380         gtk_tree_model_get (tree_model, iter,
381                             TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname,
382                             TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
383                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
384                             -1);
385         if (!fname || !instance)
386                 goto end;
387
388         ModestFolderView *self = MODEST_FOLDER_VIEW (data);
389         priv =  MODEST_FOLDER_VIEW_GET_PRIVATE (self);
390         
391         gchar *item_name = NULL;
392         gint item_weight = 400;
393         
394         if (type != TNY_FOLDER_TYPE_ROOT) {
395                 gint number = 0;
396                 
397                 if (modest_tny_folder_is_local_folder (TNY_FOLDER (instance)) ||
398                     modest_tny_folder_is_memory_card_folder (TNY_FOLDER (instance))) {
399                         type = modest_tny_folder_get_local_or_mmc_folder_type (TNY_FOLDER (instance));
400                         if (type != TNY_FOLDER_TYPE_UNKNOWN) {
401                                 g_free (fname);
402                                 fname = g_strdup (modest_local_folder_info_get_type_display_name (type));
403                         }
404                 }
405
406                 /* note: we cannot reliably get the counts from the tree model, we need
407                  * to use explicit calls on tny_folder for some reason.
408                  */
409                 /* Select the number to show: the unread or unsent messages. in case of outbox/drafts, show all */
410                 if ((type == TNY_FOLDER_TYPE_DRAFTS) ||
411                     (type == TNY_FOLDER_TYPE_OUTBOX) ||
412                     (type == TNY_FOLDER_TYPE_MERGE)) /* _OUTBOX actually returns _MERGE... */
413                         number = tny_folder_get_all_count (TNY_FOLDER(instance));
414                 else
415                         number = tny_folder_get_unread_count (TNY_FOLDER(instance));
416                                                                 
417                 /* Use bold font style if there are unread or unset messages */
418                 if (number > 0) {
419                         if (type == TNY_FOLDER_TYPE_INBOX)
420                                 item_name = g_strdup_printf ("%s (%d)", _("mcen_me_folder_inbox"), number);
421                         else
422                                 item_name = g_strdup_printf ("%s (%d)", fname, number);
423                         item_weight = 800;
424                 } else {
425                         if (type == TNY_FOLDER_TYPE_INBOX)
426                                 item_name = g_strdup (_("mcen_me_folder_inbox"));
427                         else
428                                 item_name = g_strdup (fname);
429                         item_weight = 400;
430                 }
431                 
432         } else if (TNY_IS_ACCOUNT (instance)) {
433                 /* If it's a server account */
434                 if (modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (instance))) {
435                         item_name = g_strdup (priv->local_account_name);
436                         item_weight = 800;
437                 } else if (modest_tny_account_is_memory_card_account (TNY_ACCOUNT (instance))) {
438                         /* fname is only correct when the items are first 
439                          * added to the model, not when the account is 
440                          * changed later, so get the name from the account
441                          * instance: */
442                         item_name = g_strdup (tny_account_get_name (TNY_ACCOUNT (instance)));
443                         item_weight = 800;
444                 } else {
445                         item_name = g_strdup (fname);
446                         item_weight = 800;
447                 }
448         }
449         
450         if (!item_name)
451                 item_name = g_strdup ("unknown");
452                         
453         if (item_name && item_weight) {
454                 /* Set the name in the treeview cell: */
455                 g_object_set (rendobj,"text", item_name, "weight", item_weight, NULL);
456                 
457                 /* Notify display name observers */
458                 /* TODO: What listens for this signal, and how can it use only the new name? */
459                 if (((GObject *) priv->cur_folder_store) == instance) {
460                         g_signal_emit (G_OBJECT(self),
461                                                signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
462                                                item_name);
463                 }
464                 g_free (item_name);
465                 
466         }
467         
468         /* If it is a Memory card account, make sure that we have the correct name.
469          * This function will be trigerred again when the name has been retrieved: */
470         if (TNY_IS_STORE_ACCOUNT (instance) && 
471                 modest_tny_account_is_memory_card_account (TNY_ACCOUNT (instance))) {
472
473                 /* Get the account name asynchronously: */
474                 GetMmcAccountNameData *callback_data = 
475                         g_slice_new0(GetMmcAccountNameData);
476                 callback_data->self = self;
477
478                 const gchar *name = tny_account_get_name (TNY_ACCOUNT(instance));
479                 if (name)
480                         callback_data->previous_name = g_strdup (name); 
481
482                 modest_tny_account_get_mmc_account_name (TNY_STORE_ACCOUNT (instance), 
483                                                          on_get_mmc_account_name, callback_data);
484         }
485  end:                   
486         if (instance)
487                 g_object_unref (G_OBJECT (instance));
488         if (fname)
489                 g_free (fname);
490 }
491
492
493 typedef struct {
494         GdkPixbuf *pixbuf;
495         GdkPixbuf *pixbuf_open;
496         GdkPixbuf *pixbuf_close;
497 } ThreePixbufs;
498
499
500 static inline GdkPixbuf *
501 get_composite_pixbuf (const gchar *icon_name, 
502                       const gint size, 
503                       GdkPixbuf *base_pixbuf)
504 {
505         GdkPixbuf *emblem, *retval = NULL;
506
507         emblem = modest_platform_get_icon (icon_name, size);
508         if (emblem) {
509                 retval = gdk_pixbuf_copy (base_pixbuf);
510                 gdk_pixbuf_composite (emblem, retval, 0, 0, 
511                                       MIN (gdk_pixbuf_get_width (emblem), 
512                                            gdk_pixbuf_get_width (retval)),
513                                       MIN (gdk_pixbuf_get_height (emblem), 
514                                            gdk_pixbuf_get_height (retval)),
515                                                   0, 0, 1, 1, GDK_INTERP_NEAREST, 255);
516                 g_object_unref (emblem);
517         }
518         return retval;
519 }
520
521 static inline ThreePixbufs *
522 get_composite_icons (const gchar *icon_code,
523                      GdkPixbuf **pixbuf,
524                      GdkPixbuf **pixbuf_open,
525                      GdkPixbuf **pixbuf_close)
526 {
527         ThreePixbufs *retval;
528
529         if (!*pixbuf)
530                 *pixbuf = gdk_pixbuf_copy (modest_platform_get_icon (icon_code, MODEST_ICON_SIZE_SMALL));
531         
532         if (!*pixbuf_open)
533                 *pixbuf_open = get_composite_pixbuf ("qgn_list_gene_fldr_exp",
534                                                      MODEST_ICON_SIZE_SMALL,
535                                                      *pixbuf);
536         
537         if (!*pixbuf_close)
538                 *pixbuf_close = get_composite_pixbuf ("qgn_list_gene_fldr_clp",
539                                                       MODEST_ICON_SIZE_SMALL,
540                                                       *pixbuf);
541
542         retval = g_slice_new0 (ThreePixbufs);
543         if (*pixbuf)
544                 retval->pixbuf = g_object_ref (*pixbuf);
545         if (*pixbuf_open)
546                 retval->pixbuf_open = g_object_ref (*pixbuf_open);
547         if (*pixbuf_close)
548                 retval->pixbuf_close = g_object_ref (*pixbuf_close);
549
550         return retval;
551 }
552
553 static ThreePixbufs*
554 get_folder_icons (TnyFolderType type, GObject *instance)
555 {
556         static GdkPixbuf *inbox_pixbuf = NULL, *outbox_pixbuf = NULL,
557                 *junk_pixbuf = NULL, *sent_pixbuf = NULL,
558                 *trash_pixbuf = NULL, *draft_pixbuf = NULL,
559                 *normal_pixbuf = NULL, *anorm_pixbuf = NULL, 
560                 *ammc_pixbuf = NULL, *avirt_pixbuf = NULL;
561
562         static GdkPixbuf *inbox_pixbuf_open = NULL, *outbox_pixbuf_open = NULL,
563                 *junk_pixbuf_open = NULL, *sent_pixbuf_open = NULL,
564                 *trash_pixbuf_open = NULL, *draft_pixbuf_open = NULL,
565                 *normal_pixbuf_open = NULL, *anorm_pixbuf_open = NULL, 
566                 *ammc_pixbuf_open = NULL, *avirt_pixbuf_open = NULL;
567
568         static GdkPixbuf *inbox_pixbuf_close = NULL, *outbox_pixbuf_close = NULL,
569                 *junk_pixbuf_close = NULL, *sent_pixbuf_close = NULL,
570                 *trash_pixbuf_close = NULL, *draft_pixbuf_close = NULL,
571                 *normal_pixbuf_close = NULL, *anorm_pixbuf_close = NULL, 
572                 *ammc_pixbuf_close = NULL, *avirt_pixbuf_close = NULL;
573
574         ThreePixbufs *retval = NULL;
575
576         /* MERGE is not needed anymore as the folder now has the correct type jschmid */
577         /* We include the MERGE type here because it's used to create
578            the local OUTBOX folder */
579         if (type == TNY_FOLDER_TYPE_NORMAL || 
580             type == TNY_FOLDER_TYPE_UNKNOWN) {
581                 type = modest_tny_folder_guess_folder_type (TNY_FOLDER (instance));             
582         }
583
584         switch (type) {
585         case TNY_FOLDER_TYPE_INVALID:
586                 g_warning ("%s: BUG: TNY_FOLDER_TYPE_INVALID", __FUNCTION__);
587                 break;
588                 
589         case TNY_FOLDER_TYPE_ROOT:
590                 if (TNY_IS_ACCOUNT (instance)) {
591                         
592                         if (modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (instance))) {
593                                 retval = get_composite_icons (MODEST_FOLDER_ICON_LOCAL_FOLDERS,
594                                                               &avirt_pixbuf,
595                                                               &avirt_pixbuf_open,
596                                                               &avirt_pixbuf_close);
597                         } else {
598                                 const gchar *account_id = tny_account_get_id (TNY_ACCOUNT (instance));
599                                 
600                                 if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID)) {
601                                         retval = get_composite_icons (MODEST_FOLDER_ICON_MMC,
602                                                                       &ammc_pixbuf,
603                                                                       &ammc_pixbuf_open,
604                                                                       &ammc_pixbuf_close);
605                                 } else {
606                                         retval = get_composite_icons (MODEST_FOLDER_ICON_ACCOUNT,
607                                                                       &anorm_pixbuf,
608                                                                       &anorm_pixbuf_open,
609                                                                       &anorm_pixbuf_close);
610                                 }
611                         }
612                 }
613                 break;
614         case TNY_FOLDER_TYPE_INBOX:
615                 retval = get_composite_icons (MODEST_FOLDER_ICON_INBOX,
616                                               &inbox_pixbuf,
617                                               &inbox_pixbuf_open,
618                                               &inbox_pixbuf_close);
619                 break;
620         case TNY_FOLDER_TYPE_OUTBOX:
621                 retval = get_composite_icons (MODEST_FOLDER_ICON_OUTBOX,
622                                               &outbox_pixbuf,
623                                               &outbox_pixbuf_open,
624                                               &outbox_pixbuf_close);
625                 break;
626         case TNY_FOLDER_TYPE_JUNK:
627                 retval = get_composite_icons (MODEST_FOLDER_ICON_JUNK,
628                                               &junk_pixbuf,
629                                               &junk_pixbuf_open,
630                                               &junk_pixbuf_close);          
631                 break;
632         case TNY_FOLDER_TYPE_SENT:
633                 retval = get_composite_icons (MODEST_FOLDER_ICON_SENT,
634                                               &sent_pixbuf,
635                                               &sent_pixbuf_open,
636                                               &sent_pixbuf_close);          
637                 break;
638         case TNY_FOLDER_TYPE_TRASH:
639                 retval = get_composite_icons (MODEST_FOLDER_ICON_TRASH,
640                                               &trash_pixbuf,
641                                               &trash_pixbuf_open,
642                                               &trash_pixbuf_close);         
643                 break;
644         case TNY_FOLDER_TYPE_DRAFTS:
645                 retval = get_composite_icons (MODEST_FOLDER_ICON_DRAFTS,
646                                               &draft_pixbuf,
647                                               &draft_pixbuf_open,
648                                               &draft_pixbuf_close);         
649                 break;  
650         case TNY_FOLDER_TYPE_NORMAL:
651         default:
652                 retval = get_composite_icons (MODEST_FOLDER_ICON_NORMAL,
653                                               &normal_pixbuf,
654                                               &normal_pixbuf_open,
655                                               &normal_pixbuf_close);
656                 break;  
657         }
658
659         return retval;
660 }
661
662 static void
663 free_pixbufs (ThreePixbufs *pixbufs)
664 {
665         if (pixbufs->pixbuf)
666                 g_object_unref (pixbufs->pixbuf);
667         if (pixbufs->pixbuf_open)
668                 g_object_unref (pixbufs->pixbuf_open);
669         if (pixbufs->pixbuf_close)
670                 g_object_unref (pixbufs->pixbuf_close);
671         g_slice_free (ThreePixbufs, pixbufs);
672 }
673
674 static void
675 icon_cell_data  (GtkTreeViewColumn *column,  
676                  GtkCellRenderer *renderer,
677                  GtkTreeModel *tree_model,  
678                  GtkTreeIter *iter, 
679                  gpointer data)
680 {
681         GObject *rendobj = NULL, *instance = NULL;
682         TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
683         gboolean has_children;
684         ThreePixbufs *pixbufs;
685
686         rendobj = (GObject *) renderer;
687
688         gtk_tree_model_get (tree_model, iter,
689                             TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
690                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
691                             -1);
692
693         if (!instance) 
694                 return;
695
696         has_children = gtk_tree_model_iter_has_child (tree_model, iter);
697         pixbufs = get_folder_icons (type, instance);    
698         g_object_unref (instance);
699
700         /* Set pixbuf */
701         g_object_set (rendobj, "pixbuf", pixbufs->pixbuf, NULL);
702
703         if (has_children) {
704                 g_object_set (rendobj, "pixbuf-expander-open", pixbufs->pixbuf_open, NULL);
705                 g_object_set (rendobj, "pixbuf-expander-closed", pixbufs->pixbuf_close, NULL);
706         }
707
708         free_pixbufs (pixbufs);
709
710         return;
711 }
712
713 static void
714 add_columns (GtkWidget *treeview)
715 {
716         GtkTreeViewColumn *column;
717         GtkCellRenderer *renderer;
718         GtkTreeSelection *sel;
719
720         /* Create column */
721         column = gtk_tree_view_column_new ();   
722         
723         /* Set icon and text render function */
724         renderer = gtk_cell_renderer_pixbuf_new();
725         gtk_tree_view_column_pack_start (column, renderer, FALSE);
726         gtk_tree_view_column_set_cell_data_func(column, renderer,
727                                                 icon_cell_data, treeview, NULL);
728         
729         renderer = gtk_cell_renderer_text_new();
730         g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, 
731                         "ellipsize-set", TRUE, NULL);
732         gtk_tree_view_column_pack_start (column, renderer, TRUE);
733         gtk_tree_view_column_set_cell_data_func(column, renderer,
734                                                 text_cell_data, treeview, NULL);
735         
736         /* Set selection mode */
737         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(treeview));
738         gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
739
740         /* Set treeview appearance */
741         gtk_tree_view_column_set_spacing (column, 2);
742         gtk_tree_view_column_set_resizable (column, TRUE);
743         gtk_tree_view_column_set_fixed_width (column, TRUE);            
744         gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(treeview), FALSE);
745         gtk_tree_view_set_enable_search (GTK_TREE_VIEW(treeview), FALSE);
746
747         /* Add column */
748         gtk_tree_view_append_column (GTK_TREE_VIEW(treeview),column);
749 }
750
751 static void
752 modest_folder_view_init (ModestFolderView *obj)
753 {
754         ModestFolderViewPrivate *priv;
755         ModestConf *conf;
756         
757         priv =  MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
758         
759         priv->timer_expander = 0;
760         priv->account_store  = NULL;
761         priv->query          = NULL;
762         priv->style          = MODEST_FOLDER_VIEW_STYLE_SHOW_ALL;
763         priv->cur_folder_store   = NULL;
764         priv->visible_account_id = NULL;
765         priv->folder_to_select = NULL;
766
767         priv->reexpand = TRUE;
768
769         /* Initialize the local account name */
770         conf = modest_runtime_get_conf();
771         priv->local_account_name = modest_conf_get_string (conf, MODEST_CONF_DEVICE_NAME, NULL);
772
773         /* Init email clipboard */
774         priv->clipboard = modest_runtime_get_email_clipboard ();
775         priv->hidding_ids = NULL;
776         priv->n_selected = 0;
777         priv->reselect = FALSE;
778         priv->show_non_move = TRUE;
779
780         /* Build treeview */
781         add_columns (GTK_WIDGET (obj));
782
783         /* Setup drag and drop */
784         setup_drag_and_drop (GTK_TREE_VIEW(obj));
785
786         /* Connect signals */
787         g_signal_connect (G_OBJECT (obj), 
788                           "key-press-event", 
789                           G_CALLBACK (on_key_pressed), NULL);
790
791         priv->display_name_changed_signal = 
792                 g_signal_connect (modest_runtime_get_account_mgr (),
793                                   "display_name_changed",
794                                   G_CALLBACK (on_display_name_changed),
795                                   obj);
796
797         /*
798          * Track changes in the local account name (in the device it
799          * will be the device name)
800          */
801         priv->conf_key_signal = g_signal_connect (G_OBJECT(conf), 
802                                                   "key_changed",
803                                                   G_CALLBACK(on_configuration_key_changed), 
804                                                   obj);
805 }
806
807 static void
808 tny_account_store_view_init (gpointer g, gpointer iface_data)
809 {
810         TnyAccountStoreViewIface *klass = (TnyAccountStoreViewIface *)g;
811
812         klass->set_account_store = modest_folder_view_set_account_store;
813 }
814
815 static void
816 modest_folder_view_finalize (GObject *obj)
817 {
818         ModestFolderViewPrivate *priv;
819         GtkTreeSelection    *sel;
820         
821         g_return_if_fail (obj);
822         
823         priv =  MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
824
825         if (priv->timer_expander != 0) {
826                 g_source_remove (priv->timer_expander);
827                 priv->timer_expander = 0;
828         }
829
830         if (priv->account_store) {
831                 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
832                                              priv->account_inserted_signal);
833                 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
834                                              priv->account_removed_signal);
835                 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
836                                              priv->account_changed_signal);
837                 g_object_unref (G_OBJECT(priv->account_store));
838                 priv->account_store = NULL;
839         }
840
841         if (priv->query) {
842                 g_object_unref (G_OBJECT (priv->query));
843                 priv->query = NULL;
844         }
845
846         if (priv->folder_to_select) {
847                 g_object_unref (G_OBJECT(priv->folder_to_select));
848                 priv->folder_to_select = NULL;
849         }
850    
851         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(obj));
852         if (sel)
853                 g_signal_handler_disconnect (G_OBJECT(sel), priv->changed_signal);
854
855         g_free (priv->local_account_name);
856         g_free (priv->visible_account_id);
857         
858         if (priv->conf_key_signal) {
859                 g_signal_handler_disconnect (modest_runtime_get_conf (),
860                                              priv->conf_key_signal);
861                 priv->conf_key_signal = 0;
862         }
863
864         if (priv->cur_folder_store) {
865                 g_object_unref (priv->cur_folder_store);
866                 priv->cur_folder_store = NULL;
867         }
868
869         /* Clear hidding array created by cut operation */
870         _clear_hidding_filter (MODEST_FOLDER_VIEW (obj));
871
872         G_OBJECT_CLASS(parent_class)->finalize (obj);
873 }
874
875
876 static void
877 modest_folder_view_set_account_store (TnyAccountStoreView *self, TnyAccountStore *account_store)
878 {
879         ModestFolderViewPrivate *priv;
880         TnyDevice *device;
881
882         g_return_if_fail (MODEST_IS_FOLDER_VIEW (self));
883         g_return_if_fail (TNY_IS_ACCOUNT_STORE (account_store));
884
885         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
886         device = tny_account_store_get_device (account_store);
887
888         if (G_UNLIKELY (priv->account_store)) {
889
890                 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store),
891                                                    priv->account_inserted_signal))
892                         g_signal_handler_disconnect (G_OBJECT (priv->account_store),
893                                                      priv->account_inserted_signal);
894                 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store), 
895                                                    priv->account_removed_signal))
896                         g_signal_handler_disconnect (G_OBJECT (priv->account_store), 
897                                                      priv->account_removed_signal);
898                 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store), 
899                                                    priv->account_changed_signal))
900                         g_signal_handler_disconnect (G_OBJECT (priv->account_store), 
901                                                      priv->account_changed_signal);
902                 g_object_unref (G_OBJECT (priv->account_store));
903         }
904
905         priv->account_store = g_object_ref (G_OBJECT (account_store));
906
907         priv->account_removed_signal = 
908                 g_signal_connect (G_OBJECT(account_store), "account_removed",
909                                   G_CALLBACK (on_account_removed), self);
910
911         priv->account_inserted_signal =
912                 g_signal_connect (G_OBJECT(account_store), "account_inserted",
913                                   G_CALLBACK (on_account_inserted), self);
914
915         priv->account_changed_signal =
916                 g_signal_connect (G_OBJECT(account_store), "account_changed",
917                                   G_CALLBACK (on_account_changed), self);
918
919         modest_folder_view_update_model (MODEST_FOLDER_VIEW (self), account_store);
920         priv->reselect = FALSE;
921         modest_folder_view_select_first_inbox_or_local (MODEST_FOLDER_VIEW (self));
922         
923         g_object_unref (G_OBJECT (device));
924 }
925
926 static void
927 on_account_inserted (TnyAccountStore *account_store, 
928                      TnyAccount *account,
929                      gpointer user_data)
930 {
931         ModestFolderViewPrivate *priv;
932         GtkTreeModel *sort_model, *filter_model;
933
934         /* Ignore transport account insertions, we're not showing them
935            in the folder view */
936         if (TNY_IS_TRANSPORT_ACCOUNT (account))
937                 return;
938
939         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (user_data);
940
941
942         /* If we're adding a new account, and there is no previous
943            one, we need to select the visible server account */
944         if (priv->style == MODEST_FOLDER_VIEW_STYLE_SHOW_ONE &&
945             !priv->visible_account_id)
946                 modest_widget_memory_restore (modest_runtime_get_conf(), 
947                                               G_OBJECT (user_data),
948                                               MODEST_CONF_FOLDER_VIEW_KEY);
949
950         if (!GTK_IS_TREE_VIEW(user_data)) {
951                 g_warning ("BUG: %s: not a valid tree view", __FUNCTION__);
952                 return;
953         }
954         
955         /* Get the inner model */
956         /* check, is some rare cases, we did not get the right thing here,
957          * NB#84097 */
958         filter_model = gtk_tree_view_get_model (GTK_TREE_VIEW (user_data));
959         if (!GTK_IS_TREE_MODEL_FILTER(filter_model)) {
960                 g_warning ("BUG: %s: not a valid filter model", __FUNCTION__);
961                 return;
962         }
963
964         /* check, is some rare cases, we did not get the right thing here,
965          * NB#84097 */
966         sort_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (filter_model));
967         if (!GTK_IS_TREE_MODEL_SORT(sort_model)) {
968                 g_warning ("BUG: %s: not a valid sort model", __FUNCTION__);
969                 return;
970         }
971
972         /* Insert the account in the model */
973         tny_list_append (TNY_LIST (gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (sort_model))),
974                          G_OBJECT (account));
975         
976         /* Refilter the model */
977         gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (filter_model));
978 }
979
980
981 static gboolean
982 same_account_selected (ModestFolderView *self,
983                        TnyAccount *account)
984 {
985         ModestFolderViewPrivate *priv;
986         gboolean same_account = FALSE;
987
988         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
989
990         if (priv->cur_folder_store) {
991                 TnyAccount *selected_folder_account = NULL;
992
993                 if (TNY_IS_FOLDER (priv->cur_folder_store)) {
994                         selected_folder_account = 
995                                 tny_folder_get_account (TNY_FOLDER (priv->cur_folder_store));
996                 } else {
997                         selected_folder_account = 
998                                 TNY_ACCOUNT (g_object_ref (priv->cur_folder_store));
999                 }
1000
1001                 if (selected_folder_account == account)
1002                         same_account = TRUE;
1003
1004                 g_object_unref (selected_folder_account);
1005         }
1006         return same_account;
1007 }
1008
1009 /**
1010  *
1011  * Selects the first inbox or the local account in an idle
1012  */
1013 static gboolean
1014 on_idle_select_first_inbox_or_local (gpointer user_data)
1015 {
1016         ModestFolderView *self = MODEST_FOLDER_VIEW (user_data);
1017
1018         gdk_threads_enter ();
1019         modest_folder_view_select_first_inbox_or_local (self);
1020         gdk_threads_leave ();
1021
1022         return FALSE;
1023 }
1024
1025 static void
1026 on_account_changed (TnyAccountStore *account_store, 
1027                     TnyAccount *tny_account,
1028                     gpointer user_data)
1029 {
1030         ModestFolderView *self;
1031         ModestFolderViewPrivate *priv;
1032         GtkTreeModel *sort_model, *filter_model;
1033         GtkTreeSelection *sel;
1034         gboolean same_account;
1035
1036         /* Ignore transport account insertions, we're not showing them
1037            in the folder view */
1038         if (TNY_IS_TRANSPORT_ACCOUNT (tny_account))
1039                 return;
1040
1041         if (!MODEST_IS_FOLDER_VIEW(user_data)) {
1042                 g_warning ("BUG: %s: not a valid folder view", __FUNCTION__);
1043                 return;
1044         }
1045
1046         self = MODEST_FOLDER_VIEW (user_data);
1047         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (user_data);
1048
1049         /* Get the inner model */
1050         filter_model = gtk_tree_view_get_model (GTK_TREE_VIEW (user_data));
1051         if (!GTK_IS_TREE_MODEL_FILTER(filter_model)) {
1052                 g_warning ("BUG: %s: not a valid filter model", __FUNCTION__);
1053                 return;
1054         }
1055
1056         sort_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (filter_model));
1057         if (!GTK_IS_TREE_MODEL_SORT(sort_model)) {
1058                 g_warning ("BUG: %s: not a valid sort model", __FUNCTION__);
1059                 return;
1060         }
1061
1062         /* Invalidate the cur_folder_store only if the selected folder
1063            belongs to the account that is being removed */
1064         same_account = same_account_selected (self, tny_account);
1065         if (same_account) {
1066                 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
1067                 gtk_tree_selection_unselect_all (sel);
1068         }
1069
1070         /* Remove the account from the model */
1071         tny_list_remove (TNY_LIST (gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (sort_model))),
1072                          G_OBJECT (tny_account));
1073         
1074         /* Insert the account in the model */
1075         tny_list_append (TNY_LIST (gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (sort_model))),
1076                          G_OBJECT (tny_account));
1077
1078         /* Refilter the model */
1079         gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (filter_model));
1080
1081         /* Select the first INBOX if the currently selected folder
1082            belongs to the account that is being deleted */
1083         if (same_account)
1084                 g_idle_add (on_idle_select_first_inbox_or_local, self);
1085 }
1086
1087 static void
1088 on_account_removed (TnyAccountStore *account_store, 
1089                     TnyAccount *account,
1090                     gpointer user_data)
1091 {
1092         ModestFolderView *self = NULL;
1093         ModestFolderViewPrivate *priv;
1094         GtkTreeModel *sort_model, *filter_model;
1095         GtkTreeSelection *sel = NULL;
1096         gboolean same_account = FALSE;
1097
1098         /* Ignore transport account removals, we're not showing them
1099            in the folder view */
1100         if (TNY_IS_TRANSPORT_ACCOUNT (account))
1101                 return;
1102
1103         if (!MODEST_IS_FOLDER_VIEW(user_data)) {
1104                 g_warning ("BUG: %s: not a valid folder view", __FUNCTION__);
1105                 return;
1106         }
1107
1108         self = MODEST_FOLDER_VIEW (user_data);
1109         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
1110
1111         /* Invalidate the cur_folder_store only if the selected folder
1112            belongs to the account that is being removed */
1113         same_account = same_account_selected (self, account);
1114         if (same_account) {
1115                 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
1116                 gtk_tree_selection_unselect_all (sel);
1117         }
1118
1119         /* Invalidate row to select only if the folder to select
1120            belongs to the account that is being removed*/
1121         if (priv->folder_to_select) {
1122                 TnyAccount *folder_to_select_account = NULL;
1123
1124                 folder_to_select_account = tny_folder_get_account (priv->folder_to_select);
1125                 if (folder_to_select_account == account) {
1126                         modest_folder_view_disable_next_folder_selection (self);
1127                         g_object_unref (priv->folder_to_select);
1128                         priv->folder_to_select = NULL;
1129                 }
1130                 g_object_unref (folder_to_select_account);
1131         }
1132
1133         /* Remove the account from the model */
1134         filter_model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
1135         if (!GTK_IS_TREE_MODEL_FILTER(filter_model)) {
1136                 g_warning ("BUG: %s: not a valid filter model", __FUNCTION__);
1137                 return;
1138         }
1139
1140         sort_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (filter_model));
1141         if (!GTK_IS_TREE_MODEL_SORT(sort_model)) {
1142                 g_warning ("BUG: %s: not a valid sort model", __FUNCTION__);
1143                 return;
1144         }
1145         
1146         tny_list_remove (TNY_LIST (gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (sort_model))),
1147                          G_OBJECT (account));
1148
1149         /* If the removed account is the currently viewed one then
1150            clear the configuration value. The new visible account will be the default account */
1151         if (priv->visible_account_id &&
1152             !strcmp (priv->visible_account_id, tny_account_get_id (account))) {
1153
1154                 /* Clear the current visible account_id */
1155                 modest_folder_view_set_account_id_of_visible_server_account (self, NULL);
1156
1157                 /* Call the restore method, this will set the new visible account */
1158                 modest_widget_memory_restore (modest_runtime_get_conf(), G_OBJECT(self),
1159                                               MODEST_CONF_FOLDER_VIEW_KEY);
1160         }
1161
1162         /* Refilter the model */
1163         gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (filter_model));
1164
1165         /* Select the first INBOX if the currently selected folder
1166            belongs to the account that is being deleted */
1167         if (same_account)
1168                 g_idle_add (on_idle_select_first_inbox_or_local, self);
1169 }
1170
1171 void
1172 modest_folder_view_set_title (ModestFolderView *self, const gchar *title)
1173 {
1174         GtkTreeViewColumn *col;
1175         
1176         g_return_if_fail (self && MODEST_IS_FOLDER_VIEW(self));
1177
1178         col = gtk_tree_view_get_column (GTK_TREE_VIEW(self), 0);
1179         if (!col) {
1180                 g_printerr ("modest: failed get column for title\n");
1181                 return;
1182         }
1183
1184         gtk_tree_view_column_set_title (col, title);
1185         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self),
1186                                            title != NULL);
1187 }
1188
1189 static gboolean
1190 modest_folder_view_on_map (ModestFolderView *self, 
1191                            GdkEventExpose *event,
1192                            gpointer data)
1193 {
1194         ModestFolderViewPrivate *priv;
1195
1196         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
1197
1198         /* This won't happen often */
1199         if (G_UNLIKELY (priv->reselect)) {
1200                 /* Select the first inbox or the local account if not found */
1201
1202                 /* TODO: this could cause a lock at startup, so we
1203                    comment it for the moment. We know that this will
1204                    be a bug, because the INBOX is not selected, but we
1205                    need to rewrite some parts of Modest to avoid the
1206                    deathlock situation */
1207                 /* TODO: check if this is still the case */
1208                 priv->reselect = FALSE;
1209                 modest_folder_view_select_first_inbox_or_local (self);
1210                 /* Notify the display name observers */
1211                 g_signal_emit (G_OBJECT(self),
1212                                signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
1213                                NULL);
1214         }
1215
1216         if (priv->reexpand) {
1217                 expand_root_items (self); 
1218                 priv->reexpand = FALSE;
1219         }
1220
1221         return FALSE;
1222 }
1223
1224 GtkWidget*
1225 modest_folder_view_new (TnyFolderStoreQuery *query)
1226 {
1227         GObject *self;
1228         ModestFolderViewPrivate *priv;
1229         GtkTreeSelection *sel;
1230         
1231         self = G_OBJECT (g_object_new (MODEST_TYPE_FOLDER_VIEW, NULL));
1232         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
1233
1234         if (query)
1235                 priv->query = g_object_ref (query);
1236         
1237         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
1238         priv->changed_signal = g_signal_connect (sel, "changed",
1239                                                  G_CALLBACK (on_selection_changed), self);
1240
1241         g_signal_connect (self, "expose-event", G_CALLBACK (modest_folder_view_on_map), NULL);
1242
1243         return GTK_WIDGET(self);
1244 }
1245
1246 /* this feels dirty; any other way to expand all the root items? */
1247 static void
1248 expand_root_items (ModestFolderView *self)
1249 {
1250         GtkTreePath *path;
1251         GtkTreeModel *model;
1252         GtkTreeIter iter;
1253
1254         model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
1255         path = gtk_tree_path_new_first ();
1256
1257         /* all folders should have child items, so.. */
1258         do {
1259                 gtk_tree_view_expand_row (GTK_TREE_VIEW(self), path, FALSE);
1260                 gtk_tree_path_next (path);
1261         } while (gtk_tree_model_get_iter (model, &iter, path));
1262         
1263         gtk_tree_path_free (path);
1264 }
1265
1266 /*
1267  * We use this function to implement the
1268  * MODEST_FOLDER_VIEW_STYLE_SHOW_ONE style. We only show the default
1269  * account in this case, and the local folders.
1270  */
1271 static gboolean 
1272 filter_row (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
1273 {
1274         ModestFolderViewPrivate *priv;
1275         gboolean retval = TRUE;
1276         TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
1277         GObject *instance = NULL;
1278         const gchar *id = NULL;
1279         guint i;
1280         gboolean found = FALSE;
1281         gboolean cleared = FALSE;
1282
1283         g_return_val_if_fail (MODEST_IS_FOLDER_VIEW (data), FALSE);
1284         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (data);
1285
1286         gtk_tree_model_get (model, iter,
1287                             TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
1288                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
1289                             -1);
1290
1291         /* Do not show if there is no instance, this could indeed
1292            happen when the model is being modified while it's being
1293            drawn. This could occur for example when moving folders
1294            using drag&drop */
1295         if (!instance)
1296                 return FALSE;
1297
1298         if (type == TNY_FOLDER_TYPE_ROOT) {
1299                 /* TNY_FOLDER_TYPE_ROOT means that the instance is an
1300                    account instead of a folder. */
1301                 if (TNY_IS_ACCOUNT (instance)) {
1302                         TnyAccount *acc = TNY_ACCOUNT (instance);
1303                         const gchar *account_id = tny_account_get_id (acc);
1304         
1305                         /* If it isn't a special folder, 
1306                          * don't show it unless it is the visible account: */
1307                         if (priv->style == MODEST_FOLDER_VIEW_STYLE_SHOW_ONE &&
1308                             !modest_tny_account_is_virtual_local_folders (acc) &&
1309                             strcmp (account_id, MODEST_MMC_ACCOUNT_ID)) {
1310                                 
1311                                 /* Show only the visible account id */
1312                                 if (priv->visible_account_id) {
1313                                         if (strcmp (account_id, priv->visible_account_id))
1314                                                 retval = FALSE;
1315                                 } else {
1316                                         retval = FALSE;
1317                                 }                               
1318                         }
1319                         
1320                         /* Never show these to the user. They are merged into one folder 
1321                          * in the local-folders account instead: */
1322                         if (retval && MODEST_IS_TNY_OUTBOX_ACCOUNT (acc))
1323                                 retval = FALSE;
1324                 }
1325         }
1326
1327         /* Check hiding (if necessary) */
1328         cleared = modest_email_clipboard_cleared (priv->clipboard);            
1329         if ((retval) && (!cleared) && (TNY_IS_FOLDER (instance))) {
1330                 id = tny_folder_get_id (TNY_FOLDER(instance));
1331                 if (priv->hidding_ids != NULL)
1332                         for (i=0; i < priv->n_selected && !found; i++)
1333                                 if (priv->hidding_ids[i] != NULL && id != NULL)
1334                                         found = (!strcmp (priv->hidding_ids[i], id));
1335                 
1336                 retval = !found;
1337         }
1338         
1339         
1340         /* If this is a move to dialog, hide Sent, Outbox and Drafts
1341         folder as no message can be move there according to UI specs */
1342         if (!priv->show_non_move) {
1343                 switch (type) {
1344                         case TNY_FOLDER_TYPE_OUTBOX:
1345                         case TNY_FOLDER_TYPE_SENT:
1346                         case TNY_FOLDER_TYPE_DRAFTS:
1347                                 retval = FALSE;
1348                                 break;
1349                         case TNY_FOLDER_TYPE_UNKNOWN:
1350                         case TNY_FOLDER_TYPE_NORMAL:
1351                                 type = modest_tny_folder_guess_folder_type(TNY_FOLDER(instance));
1352                                 if (type == TNY_FOLDER_TYPE_INVALID)
1353                                         g_warning ("%s: BUG: TNY_FOLDER_TYPE_INVALID", __FUNCTION__);
1354                                 
1355                                 if (type == TNY_FOLDER_TYPE_OUTBOX || 
1356                                     type == TNY_FOLDER_TYPE_SENT
1357                                     || type == TNY_FOLDER_TYPE_DRAFTS)
1358                                         retval = FALSE;
1359                                 break;
1360                         default:
1361                                 break;
1362                 }
1363         }
1364         
1365         /* Free */
1366         g_object_unref (instance);
1367
1368         return retval;
1369 }
1370
1371
1372 gboolean
1373 modest_folder_view_update_model (ModestFolderView *self,
1374                                  TnyAccountStore *account_store)
1375 {
1376         ModestFolderViewPrivate *priv;
1377         GtkTreeModel *model /* , *old_model */;                                                    
1378         GtkTreeModel *filter_model = NULL, *sortable = NULL;
1379
1380         g_return_val_if_fail (self && MODEST_IS_FOLDER_VIEW (self), FALSE);
1381         g_return_val_if_fail (account_store && MODEST_IS_TNY_ACCOUNT_STORE(account_store),
1382                               FALSE);
1383         
1384         priv =  MODEST_FOLDER_VIEW_GET_PRIVATE(self);
1385         
1386         /* Notify that there is no folder selected */
1387         g_signal_emit (G_OBJECT(self),
1388                        signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
1389                        NULL, FALSE);
1390         if (priv->cur_folder_store) {
1391                 g_object_unref (priv->cur_folder_store);
1392                 priv->cur_folder_store = NULL;
1393         }
1394
1395         /* FIXME: the local accounts are not shown when the query
1396            selects only the subscribed folders */
1397         model = tny_gtk_folder_store_tree_model_new (NULL);
1398
1399         /* Get the accounts: */
1400         tny_account_store_get_accounts (TNY_ACCOUNT_STORE(account_store),
1401                                         TNY_LIST (model),
1402                                         TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
1403
1404         sortable = gtk_tree_model_sort_new_with_model (model);
1405         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(sortable),
1406                                               TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, 
1407                                               GTK_SORT_ASCENDING);
1408         gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (sortable),
1409                                          TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
1410                                          cmp_rows, NULL, NULL);
1411
1412         /* Create filter model */
1413         filter_model = gtk_tree_model_filter_new (sortable, NULL);
1414         gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (filter_model),
1415                                                 filter_row,
1416                                                 self,
1417                                                 NULL);
1418
1419         /* Set new model */
1420         gtk_tree_view_set_model (GTK_TREE_VIEW(self), filter_model);
1421         g_signal_connect (G_OBJECT(filter_model), "row-inserted",
1422                           (GCallback) on_row_inserted_maybe_select_folder, self);
1423
1424         g_object_unref (model);
1425         g_object_unref (filter_model);          
1426         g_object_unref (sortable);
1427         
1428         /* Force a reselection of the INBOX next time the widget is shown */
1429         priv->reselect = TRUE;
1430                         
1431         return TRUE;
1432 }
1433
1434
1435 static void
1436 on_selection_changed (GtkTreeSelection *sel, gpointer user_data)
1437 {
1438         GtkTreeModel *model = NULL;
1439         TnyFolderStore *folder = NULL;
1440         GtkTreeIter iter;
1441         ModestFolderView *tree_view = NULL;
1442         ModestFolderViewPrivate *priv = NULL;
1443         gboolean selected = FALSE;
1444
1445         g_return_if_fail (sel);
1446         g_return_if_fail (user_data);
1447         
1448         priv = MODEST_FOLDER_VIEW_GET_PRIVATE(user_data);
1449
1450         selected = gtk_tree_selection_get_selected (sel, &model, &iter);
1451
1452         tree_view = MODEST_FOLDER_VIEW (user_data);
1453
1454         if (selected) {
1455                 gtk_tree_model_get (model, &iter,
1456                                     TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder,
1457                                     -1);
1458
1459                 /* If the folder is the same do not notify */
1460                 if (folder && priv->cur_folder_store == folder) {
1461                         g_object_unref (folder);
1462                         return;
1463                 }
1464         }
1465         
1466         /* Current folder was unselected */
1467         if (priv->cur_folder_store) {
1468                 g_signal_emit (G_OBJECT(tree_view), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
1469                        priv->cur_folder_store, FALSE);
1470
1471                 if (TNY_IS_FOLDER(priv->cur_folder_store))
1472                         tny_folder_sync_async (TNY_FOLDER(priv->cur_folder_store),
1473                                                FALSE, NULL, NULL, NULL);
1474
1475                 /* FALSE --> don't expunge the messages */
1476
1477                 g_object_unref (priv->cur_folder_store);
1478                 priv->cur_folder_store = NULL;
1479         }
1480
1481         /* New current references */
1482         priv->cur_folder_store = folder;
1483
1484         /* New folder has been selected. Do not notify if there is
1485            nothing new selected */
1486         if (selected) {
1487                 g_signal_emit (G_OBJECT(tree_view),
1488                                signals[FOLDER_SELECTION_CHANGED_SIGNAL],
1489                                0, priv->cur_folder_store, TRUE);
1490         }
1491 }
1492
1493 TnyFolderStore *
1494 modest_folder_view_get_selected (ModestFolderView *self)
1495 {
1496         ModestFolderViewPrivate *priv;
1497         
1498         g_return_val_if_fail (self && MODEST_IS_FOLDER_VIEW(self), NULL);
1499         
1500         priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
1501         if (priv->cur_folder_store)
1502                 g_object_ref (priv->cur_folder_store);
1503
1504         return priv->cur_folder_store;
1505 }
1506
1507 static gint
1508 get_cmp_rows_type_pos (GObject *folder)
1509 {
1510         /* Remote accounts -> Local account -> MMC account .*/
1511         /* 0, 1, 2 */
1512         
1513         if (TNY_IS_ACCOUNT (folder) && 
1514                 modest_tny_account_is_virtual_local_folders (
1515                         TNY_ACCOUNT (folder))) {
1516                 return 1;
1517         } else if (TNY_IS_ACCOUNT (folder)) {
1518                 TnyAccount *account = TNY_ACCOUNT (folder);
1519                 const gchar *account_id = tny_account_get_id (account);
1520                 if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID))
1521                         return 2;
1522                 else
1523                         return 0;
1524         }
1525         else {
1526                 printf ("DEBUG: %s: unexpected type.\n", __FUNCTION__);
1527                 return -1; /* Should never happen */
1528         }
1529 }
1530
1531 static gint
1532 get_cmp_subfolder_type_pos (TnyFolderType t)
1533 {
1534         /* Inbox, Outbox, Drafts, Sent, User */
1535         /* 0, 1, 2, 3, 4 */
1536
1537         switch (t) {
1538         case TNY_FOLDER_TYPE_INBOX:
1539                 return 0;
1540                 break;
1541         case TNY_FOLDER_TYPE_OUTBOX:
1542                 return 1;
1543                 break;
1544         case TNY_FOLDER_TYPE_DRAFTS:
1545                 return 2;
1546                 break;
1547         case TNY_FOLDER_TYPE_SENT:
1548                 return 3;
1549                 break;
1550         default:
1551                 return 4;
1552         }
1553 }
1554
1555 /*
1556  * This function orders the mail accounts according to these rules:
1557  * 1st - remote accounts
1558  * 2nd - local account
1559  * 3rd - MMC account
1560  */
1561 static gint
1562 cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2,
1563           gpointer user_data)
1564 {
1565         gint cmp = 0;
1566         gchar *name1 = NULL;
1567         gchar *name2 = NULL;
1568         TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
1569         TnyFolderType type2 = TNY_FOLDER_TYPE_UNKNOWN;
1570         GObject *folder1 = NULL;
1571         GObject *folder2 = NULL;
1572
1573         gtk_tree_model_get (tree_model, iter1,
1574                             TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name1,
1575                             TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
1576                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder1,
1577                             -1);
1578         gtk_tree_model_get (tree_model, iter2,
1579                             TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name2,
1580                             TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type2,
1581                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder2,
1582                             -1);
1583
1584         /* Return if we get no folder. This could happen when folder
1585            operations are happening. The model is updated after the
1586            folder copy/move actually occurs, so there could be
1587            situations where the model to be drawn is not correct */
1588         if (!folder1 || !folder2)
1589                 goto finish;
1590
1591         if (type == TNY_FOLDER_TYPE_ROOT) {
1592                 /* Compare the types, so that 
1593                  * Remote accounts -> Local account -> MMC account .*/
1594                 const gint pos1 = get_cmp_rows_type_pos (folder1);
1595                 const gint pos2 = get_cmp_rows_type_pos (folder2);
1596                 /* printf ("DEBUG: %s:\n  type1=%s, pos1=%d\n  type2=%s, pos2=%d\n", 
1597                         __FUNCTION__, G_OBJECT_TYPE_NAME(folder1), pos1, G_OBJECT_TYPE_NAME(folder2), pos2); */
1598                 if (pos1 <  pos2)
1599                         cmp = -1;
1600                 else if (pos1 > pos2)
1601                         cmp = 1;
1602                 else {
1603                         /* Compare items of the same type: */
1604                         
1605                         TnyAccount *account1 = NULL;
1606                         if (TNY_IS_ACCOUNT (folder1))
1607                                 account1 = TNY_ACCOUNT (folder1);
1608                                 
1609                         TnyAccount *account2 = NULL;
1610                         if (TNY_IS_ACCOUNT (folder2))
1611                                 account2 = TNY_ACCOUNT (folder2);
1612                                 
1613                         const gchar *account_id = account1 ? tny_account_get_id (account1) : NULL;
1614                         const gchar *account_id2 = account2 ? tny_account_get_id (account2) : NULL;
1615         
1616                         if (!account_id && !account_id2) {
1617                                 cmp = 0;
1618                         } else if (!account_id) {
1619                                 cmp = -1;
1620                         } else if (!account_id2) {
1621                                 cmp = +1;
1622                         } else if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID)) {
1623                                 cmp = +1;
1624                         } else {
1625                                 cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
1626                         }
1627                 }
1628         } else {
1629                 gint cmp1 = 0, cmp2 = 0;
1630                 /* get the parent to know if it's a local folder */
1631
1632                 GtkTreeIter parent;
1633                 gboolean has_parent;
1634                 has_parent = gtk_tree_model_iter_parent (tree_model, &parent, iter1);
1635                 if (has_parent) {
1636                         GObject *parent_folder;
1637                         TnyFolderType parent_type = TNY_FOLDER_TYPE_UNKNOWN;
1638                         gtk_tree_model_get (tree_model, &parent, 
1639                                             TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &parent_type,
1640                                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &parent_folder,
1641                                             -1);
1642                         if ((parent_type == TNY_FOLDER_TYPE_ROOT) &&
1643                             TNY_IS_ACCOUNT (parent_folder)) {
1644                                 if (modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (parent_folder))) {
1645                                         cmp1 = get_cmp_subfolder_type_pos (modest_tny_folder_get_local_or_mmc_folder_type
1646                                                                            (TNY_FOLDER (folder1)));
1647                                         cmp2 = get_cmp_subfolder_type_pos (modest_tny_folder_get_local_or_mmc_folder_type
1648                                                                            (TNY_FOLDER (folder2)));
1649                                 } else if (modest_tny_account_is_memory_card_account (TNY_ACCOUNT (parent_folder))) {
1650                                         if (modest_local_folder_info_get_type (tny_folder_get_name (TNY_FOLDER (folder1))) == TNY_FOLDER_TYPE_ARCHIVE) {
1651                                                 cmp1 = 0;
1652                                                 cmp2 = 1;
1653                                                 } else if (modest_local_folder_info_get_type (tny_folder_get_name (TNY_FOLDER (folder2))) == TNY_FOLDER_TYPE_ARCHIVE) {
1654                                                 cmp1 = 1;
1655                                                 cmp2 = 0;
1656                                         }
1657                                 }
1658                         }
1659                         g_object_unref (parent_folder);
1660                 }
1661                 
1662                 /* if they are not local folders */
1663                 if (cmp1 == cmp2) {
1664                         cmp1 = get_cmp_subfolder_type_pos (tny_folder_get_folder_type (TNY_FOLDER (folder1)));
1665                         cmp2 = get_cmp_subfolder_type_pos (tny_folder_get_folder_type (TNY_FOLDER (folder2)));
1666                 }
1667
1668                 if (cmp1 == cmp2)
1669                         cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
1670                 else 
1671                         cmp = (cmp1 - cmp2);
1672         }
1673
1674 finish: 
1675         if (folder1)
1676                 g_object_unref(G_OBJECT(folder1));
1677         if (folder2)
1678                 g_object_unref(G_OBJECT(folder2));
1679
1680         g_free (name1);
1681         g_free (name2);
1682
1683         return cmp;     
1684 }
1685
1686 /*****************************************************************************/
1687 /*                        DRAG and DROP stuff                                */
1688 /*****************************************************************************/
1689 /*
1690  * This function fills the #GtkSelectionData with the row and the
1691  * model that has been dragged. It's called when this widget is a
1692  * source for dnd after the event drop happened
1693  */
1694 static void
1695 on_drag_data_get (GtkWidget *widget, GdkDragContext *context, GtkSelectionData *selection_data, 
1696                   guint info, guint time, gpointer data)
1697 {
1698         GtkTreeSelection *selection;
1699         GtkTreeModel *model;
1700         GtkTreeIter iter;
1701         GtkTreePath *source_row;
1702         
1703         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
1704         if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
1705
1706                 source_row = gtk_tree_model_get_path (model, &iter);
1707                 gtk_tree_set_row_drag_data (selection_data,
1708                                             model,
1709                                             source_row);
1710                 
1711                 gtk_tree_path_free (source_row);
1712         }
1713 }
1714
1715 typedef struct _DndHelper {
1716         ModestFolderView *folder_view;
1717         gboolean delete_source;
1718         GtkTreePath *source_row;
1719 } DndHelper;
1720
1721 static void
1722 dnd_helper_destroyer (DndHelper *helper)
1723 {
1724         /* Free the helper */
1725         gtk_tree_path_free (helper->source_row);
1726         g_slice_free (DndHelper, helper);
1727 }
1728
1729 static void
1730 xfer_folder_cb (ModestMailOperation *mail_op, 
1731                 TnyFolder *new_folder,
1732                 gpointer user_data)
1733 {
1734         if (new_folder) {       
1735                 /* Select the folder */
1736                 modest_folder_view_select_folder (MODEST_FOLDER_VIEW (user_data), 
1737                                                   new_folder, FALSE);
1738         }
1739 }
1740
1741
1742 /* get the folder for the row the treepath refers to. */
1743 /* folder must be unref'd */
1744 static TnyFolderStore *
1745 tree_path_to_folder (GtkTreeModel *model, GtkTreePath *path)
1746 {
1747         GtkTreeIter iter;
1748         TnyFolderStore *folder = NULL;
1749         
1750         if (gtk_tree_model_get_iter (model,&iter, path))
1751                 gtk_tree_model_get (model, &iter,
1752                                     TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder,
1753                                     -1);
1754         return folder;
1755 }
1756
1757
1758 /*
1759  * This function is used by drag_data_received_cb to manage drag and
1760  * drop of a header, i.e, and drag from the header view to the folder
1761  * view.
1762  */
1763 static void
1764 drag_and_drop_from_header_view (GtkTreeModel *source_model,
1765                                 GtkTreeModel *dest_model,
1766                                 GtkTreePath  *dest_row,
1767                                 GtkSelectionData *selection_data)
1768 {
1769         TnyList *headers = NULL;
1770         TnyFolder *folder = NULL, *src_folder = NULL;
1771         TnyFolderType folder_type;
1772         GtkTreeIter source_iter, dest_iter;
1773         ModestWindowMgr *mgr = NULL;
1774         ModestWindow *main_win = NULL;
1775         gchar **uris, **tmp;
1776         
1777         /* Build the list of headers */
1778         mgr = modest_runtime_get_window_mgr ();
1779         headers = tny_simple_list_new ();
1780         uris = modest_dnd_selection_data_get_paths (selection_data);
1781         tmp = uris;
1782
1783         while (*tmp != NULL) {
1784                 TnyHeader *header;
1785                 GtkTreePath *path;
1786                 gboolean first = TRUE;
1787
1788                 /* Get header */
1789                 path = gtk_tree_path_new_from_string (*tmp);
1790                 gtk_tree_model_get_iter (source_model, &source_iter, path);
1791                 gtk_tree_model_get (source_model, &source_iter, 
1792                                     TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN, 
1793                                     &header, -1);
1794
1795                 /* Do not enable d&d of headers already opened */
1796                 if (!modest_window_mgr_find_registered_header(mgr, header, NULL))
1797                         tny_list_append (headers, G_OBJECT (header));
1798
1799                 if (G_UNLIKELY (first)) {
1800                         src_folder = tny_header_get_folder (header);
1801                         first = FALSE;
1802                 }
1803
1804                 /* Free and go on */
1805                 gtk_tree_path_free (path);
1806                 g_object_unref (header);
1807                 tmp++;
1808         }
1809         g_strfreev (uris);
1810
1811         /* Get the target folder */
1812         gtk_tree_model_get_iter (dest_model, &dest_iter, dest_row);
1813         gtk_tree_model_get (dest_model, &dest_iter, 
1814                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
1815                             &folder, -1);
1816         
1817         if (!folder || !TNY_IS_FOLDER(folder)) {
1818 /*              g_warning ("%s: not a valid target folder (%p)", __FUNCTION__, folder); */
1819                 goto cleanup;
1820         }
1821         
1822         folder_type = modest_tny_folder_guess_folder_type (folder);
1823         if (folder_type == TNY_FOLDER_TYPE_INVALID) {
1824 /*              g_warning ("%s: invalid target folder", __FUNCTION__); */
1825                 goto cleanup;  /* cannot move messages there */
1826         }
1827         
1828         if (modest_tny_folder_get_rules((TNY_FOLDER(folder))) & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE) {
1829 /*              g_warning ("folder not writable"); */
1830                 goto cleanup; /* verboten! */
1831         }
1832         
1833         /* Ask for confirmation to move */
1834         main_win = modest_window_mgr_get_main_window (mgr, FALSE); /* don't create */
1835         if (!main_win) {
1836                 g_warning ("%s: BUG: no main window found", __FUNCTION__);
1837                 goto cleanup;
1838         }
1839
1840         /* Transfer messages */
1841         modest_ui_actions_transfer_messages_helper (GTK_WINDOW (main_win), src_folder, 
1842                                                     headers, folder);
1843         
1844         /* Frees */
1845 cleanup:
1846         if (G_IS_OBJECT (src_folder))
1847                 g_object_unref (src_folder);
1848         if (G_IS_OBJECT(folder))
1849                 g_object_unref (G_OBJECT (folder));
1850         if (G_IS_OBJECT(headers))
1851                 g_object_unref (headers);
1852 }
1853
1854 typedef struct {
1855         TnyFolderStore *src_folder;
1856         TnyFolderStore *dst_folder;
1857         ModestFolderView *folder_view;
1858         DndHelper *helper; 
1859 } DndFolderInfo;
1860
1861 static void
1862 dnd_folder_info_destroyer (DndFolderInfo *info)
1863 {
1864         if (info->src_folder)
1865                 g_object_unref (info->src_folder);
1866         if (info->dst_folder)
1867                 g_object_unref (info->dst_folder);
1868         g_slice_free (DndFolderInfo, info);
1869 }
1870
1871 static void
1872 dnd_on_connection_failed_destroyer (DndFolderInfo *info,
1873                                     GtkWindow *parent_window,
1874                                     TnyAccount *account)
1875 {
1876         /* Show error */
1877         modest_ui_actions_on_account_connection_error (parent_window, account);
1878
1879         /* Free the helper & info */
1880         dnd_helper_destroyer (info->helper);
1881         dnd_folder_info_destroyer (info);
1882 }
1883
1884 static void
1885 drag_and_drop_from_folder_view_src_folder_performer (gboolean canceled, 
1886                                                      GError *err,
1887                                                      GtkWindow *parent_window, 
1888                                                      TnyAccount *account, 
1889                                                      gpointer user_data)
1890 {
1891         DndFolderInfo *info = NULL;
1892         ModestMailOperation *mail_op;
1893
1894         info = (DndFolderInfo *) user_data;
1895
1896         if (err || canceled) {
1897                 dnd_on_connection_failed_destroyer (info, parent_window, account);
1898                 return;
1899         }
1900
1901         /* Do the mail operation */
1902         mail_op = modest_mail_operation_new_with_error_handling ((GObject *) parent_window,
1903                                                                  modest_ui_actions_move_folder_error_handler,
1904                                                                  info->src_folder, NULL);
1905
1906         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
1907                                          mail_op);
1908
1909         /* Transfer the folder */
1910         modest_mail_operation_xfer_folder (mail_op,
1911                                            TNY_FOLDER (info->src_folder),
1912                                            info->dst_folder,
1913                                            info->helper->delete_source,
1914                                            xfer_folder_cb,
1915                                            info->helper->folder_view);
1916
1917         /* Frees */     
1918         g_object_unref (G_OBJECT (mail_op));
1919         dnd_helper_destroyer (info->helper);
1920         dnd_folder_info_destroyer (info);
1921 }
1922
1923
1924 static void
1925 drag_and_drop_from_folder_view_dst_folder_performer (gboolean canceled, 
1926                                                      GError *err,
1927                                                      GtkWindow *parent_window, 
1928                                                      TnyAccount *account, 
1929                                                      gpointer user_data)
1930 {
1931         DndFolderInfo *info = NULL;
1932
1933         info = (DndFolderInfo *) user_data;
1934
1935         if (err || canceled) {
1936                 dnd_on_connection_failed_destroyer (info, parent_window, account);
1937                 return;
1938         }
1939
1940         /* Connect to source folder and perform the copy/move */
1941         modest_platform_connect_if_remote_and_perform (NULL, TRUE,
1942                                                        info->src_folder,
1943                                                        drag_and_drop_from_folder_view_src_folder_performer,
1944                                                        info);
1945 }
1946
1947 /*
1948  * This function is used by drag_data_received_cb to manage drag and
1949  * drop of a folder, i.e, and drag from the folder view to the same
1950  * folder view.
1951  */
1952 static void
1953 drag_and_drop_from_folder_view (GtkTreeModel     *source_model,
1954                                 GtkTreeModel     *dest_model,
1955                                 GtkTreePath      *dest_row,
1956                                 GtkSelectionData *selection_data,
1957                                 DndHelper        *helper)
1958 {
1959         GtkTreeIter dest_iter, iter;
1960         TnyFolderStore *dest_folder = NULL;
1961         TnyFolderStore *folder = NULL;
1962         gboolean forbidden = FALSE;
1963         ModestWindow *win;
1964         DndFolderInfo *info = NULL;
1965
1966         win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr(), FALSE); /* don't create */
1967         if (!win) {
1968                 g_warning ("%s: BUG: no main window", __FUNCTION__);
1969                 dnd_helper_destroyer (helper);
1970                 return;
1971         }
1972         
1973         if (!forbidden) {
1974                 /* check the folder rules for the destination */
1975                 folder = tree_path_to_folder (dest_model, dest_row);
1976                 if (TNY_IS_FOLDER(folder)) {
1977                         ModestTnyFolderRules rules =
1978                                 modest_tny_folder_get_rules (TNY_FOLDER (folder));
1979                         forbidden = rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE;
1980                 } else if (TNY_IS_FOLDER_STORE(folder)) {
1981                         /* enable local root as destination for folders */
1982                         if (!MODEST_IS_TNY_LOCAL_FOLDERS_ACCOUNT (folder) && 
1983                             !modest_tny_account_is_memory_card_account (TNY_ACCOUNT (folder)))
1984                                 forbidden = TRUE;
1985                 }
1986                 g_object_unref (folder);
1987         }
1988         if (!forbidden) {
1989                 /* check the folder rules for the source */
1990                 folder = tree_path_to_folder (source_model, helper->source_row);
1991                 if (TNY_IS_FOLDER(folder)) {
1992                         ModestTnyFolderRules rules =
1993                                 modest_tny_folder_get_rules (TNY_FOLDER (folder));
1994                         forbidden = rules & MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE;
1995                 } else
1996                         forbidden = TRUE;
1997                 g_object_unref (folder);
1998         }
1999
2000         
2001         /* Check if the drag is possible */
2002         if (forbidden || !gtk_tree_path_compare (helper->source_row, dest_row)) {
2003                 dnd_helper_destroyer (helper);
2004                 return;
2005         }
2006
2007         /* Get data */
2008         gtk_tree_model_get_iter (dest_model, &dest_iter, dest_row);
2009         gtk_tree_model_get (dest_model, &dest_iter, 
2010                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, 
2011                             &dest_folder, -1);
2012         gtk_tree_model_get_iter (source_model, &iter, helper->source_row);
2013         gtk_tree_model_get (source_model, &iter,
2014                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
2015                             &folder, -1);
2016
2017         /* Create the info for the performer */
2018         info = g_slice_new0 (DndFolderInfo);
2019         info->src_folder = g_object_ref (folder);
2020         info->dst_folder = g_object_ref (dest_folder);
2021         info->helper = helper;
2022
2023         /* Connect to the destination folder and perform the copy/move */
2024         modest_platform_connect_if_remote_and_perform (GTK_WINDOW (win), TRUE,
2025                                                        dest_folder,
2026                                                        drag_and_drop_from_folder_view_dst_folder_performer,
2027                                                        info);
2028         
2029         /* Frees */
2030         g_object_unref (dest_folder);
2031         g_object_unref (folder);
2032 }
2033
2034 /*
2035  * This function receives the data set by the "drag-data-get" signal
2036  * handler. This information comes within the #GtkSelectionData. This
2037  * function will manage both the drags of folders of the treeview and
2038  * drags of headers of the header view widget.
2039  */
2040 static void 
2041 on_drag_data_received (GtkWidget *widget, 
2042                        GdkDragContext *context, 
2043                        gint x, 
2044                        gint y, 
2045                        GtkSelectionData *selection_data, 
2046                        guint target_type, 
2047                        guint time, 
2048                        gpointer data)
2049 {
2050         GtkWidget *source_widget;
2051         GtkTreeModel *dest_model, *source_model;
2052         GtkTreePath *source_row, *dest_row;
2053         GtkTreeViewDropPosition pos;
2054         gboolean delete_source = FALSE;
2055         gboolean success = FALSE;
2056
2057         /* Do not allow further process */
2058         g_signal_stop_emission_by_name (widget, "drag-data-received");
2059         source_widget = gtk_drag_get_source_widget (context);
2060
2061         /* Get the action */
2062         if (context->action == GDK_ACTION_MOVE) {
2063                 delete_source = TRUE;
2064
2065                 /* Notify that there is no folder selected. We need to
2066                    do this in order to update the headers view (and
2067                    its monitors, because when moving, the old folder
2068                    won't longer exist. We can not wait for the end of
2069                    the operation, because the operation won't start if
2070                    the folder is in use */
2071                 if (source_widget == widget) {
2072                         GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
2073                         gtk_tree_selection_unselect_all (sel);
2074                 }
2075         }
2076
2077         /* Check if the get_data failed */
2078         if (selection_data == NULL || selection_data->length < 0)
2079                 goto end;
2080
2081         /* Select the destination model */
2082         dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
2083
2084         /* Get the path to the destination row. Can not call
2085            gtk_tree_view_get_drag_dest_row() because the source row
2086            is not selected anymore */
2087         gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget), x, y,
2088                                            &dest_row, &pos);
2089
2090         /* Only allow drops IN other rows */
2091         if (!dest_row || 
2092             pos == GTK_TREE_VIEW_DROP_BEFORE || 
2093             pos == GTK_TREE_VIEW_DROP_AFTER)
2094                 goto end;
2095
2096         success = TRUE;
2097         /* Drags from the header view */
2098         if (source_widget != widget) {
2099                 source_model = gtk_tree_view_get_model (GTK_TREE_VIEW (source_widget));
2100
2101                 drag_and_drop_from_header_view (source_model,
2102                                                 dest_model,
2103                                                 dest_row,
2104                                                 selection_data);
2105         } else {
2106                 DndHelper *helper = NULL; 
2107
2108                 /* Get the source model and row */
2109                 gtk_tree_get_row_drag_data (selection_data,
2110                                             &source_model,
2111                                             &source_row);
2112
2113                 /* Create the helper */
2114                 helper = g_slice_new0 (DndHelper);
2115                 helper->delete_source = delete_source;
2116                 helper->source_row = gtk_tree_path_copy (source_row);
2117                 helper->folder_view = MODEST_FOLDER_VIEW (widget);
2118
2119                 drag_and_drop_from_folder_view (source_model,
2120                                                 dest_model,
2121                                                 dest_row,
2122                                                 selection_data, 
2123                                                 helper);
2124
2125                 gtk_tree_path_free (source_row);
2126         }
2127
2128         /* Frees */
2129         gtk_tree_path_free (dest_row);
2130
2131  end:
2132         /* Finish the drag and drop */
2133         gtk_drag_finish (context, success, FALSE, time);
2134 }
2135
2136 /*
2137  * We define a "drag-drop" signal handler because we do not want to
2138  * use the default one, because the default one always calls
2139  * gtk_drag_finish and we prefer to do it in the "drag-data-received"
2140  * signal handler, because there we have all the information available
2141  * to know if the dnd was a success or not.
2142  */
2143 static gboolean
2144 drag_drop_cb (GtkWidget      *widget,
2145               GdkDragContext *context,
2146               gint            x,
2147               gint            y,
2148               guint           time,
2149               gpointer        user_data) 
2150 {
2151         gpointer target;
2152
2153         if (!context->targets)
2154                 return FALSE;
2155
2156         /* Check if we're dragging a folder row */
2157         target = gtk_drag_dest_find_target (widget, context, NULL);
2158
2159         /* Request the data from the source. */
2160         gtk_drag_get_data(widget, context, target, time);
2161
2162     return TRUE;
2163 }
2164
2165 /*
2166  * This function expands a node of a tree view if it's not expanded
2167  * yet. Not sure why it needs the threads stuff, but gtk+`example code
2168  * does that, so that's why they're here.
2169  */
2170 static gint
2171 expand_row_timeout (gpointer data)
2172 {
2173         GtkTreeView *tree_view = data;
2174         GtkTreePath *dest_path = NULL;
2175         GtkTreeViewDropPosition pos;
2176         gboolean result = FALSE;
2177         
2178         gdk_threads_enter ();
2179         
2180         gtk_tree_view_get_drag_dest_row (tree_view,
2181                                          &dest_path,
2182                                          &pos);
2183         
2184         if (dest_path &&
2185             (pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER ||
2186              pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)) {
2187                 gtk_tree_view_expand_row (tree_view, dest_path, FALSE);
2188                 gtk_tree_path_free (dest_path);
2189         }
2190         else {
2191                 if (dest_path)
2192                         gtk_tree_path_free (dest_path);
2193                 
2194                 result = TRUE;
2195         }
2196         
2197         gdk_threads_leave ();
2198
2199         return result;
2200 }
2201
2202 /*
2203  * This function is called whenever the pointer is moved over a widget
2204  * while dragging some data. It installs a timeout that will expand a
2205  * node of the treeview if not expanded yet. This function also calls
2206  * gdk_drag_status in order to set the suggested action that will be
2207  * used by the "drag-data-received" signal handler to know if we
2208  * should do a move or just a copy of the data.
2209  */
2210 static gboolean
2211 on_drag_motion (GtkWidget      *widget,
2212                 GdkDragContext *context,
2213                 gint            x,
2214                 gint            y,
2215                 guint           time,
2216                 gpointer        user_data)  
2217 {
2218         GtkTreeViewDropPosition pos;
2219         GtkTreePath *dest_row;
2220         GtkTreeModel *dest_model;
2221         ModestFolderViewPrivate *priv;
2222         GdkDragAction suggested_action;
2223         gboolean valid_location = FALSE;
2224         TnyFolderStore *folder = NULL;
2225
2226         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (widget);
2227
2228         if (priv->timer_expander != 0) {
2229                 g_source_remove (priv->timer_expander);
2230                 priv->timer_expander = 0;
2231         }
2232
2233         gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
2234                                            x, y,
2235                                            &dest_row,
2236                                            &pos);
2237
2238         /* Do not allow drops between folders */
2239         if (!dest_row ||
2240             pos == GTK_TREE_VIEW_DROP_BEFORE ||
2241             pos == GTK_TREE_VIEW_DROP_AFTER) {
2242                 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW (widget), NULL, 0);
2243                 gdk_drag_status(context, 0, time);
2244                 valid_location = FALSE;
2245                 goto out;
2246         } else {
2247                 valid_location = TRUE;
2248         }
2249
2250         /* Check that the destination folder is writable */
2251         dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
2252         folder = tree_path_to_folder (dest_model, dest_row);
2253         if (folder && TNY_IS_FOLDER (folder)) {
2254                 ModestTnyFolderRules rules = modest_tny_folder_get_rules(TNY_FOLDER (folder));
2255
2256                 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE) {
2257                         valid_location = FALSE;
2258                         goto out;
2259                 }
2260         }
2261
2262         /* Expand the selected row after 1/2 second */
2263         if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), dest_row)) {
2264                 priv->timer_expander = g_timeout_add (500, expand_row_timeout, widget);
2265         }
2266         gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), dest_row, pos);
2267
2268         /* Select the desired action. By default we pick MOVE */
2269         suggested_action = GDK_ACTION_MOVE;
2270
2271         if (context->actions == GDK_ACTION_COPY)
2272             gdk_drag_status(context, GDK_ACTION_COPY, time);
2273         else if (context->actions == GDK_ACTION_MOVE)
2274             gdk_drag_status(context, GDK_ACTION_MOVE, time);
2275         else if (context->actions & suggested_action)
2276             gdk_drag_status(context, suggested_action, time);
2277         else
2278             gdk_drag_status(context, GDK_ACTION_DEFAULT, time);
2279
2280  out:
2281         if (folder)
2282                 g_object_unref (folder);
2283         if (dest_row) {
2284                 gtk_tree_path_free (dest_row);
2285         }
2286         g_signal_stop_emission_by_name (widget, "drag-motion");
2287
2288         return valid_location;
2289 }
2290
2291 /*
2292  * This function sets the treeview as a source and a target for dnd
2293  * events. It also connects all the requirede signals.
2294  */
2295 static void
2296 setup_drag_and_drop (GtkTreeView *self)
2297 {
2298         /* Set up the folder view as a dnd destination. Set only the
2299            highlight flag, otherwise gtk will have a different
2300            behaviour */
2301         gtk_drag_dest_set (GTK_WIDGET (self),
2302                            GTK_DEST_DEFAULT_HIGHLIGHT,
2303                            folder_view_drag_types,
2304                            G_N_ELEMENTS (folder_view_drag_types),
2305                            GDK_ACTION_MOVE | GDK_ACTION_COPY);
2306
2307         g_signal_connect (G_OBJECT (self),
2308                           "drag_data_received",
2309                           G_CALLBACK (on_drag_data_received),
2310                           NULL);
2311
2312
2313         /* Set up the treeview as a dnd source */
2314         gtk_drag_source_set (GTK_WIDGET (self),
2315                              GDK_BUTTON1_MASK,
2316                              folder_view_drag_types,
2317                              G_N_ELEMENTS (folder_view_drag_types),
2318                              GDK_ACTION_MOVE | GDK_ACTION_COPY);
2319
2320         g_signal_connect (G_OBJECT (self),
2321                           "drag_motion",
2322                           G_CALLBACK (on_drag_motion),
2323                           NULL);
2324         
2325         g_signal_connect (G_OBJECT (self),
2326                           "drag_data_get",
2327                           G_CALLBACK (on_drag_data_get),
2328                           NULL);
2329
2330         g_signal_connect (G_OBJECT (self),
2331                           "drag_drop",
2332                           G_CALLBACK (drag_drop_cb),
2333                           NULL);
2334 }
2335
2336 /*
2337  * This function manages the navigation through the folders using the
2338  * keyboard or the hardware keys in the device
2339  */
2340 static gboolean
2341 on_key_pressed (GtkWidget *self,
2342                 GdkEventKey *event,
2343                 gpointer user_data)
2344 {
2345         GtkTreeSelection *selection;
2346         GtkTreeIter iter;
2347         GtkTreeModel *model;
2348         gboolean retval = FALSE;
2349
2350         /* Up and Down are automatically managed by the treeview */
2351         if (event->keyval == GDK_Return) {
2352                 /* Expand/Collapse the selected row */
2353                 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
2354                 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
2355                         GtkTreePath *path;
2356
2357                         path = gtk_tree_model_get_path (model, &iter);
2358
2359                         if (gtk_tree_view_row_expanded (GTK_TREE_VIEW (self), path))
2360                                 gtk_tree_view_collapse_row (GTK_TREE_VIEW (self), path);
2361                         else
2362                                 gtk_tree_view_expand_row (GTK_TREE_VIEW (self), path, FALSE);
2363                         gtk_tree_path_free (path);
2364                 }
2365                 /* No further processing */
2366                 retval = TRUE;
2367         }
2368
2369         return retval;
2370 }
2371
2372 /*
2373  * We listen to the changes in the local folder account name key,
2374  * because we want to show the right name in the view. The local
2375  * folder account name corresponds to the device name in the Maemo
2376  * version. We do this because we do not want to query gconf on each
2377  * tree view refresh. It's better to cache it and change whenever
2378  * necessary.
2379  */
2380 static void 
2381 on_configuration_key_changed (ModestConf* conf, 
2382                               const gchar *key, 
2383                               ModestConfEvent event,
2384                               ModestConfNotificationId id, 
2385                               ModestFolderView *self)
2386 {
2387         ModestFolderViewPrivate *priv;
2388
2389
2390         g_return_if_fail (MODEST_IS_FOLDER_VIEW (self));
2391         priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
2392
2393         if (!strcmp (key, MODEST_CONF_DEVICE_NAME)) {
2394                 g_free (priv->local_account_name);
2395
2396                 if (event == MODEST_CONF_EVENT_KEY_UNSET)
2397                         priv->local_account_name = g_strdup (MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME);
2398                 else
2399                         priv->local_account_name = modest_conf_get_string (modest_runtime_get_conf(),
2400                                                                            MODEST_CONF_DEVICE_NAME, NULL);
2401
2402                 /* Force a redraw */
2403 #if GTK_CHECK_VERSION(2, 8, 0)
2404                 GtkTreeViewColumn * tree_column;
2405
2406                 tree_column = gtk_tree_view_get_column (GTK_TREE_VIEW (self), 
2407                                                         TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN);
2408                 gtk_tree_view_column_queue_resize (tree_column);
2409 #else
2410                 gtk_widget_queue_draw (GTK_WIDGET (self));
2411 #endif
2412         }
2413 }
2414
2415 void
2416 modest_folder_view_set_style (ModestFolderView *self,
2417                               ModestFolderViewStyle style)
2418 {
2419         ModestFolderViewPrivate *priv;
2420
2421         g_return_if_fail (self && MODEST_IS_FOLDER_VIEW(self));
2422         g_return_if_fail (style == MODEST_FOLDER_VIEW_STYLE_SHOW_ALL ||
2423                           style == MODEST_FOLDER_VIEW_STYLE_SHOW_ONE);
2424         
2425         priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
2426
2427         
2428         priv->style = style;
2429 }
2430
2431 void
2432 modest_folder_view_set_account_id_of_visible_server_account (ModestFolderView *self,
2433                                                              const gchar *account_id)
2434 {
2435         ModestFolderViewPrivate *priv;
2436         GtkTreeModel *model;
2437
2438         g_return_if_fail (self && MODEST_IS_FOLDER_VIEW(self));
2439         
2440         priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
2441
2442         /* This will be used by the filter_row callback,
2443          * to decided which rows to show: */
2444         if (priv->visible_account_id) {
2445                 g_free (priv->visible_account_id);
2446                 priv->visible_account_id = NULL;
2447         }
2448         if (account_id)
2449                 priv->visible_account_id = g_strdup (account_id);
2450
2451         /* Refilter */
2452         model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
2453         if (GTK_IS_TREE_MODEL_FILTER (model))
2454                 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (model));
2455
2456         /* Save settings to gconf */
2457         modest_widget_memory_save (modest_runtime_get_conf (), G_OBJECT(self),
2458                                    MODEST_CONF_FOLDER_VIEW_KEY);
2459 }
2460
2461 const gchar *
2462 modest_folder_view_get_account_id_of_visible_server_account (ModestFolderView *self)
2463 {
2464         ModestFolderViewPrivate *priv;
2465
2466         g_return_val_if_fail (self && MODEST_IS_FOLDER_VIEW(self), NULL);
2467         
2468         priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
2469
2470         return (const gchar *) priv->visible_account_id;
2471 }
2472
2473 static gboolean
2474 find_inbox_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreeIter *inbox_iter)
2475 {
2476         do {
2477                 GtkTreeIter child;
2478                 TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
2479
2480                 gtk_tree_model_get (model, iter, 
2481                                     TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, 
2482                                     &type, -1);
2483                         
2484                 gboolean result = FALSE;
2485                 if (type == TNY_FOLDER_TYPE_INBOX) {
2486                         result = TRUE;
2487                 }               
2488                 if (result) {
2489                         *inbox_iter = *iter;
2490                         return TRUE;
2491                 }
2492
2493                 if (gtk_tree_model_iter_children (model, &child, iter)) {
2494                         if (find_inbox_iter (model, &child, inbox_iter))
2495                                 return TRUE;
2496                 }
2497
2498         } while (gtk_tree_model_iter_next (model, iter));
2499
2500         return FALSE;
2501 }
2502
2503
2504
2505
2506 void 
2507 modest_folder_view_select_first_inbox_or_local (ModestFolderView *self)
2508 {
2509         GtkTreeModel *model;
2510         GtkTreeIter iter, inbox_iter;
2511         GtkTreeSelection *sel;
2512         GtkTreePath *path = NULL;
2513
2514         g_return_if_fail (self && MODEST_IS_FOLDER_VIEW(self));
2515         
2516         model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
2517         if (!model)
2518                 return;
2519
2520         expand_root_items (self);
2521         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
2522
2523         if (!gtk_tree_model_get_iter_first (model, &iter)) {
2524                 g_warning ("%s: model is empty", __FUNCTION__);
2525                 return;
2526         }
2527
2528         if (find_inbox_iter (model, &iter, &inbox_iter))
2529                 path = gtk_tree_model_get_path (model, &inbox_iter);
2530         else
2531                 path = gtk_tree_path_new_first ();
2532
2533         /* Select the row and free */
2534         gtk_tree_view_set_cursor (GTK_TREE_VIEW (self), path, NULL, FALSE);
2535         gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (self), path, NULL, FALSE, 0.0, 0.0);
2536         gtk_tree_path_free (path);
2537
2538         /* set focus */
2539         gtk_widget_grab_focus (GTK_WIDGET(self));
2540 }
2541
2542
2543 /* recursive */
2544 static gboolean
2545 find_folder_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreeIter *folder_iter, 
2546                   TnyFolder* folder)
2547 {
2548         do {
2549                 GtkTreeIter child;
2550                 TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
2551                 TnyFolder* a_folder;
2552                 gchar *name = NULL;
2553                 
2554                 gtk_tree_model_get (model, iter, 
2555                                     TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &a_folder,
2556                                     TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name,
2557                                     TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type, 
2558                                     -1);                
2559                 g_free (name);
2560
2561                 if (folder == a_folder) {
2562                         g_object_unref (a_folder);
2563                         *folder_iter = *iter;
2564                         return TRUE;
2565                 }
2566                 g_object_unref (a_folder);
2567                 
2568                 if (gtk_tree_model_iter_children (model, &child, iter)) {
2569                         if (find_folder_iter (model, &child, folder_iter, folder)) 
2570                                 return TRUE;
2571                 }
2572
2573         } while (gtk_tree_model_iter_next (model, iter));
2574
2575         return FALSE;
2576 }
2577
2578
2579 static void
2580 on_row_inserted_maybe_select_folder (GtkTreeModel *tree_model, 
2581                                      GtkTreePath *path, 
2582                                      GtkTreeIter *iter,
2583                                      ModestFolderView *self)
2584 {
2585         ModestFolderViewPrivate *priv = NULL;
2586         GtkTreeSelection *sel;
2587         TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
2588         GObject *instance = NULL;
2589
2590         if (!MODEST_IS_FOLDER_VIEW(self))
2591                 return;
2592         
2593         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
2594
2595         priv->reexpand = TRUE;
2596
2597         gtk_tree_model_get (tree_model, iter, 
2598                             TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
2599                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
2600                             -1);
2601         if (type == TNY_FOLDER_TYPE_INBOX && priv->folder_to_select == NULL) {
2602                 priv->folder_to_select = g_object_ref (instance);
2603         }
2604         g_object_unref (instance);
2605         
2606         if (priv->folder_to_select) {
2607                 
2608                 if (!modest_folder_view_select_folder (self, priv->folder_to_select,
2609                                                        FALSE)) {
2610                         GtkTreePath *path;
2611                         path = gtk_tree_model_get_path (tree_model, iter);
2612                         gtk_tree_view_expand_to_path (GTK_TREE_VIEW(self), path);
2613                         
2614                         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
2615
2616                         gtk_tree_selection_select_iter (sel, iter);
2617                         gtk_tree_view_set_cursor (GTK_TREE_VIEW(self), path, NULL, FALSE);
2618
2619                         gtk_tree_path_free (path);              
2620                 }
2621
2622                 /* Disable next */
2623                 modest_folder_view_disable_next_folder_selection (self);
2624                 
2625                 /* Refilter the model */
2626                 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (tree_model));
2627         }
2628 }
2629
2630
2631 void
2632 modest_folder_view_disable_next_folder_selection (ModestFolderView *self) 
2633 {
2634         ModestFolderViewPrivate *priv;
2635
2636         g_return_if_fail (self && MODEST_IS_FOLDER_VIEW(self));
2637
2638         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
2639
2640         if (priv->folder_to_select)
2641                 g_object_unref(priv->folder_to_select);
2642         
2643         priv->folder_to_select = NULL;
2644 }
2645
2646 gboolean
2647 modest_folder_view_select_folder (ModestFolderView *self, TnyFolder *folder, 
2648                                   gboolean after_change)
2649 {
2650         GtkTreeModel *model;
2651         GtkTreeIter iter, folder_iter;
2652         GtkTreeSelection *sel;
2653         ModestFolderViewPrivate *priv = NULL;
2654         
2655         g_return_val_if_fail (self && MODEST_IS_FOLDER_VIEW (self), FALSE);     
2656         g_return_val_if_fail (folder && TNY_IS_FOLDER (folder), FALSE); 
2657                 
2658         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
2659
2660         if (after_change) {
2661                 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
2662                 gtk_tree_selection_unselect_all (sel);
2663
2664                 if (priv->folder_to_select)
2665                         g_object_unref(priv->folder_to_select);
2666                 priv->folder_to_select = TNY_FOLDER(g_object_ref(folder));
2667                 return TRUE;
2668         }
2669                 
2670         model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
2671         if (!model)
2672                 return FALSE;
2673
2674
2675         /* Refilter the model, before selecting the folder */
2676         gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (model));
2677
2678         if (!gtk_tree_model_get_iter_first (model, &iter)) {
2679                 g_warning ("%s: model is empty", __FUNCTION__);
2680                 return FALSE;
2681         }
2682         
2683         if (find_folder_iter (model, &iter, &folder_iter, folder)) {
2684                 GtkTreePath *path;
2685
2686                 path = gtk_tree_model_get_path (model, &folder_iter);
2687                 gtk_tree_view_expand_to_path (GTK_TREE_VIEW(self), path);
2688
2689                 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
2690                 gtk_tree_selection_select_iter (sel, &folder_iter);
2691                 gtk_tree_view_set_cursor (GTK_TREE_VIEW(self), path, NULL, FALSE);
2692
2693                 gtk_tree_path_free (path);
2694                 return TRUE;
2695         }
2696         return FALSE;
2697 }
2698
2699
2700 void 
2701 modest_folder_view_copy_selection (ModestFolderView *self)
2702 {
2703         g_return_if_fail (self && MODEST_IS_FOLDER_VIEW(self));
2704         
2705         /* Copy selection */
2706         _clipboard_set_selected_data (self, FALSE);
2707 }
2708
2709 void 
2710 modest_folder_view_cut_selection (ModestFolderView *folder_view)
2711 {
2712         ModestFolderViewPrivate *priv = NULL;
2713         GtkTreeModel *model = NULL;
2714         const gchar **hidding = NULL;
2715         guint i, n_selected;
2716
2717         g_return_if_fail (folder_view && MODEST_IS_FOLDER_VIEW (folder_view));
2718         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (folder_view);
2719
2720         /* Copy selection */
2721         if (!_clipboard_set_selected_data (folder_view, TRUE))
2722                 return;
2723
2724         /* Get hidding ids */
2725         hidding = modest_email_clipboard_get_hidding_ids (priv->clipboard, &n_selected); 
2726         
2727         /* Clear hidding array created by previous cut operation */
2728         _clear_hidding_filter (MODEST_FOLDER_VIEW (folder_view));
2729
2730         /* Copy hidding array */
2731         priv->n_selected = n_selected;
2732         priv->hidding_ids = g_malloc0(sizeof(gchar *) * n_selected);
2733         for (i=0; i < n_selected; i++) 
2734                 priv->hidding_ids[i] = g_strdup(hidding[i]);            
2735
2736         /* Hide cut folders */
2737         model = gtk_tree_view_get_model (GTK_TREE_VIEW (folder_view));
2738         gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (model));
2739 }
2740
2741 void
2742 modest_folder_view_copy_model (ModestFolderView *folder_view_src,
2743                                ModestFolderView *folder_view_dst)
2744 {
2745         GtkTreeModel *filter_model = NULL;
2746         GtkTreeModel *model = NULL;
2747         GtkTreeModel *new_filter_model = NULL;
2748         
2749         g_return_if_fail (folder_view_src && MODEST_IS_FOLDER_VIEW (folder_view_src));
2750         g_return_if_fail (folder_view_dst && MODEST_IS_FOLDER_VIEW (folder_view_dst));
2751         
2752         /* Get src model*/
2753         filter_model = gtk_tree_view_get_model (GTK_TREE_VIEW (folder_view_src));
2754         model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER(filter_model));
2755
2756         /* Build new filter model */
2757         new_filter_model = gtk_tree_model_filter_new (model, NULL);     
2758         gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (new_filter_model),
2759                                                 filter_row,
2760                                                 folder_view_dst,
2761                                                 NULL);
2762         /* Set copied model */
2763         gtk_tree_view_set_model (GTK_TREE_VIEW (folder_view_dst), new_filter_model);
2764         g_signal_connect (G_OBJECT(new_filter_model), "row-inserted",
2765                           (GCallback) on_row_inserted_maybe_select_folder, folder_view_dst);
2766
2767         /* Free */
2768         g_object_unref (new_filter_model);
2769 }
2770
2771 void
2772 modest_folder_view_show_non_move_folders (ModestFolderView *folder_view,
2773                                           gboolean show)
2774 {
2775         GtkTreeModel *model = NULL;
2776         ModestFolderViewPrivate* priv;
2777
2778         g_return_if_fail (folder_view && MODEST_IS_FOLDER_VIEW (folder_view));
2779
2780         priv = MODEST_FOLDER_VIEW_GET_PRIVATE(folder_view);     
2781         priv->show_non_move = show;
2782 /*      modest_folder_view_update_model(folder_view, */
2783 /*                                      TNY_ACCOUNT_STORE(modest_runtime_get_account_store())); */
2784
2785         /* Hide special folders */
2786         model = gtk_tree_view_get_model (GTK_TREE_VIEW (folder_view));
2787         if (GTK_IS_TREE_MODEL_FILTER (model)) {
2788                 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (model));
2789         }
2790 }
2791
2792 /* Returns FALSE if it did not selected anything */
2793 static gboolean
2794 _clipboard_set_selected_data (ModestFolderView *folder_view,
2795                               gboolean delete)
2796 {
2797         ModestFolderViewPrivate *priv = NULL;
2798         TnyFolderStore *folder = NULL;
2799         gboolean retval = FALSE;
2800
2801         g_return_val_if_fail (MODEST_IS_FOLDER_VIEW (folder_view), FALSE);
2802         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (folder_view);
2803                 
2804         /* Set selected data on clipboard   */
2805         g_return_val_if_fail (MODEST_IS_EMAIL_CLIPBOARD (priv->clipboard), FALSE);
2806         folder = modest_folder_view_get_selected (folder_view);
2807
2808         /* Do not allow to select an account */
2809         if (TNY_IS_FOLDER (folder)) {
2810                 modest_email_clipboard_set_data (priv->clipboard, TNY_FOLDER(folder), NULL, delete);
2811                 retval = TRUE;
2812         }
2813
2814         /* Free */
2815         g_object_unref (folder);
2816
2817         return retval;
2818 }
2819
2820 static void
2821 _clear_hidding_filter (ModestFolderView *folder_view) 
2822 {
2823         ModestFolderViewPrivate *priv;
2824         guint i;
2825         
2826         g_return_if_fail (MODEST_IS_FOLDER_VIEW (folder_view)); 
2827         priv = MODEST_FOLDER_VIEW_GET_PRIVATE(folder_view);
2828
2829         if (priv->hidding_ids != NULL) {
2830                 for (i=0; i < priv->n_selected; i++) 
2831                         g_free (priv->hidding_ids[i]);
2832                 g_free(priv->hidding_ids);
2833         }       
2834 }
2835
2836
2837 static void 
2838 on_display_name_changed (ModestAccountMgr *mgr, 
2839                          const gchar *account,
2840                          gpointer user_data)
2841 {
2842         ModestFolderView *self;
2843
2844         self = MODEST_FOLDER_VIEW (user_data);
2845
2846         /* Force a redraw */
2847 #if GTK_CHECK_VERSION(2, 8, 0)
2848         GtkTreeViewColumn * tree_column;
2849         
2850         tree_column = gtk_tree_view_get_column (GTK_TREE_VIEW (self), 
2851                                                 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN);
2852         gtk_tree_view_column_queue_resize (tree_column);
2853 #else
2854         gtk_widget_queue_draw (GTK_WIDGET (self));
2855 #endif
2856 }