Commit 89aa77b7 authored by Matthias Clasen's avatar Matthias Clasen

portal: Implement OpenFiles and SaveFile

This fleshes out the portal file chooser some more.
parent 5be03510
......@@ -87,19 +87,19 @@ filechooser_portal_data_free (FilechooserPortalData *data)
}
static void
open_file_response (GDBusConnection *connection,
const gchar *sender_name,
const gchar *object_path,
const gchar *interface_name,
const gchar *signal_name,
GVariant *parameters,
gpointer user_data)
one_file_response (GDBusConnection *connection,
const gchar *sender_name,
const gchar *object_path,
const gchar *interface_name,
const gchar *signal_name,
GVariant *parameters,
gpointer user_data)
{
GtkFileChooserNative *self = user_data;
FilechooserPortalData *data = self->mode_data;
guint32 portal_response;
int gtk_response;
char *uri, *handle;;
char *uri, *handle;
GVariant *response_data;
g_variant_get (parameters, "(&ou&s@a{sv})", &handle, &portal_response, &uri, &response_data);
......@@ -131,6 +131,55 @@ open_file_response (GDBusConnection *connection,
_gtk_native_dialog_emit_response (GTK_NATIVE_DIALOG (self), gtk_response);
}
static void
multi_file_response (GDBusConnection *connection,
const gchar *sender_name,
const gchar *object_path,
const gchar *interface_name,
const gchar *signal_name,
GVariant *parameters,
gpointer user_data)
{
GtkFileChooserNative *self = user_data;
FilechooserPortalData *data = self->mode_data;
guint32 portal_response;
int gtk_response;
char *handle;
char **uris;
int i;
GVariant *response_data;
g_variant_get (parameters, "(&ou^a&s@a{sv})", &handle, &portal_response, &uris, &response_data);
if (data->portal_handle == NULL ||
strcmp (handle, data->portal_handle) != 0)
return;
g_slist_free_full (self->custom_files, g_object_unref);
self->custom_files = NULL;
for (i = 0; uris[i]; i++)
self->custom_files = g_slist_prepend (self->custom_files, g_file_new_for_uri (uris[i]));
switch (portal_response)
{
case 0:
gtk_response = GTK_RESPONSE_OK;
break;
case 1:
gtk_response = GTK_RESPONSE_CANCEL;
break;
case 2:
default:
gtk_response = GTK_RESPONSE_DELETE_EVENT;
break;
}
filechooser_portal_data_free (data);
self->mode_data = NULL;
_gtk_native_dialog_emit_response (GTK_NATIVE_DIALOG (self), gtk_response);
}
static void
send_close (FilechooserPortalData *data)
{
......@@ -196,6 +245,11 @@ gtk_file_chooser_native_portal_show (GtkFileChooserNative *self)
char *parent_window_str;
GDBusMessage *message;
GVariantBuilder opt_builder;
GtkFileChooserAction action;
gboolean multiple;
const char *method_name;
const char *signal_name;
GDBusSignalCallback signal_callback;
if (g_getenv ("GTK_USE_PORTAL") == NULL)
return FALSE;
......@@ -215,10 +269,34 @@ gtk_file_chooser_native_portal_show (GtkFileChooserNative *self)
data->self = g_object_ref (self);
data->connection = connection;
action = gtk_file_chooser_get_action (GTK_FILE_CHOOSER (self));
multiple = gtk_file_chooser_get_select_multiple (GTK_FILE_CHOOSER (self));
if (action == GTK_FILE_CHOOSER_ACTION_OPEN && !multiple)
{
method_name = "OpenFile";
signal_name = "OpenFileResponse";
signal_callback = one_file_response;
}
else if (action == GTK_FILE_CHOOSER_ACTION_OPEN && multiple)
{
method_name = "OpenFiles";
signal_name = "OpenFilesResponse";
signal_callback = multi_file_response;
}
else if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
{
method_name = "SaveFile";
signal_name = "SaveFileResponse";
signal_callback = one_file_response;
}
else
g_assert_not_reached ();
message = g_dbus_message_new_method_call ("org.freedesktop.portal.Desktop",
"/org/freedesktop/portal/desktop",
"org.freedesktop.portal.FileChooser",
"OpenFile");
method_name);
parent_window_str = NULL;
transient_for = gtk_native_dialog_get_transient_for (GTK_NATIVE_DIALOG (self));
......@@ -261,11 +339,11 @@ gtk_file_chooser_native_portal_show (GtkFileChooserNative *self)
g_dbus_connection_signal_subscribe (data->connection,
"org.freedesktop.portal.Desktop",
"org.freedesktop.portal.FileChooser",
"OpenFileResponse",
signal_name,
"/org/freedesktop/portal/desktop",
NULL,
G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE,
open_file_response,
signal_callback,
self, NULL);
g_dbus_connection_send_message_with_reply (data->connection,
......@@ -277,7 +355,6 @@ gtk_file_chooser_native_portal_show (GtkFileChooserNative *self)
open_file_msg_cb,
data);
self->mode_data = data;
return TRUE;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment