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-account-store.h>
39 #include <tny-account.h>
40 #include <tny-folder.h>
41 #include <tny-camel-folder.h>
42 #include <tny-simple-list.h>
43 #include <modest-tny-folder.h>
44 #include <modest-marshal.h>
45 #include <modest-icon-names.h>
46 #include <modest-tny-account-store.h>
47 #include <modest-text-utils.h>
48 #include <modest-runtime.h>
49 #include "modest-folder-view.h"
50 #include <modest-dnd.h>
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 void on_selection_changed (GtkTreeSelection *sel, gpointer data);
68 static void on_account_update (TnyAccountStore *account_store,
72 static void on_accounts_reloaded (TnyAccountStore *store,
75 static gint cmp_rows (GtkTreeModel *tree_model,
81 static void on_drag_data_get (GtkWidget *widget,
82 GdkDragContext *context,
83 GtkSelectionData *selection_data,
88 static void on_drag_data_received (GtkWidget *widget,
89 GdkDragContext *context,
92 GtkSelectionData *selection_data,
97 static gboolean on_drag_motion (GtkWidget *widget,
98 GdkDragContext *context,
104 static gint expand_row_timeout (gpointer data);
106 static void setup_drag_and_drop (GtkTreeView *self);
109 FOLDER_SELECTION_CHANGED_SIGNAL,
113 typedef struct _ModestFolderViewPrivate ModestFolderViewPrivate;
114 struct _ModestFolderViewPrivate {
115 TnyAccountStore *account_store;
116 TnyFolder *cur_folder;
117 GtkTreeRowReference *cur_row;
119 gulong account_update_signal;
120 gulong changed_signal;
121 gulong accounts_reloaded_signal;
123 GtkTreeSelection *cur_selection;
124 TnyFolderStoreQuery *query;
125 guint timer_expander;
127 #define MODEST_FOLDER_VIEW_GET_PRIVATE(o) \
128 (G_TYPE_INSTANCE_GET_PRIVATE((o), \
129 MODEST_TYPE_FOLDER_VIEW, \
130 ModestFolderViewPrivate))
132 static GObjectClass *parent_class = NULL;
134 static guint signals[LAST_SIGNAL] = {0};
137 modest_folder_view_get_type (void)
139 static GType my_type = 0;
141 static const GTypeInfo my_info = {
142 sizeof(ModestFolderViewClass),
143 NULL, /* base init */
144 NULL, /* base finalize */
145 (GClassInitFunc) modest_folder_view_class_init,
146 NULL, /* class finalize */
147 NULL, /* class data */
148 sizeof(ModestFolderView),
150 (GInstanceInitFunc) modest_folder_view_init,
154 static const GInterfaceInfo tny_account_store_view_info = {
155 (GInterfaceInitFunc) tny_account_store_view_init, /* interface_init */
156 NULL, /* interface_finalize */
157 NULL /* interface_data */
161 my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
165 g_type_add_interface_static (my_type,
166 TNY_TYPE_ACCOUNT_STORE_VIEW,
167 &tny_account_store_view_info);
173 modest_folder_view_class_init (ModestFolderViewClass *klass)
175 GObjectClass *gobject_class;
176 gobject_class = (GObjectClass*) klass;
178 parent_class = g_type_class_peek_parent (klass);
179 gobject_class->finalize = modest_folder_view_finalize;
181 g_type_class_add_private (gobject_class,
182 sizeof(ModestFolderViewPrivate));
184 signals[FOLDER_SELECTION_CHANGED_SIGNAL] =
185 g_signal_new ("folder_selection_changed",
186 G_TYPE_FROM_CLASS (gobject_class),
188 G_STRUCT_OFFSET (ModestFolderViewClass,
189 folder_selection_changed),
191 modest_marshal_VOID__POINTER_BOOLEAN,
192 G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_BOOLEAN);
198 text_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
199 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
205 TnyFolder *folder = NULL;
207 g_return_if_fail (column);
208 g_return_if_fail (tree_model);
210 gtk_tree_model_get (tree_model, iter,
211 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname,
212 TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN, &unread,
213 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
214 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder,
216 rendobj = G_OBJECT(renderer);
221 if (folder && type != TNY_FOLDER_TYPE_ROOT) { /* FIXME: tnymail bug? crashes with root folders */
222 if (modest_tny_folder_is_local_folder (folder)) {
224 type = modest_tny_folder_get_local_folder_type (folder);
225 if (type != TNY_FOLDER_TYPE_UNKNOWN) {
227 fname = g_strdup(modest_local_folder_info_get_type_display_name (type));
230 } else if (folder && type == TNY_FOLDER_TYPE_ROOT) {
235 gchar *folder_title = g_strdup_printf ("%s (%d)", fname, unread);
236 g_object_set (rendobj,"text", folder_title, "weight", 800, NULL);
237 g_free (folder_title);
239 g_object_set (rendobj,"text", fname, "weight", 400, NULL);
242 if (folder) g_object_unref (G_OBJECT (folder));
247 get_cached_icon (const gchar *name)
252 static GHashTable *icon_cache = NULL;
254 g_return_val_if_fail (name, NULL);
256 if (G_UNLIKELY(!icon_cache))
257 icon_cache = modest_cache_mgr_get_cache (modest_runtime_get_cache_mgr(),
258 MODEST_CACHE_MGR_CACHE_TYPE_PIXBUF);
260 if (!icon_cache || !g_hash_table_lookup_extended (icon_cache, name, &orig_key, &pixbuf)) {
261 pixbuf = (gpointer)gdk_pixbuf_new_from_file (name, &err);
263 g_printerr ("modest: error in icon factory while loading '%s': %s\n",
267 /* if we cannot find it, we still insert (if we have a cache), so we get the error
270 g_hash_table_insert (icon_cache, g_strdup(name),(gpointer)pixbuf);
272 return GDK_PIXBUF(pixbuf);
277 icon_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
278 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
286 rendobj = G_OBJECT(renderer);
287 gtk_tree_model_get (tree_model, iter,
288 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
289 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname,
290 TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN, &unread,
292 rendobj = G_OBJECT(renderer);
294 if (type == TNY_FOLDER_TYPE_NORMAL || type == TNY_FOLDER_TYPE_UNKNOWN) {
295 type = modest_tny_folder_guess_folder_type_from_name (fname);
300 case TNY_FOLDER_TYPE_ROOT:
301 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_ACCOUNT);
303 case TNY_FOLDER_TYPE_INBOX:
304 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_INBOX);
306 case TNY_FOLDER_TYPE_OUTBOX:
307 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_OUTBOX);
309 case TNY_FOLDER_TYPE_JUNK:
310 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_JUNK);
312 case TNY_FOLDER_TYPE_SENT:
313 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_SENT);
315 case TNY_FOLDER_TYPE_TRASH:
316 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_TRASH);
318 case TNY_FOLDER_TYPE_DRAFTS:
319 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_DRAFTS);
321 case TNY_FOLDER_TYPE_NOTES:
322 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_NOTES);
324 case TNY_FOLDER_TYPE_CALENDAR:
325 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_CALENDAR);
327 case TNY_FOLDER_TYPE_CONTACTS:
328 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_CONTACTS);
330 case TNY_FOLDER_TYPE_NORMAL:
332 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_NORMAL);
335 g_object_set (rendobj, "pixbuf", pixbuf, NULL);
339 modest_folder_view_init (ModestFolderView *obj)
341 ModestFolderViewPrivate *priv;
342 GtkTreeViewColumn *column;
343 GtkCellRenderer *renderer;
344 GtkTreeSelection *sel;
346 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
348 priv->timer_expander = 0;
349 priv->account_store = NULL;
350 priv->cur_folder = NULL;
351 priv->cur_row = NULL;
354 column = gtk_tree_view_column_new ();
356 renderer = gtk_cell_renderer_pixbuf_new();
357 gtk_tree_view_column_pack_start (column, renderer, FALSE);
358 gtk_tree_view_column_set_cell_data_func(column, renderer,
359 icon_cell_data, NULL, NULL);
361 renderer = gtk_cell_renderer_text_new();
362 gtk_tree_view_column_pack_start (column, renderer, FALSE);
363 gtk_tree_view_column_set_cell_data_func(column, renderer,
364 text_cell_data, NULL, NULL);
366 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(obj));
367 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
369 gtk_tree_view_column_set_spacing (column, 2);
370 gtk_tree_view_column_set_resizable (column, TRUE);
371 gtk_tree_view_column_set_fixed_width (column, TRUE);
372 gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(obj), FALSE);
373 gtk_tree_view_set_enable_search (GTK_TREE_VIEW(obj), FALSE);
375 gtk_tree_view_append_column (GTK_TREE_VIEW(obj),column);
377 setup_drag_and_drop (GTK_TREE_VIEW(obj));
381 tny_account_store_view_init (gpointer g, gpointer iface_data)
383 TnyAccountStoreViewIface *klass = (TnyAccountStoreViewIface *)g;
385 klass->set_account_store_func = modest_folder_view_set_account_store;
391 modest_folder_view_finalize (GObject *obj)
393 ModestFolderViewPrivate *priv;
394 GtkTreeSelection *sel;
396 g_return_if_fail (obj);
398 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
400 if (priv->timer_expander != 0) {
401 g_source_remove (priv->timer_expander);
402 priv->timer_expander = 0;
405 if (priv->account_store) {
406 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
407 priv->account_update_signal);
408 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
409 priv->accounts_reloaded_signal);
410 g_object_unref (G_OBJECT(priv->account_store));
411 priv->account_store = NULL;
415 g_object_unref (G_OBJECT (priv->query));
419 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(obj));
421 g_signal_handler_disconnect (G_OBJECT(sel), priv->changed_signal);
423 G_OBJECT_CLASS(parent_class)->finalize (obj);
428 modest_folder_view_set_account_store (TnyAccountStoreView *self, TnyAccountStore *account_store)
430 ModestFolderViewPrivate *priv;
433 g_return_if_fail (MODEST_IS_FOLDER_VIEW (self));
434 g_return_if_fail (TNY_IS_ACCOUNT_STORE (account_store));
436 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
437 device = tny_account_store_get_device (account_store);
439 if (G_UNLIKELY (priv->account_store)) {
441 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store),
442 priv->account_update_signal))
443 g_signal_handler_disconnect (G_OBJECT (priv->account_store),
444 priv->account_update_signal);
445 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store),
446 priv->accounts_reloaded_signal))
447 g_signal_handler_disconnect (G_OBJECT (priv->account_store),
448 priv->accounts_reloaded_signal);
450 g_object_unref (G_OBJECT (priv->account_store));
453 priv->account_store = g_object_ref (G_OBJECT (account_store));
455 priv->account_update_signal =
456 g_signal_connect (G_OBJECT(account_store), "account_update",
457 G_CALLBACK (on_account_update), self);
459 priv->accounts_reloaded_signal =
460 g_signal_connect (G_OBJECT(account_store), "accounts_reloaded",
461 G_CALLBACK (on_accounts_reloaded), self);
463 if (!update_model (MODEST_FOLDER_VIEW (self),
464 MODEST_TNY_ACCOUNT_STORE (priv->account_store)))
465 g_printerr ("modest: failed to update model\n");
467 g_object_unref (G_OBJECT (device));
471 on_account_update (TnyAccountStore *account_store, const gchar *account,
474 if (!update_model (MODEST_FOLDER_VIEW(user_data),
475 MODEST_TNY_ACCOUNT_STORE(account_store)))
476 g_printerr ("modest: failed to update model for changes in '%s'",
481 on_accounts_reloaded (TnyAccountStore *account_store,
484 update_model (MODEST_FOLDER_VIEW (user_data),
485 MODEST_TNY_ACCOUNT_STORE(account_store));
489 modest_folder_view_set_title (ModestFolderView *self, const gchar *title)
491 GtkTreeViewColumn *col;
493 g_return_if_fail (self);
495 col = gtk_tree_view_get_column (GTK_TREE_VIEW(self), 0);
497 g_printerr ("modest: failed get column for title\n");
501 gtk_tree_view_column_set_title (col, title);
502 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self),
507 modest_folder_view_new (TnyFolderStoreQuery *query)
510 ModestFolderViewPrivate *priv;
511 GtkTreeSelection *sel;
513 self = G_OBJECT (g_object_new (MODEST_TYPE_FOLDER_VIEW, NULL));
514 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
516 priv->query = g_object_ref (query);
518 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
519 priv->changed_signal = g_signal_connect (sel, "changed",
520 G_CALLBACK (on_selection_changed), self);
521 return GTK_WIDGET(self);
524 /* this feels dirty; any other way to expand all the root items? */
526 expand_root_items (ModestFolderView *self)
529 path = gtk_tree_path_new_first ();
531 /* all folders should have child items, so.. */
532 while (gtk_tree_view_expand_row (GTK_TREE_VIEW(self), path, FALSE))
533 gtk_tree_path_next (path);
535 gtk_tree_path_free (path);
539 update_model (ModestFolderView *self, ModestTnyAccountStore *account_store)
541 ModestFolderViewPrivate *priv;
543 TnyList *account_list;
544 GtkTreeModel *model, *sortable;
546 g_return_val_if_fail (account_store, FALSE);
548 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
550 /* Notify that there is no folder selected*/
551 g_signal_emit (G_OBJECT(self),
552 signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
555 /* FIXME: the local accounts are not shown when the query
556 selects only the subscribed folders. */
557 /* model = tny_gtk_folder_store_tree_model_new (TRUE, priv->query); */
558 model = tny_gtk_folder_store_tree_model_new (TRUE, NULL);
559 account_list = TNY_LIST(model);
561 tny_account_store_get_accounts (TNY_ACCOUNT_STORE(account_store),
563 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
565 sortable = gtk_tree_model_sort_new_with_model (model);
566 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(sortable),
567 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
569 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (sortable),
570 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
571 cmp_rows, NULL, NULL);
574 gtk_tree_view_set_model (GTK_TREE_VIEW(self), sortable);
575 expand_root_items (self); /* expand all account folders */
576 g_object_unref (account_list);
584 on_selection_changed (GtkTreeSelection *sel, gpointer user_data)
586 GtkTreeModel *model_sort, *model;
587 TnyFolder *folder = NULL;
588 GtkTreeIter iter, iter_sort;
590 ModestFolderView *tree_view;
591 ModestFolderViewPrivate *priv;
594 g_return_if_fail (sel);
595 g_return_if_fail (user_data);
597 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(user_data);
598 priv->cur_selection = sel;
600 /* folder was _un_selected if true */
601 if (!gtk_tree_selection_get_selected (sel, &model_sort, &iter_sort)) {
602 if (priv->cur_folder)
603 g_object_unref (priv->cur_folder);
605 gtk_tree_row_reference_free (priv->cur_row);
606 priv->cur_folder = NULL;
607 priv->cur_row = NULL;
611 model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
612 gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (model_sort),
616 tree_view = MODEST_FOLDER_VIEW (user_data);
618 gtk_tree_model_get (model, &iter,
619 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
620 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder,
623 /* If the folder is the same or is a root folder do not notify */
624 if ((type == TNY_FOLDER_TYPE_ROOT) || (priv->cur_folder == folder)) {
625 g_object_unref (folder);
629 /* Current folder was unselected */
630 if (priv->cur_folder) {
631 g_signal_emit (G_OBJECT(tree_view), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
632 priv->cur_folder, FALSE);
633 g_object_unref (priv->cur_folder);
637 gtk_tree_row_reference_free (priv->cur_row);
639 /* New current references */
640 path = gtk_tree_model_get_path (model_sort, &iter_sort);
641 priv->cur_folder = folder;
642 priv->cur_row = gtk_tree_row_reference_new (model_sort, path);
645 gtk_tree_path_free (path);
647 /* New folder has been selected */
648 g_signal_emit (G_OBJECT(tree_view),
649 signals[FOLDER_SELECTION_CHANGED_SIGNAL],
654 modest_folder_view_get_selected (ModestFolderView *self)
656 ModestFolderViewPrivate *priv;
658 g_return_val_if_fail (self, NULL);
660 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
661 if (priv->cur_folder)
662 g_object_ref (priv->cur_folder);
664 return priv->cur_folder;
668 cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2,
672 gchar *name1, *name2;
674 TnyFolder *folder1, *folder2;
676 gtk_tree_model_get (tree_model, iter1,
677 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name1,
678 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
679 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder1,
681 gtk_tree_model_get (tree_model, iter2,
682 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name2,
683 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder2,
686 /* local_folders should be the last one */
687 if (type == TNY_FOLDER_TYPE_ROOT) {
688 /* the account name is also the name of the root folder
689 * in case of local folders */
690 if (name1 && strcmp (name1, MODEST_LOCAL_FOLDERS_ACCOUNT_NAME) == 0)
692 else if (name2 && strcmp (name2, MODEST_LOCAL_FOLDERS_ACCOUNT_NAME) == 0)
695 cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
697 cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
701 g_object_unref(G_OBJECT(folder1));
703 g_object_unref(G_OBJECT(folder2));
711 /*****************************************************************************/
712 /* DRAG and DROP stuff */
713 /*****************************************************************************/
716 * This function fills the #GtkSelectionData with the row and the
717 * model that has been dragged. It's called when this widget is a
718 * source for dnd after the event drop happened
721 on_drag_data_get (GtkWidget *widget,
722 GdkDragContext *context,
723 GtkSelectionData *selection_data,
728 GtkTreeSelection *selection;
729 GtkTreeModel *model_sort, *model;
731 GtkTreePath *source_row_sort;
732 GtkTreePath *source_row;
734 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
735 gtk_tree_selection_get_selected (selection, &model_sort, &iter);
736 source_row_sort = gtk_tree_model_get_path (model_sort, &iter);
738 /* Get the unsorted path and model */
739 model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
740 source_row = gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
743 gtk_tree_set_row_drag_data (selection_data,
747 gtk_tree_path_free (source_row_sort);
748 gtk_tree_path_free (source_row);
751 typedef struct _DndHelper {
752 gboolean delete_source;
753 GtkTreePath *source_row;
754 GdkDragContext *context;
760 * This function is the callback of the
761 * modest_mail_operation_xfer_msg() call. We check here if the message
762 * was correctly asynchronously transfered
765 on_progress_changed (ModestMailOperation *mail_op, gpointer user_data)
767 ModestMailOperationQueue *queue;
768 gboolean success = FALSE;
771 helper = (DndHelper *) user_data;
773 if (modest_mail_operation_get_status (mail_op) ==
774 MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
778 error = modest_mail_operation_get_error (mail_op);
779 g_warning ("Error transferring messages: %s\n", error->message);
782 /* Remove the mail operation */
783 queue = modest_runtime_get_mail_operation_queue ();
784 modest_mail_operation_queue_remove (queue, mail_op);
785 g_object_unref (G_OBJECT (mail_op));
787 /* Notify the drag source. Never call delete, the monitor will
788 do the job if needed */
789 gtk_drag_finish (helper->context, success, FALSE, helper->time);
791 /* Free the helper */
792 gtk_tree_path_free (helper->source_row);
793 g_slice_free (DndHelper, helper);
797 * This function is used by drag_data_received_cb to manage drag and
798 * drop of a header, i.e, and drag from the header view to the folder
802 drag_and_drop_from_header_view (GtkTreeModel *source_model,
803 GtkTreeModel *dest_model,
804 GtkTreePath *dest_row,
809 ModestMailOperationQueue *queue;
810 ModestMailOperation *mail_op;
812 GtkTreeIter source_iter, dest_iter;
815 gtk_tree_model_get_iter (source_model, &source_iter, helper->source_row);
816 gtk_tree_model_get (source_model, &source_iter,
817 TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN,
821 gtk_tree_model_get_iter (dest_model, &dest_iter, dest_row);
822 gtk_tree_model_get (dest_model, &dest_iter,
823 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
826 /* Transfer message */
827 queue = modest_runtime_get_mail_operation_queue ();
828 mail_op = modest_mail_operation_new ();
829 started = modest_mail_operation_xfer_msg (mail_op, header,
830 folder, helper->delete_source);
832 g_signal_connect (G_OBJECT (mail_op), "progress_changed",
833 G_CALLBACK (on_progress_changed), helper);
834 modest_mail_operation_queue_add (queue, mail_op);
837 error = modest_mail_operation_get_error (mail_op);
839 g_warning ("Error trying to transfer messages: %s\n",
842 g_slice_free (DndHelper, helper);
846 g_object_unref (G_OBJECT (mail_op));
847 g_object_unref (G_OBJECT (header));
848 g_object_unref (G_OBJECT (folder));
852 * This function is used by drag_data_received_cb to manage drag and
853 * drop of a folder, i.e, and drag from the folder view to the same
857 drag_and_drop_from_folder_view (GtkTreeModel *source_model,
858 GtkTreeModel *dest_model,
859 GtkTreePath *dest_row,
860 GtkSelectionData *selection_data,
863 ModestMailOperation *mail_op;
865 GtkTreeIter parent_iter, iter;
867 TnyFolderStore *parent_folder;
868 gboolean success = FALSE;
870 /* Check if the drag is possible */
871 if (!gtk_tree_path_compare (helper->source_row, dest_row))
874 if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (dest_model),
880 gtk_tree_model_get_iter (source_model, &parent_iter, dest_row);
881 gtk_tree_model_get_iter (source_model, &iter, helper->source_row);
882 gtk_tree_model_get (source_model, &parent_iter,
883 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
885 gtk_tree_model_get (source_model, &iter,
886 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
889 /* Do the mail operation */
890 mail_op = modest_mail_operation_new ();
891 modest_mail_operation_xfer_folder (mail_op, folder, parent_folder,
892 helper->delete_source);
894 g_object_unref (G_OBJECT (parent_folder));
895 g_object_unref (G_OBJECT (folder));
897 error = modest_mail_operation_get_error (mail_op);
899 g_warning ("Error transferring folder: %s\n", error->message);
900 g_object_unref (G_OBJECT (mail_op));
903 g_object_unref (G_OBJECT (mail_op));
906 gtk_drag_finish (helper->context, success, FALSE, helper->time);
908 /* Free the helper */
909 gtk_tree_path_free (helper->source_row);
910 g_slice_free (DndHelper, helper);
914 * This function receives the data set by the "drag-data-get" signal
915 * handler. This information comes within the #GtkSelectionData. This
916 * function will manage both the drags of folders of the treeview and
917 * drags of headers of the header view widget.
920 on_drag_data_received (GtkWidget *widget,
921 GdkDragContext *context,
924 GtkSelectionData *selection_data,
929 GtkWidget *source_widget;
930 GtkTreeModel *model_sort, *dest_model, *source_model;
931 GtkTreePath *source_row, *dest_row, *child_dest_row;
932 GtkTreeViewDropPosition pos;
933 gboolean success = FALSE, delete_source = FALSE;
936 /* Do not allow further process */
937 g_signal_stop_emission_by_name (widget, "drag-data-received");
940 if (context->action == GDK_ACTION_MOVE)
941 delete_source = TRUE;
943 /* Check if the get_data failed */
944 if (selection_data == NULL || selection_data->length < 0)
945 gtk_drag_finish (context, success, FALSE, time);
948 source_widget = gtk_drag_get_source_widget (context);
949 gtk_tree_get_row_drag_data (selection_data,
953 model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
954 /* Select the destination model */
955 if (source_widget == widget) {
956 dest_model = source_model;
958 dest_model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
961 /* Get the path to the destination row. Can not call
962 gtk_tree_view_get_drag_dest_row() because the source row
963 is not selected anymore */
964 gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget), x, y,
967 /* Only allow drops IN other rows */
968 if (!dest_row || pos == GTK_TREE_VIEW_DROP_BEFORE || pos == GTK_TREE_VIEW_DROP_AFTER)
969 gtk_drag_finish (context, success, FALSE, time);
971 /* Create the helper */
972 helper = g_slice_new0 (DndHelper);
973 helper->delete_source = delete_source;
974 helper->source_row = gtk_tree_path_copy (source_row);
975 helper->context = context;
978 /* Get path from the unsorted model */
980 gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
982 gtk_tree_path_free (dest_row);
984 /* Drags from the header view */
985 if (source_widget != widget) {
987 drag_and_drop_from_header_view (source_model,
994 drag_and_drop_from_folder_view (source_model,
1002 gtk_tree_path_free (source_row);
1003 gtk_tree_path_free (child_dest_row);
1007 * We define a "drag-drop" signal handler because we do not want to
1008 * use the default one, because the default one always calls
1009 * gtk_drag_finish and we prefer to do it in the "drag-data-received"
1010 * signal handler, because there we have all the information available
1011 * to know if the dnd was a success or not.
1014 drag_drop_cb (GtkWidget *widget,
1015 GdkDragContext *context,
1023 if (!context->targets)
1026 /* Check if we're dragging a folder row */
1027 target = gtk_drag_dest_find_target (widget, context, NULL);
1029 /* Request the data from the source. */
1030 gtk_drag_get_data(widget, context, target, time);
1036 * This function expands a node of a tree view if it's not expanded
1037 * yet. Not sure why it needs the threads stuff, but gtk+`example code
1038 * does that, so that's why they're here.
1041 expand_row_timeout (gpointer data)
1043 GtkTreeView *tree_view = data;
1044 GtkTreePath *dest_path = NULL;
1045 GtkTreeViewDropPosition pos;
1046 gboolean result = FALSE;
1048 GDK_THREADS_ENTER ();
1050 gtk_tree_view_get_drag_dest_row (tree_view,
1055 (pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER ||
1056 pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)) {
1057 gtk_tree_view_expand_row (tree_view, dest_path, FALSE);
1058 gtk_tree_path_free (dest_path);
1062 gtk_tree_path_free (dest_path);
1067 GDK_THREADS_LEAVE ();
1073 * This function is called whenever the pointer is moved over a widget
1074 * while dragging some data. It installs a timeout that will expand a
1075 * node of the treeview if not expanded yet. This function also calls
1076 * gdk_drag_status in order to set the suggested action that will be
1077 * used by the "drag-data-received" signal handler to know if we
1078 * should do a move or just a copy of the data.
1081 on_drag_motion (GtkWidget *widget,
1082 GdkDragContext *context,
1088 GtkTreeViewDropPosition pos;
1089 GtkTreePath *dest_row;
1090 ModestFolderViewPrivate *priv;
1091 GdkDragAction suggested_action;
1092 gboolean valid_location = FALSE;
1094 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (widget);
1096 if (priv->timer_expander != 0) {
1097 g_source_remove (priv->timer_expander);
1098 priv->timer_expander = 0;
1101 gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
1106 /* Do not allow drops between folders */
1108 pos == GTK_TREE_VIEW_DROP_BEFORE ||
1109 pos == GTK_TREE_VIEW_DROP_AFTER) {
1110 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW (widget), NULL, 0);
1111 gdk_drag_status(context, 0, time);
1112 valid_location = FALSE;
1115 valid_location = TRUE;
1118 /* Expand the selected row after 1/2 second */
1119 if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), dest_row)) {
1120 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), dest_row, pos);
1121 priv->timer_expander = g_timeout_add (500, expand_row_timeout, widget);
1123 gtk_tree_path_free (dest_row);
1125 /* Select the desired action. By default we pick MOVE */
1126 suggested_action = GDK_ACTION_MOVE;
1128 if (context->actions == GDK_ACTION_COPY)
1129 gdk_drag_status(context, GDK_ACTION_COPY, time);
1130 else if (context->actions == GDK_ACTION_MOVE)
1131 gdk_drag_status(context, GDK_ACTION_MOVE, time);
1132 else if (context->actions & suggested_action)
1133 gdk_drag_status(context, suggested_action, time);
1135 gdk_drag_status(context, GDK_ACTION_DEFAULT, time);
1138 g_signal_stop_emission_by_name (widget, "drag-motion");
1139 return valid_location;
1143 /* Folder view drag types */
1144 const GtkTargetEntry folder_view_drag_types[] =
1146 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, MODEST_FOLDER_ROW },
1147 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_APP, MODEST_HEADER_ROW }
1151 * This function sets the treeview as a source and a target for dnd
1152 * events. It also connects all the requirede signals.
1155 setup_drag_and_drop (GtkTreeView *self)
1157 /* Set up the folder view as a dnd destination. Set only the
1158 highlight flag, otherwise gtk will have a different
1160 gtk_drag_dest_set (GTK_WIDGET (self),
1161 GTK_DEST_DEFAULT_HIGHLIGHT,
1162 folder_view_drag_types,
1163 G_N_ELEMENTS (folder_view_drag_types),
1164 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1166 g_signal_connect (G_OBJECT (self),
1167 "drag_data_received",
1168 G_CALLBACK (on_drag_data_received),
1172 /* Set up the treeview as a dnd source */
1173 gtk_drag_source_set (GTK_WIDGET (self),
1175 folder_view_drag_types,
1176 G_N_ELEMENTS (folder_view_drag_types),
1177 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1179 g_signal_connect (G_OBJECT (self),
1181 G_CALLBACK (on_drag_motion),
1184 g_signal_connect (G_OBJECT (self),
1186 G_CALLBACK (on_drag_data_get),
1189 g_signal_connect (G_OBJECT (self),
1191 G_CALLBACK (drag_drop_cb),