X-Git-Url: http://git.maemo.org/git/?p=modest;a=blobdiff_plain;f=src%2Fwidgets%2Fmodest-folder-view.c;h=7e1cff1c18bc9c6117840f40fde5bea602ee885a;hp=d034076df059c44d52dc37ae43fb3096d6916015;hb=96d9a8c787c693406a0f26e3d3a00065c7236fe5;hpb=380576d30a614bf98bf1b108581cbca085aac772 diff --git a/src/widgets/modest-folder-view.c b/src/widgets/modest-folder-view.c index d034076..7e1cff1 100644 --- a/src/widgets/modest-folder-view.c +++ b/src/widgets/modest-folder-view.c @@ -29,7 +29,7 @@ #include #include - +#include #include #include #include @@ -40,14 +40,21 @@ #include #include #include +#include #include +#include #include #include #include +#include #include #include #include "modest-folder-view.h" #include +#include +#include +#include + /* 'private'/'protected' functions */ static void modest_folder_view_class_init (ModestFolderViewClass *klass); @@ -77,6 +84,19 @@ static gint cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter2, gpointer user_data); +static gboolean filter_row (GtkTreeModel *model, + GtkTreeIter *iter, + gpointer data); + +static gboolean on_key_pressed (GtkWidget *self, + GdkEventKey *event, + gpointer user_data); + +static void on_configuration_key_changed (ModestConf* conf, + const gchar *key, + ModestConfEvent event, + ModestFolderView *self); + /* DnD functions */ static void on_drag_data_get (GtkWidget *widget, GdkDragContext *context, @@ -107,22 +127,26 @@ static void setup_drag_and_drop (GtkTreeView *self); enum { FOLDER_SELECTION_CHANGED_SIGNAL, + FOLDER_DISPLAY_NAME_CHANGED_SIGNAL, LAST_SIGNAL }; typedef struct _ModestFolderViewPrivate ModestFolderViewPrivate; struct _ModestFolderViewPrivate { - TnyAccountStore *account_store; - TnyFolder *cur_folder; - GtkTreeRowReference *cur_row; + TnyAccountStore *account_store; + TnyFolderStore *cur_folder_store; - gulong account_update_signal; - gulong changed_signal; - gulong accounts_reloaded_signal; + gulong account_update_signal; + gulong changed_signal; + gulong accounts_reloaded_signal; - GtkTreeSelection *cur_selection; - TnyFolderStoreQuery *query; - guint timer_expander; + GtkTreeSelection *cur_selection; + TnyFolderStoreQuery *query; + guint timer_expander; + + gchar *local_account_name; + gchar *visible_account_id; + ModestFolderViewStyle style; }; #define MODEST_FOLDER_VIEW_GET_PRIVATE(o) \ (G_TYPE_INSTANCE_GET_PRIVATE((o), \ @@ -190,190 +214,301 @@ modest_folder_view_class_init (ModestFolderViewClass *klass) NULL, NULL, modest_marshal_VOID__POINTER_BOOLEAN, G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_BOOLEAN); -} - + /* + * This signal is emitted whenever the currently selected + * folder display name is computed. Note that the name could + * be different to the folder name, because we could append + * the unread messages count to the folder name to build the + * folder display name + */ + signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL] = + g_signal_new ("folder-display-name-changed", + G_TYPE_FROM_CLASS (gobject_class), + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (ModestFolderViewClass, + folder_display_name_changed), + NULL, NULL, + g_cclosure_marshal_VOID__STRING, + G_TYPE_NONE, 1, G_TYPE_STRING); +} static void text_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer, GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data) { + ModestFolderViewPrivate *priv; GObject *rendobj; gchar *fname = NULL; - gint unread; + gint unread, all; TnyFolderType type; - TnyFolder *folder = NULL; + GObject *instance = NULL; g_return_if_fail (column); g_return_if_fail (tree_model); gtk_tree_model_get (tree_model, iter, TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname, + TNY_GTK_FOLDER_STORE_TREE_MODEL_ALL_COLUMN, &all, TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN, &unread, TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type, - TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder, + TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance, -1); rendobj = G_OBJECT(renderer); if (!fname) return; + + if (!instance) { + g_free (fname); + return; + } + + + priv = MODEST_FOLDER_VIEW_GET_PRIVATE (data); + + gchar *item_name = NULL; + gint item_weight = 400; - if (folder && type != TNY_FOLDER_TYPE_ROOT) { /* FIXME: tnymail bug? crashes with root folders */ - if (modest_tny_folder_is_local_folder (folder)) { + if (type != TNY_FOLDER_TYPE_ROOT) { + gint number = 0; + + if (modest_tny_folder_is_local_folder (TNY_FOLDER (instance))) { TnyFolderType type; - type = modest_tny_folder_get_local_folder_type (folder); + type = modest_tny_folder_get_local_folder_type (TNY_FOLDER (instance)); if (type != TNY_FOLDER_TYPE_UNKNOWN) { g_free (fname); fname = g_strdup(modest_local_folder_info_get_type_display_name (type)); } } - } else if (folder && type == TNY_FOLDER_TYPE_ROOT) { - /* FIXME: todo */ - } - - if (unread > 0) { - gchar *folder_title = g_strdup_printf ("%s (%d)", fname, unread); - g_object_set (rendobj,"text", folder_title, "weight", 800, NULL); - g_free (folder_title); - } else - g_object_set (rendobj,"text", fname, "weight", 400, NULL); - - g_free (fname); - if (folder) g_object_unref (G_OBJECT (folder)); -} + /* Select the number to show */ + if ((type == TNY_FOLDER_TYPE_DRAFTS) || (type == TNY_FOLDER_TYPE_OUTBOX)) + number = all; + else + number = unread; + + /* Use bold font style if there are unread messages */ + if (unread > 0) { + item_name = g_strdup_printf ("%s (%d)", fname, unread); + item_weight = 800; + } else { + item_name = g_strdup (fname); + item_weight = 400; + } -static GdkPixbuf* -get_cached_icon (const gchar *name) -{ - GError *err = NULL; - gpointer pixbuf; - gpointer orig_key; - static GHashTable *icon_cache = NULL; - - g_return_val_if_fail (name, NULL); + } else if (TNY_IS_ACCOUNT (instance)) { + /* If it's a server account */ + const gchar * account_id = tny_account_get_id (TNY_ACCOUNT (instance)); + if (!strcmp (account_id, MODEST_ACTUAL_LOCAL_FOLDERS_ACCOUNT_ID)) { + item_name = g_strdup (priv->local_account_name); + } else { + if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID)) { + /* TODO: get MMC card name */ + item_name = g_strdup (_("MMC")); + } else { + item_name = g_strdup (fname); + } + } - if (G_UNLIKELY(!icon_cache)) - icon_cache = modest_cache_mgr_get_cache (modest_runtime_get_cache_mgr(), - MODEST_CACHE_MGR_CACHE_TYPE_PIXBUF); + item_weight = 800; + } else if (modest_tny_folder_store_is_virtual_local_folders ( + TNY_FOLDER_STORE(instance))) + { + /* We use ModestTnySimpleFolder store to group the outboxes and + * the other local folders together: */ + item_name = g_strdup (_("Local Folders")); + item_weight = 400; + } - if (!icon_cache || !g_hash_table_lookup_extended (icon_cache, name, &orig_key, &pixbuf)) { - pixbuf = (gpointer)gdk_pixbuf_new_from_file (name, &err); - if (!pixbuf) { - g_printerr ("modest: error in icon factory while loading '%s': %s\n", - name, err->message); - g_error_free (err); + if (!item_name) + item_name = g_strdup ("unknown"); + + if (item_name && item_weight) { + /* Set the name in the treeview cell: */ + g_object_set (rendobj,"text", item_name, "weight", item_weight, NULL); + + /* Notify display name observers */ + if (G_OBJECT (priv->cur_folder_store) == instance) { + g_signal_emit (G_OBJECT(data), + signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0, + item_name); } - /* if we cannot find it, we still insert (if we have a cache), so we get the error - * only once */ - if (icon_cache) - g_hash_table_insert (icon_cache, g_strdup(name),(gpointer)pixbuf); + + g_free (item_name); + } - return GDK_PIXBUF(pixbuf); + + g_object_unref (G_OBJECT (instance)); + g_free (fname); } + static void icon_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer, GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data) { - GObject *rendobj; - GdkPixbuf *pixbuf; + GObject *rendobj = NULL, *instance = NULL; + GdkPixbuf *pixbuf = NULL; TnyFolderType type; gchar *fname = NULL; + const gchar *account_id = NULL; gint unread; rendobj = G_OBJECT(renderer); gtk_tree_model_get (tree_model, iter, TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type, TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname, - TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN, &unread, + TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN, &unread, + TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance, -1); - rendobj = G_OBJECT(renderer); + + if (!fname) + return; + + if (!instance) { + g_free (fname); + return; + } if (type == TNY_FOLDER_TYPE_NORMAL || type == TNY_FOLDER_TYPE_UNKNOWN) { type = modest_tny_folder_guess_folder_type_from_name (fname); } - g_free (fname); switch (type) { case TNY_FOLDER_TYPE_ROOT: - pixbuf = get_cached_icon (MODEST_FOLDER_ICON_ACCOUNT); - break; + if (TNY_IS_ACCOUNT (instance)) { + account_id = tny_account_get_id (TNY_ACCOUNT (instance)); + /* + if (!strcmp (account_id, MODEST_ACTUAL_LOCAL_FOLDERS_ACCOUNT_ID)) { + pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_LOCAL_FOLDERS); + } else { + */ + if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID)) + pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_MMC); + else + pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_ACCOUNT); + /* + } + */ + } + else if (modest_tny_folder_store_is_virtual_local_folders ( + TNY_FOLDER_STORE (instance))) { + pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_LOCAL_FOLDERS); + } + break; case TNY_FOLDER_TYPE_INBOX: - pixbuf = get_cached_icon (MODEST_FOLDER_ICON_INBOX); - break; - case TNY_FOLDER_TYPE_OUTBOX: - pixbuf = get_cached_icon (MODEST_FOLDER_ICON_OUTBOX); - break; - case TNY_FOLDER_TYPE_JUNK: - pixbuf = get_cached_icon (MODEST_FOLDER_ICON_JUNK); - break; - case TNY_FOLDER_TYPE_SENT: - pixbuf = get_cached_icon (MODEST_FOLDER_ICON_SENT); - break; + pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_INBOX); + break; + case TNY_FOLDER_TYPE_OUTBOX: + pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_OUTBOX); + break; + case TNY_FOLDER_TYPE_JUNK: + pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_JUNK); + break; + case TNY_FOLDER_TYPE_SENT: + pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_SENT); + break; case TNY_FOLDER_TYPE_TRASH: - pixbuf = get_cached_icon (MODEST_FOLDER_ICON_TRASH); - break; + pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_TRASH); + break; case TNY_FOLDER_TYPE_DRAFTS: - pixbuf = get_cached_icon (MODEST_FOLDER_ICON_DRAFTS); - break; - case TNY_FOLDER_TYPE_NOTES: - pixbuf = get_cached_icon (MODEST_FOLDER_ICON_NOTES); - break; - case TNY_FOLDER_TYPE_CALENDAR: - pixbuf = get_cached_icon (MODEST_FOLDER_ICON_CALENDAR); - break; - case TNY_FOLDER_TYPE_CONTACTS: - pixbuf = get_cached_icon (MODEST_FOLDER_ICON_CONTACTS); - break; + pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_DRAFTS); + break; case TNY_FOLDER_TYPE_NORMAL: - default: - pixbuf = get_cached_icon (MODEST_FOLDER_ICON_NORMAL); + default: + pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_NORMAL); break; - } + } + + g_object_unref (G_OBJECT (instance)); + g_free (fname); + + /* Set pixbuf */ g_object_set (rendobj, "pixbuf", pixbuf, NULL); + + if (pixbuf != NULL) + g_object_unref (pixbuf); } static void -modest_folder_view_init (ModestFolderView *obj) +add_columns (GtkWidget *treeview) { - ModestFolderViewPrivate *priv; GtkTreeViewColumn *column; GtkCellRenderer *renderer; GtkTreeSelection *sel; - - priv = MODEST_FOLDER_VIEW_GET_PRIVATE(obj); - - priv->timer_expander = 0; - priv->account_store = NULL; - priv->cur_folder = NULL; - priv->cur_row = NULL; - priv->query = NULL; + /* Create column */ column = gtk_tree_view_column_new (); - gtk_tree_view_append_column (GTK_TREE_VIEW(obj),column); + /* Set icon and text render function */ renderer = gtk_cell_renderer_pixbuf_new(); gtk_tree_view_column_pack_start (column, renderer, FALSE); gtk_tree_view_column_set_cell_data_func(column, renderer, - icon_cell_data, NULL, NULL); + icon_cell_data, treeview, NULL); renderer = gtk_cell_renderer_text_new(); gtk_tree_view_column_pack_start (column, renderer, FALSE); gtk_tree_view_column_set_cell_data_func(column, renderer, - text_cell_data, NULL, NULL); + text_cell_data, treeview, NULL); - sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(obj)); + /* Set selection mode */ + sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(treeview)); gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE); + /* Set treeview appearance */ gtk_tree_view_column_set_spacing (column, 2); gtk_tree_view_column_set_resizable (column, TRUE); gtk_tree_view_column_set_fixed_width (column, TRUE); - gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(obj), FALSE); - gtk_tree_view_set_enable_search (GTK_TREE_VIEW(obj), FALSE); + gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(treeview), FALSE); + gtk_tree_view_set_enable_search (GTK_TREE_VIEW(treeview), FALSE); + + /* Add column */ + gtk_tree_view_append_column (GTK_TREE_VIEW(treeview),column); +} + +static void +modest_folder_view_init (ModestFolderView *obj) +{ + ModestFolderViewPrivate *priv; + ModestConf *conf; + + priv = MODEST_FOLDER_VIEW_GET_PRIVATE(obj); + + priv->timer_expander = 0; + priv->account_store = NULL; + priv->query = NULL; + priv->style = MODEST_FOLDER_VIEW_STYLE_SHOW_ALL; + priv->cur_folder_store = NULL; + priv->visible_account_id = NULL; + + /* Initialize the local account name */ + conf = modest_runtime_get_conf(); + priv->local_account_name = modest_conf_get_string (conf, MODEST_CONF_DEVICE_NAME, NULL); + /* Build treeview */ + add_columns (GTK_WIDGET (obj)); + + /* Setup drag and drop */ setup_drag_and_drop (GTK_TREE_VIEW(obj)); + + /* Restore conf */ + modest_widget_memory_restore (conf, G_OBJECT (obj), MODEST_CONF_FOLDER_VIEW_KEY); + + /* Connect signals */ + g_signal_connect (G_OBJECT (obj), + "key-press-event", + G_CALLBACK (on_key_pressed), NULL); + + /* + * Track changes in the local account name (in the device it + * will be the device name) + */ + g_signal_connect (G_OBJECT(conf), + "key_changed", + G_CALLBACK(on_configuration_key_changed), obj); + } static void @@ -418,6 +553,9 @@ modest_folder_view_finalize (GObject *obj) sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(obj)); if (sel) g_signal_handler_disconnect (G_OBJECT(sel), priv->changed_signal); + + g_free (priv->local_account_name); + g_free (priv->visible_account_id); G_OBJECT_CLASS(parent_class)->finalize (obj); } @@ -512,11 +650,13 @@ modest_folder_view_new (TnyFolderStoreQuery *query) self = G_OBJECT (g_object_new (MODEST_TYPE_FOLDER_VIEW, NULL)); priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self); - priv->query = g_object_ref (query); + if (query) + priv->query = g_object_ref (query); sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self)); priv->changed_signal = g_signal_connect (sel, "changed", G_CALLBACK (on_selection_changed), self); + return GTK_WIDGET(self); } @@ -534,13 +674,158 @@ expand_root_items (ModestFolderView *self) gtk_tree_path_free (path); } +/* + * We use this function to implement the + * MODEST_FOLDER_VIEW_STYLE_SHOW_ONE style. We only show the default + * account in this case, and the local folders. + */ +static gboolean +filter_row (GtkTreeModel *model, + GtkTreeIter *iter, + gpointer data) +{ + gboolean retval = TRUE; + gint type = 0; + GObject *instance = NULL; + + gtk_tree_model_get (model, iter, + TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type, + TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance, + -1); + + if (type == TNY_FOLDER_TYPE_ROOT) { + /* TNY_FOLDER_TYPE_ROOT means that the instance is an account instead of a folder. */ + if (TNY_IS_ACCOUNT (instance)) { + TnyAccount *acc = TNY_ACCOUNT (instance); + const gchar *account_id = tny_account_get_id (acc); + + /* If it isn't a special folder, + * don't show it unless it is the visible account: */ + if (strcmp (account_id, MODEST_ACTUAL_LOCAL_FOLDERS_ACCOUNT_ID) && + strcmp (account_id, MODEST_MMC_ACCOUNT_ID)) { + + /* TODO: Merge the folders before we get to this point? */ + + /* Show only the visible account id */ + /* + ModestFolderViewPrivate *priv = + MODEST_FOLDER_VIEW_GET_PRIVATE (data); + if (priv->visible_account_id && strcmp (account_id, priv->visible_account_id)) + retval = FALSE; + */ + } + } + } + + g_object_unref (instance); + + return retval; +} + +/* +static void on_tnylist_accounts_debug_print(gpointer data, gpointer user_data) +{ + TnyAccount* account = TNY_ACCOUNT(data); + const gchar *prefix = (const gchar*)(user_data); + + printf("%s account id=%s\n", prefix, tny_account_get_id (account)); +} +*/ + +static void +add_account_folders_to_merged_folder (TnyAccount *account, TnyMergeFolder* merge_folder) +{ + const gchar* account_id = tny_account_get_id (account); + const gboolean is_actual_local_folders_account = account_id && + (strcmp (account_id, MODEST_ACTUAL_LOCAL_FOLDERS_ACCOUNT_ID) == 0); + + TnyList *list_outbox_folders = tny_simple_list_new (); + tny_folder_store_get_folders (TNY_FOLDER_STORE (account), + list_outbox_folders, NULL, NULL); + + TnyIterator* iter = tny_list_create_iterator (list_outbox_folders); + while (!tny_iterator_is_done (iter)) + { + TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (iter)); + + if (folder) { + gboolean add = TRUE; + /* TODO: Do not add outboxes that are inside local-folders/, + * because these are just left-over from earlier Modest versions + * that put the outbox there: */ + if (is_actual_local_folders_account) { + const TnyFolderType type = modest_tny_folder_get_local_folder_type (folder); + if (type == TNY_FOLDER_TYPE_OUTBOX) { + add = FALSE; + } + } + + if (add) + tny_merge_folder_add_folder (merge_folder, folder); + + g_object_unref (folder); + } + + tny_iterator_next (iter); + } + + g_object_unref (list_outbox_folders); +} + + +static void +add_account_folders_to_simple_folder_store (TnyAccount *account, ModestTnySimpleFolderStore* store) +{ + g_return_if_fail (account); + g_return_if_fail (store); + + TnyList *list_outbox_folders = tny_simple_list_new (); + tny_folder_store_get_folders (TNY_FOLDER_STORE (account), + list_outbox_folders, NULL, NULL); + + /* Special handling for the .modest/local-folders account, + * to avoid adding unwanted folders. + * We cannot prevent them from being in the TnyAccount without + * changing the libtinymail-camel. */ + const gchar* account_id = tny_account_get_id (account); + const gboolean is_actual_local_folders_account = account_id && + (strcmp (account_id, MODEST_ACTUAL_LOCAL_FOLDERS_ACCOUNT_ID) == 0); + + TnyIterator* iter = tny_list_create_iterator (list_outbox_folders); + while (!tny_iterator_is_done (iter)) + { + TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (iter)); + + if (folder) { + gboolean add = TRUE; + /* TODO: Do not add outboxes that are inside local-folders/, + * because these are just left-over from earlier Modest versions + * that put the outbox there: */ + if (is_actual_local_folders_account) { + const TnyFolderType type = modest_tny_folder_get_local_folder_type (folder); + if (type == TNY_FOLDER_TYPE_OUTBOX) { + add = FALSE; + } + } + + if (add) + modest_tny_simple_folder_store_add_folder (store, folder); + + g_object_unref (folder); + } + + tny_iterator_next (iter); + } + + g_object_unref (list_outbox_folders); +} + static gboolean update_model (ModestFolderView *self, ModestTnyAccountStore *account_store) { ModestFolderViewPrivate *priv; - TnyList *account_list; - GtkTreeModel *model, *sortable; + GtkTreeModel *model; g_return_val_if_fail (account_store, FALSE); @@ -555,26 +840,98 @@ update_model (ModestFolderView *self, ModestTnyAccountStore *account_store) selects only the subscribed folders. */ /* model = tny_gtk_folder_store_tree_model_new (TRUE, priv->query); */ model = tny_gtk_folder_store_tree_model_new (TRUE, NULL); - account_list = TNY_LIST(model); - + + /* Deal with the model via its TnyList Interface, + * filling the TnyList via a get_accounts() call: */ + TnyList *model_as_list = TNY_LIST(model); + + /* Create a virtual local-folders folder store, + * containing the real local folders and the (merged) various per-account + * outbox folders: + */ + ModestTnySimpleFolderStore *store = modest_tny_simple_folder_store_new (); + + /* Get the accounts: */ + TnyList *account_list = tny_simple_list_new (); tny_account_store_get_accounts (TNY_ACCOUNT_STORE(account_store), account_list, - TNY_ACCOUNT_STORE_STORE_ACCOUNTS); - if (account_list) { - sortable = gtk_tree_model_sort_new_with_model (model); - gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(sortable), - TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, - GTK_SORT_ASCENDING); - gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (sortable), - TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, - cmp_rows, NULL, NULL); - - /* Set new model */ - gtk_tree_view_set_model (GTK_TREE_VIEW(self), sortable); - expand_root_items (self); /* expand all account folders */ - g_object_unref (account_list); + TNY_ACCOUNT_STORE_STORE_ACCOUNTS); + TnyIterator* iter = tny_list_create_iterator (account_list); + + /* All per-account outbox folders are merged into one folders + * so that they appear as one outbox to the user: */ + TnyMergeFolder *merged_outbox = TNY_MERGE_FOLDER (tny_merge_folder_new()); + + while (!tny_iterator_is_done (iter)) + { + GObject *cur = tny_iterator_get_current (iter); + TnyAccount *account = TNY_ACCOUNT (cur); + if (account) { + /* Add both outbox account and local-folders account folders + * to our one combined account: + */ + if (MODEST_IS_TNY_OUTBOX_ACCOUNT (account)) { + /* Add the folder to the merged folder. + * We will add it later to the virtual local-folders store: */ + add_account_folders_to_merged_folder (account, merged_outbox); + } else { + const gchar *account_id = tny_account_get_id (account); + if (account_id && !strcmp (account_id, MODEST_ACTUAL_LOCAL_FOLDERS_ACCOUNT_ID)) { + /* Add the folders to the virtual local-folders store: */ + add_account_folders_to_simple_folder_store (account, store); + } + else { + /* Just add the account: */ + tny_list_append (model_as_list, G_OBJECT (account)); + } + } + } + + g_object_unref (cur); + tny_iterator_next (iter); } + /* Add the merged outbox folder to the virtual local-folders store: */ + modest_tny_simple_folder_store_add_folder (store, TNY_FOLDER(merged_outbox)); + g_object_unref (merged_outbox); + merged_outbox = NULL; + + /* Add the virtual local-folders store to the model: */ + tny_list_append (model_as_list, G_OBJECT (store)); + + + g_object_unref (account_list); + account_list = NULL; + + g_object_unref (model_as_list); + model_as_list = NULL; + + /* tny_list_foreach (account_list, on_tnylist_accounts_debug_print, "update_model: "); */ + + GtkTreeModel *filter_model = NULL, *sortable = NULL; + + sortable = gtk_tree_model_sort_new_with_model (model); + gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(sortable), + TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, + GTK_SORT_ASCENDING); + gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (sortable), + TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, + cmp_rows, NULL, NULL); + + /* Create filter model */ + if (priv->style == MODEST_FOLDER_VIEW_STYLE_SHOW_ONE) { + filter_model = gtk_tree_model_filter_new (sortable, NULL); + gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (filter_model), + filter_row, + self, + NULL); + } + + /* Set new model */ + gtk_tree_view_set_model (GTK_TREE_VIEW(self), + (filter_model) ? filter_model : sortable); + expand_root_items (self); /* expand all account folders */ + return TRUE; } @@ -582,10 +939,9 @@ update_model (ModestFolderView *self, ModestTnyAccountStore *account_store) static void on_selection_changed (GtkTreeSelection *sel, gpointer user_data) { - GtkTreeModel *model_sort, *model; - TnyFolder *folder = NULL; - GtkTreeIter iter, iter_sort; - GtkTreePath *path; + GtkTreeModel *model; + TnyFolderStore *folder = NULL; + GtkTreeIter iter; ModestFolderView *tree_view; ModestFolderViewPrivate *priv; gint type; @@ -597,18 +953,18 @@ on_selection_changed (GtkTreeSelection *sel, gpointer user_data) priv->cur_selection = sel; /* folder was _un_selected if true */ - if (!gtk_tree_selection_get_selected (sel, &model_sort, &iter_sort)) { - priv->cur_folder = NULL; /* FIXME: need this? */ - gtk_tree_row_reference_free (priv->cur_row); - priv->cur_row = NULL; - return; + if (!gtk_tree_selection_get_selected (sel, &model, &iter)) { + if (priv->cur_folder_store) + g_object_unref (priv->cur_folder_store); + priv->cur_folder_store = NULL; + + /* Notify the display name observers */ + g_signal_emit (G_OBJECT(user_data), + signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0, + NULL); + return; } - model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort)); - gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (model_sort), - &iter, - &iter_sort); - tree_view = MODEST_FOLDER_VIEW (user_data); gtk_tree_model_get (model, &iter, @@ -616,36 +972,29 @@ on_selection_changed (GtkTreeSelection *sel, gpointer user_data) TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder, -1); - if (type == TNY_FOLDER_TYPE_ROOT) { + /* If the folder is the same do not notify */ + if (priv->cur_folder_store == folder) { g_object_unref (folder); return; } /* Current folder was unselected */ - g_signal_emit (G_OBJECT(tree_view), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0, - priv->cur_folder, FALSE); - - if (priv->cur_row) { -/* tny_folder_sync (priv->cur_folder, TRUE, NULL); /\* FIXME *\/ */ - gtk_tree_row_reference_free (priv->cur_row); + if (priv->cur_folder_store) { + g_signal_emit (G_OBJECT(tree_view), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0, + priv->cur_folder_store, FALSE); + g_object_unref (priv->cur_folder_store); } /* New current references */ - path = gtk_tree_model_get_path (model_sort, &iter_sort); - priv->cur_folder = folder; - priv->cur_row = gtk_tree_row_reference_new (model_sort, path); - - /* Frees */ - gtk_tree_path_free (path); - g_object_unref (G_OBJECT (folder)); + priv->cur_folder_store = folder; /* New folder has been selected */ - g_signal_emit (G_OBJECT(tree_view), - signals[FOLDER_SELECTION_CHANGED_SIGNAL], - 0, folder, TRUE); + g_signal_emit (G_OBJECT(tree_view), + signals[FOLDER_SELECTION_CHANGED_SIGNAL], + 0, folder, TRUE); } -TnyFolder * +TnyFolderStore * modest_folder_view_get_selected (ModestFolderView *self) { ModestFolderViewPrivate *priv; @@ -653,44 +1002,42 @@ modest_folder_view_get_selected (ModestFolderView *self) g_return_val_if_fail (self, NULL); priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self); - if (priv->cur_folder) - g_object_ref (priv->cur_folder); + if (priv->cur_folder_store) + g_object_ref (priv->cur_folder_store); - return priv->cur_folder; + return priv->cur_folder_store; } -/* static gboolean */ -/* get_model_iter (ModestFolderView *self, */ -/* GtkTreeModel **model, */ -/* GtkTreeIter *iter) */ -/* { */ -/* GtkTreeModel *model_sort; */ -/* GtkTreeIter iter_sort; */ -/* GtkTreePath *path; */ -/* ModestFolderViewPrivate *priv; */ - -/* priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self); */ - -/* if (!priv->cur_folder) */ -/* return FALSE; */ - -/* if (!gtk_tree_row_reference_valid (priv->cur_row)) */ -/* return FALSE; */ - -/* model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (self)); */ -/* *model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort)); */ - -/* /\* Get path to retrieve iter *\/ */ -/* path = gtk_tree_row_reference_get_path (priv->cur_row); */ -/* if (!gtk_tree_model_get_iter (model_sort, &iter_sort, path)) */ -/* return FALSE; */ - -/* gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (model_sort), */ -/* iter, */ -/* &iter_sort); */ -/* return TRUE; */ -/* } */ +static gint +get_cmp_rows_type_pos (GObject *folder) +{ + /* Remote accounts -> Local account -> MMC account .*/ + /* 0, 1, 2 */ + + if (TNY_IS_FOLDER_STORE (folder) && + modest_tny_folder_store_is_virtual_local_folders ( + TNY_FOLDER_STORE (folder))) { + return 1; + } else if (TNY_IS_ACCOUNT (folder)) { + TnyAccount *account = TNY_ACCOUNT (folder); + const gchar *account_id = tny_account_get_id (account); + if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID)) + return 2; + else + return 0; + } + else { + printf ("DEBUG: %s: unexpected type.\n", __FUNCTION__); + return -1; /* Should never happen */ + } +} +/* + * This function orders the mail accounts according to these rules: + * 1st - remote accounts + * 2nd - local account + * 3rd - MMC account + */ static gint cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2, gpointer user_data) @@ -698,8 +1045,9 @@ cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2, gint cmp; gchar *name1, *name2; TnyFolderType type; - TnyFolder *folder1, *folder2; - + GObject *folder1 = NULL; + GObject *folder2 = NULL; + gtk_tree_model_get (tree_model, iter1, TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name1, TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type, @@ -710,25 +1058,51 @@ cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2, TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder2, -1); - /* local_folders should be the last one */ if (type == TNY_FOLDER_TYPE_ROOT) { - /* the account name is also the name of the root folder - * in case of local folders */ - if (name1 && strcmp (name1, MODEST_LOCAL_FOLDERS_ACCOUNT_NAME) == 0) - cmp = +1; - else if (name2 && strcmp (name2, MODEST_LOCAL_FOLDERS_ACCOUNT_NAME) == 0) + /* Compare the types, so that + * Remote accounts -> Local account -> MMC account .*/ + const gint pos1 = get_cmp_rows_type_pos (folder1); + const gint pos2 = get_cmp_rows_type_pos (folder2); + /* printf ("DEBUG: %s:\n type1=%s, pos1=%d\n type2=%s, pos2=%d\n", + __FUNCTION__, G_OBJECT_TYPE_NAME(folder1), pos1, G_OBJECT_TYPE_NAME(folder2), pos2); */ + if (pos1 < pos2) cmp = -1; - else - cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE); - } else + else if (pos1 > pos2) + cmp = 1; + else { + /* Compare items of the same type: */ + + TnyAccount *account1 = NULL; + if (TNY_IS_ACCOUNT (folder1)) + account1 = TNY_ACCOUNT (folder1); + + TnyAccount *account2 = NULL; + if (TNY_IS_ACCOUNT (folder2)) + account2 = TNY_ACCOUNT (folder2); + + const gchar *account_id = account1 ? tny_account_get_id (account1) : NULL; + const gchar *account_id2 = account2 ? tny_account_get_id (account2) : NULL; + + if (!account_id && !account_id2) + return 0; + else if (!account_id) + return -1; + else if (!account_id2) + return +1; + else if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID)) + cmp = +1; + else + cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE); + } + } else { cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE); - + } if (folder1) g_object_unref(G_OBJECT(folder1)); if (folder2) g_object_unref(G_OBJECT(folder2)); - + g_free (name1); g_free (name2); @@ -753,32 +1127,23 @@ on_drag_data_get (GtkWidget *widget, gpointer data) { GtkTreeSelection *selection; - GtkTreeModel *model_sort, *model; + GtkTreeModel *model; GtkTreeIter iter; - GtkTreePath *source_row_sort; GtkTreePath *source_row; selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget)); - gtk_tree_selection_get_selected (selection, &model_sort, &iter); - source_row_sort = gtk_tree_model_get_path (model_sort, &iter); - - /* Get the unsorted path and model */ - model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort)); - source_row = gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort), - source_row_sort); + gtk_tree_selection_get_selected (selection, &model, &iter); + source_row = gtk_tree_model_get_path (model, &iter); gtk_tree_set_row_drag_data (selection_data, model, source_row); - gtk_tree_path_free (source_row_sort); gtk_tree_path_free (source_row); } typedef struct _DndHelper { gboolean delete_source; - GtkWidget *source_widget; - GtkWidget *dest_widget; GtkTreePath *source_row; GdkDragContext *context; guint time; @@ -786,61 +1151,38 @@ typedef struct _DndHelper { /* - * This function saves the source row in the source widget, will be - * used by the drag-data-delete handler to remove the source row - */ -static void -save_and_clean (DndHelper *helper, - gboolean success) -{ - /* Save row data */ - if (success && helper->delete_source) - g_object_set_data (G_OBJECT (helper->source_widget), - ROW_REF_DATA_NAME, - gtk_tree_path_copy (helper->source_row)); - - /* Clean dest row */ - gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (helper->dest_widget), - NULL, - GTK_TREE_VIEW_DROP_BEFORE); - -} - -/* * This function is the callback of the - * modest_mail_operation_xfer_msg() call. We check here if the message - * was correctly asynchronously transfered + * modest_mail_operation_xfer_msgs () and + * modest_mail_operation_xfer_folder() calls. We check here if the + * message/folder was correctly asynchronously transferred. The reason + * to use the same callback is that the code is the same, it only has + * to check that the operation went fine and then finalize the drag + * and drop action */ static void on_progress_changed (ModestMailOperation *mail_op, gpointer user_data) { - ModestMailOperationQueue *queue; - gboolean success = FALSE; + gboolean success; DndHelper *helper; helper = (DndHelper *) user_data; + if (!modest_mail_operation_is_finished (mail_op)) + return; + if (modest_mail_operation_get_status (mail_op) == MODEST_MAIL_OPERATION_STATUS_SUCCESS) { success = TRUE; } else { - const GError *error; - error = modest_mail_operation_get_error (mail_op); - g_warning ("Error transferring messages: %s\n", error->message); + success = FALSE; } - /* Remove the mail operation */ - queue = modest_runtime_get_mail_operation_queue (); - modest_mail_operation_queue_remove (queue, mail_op); - g_object_unref (G_OBJECT (mail_op)); - - /* Save and clean */ - save_and_clean (helper, success); - - /* Notify the drag source */ - gtk_drag_finish (helper->context, success, (success && helper->delete_source), helper->time); + /* Notify the drag source. Never call delete, the monitor will + do the job if needed */ + gtk_drag_finish (helper->context, success, FALSE, helper->time); /* Free the helper */ + gtk_tree_path_free (helper->source_row); g_slice_free (DndHelper, helper); } @@ -855,11 +1197,10 @@ drag_and_drop_from_header_view (GtkTreeModel *source_model, GtkTreePath *dest_row, DndHelper *helper) { + TnyList *headers; TnyHeader *header; TnyFolder *folder; - ModestMailOperationQueue *queue; ModestMailOperation *mail_op; - gboolean started; GtkTreeIter source_iter, dest_iter; /* Get header */ @@ -875,28 +1216,24 @@ drag_and_drop_from_header_view (GtkTreeModel *source_model, &folder, -1); /* Transfer message */ - queue = modest_runtime_get_mail_operation_queue (); - mail_op = modest_mail_operation_new (); - started = modest_mail_operation_xfer_msg (mail_op, header, - folder, helper->delete_source); - if (started) { - g_signal_connect (G_OBJECT (mail_op), "progress_changed", - G_CALLBACK (on_progress_changed), helper); - modest_mail_operation_queue_add (queue, mail_op); - } else { - const GError *error; - error = modest_mail_operation_get_error (mail_op); - if (error) - g_warning ("Error trying to transfer messages: %s\n", - error->message); + mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE, NULL); - g_slice_free (DndHelper, helper); - } + modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), + mail_op); + g_signal_connect (G_OBJECT (mail_op), "progress-changed", + G_CALLBACK (on_progress_changed), helper); + + /* FIXME: I replaced this because the API changed, but D&D + should be reviewed in order to allow multiple drags*/ + headers = tny_simple_list_new (); + tny_list_append (headers, G_OBJECT (header)); + modest_mail_operation_xfer_msgs (mail_op, headers, folder, helper->delete_source, NULL, NULL); /* Frees */ g_object_unref (G_OBJECT (mail_op)); g_object_unref (G_OBJECT (header)); g_object_unref (G_OBJECT (folder)); + g_object_unref (headers); } /* @@ -912,21 +1249,22 @@ drag_and_drop_from_folder_view (GtkTreeModel *source_model, DndHelper *helper) { ModestMailOperation *mail_op; - const GError *error; - GtkTreeRowReference *source_row_reference; GtkTreeIter parent_iter, iter; - TnyFolder *folder, *new_folder; TnyFolderStore *parent_folder; - gboolean success = FALSE; + TnyFolder *folder; /* Check if the drag is possible */ - if (!gtk_tree_path_compare (helper->source_row, dest_row)) - goto out; - - if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (dest_model), - dest_row, - selection_data)) - goto out; +/* if (!gtk_tree_path_compare (helper->source_row, dest_row) || */ +/* !gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (dest_model), */ +/* dest_row, */ +/* selection_data)) { */ + if (!gtk_tree_path_compare (helper->source_row, dest_row)) { + + gtk_drag_finish (helper->context, FALSE, FALSE, helper->time); + gtk_tree_path_free (helper->source_row); + g_slice_free (DndHelper, helper); + return; + } /* Get data */ gtk_tree_model_get_iter (source_model, &parent_iter, dest_row); @@ -939,54 +1277,21 @@ drag_and_drop_from_folder_view (GtkTreeModel *source_model, &folder, -1); /* Do the mail operation */ - mail_op = modest_mail_operation_new (); - new_folder = modest_mail_operation_xfer_folder (mail_op, folder, parent_folder, - helper->delete_source); - + mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE, NULL); + modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), + mail_op); + g_signal_connect (G_OBJECT (mail_op), "progress-changed", + G_CALLBACK (on_progress_changed), helper); + + modest_mail_operation_xfer_folder (mail_op, + folder, + parent_folder, + helper->delete_source); + + /* Frees */ g_object_unref (G_OBJECT (parent_folder)); g_object_unref (G_OBJECT (folder)); - - error = modest_mail_operation_get_error (mail_op); - if (error) { - g_warning ("Error transferring folder: %s\n", error->message); - g_object_unref (G_OBJECT (mail_op)); - goto out; - } g_object_unref (G_OBJECT (mail_op)); - - /* Get a row reference to the source path because the path - could change after the insertion. The gtk_drag_finish() is - not able to delete the source because that, so we have to - do it manually */ - source_row_reference = gtk_tree_row_reference_new (source_model, helper->source_row); - gtk_tree_path_free (helper->source_row); - - /* Insert the dragged row as a child of the dest row */ - gtk_tree_path_down (dest_row); - if (gtk_tree_drag_dest_drag_data_received (GTK_TREE_DRAG_DEST (dest_model), - dest_row, - selection_data)) { - - GtkTreeIter iter; - - /* Set the newly created folder as the instance in the row */ - gtk_tree_model_get_iter (dest_model, &iter, dest_row); - gtk_tree_store_set (GTK_TREE_STORE (dest_model), &iter, - TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, - new_folder, -1); - g_object_unref (G_OBJECT (new_folder)); - - helper->source_row = gtk_tree_row_reference_get_path (source_row_reference); - - success = TRUE; - } - gtk_tree_row_reference_free (source_row_reference); - - /* Save and clean */ - save_and_clean (helper, success); - - out: - gtk_drag_finish (helper->context, success, (success && helper->delete_source), helper->time); } /* @@ -1006,35 +1311,45 @@ on_drag_data_received (GtkWidget *widget, gpointer data) { GtkWidget *source_widget; - GtkTreeModel *model_sort, *dest_model, *source_model; - GtkTreePath *source_row, *dest_row, *child_dest_row; + GtkTreeModel *dest_model, *source_model; + GtkTreePath *source_row, *dest_row; GtkTreeViewDropPosition pos; gboolean success = FALSE, delete_source = FALSE; - DndHelper *helper; + DndHelper *helper = NULL; /* Do not allow further process */ g_signal_stop_emission_by_name (widget, "drag-data-received"); + source_widget = gtk_drag_get_source_widget (context); /* Get the action */ - if (context->action == GDK_ACTION_MOVE) + if (context->action == GDK_ACTION_MOVE) { delete_source = TRUE; + /* Notify that there is no folder selected. We need to + do this in order to update the headers view (and + its monitors, because when moving, the old folder + won't longer exist. We can not wait for the end of + the operation, because the operation won't start if + the folder is in use */ + if (source_widget == widget) + g_signal_emit (G_OBJECT (widget), + signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0, NULL, TRUE); + } + /* Check if the get_data failed */ if (selection_data == NULL || selection_data->length < 0) - gtk_drag_finish (context, success, (success && delete_source), time); + gtk_drag_finish (context, success, FALSE, time); /* Get the models */ - source_widget = gtk_drag_get_source_widget (context); gtk_tree_get_row_drag_data (selection_data, &source_model, &source_row); - model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (widget)); /* Select the destination model */ if (source_widget == widget) { dest_model = source_model; } else { - dest_model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort)); + dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget)); } /* Get the path to the destination row. Can not call @@ -1045,40 +1360,35 @@ on_drag_data_received (GtkWidget *widget, /* Only allow drops IN other rows */ if (!dest_row || pos == GTK_TREE_VIEW_DROP_BEFORE || pos == GTK_TREE_VIEW_DROP_AFTER) - gtk_drag_finish (context, success, (success && delete_source), time); + gtk_drag_finish (context, success, FALSE, time); /* Create the helper */ helper = g_slice_new0 (DndHelper); helper->delete_source = delete_source; - helper->source_widget = source_widget; - helper->dest_widget = widget; - helper->source_row = source_row; + helper->source_row = gtk_tree_path_copy (source_row); helper->context = context; helper->time = time; - /* Get path from the unsorted model */ - child_dest_row = - gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort), - dest_row); - gtk_tree_path_free (dest_row); - /* Drags from the header view */ if (source_widget != widget) { drag_and_drop_from_header_view (source_model, dest_model, - child_dest_row, + dest_row, helper); } else { drag_and_drop_from_folder_view (source_model, dest_model, - child_dest_row, + dest_row, selection_data, helper); } - gtk_tree_path_free (child_dest_row); + + /* Frees */ + gtk_tree_path_free (source_row); + gtk_tree_path_free (dest_row); } /* @@ -1111,31 +1421,6 @@ drag_drop_cb (GtkWidget *widget, } /* - * This function deletes the data that has been dragged from its - * source widget. Since is a function received by the source of the - * drag, this function only deletes rows of the folder view - * widget. The header view widget will need to define its own one. - */ -static void -drag_data_delete_cb (GtkWidget *widget, - GdkDragContext *context, - gpointer user_data) -{ - GtkTreePath *source_row; - GtkTreeModel *model_sort, *model; - - model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (widget)); - model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort)); - source_row = g_object_steal_data (G_OBJECT (widget), ROW_REF_DATA_NAME); - - /* Delete the source row */ - gtk_tree_drag_source_drag_data_delete (GTK_TREE_DRAG_SOURCE (model), - source_row); - - gtk_tree_path_free (source_row); -} - -/* * This function expands a node of a tree view if it's not expanded * yet. Not sure why it needs the threads stuff, but gtk+`example code * does that, so that's why they're here. @@ -1192,6 +1477,7 @@ on_drag_motion (GtkWidget *widget, GtkTreePath *dest_row; ModestFolderViewPrivate *priv; GdkDragAction suggested_action; + gboolean valid_location = FALSE; priv = MODEST_FOLDER_VIEW_GET_PRIVATE (widget); @@ -1205,15 +1491,23 @@ on_drag_motion (GtkWidget *widget, &dest_row, &pos); - if (!dest_row) - return FALSE; + /* Do not allow drops between folders */ + if (!dest_row || + pos == GTK_TREE_VIEW_DROP_BEFORE || + pos == GTK_TREE_VIEW_DROP_AFTER) { + gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW (widget), NULL, 0); + gdk_drag_status(context, 0, time); + valid_location = FALSE; + goto out; + } else { + valid_location = TRUE; + } /* Expand the selected row after 1/2 second */ if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), dest_row)) { gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), dest_row, pos); priv->timer_expander = g_timeout_add (500, expand_row_timeout, widget); } - gtk_tree_path_free (dest_row); /* Select the desired action. By default we pick MOVE */ suggested_action = GDK_ACTION_MOVE; @@ -1227,15 +1521,19 @@ on_drag_motion (GtkWidget *widget, else gdk_drag_status(context, GDK_ACTION_DEFAULT, time); - return TRUE; + out: + if (dest_row) + gtk_tree_path_free (dest_row); + g_signal_stop_emission_by_name (widget, "drag-motion"); + return valid_location; } /* Folder view drag types */ const GtkTargetEntry folder_view_drag_types[] = { - { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, FOLDER_ROW }, - { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_APP, HEADER_ROW } + { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, MODEST_FOLDER_ROW }, + { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_APP, MODEST_HEADER_ROW } }; /* @@ -1254,10 +1552,10 @@ setup_drag_and_drop (GtkTreeView *self) G_N_ELEMENTS (folder_view_drag_types), GDK_ACTION_MOVE | GDK_ACTION_COPY); - gtk_signal_connect(GTK_OBJECT (self), - "drag_data_received", - GTK_SIGNAL_FUNC(on_drag_data_received), - NULL); + g_signal_connect (G_OBJECT (self), + "drag_data_received", + G_CALLBACK (on_drag_data_received), + NULL); /* Set up the treeview as a dnd source */ @@ -1267,24 +1565,147 @@ setup_drag_and_drop (GtkTreeView *self) G_N_ELEMENTS (folder_view_drag_types), GDK_ACTION_MOVE | GDK_ACTION_COPY); - gtk_signal_connect(GTK_OBJECT (self), - "drag_data_delete", - GTK_SIGNAL_FUNC(drag_data_delete_cb), - NULL); + g_signal_connect (G_OBJECT (self), + "drag_motion", + G_CALLBACK (on_drag_motion), + NULL); + + g_signal_connect (G_OBJECT (self), + "drag_data_get", + G_CALLBACK (on_drag_data_get), + NULL); + + g_signal_connect (G_OBJECT (self), + "drag_drop", + G_CALLBACK (drag_drop_cb), + NULL); +} + +/* + * This function manages the navigation through the folders using the + * keyboard or the hardware keys in the device + */ +static gboolean +on_key_pressed (GtkWidget *self, + GdkEventKey *event, + gpointer user_data) +{ + GtkTreeSelection *selection; + GtkTreeIter iter; + GtkTreeModel *model; + gboolean retval = FALSE; + + /* Up and Down are automatically managed by the treeview */ + if (event->keyval == GDK_Return) { + /* Expand/Collapse the selected row */ + selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self)); + if (gtk_tree_selection_get_selected (selection, &model, &iter)) { + GtkTreePath *path; + + path = gtk_tree_model_get_path (model, &iter); + + if (gtk_tree_view_row_expanded (GTK_TREE_VIEW (self), path)) + gtk_tree_view_collapse_row (GTK_TREE_VIEW (self), path); + else + gtk_tree_view_expand_row (GTK_TREE_VIEW (self), path, FALSE); + gtk_tree_path_free (path); + } + /* No further processing */ + retval = TRUE; + } + + return retval; +} + +/* + * We listen to the changes in the local folder account name key, + * because we want to show the right name in the view. The local + * folder account name corresponds to the device name in the Maemo + * version. We do this because we do not want to query gconf on each + * tree view refresh. It's better to cache it and change whenever + * necessary. + */ +static void +on_configuration_key_changed (ModestConf* conf, + const gchar *key, + ModestConfEvent event, + ModestFolderView *self) +{ + ModestFolderViewPrivate *priv; + + if (!key) + return; + + g_return_if_fail (MODEST_IS_FOLDER_VIEW (self)); + priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self); + + if (!strcmp (key, MODEST_CONF_DEVICE_NAME)) { + g_free (priv->local_account_name); + + if (event == MODEST_CONF_EVENT_KEY_UNSET) + priv->local_account_name = g_strdup (MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME); + else + priv->local_account_name = modest_conf_get_string (modest_runtime_get_conf(), + MODEST_CONF_DEVICE_NAME, NULL); + + /* Force a redraw */ +#if GTK_CHECK_VERSION(2, 8, 0) /* gtk_tree_view_column_queue_resize is only available in GTK+ 2.8 */ + GtkTreeViewColumn * tree_column = gtk_tree_view_get_column (GTK_TREE_VIEW (self), + TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN); + gtk_tree_view_column_queue_resize (tree_column); +#endif + } +} + +void +modest_folder_view_set_style (ModestFolderView *self, + ModestFolderViewStyle style) +{ + ModestFolderViewPrivate *priv; + + g_return_if_fail (self); + + priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self); + + priv->style = style; +} + +void +modest_folder_view_set_account_id_of_visible_server_account (ModestFolderView *self, + const gchar *account_id) +{ + ModestFolderViewPrivate *priv; + ModestConf *conf; + GtkTreeModel *model; + + g_return_if_fail (self); + + priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self); + + /* This will be used by the filter_row callback, + * to decided which rows to show: */ + if (priv->visible_account_id) + g_free (priv->visible_account_id); + priv->visible_account_id = g_strdup (account_id); - gtk_signal_connect(GTK_OBJECT (self), - "drag_motion", - GTK_SIGNAL_FUNC(on_drag_motion), - NULL); + /* Save preferences */ + conf = modest_runtime_get_conf (); + modest_widget_memory_save (conf, G_OBJECT (self), MODEST_CONF_FOLDER_VIEW_KEY); + + /* Refilter */ + model = gtk_tree_view_get_model (GTK_TREE_VIEW (self)); + if (GTK_IS_TREE_MODEL_FILTER (model)) + gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (model)); +} +const gchar * +modest_folder_view_get_account_id_of_visible_server_account (ModestFolderView *self) +{ + ModestFolderViewPrivate *priv; - gtk_signal_connect(GTK_OBJECT (self), - "drag_data_get", - GTK_SIGNAL_FUNC(on_drag_data_get), - NULL); + g_return_val_if_fail (self, NULL); + + priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self); - gtk_signal_connect(GTK_OBJECT (self), - "drag_drop", - GTK_SIGNAL_FUNC(drag_drop_cb), - NULL); + return (const gchar *) priv->visible_account_id; }