+/*
+ * This function orders the mail accounts following the next rules
+ * 1st - remote accounts
+ * 2nd - local account
+ * 3rd - MMC account
+ */
+static gint
+cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2,
+ gpointer user_data)
+{
+ gint cmp;
+ gchar *name1, *name2;
+ TnyFolderType type;
+ TnyFolder *folder1, *folder2;
+
+ 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,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder1,
+ -1);
+ gtk_tree_model_get (tree_model, iter2,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name2,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder2,
+ -1);
+
+ /* Order must be: Remote accounts -> Local account -> MMC account */
+ if (type == TNY_FOLDER_TYPE_ROOT) {
+ const gchar *account_id = tny_account_get_id (TNY_ACCOUNT (folder1));
+ const gchar *account_id2 = tny_account_get_id (TNY_ACCOUNT (folder2));
+
+ if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID))
+ cmp = +1;
+ else {
+ if (!strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID)) {
+ if (!strcmp (account_id2, MODEST_MMC_ACCOUNT_ID))
+ cmp = -1;
+ else
+ cmp = +1;
+ } else {
+ if (!strcmp (account_id2, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) ||
+ !strcmp (account_id2, 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);
+
+ return cmp;
+}
+
+/*****************************************************************************/
+/* DRAG and DROP stuff */
+/*****************************************************************************/
+
+/*
+ * This function fills the #GtkSelectionData with the row and the
+ * model that has been dragged. It's called when this widget is a
+ * source for dnd after the event drop happened
+ */
+static void
+on_drag_data_get (GtkWidget *widget,
+ GdkDragContext *context,
+ GtkSelectionData *selection_data,
+ guint info,
+ guint time,
+ gpointer data)
+{
+ GtkTreeSelection *selection;
+ GtkTreeModel *model_sort, *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_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;
+ GtkTreePath *source_row;
+ GdkDragContext *context;
+ guint time;
+} DndHelper;
+
+
+/*
+ * This function is the callback of the
+ * 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)
+{
+ 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 {
+ success = FALSE;
+ }
+
+ /* 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);
+}
+
+/*
+ * This function is used by drag_data_received_cb to manage drag and
+ * drop of a header, i.e, and drag from the header view to the folder
+ * view.
+ */
+static void
+drag_and_drop_from_header_view (GtkTreeModel *source_model,
+ GtkTreeModel *dest_model,
+ GtkTreePath *dest_row,
+ DndHelper *helper)
+{
+ TnyList *headers;
+ TnyHeader *header;
+ TnyFolder *folder;
+ ModestMailOperation *mail_op;
+ GtkTreeIter source_iter, dest_iter;
+
+ /* Get header */
+ gtk_tree_model_get_iter (source_model, &source_iter, helper->source_row);
+ gtk_tree_model_get (source_model, &source_iter,
+ TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN,
+ &header, -1);
+
+ /* Get Folder */
+ gtk_tree_model_get_iter (dest_model, &dest_iter, dest_row);
+ gtk_tree_model_get (dest_model, &dest_iter,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
+ &folder, -1);
+
+ /* Transfer message */
+ 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);
+
+ /* 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));
+ g_object_unref (header);
+ modest_mail_operation_xfer_msgs (mail_op, headers, folder, helper->delete_source);
+
+ /* Frees */
+ g_object_unref (G_OBJECT (mail_op));
+ g_object_unref (G_OBJECT (header));
+ g_object_unref (G_OBJECT (folder));
+}
+
+/*
+ * This function is used by drag_data_received_cb to manage drag and
+ * drop of a folder, i.e, and drag from the folder view to the same
+ * folder view.
+ */
+static void
+drag_and_drop_from_folder_view (GtkTreeModel *source_model,
+ GtkTreeModel *dest_model,
+ GtkTreePath *dest_row,
+ GtkSelectionData *selection_data,
+ DndHelper *helper)
+{
+ ModestMailOperation *mail_op;
+ GtkTreeIter parent_iter, iter;
+ TnyFolderStore *parent_folder;
+ TnyFolder *folder;
+
+ /* Check if the drag is possible */
+ 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)) {
+
+ 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);
+ gtk_tree_model_get_iter (source_model, &iter, helper->source_row);
+ gtk_tree_model_get (source_model, &parent_iter,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
+ &parent_folder, -1);
+ gtk_tree_model_get (source_model, &iter,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
+ &folder, -1);
+
+ /* Do the mail operation */
+ 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));
+ g_object_unref (G_OBJECT (mail_op));
+}
+
+/*
+ * This function receives the data set by the "drag-data-get" signal
+ * handler. This information comes within the #GtkSelectionData. This
+ * function will manage both the drags of folders of the treeview and
+ * drags of headers of the header view widget.
+ */