1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
30 #include <glib/gi18n.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"
60 /* Folder view drag types */
61 const GtkTargetEntry folder_view_drag_types[] =
63 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, MODEST_FOLDER_ROW },
64 { GTK_TREE_PATH_AS_STRING_LIST, GTK_TARGET_SAME_APP, MODEST_HEADER_ROW }
67 /* 'private'/'protected' functions */
68 static void modest_folder_view_class_init (ModestFolderViewClass *klass);
69 static void modest_folder_view_init (ModestFolderView *obj);
70 static void modest_folder_view_finalize (GObject *obj);
72 static void tny_account_store_view_init (gpointer g,
75 static void modest_folder_view_set_account_store (TnyAccountStoreView *self,
76 TnyAccountStore *account_store);
78 static void on_selection_changed (GtkTreeSelection *sel, gpointer data);
80 static void on_account_removed (TnyAccountStore *self,
84 static void on_account_inserted (TnyAccountStore *self,
88 static void on_account_changed (TnyAccountStore *self,
92 static gint cmp_rows (GtkTreeModel *tree_model,
97 static gboolean filter_row (GtkTreeModel *model,
101 static gboolean on_key_pressed (GtkWidget *self,
105 static void on_configuration_key_changed (ModestConf* conf,
107 ModestConfEvent event,
108 ModestConfNotificationId notification_id,
109 ModestFolderView *self);
112 static void on_drag_data_get (GtkWidget *widget,
113 GdkDragContext *context,
114 GtkSelectionData *selection_data,
119 static void on_drag_data_received (GtkWidget *widget,
120 GdkDragContext *context,
123 GtkSelectionData *selection_data,
128 static gboolean on_drag_motion (GtkWidget *widget,
129 GdkDragContext *context,
135 static void expand_root_items (ModestFolderView *self);
137 static gint expand_row_timeout (gpointer data);
139 static void setup_drag_and_drop (GtkTreeView *self);
141 static gboolean _clipboard_set_selected_data (ModestFolderView *folder_view,
144 static void _clear_hidding_filter (ModestFolderView *folder_view);
146 static void on_row_inserted_maybe_select_folder (GtkTreeModel *tree_model,
149 ModestFolderView *self);
152 FOLDER_SELECTION_CHANGED_SIGNAL,
153 FOLDER_DISPLAY_NAME_CHANGED_SIGNAL,
157 typedef struct _ModestFolderViewPrivate ModestFolderViewPrivate;
158 struct _ModestFolderViewPrivate {
159 TnyAccountStore *account_store;
160 TnyFolderStore *cur_folder_store;
162 TnyFolder *folder_to_select; /* folder to select after the next update */
164 ModestConfNotificationId notification_id;
166 gulong changed_signal;
167 gulong account_inserted_signal;
168 gulong account_removed_signal;
169 gulong account_changed_signal;
170 gulong conf_key_signal;
172 /* not unref this object, its a singlenton */
173 ModestEmailClipboard *clipboard;
175 /* Filter tree model */
179 TnyFolderStoreQuery *query;
180 guint timer_expander;
182 gchar *local_account_name;
183 gchar *visible_account_id;
184 ModestFolderViewStyle style;
186 gboolean reselect; /* we use this to force a reselection of the INBOX */
187 gboolean show_non_move;
188 gboolean reexpand; /* next time we expose, we'll expand all root folders */
190 #define MODEST_FOLDER_VIEW_GET_PRIVATE(o) \
191 (G_TYPE_INSTANCE_GET_PRIVATE((o), \
192 MODEST_TYPE_FOLDER_VIEW, \
193 ModestFolderViewPrivate))
195 static GObjectClass *parent_class = NULL;
197 static guint signals[LAST_SIGNAL] = {0};
200 modest_folder_view_get_type (void)
202 static GType my_type = 0;
204 static const GTypeInfo my_info = {
205 sizeof(ModestFolderViewClass),
206 NULL, /* base init */
207 NULL, /* base finalize */
208 (GClassInitFunc) modest_folder_view_class_init,
209 NULL, /* class finalize */
210 NULL, /* class data */
211 sizeof(ModestFolderView),
213 (GInstanceInitFunc) modest_folder_view_init,
217 static const GInterfaceInfo tny_account_store_view_info = {
218 (GInterfaceInitFunc) tny_account_store_view_init, /* interface_init */
219 NULL, /* interface_finalize */
220 NULL /* interface_data */
224 my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
228 g_type_add_interface_static (my_type,
229 TNY_TYPE_ACCOUNT_STORE_VIEW,
230 &tny_account_store_view_info);
236 modest_folder_view_class_init (ModestFolderViewClass *klass)
238 GObjectClass *gobject_class;
239 gobject_class = (GObjectClass*) klass;
241 parent_class = g_type_class_peek_parent (klass);
242 gobject_class->finalize = modest_folder_view_finalize;
244 g_type_class_add_private (gobject_class,
245 sizeof(ModestFolderViewPrivate));
247 signals[FOLDER_SELECTION_CHANGED_SIGNAL] =
248 g_signal_new ("folder_selection_changed",
249 G_TYPE_FROM_CLASS (gobject_class),
251 G_STRUCT_OFFSET (ModestFolderViewClass,
252 folder_selection_changed),
254 modest_marshal_VOID__POINTER_BOOLEAN,
255 G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_BOOLEAN);
258 * This signal is emitted whenever the currently selected
259 * folder display name is computed. Note that the name could
260 * be different to the folder name, because we could append
261 * the unread messages count to the folder name to build the
262 * folder display name
264 signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL] =
265 g_signal_new ("folder-display-name-changed",
266 G_TYPE_FROM_CLASS (gobject_class),
268 G_STRUCT_OFFSET (ModestFolderViewClass,
269 folder_display_name_changed),
271 g_cclosure_marshal_VOID__STRING,
272 G_TYPE_NONE, 1, G_TYPE_STRING);
275 /* Simplify checks for NULLs: */
277 strings_are_equal (const gchar *a, const gchar *b)
283 return (strcmp (a, b) == 0);
290 on_model_foreach_set_name(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
292 GObject *instance = NULL;
294 gtk_tree_model_get (model, iter,
295 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
299 return FALSE; /* keep walking */
301 if (!TNY_IS_ACCOUNT (instance)) {
302 g_object_unref (instance);
303 return FALSE; /* keep walking */
306 /* Check if this is the looked-for account: */
307 TnyAccount *this_account = TNY_ACCOUNT (instance);
308 TnyAccount *account = TNY_ACCOUNT (data);
310 const gchar *this_account_id = tny_account_get_id(this_account);
311 const gchar *account_id = tny_account_get_id(account);
312 g_object_unref (instance);
315 /* printf ("DEBUG: %s: this_account_id=%s, account_id=%s\n", __FUNCTION__, this_account_id, account_id); */
316 if (strings_are_equal(this_account_id, account_id)) {
317 /* Tell the model that the data has changed, so that
318 * it calls the cell_data_func callbacks again: */
319 /* TODO: This does not seem to actually cause the new string to be shown: */
320 gtk_tree_model_row_changed (model, path, iter);
322 return TRUE; /* stop walking */
325 return FALSE; /* keep walking */
330 ModestFolderView *self;
331 gchar *previous_name;
332 } GetMmcAccountNameData;
335 on_get_mmc_account_name (TnyStoreAccount* account, gpointer user_data)
337 /* printf ("DEBU1G: %s: account name=%s\n", __FUNCTION__, tny_account_get_name (TNY_ACCOUNT(account))); */
339 GetMmcAccountNameData *data = (GetMmcAccountNameData*)user_data;
341 if (!strings_are_equal (
342 tny_account_get_name(TNY_ACCOUNT(account)),
343 data->previous_name)) {
345 /* Tell the model that the data has changed, so that
346 * it calls the cell_data_func callbacks again: */
347 ModestFolderView *self = data->self;
348 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
350 gtk_tree_model_foreach(model, on_model_foreach_set_name, account);
353 g_free (data->previous_name);
354 g_slice_free (GetMmcAccountNameData, data);
358 text_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
359 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
361 ModestFolderViewPrivate *priv;
364 TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
365 GObject *instance = NULL;
367 g_return_if_fail (column);
368 g_return_if_fail (tree_model);
369 g_return_if_fail (iter != NULL);
371 gtk_tree_model_get (tree_model, iter,
372 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname,
373 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
374 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
376 rendobj = G_OBJECT(renderer);
386 ModestFolderView *self = MODEST_FOLDER_VIEW (data);
387 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
389 gchar *item_name = NULL;
390 gint item_weight = 400;
392 if (type != TNY_FOLDER_TYPE_ROOT) {
395 if (modest_tny_folder_is_local_folder (TNY_FOLDER (instance)) ||
396 modest_tny_folder_is_memory_card_folder (TNY_FOLDER (instance))) {
397 type = modest_tny_folder_get_local_or_mmc_folder_type (TNY_FOLDER (instance));
398 if (type != TNY_FOLDER_TYPE_UNKNOWN) {
400 fname = g_strdup(modest_local_folder_info_get_type_display_name (type));
404 /* note: we cannot reliably get the counts from the tree model, we need
405 * to use explicit calls on tny_folder for some reason.
407 /* Select the number to show: the unread or unsent messages. in case of outbox/drafts, show all */
408 if ((type == TNY_FOLDER_TYPE_DRAFTS) ||
409 (type == TNY_FOLDER_TYPE_OUTBOX) ||
410 (type == TNY_FOLDER_TYPE_MERGE)) /* _OUTBOX actually returns _MERGE... */
411 number = tny_folder_get_all_count (TNY_FOLDER(instance));
413 number = tny_folder_get_unread_count (TNY_FOLDER(instance));
415 /* Use bold font style if there are unread or unset messages */
417 item_name = g_strdup_printf ("%s (%d)", fname, number);
420 item_name = g_strdup (fname);
424 } else if (TNY_IS_ACCOUNT (instance)) {
425 /* If it's a server account */
426 if (modest_tny_account_is_virtual_local_folders (
427 TNY_ACCOUNT (instance))) {
428 item_name = g_strdup (priv->local_account_name);
430 } else if (modest_tny_account_is_memory_card_account (
431 TNY_ACCOUNT (instance))) {
432 /* fname is only correct when the items are first
433 * added to the model, not when the account is
434 * changed later, so get the name from the account
436 item_name = g_strdup (tny_account_get_name (TNY_ACCOUNT (instance)));
439 item_name = g_strdup (fname);
445 item_name = g_strdup ("unknown");
447 if (item_name && item_weight) {
448 /* Set the name in the treeview cell: */
449 g_object_set (rendobj,"text", item_name, "weight", item_weight, NULL);
451 /* Notify display name observers */
452 /* TODO: What listens for this signal, and how can it use only the new name? */
453 if (G_OBJECT (priv->cur_folder_store) == instance) {
454 g_signal_emit (G_OBJECT(self),
455 signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
462 /* If it is a Memory card account, make sure that we have the correct name.
463 * This function will be trigerred again when the name has been retrieved: */
464 if (TNY_IS_STORE_ACCOUNT (instance) &&
465 modest_tny_account_is_memory_card_account (TNY_ACCOUNT (instance))) {
467 /* Get the account name asynchronously: */
468 GetMmcAccountNameData *callback_data =
469 g_slice_new0(GetMmcAccountNameData);
470 callback_data->self = self;
472 const gchar *name = tny_account_get_name (TNY_ACCOUNT(instance));
474 callback_data->previous_name = g_strdup (name);
476 modest_tny_account_get_mmc_account_name (TNY_STORE_ACCOUNT (instance),
477 on_get_mmc_account_name, callback_data);
480 g_object_unref (G_OBJECT (instance));
485 icon_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
486 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
488 GObject *rendobj = NULL, *instance = NULL;
489 GdkPixbuf *pixbuf = NULL;
490 TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
491 const gchar *account_id = NULL;
492 gboolean has_children;
494 rendobj = G_OBJECT(renderer);
495 gtk_tree_model_get (tree_model, iter,
496 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
497 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
499 has_children = gtk_tree_model_iter_has_child (tree_model, iter);
504 /* MERGE is not needed anymore as the folder now has the correct type jschmid */
505 /* We include the MERGE type here because it's used to create
506 the local OUTBOX folder */
507 if (type == TNY_FOLDER_TYPE_NORMAL ||
508 type == TNY_FOLDER_TYPE_UNKNOWN) {
509 type = modest_tny_folder_guess_folder_type (TNY_FOLDER (instance));
513 case TNY_FOLDER_TYPE_ROOT:
514 if (TNY_IS_ACCOUNT (instance)) {
516 if (modest_tny_account_is_virtual_local_folders (
517 TNY_ACCOUNT (instance))) {
518 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_LOCAL_FOLDERS);
521 account_id = tny_account_get_id (TNY_ACCOUNT (instance));
523 if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID))
524 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_MMC);
526 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_ACCOUNT);
530 case TNY_FOLDER_TYPE_INBOX:
531 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_INBOX);
533 case TNY_FOLDER_TYPE_OUTBOX:
534 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_OUTBOX);
536 case TNY_FOLDER_TYPE_JUNK:
537 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_JUNK);
539 case TNY_FOLDER_TYPE_SENT:
540 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_SENT);
542 case TNY_FOLDER_TYPE_TRASH:
543 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_TRASH);
545 case TNY_FOLDER_TYPE_DRAFTS:
546 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_DRAFTS);
548 case TNY_FOLDER_TYPE_NORMAL:
550 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_NORMAL);
554 g_object_unref (G_OBJECT (instance));
557 g_object_set (rendobj, "pixbuf", pixbuf, NULL);
558 if (has_children && (pixbuf != NULL)) {
559 GdkPixbuf *open_pixbuf, *closed_pixbuf;
560 GdkPixbuf *open_emblem, *closed_emblem;
561 open_pixbuf = gdk_pixbuf_copy (pixbuf);
562 closed_pixbuf = gdk_pixbuf_copy (pixbuf);
563 open_emblem = modest_platform_get_icon ("qgn_list_gene_fldr_exp");
564 closed_emblem = modest_platform_get_icon ("qgn_list_gene_fldr_clp");
567 gdk_pixbuf_composite (open_emblem, open_pixbuf, 0, 0,
568 MIN (gdk_pixbuf_get_width (open_emblem),
569 gdk_pixbuf_get_width (open_pixbuf)),
570 MIN (gdk_pixbuf_get_height (open_emblem),
571 gdk_pixbuf_get_height (open_pixbuf)),
572 0, 0, 1, 1, GDK_INTERP_NEAREST, 255);
573 g_object_set (rendobj, "pixbuf-expander-open", open_pixbuf, NULL);
574 g_object_unref (open_emblem);
577 gdk_pixbuf_composite (closed_emblem, closed_pixbuf, 0, 0,
578 MIN (gdk_pixbuf_get_width (closed_emblem),
579 gdk_pixbuf_get_width (closed_pixbuf)),
580 MIN (gdk_pixbuf_get_height (closed_emblem),
581 gdk_pixbuf_get_height (closed_pixbuf)),
582 0, 0, 1, 1, GDK_INTERP_NEAREST, 255);
583 g_object_set (rendobj, "pixbuf-expander-closed", closed_pixbuf, NULL);
584 g_object_unref (closed_emblem);
587 g_object_unref (closed_pixbuf);
589 g_object_unref (open_pixbuf);
593 g_object_unref (pixbuf);
597 add_columns (GtkWidget *treeview)
599 GtkTreeViewColumn *column;
600 GtkCellRenderer *renderer;
601 GtkTreeSelection *sel;
604 column = gtk_tree_view_column_new ();
606 /* Set icon and text render function */
607 renderer = gtk_cell_renderer_pixbuf_new();
608 gtk_tree_view_column_pack_start (column, renderer, FALSE);
609 gtk_tree_view_column_set_cell_data_func(column, renderer,
610 icon_cell_data, treeview, NULL);
612 renderer = gtk_cell_renderer_text_new();
613 gtk_tree_view_column_pack_start (column, renderer, FALSE);
614 gtk_tree_view_column_set_cell_data_func(column, renderer,
615 text_cell_data, treeview, NULL);
617 /* Set selection mode */
618 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(treeview));
619 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
621 /* Set treeview appearance */
622 gtk_tree_view_column_set_spacing (column, 2);
623 gtk_tree_view_column_set_resizable (column, TRUE);
624 gtk_tree_view_column_set_fixed_width (column, TRUE);
625 gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(treeview), FALSE);
626 gtk_tree_view_set_enable_search (GTK_TREE_VIEW(treeview), FALSE);
629 gtk_tree_view_append_column (GTK_TREE_VIEW(treeview),column);
633 modest_folder_view_init (ModestFolderView *obj)
635 ModestFolderViewPrivate *priv;
638 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
640 priv->timer_expander = 0;
641 priv->account_store = NULL;
643 priv->style = MODEST_FOLDER_VIEW_STYLE_SHOW_ALL;
644 priv->cur_folder_store = NULL;
645 priv->visible_account_id = NULL;
646 priv->folder_to_select = NULL;
648 priv->reexpand = TRUE;
650 /* Initialize the local account name */
651 conf = modest_runtime_get_conf();
652 priv->local_account_name = modest_conf_get_string (conf, MODEST_CONF_DEVICE_NAME, NULL);
654 /* Init email clipboard */
655 priv->clipboard = modest_runtime_get_email_clipboard ();
656 priv->hidding_ids = NULL;
657 priv->n_selected = 0;
658 priv->reselect = FALSE;
659 priv->show_non_move = TRUE;
662 add_columns (GTK_WIDGET (obj));
664 /* Setup drag and drop */
665 setup_drag_and_drop (GTK_TREE_VIEW(obj));
667 /* Connect signals */
668 g_signal_connect (G_OBJECT (obj),
670 G_CALLBACK (on_key_pressed), NULL);
673 * Track changes in the local account name (in the device it
674 * will be the device name)
676 priv->notification_id = modest_conf_listen_to_namespace (conf,
677 MODEST_CONF_NAMESPACE);
678 priv->conf_key_signal = g_signal_connect (G_OBJECT(conf),
680 G_CALLBACK(on_configuration_key_changed),
685 tny_account_store_view_init (gpointer g, gpointer iface_data)
687 TnyAccountStoreViewIface *klass = (TnyAccountStoreViewIface *)g;
689 klass->set_account_store_func = modest_folder_view_set_account_store;
695 modest_folder_view_finalize (GObject *obj)
697 ModestFolderViewPrivate *priv;
698 GtkTreeSelection *sel;
700 g_return_if_fail (obj);
702 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
704 if (priv->notification_id) {
705 modest_conf_forget_namespace (modest_runtime_get_conf (),
706 MODEST_CONF_NAMESPACE,
707 priv->notification_id);
710 if (priv->timer_expander != 0) {
711 g_source_remove (priv->timer_expander);
712 priv->timer_expander = 0;
715 if (priv->account_store) {
716 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
717 priv->account_inserted_signal);
718 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
719 priv->account_removed_signal);
720 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
721 priv->account_changed_signal);
722 g_object_unref (G_OBJECT(priv->account_store));
723 priv->account_store = NULL;
727 g_object_unref (G_OBJECT (priv->query));
731 /* modest_folder_view_disable_next_folder_selection (MODEST_FOLDER_VIEW(obj)); */
732 if (priv->folder_to_select) {
733 g_object_unref (G_OBJECT(priv->folder_to_select));
734 priv->folder_to_select = NULL;
737 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(obj));
739 g_signal_handler_disconnect (G_OBJECT(sel), priv->changed_signal);
741 g_free (priv->local_account_name);
742 g_free (priv->visible_account_id);
744 if (priv->conf_key_signal) {
745 g_signal_handler_disconnect (modest_runtime_get_conf (),
746 priv->conf_key_signal);
747 priv->conf_key_signal = 0;
750 if (priv->cur_folder_store) {
751 if (TNY_IS_FOLDER(priv->cur_folder_store))
752 tny_folder_sync (TNY_FOLDER(priv->cur_folder_store), FALSE, NULL);
753 /* FALSE --> expunge the message */
755 g_object_unref (priv->cur_folder_store);
756 priv->cur_folder_store = NULL;
759 /* Clear hidding array created by cut operation */
760 _clear_hidding_filter (MODEST_FOLDER_VIEW (obj));
762 G_OBJECT_CLASS(parent_class)->finalize (obj);
767 modest_folder_view_set_account_store (TnyAccountStoreView *self, TnyAccountStore *account_store)
769 ModestFolderViewPrivate *priv;
772 g_return_if_fail (MODEST_IS_FOLDER_VIEW (self));
773 g_return_if_fail (TNY_IS_ACCOUNT_STORE (account_store));
775 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
776 device = tny_account_store_get_device (account_store);
778 if (G_UNLIKELY (priv->account_store)) {
780 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store),
781 priv->account_inserted_signal))
782 g_signal_handler_disconnect (G_OBJECT (priv->account_store),
783 priv->account_inserted_signal);
784 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store),
785 priv->account_removed_signal))
786 g_signal_handler_disconnect (G_OBJECT (priv->account_store),
787 priv->account_removed_signal);
788 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store),
789 priv->account_changed_signal))
790 g_signal_handler_disconnect (G_OBJECT (priv->account_store),
791 priv->account_changed_signal);
792 g_object_unref (G_OBJECT (priv->account_store));
795 priv->account_store = g_object_ref (G_OBJECT (account_store));
797 priv->account_removed_signal =
798 g_signal_connect (G_OBJECT(account_store), "account_removed",
799 G_CALLBACK (on_account_removed), self);
801 priv->account_inserted_signal =
802 g_signal_connect (G_OBJECT(account_store), "account_inserted",
803 G_CALLBACK (on_account_inserted), self);
805 priv->account_changed_signal =
806 g_signal_connect (G_OBJECT(account_store), "account_changed",
807 G_CALLBACK (on_account_changed), self);
809 modest_folder_view_update_model (MODEST_FOLDER_VIEW (self), account_store);
811 g_object_unref (G_OBJECT (device));
815 on_account_inserted (TnyAccountStore *account_store,
819 ModestFolderViewPrivate *priv;
820 GtkTreeModel *sort_model, *filter_model;
822 /* Ignore transport account insertions, we're not showing them
823 in the folder view */
824 if (TNY_IS_TRANSPORT_ACCOUNT (account))
827 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (user_data);
829 /* If we're adding a new account, and there is no previous
830 one, we need to select the visible server account */
831 if (priv->style == MODEST_FOLDER_VIEW_STYLE_SHOW_ONE &&
832 !priv->visible_account_id)
833 modest_widget_memory_restore (modest_runtime_get_conf(),
834 G_OBJECT (user_data),
835 MODEST_CONF_FOLDER_VIEW_KEY);
837 /* Get the inner model */
838 filter_model = gtk_tree_view_get_model (GTK_TREE_VIEW (user_data));
839 sort_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (filter_model));
841 /* Insert the account in the model */
842 tny_list_append (TNY_LIST (gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (sort_model))),
849 on_account_changed (TnyAccountStore *account_store,
850 TnyAccount *tny_account,
854 ModestFolderViewPrivate *priv;
855 GtkTreeModel *sort_model, *filter_model;
857 /* Ignore transport account insertions, we're not showing them
858 in the folder view */
859 if (TNY_IS_TRANSPORT_ACCOUNT (tny_account))
862 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (user_data);
864 /* Get the inner model */
865 filter_model = gtk_tree_view_get_model (GTK_TREE_VIEW (user_data));
866 sort_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (filter_model));
868 /* Remove the account from the model */
869 tny_list_remove (TNY_LIST (gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (sort_model))),
870 G_OBJECT (tny_account));
872 /* Insert the account in the model */
873 tny_list_append (TNY_LIST (gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (sort_model))),
874 G_OBJECT (tny_account));
880 on_account_removed (TnyAccountStore *account_store,
884 ModestFolderView *self = NULL;
885 ModestFolderViewPrivate *priv;
886 GtkTreeModel *sort_model, *filter_model;
887 GtkTreeSelection *sel = NULL;
889 /* Ignore transport account removals, we're not showing them
890 in the folder view */
891 if (TNY_IS_TRANSPORT_ACCOUNT (account))
894 self = MODEST_FOLDER_VIEW (user_data);
895 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
897 /* Invalidate the cur_folder_store only if the selected folder
898 belongs to the account that is being removed */
899 if (priv->cur_folder_store) {
900 TnyAccount *selected_folder_account = NULL;
902 if (TNY_IS_FOLDER (priv->cur_folder_store)) {
903 selected_folder_account =
904 tny_folder_get_account (TNY_FOLDER (priv->cur_folder_store));
906 selected_folder_account =
907 TNY_ACCOUNT (g_object_ref (priv->cur_folder_store));
910 if (selected_folder_account == account) {
911 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
912 gtk_tree_selection_unselect_all (sel);
914 g_object_unref (selected_folder_account);
917 /* Invalidate row to select only if the folder to select
918 belongs to the account that is being removed*/
919 if (priv->folder_to_select) {
920 TnyAccount *folder_to_select_account = NULL;
922 folder_to_select_account = tny_folder_get_account (priv->folder_to_select);
923 if (folder_to_select_account == account) {
924 /* modest_folder_view_disable_next_folder_selection (self); */
925 g_object_unref (priv->folder_to_select);
926 priv->folder_to_select = NULL;
928 g_object_unref (folder_to_select_account);
931 /* Remove the account from the model */
932 filter_model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
933 sort_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (filter_model));
934 tny_list_remove (TNY_LIST (gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (sort_model))),
937 /* If the removed account is the currently viewed one then
938 clear the configuration value. The new visible account will be the default account */
939 if (priv->visible_account_id &&
940 !strcmp (priv->visible_account_id, tny_account_get_id (account))) {
942 /* Clear the current visible account_id */
943 modest_folder_view_set_account_id_of_visible_server_account (self, NULL);
945 /* Call the restore method, this will set the new visible account */
946 modest_widget_memory_restore (modest_runtime_get_conf(), G_OBJECT(self),
947 MODEST_CONF_FOLDER_VIEW_KEY);
950 /* Select the INBOX */
951 modest_folder_view_select_first_inbox_or_local (self);
955 modest_folder_view_set_title (ModestFolderView *self, const gchar *title)
957 GtkTreeViewColumn *col;
959 g_return_if_fail (self);
961 col = gtk_tree_view_get_column (GTK_TREE_VIEW(self), 0);
963 g_printerr ("modest: failed get column for title\n");
967 gtk_tree_view_column_set_title (col, title);
968 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self),
973 modest_folder_view_on_map (ModestFolderView *self,
974 GdkEventExpose *event,
977 ModestFolderViewPrivate *priv;
979 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
981 /* This won't happen often */
982 if (G_UNLIKELY (priv->reselect)) {
983 /* Select the first inbox or the local account if not found */
985 /* TODO: this could cause a lock at startup, so we
986 comment it for the moment. We know that this will
987 be a bug, because the INBOX is not selected, but we
988 need to rewrite some parts of Modest to avoid the
989 deathlock situation */
990 /* TODO: check if this is still the case */
991 priv->reselect = FALSE;
992 modest_folder_view_select_first_inbox_or_local (self);
993 /* Notify the display name observers */
994 g_signal_emit (G_OBJECT(self),
995 signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
999 if (priv->reexpand) {
1000 expand_root_items (self);
1001 priv->reexpand = FALSE;
1008 modest_folder_view_new (TnyFolderStoreQuery *query)
1011 ModestFolderViewPrivate *priv;
1012 GtkTreeSelection *sel;
1014 self = G_OBJECT (g_object_new (MODEST_TYPE_FOLDER_VIEW, NULL));
1015 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
1018 priv->query = g_object_ref (query);
1020 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
1021 priv->changed_signal = g_signal_connect (sel, "changed",
1022 G_CALLBACK (on_selection_changed), self);
1024 g_signal_connect (self, "expose-event", G_CALLBACK (modest_folder_view_on_map), NULL);
1026 return GTK_WIDGET(self);
1029 /* this feels dirty; any other way to expand all the root items? */
1031 expand_root_items (ModestFolderView *self)
1034 GtkTreeModel *model;
1037 model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
1038 path = gtk_tree_path_new_first ();
1040 /* all folders should have child items, so.. */
1042 gtk_tree_view_expand_row (GTK_TREE_VIEW(self), path, FALSE);
1043 gtk_tree_path_next (path);
1044 } while (gtk_tree_model_get_iter (model, &iter, path));
1046 gtk_tree_path_free (path);
1050 * We use this function to implement the
1051 * MODEST_FOLDER_VIEW_STYLE_SHOW_ONE style. We only show the default
1052 * account in this case, and the local folders.
1055 filter_row (GtkTreeModel *model,
1059 ModestFolderViewPrivate *priv;
1060 gboolean retval = TRUE;
1061 TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
1062 GObject *instance = NULL;
1063 const gchar *id = NULL;
1065 gboolean found = FALSE;
1066 gboolean cleared = FALSE;
1068 g_return_val_if_fail (MODEST_IS_FOLDER_VIEW (data), FALSE);
1069 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (data);
1071 gtk_tree_model_get (model, iter,
1072 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
1073 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
1076 /* Do not show if there is no instance, this could indeed
1077 happen when the model is being modified while it's being
1078 drawn. This could occur for example when moving folders
1083 if (type == TNY_FOLDER_TYPE_ROOT) {
1084 /* TNY_FOLDER_TYPE_ROOT means that the instance is an
1085 account instead of a folder. */
1086 if (TNY_IS_ACCOUNT (instance)) {
1087 TnyAccount *acc = TNY_ACCOUNT (instance);
1088 const gchar *account_id = tny_account_get_id (acc);
1090 /* If it isn't a special folder,
1091 * don't show it unless it is the visible account: */
1092 if (priv->style == MODEST_FOLDER_VIEW_STYLE_SHOW_ONE &&
1093 !modest_tny_account_is_virtual_local_folders (acc) &&
1094 strcmp (account_id, MODEST_MMC_ACCOUNT_ID)) {
1096 /* Show only the visible account id */
1097 if (priv->visible_account_id) {
1098 if (strcmp (account_id, priv->visible_account_id))
1105 /* Never show these to the user. They are merged into one folder
1106 * in the local-folders account instead: */
1107 if (retval && MODEST_IS_TNY_OUTBOX_ACCOUNT (acc))
1112 /* Check hiding (if necessary) */
1113 cleared = modest_email_clipboard_cleared (priv->clipboard);
1114 if ((retval) && (!cleared) && (TNY_IS_FOLDER (instance))) {
1115 id = tny_folder_get_id (TNY_FOLDER(instance));
1116 if (priv->hidding_ids != NULL)
1117 for (i=0; i < priv->n_selected && !found; i++)
1118 if (priv->hidding_ids[i] != NULL && id != NULL)
1119 found = (!strcmp (priv->hidding_ids[i], id));
1125 /* If this is a move to dialog, hide Sent, Outbox and Drafts
1126 folder as no message can be move there according to UI specs */
1127 if (!priv->show_non_move)
1131 case TNY_FOLDER_TYPE_OUTBOX:
1132 case TNY_FOLDER_TYPE_SENT:
1133 case TNY_FOLDER_TYPE_DRAFTS:
1136 case TNY_FOLDER_TYPE_UNKNOWN:
1137 case TNY_FOLDER_TYPE_NORMAL:
1138 type = modest_tny_folder_guess_folder_type(TNY_FOLDER(instance));
1139 if (type == TNY_FOLDER_TYPE_OUTBOX || type == TNY_FOLDER_TYPE_SENT
1140 || type == TNY_FOLDER_TYPE_DRAFTS)
1151 g_object_unref (instance);
1158 modest_folder_view_update_model (ModestFolderView *self,
1159 TnyAccountStore *account_store)
1161 ModestFolderViewPrivate *priv;
1162 GtkTreeModel *model /* , *old_model */;
1163 /* TnyAccount *local_account; */
1164 TnyList *model_as_list;
1166 g_return_val_if_fail (MODEST_IS_FOLDER_VIEW (self), FALSE);
1167 g_return_val_if_fail (account_store, FALSE);
1169 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
1171 /* Notify that there is no folder selected */
1172 g_signal_emit (G_OBJECT(self),
1173 signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
1175 if (priv->cur_folder_store) {
1176 g_object_unref (priv->cur_folder_store);
1177 priv->cur_folder_store = NULL;
1180 /* FIXME: the local accounts are not shown when the query
1181 selects only the subscribed folders. */
1182 /* model = tny_gtk_folder_store_tree_model_new (TRUE, priv->query); */
1183 model = tny_gtk_folder_store_tree_model_new (NULL);
1185 /* Deal with the model via its TnyList Interface,
1186 * filling the TnyList via a get_accounts() call: */
1187 model_as_list = TNY_LIST(model);
1189 /* Get the accounts: */
1190 tny_account_store_get_accounts (TNY_ACCOUNT_STORE(account_store),
1192 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
1193 g_object_unref (model_as_list);
1194 model_as_list = NULL;
1196 GtkTreeModel *filter_model = NULL, *sortable = NULL;
1198 sortable = gtk_tree_model_sort_new_with_model (model);
1199 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(sortable),
1200 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
1201 GTK_SORT_ASCENDING);
1202 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (sortable),
1203 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
1204 cmp_rows, NULL, NULL);
1206 /* Create filter model */
1207 filter_model = gtk_tree_model_filter_new (sortable, NULL);
1208 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (filter_model),
1214 gtk_tree_view_set_model (GTK_TREE_VIEW(self), filter_model);
1215 g_signal_connect (G_OBJECT(filter_model), "row-inserted",
1216 (GCallback) on_row_inserted_maybe_select_folder, self);
1219 g_object_unref (model);
1220 g_object_unref (filter_model);
1221 g_object_unref (sortable);
1223 /* Force a reselection of the INBOX next time the widget is shown */
1224 priv->reselect = TRUE;
1231 on_selection_changed (GtkTreeSelection *sel, gpointer user_data)
1233 GtkTreeModel *model = NULL;
1234 TnyFolderStore *folder = NULL;
1236 ModestFolderView *tree_view = NULL;
1237 ModestFolderViewPrivate *priv = NULL;
1238 gboolean selected = FALSE;
1240 g_return_if_fail (sel);
1241 g_return_if_fail (user_data);
1243 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(user_data);
1245 selected = gtk_tree_selection_get_selected (sel, &model, &iter);
1247 /* Notify the display name observers */
1248 g_signal_emit (G_OBJECT(user_data),
1249 signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
1252 tree_view = MODEST_FOLDER_VIEW (user_data);
1255 gtk_tree_model_get (model, &iter,
1256 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder,
1259 /* If the folder is the same do not notify */
1260 if (folder && priv->cur_folder_store == folder) {
1261 g_object_unref (folder);
1266 /* Current folder was unselected */
1267 if (priv->cur_folder_store) {
1268 g_signal_emit (G_OBJECT(tree_view), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
1269 priv->cur_folder_store, FALSE);
1271 if (TNY_IS_FOLDER(priv->cur_folder_store))
1272 tny_folder_sync_async (TNY_FOLDER(priv->cur_folder_store),
1273 FALSE, NULL, NULL, NULL);
1275 /* FALSE --> don't expunge the messages */
1277 g_object_unref (priv->cur_folder_store);
1278 priv->cur_folder_store = NULL;
1281 /* New current references */
1282 priv->cur_folder_store = folder;
1284 /* New folder has been selected */
1285 g_signal_emit (G_OBJECT(tree_view),
1286 signals[FOLDER_SELECTION_CHANGED_SIGNAL],
1287 0, priv->cur_folder_store, TRUE);
1291 modest_folder_view_get_selected (ModestFolderView *self)
1293 ModestFolderViewPrivate *priv;
1295 g_return_val_if_fail (self, NULL);
1297 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
1298 if (priv->cur_folder_store)
1299 g_object_ref (priv->cur_folder_store);
1301 return priv->cur_folder_store;
1305 get_cmp_rows_type_pos (GObject *folder)
1307 /* Remote accounts -> Local account -> MMC account .*/
1310 if (TNY_IS_ACCOUNT (folder) &&
1311 modest_tny_account_is_virtual_local_folders (
1312 TNY_ACCOUNT (folder))) {
1314 } else if (TNY_IS_ACCOUNT (folder)) {
1315 TnyAccount *account = TNY_ACCOUNT (folder);
1316 const gchar *account_id = tny_account_get_id (account);
1317 if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID))
1323 printf ("DEBUG: %s: unexpected type.\n", __FUNCTION__);
1324 return -1; /* Should never happen */
1329 get_cmp_subfolder_type_pos (TnyFolderType t)
1331 /* Inbox, Outbox, Drafts, Sent, User */
1335 case TNY_FOLDER_TYPE_INBOX:
1338 case TNY_FOLDER_TYPE_OUTBOX:
1341 case TNY_FOLDER_TYPE_DRAFTS:
1344 case TNY_FOLDER_TYPE_SENT:
1353 * This function orders the mail accounts according to these rules:
1354 * 1st - remote accounts
1355 * 2nd - local account
1359 cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2,
1363 gchar *name1 = NULL;
1364 gchar *name2 = NULL;
1365 TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
1366 TnyFolderType type2 = TNY_FOLDER_TYPE_UNKNOWN;
1367 GObject *folder1 = NULL;
1368 GObject *folder2 = NULL;
1370 gtk_tree_model_get (tree_model, iter1,
1371 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name1,
1372 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
1373 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder1,
1375 gtk_tree_model_get (tree_model, iter2,
1376 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name2,
1377 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type2,
1378 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder2,
1381 /* Return if we get no folder. This could happen when folder
1382 operations are happening. The model is updated after the
1383 folder copy/move actually occurs, so there could be
1384 situations where the model to be drawn is not correct */
1385 if (!folder1 || !folder2)
1388 if (type == TNY_FOLDER_TYPE_ROOT) {
1389 /* Compare the types, so that
1390 * Remote accounts -> Local account -> MMC account .*/
1391 const gint pos1 = get_cmp_rows_type_pos (folder1);
1392 const gint pos2 = get_cmp_rows_type_pos (folder2);
1393 /* printf ("DEBUG: %s:\n type1=%s, pos1=%d\n type2=%s, pos2=%d\n",
1394 __FUNCTION__, G_OBJECT_TYPE_NAME(folder1), pos1, G_OBJECT_TYPE_NAME(folder2), pos2); */
1397 else if (pos1 > pos2)
1400 /* Compare items of the same type: */
1402 TnyAccount *account1 = NULL;
1403 if (TNY_IS_ACCOUNT (folder1))
1404 account1 = TNY_ACCOUNT (folder1);
1406 TnyAccount *account2 = NULL;
1407 if (TNY_IS_ACCOUNT (folder2))
1408 account2 = TNY_ACCOUNT (folder2);
1410 const gchar *account_id = account1 ? tny_account_get_id (account1) : NULL;
1411 const gchar *account_id2 = account2 ? tny_account_get_id (account2) : NULL;
1413 if (!account_id && !account_id2) {
1415 } else if (!account_id) {
1417 } else if (!account_id2) {
1419 } else if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID)) {
1422 cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
1426 gint cmp1 = 0, cmp2 = 0;
1427 /* get the parent to know if it's a local folder */
1430 gboolean has_parent;
1431 has_parent = gtk_tree_model_iter_parent (tree_model, &parent, iter1);
1433 GObject *parent_folder;
1434 TnyFolderType parent_type = TNY_FOLDER_TYPE_UNKNOWN;
1435 gtk_tree_model_get (tree_model, &parent,
1436 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &parent_type,
1437 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &parent_folder,
1439 if ((parent_type == TNY_FOLDER_TYPE_ROOT) &&
1440 TNY_IS_ACCOUNT (parent_folder) &&
1441 modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (parent_folder))) {
1442 cmp1 = get_cmp_subfolder_type_pos (modest_tny_folder_get_local_or_mmc_folder_type (TNY_FOLDER (folder1)));
1443 cmp2 = get_cmp_subfolder_type_pos (modest_tny_folder_get_local_or_mmc_folder_type (TNY_FOLDER (folder2)));
1445 g_object_unref (parent_folder);
1448 /* if they are not local folders */
1450 cmp1 = get_cmp_subfolder_type_pos (tny_folder_get_folder_type (TNY_FOLDER (folder1)));
1451 cmp2 = get_cmp_subfolder_type_pos (tny_folder_get_folder_type (TNY_FOLDER (folder2)));
1455 cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
1457 cmp = (cmp1 - cmp2);
1462 g_object_unref(G_OBJECT(folder1));
1464 g_object_unref(G_OBJECT(folder2));
1472 /*****************************************************************************/
1473 /* DRAG and DROP stuff */
1474 /*****************************************************************************/
1476 * This function fills the #GtkSelectionData with the row and the
1477 * model that has been dragged. It's called when this widget is a
1478 * source for dnd after the event drop happened
1481 on_drag_data_get (GtkWidget *widget,
1482 GdkDragContext *context,
1483 GtkSelectionData *selection_data,
1488 GtkTreeSelection *selection;
1489 GtkTreeModel *model;
1491 GtkTreePath *source_row;
1493 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
1494 gtk_tree_selection_get_selected (selection, &model, &iter);
1495 source_row = gtk_tree_model_get_path (model, &iter);
1497 gtk_tree_set_row_drag_data (selection_data,
1501 gtk_tree_path_free (source_row);
1504 typedef struct _DndHelper {
1505 gboolean delete_source;
1506 GtkTreePath *source_row;
1507 GdkDragContext *context;
1513 * This function is the callback of the
1514 * modest_mail_operation_xfer_msgs () and
1515 * modest_mail_operation_xfer_folder() calls. We check here if the
1516 * message/folder was correctly asynchronously transferred. The reason
1517 * to use the same callback is that the code is the same, it only has
1518 * to check that the operation went fine and then finalize the drag
1522 xfer_cb (ModestMailOperation *mail_op,
1528 helper = (DndHelper *) user_data;
1530 if (modest_mail_operation_get_status (mail_op) ==
1531 MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
1537 /* Notify the drag source. Never call delete, the monitor will
1538 do the job if needed */
1539 gtk_drag_finish (helper->context, success, FALSE, helper->time);
1541 /* Free the helper */
1542 gtk_tree_path_free (helper->source_row);
1543 g_slice_free (DndHelper, helper);
1546 /* get the folder for the row the treepath refers to. */
1547 /* folder must be unref'd */
1549 tree_path_to_folder (GtkTreeModel *model, GtkTreePath *path)
1552 TnyFolder *folder = NULL;
1554 if (gtk_tree_model_get_iter (model,&iter, path))
1555 gtk_tree_model_get (model, &iter,
1556 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder,
1562 * This function is used by drag_data_received_cb to manage drag and
1563 * drop of a header, i.e, and drag from the header view to the folder
1567 drag_and_drop_from_header_view (GtkTreeModel *source_model,
1568 GtkTreeModel *dest_model,
1569 GtkTreePath *dest_row,
1570 GtkSelectionData *selection_data,
1573 TnyList *headers = NULL;
1574 TnyFolder *folder = NULL;
1575 ModestMailOperation *mail_op = NULL;
1576 GtkTreeIter source_iter, dest_iter;
1577 ModestWindowMgr *mgr = NULL;
1578 ModestWindow *main_win = NULL;
1579 gchar **uris, **tmp;
1582 /* Build the list of headers */
1583 mgr = modest_runtime_get_window_mgr ();
1584 headers = tny_simple_list_new ();
1585 uris = modest_dnd_selection_data_get_paths (selection_data);
1588 while (*tmp != NULL) {
1593 path = gtk_tree_path_new_from_string (*tmp);
1594 gtk_tree_model_get_iter (source_model, &source_iter, path);
1595 gtk_tree_model_get (source_model, &source_iter,
1596 TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN,
1599 /* Do not enable d&d of headers already opened */
1600 if (!modest_window_mgr_find_registered_header(mgr, header, NULL))
1601 tny_list_append (headers, G_OBJECT (header));
1603 /* Free and go on */
1604 gtk_tree_path_free (path);
1605 g_object_unref (header);
1610 /* Get the target folder */
1611 gtk_tree_model_get_iter (dest_model, &dest_iter, dest_row);
1612 gtk_tree_model_get (dest_model, &dest_iter,
1613 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
1616 /* Ask for confirmation to move */
1617 main_win = modest_window_mgr_get_main_window(mgr);
1618 response = modest_ui_actions_msgs_move_to_confirmation (GTK_WINDOW(main_win), folder,
1620 if (response == GTK_RESPONSE_CANCEL)
1623 /* Transfer messages */
1624 mail_op = modest_mail_operation_new_with_error_handling (MODEST_MAIL_OPERATION_TYPE_RECEIVE,
1626 modest_ui_actions_move_folder_error_handler,
1629 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
1632 modest_mail_operation_xfer_msgs (mail_op,
1635 helper->delete_source,
1640 if (G_IS_OBJECT(mail_op))
1641 g_object_unref (G_OBJECT (mail_op));
1642 if (G_IS_OBJECT(folder))
1643 g_object_unref (G_OBJECT (folder));
1644 if (G_IS_OBJECT(headers))
1645 g_object_unref (headers);
1649 * This function is used by drag_data_received_cb to manage drag and
1650 * drop of a folder, i.e, and drag from the folder view to the same
1654 drag_and_drop_from_folder_view (GtkTreeModel *source_model,
1655 GtkTreeModel *dest_model,
1656 GtkTreePath *dest_row,
1657 GtkSelectionData *selection_data,
1660 ModestMailOperation *mail_op = NULL;
1661 GtkTreeIter dest_iter, iter;
1662 TnyFolderStore *dest_folder = NULL;
1663 TnyFolder *folder = NULL;
1664 gboolean forbidden = FALSE;
1667 /* check the folder rules for the destination */
1668 folder = tree_path_to_folder (dest_model, dest_row);
1669 if (TNY_IS_FOLDER(folder)) {
1670 ModestTnyFolderRules rules =
1671 modest_tny_folder_get_rules (folder);
1672 forbidden = rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE;
1673 } else if (TNY_IS_FOLDER_STORE(folder)) {
1674 /* enable local root as destination for folders */
1675 if (!MODEST_IS_TNY_LOCAL_FOLDERS_ACCOUNT (folder)
1676 && TNY_IS_ACCOUNT (folder))
1679 g_object_unref (folder);
1682 /* check the folder rules for the source */
1683 folder = tree_path_to_folder (source_model, helper->source_row);
1684 if (TNY_IS_FOLDER(folder)) {
1685 ModestTnyFolderRules rules =
1686 modest_tny_folder_get_rules (folder);
1687 forbidden = rules & MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE;
1690 g_object_unref (folder);
1694 /* Check if the drag is possible */
1695 if (forbidden || !gtk_tree_path_compare (helper->source_row, dest_row)) {
1696 gtk_drag_finish (helper->context, FALSE, FALSE, helper->time);
1697 gtk_tree_path_free (helper->source_row);
1698 g_slice_free (DndHelper, helper);
1703 gtk_tree_model_get_iter (dest_model, &dest_iter, dest_row);
1704 gtk_tree_model_get (dest_model, &dest_iter,
1705 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
1707 gtk_tree_model_get_iter (source_model, &iter, helper->source_row);
1708 gtk_tree_model_get (source_model, &iter,
1709 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
1712 /* Offer the connection dialog if necessary, for the destination parent folder and source folder: */
1713 if (modest_platform_connect_and_wait_if_network_folderstore (NULL, dest_folder) &&
1714 modest_platform_connect_and_wait_if_network_folderstore (NULL, TNY_FOLDER_STORE (folder))) {
1715 ModestWindowMgr *mgr = modest_runtime_get_window_mgr ();
1717 /* Do the mail operation */
1719 modest_mail_operation_new_with_error_handling (MODEST_MAIL_OPERATION_TYPE_RECEIVE,
1720 G_OBJECT (modest_window_mgr_get_main_window (mgr)),
1721 modest_ui_actions_move_folder_error_handler,
1724 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
1727 modest_mail_operation_xfer_folder (mail_op,
1730 helper->delete_source,
1734 g_object_unref (G_OBJECT (mail_op));
1738 g_object_unref (G_OBJECT (dest_folder));
1739 g_object_unref (G_OBJECT (folder));
1743 * This function receives the data set by the "drag-data-get" signal
1744 * handler. This information comes within the #GtkSelectionData. This
1745 * function will manage both the drags of folders of the treeview and
1746 * drags of headers of the header view widget.
1749 on_drag_data_received (GtkWidget *widget,
1750 GdkDragContext *context,
1753 GtkSelectionData *selection_data,
1758 GtkWidget *source_widget;
1759 GtkTreeModel *dest_model, *source_model;
1760 GtkTreePath *source_row, *dest_row;
1761 GtkTreeViewDropPosition pos;
1762 gboolean success = FALSE, delete_source = FALSE;
1763 DndHelper *helper = NULL;
1765 /* Do not allow further process */
1766 g_signal_stop_emission_by_name (widget, "drag-data-received");
1767 source_widget = gtk_drag_get_source_widget (context);
1769 /* Get the action */
1770 if (context->action == GDK_ACTION_MOVE) {
1771 delete_source = TRUE;
1773 /* Notify that there is no folder selected. We need to
1774 do this in order to update the headers view (and
1775 its monitors, because when moving, the old folder
1776 won't longer exist. We can not wait for the end of
1777 the operation, because the operation won't start if
1778 the folder is in use */
1779 if (source_widget == widget) {
1780 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
1781 gtk_tree_selection_unselect_all (sel);
1785 /* Check if the get_data failed */
1786 if (selection_data == NULL || selection_data->length < 0)
1787 gtk_drag_finish (context, success, FALSE, time);
1789 /* Select the destination model */
1790 dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
1792 /* Get the path to the destination row. Can not call
1793 gtk_tree_view_get_drag_dest_row() because the source row
1794 is not selected anymore */
1795 gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget), x, y,
1798 /* Only allow drops IN other rows */
1800 pos == GTK_TREE_VIEW_DROP_BEFORE ||
1801 pos == GTK_TREE_VIEW_DROP_AFTER)
1802 gtk_drag_finish (context, success, FALSE, time);
1804 /* Create the helper */
1805 helper = g_slice_new0 (DndHelper);
1806 helper->delete_source = delete_source;
1807 helper->context = context;
1808 helper->time = time;
1810 /* Drags from the header view */
1811 if (source_widget != widget) {
1812 source_model = gtk_tree_view_get_model (GTK_TREE_VIEW (source_widget));
1814 drag_and_drop_from_header_view (source_model,
1820 /* Get the source model and row */
1821 gtk_tree_get_row_drag_data (selection_data,
1824 helper->source_row = gtk_tree_path_copy (source_row);
1826 drag_and_drop_from_folder_view (source_model,
1832 gtk_tree_path_free (source_row);
1836 gtk_tree_path_free (dest_row);
1840 * We define a "drag-drop" signal handler because we do not want to
1841 * use the default one, because the default one always calls
1842 * gtk_drag_finish and we prefer to do it in the "drag-data-received"
1843 * signal handler, because there we have all the information available
1844 * to know if the dnd was a success or not.
1847 drag_drop_cb (GtkWidget *widget,
1848 GdkDragContext *context,
1856 if (!context->targets)
1859 /* Check if we're dragging a folder row */
1860 target = gtk_drag_dest_find_target (widget, context, NULL);
1862 /* Request the data from the source. */
1863 gtk_drag_get_data(widget, context, target, time);
1869 * This function expands a node of a tree view if it's not expanded
1870 * yet. Not sure why it needs the threads stuff, but gtk+`example code
1871 * does that, so that's why they're here.
1874 expand_row_timeout (gpointer data)
1876 GtkTreeView *tree_view = data;
1877 GtkTreePath *dest_path = NULL;
1878 GtkTreeViewDropPosition pos;
1879 gboolean result = FALSE;
1881 GDK_THREADS_ENTER ();
1883 gtk_tree_view_get_drag_dest_row (tree_view,
1888 (pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER ||
1889 pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)) {
1890 gtk_tree_view_expand_row (tree_view, dest_path, FALSE);
1891 gtk_tree_path_free (dest_path);
1895 gtk_tree_path_free (dest_path);
1900 GDK_THREADS_LEAVE ();
1906 * This function is called whenever the pointer is moved over a widget
1907 * while dragging some data. It installs a timeout that will expand a
1908 * node of the treeview if not expanded yet. This function also calls
1909 * gdk_drag_status in order to set the suggested action that will be
1910 * used by the "drag-data-received" signal handler to know if we
1911 * should do a move or just a copy of the data.
1914 on_drag_motion (GtkWidget *widget,
1915 GdkDragContext *context,
1921 GtkTreeViewDropPosition pos;
1922 GtkTreePath *dest_row;
1923 GtkTreeModel *dest_model;
1924 ModestFolderViewPrivate *priv;
1925 GdkDragAction suggested_action;
1926 gboolean valid_location = FALSE;
1929 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (widget);
1931 if (priv->timer_expander != 0) {
1932 g_source_remove (priv->timer_expander);
1933 priv->timer_expander = 0;
1936 gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
1941 /* Do not allow drops between folders */
1943 pos == GTK_TREE_VIEW_DROP_BEFORE ||
1944 pos == GTK_TREE_VIEW_DROP_AFTER) {
1945 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW (widget), NULL, 0);
1946 gdk_drag_status(context, 0, time);
1947 valid_location = FALSE;
1950 valid_location = TRUE;
1953 /* Check that the destination folder is writable */
1954 dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
1955 folder = tree_path_to_folder (dest_model, dest_row);
1957 ModestTnyFolderRules rules = modest_tny_folder_get_rules(folder);
1959 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE) {
1960 valid_location = FALSE;
1963 g_object_unref (folder);
1966 /* Expand the selected row after 1/2 second */
1967 if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), dest_row)) {
1968 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), dest_row, pos);
1969 priv->timer_expander = g_timeout_add (500, expand_row_timeout, widget);
1972 /* Select the desired action. By default we pick MOVE */
1973 suggested_action = GDK_ACTION_MOVE;
1975 if (context->actions == GDK_ACTION_COPY)
1976 gdk_drag_status(context, GDK_ACTION_COPY, time);
1977 else if (context->actions == GDK_ACTION_MOVE)
1978 gdk_drag_status(context, GDK_ACTION_MOVE, time);
1979 else if (context->actions & suggested_action)
1980 gdk_drag_status(context, suggested_action, time);
1982 gdk_drag_status(context, GDK_ACTION_DEFAULT, time);
1986 gtk_tree_path_free (dest_row);
1987 g_signal_stop_emission_by_name (widget, "drag-motion");
1989 return valid_location;
1993 * This function sets the treeview as a source and a target for dnd
1994 * events. It also connects all the requirede signals.
1997 setup_drag_and_drop (GtkTreeView *self)
1999 /* Set up the folder view as a dnd destination. Set only the
2000 highlight flag, otherwise gtk will have a different
2002 gtk_drag_dest_set (GTK_WIDGET (self),
2003 GTK_DEST_DEFAULT_HIGHLIGHT,
2004 folder_view_drag_types,
2005 G_N_ELEMENTS (folder_view_drag_types),
2006 GDK_ACTION_MOVE | GDK_ACTION_COPY);
2008 g_signal_connect (G_OBJECT (self),
2009 "drag_data_received",
2010 G_CALLBACK (on_drag_data_received),
2014 /* Set up the treeview as a dnd source */
2015 gtk_drag_source_set (GTK_WIDGET (self),
2017 folder_view_drag_types,
2018 G_N_ELEMENTS (folder_view_drag_types),
2019 GDK_ACTION_MOVE | GDK_ACTION_COPY);
2021 g_signal_connect (G_OBJECT (self),
2023 G_CALLBACK (on_drag_motion),
2026 g_signal_connect (G_OBJECT (self),
2028 G_CALLBACK (on_drag_data_get),
2031 g_signal_connect (G_OBJECT (self),
2033 G_CALLBACK (drag_drop_cb),
2038 * This function manages the navigation through the folders using the
2039 * keyboard or the hardware keys in the device
2042 on_key_pressed (GtkWidget *self,
2046 GtkTreeSelection *selection;
2048 GtkTreeModel *model;
2049 gboolean retval = FALSE;
2051 /* Up and Down are automatically managed by the treeview */
2052 if (event->keyval == GDK_Return) {
2053 /* Expand/Collapse the selected row */
2054 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
2055 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
2058 path = gtk_tree_model_get_path (model, &iter);
2060 if (gtk_tree_view_row_expanded (GTK_TREE_VIEW (self), path))
2061 gtk_tree_view_collapse_row (GTK_TREE_VIEW (self), path);
2063 gtk_tree_view_expand_row (GTK_TREE_VIEW (self), path, FALSE);
2064 gtk_tree_path_free (path);
2066 /* No further processing */
2074 * We listen to the changes in the local folder account name key,
2075 * because we want to show the right name in the view. The local
2076 * folder account name corresponds to the device name in the Maemo
2077 * version. We do this because we do not want to query gconf on each
2078 * tree view refresh. It's better to cache it and change whenever
2082 on_configuration_key_changed (ModestConf* conf,
2084 ModestConfEvent event,
2085 ModestConfNotificationId id,
2086 ModestFolderView *self)
2088 ModestFolderViewPrivate *priv;
2091 g_return_if_fail (MODEST_IS_FOLDER_VIEW (self));
2092 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
2094 /* Do not listen for changes in other namespaces */
2095 if (priv->notification_id != id)
2098 if (!strcmp (key, MODEST_CONF_DEVICE_NAME)) {
2099 g_free (priv->local_account_name);
2101 if (event == MODEST_CONF_EVENT_KEY_UNSET)
2102 priv->local_account_name = g_strdup (MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME);
2104 priv->local_account_name = modest_conf_get_string (modest_runtime_get_conf(),
2105 MODEST_CONF_DEVICE_NAME, NULL);
2107 /* Force a redraw */
2108 #if GTK_CHECK_VERSION(2, 8, 0)
2109 GtkTreeViewColumn * tree_column;
2111 tree_column = gtk_tree_view_get_column (GTK_TREE_VIEW (self),
2112 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN);
2113 gtk_tree_view_column_queue_resize (tree_column);
2115 gtk_widget_queue_draw (GTK_WIDGET (self));
2121 modest_folder_view_set_style (ModestFolderView *self,
2122 ModestFolderViewStyle style)
2124 ModestFolderViewPrivate *priv;
2126 g_return_if_fail (self);
2128 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
2130 priv->style = style;
2134 modest_folder_view_set_account_id_of_visible_server_account (ModestFolderView *self,
2135 const gchar *account_id)
2137 ModestFolderViewPrivate *priv;
2138 GtkTreeModel *model;
2140 g_return_if_fail (self);
2142 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
2144 /* This will be used by the filter_row callback,
2145 * to decided which rows to show: */
2146 if (priv->visible_account_id) {
2147 g_free (priv->visible_account_id);
2148 priv->visible_account_id = NULL;
2151 priv->visible_account_id = g_strdup (account_id);
2154 model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
2155 if (GTK_IS_TREE_MODEL_FILTER (model))
2156 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (model));
2158 /* Save settings to gconf */
2159 modest_widget_memory_save (modest_runtime_get_conf (), G_OBJECT(self),
2160 MODEST_CONF_FOLDER_VIEW_KEY);
2164 modest_folder_view_get_account_id_of_visible_server_account (ModestFolderView *self)
2166 ModestFolderViewPrivate *priv;
2168 g_return_val_if_fail (self, NULL);
2170 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
2172 return (const gchar *) priv->visible_account_id;
2176 find_inbox_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreeIter *inbox_iter)
2180 TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
2182 gtk_tree_model_get (model, iter,
2183 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN,
2186 gboolean result = FALSE;
2187 if (type == TNY_FOLDER_TYPE_INBOX) {
2191 *inbox_iter = *iter;
2195 if (gtk_tree_model_iter_children (model, &child, iter)) {
2196 if (find_inbox_iter (model, &child, inbox_iter))
2200 } while (gtk_tree_model_iter_next (model, iter));
2209 modest_folder_view_select_first_inbox_or_local (ModestFolderView *self)
2211 GtkTreeModel *model;
2212 GtkTreeIter iter, inbox_iter;
2213 GtkTreeSelection *sel;
2214 GtkTreePath *path = NULL;
2216 model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
2220 expand_root_items (self);
2221 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
2223 gtk_tree_model_get_iter_first (model, &iter);
2225 if (find_inbox_iter (model, &iter, &inbox_iter))
2226 path = gtk_tree_model_get_path (model, &inbox_iter);
2228 path = gtk_tree_path_new_first ();
2230 /* Select the row and free */
2231 gtk_tree_view_set_cursor (GTK_TREE_VIEW (self), path, NULL, FALSE);
2232 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (self), path, NULL, FALSE, 0.0, 0.0);
2233 gtk_tree_path_free (path);
2236 gtk_widget_grab_focus (GTK_WIDGET(self));
2242 find_folder_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreeIter *folder_iter,
2247 TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
2248 TnyFolder* a_folder;
2251 gtk_tree_model_get (model, iter,
2252 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &a_folder,
2253 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name,
2254 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
2258 if (folder == a_folder) {
2259 g_object_unref (a_folder);
2260 *folder_iter = *iter;
2263 g_object_unref (a_folder);
2265 if (gtk_tree_model_iter_children (model, &child, iter)) {
2266 if (find_folder_iter (model, &child, folder_iter, folder))
2270 } while (gtk_tree_model_iter_next (model, iter));
2277 on_row_inserted_maybe_select_folder (GtkTreeModel *tree_model, GtkTreePath *path, GtkTreeIter *iter,
2278 ModestFolderView *self)
2280 ModestFolderViewPrivate *priv = NULL;
2281 GtkTreeSelection *sel;
2282 TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
2283 GObject *instance = NULL;
2285 if (!MODEST_IS_FOLDER_VIEW(self))
2288 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
2290 priv->reexpand = TRUE;
2292 gtk_tree_model_get (tree_model, iter,
2293 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
2294 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
2296 if (type == TNY_FOLDER_TYPE_INBOX && priv->folder_to_select == NULL) {
2297 priv->folder_to_select = g_object_ref (instance);
2299 g_object_unref (instance);
2302 if (priv->folder_to_select) {
2304 if (!modest_folder_view_select_folder (self, priv->folder_to_select,
2307 path = gtk_tree_model_get_path (tree_model, iter);
2308 gtk_tree_view_expand_to_path (GTK_TREE_VIEW(self), path);
2310 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
2312 gtk_tree_selection_select_iter (sel, iter);
2313 gtk_tree_view_set_cursor (GTK_TREE_VIEW(self), path, NULL, FALSE);
2315 gtk_tree_path_free (path);
2320 modest_folder_view_disable_next_folder_selection (self);
2321 /* g_object_unref (priv->folder_to_select); */
2322 /* priv->folder_to_select = NULL; */
2328 modest_folder_view_disable_next_folder_selection (ModestFolderView *self)
2330 ModestFolderViewPrivate *priv = NULL;
2332 g_return_if_fail (MODEST_IS_FOLDER_VIEW (self));
2333 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
2335 if (priv->folder_to_select)
2336 g_object_unref(priv->folder_to_select);
2338 priv->folder_to_select = NULL;
2342 modest_folder_view_select_folder (ModestFolderView *self, TnyFolder *folder,
2343 gboolean after_change)
2345 GtkTreeModel *model;
2346 GtkTreeIter iter, folder_iter;
2347 GtkTreeSelection *sel;
2348 ModestFolderViewPrivate *priv = NULL;
2350 g_return_val_if_fail (MODEST_IS_FOLDER_VIEW (self), FALSE);
2351 g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
2353 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
2357 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
2358 gtk_tree_selection_unselect_all (sel);
2360 if (priv->folder_to_select)
2361 g_object_unref(priv->folder_to_select);
2362 priv->folder_to_select = TNY_FOLDER(g_object_ref(folder));
2366 model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
2371 gtk_tree_model_get_iter_first (model, &iter);
2372 if (find_folder_iter (model, &iter, &folder_iter, folder)) {
2375 path = gtk_tree_model_get_path (model, &folder_iter);
2376 gtk_tree_view_expand_to_path (GTK_TREE_VIEW(self), path);
2378 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
2379 gtk_tree_selection_select_iter (sel, &folder_iter);
2380 gtk_tree_view_set_cursor (GTK_TREE_VIEW(self), path, NULL, FALSE);
2382 gtk_tree_path_free (path);
2390 modest_folder_view_copy_selection (ModestFolderView *folder_view)
2392 /* Copy selection */
2393 _clipboard_set_selected_data (folder_view, FALSE);
2397 modest_folder_view_cut_selection (ModestFolderView *folder_view)
2399 ModestFolderViewPrivate *priv = NULL;
2400 GtkTreeModel *model = NULL;
2401 const gchar **hidding = NULL;
2402 guint i, n_selected;
2404 g_return_if_fail (MODEST_IS_FOLDER_VIEW (folder_view));
2405 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (folder_view);
2407 /* Copy selection */
2408 if (!_clipboard_set_selected_data (folder_view, TRUE))
2411 /* Get hidding ids */
2412 hidding = modest_email_clipboard_get_hidding_ids (priv->clipboard, &n_selected);
2414 /* Clear hidding array created by previous cut operation */
2415 _clear_hidding_filter (MODEST_FOLDER_VIEW (folder_view));
2417 /* Copy hidding array */
2418 priv->n_selected = n_selected;
2419 priv->hidding_ids = g_malloc0(sizeof(gchar *) * n_selected);
2420 for (i=0; i < n_selected; i++)
2421 priv->hidding_ids[i] = g_strdup(hidding[i]);
2423 /* Hide cut folders */
2424 model = gtk_tree_view_get_model (GTK_TREE_VIEW (folder_view));
2425 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (model));
2429 modest_folder_view_copy_model (ModestFolderView *folder_view_src,
2430 ModestFolderView *folder_view_dst)
2432 GtkTreeModel *filter_model = NULL;
2433 GtkTreeModel *model = NULL;
2434 GtkTreeModel *new_filter_model = NULL;
2436 g_return_if_fail (MODEST_IS_FOLDER_VIEW (folder_view_src));
2437 g_return_if_fail (MODEST_IS_FOLDER_VIEW (folder_view_dst));
2440 filter_model = gtk_tree_view_get_model (GTK_TREE_VIEW (folder_view_src));
2441 model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER(filter_model));
2443 /* Build new filter model */
2444 new_filter_model = gtk_tree_model_filter_new (model, NULL);
2445 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (new_filter_model),
2449 /* Set copied model */
2450 gtk_tree_view_set_model (GTK_TREE_VIEW (folder_view_dst), new_filter_model);
2451 g_signal_connect (G_OBJECT(new_filter_model), "row-inserted",
2452 (GCallback) on_row_inserted_maybe_select_folder, folder_view_dst);
2455 g_object_unref (new_filter_model);
2459 modest_folder_view_show_non_move_folders (ModestFolderView *folder_view,
2462 GtkTreeModel *model = NULL;
2463 ModestFolderViewPrivate* priv = MODEST_FOLDER_VIEW_GET_PRIVATE(folder_view);
2464 priv->show_non_move = show;
2465 /* modest_folder_view_update_model(folder_view, */
2466 /* TNY_ACCOUNT_STORE(modest_runtime_get_account_store())); */
2468 /* Hide special folders */
2469 model = gtk_tree_view_get_model (GTK_TREE_VIEW (folder_view));
2470 if (GTK_IS_TREE_MODEL_FILTER (model)) {
2471 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (model));
2475 /* Returns FALSE if it did not selected anything */
2477 _clipboard_set_selected_data (ModestFolderView *folder_view,
2480 ModestFolderViewPrivate *priv = NULL;
2481 TnyFolderStore *folder = NULL;
2482 gboolean retval = FALSE;
2484 g_return_val_if_fail (MODEST_IS_FOLDER_VIEW (folder_view), FALSE);
2485 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (folder_view);
2487 /* Set selected data on clipboard */
2488 g_return_val_if_fail (MODEST_IS_EMAIL_CLIPBOARD (priv->clipboard), FALSE);
2489 folder = modest_folder_view_get_selected (folder_view);
2491 /* Do not allow to select an account */
2492 if (TNY_IS_FOLDER (folder)) {
2493 modest_email_clipboard_set_data (priv->clipboard, TNY_FOLDER(folder), NULL, delete);
2498 g_object_unref (folder);
2504 _clear_hidding_filter (ModestFolderView *folder_view)
2506 ModestFolderViewPrivate *priv;
2509 g_return_if_fail (MODEST_IS_FOLDER_VIEW (folder_view));
2510 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(folder_view);
2512 if (priv->hidding_ids != NULL) {
2513 for (i=0; i < priv->n_selected; i++)
2514 g_free (priv->hidding_ids[i]);
2515 g_free(priv->hidding_ids);