Merge branch 'file-launcher-writable' into 'main'

file laucher: Add a writable property

Closes #4247

See merge request GNOME/gtk!6600
This commit is contained in:
Matthias Clasen 2023-11-23 00:13:19 +00:00
commit a35a885902
4 changed files with 98 additions and 10 deletions

View File

@ -53,11 +53,13 @@ struct _GtkFileLauncher
GFile *file; GFile *file;
unsigned int always_ask : 1; unsigned int always_ask : 1;
unsigned int writable : 1;
}; };
enum { enum {
PROP_FILE = 1, PROP_FILE = 1,
PROP_ALWAYS_ASK = 2, PROP_ALWAYS_ASK,
PROP_WRITABLE,
NUM_PROPERTIES NUM_PROPERTIES
}; };
@ -99,6 +101,10 @@ gtk_file_launcher_get_property (GObject *object,
g_value_set_boolean (value, self->always_ask); g_value_set_boolean (value, self->always_ask);
break; break;
case PROP_WRITABLE:
g_value_set_boolean (value, self->writable);
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break; break;
@ -123,6 +129,10 @@ gtk_file_launcher_set_property (GObject *object,
gtk_file_launcher_set_always_ask (self, g_value_get_boolean (value)); gtk_file_launcher_set_always_ask (self, g_value_get_boolean (value));
break; break;
case PROP_WRITABLE:
gtk_file_launcher_set_writable (self, g_value_get_boolean (value));
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break; break;
@ -163,6 +173,18 @@ gtk_file_launcher_class_init (GtkFileLauncherClass *class)
FALSE, FALSE,
G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS|G_PARAM_EXPLICIT_NOTIFY); G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS|G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkFileLauncher:writable: (attributes org.gtk.Property.get=gtk_file_launcher_get_writable org.gtk.Property.set=gtk_file_launcher_set_writable)
*
* Whether to make the file writable for the handler.
*
* Since: 4.14
*/
properties[PROP_WRITABLE] =
g_param_spec_boolean ("writable", NULL, NULL,
FALSE,
G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS|G_PARAM_EXPLICIT_NOTIFY);
g_object_class_install_properties (object_class, NUM_PROPERTIES, properties); g_object_class_install_properties (object_class, NUM_PROPERTIES, properties);
} }
@ -272,6 +294,47 @@ gtk_file_launcher_set_always_ask (GtkFileLauncher *self,
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ALWAYS_ASK]); g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ALWAYS_ASK]);
} }
/**
* gtk_file_launcher_get_writable:
* @self: a `GtkFileLauncher`
*
* Returns whether to make the file writable for the handler.
*
* Returns: `TRUE` if the file will be made writable
*
* Since: 4.14
*/
gboolean
gtk_file_launcher_get_writable (GtkFileLauncher *self)
{
g_return_val_if_fail (GTK_IS_FILE_LAUNCHER (self), FALSE);
return self->writable;
}
/**
* gtk_file_launcher_set_writable:
* @self: a `GtkFileLauncher`
* @writable: a `gboolean`
*
* Sets whether to make the file writable for the handler.
*
* Since: 4.14
*/
void
gtk_file_launcher_set_writable (GtkFileLauncher *self,
gboolean writable)
{
g_return_if_fail (GTK_IS_FILE_LAUNCHER (self));
if (self->writable == writable)
return;
self->writable = writable;
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_WRITABLE]);
}
/* }}} */ /* }}} */
/* {{{ Async implementation */ /* {{{ Async implementation */
@ -433,7 +496,15 @@ gtk_file_launcher_launch (GtkFileLauncher *self,
#ifndef G_OS_WIN32 #ifndef G_OS_WIN32
if (gtk_openuri_portal_is_available ()) if (gtk_openuri_portal_is_available ())
{ {
gtk_openuri_portal_open_async (self->file, FALSE, self->always_ask, parent, cancellable, open_done, task); GtkOpenuriFlags flags = 0;
if (self->always_ask)
flags |= GTK_OPENURI_FLAGS_ASK;
if (self->writable)
flags |= GTK_OPENURI_FLAGS_WRITABLE;
gtk_openuri_portal_open_async (self->file, FALSE, flags, parent, cancellable, open_done, task);
} }
else else
#endif #endif
@ -527,7 +598,9 @@ gtk_file_launcher_open_containing_folder (GtkFileLauncher *self,
#ifndef G_OS_WIN32 #ifndef G_OS_WIN32
if (gtk_openuri_portal_is_available ()) if (gtk_openuri_portal_is_available ())
{ {
gtk_openuri_portal_open_async (self->file, TRUE, FALSE, parent, cancellable, open_done, task); GtkOpenuriFlags flags = 0;
gtk_openuri_portal_open_async (self->file, TRUE, flags, parent, cancellable, open_done, task);
} }
else else
#endif #endif

View File

@ -46,6 +46,11 @@ GDK_AVAILABLE_IN_4_12
void gtk_file_launcher_set_always_ask (GtkFileLauncher *self, void gtk_file_launcher_set_always_ask (GtkFileLauncher *self,
gboolean always_ask); gboolean always_ask);
GDK_AVAILABLE_IN_4_14
gboolean gtk_file_launcher_get_writable (GtkFileLauncher *self);
GDK_AVAILABLE_IN_4_14
void gtk_file_launcher_set_writable (GtkFileLauncher *self,
gboolean writable);
GDK_AVAILABLE_IN_4_10 GDK_AVAILABLE_IN_4_10
void gtk_file_launcher_launch (GtkFileLauncher *self, void gtk_file_launcher_launch (GtkFileLauncher *self,
GtkWindow *parent, GtkWindow *parent,

View File

@ -112,7 +112,7 @@ typedef struct {
GFile *file; GFile *file;
char *uri; char *uri;
gboolean open_folder; gboolean open_folder;
gboolean always_ask; GtkOpenuriFlags flags;
GDBusConnection *connection; GDBusConnection *connection;
GCancellable *cancellable; GCancellable *cancellable;
GTask *task; GTask *task;
@ -276,7 +276,6 @@ open_uri (OpenUriData *data,
{ {
GFile *file = data->file; GFile *file = data->file;
gboolean open_folder = data->open_folder; gboolean open_folder = data->open_folder;
gboolean always_ask = data->always_ask;
GTask *task; GTask *task;
GVariant *opts = NULL; GVariant *opts = NULL;
int i; int i;
@ -321,8 +320,13 @@ open_uri (OpenUriData *data,
if (activation_token) if (activation_token)
g_variant_builder_add (&opt_builder, "{sv}", "activation_token", g_variant_new_string (activation_token)); g_variant_builder_add (&opt_builder, "{sv}", "activation_token", g_variant_new_string (activation_token));
if (always_ask && !open_folder) if (!open_folder)
g_variant_builder_add (&opt_builder, "{sv}", "ask", g_variant_new_boolean (always_ask)); {
if (data->flags & GTK_OPENURI_FLAGS_ASK)
g_variant_builder_add (&opt_builder, "{sv}", "ask", g_variant_new_boolean (TRUE));
if (data->flags & GTK_OPENURI_FLAGS_WRITABLE)
g_variant_builder_add (&opt_builder, "{sv}", "writable", g_variant_new_boolean (TRUE));
}
opts = g_variant_builder_end (&opt_builder); opts = g_variant_builder_end (&opt_builder);
@ -458,7 +462,7 @@ window_handle_exported (GtkWindow *window,
void void
gtk_openuri_portal_open_async (GFile *file, gtk_openuri_portal_open_async (GFile *file,
gboolean open_folder, gboolean open_folder,
gboolean always_ask, GtkOpenuriFlags flags,
GtkWindow *parent, GtkWindow *parent,
GCancellable *cancellable, GCancellable *cancellable,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
@ -478,7 +482,7 @@ gtk_openuri_portal_open_async (GFile *file,
data->parent = parent ? g_object_ref (parent) : NULL; data->parent = parent ? g_object_ref (parent) : NULL;
data->file = g_object_ref (file); data->file = g_object_ref (file);
data->open_folder = open_folder; data->open_folder = open_folder;
data->always_ask = always_ask; data->flags = flags;
data->cancellable = cancellable ? g_object_ref (cancellable) : NULL; data->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
data->task = g_task_new (parent, cancellable, callback, user_data); data->task = g_task_new (parent, cancellable, callback, user_data);
g_task_set_check_cancellable (data->task, FALSE); g_task_set_check_cancellable (data->task, FALSE);

View File

@ -28,9 +28,15 @@ G_BEGIN_DECLS
gboolean gtk_openuri_portal_is_available (void); gboolean gtk_openuri_portal_is_available (void);
typedef enum
{
GTK_OPENURI_FLAGS_ASK = 1 << 0,
GTK_OPENURI_FLAGS_WRITABLE = 1 << 1,
} GtkOpenuriFlags;
void gtk_openuri_portal_open_async (GFile *file, void gtk_openuri_portal_open_async (GFile *file,
gboolean open_folder, gboolean open_folder,
gboolean always_ask, GtkOpenuriFlags flags,
GtkWindow *window, GtkWindow *window,
GCancellable *cancellable, GCancellable *cancellable,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,