+ /* 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)
+ cmp = -1;
+ else
+ cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
+ } else
+ cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
+
+ g_free (name1);
+ g_free (name2);
+
+ return cmp;
+}
+
+/*****************************************************************************/
+/* DRAG and DROP stuff */
+/*****************************************************************************/
+static void
+drag_data_get_cb (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);
+
+ 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);
+}
+
+static void
+drag_data_received_cb (GtkWidget *widget,
+ GdkDragContext *context,
+ gint x,
+ gint y,
+ GtkSelectionData *selection_data,
+ guint info,
+ guint time,
+ gpointer data)
+{
+ GtkTreeModel *model_sort, *model;
+ GtkTreeRowReference *source_row_reference;
+ GtkTreePath *source_row, *dest_row, *child_dest_row;
+ GtkTreeViewDropPosition pos;
+ GtkTreeIter parent_iter, iter;
+ TnyFolder *folder;
+ TnyFolderStore *parent_folder;
+ gboolean done;
+
+ g_signal_stop_emission_by_name (widget, "drag-data-received");
+ model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
+
+ /* Get the unsorted model, and the path to the source row */
+ gtk_tree_get_row_drag_data (selection_data,
+ &model,
+ &source_row);
+
+ /* Can not call gtk_tree_view_get_drag_dest_row() because it's
+ not selected anymore */
+ gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
+ x, y,
+ &dest_row,
+ &pos);
+
+ /* Only allow drops IN other rows */
+ if (!dest_row ||
+ pos == GTK_TREE_VIEW_DROP_BEFORE ||
+ pos == GTK_TREE_VIEW_DROP_AFTER)
+ return;
+
+ 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);
+
+ if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (model),
+ child_dest_row,
+ selection_data))
+ goto out;
+
+ /* Do the mail operation */
+ gtk_tree_model_get_iter (model, &parent_iter, child_dest_row);
+ gtk_tree_model_get_iter (model, &iter, source_row);
+ gtk_tree_model_get (model, &parent_iter,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &parent_folder, -1);
+ gtk_tree_model_get (model, &iter,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder, -1);
+
+ g_signal_emit (G_OBJECT (widget), signals[FOLDER_MOVED_SIGNAL], 0,
+ folder, parent_folder, &done);
+ if (!done)
+ goto out;
+
+ /* Get a row reference to the source path because the path
+ could change after the insertion */
+ source_row_reference = gtk_tree_row_reference_new (model, source_row);
+ gtk_tree_path_free (source_row);
+
+ /* Insert the dragged row as a child of the dest row */
+ gtk_tree_path_down (child_dest_row);
+ if (gtk_tree_drag_dest_drag_data_received (GTK_TREE_DRAG_DEST (model),
+ child_dest_row,
+ selection_data)) {
+
+ /* Clean dest row */
+ gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget),
+ NULL,
+ GTK_TREE_VIEW_DROP_BEFORE);
+
+ /* Get the new path of the source row */
+ source_row = gtk_tree_row_reference_get_path (source_row_reference);
+
+ /* Delete the source row */
+ gtk_tree_drag_source_drag_data_delete (GTK_TREE_DRAG_SOURCE (model),
+ source_row);
+
+ gtk_tree_path_free (source_row);
+ }
+
+ gtk_tree_row_reference_free (source_row_reference);
+
+ out:
+ gtk_tree_path_free (child_dest_row);
+}
+
+static gint
+expand_row_timeout (gpointer data)
+{
+ GtkTreeView *tree_view = data;
+ GtkTreePath *dest_path = NULL;
+ GtkTreeViewDropPosition pos;
+ gboolean result = FALSE;
+
+ GDK_THREADS_ENTER ();
+
+ gtk_tree_view_get_drag_dest_row (tree_view,
+ &dest_path,
+ &pos);
+
+ if (dest_path &&
+ (pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER ||
+ pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)) {
+ gtk_tree_view_expand_row (tree_view, dest_path, FALSE);
+ gtk_tree_path_free (dest_path);
+ }
+ else {
+ if (dest_path)
+ gtk_tree_path_free (dest_path);
+
+ result = TRUE;
+ }
+
+ GDK_THREADS_LEAVE ();
+
+ return result;
+}
+
+
+static gboolean
+drag_motion_cb (GtkWidget *widget,
+ GdkDragContext *context,
+ gint x,
+ gint y,
+ guint time,
+ gpointer user_data)
+{
+ GtkTreeViewDropPosition pos;
+ GtkTreePath *dest_row;
+ ModestFolderViewPrivate *priv;
+
+ priv = MODEST_FOLDER_VIEW_GET_PRIVATE (widget);
+
+ if (priv->timer_expander != 0) {
+ g_source_remove (priv->timer_expander);
+ priv->timer_expander = 0;
+ }
+
+ gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
+ x, y,
+ &dest_row,
+ &pos);
+
+ if (!dest_row)
+ return FALSE;
+
+ /* 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);
+
+ return TRUE;
+}
+
+static void
+setup_drag_and_drop (GtkTreeView *self)
+{
+ gtk_drag_dest_set (GTK_WIDGET (self),
+ GTK_DEST_DEFAULT_ALL,
+ drag_types,
+ G_N_ELEMENTS (drag_types),
+ GDK_ACTION_MOVE);
+
+ gtk_signal_connect(GTK_OBJECT (self),
+ "drag_data_received",
+ GTK_SIGNAL_FUNC(drag_data_received_cb),
+ NULL);
+
+
+ gtk_drag_source_set (GTK_WIDGET (self),
+ GDK_BUTTON1_MASK | GDK_BUTTON3_MASK,
+ drag_types,
+ G_N_ELEMENTS (drag_types),
+ GDK_ACTION_MOVE);
+
+
+ gtk_signal_connect(GTK_OBJECT (self),
+ "drag_motion",
+ GTK_SIGNAL_FUNC(drag_motion_cb),
+ NULL);
+
+
+ gtk_signal_connect(GTK_OBJECT (self),
+ "drag_data_get",
+ GTK_SIGNAL_FUNC(drag_data_get_cb),
+ NULL);