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-folder-store-observer.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-account.h>
45 #include <modest-tny-folder.h>
46 #include <modest-tny-local-folders-account.h>
47 #include <modest-tny-outbox-account.h>
48 #include <modest-marshal.h>
49 #include <modest-icon-names.h>
50 #include <modest-tny-account-store.h>
51 #include <modest-text-utils.h>
52 #include <modest-runtime.h>
53 #include "modest-folder-view.h"
54 #include <modest-dnd.h>
55 #include <modest-platform.h>
56 #include <modest-widget-memory.h>
57 #include <modest-ui-actions.h>
59 /* 'private'/'protected' functions */
60 static void modest_folder_view_class_init (ModestFolderViewClass *klass);
61 static void modest_folder_view_init (ModestFolderView *obj);
62 static void modest_folder_view_finalize (GObject *obj);
64 static void tny_account_store_view_init (gpointer g,
67 static void modest_folder_view_set_account_store (TnyAccountStoreView *self,
68 TnyAccountStore *account_store);
70 static void on_selection_changed (GtkTreeSelection *sel, gpointer data);
72 static void on_account_update (TnyAccountStore *account_store,
76 static void on_account_removed (TnyAccountStore *self,
80 static void on_accounts_reloaded (TnyAccountStore *store,
83 static gint cmp_rows (GtkTreeModel *tree_model,
88 static gboolean filter_row (GtkTreeModel *model,
92 static gboolean on_key_pressed (GtkWidget *self,
96 static void on_configuration_key_changed (ModestConf* conf,
98 ModestConfEvent event,
99 ModestFolderView *self);
102 static void on_drag_data_get (GtkWidget *widget,
103 GdkDragContext *context,
104 GtkSelectionData *selection_data,
109 static void on_drag_data_received (GtkWidget *widget,
110 GdkDragContext *context,
113 GtkSelectionData *selection_data,
118 static gboolean on_drag_motion (GtkWidget *widget,
119 GdkDragContext *context,
125 static gint expand_row_timeout (gpointer data);
127 static void setup_drag_and_drop (GtkTreeView *self);
129 static gboolean _clipboard_set_selected_data (ModestFolderView *folder_view, gboolean delete);
131 static void _clear_hidding_filter (ModestFolderView *folder_view);
136 FOLDER_SELECTION_CHANGED_SIGNAL,
137 FOLDER_DISPLAY_NAME_CHANGED_SIGNAL,
141 typedef struct _ModestFolderViewPrivate ModestFolderViewPrivate;
142 struct _ModestFolderViewPrivate {
143 TnyAccountStore *account_store;
144 TnyFolderStore *cur_folder_store;
146 gulong account_update_signal;
147 gulong changed_signal;
148 gulong accounts_reloaded_signal;
149 gulong account_removed_signal;
150 gulong conf_key_signal;
152 /* not unref this object, its a singlenton */
153 ModestEmailClipboard *clipboard;
155 /* Filter tree model */
159 TnyFolderStoreQuery *query;
160 guint timer_expander;
162 gchar *local_account_name;
163 gchar *visible_account_id;
164 ModestFolderViewStyle style;
166 gboolean reselect; /* we use this to force a reselection of the INBOX */
168 #define MODEST_FOLDER_VIEW_GET_PRIVATE(o) \
169 (G_TYPE_INSTANCE_GET_PRIVATE((o), \
170 MODEST_TYPE_FOLDER_VIEW, \
171 ModestFolderViewPrivate))
173 static GObjectClass *parent_class = NULL;
175 static guint signals[LAST_SIGNAL] = {0};
178 modest_folder_view_get_type (void)
180 static GType my_type = 0;
182 static const GTypeInfo my_info = {
183 sizeof(ModestFolderViewClass),
184 NULL, /* base init */
185 NULL, /* base finalize */
186 (GClassInitFunc) modest_folder_view_class_init,
187 NULL, /* class finalize */
188 NULL, /* class data */
189 sizeof(ModestFolderView),
191 (GInstanceInitFunc) modest_folder_view_init,
195 static const GInterfaceInfo tny_account_store_view_info = {
196 (GInterfaceInitFunc) tny_account_store_view_init, /* interface_init */
197 NULL, /* interface_finalize */
198 NULL /* interface_data */
202 my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
206 g_type_add_interface_static (my_type,
207 TNY_TYPE_ACCOUNT_STORE_VIEW,
208 &tny_account_store_view_info);
214 modest_folder_view_class_init (ModestFolderViewClass *klass)
216 GObjectClass *gobject_class;
217 gobject_class = (GObjectClass*) klass;
219 parent_class = g_type_class_peek_parent (klass);
220 gobject_class->finalize = modest_folder_view_finalize;
222 g_type_class_add_private (gobject_class,
223 sizeof(ModestFolderViewPrivate));
225 signals[FOLDER_SELECTION_CHANGED_SIGNAL] =
226 g_signal_new ("folder_selection_changed",
227 G_TYPE_FROM_CLASS (gobject_class),
229 G_STRUCT_OFFSET (ModestFolderViewClass,
230 folder_selection_changed),
232 modest_marshal_VOID__POINTER_BOOLEAN,
233 G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_BOOLEAN);
236 * This signal is emitted whenever the currently selected
237 * folder display name is computed. Note that the name could
238 * be different to the folder name, because we could append
239 * the unread messages count to the folder name to build the
240 * folder display name
242 signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL] =
243 g_signal_new ("folder-display-name-changed",
244 G_TYPE_FROM_CLASS (gobject_class),
246 G_STRUCT_OFFSET (ModestFolderViewClass,
247 folder_display_name_changed),
249 g_cclosure_marshal_VOID__STRING,
250 G_TYPE_NONE, 1, G_TYPE_STRING);
253 /* Simplify checks for NULLs: */
254 static gboolean strings_are_equal (const gchar *a, const gchar *b)
260 return (strcmp (a, b) == 0);
266 static gboolean on_model_foreach_set_name(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
268 GObject *instance = NULL;
270 gtk_tree_model_get (model, iter,
271 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
275 return FALSE; /* keep walking */
277 if (!TNY_IS_ACCOUNT (instance)) {
278 g_object_unref (instance);
279 return FALSE; /* keep walking */
282 /* Check if this is the looked-for account: */
283 TnyAccount *this_account = TNY_ACCOUNT (instance);
284 TnyAccount *account = TNY_ACCOUNT (data);
286 const gchar *this_account_id = tny_account_get_id(this_account);
287 const gchar *account_id = tny_account_get_id(account);
288 g_object_unref (instance);
291 /* printf ("DEBUG: %s: this_account_id=%s, account_id=%s\n", __FUNCTION__, this_account_id, account_id); */
292 if (strings_are_equal(this_account_id, account_id)) {
293 /* Tell the model that the data has changed, so that
294 * it calls the cell_data_func callbacks again: */
295 /* TODO: This does not seem to actually cause the new string to be shown: */
296 gtk_tree_model_row_changed (model, path, iter);
298 return TRUE; /* stop walking */
301 return FALSE; /* keep walking */
306 ModestFolderView *self;
307 gchar *previous_name;
308 } GetMmcAccountNameData;
310 static void on_get_mmc_account_name (TnyStoreAccount* account, gpointer user_data)
312 /* printf ("DEBU1G: %s: account name=%s\n", __FUNCTION__, tny_account_get_name (TNY_ACCOUNT(account))); */
314 GetMmcAccountNameData *data = (GetMmcAccountNameData*)user_data;
316 if (!strings_are_equal (
317 tny_account_get_name(TNY_ACCOUNT(account)),
318 data->previous_name)) {
320 /* Tell the model that the data has changed, so that
321 * it calls the cell_data_func callbacks again: */
322 ModestFolderView *self = data->self;
323 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
325 gtk_tree_model_foreach(model, on_model_foreach_set_name, account);
328 g_free (data->previous_name);
329 g_slice_free (GetMmcAccountNameData, data);
333 text_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
334 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
336 ModestFolderViewPrivate *priv;
341 TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
342 GObject *instance = NULL;
344 g_return_if_fail (column);
345 g_return_if_fail (tree_model);
347 gtk_tree_model_get (tree_model, iter,
348 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname,
349 TNY_GTK_FOLDER_STORE_TREE_MODEL_ALL_COLUMN, &all,
350 TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN, &unread,
351 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
352 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
354 rendobj = G_OBJECT(renderer);
364 ModestFolderView *self = MODEST_FOLDER_VIEW (data);
365 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
367 gchar *item_name = NULL;
368 gint item_weight = 400;
370 if (type != TNY_FOLDER_TYPE_ROOT) {
373 if (modest_tny_folder_is_local_folder (TNY_FOLDER (instance))) {
374 type = modest_tny_folder_get_local_folder_type (TNY_FOLDER (instance));
375 if (type != TNY_FOLDER_TYPE_UNKNOWN) {
377 fname = g_strdup(modest_local_folder_info_get_type_display_name (type));
381 /* Select the number to show: the unread or unsent messages */
382 if ((type == TNY_FOLDER_TYPE_DRAFTS) || (type == TNY_FOLDER_TYPE_OUTBOX))
387 /* Use bold font style if there are unread or unset messages */
389 item_name = g_strdup_printf ("%s (%d)", fname, number);
392 item_name = g_strdup (fname);
396 } else if (TNY_IS_ACCOUNT (instance)) {
397 /* If it's a server account */
398 if (modest_tny_account_is_virtual_local_folders (
399 TNY_ACCOUNT (instance))) {
400 item_name = g_strdup (priv->local_account_name);
402 } else if (modest_tny_account_is_memory_card_account (
403 TNY_ACCOUNT (instance))) {
404 /* fname is only correct when the items are first
405 * added to the model, not when the account is
406 * changed later, so get the name from the account
408 item_name = g_strdup (tny_account_get_name (
409 TNY_ACCOUNT (instance)));
412 item_name = g_strdup (fname);
418 item_name = g_strdup ("unknown");
420 if (item_name && item_weight) {
421 /* Set the name in the treeview cell: */
422 g_object_set (rendobj,"text", item_name, "weight", item_weight, NULL);
424 /* Notify display name observers */
425 /* TODO: What listens for this signal, and how can it use only the new name? */
426 if (G_OBJECT (priv->cur_folder_store) == instance) {
427 g_signal_emit (G_OBJECT(self),
428 signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
436 /* If it is a Memory card account, make sure that we have the correct name.
437 * This function will be trigerred again when the name has been retrieved: */
438 if (TNY_IS_STORE_ACCOUNT (instance) &&
439 modest_tny_account_is_memory_card_account (TNY_ACCOUNT (instance))) {
441 /* Get the account name asynchronously: */
442 GetMmcAccountNameData *callback_data =
443 g_slice_new0(GetMmcAccountNameData);
444 callback_data->self = self;
446 const gchar *name = tny_account_get_name (TNY_ACCOUNT(instance));
448 callback_data->previous_name = g_strdup (name);
450 modest_tny_account_get_mmc_account_name (TNY_STORE_ACCOUNT (instance),
451 on_get_mmc_account_name, callback_data);
454 g_object_unref (G_OBJECT (instance));
461 icon_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
462 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
464 GObject *rendobj = NULL, *instance = NULL;
465 GdkPixbuf *pixbuf = NULL;
466 TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
468 const gchar *account_id = NULL;
471 rendobj = G_OBJECT(renderer);
472 gtk_tree_model_get (tree_model, iter,
473 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
474 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname,
475 TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN, &unread,
476 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
487 /* We include the MERGE type here because it's used to create
488 the local OUTBOX folder */
489 if (type == TNY_FOLDER_TYPE_NORMAL ||
490 type == TNY_FOLDER_TYPE_UNKNOWN ||
491 type == TNY_FOLDER_TYPE_MERGE) {
492 type = modest_tny_folder_guess_folder_type (TNY_FOLDER (instance));
496 case TNY_FOLDER_TYPE_ROOT:
497 if (TNY_IS_ACCOUNT (instance)) {
499 if (modest_tny_account_is_virtual_local_folders (
500 TNY_ACCOUNT (instance))) {
501 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_LOCAL_FOLDERS);
504 account_id = tny_account_get_id (TNY_ACCOUNT (instance));
506 if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID))
507 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_MMC);
509 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_ACCOUNT);
513 case TNY_FOLDER_TYPE_INBOX:
514 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_INBOX);
516 case TNY_FOLDER_TYPE_OUTBOX:
517 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_OUTBOX);
519 case TNY_FOLDER_TYPE_JUNK:
520 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_JUNK);
522 case TNY_FOLDER_TYPE_SENT:
523 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_SENT);
525 case TNY_FOLDER_TYPE_TRASH:
526 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_TRASH);
528 case TNY_FOLDER_TYPE_DRAFTS:
529 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_DRAFTS);
531 case TNY_FOLDER_TYPE_NORMAL:
533 pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_NORMAL);
537 g_object_unref (G_OBJECT (instance));
541 g_object_set (rendobj, "pixbuf", pixbuf, NULL);
544 g_object_unref (pixbuf);
548 add_columns (GtkWidget *treeview)
550 GtkTreeViewColumn *column;
551 GtkCellRenderer *renderer;
552 GtkTreeSelection *sel;
555 column = gtk_tree_view_column_new ();
557 /* Set icon and text render function */
558 renderer = gtk_cell_renderer_pixbuf_new();
559 gtk_tree_view_column_pack_start (column, renderer, FALSE);
560 gtk_tree_view_column_set_cell_data_func(column, renderer,
561 icon_cell_data, treeview, NULL);
563 renderer = gtk_cell_renderer_text_new();
564 gtk_tree_view_column_pack_start (column, renderer, FALSE);
565 gtk_tree_view_column_set_cell_data_func(column, renderer,
566 text_cell_data, treeview, NULL);
568 /* Set selection mode */
569 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(treeview));
570 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
572 /* Set treeview appearance */
573 gtk_tree_view_column_set_spacing (column, 2);
574 gtk_tree_view_column_set_resizable (column, TRUE);
575 gtk_tree_view_column_set_fixed_width (column, TRUE);
576 gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(treeview), FALSE);
577 gtk_tree_view_set_enable_search (GTK_TREE_VIEW(treeview), FALSE);
580 gtk_tree_view_append_column (GTK_TREE_VIEW(treeview),column);
584 modest_folder_view_init (ModestFolderView *obj)
586 ModestFolderViewPrivate *priv;
589 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
591 priv->timer_expander = 0;
592 priv->account_store = NULL;
594 priv->style = MODEST_FOLDER_VIEW_STYLE_SHOW_ALL;
595 priv->cur_folder_store = NULL;
596 priv->visible_account_id = NULL;
598 /* Initialize the local account name */
599 conf = modest_runtime_get_conf();
600 priv->local_account_name = modest_conf_get_string (conf, MODEST_CONF_DEVICE_NAME, NULL);
602 /* Init email clipboard */
603 priv->clipboard = modest_runtime_get_email_clipboard ();
604 priv->hidding_ids = NULL;
605 priv->n_selected = 0;
606 priv->reselect = FALSE;
609 add_columns (GTK_WIDGET (obj));
611 /* Setup drag and drop */
612 setup_drag_and_drop (GTK_TREE_VIEW(obj));
614 /* Connect signals */
615 g_signal_connect (G_OBJECT (obj),
617 G_CALLBACK (on_key_pressed), NULL);
620 * Track changes in the local account name (in the device it
621 * will be the device name)
623 priv->conf_key_signal =
624 g_signal_connect (G_OBJECT(conf),
626 G_CALLBACK(on_configuration_key_changed), obj);
630 tny_account_store_view_init (gpointer g, gpointer iface_data)
632 TnyAccountStoreViewIface *klass = (TnyAccountStoreViewIface *)g;
634 klass->set_account_store_func = modest_folder_view_set_account_store;
640 modest_folder_view_finalize (GObject *obj)
642 ModestFolderViewPrivate *priv;
643 GtkTreeSelection *sel;
645 g_return_if_fail (obj);
647 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
649 if (priv->timer_expander != 0) {
650 g_source_remove (priv->timer_expander);
651 priv->timer_expander = 0;
654 if (priv->account_store) {
655 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
656 priv->account_update_signal);
657 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
658 priv->accounts_reloaded_signal);
659 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
660 priv->account_removed_signal);
661 g_object_unref (G_OBJECT(priv->account_store));
662 priv->account_store = NULL;
666 g_object_unref (G_OBJECT (priv->query));
670 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(obj));
672 g_signal_handler_disconnect (G_OBJECT(sel), priv->changed_signal);
674 g_free (priv->local_account_name);
675 g_free (priv->visible_account_id);
677 if (priv->conf_key_signal) {
678 g_signal_handler_disconnect (modest_runtime_get_conf (),
679 priv->conf_key_signal);
680 priv->conf_key_signal = 0;
683 if (priv->cur_folder_store) {
684 if (TNY_IS_FOLDER(priv->cur_folder_store))
685 tny_folder_sync (TNY_FOLDER(priv->cur_folder_store), FALSE, NULL);
686 /* FALSE --> expunge the message */
688 g_object_unref (priv->cur_folder_store);
689 priv->cur_folder_store = NULL;
692 /* Clear hidding array created by cut operation */
693 _clear_hidding_filter (MODEST_FOLDER_VIEW (obj));
695 G_OBJECT_CLASS(parent_class)->finalize (obj);
700 modest_folder_view_set_account_store (TnyAccountStoreView *self, TnyAccountStore *account_store)
702 ModestFolderViewPrivate *priv;
705 g_return_if_fail (MODEST_IS_FOLDER_VIEW (self));
706 g_return_if_fail (TNY_IS_ACCOUNT_STORE (account_store));
708 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
709 device = tny_account_store_get_device (account_store);
711 if (G_UNLIKELY (priv->account_store)) {
713 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store),
714 priv->account_update_signal))
715 g_signal_handler_disconnect (G_OBJECT (priv->account_store),
716 priv->account_update_signal);
717 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store),
718 priv->accounts_reloaded_signal))
719 g_signal_handler_disconnect (G_OBJECT (priv->account_store),
720 priv->accounts_reloaded_signal);
721 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store),
722 priv->account_removed_signal))
723 g_signal_handler_disconnect (G_OBJECT (priv->account_store),
724 priv->account_removed_signal);
726 g_object_unref (G_OBJECT (priv->account_store));
729 priv->account_store = g_object_ref (G_OBJECT (account_store));
731 priv->account_update_signal =
732 g_signal_connect (G_OBJECT(account_store), "account_update",
733 G_CALLBACK (on_account_update), self);
735 priv->account_removed_signal =
736 g_signal_connect (G_OBJECT(account_store), "account_removed",
737 G_CALLBACK (on_account_removed), self);
739 priv->accounts_reloaded_signal =
740 g_signal_connect (G_OBJECT(account_store), "accounts_reloaded",
741 G_CALLBACK (on_accounts_reloaded), self);
743 g_signal_connect (G_OBJECT(account_store), "connecting_finished",
744 G_CALLBACK (on_accounts_reloaded), self);
746 on_accounts_reloaded (account_store, (gpointer ) self);
748 g_object_unref (G_OBJECT (device));
752 on_account_removed (TnyAccountStore *account_store,
756 ModestFolderView *self = MODEST_FOLDER_VIEW (user_data);
757 ModestFolderViewPrivate *priv;
759 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
761 /* If the removed account is the currently viewed one then
762 clear the configuration value. The new visible account will be the default account */
763 if (!strcmp (priv->visible_account_id, tny_account_get_id (account))) {
764 modest_folder_view_set_account_id_of_visible_server_account (self, NULL);
769 on_account_update (TnyAccountStore *account_store,
770 const gchar *account,
773 ModestFolderView *self = NULL;
774 ModestFolderViewPrivate *priv;
776 g_return_if_fail (MODEST_IS_FOLDER_VIEW (user_data));
777 self = MODEST_FOLDER_VIEW (user_data);
778 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
780 if (!priv->visible_account_id)
781 modest_widget_memory_restore (modest_runtime_get_conf(), G_OBJECT(self),
782 MODEST_CONF_FOLDER_VIEW_KEY);
784 if (!modest_folder_view_update_model (self, account_store))
785 g_printerr ("modest: failed to update model for changes in '%s'",
790 on_accounts_reloaded (TnyAccountStore *account_store,
793 g_return_if_fail (MODEST_IS_FOLDER_VIEW (user_data));
794 modest_folder_view_update_model (MODEST_FOLDER_VIEW (user_data), account_store);
798 modest_folder_view_set_title (ModestFolderView *self, const gchar *title)
800 GtkTreeViewColumn *col;
802 g_return_if_fail (self);
804 col = gtk_tree_view_get_column (GTK_TREE_VIEW(self), 0);
806 g_printerr ("modest: failed get column for title\n");
810 gtk_tree_view_column_set_title (col, title);
811 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self),
816 modest_folder_view_on_map (ModestFolderView *self,
817 GdkEventExpose *event,
820 ModestFolderViewPrivate *priv;
822 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
824 /* This won't happen often */
825 if (G_UNLIKELY (priv->reselect)) {
826 /* Select the first inbox or the local account if not found */
828 /* TODO: this could cause a lock at startup, so we
829 comment it for the moment. We know that this will
830 be a bug, because the INBOX is not selected, but we
831 need to rewrite some parts of Modest to avoid the
832 deathlock situation */
833 /* modest_folder_view_select_first_inbox_or_local (self); */
834 priv->reselect = FALSE;
840 modest_folder_view_new (TnyFolderStoreQuery *query)
843 ModestFolderViewPrivate *priv;
844 GtkTreeSelection *sel;
846 self = G_OBJECT (g_object_new (MODEST_TYPE_FOLDER_VIEW, NULL));
847 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
850 priv->query = g_object_ref (query);
852 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
853 priv->changed_signal = g_signal_connect (sel, "changed",
854 G_CALLBACK (on_selection_changed), self);
856 g_signal_connect (self, "expose-event", G_CALLBACK (modest_folder_view_on_map), NULL);
858 return GTK_WIDGET(self);
861 /* this feels dirty; any other way to expand all the root items? */
863 expand_root_items (ModestFolderView *self)
866 path = gtk_tree_path_new_first ();
868 /* all folders should have child items, so.. */
869 while (gtk_tree_view_expand_row (GTK_TREE_VIEW(self), path, FALSE))
870 gtk_tree_path_next (path);
872 gtk_tree_path_free (path);
876 * We use this function to implement the
877 * MODEST_FOLDER_VIEW_STYLE_SHOW_ONE style. We only show the default
878 * account in this case, and the local folders.
881 filter_row (GtkTreeModel *model,
885 ModestFolderViewPrivate *priv;
886 gboolean retval = TRUE;
887 TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
888 GObject *instance = NULL;
889 const gchar *id = NULL;
891 gboolean found = FALSE;
892 gboolean cleared = FALSE;
894 g_return_val_if_fail (MODEST_IS_FOLDER_VIEW (data), FALSE);
895 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (data);
897 gtk_tree_model_get (model, iter,
898 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
899 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
902 /* Do not show if there is no instance, this could indeed
903 happen when the model is being modified while it's being
904 drawn. This could occur for example when moving folders
909 if (type == TNY_FOLDER_TYPE_ROOT) {
910 /* TNY_FOLDER_TYPE_ROOT means that the instance is an
911 account instead of a folder. */
912 if (TNY_IS_ACCOUNT (instance)) {
913 TnyAccount *acc = TNY_ACCOUNT (instance);
914 const gchar *account_id = tny_account_get_id (acc);
916 /* If it isn't a special folder,
917 * don't show it unless it is the visible account: */
918 if (!modest_tny_account_is_virtual_local_folders (acc) &&
919 strcmp (account_id, MODEST_MMC_ACCOUNT_ID)) {
920 /* Show only the visible account id */
921 if (priv->visible_account_id && strcmp (account_id, priv->visible_account_id))
925 /* Never show these to the user. They are merged into one folder
926 * in the local-folders account instead: */
927 if (retval && MODEST_IS_TNY_OUTBOX_ACCOUNT (acc))
932 /* The virtual local-folders folder store is also shown by default. */
934 /* Check hiding (if necessary) */
935 cleared = modest_email_clipboard_cleared (priv->clipboard);
936 if ((retval) && (!cleared) && (TNY_IS_FOLDER (instance))) {
937 id = tny_folder_get_id (TNY_FOLDER(instance));
938 if (priv->hidding_ids != NULL)
939 for (i=0; i < priv->n_selected && !found; i++)
940 if (priv->hidding_ids[i] != NULL && id != NULL)
941 found = (!strcmp (priv->hidding_ids[i], id));
947 g_object_unref (instance);
953 modest_folder_view_update_model (ModestFolderView *self,
954 TnyAccountStore *account_store)
956 ModestFolderViewPrivate *priv;
957 GtkTreeModel *model /* , *old_model */;
958 /* TnyAccount *local_account; */
959 TnyList *model_as_list;
961 g_return_val_if_fail (MODEST_IS_FOLDER_VIEW (self), FALSE);
962 g_return_val_if_fail (account_store, FALSE);
964 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
966 /* Notify that there is no folder selected */
967 g_signal_emit (G_OBJECT(self),
968 signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
970 if (priv->cur_folder_store) {
971 g_object_unref (priv->cur_folder_store);
972 priv->cur_folder_store = NULL;
975 /* FIXME: the local accounts are not shown when the query
976 selects only the subscribed folders. */
977 /* model = tny_gtk_folder_store_tree_model_new (TRUE, priv->query); */
978 model = tny_gtk_folder_store_tree_model_new (TRUE, NULL);
980 /* Deal with the model via its TnyList Interface,
981 * filling the TnyList via a get_accounts() call: */
982 model_as_list = TNY_LIST(model);
984 /* Get the accounts: */
985 tny_account_store_get_accounts (TNY_ACCOUNT_STORE(account_store),
987 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
988 g_object_unref (model_as_list);
989 model_as_list = NULL;
991 GtkTreeModel *filter_model = NULL, *sortable = NULL;
993 sortable = gtk_tree_model_sort_new_with_model (model);
994 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(sortable),
995 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
997 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (sortable),
998 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
999 cmp_rows, NULL, NULL);
1001 /* Create filter model */
1002 filter_model = gtk_tree_model_filter_new (sortable, NULL);
1003 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (filter_model),
1007 /* if (priv->style == MODEST_FOLDER_VIEW_STYLE_SHOW_ONE) { */
1008 /* filter_model = gtk_tree_model_filter_new (sortable, NULL); */
1009 /* gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (filter_model), */
1016 gtk_tree_view_set_model (GTK_TREE_VIEW(self), filter_model);
1017 /* gtk_tree_view_set_model (GTK_TREE_VIEW(self), */
1018 /* (filter_model) ? filter_model : sortable); */
1020 g_object_unref (model);
1021 g_object_unref (filter_model);
1022 /* if (filter_model) */
1023 /* g_object_unref (filter_model); */
1025 g_object_unref (sortable);
1027 /* Force a reselection of the INBOX next time the widget is shown */
1028 priv->reselect = TRUE;
1035 on_selection_changed (GtkTreeSelection *sel, gpointer user_data)
1037 GtkTreeModel *model;
1038 TnyFolderStore *folder = NULL;
1040 ModestFolderView *tree_view;
1041 ModestFolderViewPrivate *priv;
1043 g_return_if_fail (sel);
1044 g_return_if_fail (user_data);
1046 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(user_data);
1048 if(!gtk_tree_selection_get_selected (sel, &model, &iter))
1051 /* Notify the display name observers */
1052 g_signal_emit (G_OBJECT(user_data),
1053 signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
1056 tree_view = MODEST_FOLDER_VIEW (user_data);
1057 gtk_tree_model_get (model, &iter,
1058 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder,
1061 /* If the folder is the same do not notify */
1062 if (priv->cur_folder_store == folder && folder) {
1063 g_object_unref (folder);
1067 /* Current folder was unselected */
1068 if (priv->cur_folder_store) {
1069 if (TNY_IS_FOLDER(priv->cur_folder_store))
1070 tny_folder_sync (TNY_FOLDER(priv->cur_folder_store), FALSE, NULL);
1071 /* FALSE --> don't expunge the messages */
1073 g_signal_emit (G_OBJECT(tree_view), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
1074 priv->cur_folder_store, FALSE);
1075 g_object_unref (priv->cur_folder_store);
1076 priv->cur_folder_store = NULL;
1079 /* New current references */
1080 priv->cur_folder_store = folder;
1082 /* New folder has been selected */
1083 g_signal_emit (G_OBJECT(tree_view),
1084 signals[FOLDER_SELECTION_CHANGED_SIGNAL],
1085 0, priv->cur_folder_store, TRUE);
1089 modest_folder_view_get_selected (ModestFolderView *self)
1091 ModestFolderViewPrivate *priv;
1093 g_return_val_if_fail (self, NULL);
1095 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
1096 if (priv->cur_folder_store)
1097 g_object_ref (priv->cur_folder_store);
1099 return priv->cur_folder_store;
1103 get_cmp_rows_type_pos (GObject *folder)
1105 /* Remote accounts -> Local account -> MMC account .*/
1108 if (TNY_IS_ACCOUNT (folder) &&
1109 modest_tny_account_is_virtual_local_folders (
1110 TNY_ACCOUNT (folder))) {
1112 } else if (TNY_IS_ACCOUNT (folder)) {
1113 TnyAccount *account = TNY_ACCOUNT (folder);
1114 const gchar *account_id = tny_account_get_id (account);
1115 if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID))
1121 printf ("DEBUG: %s: unexpected type.\n", __FUNCTION__);
1122 return -1; /* Should never happen */
1127 get_cmp_subfolder_type_pos (TnyFolderType t)
1129 /* Outbox, Drafts, Sent, User */
1133 case TNY_FOLDER_TYPE_OUTBOX:
1136 case TNY_FOLDER_TYPE_DRAFTS:
1139 case TNY_FOLDER_TYPE_SENT:
1148 * This function orders the mail accounts according to these rules:
1149 * 1st - remote accounts
1150 * 2nd - local account
1154 cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2,
1158 gchar *name1 = NULL;
1159 gchar *name2 = NULL;
1160 TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
1161 TnyFolderType type2 = TNY_FOLDER_TYPE_UNKNOWN;
1162 GObject *folder1 = NULL;
1163 GObject *folder2 = NULL;
1165 gtk_tree_model_get (tree_model, iter1,
1166 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name1,
1167 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
1168 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder1,
1170 gtk_tree_model_get (tree_model, iter2,
1171 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name2,
1172 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type2,
1173 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder2,
1176 /* Return if we get no folder. This could happen when folder
1177 operations are happening. The model is updated after the
1178 folder copy/move actually occurs, so there could be
1179 situations where the model to be drawn is not correct */
1180 if (!folder1 || !folder2)
1183 if (type == TNY_FOLDER_TYPE_ROOT) {
1184 /* Compare the types, so that
1185 * Remote accounts -> Local account -> MMC account .*/
1186 const gint pos1 = get_cmp_rows_type_pos (folder1);
1187 const gint pos2 = get_cmp_rows_type_pos (folder2);
1188 /* printf ("DEBUG: %s:\n type1=%s, pos1=%d\n type2=%s, pos2=%d\n",
1189 __FUNCTION__, G_OBJECT_TYPE_NAME(folder1), pos1, G_OBJECT_TYPE_NAME(folder2), pos2); */
1192 else if (pos1 > pos2)
1195 /* Compare items of the same type: */
1197 TnyAccount *account1 = NULL;
1198 if (TNY_IS_ACCOUNT (folder1))
1199 account1 = TNY_ACCOUNT (folder1);
1201 TnyAccount *account2 = NULL;
1202 if (TNY_IS_ACCOUNT (folder2))
1203 account2 = TNY_ACCOUNT (folder2);
1205 const gchar *account_id = account1 ? tny_account_get_id (account1) : NULL;
1206 const gchar *account_id2 = account2 ? tny_account_get_id (account2) : NULL;
1208 if (!account_id && !account_id2)
1210 else if (!account_id)
1212 else if (!account_id2)
1214 else if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID))
1217 cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
1221 gboolean has_parent;
1222 gint cmp1 = 0, cmp2 = 0;
1223 /* get the parent to know if it's a local folder */
1224 has_parent = gtk_tree_model_iter_parent (tree_model, &parent, iter1);
1226 GObject *parent_folder;
1227 TnyFolderType parent_type = TNY_FOLDER_TYPE_UNKNOWN;
1228 gtk_tree_model_get (tree_model, &parent,
1229 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &parent_type,
1230 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &parent_folder,
1232 if ((parent_type == TNY_FOLDER_TYPE_ROOT) &&
1233 TNY_IS_ACCOUNT (parent_folder) &&
1234 modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (parent_folder))) {
1235 cmp1 = get_cmp_subfolder_type_pos (modest_tny_folder_get_local_folder_type (TNY_FOLDER (folder1)));
1236 cmp2 = get_cmp_subfolder_type_pos (modest_tny_folder_get_local_folder_type (TNY_FOLDER (folder2)));
1238 g_object_unref (parent_folder);
1241 cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
1243 cmp = (cmp1 - cmp2);
1247 g_object_unref(G_OBJECT(folder1));
1249 g_object_unref(G_OBJECT(folder2));
1257 /*****************************************************************************/
1258 /* DRAG and DROP stuff */
1259 /*****************************************************************************/
1262 * This function fills the #GtkSelectionData with the row and the
1263 * model that has been dragged. It's called when this widget is a
1264 * source for dnd after the event drop happened
1267 on_drag_data_get (GtkWidget *widget,
1268 GdkDragContext *context,
1269 GtkSelectionData *selection_data,
1274 GtkTreeSelection *selection;
1275 GtkTreeModel *model;
1277 GtkTreePath *source_row;
1279 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
1280 gtk_tree_selection_get_selected (selection, &model, &iter);
1281 source_row = gtk_tree_model_get_path (model, &iter);
1283 gtk_tree_set_row_drag_data (selection_data,
1287 gtk_tree_path_free (source_row);
1290 typedef struct _DndHelper {
1291 gboolean delete_source;
1292 GtkTreePath *source_row;
1293 GdkDragContext *context;
1299 * This function is the callback of the
1300 * modest_mail_operation_xfer_msgs () and
1301 * modest_mail_operation_xfer_folder() calls. We check here if the
1302 * message/folder was correctly asynchronously transferred. The reason
1303 * to use the same callback is that the code is the same, it only has
1304 * to check that the operation went fine and then finalize the drag
1308 on_progress_changed (ModestMailOperation *mail_op,
1309 ModestMailOperationState *state,
1315 helper = (DndHelper *) user_data;
1317 if (!state->finished)
1320 if (state->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
1326 /* Notify the drag source. Never call delete, the monitor will
1327 do the job if needed */
1328 gtk_drag_finish (helper->context, success, FALSE, helper->time);
1330 /* Free the helper */
1331 gtk_tree_path_free (helper->source_row);
1332 g_slice_free (DndHelper, helper);
1336 * This function is used by drag_data_received_cb to manage drag and
1337 * drop of a header, i.e, and drag from the header view to the folder
1341 drag_and_drop_from_header_view (GtkTreeModel *source_model,
1342 GtkTreeModel *dest_model,
1343 GtkTreePath *dest_row,
1346 TnyList *headers = NULL;
1347 TnyHeader *header = NULL;
1348 TnyFolder *folder = NULL;
1349 ModestMailOperation *mail_op = NULL;
1350 GtkTreeIter source_iter, dest_iter;
1352 g_return_if_fail (GTK_IS_TREE_MODEL(source_model));
1353 g_return_if_fail (GTK_IS_TREE_MODEL(dest_model));
1354 g_return_if_fail (dest_row);
1355 g_return_if_fail (helper);
1358 gtk_tree_model_get_iter (source_model, &source_iter, helper->source_row);
1359 gtk_tree_model_get (source_model, &source_iter,
1360 TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN,
1362 if (!TNY_IS_HEADER(header)) {
1363 g_warning ("BUG: %s could not get a valid header", __FUNCTION__);
1368 gtk_tree_model_get_iter (dest_model, &dest_iter, dest_row);
1369 gtk_tree_model_get (dest_model, &dest_iter,
1370 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
1373 if (!TNY_IS_FOLDER(folder)) {
1374 g_warning ("BUG: %s could not get a valid folder", __FUNCTION__);
1378 /* Transfer message */
1379 mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_RECEIVE, NULL);
1380 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
1382 g_signal_connect (G_OBJECT (mail_op), "progress-changed",
1383 G_CALLBACK (on_progress_changed), helper);
1385 headers = tny_simple_list_new ();
1386 tny_list_append (headers, G_OBJECT (header));
1387 modest_mail_operation_xfer_msgs (mail_op,
1390 helper->delete_source,
1395 if (G_IS_OBJECT(mail_op))
1396 g_object_unref (G_OBJECT (mail_op));
1397 if (G_IS_OBJECT(header))
1398 g_object_unref (G_OBJECT (header));
1399 if (G_IS_OBJECT(folder))
1400 g_object_unref (G_OBJECT (folder));
1401 if (G_IS_OBJECT(headers))
1402 g_object_unref (headers);
1406 * This function is used by drag_data_received_cb to manage drag and
1407 * drop of a folder, i.e, and drag from the folder view to the same
1411 drag_and_drop_from_folder_view (GtkTreeModel *source_model,
1412 GtkTreeModel *dest_model,
1413 GtkTreePath *dest_row,
1414 GtkSelectionData *selection_data,
1417 ModestMailOperation *mail_op;
1418 GtkTreeIter parent_iter, iter;
1419 TnyFolderStore *parent_folder;
1422 /* Check if the drag is possible */
1423 /* if (!gtk_tree_path_compare (helper->source_row, dest_row) || */
1424 /* !gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (dest_model), */
1426 /* selection_data)) { */
1427 if (!gtk_tree_path_compare (helper->source_row, dest_row)) {
1429 gtk_drag_finish (helper->context, FALSE, FALSE, helper->time);
1430 gtk_tree_path_free (helper->source_row);
1431 g_slice_free (DndHelper, helper);
1436 gtk_tree_model_get_iter (source_model, &parent_iter, dest_row);
1437 gtk_tree_model_get_iter (source_model, &iter, helper->source_row);
1438 gtk_tree_model_get (source_model, &parent_iter,
1439 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
1440 &parent_folder, -1);
1441 gtk_tree_model_get (source_model, &iter,
1442 TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
1445 /* Do the mail operation */
1446 mail_op = modest_mail_operation_new_with_error_handling (MODEST_MAIL_OPERATION_TYPE_RECEIVE,
1448 modest_ui_actions_move_folder_error_handler,
1450 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
1452 g_signal_connect (G_OBJECT (mail_op), "progress-changed",
1453 G_CALLBACK (on_progress_changed), helper);
1455 modest_mail_operation_xfer_folder (mail_op,
1458 helper->delete_source,
1463 g_object_unref (G_OBJECT (parent_folder));
1464 g_object_unref (G_OBJECT (folder));
1465 g_object_unref (G_OBJECT (mail_op));
1469 * This function receives the data set by the "drag-data-get" signal
1470 * handler. This information comes within the #GtkSelectionData. This
1471 * function will manage both the drags of folders of the treeview and
1472 * drags of headers of the header view widget.
1475 on_drag_data_received (GtkWidget *widget,
1476 GdkDragContext *context,
1479 GtkSelectionData *selection_data,
1484 GtkWidget *source_widget;
1485 GtkTreeModel *dest_model, *source_model;
1486 GtkTreePath *source_row, *dest_row;
1487 GtkTreeViewDropPosition pos;
1488 gboolean success = FALSE, delete_source = FALSE;
1489 DndHelper *helper = NULL;
1491 /* Do not allow further process */
1492 g_signal_stop_emission_by_name (widget, "drag-data-received");
1493 source_widget = gtk_drag_get_source_widget (context);
1495 /* Get the action */
1496 if (context->action == GDK_ACTION_MOVE) {
1497 delete_source = TRUE;
1499 /* Notify that there is no folder selected. We need to
1500 do this in order to update the headers view (and
1501 its monitors, because when moving, the old folder
1502 won't longer exist. We can not wait for the end of
1503 the operation, because the operation won't start if
1504 the folder is in use */
1505 if (source_widget == widget) {
1506 ModestFolderViewPrivate *priv;
1508 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (widget);
1509 if (priv->cur_folder_store) {
1510 g_object_unref (priv->cur_folder_store);
1511 priv->cur_folder_store = NULL;
1514 g_signal_emit (G_OBJECT (widget),
1515 signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0, NULL, FALSE);
1519 /* Check if the get_data failed */
1520 if (selection_data == NULL || selection_data->length < 0)
1521 gtk_drag_finish (context, success, FALSE, time);
1523 /* Get the models */
1524 gtk_tree_get_row_drag_data (selection_data,
1528 /* Select the destination model */
1529 if (source_widget == widget) {
1530 dest_model = source_model;
1532 dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
1535 /* Get the path to the destination row. Can not call
1536 gtk_tree_view_get_drag_dest_row() because the source row
1537 is not selected anymore */
1538 gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget), x, y,
1541 /* Only allow drops IN other rows */
1542 if (!dest_row || pos == GTK_TREE_VIEW_DROP_BEFORE || pos == GTK_TREE_VIEW_DROP_AFTER)
1543 gtk_drag_finish (context, success, FALSE, time);
1545 /* Create the helper */
1546 helper = g_slice_new0 (DndHelper);
1547 helper->delete_source = delete_source;
1548 helper->source_row = gtk_tree_path_copy (source_row);
1549 helper->context = context;
1550 helper->time = time;
1552 /* Drags from the header view */
1553 if (source_widget != widget) {
1555 drag_and_drop_from_header_view (source_model,
1562 drag_and_drop_from_folder_view (source_model,
1570 gtk_tree_path_free (source_row);
1571 gtk_tree_path_free (dest_row);
1575 * We define a "drag-drop" signal handler because we do not want to
1576 * use the default one, because the default one always calls
1577 * gtk_drag_finish and we prefer to do it in the "drag-data-received"
1578 * signal handler, because there we have all the information available
1579 * to know if the dnd was a success or not.
1582 drag_drop_cb (GtkWidget *widget,
1583 GdkDragContext *context,
1591 if (!context->targets)
1594 /* Check if we're dragging a folder row */
1595 target = gtk_drag_dest_find_target (widget, context, NULL);
1597 /* Request the data from the source. */
1598 gtk_drag_get_data(widget, context, target, time);
1604 * This function expands a node of a tree view if it's not expanded
1605 * yet. Not sure why it needs the threads stuff, but gtk+`example code
1606 * does that, so that's why they're here.
1609 expand_row_timeout (gpointer data)
1611 GtkTreeView *tree_view = data;
1612 GtkTreePath *dest_path = NULL;
1613 GtkTreeViewDropPosition pos;
1614 gboolean result = FALSE;
1616 GDK_THREADS_ENTER ();
1618 gtk_tree_view_get_drag_dest_row (tree_view,
1623 (pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER ||
1624 pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)) {
1625 gtk_tree_view_expand_row (tree_view, dest_path, FALSE);
1626 gtk_tree_path_free (dest_path);
1630 gtk_tree_path_free (dest_path);
1635 GDK_THREADS_LEAVE ();
1641 * This function is called whenever the pointer is moved over a widget
1642 * while dragging some data. It installs a timeout that will expand a
1643 * node of the treeview if not expanded yet. This function also calls
1644 * gdk_drag_status in order to set the suggested action that will be
1645 * used by the "drag-data-received" signal handler to know if we
1646 * should do a move or just a copy of the data.
1649 on_drag_motion (GtkWidget *widget,
1650 GdkDragContext *context,
1656 GtkTreeViewDropPosition pos;
1657 GtkTreePath *dest_row;
1658 ModestFolderViewPrivate *priv;
1659 GdkDragAction suggested_action;
1660 gboolean valid_location = FALSE;
1662 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (widget);
1664 if (priv->timer_expander != 0) {
1665 g_source_remove (priv->timer_expander);
1666 priv->timer_expander = 0;
1669 gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
1674 /* Do not allow drops between folders */
1676 pos == GTK_TREE_VIEW_DROP_BEFORE ||
1677 pos == GTK_TREE_VIEW_DROP_AFTER) {
1678 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW (widget), NULL, 0);
1679 gdk_drag_status(context, 0, time);
1680 valid_location = FALSE;
1683 valid_location = TRUE;
1686 /* Expand the selected row after 1/2 second */
1687 if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), dest_row)) {
1688 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), dest_row, pos);
1689 priv->timer_expander = g_timeout_add (500, expand_row_timeout, widget);
1692 /* Select the desired action. By default we pick MOVE */
1693 suggested_action = GDK_ACTION_MOVE;
1695 if (context->actions == GDK_ACTION_COPY)
1696 gdk_drag_status(context, GDK_ACTION_COPY, time);
1697 else if (context->actions == GDK_ACTION_MOVE)
1698 gdk_drag_status(context, GDK_ACTION_MOVE, time);
1699 else if (context->actions & suggested_action)
1700 gdk_drag_status(context, suggested_action, time);
1702 gdk_drag_status(context, GDK_ACTION_DEFAULT, time);
1706 gtk_tree_path_free (dest_row);
1707 g_signal_stop_emission_by_name (widget, "drag-motion");
1708 return valid_location;
1712 /* Folder view drag types */
1713 const GtkTargetEntry folder_view_drag_types[] =
1715 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, MODEST_FOLDER_ROW },
1716 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_APP, MODEST_HEADER_ROW }
1720 * This function sets the treeview as a source and a target for dnd
1721 * events. It also connects all the requirede signals.
1724 setup_drag_and_drop (GtkTreeView *self)
1726 /* Set up the folder view as a dnd destination. Set only the
1727 highlight flag, otherwise gtk will have a different
1729 gtk_drag_dest_set (GTK_WIDGET (self),
1730 GTK_DEST_DEFAULT_HIGHLIGHT,
1731 folder_view_drag_types,
1732 G_N_ELEMENTS (folder_view_drag_types),
1733 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1735 g_signal_connect (G_OBJECT (self),
1736 "drag_data_received",
1737 G_CALLBACK (on_drag_data_received),
1741 /* Set up the treeview as a dnd source */
1742 gtk_drag_source_set (GTK_WIDGET (self),
1744 folder_view_drag_types,
1745 G_N_ELEMENTS (folder_view_drag_types),
1746 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1748 g_signal_connect (G_OBJECT (self),
1750 G_CALLBACK (on_drag_motion),
1753 g_signal_connect (G_OBJECT (self),
1755 G_CALLBACK (on_drag_data_get),
1758 g_signal_connect (G_OBJECT (self),
1760 G_CALLBACK (drag_drop_cb),
1765 * This function manages the navigation through the folders using the
1766 * keyboard or the hardware keys in the device
1769 on_key_pressed (GtkWidget *self,
1773 GtkTreeSelection *selection;
1775 GtkTreeModel *model;
1776 gboolean retval = FALSE;
1778 /* Up and Down are automatically managed by the treeview */
1779 if (event->keyval == GDK_Return) {
1780 /* Expand/Collapse the selected row */
1781 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
1782 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
1785 path = gtk_tree_model_get_path (model, &iter);
1787 if (gtk_tree_view_row_expanded (GTK_TREE_VIEW (self), path))
1788 gtk_tree_view_collapse_row (GTK_TREE_VIEW (self), path);
1790 gtk_tree_view_expand_row (GTK_TREE_VIEW (self), path, FALSE);
1791 gtk_tree_path_free (path);
1793 /* No further processing */
1801 * We listen to the changes in the local folder account name key,
1802 * because we want to show the right name in the view. The local
1803 * folder account name corresponds to the device name in the Maemo
1804 * version. We do this because we do not want to query gconf on each
1805 * tree view refresh. It's better to cache it and change whenever
1809 on_configuration_key_changed (ModestConf* conf,
1811 ModestConfEvent event,
1812 ModestFolderView *self)
1814 ModestFolderViewPrivate *priv;
1819 g_return_if_fail (MODEST_IS_FOLDER_VIEW (self));
1820 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
1822 if (!strcmp (key, MODEST_CONF_DEVICE_NAME)) {
1823 g_free (priv->local_account_name);
1825 if (event == MODEST_CONF_EVENT_KEY_UNSET)
1826 priv->local_account_name = g_strdup (MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME);
1828 priv->local_account_name = modest_conf_get_string (modest_runtime_get_conf(),
1829 MODEST_CONF_DEVICE_NAME, NULL);
1831 /* Force a redraw */
1832 #if GTK_CHECK_VERSION(2, 8, 0) /* gtk_tree_view_column_queue_resize is only available in GTK+ 2.8 */
1833 GtkTreeViewColumn * tree_column = gtk_tree_view_get_column (GTK_TREE_VIEW (self),
1834 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN);
1835 gtk_tree_view_column_queue_resize (tree_column);
1841 modest_folder_view_set_style (ModestFolderView *self,
1842 ModestFolderViewStyle style)
1844 ModestFolderViewPrivate *priv;
1846 g_return_if_fail (self);
1848 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
1850 priv->style = style;
1854 modest_folder_view_set_account_id_of_visible_server_account (ModestFolderView *self,
1855 const gchar *account_id)
1857 ModestFolderViewPrivate *priv;
1858 GtkTreeModel *model;
1860 g_return_if_fail (self);
1862 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
1864 /* This will be used by the filter_row callback,
1865 * to decided which rows to show: */
1866 if (priv->visible_account_id) {
1867 g_free (priv->visible_account_id);
1868 priv->visible_account_id = NULL;
1871 priv->visible_account_id = g_strdup (account_id);
1874 model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
1875 if (GTK_IS_TREE_MODEL_FILTER (model))
1876 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (model));
1878 /* Save settings to gconf */
1879 modest_widget_memory_save (modest_runtime_get_conf (), G_OBJECT(self),
1880 MODEST_CONF_FOLDER_VIEW_KEY);
1884 modest_folder_view_get_account_id_of_visible_server_account (ModestFolderView *self)
1886 ModestFolderViewPrivate *priv;
1888 g_return_val_if_fail (self, NULL);
1890 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
1892 return (const gchar *) priv->visible_account_id;
1896 find_inbox_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreeIter *inbox_iter)
1900 TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
1903 gtk_tree_model_get (model, iter,
1904 TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name,
1905 TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN,
1909 printf ("DEBUG: %s: name=%s, type=%d, TNY_FOLDER_TYPE_INBOX=%d\n",
1910 __FUNCTION__, name, type, TNY_FOLDER_TYPE_INBOX);
1913 gboolean result = FALSE;
1914 if (type == TNY_FOLDER_TYPE_INBOX) {
1916 } else if (type == TNY_FOLDER_TYPE_NORMAL) {
1917 /* tinymail's camel implementation only provides TNY_FOLDER_TYPE_NORMAL
1918 * when getting folders from the cache, before connectin, so we do
1919 * an extra check. We could fix this in tinymail, but it's easier
1922 if (strcmp (name, "Inbox") == 0)
1929 *inbox_iter = *iter;
1933 if (gtk_tree_model_iter_children (model, &child, iter)) {
1934 if (find_inbox_iter (model, &child, inbox_iter))
1938 } while (gtk_tree_model_iter_next (model, iter));
1944 modest_folder_view_select_first_inbox_or_local (ModestFolderView *self)
1946 GtkTreeModel *model;
1947 GtkTreeIter iter, inbox_iter;
1948 GtkTreeSelection *sel;
1950 /* /\* Do not set it if the folder view was not painted *\/ */
1951 /* if (!GTK_WIDGET_MAPPED (self)) */
1954 model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
1958 expand_root_items (self);
1959 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
1961 gtk_tree_model_get_iter_first (model, &iter);
1962 if (find_inbox_iter (model, &iter, &inbox_iter)) {
1963 gtk_tree_selection_select_iter (sel, &inbox_iter);
1966 gtk_tree_model_get_iter_first (model, &iter);
1967 gtk_tree_selection_select_iter (sel, &iter);
1972 modest_folder_view_copy_selection (ModestFolderView *folder_view)
1974 /* Copy selection */
1975 _clipboard_set_selected_data (folder_view, FALSE);
1979 modest_folder_view_cut_selection (ModestFolderView *folder_view)
1981 ModestFolderViewPrivate *priv = NULL;
1982 GtkTreeModel *model = NULL;
1983 const gchar **hidding = NULL;
1984 guint i, n_selected;
1986 g_return_if_fail (MODEST_IS_FOLDER_VIEW (folder_view));
1987 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (folder_view);
1989 /* Copy selection */
1990 if (!_clipboard_set_selected_data (folder_view, TRUE))
1993 /* Get hidding ids */
1994 hidding = modest_email_clipboard_get_hidding_ids (priv->clipboard, &n_selected);
1996 /* Clear hidding array created by previous cut operation */
1997 _clear_hidding_filter (MODEST_FOLDER_VIEW (folder_view));
1999 /* Copy hidding array */
2000 priv->n_selected = n_selected;
2001 priv->hidding_ids = g_malloc0(sizeof(gchar *) * n_selected);
2002 for (i=0; i < n_selected; i++)
2003 priv->hidding_ids[i] = g_strdup(hidding[i]);
2005 /* Hide cut folders */
2006 model = gtk_tree_view_get_model (GTK_TREE_VIEW (folder_view));
2007 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (model));
2010 /* Returns FALSE if it did not selected anything */
2012 _clipboard_set_selected_data (ModestFolderView *folder_view,
2015 ModestFolderViewPrivate *priv = NULL;
2016 TnyFolderStore *folder = NULL;
2017 gboolean retval = FALSE;
2019 g_return_val_if_fail (MODEST_IS_FOLDER_VIEW (folder_view), FALSE);
2020 priv = MODEST_FOLDER_VIEW_GET_PRIVATE (folder_view);
2022 /* Set selected data on clipboard */
2023 g_return_val_if_fail (MODEST_IS_EMAIL_CLIPBOARD (priv->clipboard), FALSE);
2024 folder = modest_folder_view_get_selected (folder_view);
2026 /* Do not allow to select an account */
2027 if (TNY_IS_FOLDER (folder)) {
2028 modest_email_clipboard_set_data (priv->clipboard, TNY_FOLDER(folder), NULL, delete);
2033 g_object_unref (folder);
2039 _clear_hidding_filter (ModestFolderView *folder_view)
2041 ModestFolderViewPrivate *priv;
2044 g_return_if_fail (MODEST_IS_FOLDER_VIEW (folder_view));
2045 priv = MODEST_FOLDER_VIEW_GET_PRIVATE(folder_view);
2047 if (priv->hidding_ids != NULL) {
2048 for (i=0; i < priv->n_selected; i++)
2049 g_free (priv->hidding_ids[i]);
2050 g_free(priv->hidding_ids);