+ gpointer target;
+
+ if (!context->targets)
+ return FALSE;
+
+ /* Check if we're dragging a folder row */
+ target = gtk_drag_dest_find_target (widget, context, NULL);
+
+ /* Request the data from the source. */
+ gtk_drag_get_data(widget, context, target, time);
+
+ return TRUE;
+}
+
+/*
+ * 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.
+ */
+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;
+}
+
+/*
+ * This function is called whenever the pointer is moved over a widget
+ * while dragging some data. It installs a timeout that will expand a
+ * node of the treeview if not expanded yet. This function also calls
+ * gdk_drag_status in order to set the suggested action that will be
+ * used by the "drag-data-received" signal handler to know if we
+ * should do a move or just a copy of the data.
+ */
+static gboolean
+on_drag_motion (GtkWidget *widget,
+ GdkDragContext *context,
+ gint x,
+ gint y,
+ guint time,
+ gpointer user_data)
+{
+ GtkTreeViewDropPosition pos;
+ GtkTreePath *dest_row;
+ ModestFolderViewPrivate *priv;
+ GdkDragAction suggested_action;
+ gboolean valid_location = FALSE;
+
+ 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);
+
+ /* 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;
+
+ if (context->actions == GDK_ACTION_COPY)
+ gdk_drag_status(context, GDK_ACTION_COPY, time);
+ else if (context->actions == GDK_ACTION_MOVE)
+ gdk_drag_status(context, GDK_ACTION_MOVE, time);
+ else if (context->actions & suggested_action)
+ gdk_drag_status(context, suggested_action, time);
+ else
+ gdk_drag_status(context, GDK_ACTION_DEFAULT, time);
+
+ out:
+ 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, MODEST_FOLDER_ROW },
+ { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_APP, MODEST_HEADER_ROW }
+};
+
+/*
+ * This function sets the treeview as a source and a target for dnd
+ * events. It also connects all the requirede signals.
+ */
+static void
+setup_drag_and_drop (GtkTreeView *self)
+{
+ /* Set up the folder view as a dnd destination. Set only the
+ highlight flag, otherwise gtk will have a different
+ behaviour */
+ gtk_drag_dest_set (GTK_WIDGET (self),
+ GTK_DEST_DEFAULT_HIGHLIGHT,
+ folder_view_drag_types,
+ G_N_ELEMENTS (folder_view_drag_types),
+ GDK_ACTION_MOVE | GDK_ACTION_COPY);
+
+ g_signal_connect (G_OBJECT (self),
+ "drag_data_received",
+ G_CALLBACK (on_drag_data_received),
+ NULL);
+
+
+ /* Set up the treeview as a dnd source */
+ gtk_drag_source_set (GTK_WIDGET (self),
+ GDK_BUTTON1_MASK,
+ folder_view_drag_types,
+ G_N_ELEMENTS (folder_view_drag_types),
+ GDK_ACTION_MOVE | GDK_ACTION_COPY);
+
+ g_signal_connect (G_OBJECT (self),
+ "drag_motion",
+ G_CALLBACK (on_drag_motion),
+ NULL);