1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <glib/gi18n.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <tny-account-store-view.h>
34 #include <tny-gtk-account-list-model.h>
35 #include <tny-gtk-folder-store-tree-model.h>
36 #include <tny-gtk-header-list-model.h>
37 #include <tny-folder.h>
38 #include <tny-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>
51 #include <modest-platform.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 void on_selection_changed (GtkTreeSelection *sel, gpointer data);
69 static void on_account_update (TnyAccountStore *account_store,
73 static void on_accounts_reloaded (TnyAccountStore *store,
76 static gint cmp_rows (GtkTreeModel *tree_model,
82 static void on_drag_data_get (GtkWidget *widget,
83 GdkDragContext *context,
84 GtkSelectionData *selection_data,
89 static void on_drag_data_received (GtkWidget *widget,
90 GdkDragContext *context,
93 GtkSelectionData *selection_data,
98 static gboolean on_drag_motion (GtkWidget *widget,
99 GdkDragContext *context,
105 static gint expand_row_timeout (gpointer data);
107 static void setup_drag_and_drop (GtkTreeView *self);
109 static gboolean on_key_pressed (GtkWidget *self,
113 static void on_configuration_key_changed (ModestConf* conf,
115 ModestConfEvent event,
116 ModestFolderView *self);
119 FOLDER_SELECTION_CHANGED_SIGNAL,
120 FOLDER_DISPLAY_NAME_CHANGED_SIGNAL,
124 typedef struct _ModestFolderViewPrivate ModestFolderViewPrivate;
125 struct _ModestFolderViewPrivate {
126 TnyAccountStore *account_store;
127 TnyFolderStore *cur_folder_store;
129 gulong account_update_signal;
130 gulong changed_signal;
131 gulong accounts_reloaded_signal;
133 GtkTreeSelection *cur_selection;
134 TnyFolderStoreQuery *query;
135 guint timer_expander;
137 gchar *local_account_name;
139 #define MODEST_FOLDER_VIEW_GET_PRIVATE(o) \
140 (G_TYPE_INSTANCE_GET_PRIVATE((o), \
141 MODEST_TYPE_FOLDER_VIEW, \
142 ModestFolderViewPrivate))
144 static GObjectClass *parent_class = NULL;
146 static guint signals[LAST_SIGNAL] = {0};
149 modest_folder_view_get_type (void)
151 static GType my_type = 0;
153 static const GTypeInfo my_info = {
154 sizeof(ModestFolderViewClass),
155 NULL, /* base init */
156 NULL, /* base finalize */
157 (GClassInitFunc) modest_folder_view_class_init,
158 NULL, /* class finalize */
159 NULL, /* class data */
160 sizeof(ModestFolderView),
162 (GInstanceInitFunc) modest_folder_view_init,
166 static const GInterfaceInfo tny_account_store_view_info = {
167 (GInterfaceInitFunc) tny_account_store_view_init, /* interface_init */
168 NULL, /* interface_finalize */
169 NULL /* interface_data */
173 my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
177 g_type_add_interface_static (my_type,
178 TNY_TYPE_ACCOUNT_STORE_VIEW,
179 &tny_account_store_view_info);
185 modest_folder_view_class_init (ModestFolderViewClass *klass)
187 GObjectClass *gobject_class;
188 gobject_class = (GObjectClass*) klass;
190 parent_class = g_type_class_peek_parent (klass);
191 gobject_class->finalize = modest_folder_view_finalize;
193 g_type_class_add_private (gobject_class,
194 sizeof(ModestFolderViewPrivate));
196 signals[FOLDER_SELECTION_CHANGED_SIGNAL] =
197 g_signal_new ("folder_selection_changed",
198 G_TYPE_FROM_CLASS (gobject_class),
200 G_STRUCT_OFFSET (ModestFolderViewClass,
201 folder_selection_changed),
203 modest_marshal_VOID__POINTER_BOOLEAN,
204 G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_BOOLEAN);
207 * This signal is emitted whenever the currently selected
208 * folder display name is computed. Note that the name could
209 * be different to the folder name, because we could append
210 * the unread messages count to the folder name to build the
211 * folder display name
213 signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL] =
214 g_signal_new ("folder-display-name-changed",
215 G_TYPE_FROM_CLASS (gobject_class),
217 G_STRUCT_OFFSET (ModestFolderViewClass,
218 folder_display_name_changed),
220 g_cclosure_marshal_VOID__STRING,
221 G_TYPE_NONE, 1, G_TYPE_STRING);
227 text_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
228 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
230 ModestFolderViewPrivate *priv;
235 GObject *instance = NULL;
237 g_return_if_fail (column);
238 g_return_if_fail (tree_model);
240 gtk_tree_model_get (tree_model, iter,
241 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname,
242 TNY_GTK_FOLDER_STORE_TREE_MODEL_ALL_COLUMN, &all,
243 TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN, &unread,
244 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
245 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
247 rendobj = G_OBJECT(renderer);
258 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (data);
260 if (type != TNY_FOLDER_TYPE_ROOT) {
263 if (modest_tny_folder_is_local_folder (TNY_FOLDER (instance))) {
265 type = modest_tny_folder_get_local_folder_type (TNY_FOLDER (instance));
266 if (type != TNY_FOLDER_TYPE_UNKNOWN) {
268 fname = g_strdup(modest_local_folder_info_get_type_display_name (type));
272 /* Select the number to show */
273 if ((type == TNY_FOLDER_TYPE_DRAFTS) || (type == TNY_FOLDER_TYPE_OUTBOX))
278 /* Use bold font style if there are unread messages */
280 gchar *folder_title = g_strdup_printf ("%s (%d)", fname, unread);
281 g_object_set (rendobj,"text", folder_title, "weight", 800, NULL);
282 if (G_OBJECT (priv->cur_folder_store) == instance)
283 g_signal_emit (G_OBJECT(data),
284 signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
286 g_free (folder_title);
288 g_object_set (rendobj,"text", fname, "weight", 400, NULL);
289 if (G_OBJECT (priv->cur_folder_store) == instance)
290 g_signal_emit (G_OBJECT(data),
291 signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
296 const gchar *account_name = NULL;
297 const gchar *account_id = NULL;
299 /* If it's a server account */
300 account_id = tny_account_get_id (TNY_ACCOUNT (instance));
301 if (!strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID)) {
302 account_name = priv->local_account_name;
304 if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID)) {
305 /* TODO: get MMC card name */
307 account_name = fname;
311 /* Notify display name observers */
312 if (G_OBJECT (priv->cur_folder_store) == instance)
313 g_signal_emit (G_OBJECT(data),
314 signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
317 /* Use bold font style */
318 g_object_set (rendobj,"text", account_name, "weight", 800, NULL);
321 g_object_unref (G_OBJECT (instance));
328 icon_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
329 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
331 GObject *rendobj, *instance;
335 const gchar *account_id = NULL;
338 rendobj = G_OBJECT(renderer);
339 gtk_tree_model_get (tree_model, iter,
340 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
341 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname,
342 TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN, &unread,
343 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
354 if (type == TNY_FOLDER_TYPE_NORMAL || type == TNY_FOLDER_TYPE_UNKNOWN) {
355 type = modest_tny_folder_guess_folder_type_from_name (fname);
359 case TNY_FOLDER_TYPE_ROOT:
360 account_id = tny_account_get_id (TNY_ACCOUNT (instance));
361 if (!strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID)) {
362 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_LOCAL_FOLDERS);
364 if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID))
365 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_MMC);
367 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_ACCOUNT);
370 case TNY_FOLDER_TYPE_INBOX:
371 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_INBOX);
373 case TNY_FOLDER_TYPE_OUTBOX:
374 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_OUTBOX);
376 case TNY_FOLDER_TYPE_JUNK:
377 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_JUNK);
379 case TNY_FOLDER_TYPE_SENT:
380 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_SENT);
382 case TNY_FOLDER_TYPE_TRASH:
383 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_TRASH);
385 case TNY_FOLDER_TYPE_DRAFTS:
386 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_DRAFTS);
388 case TNY_FOLDER_TYPE_NORMAL:
390 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_NORMAL);
393 g_object_unref (G_OBJECT (instance));
397 g_object_set (rendobj, "pixbuf", pixbuf, NULL);
401 modest_folder_view_init (ModestFolderView *obj)
403 ModestFolderViewPrivate *priv;
404 GtkTreeViewColumn *column;
405 GtkCellRenderer *renderer;
406 GtkTreeSelection *sel;
409 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
411 priv->timer_expander = 0;
412 priv->account_store = NULL;
413 priv->cur_folder_store = NULL;
416 /* Initialize the local account name */
417 conf = modest_runtime_get_conf();
418 priv->local_account_name = modest_conf_get_string (conf, MODEST_CONF_DEVICE_NAME, NULL);
421 column = gtk_tree_view_column_new ();
423 renderer = gtk_cell_renderer_pixbuf_new();
424 gtk_tree_view_column_pack_start (column, renderer, FALSE);
425 gtk_tree_view_column_set_cell_data_func(column, renderer,
426 icon_cell_data, obj, NULL);
428 renderer = gtk_cell_renderer_text_new();
429 gtk_tree_view_column_pack_start (column, renderer, FALSE);
430 gtk_tree_view_column_set_cell_data_func(column, renderer,
431 text_cell_data, obj, NULL);
433 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(obj));
434 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
436 gtk_tree_view_column_set_spacing (column, 2);
437 gtk_tree_view_column_set_resizable (column, TRUE);
438 gtk_tree_view_column_set_fixed_width (column, TRUE);
439 gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(obj), FALSE);
440 gtk_tree_view_set_enable_search (GTK_TREE_VIEW(obj), FALSE);
442 gtk_tree_view_append_column (GTK_TREE_VIEW(obj),column);
444 /* Setup drag and drop */
445 setup_drag_and_drop (GTK_TREE_VIEW(obj));
447 /* Connect signals */
448 g_signal_connect (G_OBJECT (obj),
450 G_CALLBACK (on_key_pressed), NULL);
453 * Track changes in the local account name (in the device it
454 * will be the device name)
456 g_signal_connect (G_OBJECT(conf),
458 G_CALLBACK(on_configuration_key_changed), obj);
463 tny_account_store_view_init (gpointer g, gpointer iface_data)
465 TnyAccountStoreViewIface *klass = (TnyAccountStoreViewIface *)g;
467 klass->set_account_store_func = modest_folder_view_set_account_store;
473 modest_folder_view_finalize (GObject *obj)
475 ModestFolderViewPrivate *priv;
476 GtkTreeSelection *sel;
478 g_return_if_fail (obj);
480 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
482 if (priv->timer_expander != 0) {
483 g_source_remove (priv->timer_expander);
484 priv->timer_expander = 0;
487 if (priv->account_store) {
488 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
489 priv->account_update_signal);
490 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
491 priv->accounts_reloaded_signal);
492 g_object_unref (G_OBJECT(priv->account_store));
493 priv->account_store = NULL;
497 g_object_unref (G_OBJECT (priv->query));
501 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(obj));
503 g_signal_handler_disconnect (G_OBJECT(sel), priv->changed_signal);
505 g_free (priv->local_account_name);
507 G_OBJECT_CLASS(parent_class)->finalize (obj);
512 modest_folder_view_set_account_store (TnyAccountStoreView *self, TnyAccountStore *account_store)
514 ModestFolderViewPrivate *priv;
517 g_return_if_fail (MODEST_IS_FOLDER_VIEW (self));
518 g_return_if_fail (TNY_IS_ACCOUNT_STORE (account_store));
520 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
521 device = tny_account_store_get_device (account_store);
523 if (G_UNLIKELY (priv->account_store)) {
525 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store),
526 priv->account_update_signal))
527 g_signal_handler_disconnect (G_OBJECT (priv->account_store),
528 priv->account_update_signal);
529 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store),
530 priv->accounts_reloaded_signal))
531 g_signal_handler_disconnect (G_OBJECT (priv->account_store),
532 priv->accounts_reloaded_signal);
534 g_object_unref (G_OBJECT (priv->account_store));
537 priv->account_store = g_object_ref (G_OBJECT (account_store));
539 priv->account_update_signal =
540 g_signal_connect (G_OBJECT(account_store), "account_update",
541 G_CALLBACK (on_account_update), self);
543 priv->accounts_reloaded_signal =
544 g_signal_connect (G_OBJECT(account_store), "accounts_reloaded",
545 G_CALLBACK (on_accounts_reloaded), self);
547 if (!update_model (MODEST_FOLDER_VIEW (self),
548 MODEST_TNY_ACCOUNT_STORE (priv->account_store)))
549 g_printerr ("modest: failed to update model\n");
551 g_object_unref (G_OBJECT (device));
555 on_account_update (TnyAccountStore *account_store, const gchar *account,
558 if (!update_model (MODEST_FOLDER_VIEW(user_data),
559 MODEST_TNY_ACCOUNT_STORE(account_store)))
560 g_printerr ("modest: failed to update model for changes in '%s'",
565 on_accounts_reloaded (TnyAccountStore *account_store,
568 update_model (MODEST_FOLDER_VIEW (user_data),
569 MODEST_TNY_ACCOUNT_STORE(account_store));
573 modest_folder_view_set_title (ModestFolderView *self, const gchar *title)
575 GtkTreeViewColumn *col;
577 g_return_if_fail (self);
579 col = gtk_tree_view_get_column (GTK_TREE_VIEW(self), 0);
581 g_printerr ("modest: failed get column for title\n");
585 gtk_tree_view_column_set_title (col, title);
586 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self),
591 modest_folder_view_new (TnyFolderStoreQuery *query)
594 ModestFolderViewPrivate *priv;
595 GtkTreeSelection *sel;
597 self = G_OBJECT (g_object_new (MODEST_TYPE_FOLDER_VIEW, NULL));
598 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
600 priv->query = g_object_ref (query);
602 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
603 priv->changed_signal = g_signal_connect (sel, "changed",
604 G_CALLBACK (on_selection_changed), self);
605 return GTK_WIDGET(self);
608 /* this feels dirty; any other way to expand all the root items? */
610 expand_root_items (ModestFolderView *self)
613 path = gtk_tree_path_new_first ();
615 /* all folders should have child items, so.. */
616 while (gtk_tree_view_expand_row (GTK_TREE_VIEW(self), path, FALSE))
617 gtk_tree_path_next (path);
619 gtk_tree_path_free (path);
623 update_model (ModestFolderView *self, ModestTnyAccountStore *account_store)
625 ModestFolderViewPrivate *priv;
627 TnyList *account_list;
628 GtkTreeModel *model, *sortable;
630 g_return_val_if_fail (account_store, FALSE);
632 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
634 /* Notify that there is no folder selected */
635 g_signal_emit (G_OBJECT(self),
636 signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
639 /* FIXME: the local accounts are not shown when the query
640 selects only the subscribed folders. */
641 /* model = tny_gtk_folder_store_tree_model_new (TRUE, priv->query); */
642 model = tny_gtk_folder_store_tree_model_new (TRUE, NULL);
643 account_list = TNY_LIST(model);
645 tny_account_store_get_accounts (TNY_ACCOUNT_STORE(account_store),
647 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
649 sortable = gtk_tree_model_sort_new_with_model (model);
650 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(sortable),
651 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
653 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (sortable),
654 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
655 cmp_rows, NULL, NULL);
658 gtk_tree_view_set_model (GTK_TREE_VIEW(self), sortable);
659 expand_root_items (self); /* expand all account folders */
660 g_object_unref (account_list);
668 on_selection_changed (GtkTreeSelection *sel, gpointer user_data)
670 GtkTreeModel *model_sort, *model;
671 TnyFolderStore *folder = NULL;
672 GtkTreeIter iter, iter_sort;
673 ModestFolderView *tree_view;
674 ModestFolderViewPrivate *priv;
677 g_return_if_fail (sel);
678 g_return_if_fail (user_data);
680 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(user_data);
681 priv->cur_selection = sel;
683 /* folder was _un_selected if true */
684 if (!gtk_tree_selection_get_selected (sel, &model_sort, &iter_sort)) {
685 if (priv->cur_folder_store)
686 g_object_unref (priv->cur_folder_store);
687 priv->cur_folder_store = NULL;
689 /* Notify the display name observers */
690 g_signal_emit (G_OBJECT(user_data),
691 signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
696 model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
697 gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (model_sort),
701 tree_view = MODEST_FOLDER_VIEW (user_data);
703 gtk_tree_model_get (model, &iter,
704 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
705 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder,
708 /* If the folder is the same do not notify */
709 if (priv->cur_folder_store == folder) {
710 g_object_unref (folder);
714 /* Current folder was unselected */
715 if (priv->cur_folder_store) {
716 g_signal_emit (G_OBJECT(tree_view), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
717 priv->cur_folder_store, FALSE);
718 g_object_unref (priv->cur_folder_store);
721 /* New current references */
722 priv->cur_folder_store = folder;
724 /* New folder has been selected */
725 g_signal_emit (G_OBJECT(tree_view),
726 signals[FOLDER_SELECTION_CHANGED_SIGNAL],
731 modest_folder_view_get_selected (ModestFolderView *self)
733 ModestFolderViewPrivate *priv;
735 g_return_val_if_fail (self, NULL);
737 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
738 if (priv->cur_folder_store)
739 g_object_ref (priv->cur_folder_store);
741 return priv->cur_folder_store;
745 cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2,
749 gchar *name1, *name2;
751 TnyFolder *folder1, *folder2;
753 gtk_tree_model_get (tree_model, iter1,
754 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name1,
755 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
756 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder1,
758 gtk_tree_model_get (tree_model, iter2,
759 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name2,
760 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder2,
763 /* local_folders should be the last one */
764 if (type == TNY_FOLDER_TYPE_ROOT) {
765 /* the account name is also the name of the root folder
766 * in case of local folders */
767 if (name1 && strcmp (name1, MODEST_LOCAL_FOLDERS_ACCOUNT_NAME) == 0)
769 else if (name2 && strcmp (name2, MODEST_LOCAL_FOLDERS_ACCOUNT_NAME) == 0)
772 cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
774 cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
778 g_object_unref(G_OBJECT(folder1));
780 g_object_unref(G_OBJECT(folder2));
788 /*****************************************************************************/
789 /* DRAG and DROP stuff */
790 /*****************************************************************************/
793 * This function fills the #GtkSelectionData with the row and the
794 * model that has been dragged. It's called when this widget is a
795 * source for dnd after the event drop happened
798 on_drag_data_get (GtkWidget *widget,
799 GdkDragContext *context,
800 GtkSelectionData *selection_data,
805 GtkTreeSelection *selection;
806 GtkTreeModel *model_sort, *model;
808 GtkTreePath *source_row_sort;
809 GtkTreePath *source_row;
811 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
812 gtk_tree_selection_get_selected (selection, &model_sort, &iter);
813 source_row_sort = gtk_tree_model_get_path (model_sort, &iter);
815 /* Get the unsorted path and model */
816 model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
817 source_row = gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
820 gtk_tree_set_row_drag_data (selection_data,
824 gtk_tree_path_free (source_row_sort);
825 gtk_tree_path_free (source_row);
828 typedef struct _DndHelper {
829 gboolean delete_source;
830 GtkTreePath *source_row;
831 GdkDragContext *context;
837 * This function is the callback of the
838 * modest_mail_operation_xfer_msg() and
839 * modest_mail_operation_xfer_folder() calls. We check here if the
840 * message/folder was correctly asynchronously transferred. The reason
841 * to use the same callback is that the code is the same, it only has
842 * to check that the operation went fine and then finalize the drag
846 on_progress_changed (ModestMailOperation *mail_op, gpointer user_data)
851 helper = (DndHelper *) user_data;
853 if (!modest_mail_operation_is_finished (mail_op))
856 if (modest_mail_operation_get_status (mail_op) ==
857 MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
863 /* Notify the drag source. Never call delete, the monitor will
864 do the job if needed */
865 gtk_drag_finish (helper->context, success, FALSE, helper->time);
867 /* Free the helper */
868 gtk_tree_path_free (helper->source_row);
869 g_slice_free (DndHelper, helper);
873 * This function is used by drag_data_received_cb to manage drag and
874 * drop of a header, i.e, and drag from the header view to the folder
878 drag_and_drop_from_header_view (GtkTreeModel *source_model,
879 GtkTreeModel *dest_model,
880 GtkTreePath *dest_row,
885 ModestMailOperation *mail_op;
886 GtkTreeIter source_iter, dest_iter;
889 gtk_tree_model_get_iter (source_model, &source_iter, helper->source_row);
890 gtk_tree_model_get (source_model, &source_iter,
891 TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN,
895 gtk_tree_model_get_iter (dest_model, &dest_iter, dest_row);
896 gtk_tree_model_get (dest_model, &dest_iter,
897 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
900 /* Transfer message */
901 mail_op = modest_mail_operation_new ();
902 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
904 g_signal_connect (G_OBJECT (mail_op), "progress-changed",
905 G_CALLBACK (on_progress_changed), helper);
907 modest_mail_operation_xfer_msg (mail_op, header, folder, helper->delete_source);
910 g_object_unref (G_OBJECT (mail_op));
911 g_object_unref (G_OBJECT (header));
912 g_object_unref (G_OBJECT (folder));
916 * This function is used by drag_data_received_cb to manage drag and
917 * drop of a folder, i.e, and drag from the folder view to the same
921 drag_and_drop_from_folder_view (GtkTreeModel *source_model,
922 GtkTreeModel *dest_model,
923 GtkTreePath *dest_row,
924 GtkSelectionData *selection_data,
927 ModestMailOperation *mail_op;
928 GtkTreeIter parent_iter, iter;
929 TnyFolderStore *parent_folder;
932 /* Check if the drag is possible */
933 if (!gtk_tree_path_compare (helper->source_row, dest_row) ||
934 !gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (dest_model),
938 gtk_drag_finish (helper->context, FALSE, FALSE, helper->time);
939 gtk_tree_path_free (helper->source_row);
940 g_slice_free (DndHelper, helper);
945 gtk_tree_model_get_iter (source_model, &parent_iter, dest_row);
946 gtk_tree_model_get_iter (source_model, &iter, helper->source_row);
947 gtk_tree_model_get (source_model, &parent_iter,
948 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
950 gtk_tree_model_get (source_model, &iter,
951 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
954 /* Do the mail operation */
955 mail_op = modest_mail_operation_new ();
956 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
958 g_signal_connect (G_OBJECT (mail_op), "progress-changed",
959 G_CALLBACK (on_progress_changed), helper);
961 modest_mail_operation_xfer_folder (mail_op,
964 helper->delete_source);
967 g_object_unref (G_OBJECT (parent_folder));
968 g_object_unref (G_OBJECT (folder));
969 g_object_unref (G_OBJECT (mail_op));
973 * This function receives the data set by the "drag-data-get" signal
974 * handler. This information comes within the #GtkSelectionData. This
975 * function will manage both the drags of folders of the treeview and
976 * drags of headers of the header view widget.
979 on_drag_data_received (GtkWidget *widget,
980 GdkDragContext *context,
983 GtkSelectionData *selection_data,
988 GtkWidget *source_widget;
989 GtkTreeModel *model_sort, *dest_model, *source_model;
990 GtkTreePath *source_row, *dest_row, *child_dest_row;
991 GtkTreeViewDropPosition pos;
992 gboolean success = FALSE, delete_source = FALSE;
995 /* Do not allow further process */
996 g_signal_stop_emission_by_name (widget, "drag-data-received");
999 if (context->action == GDK_ACTION_MOVE)
1000 delete_source = TRUE;
1002 /* Check if the get_data failed */
1003 if (selection_data == NULL || selection_data->length < 0)
1004 gtk_drag_finish (context, success, FALSE, time);
1006 /* Get the models */
1007 source_widget = gtk_drag_get_source_widget (context);
1008 gtk_tree_get_row_drag_data (selection_data,
1012 model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
1013 /* Select the destination model */
1014 if (source_widget == widget) {
1015 dest_model = source_model;
1017 dest_model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
1020 /* Get the path to the destination row. Can not call
1021 gtk_tree_view_get_drag_dest_row() because the source row
1022 is not selected anymore */
1023 gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget), x, y,
1026 /* Only allow drops IN other rows */
1027 if (!dest_row || pos == GTK_TREE_VIEW_DROP_BEFORE || pos == GTK_TREE_VIEW_DROP_AFTER)
1028 gtk_drag_finish (context, success, FALSE, time);
1030 /* Create the helper */
1031 helper = g_slice_new0 (DndHelper);
1032 helper->delete_source = delete_source;
1033 helper->source_row = gtk_tree_path_copy (source_row);
1034 helper->context = context;
1035 helper->time = time;
1037 /* Get path from the unsorted model */
1039 gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
1041 gtk_tree_path_free (dest_row);
1043 /* Drags from the header view */
1044 if (source_widget != widget) {
1046 drag_and_drop_from_header_view (source_model,
1053 drag_and_drop_from_folder_view (source_model,
1061 gtk_tree_path_free (source_row);
1062 gtk_tree_path_free (child_dest_row);
1066 * We define a "drag-drop" signal handler because we do not want to
1067 * use the default one, because the default one always calls
1068 * gtk_drag_finish and we prefer to do it in the "drag-data-received"
1069 * signal handler, because there we have all the information available
1070 * to know if the dnd was a success or not.
1073 drag_drop_cb (GtkWidget *widget,
1074 GdkDragContext *context,
1082 if (!context->targets)
1085 /* Check if we're dragging a folder row */
1086 target = gtk_drag_dest_find_target (widget, context, NULL);
1088 /* Request the data from the source. */
1089 gtk_drag_get_data(widget, context, target, time);
1095 * This function expands a node of a tree view if it's not expanded
1096 * yet. Not sure why it needs the threads stuff, but gtk+`example code
1097 * does that, so that's why they're here.
1100 expand_row_timeout (gpointer data)
1102 GtkTreeView *tree_view = data;
1103 GtkTreePath *dest_path = NULL;
1104 GtkTreeViewDropPosition pos;
1105 gboolean result = FALSE;
1107 GDK_THREADS_ENTER ();
1109 gtk_tree_view_get_drag_dest_row (tree_view,
1114 (pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER ||
1115 pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)) {
1116 gtk_tree_view_expand_row (tree_view, dest_path, FALSE);
1117 gtk_tree_path_free (dest_path);
1121 gtk_tree_path_free (dest_path);
1126 GDK_THREADS_LEAVE ();
1132 * This function is called whenever the pointer is moved over a widget
1133 * while dragging some data. It installs a timeout that will expand a
1134 * node of the treeview if not expanded yet. This function also calls
1135 * gdk_drag_status in order to set the suggested action that will be
1136 * used by the "drag-data-received" signal handler to know if we
1137 * should do a move or just a copy of the data.
1140 on_drag_motion (GtkWidget *widget,
1141 GdkDragContext *context,
1147 GtkTreeViewDropPosition pos;
1148 GtkTreePath *dest_row;
1149 ModestFolderViewPrivate *priv;
1150 GdkDragAction suggested_action;
1151 gboolean valid_location = FALSE;
1153 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (widget);
1155 if (priv->timer_expander != 0) {
1156 g_source_remove (priv->timer_expander);
1157 priv->timer_expander = 0;
1160 gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
1165 /* Do not allow drops between folders */
1167 pos == GTK_TREE_VIEW_DROP_BEFORE ||
1168 pos == GTK_TREE_VIEW_DROP_AFTER) {
1169 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW (widget), NULL, 0);
1170 gdk_drag_status(context, 0, time);
1171 valid_location = FALSE;
1174 valid_location = TRUE;
1177 /* Expand the selected row after 1/2 second */
1178 if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), dest_row)) {
1179 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), dest_row, pos);
1180 priv->timer_expander = g_timeout_add (500, expand_row_timeout, widget);
1182 gtk_tree_path_free (dest_row);
1184 /* Select the desired action. By default we pick MOVE */
1185 suggested_action = GDK_ACTION_MOVE;
1187 if (context->actions == GDK_ACTION_COPY)
1188 gdk_drag_status(context, GDK_ACTION_COPY, time);
1189 else if (context->actions == GDK_ACTION_MOVE)
1190 gdk_drag_status(context, GDK_ACTION_MOVE, time);
1191 else if (context->actions & suggested_action)
1192 gdk_drag_status(context, suggested_action, time);
1194 gdk_drag_status(context, GDK_ACTION_DEFAULT, time);
1197 g_signal_stop_emission_by_name (widget, "drag-motion");
1198 return valid_location;
1202 /* Folder view drag types */
1203 const GtkTargetEntry folder_view_drag_types[] =
1205 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, MODEST_FOLDER_ROW },
1206 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_APP, MODEST_HEADER_ROW }
1210 * This function sets the treeview as a source and a target for dnd
1211 * events. It also connects all the requirede signals.
1214 setup_drag_and_drop (GtkTreeView *self)
1216 /* Set up the folder view as a dnd destination. Set only the
1217 highlight flag, otherwise gtk will have a different
1219 gtk_drag_dest_set (GTK_WIDGET (self),
1220 GTK_DEST_DEFAULT_HIGHLIGHT,
1221 folder_view_drag_types,
1222 G_N_ELEMENTS (folder_view_drag_types),
1223 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1225 g_signal_connect (G_OBJECT (self),
1226 "drag_data_received",
1227 G_CALLBACK (on_drag_data_received),
1231 /* Set up the treeview as a dnd source */
1232 gtk_drag_source_set (GTK_WIDGET (self),
1234 folder_view_drag_types,
1235 G_N_ELEMENTS (folder_view_drag_types),
1236 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1238 g_signal_connect (G_OBJECT (self),
1240 G_CALLBACK (on_drag_motion),
1243 g_signal_connect (G_OBJECT (self),
1245 G_CALLBACK (on_drag_data_get),
1248 g_signal_connect (G_OBJECT (self),
1250 G_CALLBACK (drag_drop_cb),
1255 * This function manages the navigation through the folders using the
1256 * keyboard or the hardware keys in the device
1259 on_key_pressed (GtkWidget *self,
1263 GtkTreeSelection *selection;
1265 GtkTreeModel *model;
1266 gboolean retval = FALSE;
1268 /* Up and Down are automatically managed by the treeview */
1269 if (event->keyval == GDK_Return) {
1270 /* Expand/Collapse the selected row */
1271 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
1272 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
1275 path = gtk_tree_model_get_path (model, &iter);
1277 if (gtk_tree_view_row_expanded (GTK_TREE_VIEW (self), path))
1278 gtk_tree_view_collapse_row (GTK_TREE_VIEW (self), path);
1280 gtk_tree_view_expand_row (GTK_TREE_VIEW (self), path, FALSE);
1281 gtk_tree_path_free (path);
1283 /* No further processing */
1291 * We listen to the changes in the local folder account name key,
1292 * because we want to show the right name in the view. The local
1293 * folder account name corresponds to the device name in the Maemo
1294 * version. We do this because we do not want to query gconf on each
1295 * tree view refresh. It's better to cache it and change whenever
1299 on_configuration_key_changed (ModestConf* conf,
1301 ModestConfEvent event,
1302 ModestFolderView *self)
1304 ModestFolderViewPrivate *priv;
1306 if (!key || strcmp (key, MODEST_CONF_DEVICE_NAME))
1309 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
1311 g_free (priv->local_account_name);
1313 if (event == MODEST_CONF_EVENT_KEY_UNSET)
1314 priv->local_account_name = g_strdup (MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME);
1316 priv->local_account_name = modest_conf_get_string (modest_runtime_get_conf(),
1317 MODEST_CONF_DEVICE_NAME, NULL);
1319 /* Force a redraw */
1320 #if GTK_CHECK_VERSION(2, 8, 0) /* gtk_tree_view_column_queue_resize is only available in GTK+ 2.8 */
1321 GtkTreeViewColumn * tree_column = gtk_tree_view_get_column (GTK_TREE_VIEW (self),
1322 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN);
1323 gtk_tree_view_column_queue_resize (tree_column);