filechooserwidget: Stop using selection data

Use gdk_drop_read_async()
This commit is contained in:
Benjamin Otte 2020-02-22 17:16:56 +01:00
parent 992173c382
commit a19066b17c

View File

@ -1854,35 +1854,28 @@ error_selecting_dragged_file_dialog (GtkFileChooserWidget *impl,
} }
static void static void
file_list_drag_data_select_uris (GtkFileChooserWidget *impl, file_list_drag_data_select_files (GtkFileChooserWidget *impl,
gchar **uris) GSList *files)
{ {
int i;
char *uri;
GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl); GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
GSList *l;
for (i = 1; uris[i]; i++) for (l = files; l; l = l->next)
{ {
GFile *file; GFile *file = l->data;
GError *error = NULL; GError *error = NULL;
uri = uris[i];
file = g_file_new_for_uri (uri);
gtk_file_chooser_widget_select_file (chooser, file, &error); gtk_file_chooser_widget_select_file (chooser, file, &error);
if (error) if (error)
error_selecting_dragged_file_dialog (impl, file, error); error_selecting_dragged_file_dialog (impl, file, error);
g_object_unref (file);
} }
} }
struct FileListDragData typedef struct
{ {
GtkFileChooserWidget *impl; GtkFileChooserWidget *impl;
gchar **uris; GSList *files;
GFile *file; } FileListDragData;
};
static void static void
file_list_drag_data_received_get_info_cb (GCancellable *cancellable, file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
@ -1891,7 +1884,7 @@ file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
gpointer user_data) gpointer user_data)
{ {
gboolean cancelled = g_cancellable_is_cancelled (cancellable); gboolean cancelled = g_cancellable_is_cancelled (cancellable);
struct FileListDragData *data = user_data; FileListDragData *data = user_data;
GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl); GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
GtkFileChooserWidgetPrivate *priv = gtk_file_chooser_widget_get_instance_private (data->impl); GtkFileChooserWidgetPrivate *priv = gtk_file_chooser_widget_get_instance_private (data->impl);
@ -1905,27 +1898,26 @@ file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
if ((priv->action == GTK_FILE_CHOOSER_ACTION_OPEN || if ((priv->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
priv->action == GTK_FILE_CHOOSER_ACTION_SAVE) && priv->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
data->uris[1] == 0 && !error && _gtk_file_info_consider_as_directory (info)) data->files->next == NULL && !error && _gtk_file_info_consider_as_directory (info))
change_folder_and_display_error (data->impl, data->file, FALSE); change_folder_and_display_error (data->impl, data->files->data, FALSE);
else else
{ {
GError *local_error = NULL; GError *local_error = NULL;
gtk_file_chooser_widget_unselect_all (chooser); gtk_file_chooser_widget_unselect_all (chooser);
gtk_file_chooser_widget_select_file (chooser, data->file, &local_error); gtk_file_chooser_widget_select_file (chooser, data->files->data, &local_error);
if (local_error) if (local_error)
error_selecting_dragged_file_dialog (data->impl, data->file, local_error); error_selecting_dragged_file_dialog (data->impl, data->files->data, local_error);
else else
browse_files_center_selected_row (data->impl); browse_files_center_selected_row (data->impl);
} }
if (priv->select_multiple) if (priv->select_multiple)
file_list_drag_data_select_uris (data->impl, data->uris); file_list_drag_data_select_files (data->impl, data->files->next);
out: out:
g_object_unref (data->impl); g_object_unref (data->impl);
g_strfreev (data->uris); g_slist_free_full (data->files, g_object_unref);
g_object_unref (data->file);
g_free (data); g_free (data);
g_object_unref (cancellable); g_object_unref (cancellable);
@ -1938,44 +1930,34 @@ file_list_drag_data_received_cb (GObject *source,
{ {
GtkFileChooserWidget *impl = GTK_FILE_CHOOSER_WIDGET (user_data); GtkFileChooserWidget *impl = GTK_FILE_CHOOSER_WIDGET (user_data);
GtkFileChooserWidgetPrivate *priv = gtk_file_chooser_widget_get_instance_private (impl); GtkFileChooserWidgetPrivate *priv = gtk_file_chooser_widget_get_instance_private (impl);
GtkDropTarget *dest = GTK_DROP_TARGET (source); GdkDrop *drop = GDK_DROP (source);
GtkWidget *widget = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (dest)); GSList *files;
GdkDrop *drop = gtk_drop_target_get_drop (dest); const GValue *value;
GdkDrag *drag = gdk_drop_get_drag (drop); FileListDragData *data;
gchar **uris;
char *uri;
GFile *file;
GtkSelectionData *selection_data;
selection_data = gtk_drop_target_read_selection_finish (dest, result, NULL); value = gdk_drop_read_value_finish (drop, result, NULL);
if (value == NULL)
/* Allow only drags from other widgets; see bug #533891. */
if (drag && gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (drag)) == widget)
return;
/* Parse the text/uri-list string, navigate to the first one */
uris = gtk_selection_data_get_uris (selection_data);
if (uris && uris[0])
{ {
struct FileListDragData *data; gdk_drop_finish (drop, 0);
return;
uri = uris[0];
file = g_file_new_for_uri (uri);
data = g_new0 (struct FileListDragData, 1);
data->impl = g_object_ref (impl);
data->uris = uris;
data->file = file;
if (priv->file_list_drag_data_received_cancellable)
g_cancellable_cancel (priv->file_list_drag_data_received_cancellable);
priv->file_list_drag_data_received_cancellable =
_gtk_file_system_get_info (priv->file_system, file,
"standard::type",
file_list_drag_data_received_get_info_cb,
data);
} }
files = g_value_get_boxed (value);
data = g_new0 (FileListDragData, 1);
data->impl = g_object_ref (impl);
data->files = g_slist_copy_deep (files, (GCopyFunc) g_object_ref, NULL);
if (priv->file_list_drag_data_received_cancellable)
g_cancellable_cancel (priv->file_list_drag_data_received_cancellable);
priv->file_list_drag_data_received_cancellable =
_gtk_file_system_get_info (priv->file_system, data->files->data,
"standard::type",
file_list_drag_data_received_get_info_cb,
data);
gdk_drop_finish (drop, gdk_drop_get_actions (drop));
} }
/* Don't do anything with the drag_drop signal */ /* Don't do anything with the drag_drop signal */
@ -1986,9 +1968,7 @@ file_list_drag_drop_cb (GtkDropTarget *dest,
int y, int y,
GtkFileChooserWidget *impl) GtkFileChooserWidget *impl)
{ {
const char *target = g_intern_static_string ("text/uri-list"); gdk_drop_read_value_async (drop, GDK_TYPE_FILE_LIST, G_PRIORITY_DEFAULT, NULL, file_list_drag_data_received_cb, impl);
gtk_drop_target_read_selection (dest, target, NULL, file_list_drag_data_received_cb, impl);
return TRUE; return TRUE;
} }