+
+/*****************************************************************************/
+/* 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.
+ */
+static void
+on_drag_data_received (GtkWidget *widget,
+ GdkDragContext *context,
+ gint x,
+ gint y,
+ GtkSelectionData *selection_data,
+ guint target_type,
+ guint time,
+ gpointer data)
+{
+ GtkWidget *source_widget;
+ GtkTreeModel *model_sort, *dest_model, *source_model;
+ GtkTreePath *source_row, *dest_row, *child_dest_row;
+ GtkTreeViewDropPosition pos;
+ gboolean success = FALSE, delete_source = FALSE;
+ DndHelper *helper;
+
+ /* Do not allow further process */
+ g_signal_stop_emission_by_name (widget, "drag-data-received");
+
+ /* Get the action */
+ if (context->action == GDK_ACTION_MOVE)
+ delete_source = TRUE;
+
+ /* Check if the get_data failed */
+ if (selection_data == NULL || selection_data->length < 0)
+ 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));
+ }
+
+ /* Get the path to the destination row. Can not call
+ gtk_tree_view_get_drag_dest_row() because the source row
+ is 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)
+ gtk_drag_finish (context, success, FALSE, time);
+
+ /* Create the helper */
+ helper = g_slice_new0 (DndHelper);
+ helper->delete_source = delete_source;
+ 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,
+ helper);
+ } else {
+
+
+ drag_and_drop_from_folder_view (source_model,
+ dest_model,
+ child_dest_row,
+ selection_data,
+ helper);
+ }
+
+ /* Frees */
+ gtk_tree_path_free (source_row);
+ gtk_tree_path_free (child_dest_row);
+}
+
+/*
+ * We define a "drag-drop" signal handler because we do not want to
+ * use the default one, because the default one always calls
+ * gtk_drag_finish and we prefer to do it in the "drag-data-received"
+ * signal handler, because there we have all the information available
+ * to know if the dnd was a success or not.
+ */
+static gboolean
+drag_drop_cb (GtkWidget *widget,
+ GdkDragContext *context,
+ gint x,
+ gint y,
+ guint time,
+ gpointer user_data)
+{
+ 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);
+
+ 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;
+
+ 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;
+
+ g_return_if_fail (self);
+
+ priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
+
+ if (priv->visible_account_id)
+ g_free (priv->visible_account_id);
+ priv->visible_account_id = g_strdup (account_id);
+
+ /* Save preferences */
+ conf = modest_runtime_get_conf ();
+ modest_widget_memory_save (conf, G_OBJECT (self), MODEST_CONF_FOLDER_VIEW_KEY);
+}
+
+const gchar *
+modest_folder_view_get_account_id_of_visible_server_account (ModestFolderView *self)
+{
+ ModestFolderViewPrivate *priv;
+
+ g_return_val_if_fail (self, NULL);
+
+ priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
+
+ return (const gchar *) priv->visible_account_id;
+}