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"
51 #include <modest-dnd.h>
53 /* 'private'/'protected' functions */
54 static void modest_folder_view_class_init (ModestFolderViewClass *klass);
55 static void modest_folder_view_init (ModestFolderView *obj);
56 static void modest_folder_view_finalize (GObject *obj);
58 static void tny_account_store_view_init (gpointer g,
61 static void modest_folder_view_set_account_store (TnyAccountStoreView *self,
62 TnyAccountStore *account_store);
64 static gboolean update_model (ModestFolderView *self,
65 ModestTnyAccountStore *account_store);
67 static gboolean update_model_empty (ModestFolderView *self);
69 static void on_selection_changed (GtkTreeSelection *sel, gpointer data);
71 static void on_account_update (TnyAccountStore *account_store,
75 static void on_accounts_reloaded (TnyAccountStore *store,
78 static gint cmp_rows (GtkTreeModel *tree_model,
84 static void on_drag_data_get (GtkWidget *widget,
85 GdkDragContext *context,
86 GtkSelectionData *selection_data,
91 static void on_drag_data_received (GtkWidget *widget,
92 GdkDragContext *context,
95 GtkSelectionData *selection_data,
100 static gboolean on_drag_motion (GtkWidget *widget,
101 GdkDragContext *context,
107 static gint expand_row_timeout (gpointer data);
109 static void setup_drag_and_drop (GtkTreeView *self);
112 FOLDER_SELECTION_CHANGED_SIGNAL,
116 typedef struct _ModestFolderViewPrivate ModestFolderViewPrivate;
117 struct _ModestFolderViewPrivate {
118 TnyAccountStore *account_store;
119 TnyFolder *cur_folder;
120 GtkTreeRowReference *cur_row;
122 gulong account_update_signal;
123 gulong changed_signal;
124 gulong accounts_reloaded_signal;
127 GtkTreeSelection *cur_selection;
128 TnyFolderStoreQuery *query;
129 guint timer_expander;
131 TnyFolderMonitor *monitor;
133 #define MODEST_FOLDER_VIEW_GET_PRIVATE(o) \
134 (G_TYPE_INSTANCE_GET_PRIVATE((o), \
135 MODEST_TYPE_FOLDER_VIEW, \
136 ModestFolderViewPrivate))
138 static GObjectClass *parent_class = NULL;
140 static guint signals[LAST_SIGNAL] = {0};
143 modest_folder_view_get_type (void)
145 static GType my_type = 0;
147 static const GTypeInfo my_info = {
148 sizeof(ModestFolderViewClass),
149 NULL, /* base init */
150 NULL, /* base finalize */
151 (GClassInitFunc) modest_folder_view_class_init,
152 NULL, /* class finalize */
153 NULL, /* class data */
154 sizeof(ModestFolderView),
156 (GInstanceInitFunc) modest_folder_view_init,
160 static const GInterfaceInfo tny_account_store_view_info = {
161 (GInterfaceInitFunc) tny_account_store_view_init, /* interface_init */
162 NULL, /* interface_finalize */
163 NULL /* interface_data */
167 my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
171 g_type_add_interface_static (my_type,
172 TNY_TYPE_ACCOUNT_STORE_VIEW,
173 &tny_account_store_view_info);
179 modest_folder_view_class_init (ModestFolderViewClass *klass)
181 GObjectClass *gobject_class;
182 gobject_class = (GObjectClass*) klass;
184 parent_class = g_type_class_peek_parent (klass);
185 gobject_class->finalize = modest_folder_view_finalize;
187 g_type_class_add_private (gobject_class,
188 sizeof(ModestFolderViewPrivate));
190 signals[FOLDER_SELECTION_CHANGED_SIGNAL] =
191 g_signal_new ("folder_selection_changed",
192 G_TYPE_FROM_CLASS (gobject_class),
194 G_STRUCT_OFFSET (ModestFolderViewClass,
195 folder_selection_changed),
197 modest_marshal_VOID__POINTER_BOOLEAN,
198 G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_BOOLEAN);
204 text_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
205 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
211 TnyFolder *folder = NULL;
213 g_return_if_fail (column);
214 g_return_if_fail (tree_model);
216 gtk_tree_model_get (tree_model, iter,
217 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname,
218 TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN, &unread,
219 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
220 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder,
222 rendobj = G_OBJECT(renderer);
227 if (folder && type != TNY_FOLDER_TYPE_ROOT) { /* FIXME: tnymail bug? crashes with root folders */
228 if (modest_tny_folder_is_local_folder (folder)) {
230 type = modest_tny_folder_get_local_folder_type (folder);
231 if (type != TNY_FOLDER_TYPE_UNKNOWN) {
233 fname = g_strdup(modest_local_folder_info_get_type_display_name (type));
236 } else if (folder && type == TNY_FOLDER_TYPE_ROOT) {
237 if (strcmp (fname, MODEST_LOCAL_FOLDERS_ACCOUNT_NAME) == 0) {/* FIXME: hack */
239 fname = g_strdup (MODEST_LOCAL_FOLDERS_DISPLAY_NAME);
244 gchar *folder_title = g_strdup_printf ("%s (%d)", fname, unread);
245 g_object_set (rendobj,"text", folder_title, "weight", 800, NULL);
246 g_free (folder_title);
248 g_object_set (rendobj,"text", fname, "weight", 400, NULL);
251 if (folder) g_object_unref (G_OBJECT (folder));
256 get_cached_icon (const gchar *name)
261 static GHashTable *icon_cache = NULL;
263 g_return_val_if_fail (name, NULL);
265 if (G_UNLIKELY(!icon_cache))
266 icon_cache = modest_cache_mgr_get_cache (modest_runtime_get_cache_mgr(),
267 MODEST_CACHE_MGR_CACHE_TYPE_PIXBUF);
269 if (!icon_cache || !g_hash_table_lookup_extended (icon_cache, name, &orig_key, &pixbuf)) {
270 pixbuf = (gpointer)gdk_pixbuf_new_from_file (name, &err);
272 g_printerr ("modest: error in icon factory while loading '%s': %s\n",
276 /* if we cannot find it, we still insert (if we have a cache), so we get the error
279 g_hash_table_insert (icon_cache, g_strdup(name),(gpointer)pixbuf);
281 return GDK_PIXBUF(pixbuf);
286 icon_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
287 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
295 rendobj = G_OBJECT(renderer);
296 gtk_tree_model_get (tree_model, iter,
297 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
298 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname,
299 TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN, &unread,
301 rendobj = G_OBJECT(renderer);
303 if (type == TNY_FOLDER_TYPE_NORMAL || type == TNY_FOLDER_TYPE_UNKNOWN) {
304 type = modest_tny_folder_guess_folder_type_from_name (fname);
309 case TNY_FOLDER_TYPE_ROOT:
310 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_ACCOUNT);
312 case TNY_FOLDER_TYPE_INBOX:
313 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_INBOX);
315 case TNY_FOLDER_TYPE_OUTBOX:
316 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_OUTBOX);
318 case TNY_FOLDER_TYPE_JUNK:
319 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_JUNK);
321 case TNY_FOLDER_TYPE_SENT:
322 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_SENT);
324 case TNY_FOLDER_TYPE_TRASH:
325 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_TRASH);
327 case TNY_FOLDER_TYPE_DRAFTS:
328 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_DRAFTS);
330 case TNY_FOLDER_TYPE_NOTES:
331 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_NOTES);
333 case TNY_FOLDER_TYPE_CALENDAR:
334 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_CALENDAR);
336 case TNY_FOLDER_TYPE_CONTACTS:
337 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_CONTACTS);
339 case TNY_FOLDER_TYPE_NORMAL:
341 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_NORMAL);
344 g_object_set (rendobj, "pixbuf", pixbuf, NULL);
348 modest_folder_view_init (ModestFolderView *obj)
350 ModestFolderViewPrivate *priv;
351 GtkTreeViewColumn *column;
352 GtkCellRenderer *renderer;
353 GtkTreeSelection *sel;
355 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
357 priv->timer_expander = 0;
358 priv->account_store = NULL;
359 priv->cur_folder = NULL;
360 priv->cur_row = NULL;
362 priv->monitor = NULL;
364 priv->lock = g_mutex_new ();
366 column = gtk_tree_view_column_new ();
367 gtk_tree_view_append_column (GTK_TREE_VIEW(obj),column);
369 renderer = gtk_cell_renderer_pixbuf_new();
370 gtk_tree_view_column_pack_start (column, renderer, FALSE);
371 gtk_tree_view_column_set_cell_data_func(column, renderer,
372 icon_cell_data, NULL, NULL);
374 renderer = gtk_cell_renderer_text_new();
375 gtk_tree_view_column_pack_start (column, renderer, FALSE);
376 gtk_tree_view_column_set_cell_data_func(column, renderer,
377 text_cell_data, NULL, NULL);
379 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(obj));
380 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
382 gtk_tree_view_column_set_spacing (column, 2);
383 gtk_tree_view_column_set_resizable (column, TRUE);
384 gtk_tree_view_column_set_fixed_width (column, TRUE);
385 gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(obj), FALSE);
386 gtk_tree_view_set_enable_search (GTK_TREE_VIEW(obj), FALSE);
388 setup_drag_and_drop (GTK_TREE_VIEW(obj));
392 tny_account_store_view_init (gpointer g, gpointer iface_data)
394 TnyAccountStoreViewIface *klass = (TnyAccountStoreViewIface *)g;
396 klass->set_account_store_func = modest_folder_view_set_account_store;
402 modest_folder_view_finalize (GObject *obj)
404 ModestFolderViewPrivate *priv;
405 GtkTreeSelection *sel;
407 g_return_if_fail (obj);
409 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
411 if (priv->timer_expander != 0) {
412 g_source_remove (priv->timer_expander);
413 priv->timer_expander = 0;
416 if (priv->account_store) {
417 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
418 priv->account_update_signal);
419 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
420 priv->accounts_reloaded_signal);
421 g_object_unref (G_OBJECT(priv->account_store));
422 priv->account_store = NULL;
426 g_mutex_free (priv->lock);
431 g_object_unref (G_OBJECT (priv->query));
435 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(obj));
437 g_signal_handler_disconnect (G_OBJECT(sel), priv->changed_signal);
439 G_OBJECT_CLASS(parent_class)->finalize (obj);
444 modest_folder_view_set_account_store (TnyAccountStoreView *self, TnyAccountStore *account_store)
446 ModestFolderViewPrivate *priv;
449 g_return_if_fail (MODEST_IS_FOLDER_VIEW (self));
450 g_return_if_fail (TNY_IS_ACCOUNT_STORE (account_store));
452 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
453 device = tny_account_store_get_device (account_store);
455 if (G_UNLIKELY (priv->account_store)) {
457 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store),
458 priv->account_update_signal))
459 g_signal_handler_disconnect (G_OBJECT (priv->account_store),
460 priv->account_update_signal);
461 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store),
462 priv->accounts_reloaded_signal))
463 g_signal_handler_disconnect (G_OBJECT (priv->account_store),
464 priv->accounts_reloaded_signal);
466 g_object_unref (G_OBJECT (priv->account_store));
469 priv->account_store = g_object_ref (G_OBJECT (account_store));
471 priv->account_update_signal =
472 g_signal_connect (G_OBJECT(account_store), "account_update",
473 G_CALLBACK (on_account_update), self);
475 priv->accounts_reloaded_signal =
476 g_signal_connect (G_OBJECT(account_store), "accounts_reloaded",
477 G_CALLBACK (on_accounts_reloaded), self);
479 if (!update_model (MODEST_FOLDER_VIEW (self),
480 MODEST_TNY_ACCOUNT_STORE (priv->account_store)))
481 g_printerr ("modest: failed to update model\n");
483 g_object_unref (G_OBJECT (device));
487 on_account_update (TnyAccountStore *account_store, const gchar *account,
490 if (!update_model (MODEST_FOLDER_VIEW(user_data),
491 MODEST_TNY_ACCOUNT_STORE(account_store)))
492 g_printerr ("modest: failed to update model for changes in '%s'",
497 on_accounts_reloaded (TnyAccountStore *account_store,
500 update_model (MODEST_FOLDER_VIEW (user_data),
501 MODEST_TNY_ACCOUNT_STORE(account_store));
505 modest_folder_view_set_title (ModestFolderView *self, const gchar *title)
507 GtkTreeViewColumn *col;
509 g_return_if_fail (self);
511 col = gtk_tree_view_get_column (GTK_TREE_VIEW(self), 0);
513 g_printerr ("modest: failed get column for title\n");
517 gtk_tree_view_column_set_title (col, title);
518 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self),
523 modest_folder_view_new (TnyFolderStoreQuery *query)
526 ModestFolderViewPrivate *priv;
527 GtkTreeSelection *sel;
529 self = G_OBJECT (g_object_new (MODEST_TYPE_FOLDER_VIEW, NULL));
530 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
532 priv->query = g_object_ref (query);
534 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
535 priv->changed_signal = g_signal_connect (sel, "changed",
536 G_CALLBACK (on_selection_changed), self);
537 return GTK_WIDGET(self);
542 update_model_empty (ModestFolderView *self)
544 ModestFolderViewPrivate *priv;
546 g_return_val_if_fail (self, FALSE);
547 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
549 g_mutex_lock (priv->lock);
552 tny_folder_monitor_stop (priv->monitor);
553 g_object_unref(G_OBJECT(priv->monitor));
554 priv->monitor = NULL;
557 g_mutex_unlock (priv->lock);
559 g_signal_emit (G_OBJECT(self), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
565 /* this feels dirty; any other way to expand all the root items? */
567 expand_root_items (ModestFolderView *self)
570 path = gtk_tree_path_new_first ();
572 /* all folders should have child items, so.. */
573 while (gtk_tree_view_expand_row (GTK_TREE_VIEW(self), path, FALSE))
574 gtk_tree_path_next (path);
576 gtk_tree_path_free (path);
580 update_model (ModestFolderView *self, ModestTnyAccountStore *account_store)
582 ModestFolderViewPrivate *priv;
584 TnyList *account_list;
585 GtkTreeModel *model, *sortable;
587 g_return_val_if_fail (account_store, FALSE);
589 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
591 /* Notify that there is no folder selected */
592 update_model_empty (self);
594 /* FIXME: the local accounts are not shown when the query
595 selects only the subscribed folders. */
596 /* model = tny_gtk_folder_store_tree_model_new (TRUE, priv->query); */
597 model = tny_gtk_folder_store_tree_model_new (TRUE, NULL);
598 account_list = TNY_LIST(model);
600 tny_account_store_get_accounts (TNY_ACCOUNT_STORE(account_store),
602 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
604 sortable = gtk_tree_model_sort_new_with_model (model);
605 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(sortable),
606 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
608 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (sortable),
609 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
610 cmp_rows, NULL, NULL);
613 gtk_tree_view_set_model (GTK_TREE_VIEW(self), sortable);
614 expand_root_items (self); /* expand all account folders */
615 g_object_unref (account_list);
623 on_selection_changed (GtkTreeSelection *sel, gpointer user_data)
625 GtkTreeModel *model_sort, *model;
626 TnyFolder *folder = NULL;
627 GtkTreeIter iter, iter_sort;
629 ModestFolderView *tree_view;
630 ModestFolderViewPrivate *priv;
633 g_return_if_fail (sel);
634 g_return_if_fail (user_data);
636 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(user_data);
637 priv->cur_selection = sel;
639 /* folder was _un_selected if true */
640 if (!gtk_tree_selection_get_selected (sel, &model_sort, &iter_sort)) {
641 priv->cur_folder = NULL; /* FIXME: need this? */
642 gtk_tree_row_reference_free (priv->cur_row);
643 priv->cur_row = NULL;
647 model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
648 gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (model_sort),
652 tree_view = MODEST_FOLDER_VIEW (user_data);
654 gtk_tree_model_get (model, &iter,
655 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
656 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder,
659 if (type == TNY_FOLDER_TYPE_ROOT) {
660 g_object_unref (folder);
664 /* Current folder was unselected */
665 g_signal_emit (G_OBJECT(tree_view), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
666 priv->cur_folder, FALSE);
669 /* tny_folder_sync (priv->cur_folder, TRUE, NULL); /\* FIXME *\/ */
670 gtk_tree_row_reference_free (priv->cur_row);
673 /* New current references */
674 path = gtk_tree_model_get_path (model_sort, &iter_sort);
675 priv->cur_folder = folder;
676 priv->cur_row = gtk_tree_row_reference_new (model_sort, path);
679 gtk_tree_path_free (path);
680 g_object_unref (G_OBJECT (folder));
682 /* New folder has been selected */
683 g_signal_emit (G_OBJECT(tree_view),
684 signals[FOLDER_SELECTION_CHANGED_SIGNAL],
689 modest_folder_view_get_selected (ModestFolderView *self)
691 ModestFolderViewPrivate *priv;
693 g_return_val_if_fail (self, NULL);
695 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
696 if (priv->cur_folder)
697 g_object_ref (priv->cur_folder);
699 return priv->cur_folder;
703 get_model_iter (ModestFolderView *self,
704 GtkTreeModel **model,
707 GtkTreeModel *model_sort;
708 GtkTreeIter iter_sort;
710 ModestFolderViewPrivate *priv;
712 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
714 if (!priv->cur_folder)
717 if (!gtk_tree_row_reference_valid (priv->cur_row))
720 model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
721 *model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
723 /* Get path to retrieve iter */
724 path = gtk_tree_row_reference_get_path (priv->cur_row);
725 if (!gtk_tree_model_get_iter (model_sort, &iter_sort, path))
728 gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (model_sort),
735 modest_folder_view_rename (ModestFolderView *self)
739 ModestFolderViewPrivate *priv;
741 g_return_val_if_fail (MODEST_IS_FOLDER_VIEW (self), FALSE);
743 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
745 if (!get_model_iter (self, &model, &iter))
749 gtk_tree_store_set (GTK_TREE_STORE (model), &iter,
750 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
751 tny_folder_get_name (priv->cur_folder), -1);
753 /* Invalidate selection */
754 g_signal_emit (G_OBJECT(self), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
755 priv->cur_folder, TRUE);
761 modest_folder_view_add_subfolder (ModestFolderView *self, TnyFolder *folder)
764 GtkTreeIter iter, child;
766 g_return_val_if_fail (MODEST_IS_FOLDER_VIEW (self), FALSE);
768 if (!get_model_iter (self, &model, &iter))
771 /* Append a new child to the folder */
772 gtk_tree_store_append (GTK_TREE_STORE (model), &child, &iter);
773 gtk_tree_store_set (GTK_TREE_STORE (model), &child,
774 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
775 tny_folder_get_name (TNY_FOLDER (folder)),
776 TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN,
777 tny_folder_get_unread_count (TNY_FOLDER (folder)),
778 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN,
779 tny_folder_get_folder_type (TNY_FOLDER (folder)),
780 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
787 cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2,
791 gchar *name1, *name2;
793 TnyFolder *folder1, *folder2;
795 gtk_tree_model_get (tree_model, iter1,
796 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name1,
797 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
798 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder1,
800 gtk_tree_model_get (tree_model, iter2,
801 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name2,
802 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder2,
805 /* local_folders should be the last one */
806 if (type == TNY_FOLDER_TYPE_ROOT) {
807 /* the account name is also the name of the root folder
808 * in case of local folders */
809 if (name1 && strcmp (name1, MODEST_LOCAL_FOLDERS_ACCOUNT_NAME) == 0)
811 else if (name2 && strcmp (name2, MODEST_LOCAL_FOLDERS_ACCOUNT_NAME) == 0)
814 cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
816 cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
820 g_object_unref(G_OBJECT(folder1));
822 g_object_unref(G_OBJECT(folder2));
830 /*****************************************************************************/
831 /* DRAG and DROP stuff */
832 /*****************************************************************************/
835 * This function fills the #GtkSelectionData with the row and the
836 * model that has been dragged. It's called when this widget is a
837 * source for dnd after the event drop happened
840 on_drag_data_get (GtkWidget *widget,
841 GdkDragContext *context,
842 GtkSelectionData *selection_data,
847 GtkTreeSelection *selection;
848 GtkTreeModel *model_sort, *model;
850 GtkTreePath *source_row_sort;
851 GtkTreePath *source_row;
853 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
854 gtk_tree_selection_get_selected (selection, &model_sort, &iter);
855 source_row_sort = gtk_tree_model_get_path (model_sort, &iter);
857 /* Get the unsorted path and model */
858 model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
859 source_row = gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
862 gtk_tree_set_row_drag_data (selection_data,
866 gtk_tree_path_free (source_row_sort);
867 gtk_tree_path_free (source_row);
870 typedef struct _DndHelper
872 gboolean delete_source;
873 GtkWidget *source_widget;
874 GtkWidget *dest_widget;
875 GtkTreePath *source_row;
876 GdkDragContext *context;
882 * This function saves the source row in the source widget, will be
883 * used by the drag-data-delete handler to remove the source row
886 save_and_clean (DndHelper *helper,
890 if (success && helper->delete_source)
891 g_object_set_data (G_OBJECT (helper->source_widget),
893 gtk_tree_path_copy (helper->source_row));
896 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (helper->dest_widget),
898 GTK_TREE_VIEW_DROP_BEFORE);
903 * This function is the callback of the
904 * modest_mail_operation_xfer_msg() call. We check here if the message
905 * was correctly asynchronously transfered
908 on_progress_changed (ModestMailOperation *mail_op, gpointer user_data)
910 ModestMailOperationQueue *queue;
911 gboolean success = FALSE;
914 helper = (DndHelper *) user_data;
916 if (modest_mail_operation_get_status (mail_op) ==
917 MODEST_MAIL_OPERATION_STATUS_SUCCESS)
920 /* Remove the mail operation */
921 queue = modest_runtime_get_mail_operation_queue ();
922 modest_mail_operation_queue_remove (queue, mail_op);
923 g_object_unref (G_OBJECT (mail_op));
926 save_and_clean (helper, success);
928 /* Notify the drag source */
929 gtk_drag_finish (helper->context, success, (success && helper->delete_source), helper->time);
931 /* Free the helper */
932 g_slice_free (DndHelper, helper);
936 * This function is used by drag_data_received_cb to manage drag and
937 * drop of a header, i.e, and drag from the header view to the folder
941 drag_and_drop_from_header_view (GtkTreeModel *source_model,
942 GtkTreeModel *dest_model,
943 GtkTreePath *dest_row,
948 ModestMailOperationQueue *queue;
949 ModestMailOperation *mail_op;
951 GtkTreeIter source_iter, dest_iter;
954 gtk_tree_model_get_iter (source_model, &source_iter, helper->source_row);
955 gtk_tree_model_get (source_model, &source_iter,
956 TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN,
960 gtk_tree_model_get_iter (dest_model, &dest_iter, dest_row);
961 gtk_tree_model_get (dest_model, &dest_iter,
962 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
965 /* Transfer message */
966 queue = modest_runtime_get_mail_operation_queue ();
967 mail_op = modest_mail_operation_new ();
968 started = modest_mail_operation_xfer_msg (mail_op, header,
969 folder, helper->delete_source);
971 g_signal_connect (G_OBJECT (mail_op), "progress_changed",
972 G_CALLBACK (on_progress_changed), helper);
973 modest_mail_operation_queue_add (queue, mail_op);
976 error = modest_mail_operation_get_error (mail_op);
977 g_warning ("Error trying to transfer messages: %s\n",
980 g_slice_free (DndHelper, helper);
984 g_object_unref (G_OBJECT (mail_op));
985 g_object_unref (G_OBJECT (header));
986 g_object_unref (G_OBJECT (folder));
990 * This function is used by drag_data_received_cb to manage drag and
991 * drop of a folder, i.e, and drag from the folder view to the same
995 drag_and_drop_from_folder_view (GtkTreeModel *source_model,
996 GtkTreeModel *dest_model,
997 GtkTreePath *dest_row,
998 GtkSelectionData *selection_data,
1001 ModestMailOperation *mail_op;
1002 const GError *error;
1003 GtkTreeRowReference *source_row_reference;
1004 GtkTreeIter parent_iter, iter;
1005 TnyFolder *folder, *new_folder;
1006 TnyFolderStore *parent_folder;
1007 gboolean success = FALSE;
1009 /* Check if the drag is possible */
1010 if (!gtk_tree_path_compare (helper->source_row, dest_row))
1013 if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (dest_model),
1019 gtk_tree_model_get_iter (source_model, &parent_iter, dest_row);
1020 gtk_tree_model_get_iter (source_model, &iter, helper->source_row);
1021 gtk_tree_model_get (source_model, &parent_iter,
1022 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
1023 &parent_folder, -1);
1024 gtk_tree_model_get (source_model, &iter,
1025 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
1028 /* Do the mail operation */
1029 mail_op = modest_mail_operation_new ();
1030 new_folder = modest_mail_operation_xfer_folder (mail_op, folder, parent_folder,
1031 helper->delete_source);
1033 g_object_unref (G_OBJECT (parent_folder));
1034 g_object_unref (G_OBJECT (folder));
1036 error = modest_mail_operation_get_error (mail_op);
1038 g_warning ("Error transferring folder: %s\n", error->message);
1039 g_object_unref (G_OBJECT (mail_op));
1042 g_object_unref (G_OBJECT (mail_op));
1044 /* Get a row reference to the source path because the path
1045 could change after the insertion. The gtk_drag_finish() is
1046 not able to delete the source because that, so we have to
1048 source_row_reference = gtk_tree_row_reference_new (source_model, helper->source_row);
1049 gtk_tree_path_free (helper->source_row);
1051 /* Insert the dragged row as a child of the dest row */
1052 gtk_tree_path_down (dest_row);
1053 if (gtk_tree_drag_dest_drag_data_received (GTK_TREE_DRAG_DEST (dest_model),
1059 /* Set the newly created folder as the instance in the row */
1060 gtk_tree_model_get_iter (dest_model, &iter, dest_row);
1061 gtk_tree_store_set (GTK_TREE_STORE (dest_model), &iter,
1062 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
1064 g_object_unref (G_OBJECT (new_folder));
1066 helper->source_row = gtk_tree_row_reference_get_path (source_row_reference);
1070 gtk_tree_row_reference_free (source_row_reference);
1072 /* Save and clean */
1073 save_and_clean (helper, success);
1076 gtk_drag_finish (helper->context, success, (success && helper->delete_source), helper->time);
1080 * This function receives the data set by the "drag-data-get" signal
1081 * handler. This information comes within the #GtkSelectionData. This
1082 * function will manage both the drags of folders of the treeview and
1083 * drags of headers of the header view widget.
1086 on_drag_data_received (GtkWidget *widget,
1087 GdkDragContext *context,
1090 GtkSelectionData *selection_data,
1095 GtkWidget *source_widget;
1096 GtkTreeModel *model_sort, *dest_model, *source_model;
1097 GtkTreePath *source_row, *dest_row;
1098 GtkTreeViewDropPosition pos;
1099 gboolean success = FALSE, delete_source = FALSE;
1102 /* Do not allow further process */
1103 g_signal_stop_emission_by_name (widget, "drag-data-received");
1105 /* Get the action */
1106 if (context->action == GDK_ACTION_MOVE)
1107 delete_source = TRUE;
1109 /* Check if the get_data failed */
1110 if (selection_data == NULL || selection_data->length < 0)
1111 gtk_drag_finish (context, success, (success && delete_source), time);
1113 /* Get the models */
1114 source_widget = gtk_drag_get_source_widget (context);
1115 model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
1116 gtk_tree_get_row_drag_data (selection_data,
1120 /* Select the destination model */
1121 if (source_widget == widget)
1122 dest_model = source_model;
1124 dest_model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
1127 /* Get the path to the destination row. Can not call
1128 gtk_tree_view_get_drag_dest_row() because the source row
1129 is not selected anymore */
1130 gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget), x, y,
1133 /* Only allow drops IN other rows */
1134 if (!dest_row || pos == GTK_TREE_VIEW_DROP_BEFORE || pos == GTK_TREE_VIEW_DROP_AFTER)
1135 gtk_drag_finish (context, success, (success && delete_source), time);
1137 /* Create the helper */
1138 helper = g_slice_new0 (DndHelper);
1139 /* helper->selection_data = selection_data; */
1140 helper->delete_source = delete_source;
1141 helper->source_widget = source_widget;
1142 helper->dest_widget = widget;
1143 helper->source_row = source_row;
1144 helper->context = context;
1145 helper->time = time;
1147 /* Drags from the header view */
1148 if ((target_type == FOLDER_ROW) && (source_widget != widget)) {
1150 drag_and_drop_from_header_view (source_model,
1155 gtk_tree_path_free (dest_row);
1158 GtkTreePath *child_dest_row;
1160 /* Get path from the unsorted model */
1162 gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
1164 gtk_tree_path_free (dest_row);
1166 drag_and_drop_from_folder_view (source_model,
1171 gtk_tree_path_free (child_dest_row);
1176 * We define a "drag-drop" signal handler because we do not want to
1177 * use the default one, because the default one always calls
1178 * gtk_drag_finish and we prefer to do it in the "drag-data-received"
1179 * signal handler, because there we have all the information available
1180 * to know if the dnd was a success or not.
1183 drag_drop_cb (GtkWidget *widget,
1184 GdkDragContext *context,
1190 if (!context->targets)
1193 /* Request the data from the source. */
1194 gtk_drag_get_data(widget,
1196 GDK_POINTER_TO_ATOM (context->targets->data),
1203 * This function deletes the data that has been dragged from its
1204 * source widget. Since is a function received by the source of the
1205 * drag, this function only deletes rows of the folder view
1206 * widget. The header view widget will need to define its own one.
1209 drag_data_delete_cb (GtkWidget *widget,
1210 GdkDragContext *context,
1213 GtkTreePath *source_row;
1214 GtkTreeModel *model_sort, *model;
1216 model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
1217 model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
1218 source_row = g_object_steal_data (G_OBJECT (widget), ROW_REF_DATA_NAME);
1220 /* Delete the source row */
1221 gtk_tree_drag_source_drag_data_delete (GTK_TREE_DRAG_SOURCE (model),
1224 gtk_tree_path_free (source_row);
1228 * This function expands a node of a tree view if it's not expanded
1229 * yet. Not sure why it needs the threads stuff, but gtk+`example code
1230 * does that, so that's why they're here.
1233 expand_row_timeout (gpointer data)
1235 GtkTreeView *tree_view = data;
1236 GtkTreePath *dest_path = NULL;
1237 GtkTreeViewDropPosition pos;
1238 gboolean result = FALSE;
1240 GDK_THREADS_ENTER ();
1242 gtk_tree_view_get_drag_dest_row (tree_view,
1247 (pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER ||
1248 pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)) {
1249 gtk_tree_view_expand_row (tree_view, dest_path, FALSE);
1250 gtk_tree_path_free (dest_path);
1254 gtk_tree_path_free (dest_path);
1259 GDK_THREADS_LEAVE ();
1265 * This function is called whenever the pointer is moved over a widget
1266 * while dragging some data. It installs a timeout that will expand a
1267 * node of the treeview if not expanded yet. This function also calls
1268 * gdk_drag_status in order to set the suggested action that will be
1269 * used by the "drag-data-received" signal handler to know if we
1270 * should do a move or just a copy of the data.
1273 on_drag_motion (GtkWidget *widget,
1274 GdkDragContext *context,
1280 GtkTreeViewDropPosition pos;
1281 GtkTreePath *dest_row;
1282 ModestFolderViewPrivate *priv;
1283 GdkDragAction suggested_action;
1285 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (widget);
1287 if (priv->timer_expander != 0) {
1288 g_source_remove (priv->timer_expander);
1289 priv->timer_expander = 0;
1292 gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
1300 /* Expand the selected row after 1/2 second */
1301 if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), dest_row)) {
1302 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), dest_row, pos);
1303 priv->timer_expander = g_timeout_add (500, expand_row_timeout, widget);
1305 gtk_tree_path_free (dest_row);
1307 /* Select the desired action. By default we pick MOVE */
1308 suggested_action = GDK_ACTION_MOVE;
1310 if (context->actions == GDK_ACTION_COPY)
1311 gdk_drag_status(context, GDK_ACTION_COPY, time);
1312 else if (context->actions == GDK_ACTION_MOVE)
1313 gdk_drag_status(context, GDK_ACTION_MOVE, time);
1314 else if (context->actions & suggested_action)
1315 gdk_drag_status(context, suggested_action, time);
1317 gdk_drag_status(context, GDK_ACTION_DEFAULT, time);
1323 /* Folder view drag types */
1324 const GtkTargetEntry folder_view_drag_types[] =
1326 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, FOLDER_ROW },
1330 * This function sets the treeview as a source and a target for dnd
1331 * events. It also connects all the requirede signals.
1334 setup_drag_and_drop (GtkTreeView *self)
1336 /* Set up the folder view as a dnd destination. Set only the
1337 highlight flag, otherwise gtk will have a different
1339 gtk_drag_dest_set (GTK_WIDGET (self),
1340 GTK_DEST_DEFAULT_HIGHLIGHT,
1341 folder_view_drag_types,
1342 G_N_ELEMENTS (folder_view_drag_types),
1343 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1345 gtk_signal_connect(GTK_OBJECT (self),
1346 "drag_data_received",
1347 GTK_SIGNAL_FUNC(on_drag_data_received),
1351 /* Set up the treeview as a dnd source */
1352 gtk_drag_source_set (GTK_WIDGET (self),
1354 folder_view_drag_types,
1355 G_N_ELEMENTS (folder_view_drag_types),
1356 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1358 gtk_signal_connect(GTK_OBJECT (self),
1360 GTK_SIGNAL_FUNC(drag_data_delete_cb),
1363 gtk_signal_connect(GTK_OBJECT (self),
1365 GTK_SIGNAL_FUNC(on_drag_motion),
1369 gtk_signal_connect(GTK_OBJECT (self),
1371 GTK_SIGNAL_FUNC(on_drag_data_get),
1374 gtk_signal_connect(GTK_OBJECT (self),
1376 GTK_SIGNAL_FUNC(drag_drop_cb),