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 /* Order must be: Remote accounts -> Local account -> MMC account */
764 if (type == TNY_FOLDER_TYPE_ROOT) {
765 const gchar *account_id = tny_account_get_id (TNY_ACCOUNT (folder1));
766 const gchar *account_id2 = tny_account_get_id (TNY_ACCOUNT (folder2));
768 if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID))
771 if (!strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID)) {
772 if (!strcmp (account_id2, MODEST_MMC_ACCOUNT_ID))
777 if (!strcmp (account_id2, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) ||
778 !strcmp (account_id2, MODEST_MMC_ACCOUNT_ID))
781 cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
784 g_print ("%s,%s - %d\n",account_id, account_id2,cmp);
786 cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
789 g_object_unref(G_OBJECT(folder1));
791 g_object_unref(G_OBJECT(folder2));
799 /*****************************************************************************/
800 /* DRAG and DROP stuff */
801 /*****************************************************************************/
804 * This function fills the #GtkSelectionData with the row and the
805 * model that has been dragged. It's called when this widget is a
806 * source for dnd after the event drop happened
809 on_drag_data_get (GtkWidget *widget,
810 GdkDragContext *context,
811 GtkSelectionData *selection_data,
816 GtkTreeSelection *selection;
817 GtkTreeModel *model_sort, *model;
819 GtkTreePath *source_row_sort;
820 GtkTreePath *source_row;
822 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
823 gtk_tree_selection_get_selected (selection, &model_sort, &iter);
824 source_row_sort = gtk_tree_model_get_path (model_sort, &iter);
826 /* Get the unsorted path and model */
827 model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
828 source_row = gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
831 gtk_tree_set_row_drag_data (selection_data,
835 gtk_tree_path_free (source_row_sort);
836 gtk_tree_path_free (source_row);
839 typedef struct _DndHelper {
840 gboolean delete_source;
841 GtkTreePath *source_row;
842 GdkDragContext *context;
848 * This function is the callback of the
849 * modest_mail_operation_xfer_msg() and
850 * modest_mail_operation_xfer_folder() calls. We check here if the
851 * message/folder was correctly asynchronously transferred. The reason
852 * to use the same callback is that the code is the same, it only has
853 * to check that the operation went fine and then finalize the drag
857 on_progress_changed (ModestMailOperation *mail_op, gpointer user_data)
862 helper = (DndHelper *) user_data;
864 if (!modest_mail_operation_is_finished (mail_op))
867 if (modest_mail_operation_get_status (mail_op) ==
868 MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
874 /* Notify the drag source. Never call delete, the monitor will
875 do the job if needed */
876 gtk_drag_finish (helper->context, success, FALSE, helper->time);
878 /* Free the helper */
879 gtk_tree_path_free (helper->source_row);
880 g_slice_free (DndHelper, helper);
884 * This function is used by drag_data_received_cb to manage drag and
885 * drop of a header, i.e, and drag from the header view to the folder
889 drag_and_drop_from_header_view (GtkTreeModel *source_model,
890 GtkTreeModel *dest_model,
891 GtkTreePath *dest_row,
896 ModestMailOperation *mail_op;
897 GtkTreeIter source_iter, dest_iter;
900 gtk_tree_model_get_iter (source_model, &source_iter, helper->source_row);
901 gtk_tree_model_get (source_model, &source_iter,
902 TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN,
906 gtk_tree_model_get_iter (dest_model, &dest_iter, dest_row);
907 gtk_tree_model_get (dest_model, &dest_iter,
908 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
911 /* Transfer message */
912 mail_op = modest_mail_operation_new ();
913 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
915 g_signal_connect (G_OBJECT (mail_op), "progress-changed",
916 G_CALLBACK (on_progress_changed), helper);
918 modest_mail_operation_xfer_msg (mail_op, header, folder, helper->delete_source);
921 g_object_unref (G_OBJECT (mail_op));
922 g_object_unref (G_OBJECT (header));
923 g_object_unref (G_OBJECT (folder));
927 * This function is used by drag_data_received_cb to manage drag and
928 * drop of a folder, i.e, and drag from the folder view to the same
932 drag_and_drop_from_folder_view (GtkTreeModel *source_model,
933 GtkTreeModel *dest_model,
934 GtkTreePath *dest_row,
935 GtkSelectionData *selection_data,
938 ModestMailOperation *mail_op;
939 GtkTreeIter parent_iter, iter;
940 TnyFolderStore *parent_folder;
943 /* Check if the drag is possible */
944 if (!gtk_tree_path_compare (helper->source_row, dest_row) ||
945 !gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (dest_model),
949 gtk_drag_finish (helper->context, FALSE, FALSE, helper->time);
950 gtk_tree_path_free (helper->source_row);
951 g_slice_free (DndHelper, helper);
956 gtk_tree_model_get_iter (source_model, &parent_iter, dest_row);
957 gtk_tree_model_get_iter (source_model, &iter, helper->source_row);
958 gtk_tree_model_get (source_model, &parent_iter,
959 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
961 gtk_tree_model_get (source_model, &iter,
962 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
965 /* Do the mail operation */
966 mail_op = modest_mail_operation_new ();
967 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
969 g_signal_connect (G_OBJECT (mail_op), "progress-changed",
970 G_CALLBACK (on_progress_changed), helper);
972 modest_mail_operation_xfer_folder (mail_op,
975 helper->delete_source);
978 g_object_unref (G_OBJECT (parent_folder));
979 g_object_unref (G_OBJECT (folder));
980 g_object_unref (G_OBJECT (mail_op));
984 * This function receives the data set by the "drag-data-get" signal
985 * handler. This information comes within the #GtkSelectionData. This
986 * function will manage both the drags of folders of the treeview and
987 * drags of headers of the header view widget.
990 on_drag_data_received (GtkWidget *widget,
991 GdkDragContext *context,
994 GtkSelectionData *selection_data,
999 GtkWidget *source_widget;
1000 GtkTreeModel *model_sort, *dest_model, *source_model;
1001 GtkTreePath *source_row, *dest_row, *child_dest_row;
1002 GtkTreeViewDropPosition pos;
1003 gboolean success = FALSE, delete_source = FALSE;
1006 /* Do not allow further process */
1007 g_signal_stop_emission_by_name (widget, "drag-data-received");
1009 /* Get the action */
1010 if (context->action == GDK_ACTION_MOVE)
1011 delete_source = TRUE;
1013 /* Check if the get_data failed */
1014 if (selection_data == NULL || selection_data->length < 0)
1015 gtk_drag_finish (context, success, FALSE, time);
1017 /* Get the models */
1018 source_widget = gtk_drag_get_source_widget (context);
1019 gtk_tree_get_row_drag_data (selection_data,
1023 model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
1024 /* Select the destination model */
1025 if (source_widget == widget) {
1026 dest_model = source_model;
1028 dest_model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
1031 /* Get the path to the destination row. Can not call
1032 gtk_tree_view_get_drag_dest_row() because the source row
1033 is not selected anymore */
1034 gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget), x, y,
1037 /* Only allow drops IN other rows */
1038 if (!dest_row || pos == GTK_TREE_VIEW_DROP_BEFORE || pos == GTK_TREE_VIEW_DROP_AFTER)
1039 gtk_drag_finish (context, success, FALSE, time);
1041 /* Create the helper */
1042 helper = g_slice_new0 (DndHelper);
1043 helper->delete_source = delete_source;
1044 helper->source_row = gtk_tree_path_copy (source_row);
1045 helper->context = context;
1046 helper->time = time;
1048 /* Get path from the unsorted model */
1050 gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
1052 gtk_tree_path_free (dest_row);
1054 /* Drags from the header view */
1055 if (source_widget != widget) {
1057 drag_and_drop_from_header_view (source_model,
1064 drag_and_drop_from_folder_view (source_model,
1072 gtk_tree_path_free (source_row);
1073 gtk_tree_path_free (child_dest_row);
1077 * We define a "drag-drop" signal handler because we do not want to
1078 * use the default one, because the default one always calls
1079 * gtk_drag_finish and we prefer to do it in the "drag-data-received"
1080 * signal handler, because there we have all the information available
1081 * to know if the dnd was a success or not.
1084 drag_drop_cb (GtkWidget *widget,
1085 GdkDragContext *context,
1093 if (!context->targets)
1096 /* Check if we're dragging a folder row */
1097 target = gtk_drag_dest_find_target (widget, context, NULL);
1099 /* Request the data from the source. */
1100 gtk_drag_get_data(widget, context, target, time);
1106 * This function expands a node of a tree view if it's not expanded
1107 * yet. Not sure why it needs the threads stuff, but gtk+`example code
1108 * does that, so that's why they're here.
1111 expand_row_timeout (gpointer data)
1113 GtkTreeView *tree_view = data;
1114 GtkTreePath *dest_path = NULL;
1115 GtkTreeViewDropPosition pos;
1116 gboolean result = FALSE;
1118 GDK_THREADS_ENTER ();
1120 gtk_tree_view_get_drag_dest_row (tree_view,
1125 (pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER ||
1126 pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)) {
1127 gtk_tree_view_expand_row (tree_view, dest_path, FALSE);
1128 gtk_tree_path_free (dest_path);
1132 gtk_tree_path_free (dest_path);
1137 GDK_THREADS_LEAVE ();
1143 * This function is called whenever the pointer is moved over a widget
1144 * while dragging some data. It installs a timeout that will expand a
1145 * node of the treeview if not expanded yet. This function also calls
1146 * gdk_drag_status in order to set the suggested action that will be
1147 * used by the "drag-data-received" signal handler to know if we
1148 * should do a move or just a copy of the data.
1151 on_drag_motion (GtkWidget *widget,
1152 GdkDragContext *context,
1158 GtkTreeViewDropPosition pos;
1159 GtkTreePath *dest_row;
1160 ModestFolderViewPrivate *priv;
1161 GdkDragAction suggested_action;
1162 gboolean valid_location = FALSE;
1164 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (widget);
1166 if (priv->timer_expander != 0) {
1167 g_source_remove (priv->timer_expander);
1168 priv->timer_expander = 0;
1171 gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
1176 /* Do not allow drops between folders */
1178 pos == GTK_TREE_VIEW_DROP_BEFORE ||
1179 pos == GTK_TREE_VIEW_DROP_AFTER) {
1180 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW (widget), NULL, 0);
1181 gdk_drag_status(context, 0, time);
1182 valid_location = FALSE;
1185 valid_location = TRUE;
1188 /* Expand the selected row after 1/2 second */
1189 if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), dest_row)) {
1190 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), dest_row, pos);
1191 priv->timer_expander = g_timeout_add (500, expand_row_timeout, widget);
1193 gtk_tree_path_free (dest_row);
1195 /* Select the desired action. By default we pick MOVE */
1196 suggested_action = GDK_ACTION_MOVE;
1198 if (context->actions == GDK_ACTION_COPY)
1199 gdk_drag_status(context, GDK_ACTION_COPY, time);
1200 else if (context->actions == GDK_ACTION_MOVE)
1201 gdk_drag_status(context, GDK_ACTION_MOVE, time);
1202 else if (context->actions & suggested_action)
1203 gdk_drag_status(context, suggested_action, time);
1205 gdk_drag_status(context, GDK_ACTION_DEFAULT, time);
1208 g_signal_stop_emission_by_name (widget, "drag-motion");
1209 return valid_location;
1213 /* Folder view drag types */
1214 const GtkTargetEntry folder_view_drag_types[] =
1216 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, MODEST_FOLDER_ROW },
1217 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_APP, MODEST_HEADER_ROW }
1221 * This function sets the treeview as a source and a target for dnd
1222 * events. It also connects all the requirede signals.
1225 setup_drag_and_drop (GtkTreeView *self)
1227 /* Set up the folder view as a dnd destination. Set only the
1228 highlight flag, otherwise gtk will have a different
1230 gtk_drag_dest_set (GTK_WIDGET (self),
1231 GTK_DEST_DEFAULT_HIGHLIGHT,
1232 folder_view_drag_types,
1233 G_N_ELEMENTS (folder_view_drag_types),
1234 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1236 g_signal_connect (G_OBJECT (self),
1237 "drag_data_received",
1238 G_CALLBACK (on_drag_data_received),
1242 /* Set up the treeview as a dnd source */
1243 gtk_drag_source_set (GTK_WIDGET (self),
1245 folder_view_drag_types,
1246 G_N_ELEMENTS (folder_view_drag_types),
1247 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1249 g_signal_connect (G_OBJECT (self),
1251 G_CALLBACK (on_drag_motion),
1254 g_signal_connect (G_OBJECT (self),
1256 G_CALLBACK (on_drag_data_get),
1259 g_signal_connect (G_OBJECT (self),
1261 G_CALLBACK (drag_drop_cb),
1266 * This function manages the navigation through the folders using the
1267 * keyboard or the hardware keys in the device
1270 on_key_pressed (GtkWidget *self,
1274 GtkTreeSelection *selection;
1276 GtkTreeModel *model;
1277 gboolean retval = FALSE;
1279 /* Up and Down are automatically managed by the treeview */
1280 if (event->keyval == GDK_Return) {
1281 /* Expand/Collapse the selected row */
1282 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
1283 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
1286 path = gtk_tree_model_get_path (model, &iter);
1288 if (gtk_tree_view_row_expanded (GTK_TREE_VIEW (self), path))
1289 gtk_tree_view_collapse_row (GTK_TREE_VIEW (self), path);
1291 gtk_tree_view_expand_row (GTK_TREE_VIEW (self), path, FALSE);
1292 gtk_tree_path_free (path);
1294 /* No further processing */
1302 * We listen to the changes in the local folder account name key,
1303 * because we want to show the right name in the view. The local
1304 * folder account name corresponds to the device name in the Maemo
1305 * version. We do this because we do not want to query gconf on each
1306 * tree view refresh. It's better to cache it and change whenever
1310 on_configuration_key_changed (ModestConf* conf,
1312 ModestConfEvent event,
1313 ModestFolderView *self)
1315 ModestFolderViewPrivate *priv;
1317 if (!key || strcmp (key, MODEST_CONF_DEVICE_NAME))
1320 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
1322 g_free (priv->local_account_name);
1324 if (event == MODEST_CONF_EVENT_KEY_UNSET)
1325 priv->local_account_name = g_strdup (MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME);
1327 priv->local_account_name = modest_conf_get_string (modest_runtime_get_conf(),
1328 MODEST_CONF_DEVICE_NAME, NULL);
1330 /* Force a redraw */
1331 #if GTK_CHECK_VERSION(2, 8, 0) /* gtk_tree_view_column_queue_resize is only available in GTK+ 2.8 */
1332 GtkTreeViewColumn * tree_column = gtk_tree_view_get_column (GTK_TREE_VIEW (self),
1333 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN);
1334 gtk_tree_view_column_queue_resize (tree_column);