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"
59 #include "widgets/modest-window.h"
61 /* Folder view drag types */
62 const GtkTargetEntry folder_view_drag_types[] =
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 }
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);
73 static void tny_account_store_view_init (gpointer g,
76 static void modest_folder_view_set_account_store (TnyAccountStoreView *self,
77 TnyAccountStore *account_store);
79 static void on_selection_changed (GtkTreeSelection *sel, gpointer data);
81 static void on_account_removed (TnyAccountStore *self,
85 static void on_account_inserted (TnyAccountStore *self,
89 static void on_account_changed (TnyAccountStore *self,
93 static gint cmp_rows (GtkTreeModel *tree_model,
98 static gboolean filter_row (GtkTreeModel *model,
102 static gboolean on_key_pressed (GtkWidget *self,
106 static void on_configuration_key_changed (ModestConf* conf,
108 ModestConfEvent event,
109 ModestConfNotificationId notification_id,
110 ModestFolderView *self);
113 static void on_drag_data_get (GtkWidget *widget,
114 GdkDragContext *context,
115 GtkSelectionData *selection_data,
120 static void on_drag_data_received (GtkWidget *widget,
121 GdkDragContext *context,
124 GtkSelectionData *selection_data,
129 static gboolean on_drag_motion (GtkWidget *widget,
130 GdkDragContext *context,
136 static void expand_root_items (ModestFolderView *self);
138 static gint expand_row_timeout (gpointer data);
140 static void setup_drag_and_drop (GtkTreeView *self);
142 static gboolean _clipboard_set_selected_data (ModestFolderView *folder_view,
145 static void _clear_hidding_filter (ModestFolderView *folder_view);
147 static void on_row_inserted_maybe_select_folder (GtkTreeModel *tree_model,
150 ModestFolderView *self);
152 static void on_display_name_changed (ModestAccountMgr *self,
153 const gchar *account,
157 FOLDER_SELECTION_CHANGED_SIGNAL,
158 FOLDER_DISPLAY_NAME_CHANGED_SIGNAL,
162 typedef struct _ModestFolderViewPrivate ModestFolderViewPrivate;
163 struct _ModestFolderViewPrivate {
164 TnyAccountStore *account_store;
165 TnyFolderStore *cur_folder_store;
167 TnyFolder *folder_to_select; /* folder to select after the next update */
169 gulong changed_signal;
170 gulong account_inserted_signal;
171 gulong account_removed_signal;
172 gulong account_changed_signal;
173 gulong conf_key_signal;
174 gulong display_name_changed_signal;
176 /* not unref this object, its a singlenton */
177 ModestEmailClipboard *clipboard;
179 /* Filter tree model */
183 TnyFolderStoreQuery *query;
184 guint timer_expander;
186 gchar *local_account_name;
187 gchar *visible_account_id;
188 ModestFolderViewStyle style;
190 gboolean reselect; /* we use this to force a reselection of the INBOX */
191 gboolean show_non_move;
192 gboolean reexpand; /* next time we expose, we'll expand all root folders */
194 #define MODEST_FOLDER_VIEW_GET_PRIVATE(o) \
195 (G_TYPE_INSTANCE_GET_PRIVATE((o), \
196 MODEST_TYPE_FOLDER_VIEW, \
197 ModestFolderViewPrivate))
199 static GObjectClass *parent_class = NULL;
201 static guint signals[LAST_SIGNAL] = {0};
204 modest_folder_view_get_type (void)
206 static GType my_type = 0;
208 static const GTypeInfo my_info = {
209 sizeof(ModestFolderViewClass),
210 NULL, /* base init */
211 NULL, /* base finalize */
212 (GClassInitFunc) modest_folder_view_class_init,
213 NULL, /* class finalize */
214 NULL, /* class data */
215 sizeof(ModestFolderView),
217 (GInstanceInitFunc) modest_folder_view_init,
221 static const GInterfaceInfo tny_account_store_view_info = {
222 (GInterfaceInitFunc) tny_account_store_view_init, /* interface_init */
223 NULL, /* interface_finalize */
224 NULL /* interface_data */
228 my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
232 g_type_add_interface_static (my_type,
233 TNY_TYPE_ACCOUNT_STORE_VIEW,
234 &tny_account_store_view_info);
240 modest_folder_view_class_init (ModestFolderViewClass *klass)
242 GObjectClass *gobject_class;
243 gobject_class = (GObjectClass*) klass;
245 parent_class = g_type_class_peek_parent (klass);
246 gobject_class->finalize = modest_folder_view_finalize;
248 g_type_class_add_private (gobject_class,
249 sizeof(ModestFolderViewPrivate));
251 signals[FOLDER_SELECTION_CHANGED_SIGNAL] =
252 g_signal_new ("folder_selection_changed",
253 G_TYPE_FROM_CLASS (gobject_class),
255 G_STRUCT_OFFSET (ModestFolderViewClass,
256 folder_selection_changed),
258 modest_marshal_VOID__POINTER_BOOLEAN,
259 G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_BOOLEAN);
262 * This signal is emitted whenever the currently selected
263 * folder display name is computed. Note that the name could
264 * be different to the folder name, because we could append
265 * the unread messages count to the folder name to build the
266 * folder display name
268 signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL] =
269 g_signal_new ("folder-display-name-changed",
270 G_TYPE_FROM_CLASS (gobject_class),
272 G_STRUCT_OFFSET (ModestFolderViewClass,
273 folder_display_name_changed),
275 g_cclosure_marshal_VOID__STRING,
276 G_TYPE_NONE, 1, G_TYPE_STRING);
279 /* Simplify checks for NULLs: */
281 strings_are_equal (const gchar *a, const gchar *b)
287 return (strcmp (a, b) == 0);
294 on_model_foreach_set_name(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
296 GObject *instance = NULL;
298 gtk_tree_model_get (model, iter,
299 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
303 return FALSE; /* keep walking */
305 if (!TNY_IS_ACCOUNT (instance)) {
306 g_object_unref (instance);
307 return FALSE; /* keep walking */
310 /* Check if this is the looked-for account: */
311 TnyAccount *this_account = TNY_ACCOUNT (instance);
312 TnyAccount *account = TNY_ACCOUNT (data);
314 const gchar *this_account_id = tny_account_get_id(this_account);
315 const gchar *account_id = tny_account_get_id(account);
316 g_object_unref (instance);
319 /* printf ("DEBUG: %s: this_account_id=%s, account_id=%s\n", __FUNCTION__, this_account_id, account_id); */
320 if (strings_are_equal(this_account_id, account_id)) {
321 /* Tell the model that the data has changed, so that
322 * it calls the cell_data_func callbacks again: */
323 /* TODO: This does not seem to actually cause the new string to be shown: */
324 gtk_tree_model_row_changed (model, path, iter);
326 return TRUE; /* stop walking */
329 return FALSE; /* keep walking */
334 ModestFolderView *self;
335 gchar *previous_name;
336 } GetMmcAccountNameData;
339 on_get_mmc_account_name (TnyStoreAccount* account, gpointer user_data)
341 /* printf ("DEBU1G: %s: account name=%s\n", __FUNCTION__, tny_account_get_name (TNY_ACCOUNT(account))); */
343 GetMmcAccountNameData *data = (GetMmcAccountNameData*)user_data;
345 if (!strings_are_equal (
346 tny_account_get_name(TNY_ACCOUNT(account)),
347 data->previous_name)) {
349 /* Tell the model that the data has changed, so that
350 * it calls the cell_data_func callbacks again: */
351 ModestFolderView *self = data->self;
352 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
354 gtk_tree_model_foreach(model, on_model_foreach_set_name, account);
357 g_free (data->previous_name);
358 g_slice_free (GetMmcAccountNameData, data);
362 text_cell_data (GtkTreeViewColumn *column,
363 GtkCellRenderer *renderer,
364 GtkTreeModel *tree_model,
368 ModestFolderViewPrivate *priv;
371 TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
372 GObject *instance = NULL;
374 g_return_if_fail (column);
375 g_return_if_fail (tree_model);
376 g_return_if_fail (iter != NULL);
378 gtk_tree_model_get (tree_model, iter,
379 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname,
380 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
381 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
383 rendobj = G_OBJECT(renderer);
393 ModestFolderView *self = MODEST_FOLDER_VIEW (data);
394 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
396 gchar *item_name = NULL;
397 gint item_weight = 400;
399 if (type != TNY_FOLDER_TYPE_ROOT) {
402 if (modest_tny_folder_is_local_folder (TNY_FOLDER (instance)) ||
403 modest_tny_folder_is_memory_card_folder (TNY_FOLDER (instance))) {
404 type = modest_tny_folder_get_local_or_mmc_folder_type (TNY_FOLDER (instance));
405 if (type != TNY_FOLDER_TYPE_UNKNOWN) {
407 fname = g_strdup (modest_local_folder_info_get_type_display_name (type));
411 /* note: we cannot reliably get the counts from the tree model, we need
412 * to use explicit calls on tny_folder for some reason.
414 /* Select the number to show: the unread or unsent messages. in case of outbox/drafts, show all */
415 if ((type == TNY_FOLDER_TYPE_DRAFTS) ||
416 (type == TNY_FOLDER_TYPE_OUTBOX) ||
417 (type == TNY_FOLDER_TYPE_MERGE)) /* _OUTBOX actually returns _MERGE... */
418 number = tny_folder_get_all_count (TNY_FOLDER(instance));
420 number = tny_folder_get_unread_count (TNY_FOLDER(instance));
422 /* Use bold font style if there are unread or unset messages */
424 item_name = g_strdup_printf ("%s (%d)", fname, number);
427 item_name = g_strdup (fname);
431 } else if (TNY_IS_ACCOUNT (instance)) {
432 /* If it's a server account */
433 if (modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (instance))) {
434 item_name = g_strdup (priv->local_account_name);
436 } else if (modest_tny_account_is_memory_card_account (TNY_ACCOUNT (instance))) {
437 /* fname is only correct when the items are first
438 * added to the model, not when the account is
439 * changed later, so get the name from the account
441 item_name = g_strdup (tny_account_get_name (TNY_ACCOUNT (instance)));
444 item_name = g_strdup (fname);
450 item_name = g_strdup ("unknown");
452 if (item_name && item_weight) {
453 /* Set the name in the treeview cell: */
454 g_object_set (rendobj,"text", item_name, "weight", item_weight, NULL);
456 /* Notify display name observers */
457 /* TODO: What listens for this signal, and how can it use only the new name? */
458 if (G_OBJECT (priv->cur_folder_store) == instance) {
459 g_signal_emit (G_OBJECT(self),
460 signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
467 /* If it is a Memory card account, make sure that we have the correct name.
468 * This function will be trigerred again when the name has been retrieved: */
469 if (TNY_IS_STORE_ACCOUNT (instance) &&
470 modest_tny_account_is_memory_card_account (TNY_ACCOUNT (instance))) {
472 /* Get the account name asynchronously: */
473 GetMmcAccountNameData *callback_data =
474 g_slice_new0(GetMmcAccountNameData);
475 callback_data->self = self;
477 const gchar *name = tny_account_get_name (TNY_ACCOUNT(instance));
479 callback_data->previous_name = g_strdup (name);
481 modest_tny_account_get_mmc_account_name (TNY_STORE_ACCOUNT (instance),
482 on_get_mmc_account_name, callback_data);
485 g_object_unref (G_OBJECT (instance));
490 icon_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
491 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
493 GObject *rendobj = NULL, *instance = NULL;
494 GdkPixbuf *pixbuf = NULL;
495 TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
496 const gchar *account_id = NULL;
497 gboolean has_children;
499 rendobj = G_OBJECT(renderer);
500 gtk_tree_model_get (tree_model, iter,
501 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
502 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
504 has_children = gtk_tree_model_iter_has_child (tree_model, iter);
509 /* MERGE is not needed anymore as the folder now has the correct type jschmid */
510 /* We include the MERGE type here because it's used to create
511 the local OUTBOX folder */
512 if (type == TNY_FOLDER_TYPE_NORMAL ||
513 type == TNY_FOLDER_TYPE_UNKNOWN) {
514 type = modest_tny_folder_guess_folder_type (TNY_FOLDER (instance));
518 case TNY_FOLDER_TYPE_ROOT:
519 if (TNY_IS_ACCOUNT (instance)) {
521 if (modest_tny_account_is_virtual_local_folders (
522 TNY_ACCOUNT (instance))) {
523 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_LOCAL_FOLDERS);
526 account_id = tny_account_get_id (TNY_ACCOUNT (instance));
528 if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID))
529 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_MMC);
531 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_ACCOUNT);
535 case TNY_FOLDER_TYPE_INBOX:
536 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_INBOX);
538 case TNY_FOLDER_TYPE_OUTBOX:
539 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_OUTBOX);
541 case TNY_FOLDER_TYPE_JUNK:
542 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_JUNK);
544 case TNY_FOLDER_TYPE_SENT:
545 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_SENT);
547 case TNY_FOLDER_TYPE_TRASH:
548 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_TRASH);
550 case TNY_FOLDER_TYPE_DRAFTS:
551 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_DRAFTS);
553 case TNY_FOLDER_TYPE_NORMAL:
555 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_NORMAL);
559 g_object_unref (G_OBJECT (instance));
562 g_object_set (rendobj, "pixbuf", pixbuf, NULL);
563 if (has_children && (pixbuf != NULL)) {
564 GdkPixbuf *open_pixbuf, *closed_pixbuf;
565 GdkPixbuf *open_emblem, *closed_emblem;
566 open_pixbuf = gdk_pixbuf_copy (pixbuf);
567 closed_pixbuf = gdk_pixbuf_copy (pixbuf);
568 open_emblem = modest_platform_get_icon ("qgn_list_gene_fldr_exp");
569 closed_emblem = modest_platform_get_icon ("qgn_list_gene_fldr_clp");
572 gdk_pixbuf_composite (open_emblem, open_pixbuf, 0, 0,
573 MIN (gdk_pixbuf_get_width (open_emblem),
574 gdk_pixbuf_get_width (open_pixbuf)),
575 MIN (gdk_pixbuf_get_height (open_emblem),
576 gdk_pixbuf_get_height (open_pixbuf)),
577 0, 0, 1, 1, GDK_INTERP_NEAREST, 255);
578 g_object_set (rendobj, "pixbuf-expander-open", open_pixbuf, NULL);
579 g_object_unref (open_emblem);
582 gdk_pixbuf_composite (closed_emblem, closed_pixbuf, 0, 0,
583 MIN (gdk_pixbuf_get_width (closed_emblem),
584 gdk_pixbuf_get_width (closed_pixbuf)),
585 MIN (gdk_pixbuf_get_height (closed_emblem),
586 gdk_pixbuf_get_height (closed_pixbuf)),
587 0, 0, 1, 1, GDK_INTERP_NEAREST, 255);
588 g_object_set (rendobj, "pixbuf-expander-closed", closed_pixbuf, NULL);
589 g_object_unref (closed_emblem);
592 g_object_unref (closed_pixbuf);
594 g_object_unref (open_pixbuf);
598 g_object_unref (pixbuf);
602 add_columns (GtkWidget *treeview)
604 GtkTreeViewColumn *column;
605 GtkCellRenderer *renderer;
606 GtkTreeSelection *sel;
609 column = gtk_tree_view_column_new ();
611 /* Set icon and text render function */
612 renderer = gtk_cell_renderer_pixbuf_new();
613 gtk_tree_view_column_pack_start (column, renderer, FALSE);
614 gtk_tree_view_column_set_cell_data_func(column, renderer,
615 icon_cell_data, treeview, NULL);
617 renderer = gtk_cell_renderer_text_new();
618 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END,
619 "ellipsize-set", TRUE, NULL);
620 gtk_tree_view_column_pack_start (column, renderer, TRUE);
621 gtk_tree_view_column_set_cell_data_func(column, renderer,
622 text_cell_data, treeview, NULL);
624 /* Set selection mode */
625 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(treeview));
626 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
628 /* Set treeview appearance */
629 gtk_tree_view_column_set_spacing (column, 2);
630 gtk_tree_view_column_set_resizable (column, TRUE);
631 gtk_tree_view_column_set_fixed_width (column, TRUE);
632 gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(treeview), FALSE);
633 gtk_tree_view_set_enable_search (GTK_TREE_VIEW(treeview), FALSE);
636 gtk_tree_view_append_column (GTK_TREE_VIEW(treeview),column);
640 modest_folder_view_init (ModestFolderView *obj)
642 ModestFolderViewPrivate *priv;
645 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
647 priv->timer_expander = 0;
648 priv->account_store = NULL;
650 priv->style = MODEST_FOLDER_VIEW_STYLE_SHOW_ALL;
651 priv->cur_folder_store = NULL;
652 priv->visible_account_id = NULL;
653 priv->folder_to_select = NULL;
655 priv->reexpand = TRUE;
657 /* Initialize the local account name */
658 conf = modest_runtime_get_conf();
659 priv->local_account_name = modest_conf_get_string (conf, MODEST_CONF_DEVICE_NAME, NULL);
661 /* Init email clipboard */
662 priv->clipboard = modest_runtime_get_email_clipboard ();
663 priv->hidding_ids = NULL;
664 priv->n_selected = 0;
665 priv->reselect = FALSE;
666 priv->show_non_move = TRUE;
669 add_columns (GTK_WIDGET (obj));
671 /* Setup drag and drop */
672 setup_drag_and_drop (GTK_TREE_VIEW(obj));
674 /* Connect signals */
675 g_signal_connect (G_OBJECT (obj),
677 G_CALLBACK (on_key_pressed), NULL);
679 priv->display_name_changed_signal =
680 g_signal_connect (modest_runtime_get_account_mgr (),
681 "display_name_changed",
682 G_CALLBACK (on_display_name_changed),
686 * Track changes in the local account name (in the device it
687 * will be the device name)
689 priv->conf_key_signal = g_signal_connect (G_OBJECT(conf),
691 G_CALLBACK(on_configuration_key_changed),
696 tny_account_store_view_init (gpointer g, gpointer iface_data)
698 TnyAccountStoreViewIface *klass = (TnyAccountStoreViewIface *)g;
700 klass->set_account_store_func = modest_folder_view_set_account_store;
706 modest_folder_view_finalize (GObject *obj)
708 ModestFolderViewPrivate *priv;
709 GtkTreeSelection *sel;
711 g_return_if_fail (obj);
713 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
715 if (priv->timer_expander != 0) {
716 g_source_remove (priv->timer_expander);
717 priv->timer_expander = 0;
720 if (priv->account_store) {
721 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
722 priv->account_inserted_signal);
723 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
724 priv->account_removed_signal);
725 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
726 priv->account_changed_signal);
727 g_object_unref (G_OBJECT(priv->account_store));
728 priv->account_store = NULL;
732 g_object_unref (G_OBJECT (priv->query));
736 /* modest_folder_view_disable_next_folder_selection (MODEST_FOLDER_VIEW(obj)); */
737 if (priv->folder_to_select) {
738 g_object_unref (G_OBJECT(priv->folder_to_select));
739 priv->folder_to_select = NULL;
742 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(obj));
744 g_signal_handler_disconnect (G_OBJECT(sel), priv->changed_signal);
746 g_free (priv->local_account_name);
747 g_free (priv->visible_account_id);
749 if (priv->conf_key_signal) {
750 g_signal_handler_disconnect (modest_runtime_get_conf (),
751 priv->conf_key_signal);
752 priv->conf_key_signal = 0;
755 if (priv->cur_folder_store) {
756 if (TNY_IS_FOLDER(priv->cur_folder_store))
757 tny_folder_sync (TNY_FOLDER(priv->cur_folder_store), FALSE, NULL);
758 /* FALSE --> expunge the message */
760 g_object_unref (priv->cur_folder_store);
761 priv->cur_folder_store = NULL;
764 /* Clear hidding array created by cut operation */
765 _clear_hidding_filter (MODEST_FOLDER_VIEW (obj));
767 G_OBJECT_CLASS(parent_class)->finalize (obj);
772 modest_folder_view_set_account_store (TnyAccountStoreView *self, TnyAccountStore *account_store)
774 ModestFolderViewPrivate *priv;
777 g_return_if_fail (MODEST_IS_FOLDER_VIEW (self));
778 g_return_if_fail (TNY_IS_ACCOUNT_STORE (account_store));
780 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
781 device = tny_account_store_get_device (account_store);
783 if (G_UNLIKELY (priv->account_store)) {
785 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store),
786 priv->account_inserted_signal))
787 g_signal_handler_disconnect (G_OBJECT (priv->account_store),
788 priv->account_inserted_signal);
789 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store),
790 priv->account_removed_signal))
791 g_signal_handler_disconnect (G_OBJECT (priv->account_store),
792 priv->account_removed_signal);
793 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store),
794 priv->account_changed_signal))
795 g_signal_handler_disconnect (G_OBJECT (priv->account_store),
796 priv->account_changed_signal);
797 g_object_unref (G_OBJECT (priv->account_store));
800 priv->account_store = g_object_ref (G_OBJECT (account_store));
802 priv->account_removed_signal =
803 g_signal_connect (G_OBJECT(account_store), "account_removed",
804 G_CALLBACK (on_account_removed), self);
806 priv->account_inserted_signal =
807 g_signal_connect (G_OBJECT(account_store), "account_inserted",
808 G_CALLBACK (on_account_inserted), self);
810 priv->account_changed_signal =
811 g_signal_connect (G_OBJECT(account_store), "account_changed",
812 G_CALLBACK (on_account_changed), self);
814 modest_folder_view_update_model (MODEST_FOLDER_VIEW (self), account_store);
816 g_object_unref (G_OBJECT (device));
820 on_connection_status_changed (TnyAccount *self,
821 TnyConnectionStatus status,
824 /* If the account becomes online then refresh it */
825 if (status == TNY_CONNECTION_STATUS_CONNECTED) {
826 const gchar *acc_name;
827 GtkWidget *my_window;
829 my_window = gtk_widget_get_ancestor (GTK_WIDGET (user_data), MODEST_TYPE_WINDOW);
830 acc_name = modest_tny_account_get_parent_modest_account_name_for_server_account (self);
831 modest_ui_actions_do_send_receive (acc_name, MODEST_WINDOW (my_window));
836 on_account_inserted (TnyAccountStore *account_store,
840 ModestFolderViewPrivate *priv;
841 GtkTreeModel *sort_model, *filter_model;
843 /* Ignore transport account insertions, we're not showing them
844 in the folder view */
845 if (TNY_IS_TRANSPORT_ACCOUNT (account))
848 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (user_data);
850 /* If we're adding a new account, and there is no previous
851 one, we need to select the visible server account */
852 if (priv->style == MODEST_FOLDER_VIEW_STYLE_SHOW_ONE &&
853 !priv->visible_account_id)
854 modest_widget_memory_restore (modest_runtime_get_conf(),
855 G_OBJECT (user_data),
856 MODEST_CONF_FOLDER_VIEW_KEY);
858 /* Get the inner model */
859 filter_model = gtk_tree_view_get_model (GTK_TREE_VIEW (user_data));
860 sort_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (filter_model));
862 /* Insert the account in the model */
863 tny_list_append (TNY_LIST (gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (sort_model))),
867 /* When the store account gets online refresh it */
868 g_signal_connect (account, "connection_status_changed",
869 G_CALLBACK (on_connection_status_changed),
870 MODEST_FOLDER_VIEW (user_data));
872 /* Refilter the model */
873 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (filter_model));
878 on_account_changed (TnyAccountStore *account_store,
879 TnyAccount *tny_account,
883 ModestFolderViewPrivate *priv;
884 GtkTreeModel *sort_model, *filter_model;
886 /* Ignore transport account insertions, we're not showing them
887 in the folder view */
888 if (TNY_IS_TRANSPORT_ACCOUNT (tny_account))
891 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (user_data);
893 /* Get the inner model */
894 filter_model = gtk_tree_view_get_model (GTK_TREE_VIEW (user_data));
895 sort_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (filter_model));
897 /* Remove the account from the model */
898 tny_list_remove (TNY_LIST (gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (sort_model))),
899 G_OBJECT (tny_account));
901 /* Insert the account in the model */
902 tny_list_append (TNY_LIST (gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (sort_model))),
903 G_OBJECT (tny_account));
905 /* Refilter the model */
906 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (filter_model));
912 on_account_removed (TnyAccountStore *account_store,
916 ModestFolderView *self = NULL;
917 ModestFolderViewPrivate *priv;
918 GtkTreeModel *sort_model, *filter_model;
919 GtkTreeSelection *sel = NULL;
921 /* Ignore transport account removals, we're not showing them
922 in the folder view */
923 if (TNY_IS_TRANSPORT_ACCOUNT (account))
926 self = MODEST_FOLDER_VIEW (user_data);
927 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
929 /* Invalidate the cur_folder_store only if the selected folder
930 belongs to the account that is being removed */
931 if (priv->cur_folder_store) {
932 TnyAccount *selected_folder_account = NULL;
934 if (TNY_IS_FOLDER (priv->cur_folder_store)) {
935 selected_folder_account =
936 tny_folder_get_account (TNY_FOLDER (priv->cur_folder_store));
938 selected_folder_account =
939 TNY_ACCOUNT (g_object_ref (priv->cur_folder_store));
942 if (selected_folder_account == account) {
943 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
944 gtk_tree_selection_unselect_all (sel);
946 g_object_unref (selected_folder_account);
949 /* Invalidate row to select only if the folder to select
950 belongs to the account that is being removed*/
951 if (priv->folder_to_select) {
952 TnyAccount *folder_to_select_account = NULL;
954 folder_to_select_account = tny_folder_get_account (priv->folder_to_select);
955 if (folder_to_select_account == account) {
956 /* modest_folder_view_disable_next_folder_selection (self); */
957 g_object_unref (priv->folder_to_select);
958 priv->folder_to_select = NULL;
960 g_object_unref (folder_to_select_account);
963 /* Remove the account from the model */
964 filter_model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
965 sort_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (filter_model));
966 tny_list_remove (TNY_LIST (gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (sort_model))),
969 /* If the removed account is the currently viewed one then
970 clear the configuration value. The new visible account will be the default account */
971 if (priv->visible_account_id &&
972 !strcmp (priv->visible_account_id, tny_account_get_id (account))) {
974 /* Clear the current visible account_id */
975 modest_folder_view_set_account_id_of_visible_server_account (self, NULL);
977 /* Call the restore method, this will set the new visible account */
978 modest_widget_memory_restore (modest_runtime_get_conf(), G_OBJECT(self),
979 MODEST_CONF_FOLDER_VIEW_KEY);
982 /* Refilter the model */
983 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (filter_model));
985 /* Select the first INBOX if the currently selected folder
986 belongs to the account that is being deleted */
987 if (priv->cur_folder_store) {
988 TnyAccount *folder_selected_account;
990 folder_selected_account = (TNY_IS_FOLDER (priv->cur_folder_store)) ?
991 modest_tny_folder_get_account (TNY_FOLDER (priv->cur_folder_store)) :
992 TNY_ACCOUNT (g_object_ref (priv->cur_folder_store));
993 if (account == folder_selected_account)
994 modest_folder_view_select_first_inbox_or_local (self);
995 g_object_unref (folder_selected_account);
1000 modest_folder_view_set_title (ModestFolderView *self, const gchar *title)
1002 GtkTreeViewColumn *col;
1004 g_return_if_fail (self);
1006 col = gtk_tree_view_get_column (GTK_TREE_VIEW(self), 0);
1008 g_printerr ("modest: failed get column for title\n");
1012 gtk_tree_view_column_set_title (col, title);
1013 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self),
1018 modest_folder_view_on_map (ModestFolderView *self,
1019 GdkEventExpose *event,
1022 ModestFolderViewPrivate *priv;
1024 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
1026 /* This won't happen often */
1027 if (G_UNLIKELY (priv->reselect)) {
1028 /* Select the first inbox or the local account if not found */
1030 /* TODO: this could cause a lock at startup, so we
1031 comment it for the moment. We know that this will
1032 be a bug, because the INBOX is not selected, but we
1033 need to rewrite some parts of Modest to avoid the
1034 deathlock situation */
1035 /* TODO: check if this is still the case */
1036 priv->reselect = FALSE;
1037 modest_folder_view_select_first_inbox_or_local (self);
1038 /* Notify the display name observers */
1039 g_signal_emit (G_OBJECT(self),
1040 signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
1044 if (priv->reexpand) {
1045 expand_root_items (self);
1046 priv->reexpand = FALSE;
1053 modest_folder_view_new (TnyFolderStoreQuery *query)
1056 ModestFolderViewPrivate *priv;
1057 GtkTreeSelection *sel;
1059 self = G_OBJECT (g_object_new (MODEST_TYPE_FOLDER_VIEW, NULL));
1060 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
1063 priv->query = g_object_ref (query);
1065 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
1066 priv->changed_signal = g_signal_connect (sel, "changed",
1067 G_CALLBACK (on_selection_changed), self);
1069 g_signal_connect (self, "expose-event", G_CALLBACK (modest_folder_view_on_map), NULL);
1071 return GTK_WIDGET(self);
1074 /* this feels dirty; any other way to expand all the root items? */
1076 expand_root_items (ModestFolderView *self)
1079 GtkTreeModel *model;
1082 model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
1083 path = gtk_tree_path_new_first ();
1085 /* all folders should have child items, so.. */
1087 gtk_tree_view_expand_row (GTK_TREE_VIEW(self), path, FALSE);
1088 gtk_tree_path_next (path);
1089 } while (gtk_tree_model_get_iter (model, &iter, path));
1091 gtk_tree_path_free (path);
1095 * We use this function to implement the
1096 * MODEST_FOLDER_VIEW_STYLE_SHOW_ONE style. We only show the default
1097 * account in this case, and the local folders.
1100 filter_row (GtkTreeModel *model,
1104 ModestFolderViewPrivate *priv;
1105 gboolean retval = TRUE;
1106 TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
1107 GObject *instance = NULL;
1108 const gchar *id = NULL;
1110 gboolean found = FALSE;
1111 gboolean cleared = FALSE;
1113 g_return_val_if_fail (MODEST_IS_FOLDER_VIEW (data), FALSE);
1114 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (data);
1116 gtk_tree_model_get (model, iter,
1117 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
1118 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
1121 /* Do not show if there is no instance, this could indeed
1122 happen when the model is being modified while it's being
1123 drawn. This could occur for example when moving folders
1128 if (type == TNY_FOLDER_TYPE_ROOT) {
1129 /* TNY_FOLDER_TYPE_ROOT means that the instance is an
1130 account instead of a folder. */
1131 if (TNY_IS_ACCOUNT (instance)) {
1132 TnyAccount *acc = TNY_ACCOUNT (instance);
1133 const gchar *account_id = tny_account_get_id (acc);
1135 /* If it isn't a special folder,
1136 * don't show it unless it is the visible account: */
1137 if (priv->style == MODEST_FOLDER_VIEW_STYLE_SHOW_ONE &&
1138 !modest_tny_account_is_virtual_local_folders (acc) &&
1139 strcmp (account_id, MODEST_MMC_ACCOUNT_ID)) {
1141 /* Show only the visible account id */
1142 if (priv->visible_account_id) {
1143 if (strcmp (account_id, priv->visible_account_id))
1150 /* Never show these to the user. They are merged into one folder
1151 * in the local-folders account instead: */
1152 if (retval && MODEST_IS_TNY_OUTBOX_ACCOUNT (acc))
1157 /* Check hiding (if necessary) */
1158 cleared = modest_email_clipboard_cleared (priv->clipboard);
1159 if ((retval) && (!cleared) && (TNY_IS_FOLDER (instance))) {
1160 id = tny_folder_get_id (TNY_FOLDER(instance));
1161 if (priv->hidding_ids != NULL)
1162 for (i=0; i < priv->n_selected && !found; i++)
1163 if (priv->hidding_ids[i] != NULL && id != NULL)
1164 found = (!strcmp (priv->hidding_ids[i], id));
1170 /* If this is a move to dialog, hide Sent, Outbox and Drafts
1171 folder as no message can be move there according to UI specs */
1172 if (!priv->show_non_move) {
1174 case TNY_FOLDER_TYPE_OUTBOX:
1175 case TNY_FOLDER_TYPE_SENT:
1176 case TNY_FOLDER_TYPE_DRAFTS:
1179 case TNY_FOLDER_TYPE_UNKNOWN:
1180 case TNY_FOLDER_TYPE_NORMAL:
1181 type = modest_tny_folder_guess_folder_type(TNY_FOLDER(instance));
1182 if (type == TNY_FOLDER_TYPE_OUTBOX ||
1183 type == TNY_FOLDER_TYPE_SENT
1184 || type == TNY_FOLDER_TYPE_DRAFTS)
1193 g_object_unref (instance);
1200 modest_folder_view_update_model (ModestFolderView *self,
1201 TnyAccountStore *account_store)
1203 ModestFolderViewPrivate *priv;
1204 GtkTreeModel *model /* , *old_model */;
1205 GtkTreeModel *filter_model = NULL, *sortable = NULL;
1207 g_return_val_if_fail (MODEST_IS_FOLDER_VIEW (self), FALSE);
1208 g_return_val_if_fail (account_store, FALSE);
1210 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
1212 /* Notify that there is no folder selected */
1213 g_signal_emit (G_OBJECT(self),
1214 signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
1216 if (priv->cur_folder_store) {
1217 g_object_unref (priv->cur_folder_store);
1218 priv->cur_folder_store = NULL;
1221 /* FIXME: the local accounts are not shown when the query
1222 selects only the subscribed folders */
1223 model = tny_gtk_folder_store_tree_model_new (NULL);
1225 /* Get the accounts: */
1226 tny_account_store_get_accounts (TNY_ACCOUNT_STORE(account_store),
1228 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
1230 sortable = gtk_tree_model_sort_new_with_model (model);
1231 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(sortable),
1232 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
1233 GTK_SORT_ASCENDING);
1234 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (sortable),
1235 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
1236 cmp_rows, NULL, NULL);
1238 /* Create filter model */
1239 filter_model = gtk_tree_model_filter_new (sortable, NULL);
1240 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (filter_model),
1246 gtk_tree_view_set_model (GTK_TREE_VIEW(self), filter_model);
1247 g_signal_connect (G_OBJECT(filter_model), "row-inserted",
1248 (GCallback) on_row_inserted_maybe_select_folder, self);
1251 g_object_unref (model);
1252 g_object_unref (filter_model);
1253 g_object_unref (sortable);
1255 /* Force a reselection of the INBOX next time the widget is shown */
1256 priv->reselect = TRUE;
1263 on_selection_changed (GtkTreeSelection *sel, gpointer user_data)
1265 GtkTreeModel *model = NULL;
1266 TnyFolderStore *folder = NULL;
1268 ModestFolderView *tree_view = NULL;
1269 ModestFolderViewPrivate *priv = NULL;
1270 gboolean selected = FALSE;
1272 g_return_if_fail (sel);
1273 g_return_if_fail (user_data);
1275 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(user_data);
1277 selected = gtk_tree_selection_get_selected (sel, &model, &iter);
1279 /* Notify the display name observers */
1280 g_signal_emit (G_OBJECT(user_data),
1281 signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
1284 tree_view = MODEST_FOLDER_VIEW (user_data);
1287 gtk_tree_model_get (model, &iter,
1288 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder,
1291 /* If the folder is the same do not notify */
1292 if (folder && priv->cur_folder_store == folder) {
1293 g_object_unref (folder);
1298 /* Current folder was unselected */
1299 if (priv->cur_folder_store) {
1300 g_signal_emit (G_OBJECT(tree_view), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
1301 priv->cur_folder_store, FALSE);
1303 if (TNY_IS_FOLDER(priv->cur_folder_store))
1304 tny_folder_sync_async (TNY_FOLDER(priv->cur_folder_store),
1305 FALSE, NULL, NULL, NULL);
1307 /* FALSE --> don't expunge the messages */
1309 g_object_unref (priv->cur_folder_store);
1310 priv->cur_folder_store = NULL;
1313 /* New current references */
1314 priv->cur_folder_store = folder;
1316 /* New folder has been selected */
1317 g_signal_emit (G_OBJECT(tree_view),
1318 signals[FOLDER_SELECTION_CHANGED_SIGNAL],
1319 0, priv->cur_folder_store, TRUE);
1323 modest_folder_view_get_selected (ModestFolderView *self)
1325 ModestFolderViewPrivate *priv;
1327 g_return_val_if_fail (self, NULL);
1329 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
1330 if (priv->cur_folder_store)
1331 g_object_ref (priv->cur_folder_store);
1333 return priv->cur_folder_store;
1337 get_cmp_rows_type_pos (GObject *folder)
1339 /* Remote accounts -> Local account -> MMC account .*/
1342 if (TNY_IS_ACCOUNT (folder) &&
1343 modest_tny_account_is_virtual_local_folders (
1344 TNY_ACCOUNT (folder))) {
1346 } else if (TNY_IS_ACCOUNT (folder)) {
1347 TnyAccount *account = TNY_ACCOUNT (folder);
1348 const gchar *account_id = tny_account_get_id (account);
1349 if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID))
1355 printf ("DEBUG: %s: unexpected type.\n", __FUNCTION__);
1356 return -1; /* Should never happen */
1361 get_cmp_subfolder_type_pos (TnyFolderType t)
1363 /* Inbox, Outbox, Drafts, Sent, User */
1367 case TNY_FOLDER_TYPE_INBOX:
1370 case TNY_FOLDER_TYPE_OUTBOX:
1373 case TNY_FOLDER_TYPE_DRAFTS:
1376 case TNY_FOLDER_TYPE_SENT:
1385 * This function orders the mail accounts according to these rules:
1386 * 1st - remote accounts
1387 * 2nd - local account
1391 cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2,
1395 gchar *name1 = NULL;
1396 gchar *name2 = NULL;
1397 TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
1398 TnyFolderType type2 = TNY_FOLDER_TYPE_UNKNOWN;
1399 GObject *folder1 = NULL;
1400 GObject *folder2 = NULL;
1402 gtk_tree_model_get (tree_model, iter1,
1403 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name1,
1404 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
1405 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder1,
1407 gtk_tree_model_get (tree_model, iter2,
1408 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name2,
1409 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type2,
1410 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder2,
1413 /* Return if we get no folder. This could happen when folder
1414 operations are happening. The model is updated after the
1415 folder copy/move actually occurs, so there could be
1416 situations where the model to be drawn is not correct */
1417 if (!folder1 || !folder2)
1420 if (type == TNY_FOLDER_TYPE_ROOT) {
1421 /* Compare the types, so that
1422 * Remote accounts -> Local account -> MMC account .*/
1423 const gint pos1 = get_cmp_rows_type_pos (folder1);
1424 const gint pos2 = get_cmp_rows_type_pos (folder2);
1425 /* printf ("DEBUG: %s:\n type1=%s, pos1=%d\n type2=%s, pos2=%d\n",
1426 __FUNCTION__, G_OBJECT_TYPE_NAME(folder1), pos1, G_OBJECT_TYPE_NAME(folder2), pos2); */
1429 else if (pos1 > pos2)
1432 /* Compare items of the same type: */
1434 TnyAccount *account1 = NULL;
1435 if (TNY_IS_ACCOUNT (folder1))
1436 account1 = TNY_ACCOUNT (folder1);
1438 TnyAccount *account2 = NULL;
1439 if (TNY_IS_ACCOUNT (folder2))
1440 account2 = TNY_ACCOUNT (folder2);
1442 const gchar *account_id = account1 ? tny_account_get_id (account1) : NULL;
1443 const gchar *account_id2 = account2 ? tny_account_get_id (account2) : NULL;
1445 if (!account_id && !account_id2) {
1447 } else if (!account_id) {
1449 } else if (!account_id2) {
1451 } else if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID)) {
1454 cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
1458 gint cmp1 = 0, cmp2 = 0;
1459 /* get the parent to know if it's a local folder */
1462 gboolean has_parent;
1463 has_parent = gtk_tree_model_iter_parent (tree_model, &parent, iter1);
1465 GObject *parent_folder;
1466 TnyFolderType parent_type = TNY_FOLDER_TYPE_UNKNOWN;
1467 gtk_tree_model_get (tree_model, &parent,
1468 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &parent_type,
1469 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &parent_folder,
1471 if ((parent_type == TNY_FOLDER_TYPE_ROOT) &&
1472 TNY_IS_ACCOUNT (parent_folder) &&
1473 modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (parent_folder))) {
1474 cmp1 = get_cmp_subfolder_type_pos (modest_tny_folder_get_local_or_mmc_folder_type (TNY_FOLDER (folder1)));
1475 cmp2 = get_cmp_subfolder_type_pos (modest_tny_folder_get_local_or_mmc_folder_type (TNY_FOLDER (folder2)));
1477 g_object_unref (parent_folder);
1480 /* if they are not local folders */
1482 cmp1 = get_cmp_subfolder_type_pos (tny_folder_get_folder_type (TNY_FOLDER (folder1)));
1483 cmp2 = get_cmp_subfolder_type_pos (tny_folder_get_folder_type (TNY_FOLDER (folder2)));
1487 cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
1489 cmp = (cmp1 - cmp2);
1494 g_object_unref(G_OBJECT(folder1));
1496 g_object_unref(G_OBJECT(folder2));
1504 /*****************************************************************************/
1505 /* DRAG and DROP stuff */
1506 /*****************************************************************************/
1508 * This function fills the #GtkSelectionData with the row and the
1509 * model that has been dragged. It's called when this widget is a
1510 * source for dnd after the event drop happened
1513 on_drag_data_get (GtkWidget *widget,
1514 GdkDragContext *context,
1515 GtkSelectionData *selection_data,
1520 GtkTreeSelection *selection;
1521 GtkTreeModel *model;
1523 GtkTreePath *source_row;
1525 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
1526 gtk_tree_selection_get_selected (selection, &model, &iter);
1527 source_row = gtk_tree_model_get_path (model, &iter);
1529 gtk_tree_set_row_drag_data (selection_data,
1533 gtk_tree_path_free (source_row);
1536 typedef struct _DndHelper {
1537 gboolean delete_source;
1538 GtkTreePath *source_row;
1539 GdkDragContext *context;
1545 * This function is the callback of the
1546 * modest_mail_operation_xfer_msgs () and
1547 * modest_mail_operation_xfer_folder() calls. We check here if the
1548 * message/folder was correctly asynchronously transferred. The reason
1549 * to use the same callback is that the code is the same, it only has
1550 * to check that the operation went fine and then finalize the drag
1554 xfer_cb (ModestMailOperation *mail_op,
1560 helper = (DndHelper *) user_data;
1562 if (modest_mail_operation_get_status (mail_op) ==
1563 MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
1569 /* Notify the drag source. Never call delete, the monitor will
1570 do the job if needed */
1571 gtk_drag_finish (helper->context, success, FALSE, helper->time);
1573 /* Free the helper */
1574 gtk_tree_path_free (helper->source_row);
1575 g_slice_free (DndHelper, helper);
1578 /* get the folder for the row the treepath refers to. */
1579 /* folder must be unref'd */
1580 static TnyFolderStore *
1581 tree_path_to_folder (GtkTreeModel *model, GtkTreePath *path)
1584 TnyFolderStore *folder = NULL;
1586 if (gtk_tree_model_get_iter (model,&iter, path))
1587 gtk_tree_model_get (model, &iter,
1588 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder,
1594 * This function is used by drag_data_received_cb to manage drag and
1595 * drop of a header, i.e, and drag from the header view to the folder
1599 drag_and_drop_from_header_view (GtkTreeModel *source_model,
1600 GtkTreeModel *dest_model,
1601 GtkTreePath *dest_row,
1602 GtkSelectionData *selection_data,
1605 TnyList *headers = NULL;
1606 TnyFolder *folder = NULL;
1607 ModestMailOperation *mail_op = NULL;
1608 GtkTreeIter source_iter, dest_iter;
1609 ModestWindowMgr *mgr = NULL;
1610 ModestWindow *main_win = NULL;
1611 gchar **uris, **tmp;
1614 /* Build the list of headers */
1615 mgr = modest_runtime_get_window_mgr ();
1616 headers = tny_simple_list_new ();
1617 uris = modest_dnd_selection_data_get_paths (selection_data);
1620 while (*tmp != NULL) {
1625 path = gtk_tree_path_new_from_string (*tmp);
1626 gtk_tree_model_get_iter (source_model, &source_iter, path);
1627 gtk_tree_model_get (source_model, &source_iter,
1628 TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN,
1631 /* Do not enable d&d of headers already opened */
1632 if (!modest_window_mgr_find_registered_header(mgr, header, NULL))
1633 tny_list_append (headers, G_OBJECT (header));
1635 /* Free and go on */
1636 gtk_tree_path_free (path);
1637 g_object_unref (header);
1642 /* Get the target folder */
1643 gtk_tree_model_get_iter (dest_model, &dest_iter, dest_row);
1644 gtk_tree_model_get (dest_model, &dest_iter,
1645 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
1648 /* Ask for confirmation to move */
1649 main_win = modest_window_mgr_get_main_window (mgr);
1650 response = modest_ui_actions_msgs_move_to_confirmation (main_win, folder,
1652 if (response == GTK_RESPONSE_CANCEL)
1655 /* Transfer messages */
1656 mail_op = modest_mail_operation_new_with_error_handling ((GObject *) main_win,
1657 modest_ui_actions_move_folder_error_handler,
1660 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
1663 modest_mail_operation_xfer_msgs (mail_op,
1666 helper->delete_source,
1671 if (G_IS_OBJECT(mail_op))
1672 g_object_unref (G_OBJECT (mail_op));
1673 if (G_IS_OBJECT(folder))
1674 g_object_unref (G_OBJECT (folder));
1675 if (G_IS_OBJECT(headers))
1676 g_object_unref (headers);
1680 * This function is used by drag_data_received_cb to manage drag and
1681 * drop of a folder, i.e, and drag from the folder view to the same
1685 drag_and_drop_from_folder_view (GtkTreeModel *source_model,
1686 GtkTreeModel *dest_model,
1687 GtkTreePath *dest_row,
1688 GtkSelectionData *selection_data,
1691 ModestMailOperation *mail_op = NULL;
1692 GtkTreeIter dest_iter, iter;
1693 TnyFolderStore *dest_folder = NULL;
1694 TnyFolderStore *folder = NULL;
1695 gboolean forbidden = FALSE;
1698 /* check the folder rules for the destination */
1699 folder = tree_path_to_folder (dest_model, dest_row);
1700 if (TNY_IS_FOLDER(folder)) {
1701 ModestTnyFolderRules rules =
1702 modest_tny_folder_get_rules (TNY_FOLDER (folder));
1703 forbidden = rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE;
1704 } else if (TNY_IS_FOLDER_STORE(folder)) {
1705 /* enable local root as destination for folders */
1706 if (!MODEST_IS_TNY_LOCAL_FOLDERS_ACCOUNT (folder)
1707 && TNY_IS_ACCOUNT (folder))
1710 g_object_unref (folder);
1713 /* check the folder rules for the source */
1714 folder = tree_path_to_folder (source_model, helper->source_row);
1715 if (TNY_IS_FOLDER(folder)) {
1716 ModestTnyFolderRules rules =
1717 modest_tny_folder_get_rules (TNY_FOLDER (folder));
1718 forbidden = rules & MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE;
1721 g_object_unref (folder);
1725 /* Check if the drag is possible */
1726 if (forbidden || !gtk_tree_path_compare (helper->source_row, dest_row)) {
1727 gtk_drag_finish (helper->context, FALSE, FALSE, helper->time);
1728 gtk_tree_path_free (helper->source_row);
1729 g_slice_free (DndHelper, helper);
1734 gtk_tree_model_get_iter (dest_model, &dest_iter, dest_row);
1735 gtk_tree_model_get (dest_model, &dest_iter,
1736 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
1738 gtk_tree_model_get_iter (source_model, &iter, helper->source_row);
1739 gtk_tree_model_get (source_model, &iter,
1740 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
1743 /* Offer the connection dialog if necessary, for the destination parent folder and source folder: */
1744 if (modest_platform_connect_and_wait_if_network_folderstore (NULL, dest_folder) &&
1745 modest_platform_connect_and_wait_if_network_folderstore (NULL, TNY_FOLDER_STORE (folder))) {
1746 ModestWindowMgr *mgr = modest_runtime_get_window_mgr ();
1748 /* Do the mail operation */
1750 modest_mail_operation_new_with_error_handling ((GObject *) modest_window_mgr_get_main_window (mgr),
1751 modest_ui_actions_move_folder_error_handler,
1754 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
1757 modest_mail_operation_xfer_folder (mail_op,
1758 TNY_FOLDER (folder),
1760 helper->delete_source,
1764 g_object_unref (G_OBJECT (mail_op));
1768 g_object_unref (G_OBJECT (dest_folder));
1769 g_object_unref (G_OBJECT (folder));
1773 * This function receives the data set by the "drag-data-get" signal
1774 * handler. This information comes within the #GtkSelectionData. This
1775 * function will manage both the drags of folders of the treeview and
1776 * drags of headers of the header view widget.
1779 on_drag_data_received (GtkWidget *widget,
1780 GdkDragContext *context,
1783 GtkSelectionData *selection_data,
1788 GtkWidget *source_widget;
1789 GtkTreeModel *dest_model, *source_model;
1790 GtkTreePath *source_row, *dest_row;
1791 GtkTreeViewDropPosition pos;
1792 gboolean success = FALSE, delete_source = FALSE;
1793 DndHelper *helper = NULL;
1795 /* Do not allow further process */
1796 g_signal_stop_emission_by_name (widget, "drag-data-received");
1797 source_widget = gtk_drag_get_source_widget (context);
1799 /* Get the action */
1800 if (context->action == GDK_ACTION_MOVE) {
1801 delete_source = TRUE;
1803 /* Notify that there is no folder selected. We need to
1804 do this in order to update the headers view (and
1805 its monitors, because when moving, the old folder
1806 won't longer exist. We can not wait for the end of
1807 the operation, because the operation won't start if
1808 the folder is in use */
1809 if (source_widget == widget) {
1810 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
1811 gtk_tree_selection_unselect_all (sel);
1815 /* Check if the get_data failed */
1816 if (selection_data == NULL || selection_data->length < 0)
1817 gtk_drag_finish (context, success, FALSE, time);
1819 /* Select the destination model */
1820 dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
1822 /* Get the path to the destination row. Can not call
1823 gtk_tree_view_get_drag_dest_row() because the source row
1824 is not selected anymore */
1825 gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget), x, y,
1828 /* Only allow drops IN other rows */
1830 pos == GTK_TREE_VIEW_DROP_BEFORE ||
1831 pos == GTK_TREE_VIEW_DROP_AFTER)
1832 gtk_drag_finish (context, success, FALSE, time);
1834 /* Create the helper */
1835 helper = g_slice_new0 (DndHelper);
1836 helper->delete_source = delete_source;
1837 helper->context = context;
1838 helper->time = time;
1840 /* Drags from the header view */
1841 if (source_widget != widget) {
1842 source_model = gtk_tree_view_get_model (GTK_TREE_VIEW (source_widget));
1844 drag_and_drop_from_header_view (source_model,
1850 /* Get the source model and row */
1851 gtk_tree_get_row_drag_data (selection_data,
1854 helper->source_row = gtk_tree_path_copy (source_row);
1856 drag_and_drop_from_folder_view (source_model,
1862 gtk_tree_path_free (source_row);
1866 gtk_tree_path_free (dest_row);
1870 * We define a "drag-drop" signal handler because we do not want to
1871 * use the default one, because the default one always calls
1872 * gtk_drag_finish and we prefer to do it in the "drag-data-received"
1873 * signal handler, because there we have all the information available
1874 * to know if the dnd was a success or not.
1877 drag_drop_cb (GtkWidget *widget,
1878 GdkDragContext *context,
1886 if (!context->targets)
1889 /* Check if we're dragging a folder row */
1890 target = gtk_drag_dest_find_target (widget, context, NULL);
1892 /* Request the data from the source. */
1893 gtk_drag_get_data(widget, context, target, time);
1899 * This function expands a node of a tree view if it's not expanded
1900 * yet. Not sure why it needs the threads stuff, but gtk+`example code
1901 * does that, so that's why they're here.
1904 expand_row_timeout (gpointer data)
1906 GtkTreeView *tree_view = data;
1907 GtkTreePath *dest_path = NULL;
1908 GtkTreeViewDropPosition pos;
1909 gboolean result = FALSE;
1911 GDK_THREADS_ENTER ();
1913 gtk_tree_view_get_drag_dest_row (tree_view,
1918 (pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER ||
1919 pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)) {
1920 gtk_tree_view_expand_row (tree_view, dest_path, FALSE);
1921 gtk_tree_path_free (dest_path);
1925 gtk_tree_path_free (dest_path);
1930 GDK_THREADS_LEAVE ();
1936 * This function is called whenever the pointer is moved over a widget
1937 * while dragging some data. It installs a timeout that will expand a
1938 * node of the treeview if not expanded yet. This function also calls
1939 * gdk_drag_status in order to set the suggested action that will be
1940 * used by the "drag-data-received" signal handler to know if we
1941 * should do a move or just a copy of the data.
1944 on_drag_motion (GtkWidget *widget,
1945 GdkDragContext *context,
1951 GtkTreeViewDropPosition pos;
1952 GtkTreePath *dest_row;
1953 GtkTreeModel *dest_model;
1954 ModestFolderViewPrivate *priv;
1955 GdkDragAction suggested_action;
1956 gboolean valid_location = FALSE;
1957 TnyFolderStore *folder;
1959 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (widget);
1961 if (priv->timer_expander != 0) {
1962 g_source_remove (priv->timer_expander);
1963 priv->timer_expander = 0;
1966 gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
1971 /* Do not allow drops between folders */
1973 pos == GTK_TREE_VIEW_DROP_BEFORE ||
1974 pos == GTK_TREE_VIEW_DROP_AFTER) {
1975 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW (widget), NULL, 0);
1976 gdk_drag_status(context, 0, time);
1977 valid_location = FALSE;
1980 valid_location = TRUE;
1983 /* Check that the destination folder is writable */
1984 dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
1985 folder = tree_path_to_folder (dest_model, dest_row);
1986 if (folder && TNY_IS_FOLDER (folder)) {
1987 ModestTnyFolderRules rules = modest_tny_folder_get_rules(TNY_FOLDER (folder));
1989 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE) {
1990 valid_location = FALSE;
1994 g_object_unref (folder);
1996 /* Expand the selected row after 1/2 second */
1997 if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), dest_row)) {
1998 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), dest_row, pos);
1999 priv->timer_expander = g_timeout_add (500, expand_row_timeout, widget);
2002 /* Select the desired action. By default we pick MOVE */
2003 suggested_action = GDK_ACTION_MOVE;
2005 if (context->actions == GDK_ACTION_COPY)
2006 gdk_drag_status(context, GDK_ACTION_COPY, time);
2007 else if (context->actions == GDK_ACTION_MOVE)
2008 gdk_drag_status(context, GDK_ACTION_MOVE, time);
2009 else if (context->actions & suggested_action)
2010 gdk_drag_status(context, suggested_action, time);
2012 gdk_drag_status(context, GDK_ACTION_DEFAULT, time);
2016 gtk_tree_path_free (dest_row);
2017 g_signal_stop_emission_by_name (widget, "drag-motion");
2019 return valid_location;
2023 * This function sets the treeview as a source and a target for dnd
2024 * events. It also connects all the requirede signals.
2027 setup_drag_and_drop (GtkTreeView *self)
2029 /* Set up the folder view as a dnd destination. Set only the
2030 highlight flag, otherwise gtk will have a different
2032 gtk_drag_dest_set (GTK_WIDGET (self),
2033 GTK_DEST_DEFAULT_HIGHLIGHT,
2034 folder_view_drag_types,
2035 G_N_ELEMENTS (folder_view_drag_types),
2036 GDK_ACTION_MOVE | GDK_ACTION_COPY);
2038 g_signal_connect (G_OBJECT (self),
2039 "drag_data_received",
2040 G_CALLBACK (on_drag_data_received),
2044 /* Set up the treeview as a dnd source */
2045 gtk_drag_source_set (GTK_WIDGET (self),
2047 folder_view_drag_types,
2048 G_N_ELEMENTS (folder_view_drag_types),
2049 GDK_ACTION_MOVE | GDK_ACTION_COPY);
2051 g_signal_connect (G_OBJECT (self),
2053 G_CALLBACK (on_drag_motion),
2056 g_signal_connect (G_OBJECT (self),
2058 G_CALLBACK (on_drag_data_get),
2061 g_signal_connect (G_OBJECT (self),
2063 G_CALLBACK (drag_drop_cb),
2068 * This function manages the navigation through the folders using the
2069 * keyboard or the hardware keys in the device
2072 on_key_pressed (GtkWidget *self,
2076 GtkTreeSelection *selection;
2078 GtkTreeModel *model;
2079 gboolean retval = FALSE;
2081 /* Up and Down are automatically managed by the treeview */
2082 if (event->keyval == GDK_Return) {
2083 /* Expand/Collapse the selected row */
2084 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
2085 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
2088 path = gtk_tree_model_get_path (model, &iter);
2090 if (gtk_tree_view_row_expanded (GTK_TREE_VIEW (self), path))
2091 gtk_tree_view_collapse_row (GTK_TREE_VIEW (self), path);
2093 gtk_tree_view_expand_row (GTK_TREE_VIEW (self), path, FALSE);
2094 gtk_tree_path_free (path);
2096 /* No further processing */
2104 * We listen to the changes in the local folder account name key,
2105 * because we want to show the right name in the view. The local
2106 * folder account name corresponds to the device name in the Maemo
2107 * version. We do this because we do not want to query gconf on each
2108 * tree view refresh. It's better to cache it and change whenever
2112 on_configuration_key_changed (ModestConf* conf,
2114 ModestConfEvent event,
2115 ModestConfNotificationId id,
2116 ModestFolderView *self)
2118 ModestFolderViewPrivate *priv;
2121 g_return_if_fail (MODEST_IS_FOLDER_VIEW (self));
2122 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
2124 if (!strcmp (key, MODEST_CONF_DEVICE_NAME)) {
2125 g_free (priv->local_account_name);
2127 if (event == MODEST_CONF_EVENT_KEY_UNSET)
2128 priv->local_account_name = g_strdup (MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME);
2130 priv->local_account_name = modest_conf_get_string (modest_runtime_get_conf(),
2131 MODEST_CONF_DEVICE_NAME, NULL);
2133 /* Force a redraw */
2134 #if GTK_CHECK_VERSION(2, 8, 0)
2135 GtkTreeViewColumn * tree_column;
2137 tree_column = gtk_tree_view_get_column (GTK_TREE_VIEW (self),
2138 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN);
2139 gtk_tree_view_column_queue_resize (tree_column);
2141 gtk_widget_queue_draw (GTK_WIDGET (self));
2147 modest_folder_view_set_style (ModestFolderView *self,
2148 ModestFolderViewStyle style)
2150 ModestFolderViewPrivate *priv;
2152 g_return_if_fail (self);
2154 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
2156 priv->style = style;
2160 modest_folder_view_set_account_id_of_visible_server_account (ModestFolderView *self,
2161 const gchar *account_id)
2163 ModestFolderViewPrivate *priv;
2164 GtkTreeModel *model;
2166 g_return_if_fail (self);
2168 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
2170 /* This will be used by the filter_row callback,
2171 * to decided which rows to show: */
2172 if (priv->visible_account_id) {
2173 g_free (priv->visible_account_id);
2174 priv->visible_account_id = NULL;
2177 priv->visible_account_id = g_strdup (account_id);
2180 model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
2181 if (GTK_IS_TREE_MODEL_FILTER (model))
2182 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (model));
2184 /* Save settings to gconf */
2185 modest_widget_memory_save (modest_runtime_get_conf (), G_OBJECT(self),
2186 MODEST_CONF_FOLDER_VIEW_KEY);
2190 modest_folder_view_get_account_id_of_visible_server_account (ModestFolderView *self)
2192 ModestFolderViewPrivate *priv;
2194 g_return_val_if_fail (self, NULL);
2196 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
2198 return (const gchar *) priv->visible_account_id;
2202 find_inbox_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreeIter *inbox_iter)
2206 TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
2208 gtk_tree_model_get (model, iter,
2209 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN,
2212 gboolean result = FALSE;
2213 if (type == TNY_FOLDER_TYPE_INBOX) {
2217 *inbox_iter = *iter;
2221 if (gtk_tree_model_iter_children (model, &child, iter)) {
2222 if (find_inbox_iter (model, &child, inbox_iter))
2226 } while (gtk_tree_model_iter_next (model, iter));
2235 modest_folder_view_select_first_inbox_or_local (ModestFolderView *self)
2237 GtkTreeModel *model;
2238 GtkTreeIter iter, inbox_iter;
2239 GtkTreeSelection *sel;
2240 GtkTreePath *path = NULL;
2242 model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
2246 expand_root_items (self);
2247 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
2249 gtk_tree_model_get_iter_first (model, &iter);
2251 if (find_inbox_iter (model, &iter, &inbox_iter))
2252 path = gtk_tree_model_get_path (model, &inbox_iter);
2254 path = gtk_tree_path_new_first ();
2256 /* Select the row and free */
2257 gtk_tree_view_set_cursor (GTK_TREE_VIEW (self), path, NULL, FALSE);
2258 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (self), path, NULL, FALSE, 0.0, 0.0);
2259 gtk_tree_path_free (path);
2262 gtk_widget_grab_focus (GTK_WIDGET(self));
2268 find_folder_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreeIter *folder_iter,
2273 TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
2274 TnyFolder* a_folder;
2277 gtk_tree_model_get (model, iter,
2278 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &a_folder,
2279 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name,
2280 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
2284 if (folder == a_folder) {
2285 g_object_unref (a_folder);
2286 *folder_iter = *iter;
2289 g_object_unref (a_folder);
2291 if (gtk_tree_model_iter_children (model, &child, iter)) {
2292 if (find_folder_iter (model, &child, folder_iter, folder))
2296 } while (gtk_tree_model_iter_next (model, iter));
2303 on_row_inserted_maybe_select_folder (GtkTreeModel *tree_model, GtkTreePath *path, GtkTreeIter *iter,
2304 ModestFolderView *self)
2306 ModestFolderViewPrivate *priv = NULL;
2307 GtkTreeSelection *sel;
2308 TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
2309 GObject *instance = NULL;
2311 if (!MODEST_IS_FOLDER_VIEW(self))
2314 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
2316 priv->reexpand = TRUE;
2318 gtk_tree_model_get (tree_model, iter,
2319 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
2320 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
2322 if (type == TNY_FOLDER_TYPE_INBOX && priv->folder_to_select == NULL) {
2323 priv->folder_to_select = g_object_ref (instance);
2325 g_object_unref (instance);
2328 if (priv->folder_to_select) {
2330 if (!modest_folder_view_select_folder (self, priv->folder_to_select,
2333 path = gtk_tree_model_get_path (tree_model, iter);
2334 gtk_tree_view_expand_to_path (GTK_TREE_VIEW(self), path);
2336 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
2338 gtk_tree_selection_select_iter (sel, iter);
2339 gtk_tree_view_set_cursor (GTK_TREE_VIEW(self), path, NULL, FALSE);
2341 gtk_tree_path_free (path);
2346 modest_folder_view_disable_next_folder_selection (self);
2347 /* g_object_unref (priv->folder_to_select); */
2348 /* priv->folder_to_select = NULL; */
2354 modest_folder_view_disable_next_folder_selection (ModestFolderView *self)
2356 ModestFolderViewPrivate *priv = NULL;
2358 g_return_if_fail (MODEST_IS_FOLDER_VIEW (self));
2359 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
2361 if (priv->folder_to_select)
2362 g_object_unref(priv->folder_to_select);
2364 priv->folder_to_select = NULL;
2368 modest_folder_view_select_folder (ModestFolderView *self, TnyFolder *folder,
2369 gboolean after_change)
2371 GtkTreeModel *model;
2372 GtkTreeIter iter, folder_iter;
2373 GtkTreeSelection *sel;
2374 ModestFolderViewPrivate *priv = NULL;
2376 g_return_val_if_fail (MODEST_IS_FOLDER_VIEW (self), FALSE);
2377 g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
2379 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
2383 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
2384 gtk_tree_selection_unselect_all (sel);
2386 if (priv->folder_to_select)
2387 g_object_unref(priv->folder_to_select);
2388 priv->folder_to_select = TNY_FOLDER(g_object_ref(folder));
2392 model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
2397 gtk_tree_model_get_iter_first (model, &iter);
2398 if (find_folder_iter (model, &iter, &folder_iter, folder)) {
2401 path = gtk_tree_model_get_path (model, &folder_iter);
2402 gtk_tree_view_expand_to_path (GTK_TREE_VIEW(self), path);
2404 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
2405 gtk_tree_selection_select_iter (sel, &folder_iter);
2406 gtk_tree_view_set_cursor (GTK_TREE_VIEW(self), path, NULL, FALSE);
2408 gtk_tree_path_free (path);
2416 modest_folder_view_copy_selection (ModestFolderView *folder_view)
2418 /* Copy selection */
2419 _clipboard_set_selected_data (folder_view, FALSE);
2423 modest_folder_view_cut_selection (ModestFolderView *folder_view)
2425 ModestFolderViewPrivate *priv = NULL;
2426 GtkTreeModel *model = NULL;
2427 const gchar **hidding = NULL;
2428 guint i, n_selected;
2430 g_return_if_fail (MODEST_IS_FOLDER_VIEW (folder_view));
2431 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (folder_view);
2433 /* Copy selection */
2434 if (!_clipboard_set_selected_data (folder_view, TRUE))
2437 /* Get hidding ids */
2438 hidding = modest_email_clipboard_get_hidding_ids (priv->clipboard, &n_selected);
2440 /* Clear hidding array created by previous cut operation */
2441 _clear_hidding_filter (MODEST_FOLDER_VIEW (folder_view));
2443 /* Copy hidding array */
2444 priv->n_selected = n_selected;
2445 priv->hidding_ids = g_malloc0(sizeof(gchar *) * n_selected);
2446 for (i=0; i < n_selected; i++)
2447 priv->hidding_ids[i] = g_strdup(hidding[i]);
2449 /* Hide cut folders */
2450 model = gtk_tree_view_get_model (GTK_TREE_VIEW (folder_view));
2451 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (model));
2455 modest_folder_view_copy_model (ModestFolderView *folder_view_src,
2456 ModestFolderView *folder_view_dst)
2458 GtkTreeModel *filter_model = NULL;
2459 GtkTreeModel *model = NULL;
2460 GtkTreeModel *new_filter_model = NULL;
2462 g_return_if_fail (MODEST_IS_FOLDER_VIEW (folder_view_src));
2463 g_return_if_fail (MODEST_IS_FOLDER_VIEW (folder_view_dst));
2466 filter_model = gtk_tree_view_get_model (GTK_TREE_VIEW (folder_view_src));
2467 model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER(filter_model));
2469 /* Build new filter model */
2470 new_filter_model = gtk_tree_model_filter_new (model, NULL);
2471 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (new_filter_model),
2475 /* Set copied model */
2476 gtk_tree_view_set_model (GTK_TREE_VIEW (folder_view_dst), new_filter_model);
2477 g_signal_connect (G_OBJECT(new_filter_model), "row-inserted",
2478 (GCallback) on_row_inserted_maybe_select_folder, folder_view_dst);
2481 g_object_unref (new_filter_model);
2485 modest_folder_view_show_non_move_folders (ModestFolderView *folder_view,
2488 GtkTreeModel *model = NULL;
2489 ModestFolderViewPrivate* priv = MODEST_FOLDER_VIEW_GET_PRIVATE(folder_view);
2490 priv->show_non_move = show;
2491 /* modest_folder_view_update_model(folder_view, */
2492 /* TNY_ACCOUNT_STORE(modest_runtime_get_account_store())); */
2494 /* Hide special folders */
2495 model = gtk_tree_view_get_model (GTK_TREE_VIEW (folder_view));
2496 if (GTK_IS_TREE_MODEL_FILTER (model)) {
2497 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (model));
2501 /* Returns FALSE if it did not selected anything */
2503 _clipboard_set_selected_data (ModestFolderView *folder_view,
2506 ModestFolderViewPrivate *priv = NULL;
2507 TnyFolderStore *folder = NULL;
2508 gboolean retval = FALSE;
2510 g_return_val_if_fail (MODEST_IS_FOLDER_VIEW (folder_view), FALSE);
2511 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (folder_view);
2513 /* Set selected data on clipboard */
2514 g_return_val_if_fail (MODEST_IS_EMAIL_CLIPBOARD (priv->clipboard), FALSE);
2515 folder = modest_folder_view_get_selected (folder_view);
2517 /* Do not allow to select an account */
2518 if (TNY_IS_FOLDER (folder)) {
2519 modest_email_clipboard_set_data (priv->clipboard, TNY_FOLDER(folder), NULL, delete);
2524 g_object_unref (folder);
2530 _clear_hidding_filter (ModestFolderView *folder_view)
2532 ModestFolderViewPrivate *priv;
2535 g_return_if_fail (MODEST_IS_FOLDER_VIEW (folder_view));
2536 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(folder_view);
2538 if (priv->hidding_ids != NULL) {
2539 for (i=0; i < priv->n_selected; i++)
2540 g_free (priv->hidding_ids[i]);
2541 g_free(priv->hidding_ids);
2547 on_display_name_changed (ModestAccountMgr *mgr,
2548 const gchar *account,
2551 ModestFolderView *self;
2553 self = MODEST_FOLDER_VIEW (user_data);
2555 /* Force a redraw */
2556 #if GTK_CHECK_VERSION(2, 8, 0)
2557 GtkTreeViewColumn * tree_column;
2559 tree_column = gtk_tree_view_get_column (GTK_TREE_VIEW (self),
2560 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN);
2561 gtk_tree_view_column_queue_resize (tree_column);
2563 gtk_widget_queue_draw (GTK_WIDGET (self));