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>
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-monitor.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 <modest-tny-folder.h>
45 #include <modest-marshal.h>
46 #include <modest-icon-names.h>
47 #include <modest-tny-account-store.h>
48 #include <modest-text-utils.h>
49 #include <modest-runtime.h>
50 #include "modest-folder-view.h"
52 /* 'private'/'protected' functions */
53 static void modest_folder_view_class_init (ModestFolderViewClass *klass);
54 static void modest_folder_view_init (ModestFolderView *obj);
55 static void modest_folder_view_finalize (GObject *obj);
57 static void tny_account_store_view_init (gpointer g,
60 static void modest_folder_view_set_account_store (TnyAccountStoreView *self,
61 TnyAccountStore *account_store);
63 static gboolean update_model (ModestFolderView *self,
64 ModestTnyAccountStore *account_store);
66 static gboolean update_model_empty (ModestFolderView *self);
68 static void on_selection_changed (GtkTreeSelection *sel, gpointer data);
70 static void on_account_update (TnyAccountStore *account_store,
74 static void on_accounts_reloaded (TnyAccountStore *store,
77 static gint cmp_rows (GtkTreeModel *tree_model,
83 static void on_drag_data_get (GtkWidget *widget,
84 GdkDragContext *context,
85 GtkSelectionData *selection_data,
90 static void on_drag_data_received (GtkWidget *widget,
91 GdkDragContext *context,
94 GtkSelectionData *selection_data,
99 static gboolean on_drag_motion (GtkWidget *widget,
100 GdkDragContext *context,
106 static gint expand_row_timeout (gpointer data);
108 static void setup_drag_and_drop (GtkTreeView *self);
114 static const GtkTargetEntry drag_types[] =
116 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, TARGET_TREE_ROW },
119 #define ROW_REF_DATA_NAME "row-ref"
122 FOLDER_SELECTION_CHANGED_SIGNAL,
127 typedef struct _ModestFolderViewPrivate ModestFolderViewPrivate;
128 struct _ModestFolderViewPrivate {
129 TnyAccountStore *account_store;
130 TnyFolder *cur_folder;
131 GtkTreeRowReference *cur_row;
133 gulong account_update_signal;
134 gulong changed_signal;
135 gulong accounts_reloaded_signal;
138 GtkTreeSelection *cur_selection;
139 TnyFolderStoreQuery *query;
140 guint timer_expander;
142 TnyFolderMonitor *monitor;
144 #define MODEST_FOLDER_VIEW_GET_PRIVATE(o) \
145 (G_TYPE_INSTANCE_GET_PRIVATE((o), \
146 MODEST_TYPE_FOLDER_VIEW, \
147 ModestFolderViewPrivate))
149 static GObjectClass *parent_class = NULL;
151 static guint signals[LAST_SIGNAL] = {0};
154 modest_folder_view_get_type (void)
156 static GType my_type = 0;
158 static const GTypeInfo my_info = {
159 sizeof(ModestFolderViewClass),
160 NULL, /* base init */
161 NULL, /* base finalize */
162 (GClassInitFunc) modest_folder_view_class_init,
163 NULL, /* class finalize */
164 NULL, /* class data */
165 sizeof(ModestFolderView),
167 (GInstanceInitFunc) modest_folder_view_init,
171 static const GInterfaceInfo tny_account_store_view_info = {
172 (GInterfaceInitFunc) tny_account_store_view_init, /* interface_init */
173 NULL, /* interface_finalize */
174 NULL /* interface_data */
178 my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
182 g_type_add_interface_static (my_type,
183 TNY_TYPE_ACCOUNT_STORE_VIEW,
184 &tny_account_store_view_info);
190 modest_folder_view_class_init (ModestFolderViewClass *klass)
192 GObjectClass *gobject_class;
193 gobject_class = (GObjectClass*) klass;
195 parent_class = g_type_class_peek_parent (klass);
196 gobject_class->finalize = modest_folder_view_finalize;
198 g_type_class_add_private (gobject_class,
199 sizeof(ModestFolderViewPrivate));
201 signals[FOLDER_SELECTION_CHANGED_SIGNAL] =
202 g_signal_new ("folder_selection_changed",
203 G_TYPE_FROM_CLASS (gobject_class),
205 G_STRUCT_OFFSET (ModestFolderViewClass,
206 folder_selection_changed),
208 modest_marshal_VOID__POINTER_BOOLEAN,
209 G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_BOOLEAN);
212 * ModestFolderView::folder-moved
213 * @self: the #MailOperation that emits the signal
214 * @folder: the #TnyFolder that is going to be moved
215 * @parent: then #TnyFolderStore that is going to be the new parent
216 * @done: indicates if the folder move was correctly completed or not
217 * @user_data: user data set when the signal handler was connected
219 * Emitted when a the user wants to move a folder through a
220 * drag and drop action
222 signals[FOLDER_XFER_SIGNAL] =
223 g_signal_new ("folder_xfer",
224 G_TYPE_FROM_CLASS (gobject_class),
226 G_STRUCT_OFFSET (ModestFolderViewClass, folder_xfer),
228 modest_marshal_VOID__POINTER_POINTER_BOOL_POINTER,
230 G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_BOOLEAN, G_TYPE_POINTER);
236 text_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
237 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
243 TnyFolder *folder = NULL;
245 g_return_if_fail (column);
246 g_return_if_fail (tree_model);
248 gtk_tree_model_get (tree_model, iter,
249 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname,
250 TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN, &unread,
251 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
252 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder,
254 rendobj = G_OBJECT(renderer);
259 if (folder && type != TNY_FOLDER_TYPE_ROOT) { /* FIXME: tnymail bug? crashes with root folders */
260 if (modest_tny_folder_is_local_folder (folder)) {
262 type = modest_tny_folder_get_local_folder_type (folder);
263 if (type != TNY_FOLDER_TYPE_UNKNOWN) {
265 fname = g_strdup(modest_local_folder_info_get_type_display_name (type));
268 } else if (folder && type == TNY_FOLDER_TYPE_ROOT) {
269 if (strcmp (fname, MODEST_LOCAL_FOLDERS_ACCOUNT_NAME) == 0) {/* FIXME: hack */
271 fname = g_strdup (MODEST_LOCAL_FOLDERS_DISPLAY_NAME);
276 gchar *folder_title = g_strdup_printf ("%s (%d)", fname, unread);
277 g_object_set (rendobj,"text", folder_title, "weight", 800, NULL);
278 g_free (folder_title);
280 g_object_set (rendobj,"text", fname, "weight", 400, NULL);
283 if (folder) g_object_unref (G_OBJECT (folder));
288 get_cached_icon (const gchar *name)
293 static GHashTable *icon_cache = NULL;
295 g_return_val_if_fail (name, NULL);
297 if (G_UNLIKELY(!icon_cache))
298 icon_cache = modest_cache_mgr_get_cache (modest_runtime_get_cache_mgr(),
299 MODEST_CACHE_MGR_CACHE_TYPE_PIXBUF);
301 if (!icon_cache || !g_hash_table_lookup_extended (icon_cache, name, &orig_key, &pixbuf)) {
302 pixbuf = (gpointer)gdk_pixbuf_new_from_file (name, &err);
304 g_printerr ("modest: error in icon factory while loading '%s': %s\n",
308 /* if we cannot find it, we still insert (if we have a cache), so we get the error
311 g_hash_table_insert (icon_cache, g_strdup(name),(gpointer)pixbuf);
313 return GDK_PIXBUF(pixbuf);
318 icon_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
319 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
327 rendobj = G_OBJECT(renderer);
328 gtk_tree_model_get (tree_model, iter,
329 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
330 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname,
331 TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN, &unread,
333 rendobj = G_OBJECT(renderer);
335 if (type == TNY_FOLDER_TYPE_NORMAL || type == TNY_FOLDER_TYPE_UNKNOWN) {
336 type = modest_tny_folder_guess_folder_type_from_name (fname);
341 case TNY_FOLDER_TYPE_ROOT:
342 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_ACCOUNT);
344 case TNY_FOLDER_TYPE_INBOX:
345 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_INBOX);
347 case TNY_FOLDER_TYPE_OUTBOX:
348 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_OUTBOX);
350 case TNY_FOLDER_TYPE_JUNK:
351 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_JUNK);
353 case TNY_FOLDER_TYPE_SENT:
354 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_SENT);
356 case TNY_FOLDER_TYPE_TRASH:
357 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_TRASH);
359 case TNY_FOLDER_TYPE_DRAFTS:
360 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_DRAFTS);
362 case TNY_FOLDER_TYPE_NOTES:
363 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_NOTES);
365 case TNY_FOLDER_TYPE_CALENDAR:
366 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_CALENDAR);
368 case TNY_FOLDER_TYPE_CONTACTS:
369 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_CONTACTS);
371 case TNY_FOLDER_TYPE_NORMAL:
373 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_NORMAL);
376 g_object_set (rendobj, "pixbuf", pixbuf, NULL);
380 modest_folder_view_init (ModestFolderView *obj)
382 ModestFolderViewPrivate *priv;
383 GtkTreeViewColumn *column;
384 GtkCellRenderer *renderer;
385 GtkTreeSelection *sel;
387 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
389 priv->timer_expander = 0;
390 priv->account_store = NULL;
391 priv->cur_folder = NULL;
392 priv->cur_row = NULL;
394 priv->monitor = NULL;
396 priv->lock = g_mutex_new ();
398 column = gtk_tree_view_column_new ();
399 gtk_tree_view_append_column (GTK_TREE_VIEW(obj),column);
401 renderer = gtk_cell_renderer_pixbuf_new();
402 gtk_tree_view_column_pack_start (column, renderer, FALSE);
403 gtk_tree_view_column_set_cell_data_func(column, renderer,
404 icon_cell_data, NULL, NULL);
406 renderer = gtk_cell_renderer_text_new();
407 gtk_tree_view_column_pack_start (column, renderer, FALSE);
408 gtk_tree_view_column_set_cell_data_func(column, renderer,
409 text_cell_data, NULL, NULL);
411 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(obj));
412 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
414 gtk_tree_view_column_set_spacing (column, 2);
415 gtk_tree_view_column_set_resizable (column, TRUE);
416 gtk_tree_view_column_set_fixed_width (column, TRUE);
417 gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(obj), FALSE);
418 gtk_tree_view_set_enable_search (GTK_TREE_VIEW(obj), FALSE);
420 setup_drag_and_drop (GTK_TREE_VIEW(obj));
424 tny_account_store_view_init (gpointer g, gpointer iface_data)
426 TnyAccountStoreViewIface *klass = (TnyAccountStoreViewIface *)g;
428 klass->set_account_store_func = modest_folder_view_set_account_store;
434 modest_folder_view_finalize (GObject *obj)
436 ModestFolderViewPrivate *priv;
437 GtkTreeSelection *sel;
439 g_return_if_fail (obj);
441 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
443 if (priv->timer_expander != 0) {
444 g_source_remove (priv->timer_expander);
445 priv->timer_expander = 0;
448 if (priv->account_store) {
449 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
450 priv->account_update_signal);
451 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
452 priv->accounts_reloaded_signal);
453 g_object_unref (G_OBJECT(priv->account_store));
454 priv->account_store = NULL;
458 g_mutex_free (priv->lock);
463 g_object_unref (G_OBJECT (priv->query));
467 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(obj));
469 g_signal_handler_disconnect (G_OBJECT(sel), priv->changed_signal);
471 G_OBJECT_CLASS(parent_class)->finalize (obj);
476 modest_folder_view_set_account_store (TnyAccountStoreView *self, TnyAccountStore *account_store)
478 ModestFolderViewPrivate *priv;
481 g_return_if_fail (MODEST_IS_FOLDER_VIEW (self));
482 g_return_if_fail (TNY_IS_ACCOUNT_STORE (account_store));
484 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
485 device = tny_account_store_get_device (account_store);
487 if (G_UNLIKELY (priv->account_store)) {
489 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store),
490 priv->account_update_signal))
491 g_signal_handler_disconnect (G_OBJECT (priv->account_store),
492 priv->account_update_signal);
493 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store),
494 priv->accounts_reloaded_signal))
495 g_signal_handler_disconnect (G_OBJECT (priv->account_store),
496 priv->accounts_reloaded_signal);
498 g_object_unref (G_OBJECT (priv->account_store));
501 priv->account_store = g_object_ref (G_OBJECT (account_store));
503 priv->account_update_signal =
504 g_signal_connect (G_OBJECT(account_store), "account_update",
505 G_CALLBACK (on_account_update), self);
507 priv->accounts_reloaded_signal =
508 g_signal_connect (G_OBJECT(account_store), "accounts_reloaded",
509 G_CALLBACK (on_accounts_reloaded), self);
511 if (!update_model (MODEST_FOLDER_VIEW (self),
512 MODEST_TNY_ACCOUNT_STORE (priv->account_store)))
513 g_printerr ("modest: failed to update model\n");
515 g_object_unref (G_OBJECT (device));
519 on_account_update (TnyAccountStore *account_store, const gchar *account,
522 if (!update_model (MODEST_FOLDER_VIEW(user_data),
523 MODEST_TNY_ACCOUNT_STORE(account_store)))
524 g_printerr ("modest: failed to update model for changes in '%s'",
529 on_accounts_reloaded (TnyAccountStore *account_store,
532 update_model (MODEST_FOLDER_VIEW (user_data),
533 MODEST_TNY_ACCOUNT_STORE(account_store));
537 modest_folder_view_set_title (ModestFolderView *self, const gchar *title)
539 GtkTreeViewColumn *col;
541 g_return_if_fail (self);
543 col = gtk_tree_view_get_column (GTK_TREE_VIEW(self), 0);
545 g_printerr ("modest: failed get column for title\n");
549 gtk_tree_view_column_set_title (col, title);
550 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self),
555 modest_folder_view_new (TnyFolderStoreQuery *query)
558 ModestFolderViewPrivate *priv;
559 GtkTreeSelection *sel;
561 self = G_OBJECT (g_object_new (MODEST_TYPE_FOLDER_VIEW, NULL));
562 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
564 priv->query = g_object_ref (query);
566 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
567 priv->changed_signal = g_signal_connect (sel, "changed",
568 G_CALLBACK (on_selection_changed), self);
569 return GTK_WIDGET(self);
574 update_model_empty (ModestFolderView *self)
576 ModestFolderViewPrivate *priv;
578 g_return_val_if_fail (self, FALSE);
579 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
581 g_mutex_lock (priv->lock);
584 tny_folder_monitor_stop (priv->monitor);
585 g_object_unref(G_OBJECT(priv->monitor));
586 priv->monitor = NULL;
589 g_mutex_unlock (priv->lock);
591 g_signal_emit (G_OBJECT(self), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
597 /* this feels dirty; any other way to expand all the root items? */
599 expand_root_items (ModestFolderView *self)
602 path = gtk_tree_path_new_first ();
604 /* all folders should have child items, so.. */
605 while (gtk_tree_view_expand_row (GTK_TREE_VIEW(self), path, FALSE))
606 gtk_tree_path_next (path);
608 gtk_tree_path_free (path);
612 update_model (ModestFolderView *self, ModestTnyAccountStore *account_store)
614 ModestFolderViewPrivate *priv;
616 TnyList *account_list;
617 GtkTreeModel *model, *sortable;
619 g_return_val_if_fail (account_store, FALSE);
621 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
623 /* Notify that there is no folder selected */
624 update_model_empty (self);
626 /* FIXME: the local accounts are not shown when the query
627 selects only the subscribed folders. */
628 /* model = tny_gtk_folder_store_tree_model_new (TRUE, priv->query); */
629 model = tny_gtk_folder_store_tree_model_new (TRUE, NULL);
630 account_list = TNY_LIST(model);
632 tny_account_store_get_accounts (TNY_ACCOUNT_STORE(account_store),
634 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
636 sortable = gtk_tree_model_sort_new_with_model (model);
637 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(sortable),
638 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
640 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (sortable),
641 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
642 cmp_rows, NULL, NULL);
645 gtk_tree_view_set_model (GTK_TREE_VIEW(self), sortable);
646 expand_root_items (self); /* expand all account folders */
647 g_object_unref (account_list);
655 on_selection_changed (GtkTreeSelection *sel, gpointer user_data)
657 GtkTreeModel *model_sort, *model;
658 TnyFolder *folder = NULL;
659 GtkTreeIter iter, iter_sort;
661 ModestFolderView *tree_view;
662 ModestFolderViewPrivate *priv;
665 g_return_if_fail (sel);
666 g_return_if_fail (user_data);
668 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(user_data);
669 priv->cur_selection = sel;
671 /* folder was _un_selected if true */
672 if (!gtk_tree_selection_get_selected (sel, &model_sort, &iter_sort)) {
673 priv->cur_folder = NULL; /* FIXME: need this? */
674 gtk_tree_row_reference_free (priv->cur_row);
675 priv->cur_row = NULL;
679 model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
680 gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (model_sort),
684 tree_view = MODEST_FOLDER_VIEW (user_data);
686 gtk_tree_model_get (model, &iter,
687 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
688 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder,
691 if (type == TNY_FOLDER_TYPE_ROOT) {
692 g_object_unref (folder);
696 /* Current folder was unselected */
697 g_signal_emit (G_OBJECT(tree_view), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
698 priv->cur_folder, FALSE);
701 /* tny_folder_sync (priv->cur_folder, TRUE, NULL); /\* FIXME *\/ */
702 gtk_tree_row_reference_free (priv->cur_row);
705 /* New current references */
706 path = gtk_tree_model_get_path (model_sort, &iter_sort);
707 priv->cur_folder = folder;
708 priv->cur_row = gtk_tree_row_reference_new (model_sort, path);
711 gtk_tree_path_free (path);
712 g_object_unref (G_OBJECT (folder));
714 /* New folder has been selected */
715 g_signal_emit (G_OBJECT(tree_view),
716 signals[FOLDER_SELECTION_CHANGED_SIGNAL],
721 modest_folder_view_get_selected (ModestFolderView *self)
723 ModestFolderViewPrivate *priv;
725 g_return_val_if_fail (self, NULL);
727 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
728 if (priv->cur_folder)
729 g_object_ref (priv->cur_folder);
731 return priv->cur_folder;
735 get_model_iter (ModestFolderView *self,
736 GtkTreeModel **model,
739 GtkTreeModel *model_sort;
740 GtkTreeIter iter_sort;
742 ModestFolderViewPrivate *priv;
744 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
746 if (!priv->cur_folder)
749 if (!gtk_tree_row_reference_valid (priv->cur_row))
752 model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
753 *model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
755 /* Get path to retrieve iter */
756 path = gtk_tree_row_reference_get_path (priv->cur_row);
757 if (!gtk_tree_model_get_iter (model_sort, &iter_sort, path))
760 gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (model_sort),
767 modest_folder_view_rename (ModestFolderView *self)
771 ModestFolderViewPrivate *priv;
773 g_return_val_if_fail (MODEST_IS_FOLDER_VIEW (self), FALSE);
775 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
777 if (!get_model_iter (self, &model, &iter))
781 gtk_tree_store_set (GTK_TREE_STORE (model), &iter,
782 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
783 tny_folder_get_name (priv->cur_folder), -1);
785 /* Invalidate selection */
786 g_signal_emit (G_OBJECT(self), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
787 priv->cur_folder, TRUE);
793 modest_folder_view_add_subfolder (ModestFolderView *self, TnyFolder *folder)
796 GtkTreeIter iter, child;
798 g_return_val_if_fail (MODEST_IS_FOLDER_VIEW (self), FALSE);
800 if (!get_model_iter (self, &model, &iter))
803 /* Append a new child to the folder */
804 gtk_tree_store_append (GTK_TREE_STORE (model), &child, &iter);
805 gtk_tree_store_set (GTK_TREE_STORE (model), &child,
806 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
807 tny_folder_get_name (TNY_FOLDER (folder)),
808 TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN,
809 tny_folder_get_unread_count (TNY_FOLDER (folder)),
810 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN,
811 tny_folder_get_folder_type (TNY_FOLDER (folder)),
812 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
819 cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2,
823 gchar *name1, *name2;
825 TnyFolder *folder1, *folder2;
827 gtk_tree_model_get (tree_model, iter1,
828 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name1,
829 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
830 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder1,
832 gtk_tree_model_get (tree_model, iter2,
833 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name2,
834 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder2,
837 /* local_folders should be the last one */
838 if (type == TNY_FOLDER_TYPE_ROOT) {
839 /* the account name is also the name of the root folder
840 * in case of local folders */
841 if (name1 && strcmp (name1, MODEST_LOCAL_FOLDERS_ACCOUNT_NAME) == 0)
843 else if (name2 && strcmp (name2, MODEST_LOCAL_FOLDERS_ACCOUNT_NAME) == 0)
846 cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
848 cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
852 g_object_unref(G_OBJECT(folder1));
854 g_object_unref(G_OBJECT(folder2));
862 /*****************************************************************************/
863 /* DRAG and DROP stuff */
864 /*****************************************************************************/
867 * This function fills the #GtkSelectionData with the row and the
868 * model that has been dragged. It's called when this widget is a
869 * source for dnd after the event drop happened
872 on_drag_data_get (GtkWidget *widget,
873 GdkDragContext *context,
874 GtkSelectionData *selection_data,
879 GtkTreeSelection *selection;
880 GtkTreeModel *model_sort, *model;
882 GtkTreePath *source_row_sort;
883 GtkTreePath *source_row;
885 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
886 gtk_tree_selection_get_selected (selection, &model_sort, &iter);
887 source_row_sort = gtk_tree_model_get_path (model_sort, &iter);
889 /* Get the unsorted path and model */
890 model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
891 source_row = gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
894 gtk_tree_set_row_drag_data (selection_data,
898 gtk_tree_path_free (source_row_sort);
899 gtk_tree_path_free (source_row);
904 on_progress_changed (ModestMailOperation *mail_op, gpointer user_data)
906 ModestMailOperationQueue *queue;
908 if (modest_mail_operation_get_status (mail_op) ==
909 MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
910 g_print ("Bien bien");
912 queue = modest_runtime_get_mail_operation_queue ();
914 modest_mail_operation_queue_remove (queue, mail_op);
915 g_object_unref (G_OBJECT (mail_op));
919 * This function receives the data set by the "drag-data-get" signal
920 * handler. This information comes within the #GtkSelectionData. This
921 * function will manage both the drags of folders of the treeview and
922 * drags of headers of the header view widget.
925 on_drag_data_received (GtkWidget *widget,
926 GdkDragContext *context,
929 GtkSelectionData *selection_data,
934 GtkWidget *source_widget;
935 GtkTreeModel *model_sort, *dest_model, *source_model;
936 GtkTreePath *source_row, *dest_row, *child_dest_row;
937 GtkTreeViewDropPosition pos;
938 gboolean success = FALSE, delete_source = FALSE;
940 /* Do not allow further process */
941 g_signal_stop_emission_by_name (widget, "drag-data-received");
943 /* Check if the get_data failed */
944 if (selection_data == NULL || selection_data->length < 0)
948 if (context->action == GDK_ACTION_MOVE)
949 delete_source = TRUE;
952 source_widget = gtk_drag_get_source_widget (context);
953 model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
955 gtk_tree_get_row_drag_data (selection_data,
959 /* Select the destination model */
960 if (source_widget == widget)
961 dest_model = source_model;
963 dest_model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
966 /* Get the path to the destination row. Can not call
967 gtk_tree_view_get_drag_dest_row() because the source row
968 it's not selected anymore */
969 gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
974 /* Only allow drops IN other rows */
976 pos == GTK_TREE_VIEW_DROP_BEFORE ||
977 pos == GTK_TREE_VIEW_DROP_AFTER)
980 /* Get the destination row in the usorted model */
982 gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
985 /* Drags from the header view */
986 if ((target_type == TARGET_TREE_ROW) && (source_widget != widget)) {
989 ModestMailOperationQueue *queue;
990 ModestMailOperation *mail_op;
992 GtkTreeIter source_iter, dest_iter;
995 gtk_tree_model_get_iter (source_model, &source_iter, source_row);
996 gtk_tree_model_get (source_model, &source_iter,
997 TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN,
1001 gtk_tree_model_get_iter (dest_model, &dest_iter, dest_row);
1002 gtk_tree_model_get (dest_model, &dest_iter,
1003 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
1006 /* Transfer message */
1007 queue = modest_runtime_get_mail_operation_queue ();
1008 mail_op = modest_mail_operation_new ();
1009 started = modest_mail_operation_xfer_msg (mail_op, header,
1010 folder, delete_source);
1012 g_signal_connect (G_OBJECT (mail_op), "progress_changed",
1013 G_CALLBACK (on_progress_changed), queue);
1014 modest_mail_operation_queue_add (queue, mail_op);
1016 const GError *error;
1017 error = modest_mail_operation_get_error (mail_op);
1018 g_warning ("Error trying to transfer messages: %s\n",
1021 g_object_unref (G_OBJECT (mail_op));
1022 g_object_unref (G_OBJECT (header));
1023 g_object_unref (G_OBJECT (folder));
1028 GtkTreeRowReference *source_row_reference;
1029 GtkTreeIter parent_iter, iter;
1030 TnyFolder *folder, *new_folder;
1031 TnyFolderStore *parent_folder;
1033 /* Check if the drag is possible */
1034 if (!gtk_tree_path_compare (source_row, child_dest_row))
1037 if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (dest_model),
1042 /* Do the mail operation */
1043 gtk_tree_model_get_iter (source_model, &parent_iter, child_dest_row);
1044 gtk_tree_model_get_iter (source_model, &iter, source_row);
1045 gtk_tree_model_get (source_model, &parent_iter,
1046 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
1047 &parent_folder, -1);
1048 gtk_tree_model_get (source_model, &iter,
1049 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
1052 g_signal_emit (G_OBJECT (widget), signals[FOLDER_XFER_SIGNAL], 0,
1053 folder, parent_folder, delete_source, &new_folder);
1055 g_object_unref (G_OBJECT (parent_folder));
1056 g_object_unref (G_OBJECT (folder));
1061 /* Get a row reference to the source path because the path
1062 could change after the insertion. The gtk_drag_finish() is
1063 not able to delete the source because that, so we have to
1065 source_row_reference = gtk_tree_row_reference_new (source_model, source_row);
1066 gtk_tree_path_free (source_row);
1068 /* Insert the dragged row as a child of the dest row */
1069 gtk_tree_path_down (child_dest_row);
1070 if (gtk_tree_drag_dest_drag_data_received (GTK_TREE_DRAG_DEST (dest_model),
1077 gtk_tree_model_get_iter (dest_model, &iter, child_dest_row);
1078 gtk_tree_store_set (GTK_TREE_STORE (dest_model), &iter,
1079 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, new_folder, -1);
1080 g_object_unref (G_OBJECT (new_folder));
1083 source_row = gtk_tree_row_reference_get_path (source_row_reference);
1087 gtk_tree_row_reference_free (source_row_reference);
1090 g_object_unref (G_OBJECT(folder));
1092 g_object_unref (G_OBJECT(parent_folder));
1096 gtk_tree_path_free (dest_row);
1097 gtk_tree_path_free (child_dest_row);
1099 /* Save the new path, will be used by the
1100 drag-data-delete handler */
1101 if (success && delete_source)
1102 g_object_set_data (G_OBJECT (source_widget),
1104 gtk_tree_path_copy (source_row));
1106 /* Clean dest row */
1107 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget),
1109 GTK_TREE_VIEW_DROP_BEFORE);
1112 gtk_drag_finish (context, success, (success && delete_source), time);
1116 * We define a "drag-drop" signal handler because we do not want to
1117 * use the default one, because the default one always calls
1118 * gtk_drag_finish and we prefer to do it in the "drag-data-received"
1119 * signal handler, because there we have all the information available
1120 * to know if the dnd was a success or not.
1123 drag_drop_cb (GtkWidget *widget,
1124 GdkDragContext *context,
1130 if (!context->targets)
1133 /* Request the data from the source. */
1134 gtk_drag_get_data(widget,
1136 GDK_POINTER_TO_ATOM (context->targets->data),
1143 * This function deletes the data that has been dragged from its
1144 * source widget. Since is a function received by the source of the
1145 * drag, this function only deletes rows of the folder view
1146 * widget. The header view widget will need to define its own one.
1149 drag_data_delete_cb (GtkWidget *widget,
1150 GdkDragContext *context,
1153 GtkTreePath *source_row;
1154 GtkTreeModel *model_sort, *model;
1156 model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
1157 model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
1158 source_row = g_object_steal_data (G_OBJECT (widget), ROW_REF_DATA_NAME);
1160 /* Delete the source row */
1161 gtk_tree_drag_source_drag_data_delete (GTK_TREE_DRAG_SOURCE (model),
1164 gtk_tree_path_free (source_row);
1168 * This function expands a node of a tree view if it's not expanded
1169 * yet. Not sure why it needs the threads stuff, but gtk+`example code
1170 * does that, so that's why they're here.
1173 expand_row_timeout (gpointer data)
1175 GtkTreeView *tree_view = data;
1176 GtkTreePath *dest_path = NULL;
1177 GtkTreeViewDropPosition pos;
1178 gboolean result = FALSE;
1180 GDK_THREADS_ENTER ();
1182 gtk_tree_view_get_drag_dest_row (tree_view,
1187 (pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER ||
1188 pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)) {
1189 gtk_tree_view_expand_row (tree_view, dest_path, FALSE);
1190 gtk_tree_path_free (dest_path);
1194 gtk_tree_path_free (dest_path);
1199 GDK_THREADS_LEAVE ();
1205 * This function is called whenever the pointer is moved over a widget
1206 * while dragging some data. It installs a timeout that will expand a
1207 * node of the treeview if not expanded yet. This function also calls
1208 * gdk_drag_status in order to set the suggested action that will be
1209 * used by the "drag-data-received" signal handler to know if we
1210 * should do a move or just a copy of the data.
1213 on_drag_motion (GtkWidget *widget,
1214 GdkDragContext *context,
1220 GtkTreeViewDropPosition pos;
1221 GtkTreePath *dest_row;
1222 ModestFolderViewPrivate *priv;
1223 GdkDragAction suggested_action;
1225 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (widget);
1227 if (priv->timer_expander != 0) {
1228 g_source_remove (priv->timer_expander);
1229 priv->timer_expander = 0;
1232 gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
1240 /* Expand the selected row after 1/2 second */
1241 if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), dest_row)) {
1242 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), dest_row, pos);
1243 priv->timer_expander = g_timeout_add (500, expand_row_timeout, widget);
1245 gtk_tree_path_free (dest_row);
1247 /* Select the desired action. By default we pick MOVE */
1248 suggested_action = GDK_ACTION_MOVE;
1250 if (context->actions == GDK_ACTION_COPY)
1251 gdk_drag_status(context, GDK_ACTION_COPY, time);
1252 else if (context->actions == GDK_ACTION_MOVE)
1253 gdk_drag_status(context, GDK_ACTION_MOVE, time);
1254 else if (context->actions & suggested_action)
1255 gdk_drag_status(context, suggested_action, time);
1257 gdk_drag_status(context, GDK_ACTION_DEFAULT, time);
1263 * This function sets the treeview as a source and a target for dnd
1264 * events. It also connects all the requirede signals.
1267 setup_drag_and_drop (GtkTreeView *self)
1269 /* Set up the folder view as a dnd destination. Set only the
1270 highlight flag, otherwise gtk will have a different
1272 gtk_drag_dest_set (GTK_WIDGET (self),
1273 GTK_DEST_DEFAULT_HIGHLIGHT,
1275 G_N_ELEMENTS (drag_types),
1276 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1278 gtk_signal_connect(GTK_OBJECT (self),
1279 "drag_data_received",
1280 GTK_SIGNAL_FUNC(on_drag_data_received),
1284 /* Set up the treeview as a dnd source */
1285 gtk_drag_source_set (GTK_WIDGET (self),
1288 G_N_ELEMENTS (drag_types),
1289 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1291 gtk_signal_connect(GTK_OBJECT (self),
1293 GTK_SIGNAL_FUNC(drag_data_delete_cb),
1296 gtk_signal_connect(GTK_OBJECT (self),
1298 GTK_SIGNAL_FUNC(on_drag_motion),
1302 gtk_signal_connect(GTK_OBJECT (self),
1304 GTK_SIGNAL_FUNC(on_drag_data_get),
1307 gtk_signal_connect(GTK_OBJECT (self),
1309 GTK_SIGNAL_FUNC(drag_drop_cb),