[totem/wip/hadess/opendir-portal: 9/9] open-directory: Use libportal to open containing folder




commit 2c7772ef4668e2e56b5f4cbf385ac286d363537c
Author: Bastien Nocera <hadess hadess net>
Date:   Tue May 24 10:08:58 2022 +0200

    open-directory: Use libportal to open containing folder
    
    This should make the 'Open Containing Folder' functionality carry on
    working when we've restricted filesystem access further.

 meson.build                                       |  4 ++
 meson_options.txt                                 |  1 +
 src/plugins/meson.build                           |  7 +-
 src/plugins/open-directory/meson.build            |  1 +
 src/plugins/open-directory/totem-open-directory.c | 85 +++++++++--------------
 5 files changed, 45 insertions(+), 53 deletions(-)
---
diff --git a/meson.build b/meson.build
index 4eb542e09..c960159e3 100644
--- a/meson.build
+++ b/meson.build
@@ -207,6 +207,10 @@ if python_option != 'no'
   endif
 endif
 
+# libportal support
+libportal_dep = dependency('libportal-gtk3', required: get_option('libportal'))
+have_libportal = libportal_dep.found()
+
 configure_file(
   output: 'config.h',
   configuration: config_h
diff --git a/meson_options.txt b/meson_options.txt
index 31edd2c20..4611a1376 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -1,6 +1,7 @@
 option('help', type: 'boolean', value: true, description: 'Build help')
 option('enable-easy-codec-installation', type: 'combo', choices: ['yes', 'no', 'auto'], value: 'auto', 
description: 'Whether to enable easy codec installation support for GStreamer')
 option('enable-python', type: 'combo', choices: ['yes', 'no', 'auto'], value: 'auto', description: 'Enable 
python support')
+option('libportal', type: 'feature', value: 'auto', description: 'Build plugins requiring libportal')
 option('with-plugins', type: 'combo', choices: ['all', 'none', 'auto'], value: 'auto', description: 'Which 
Totem plugins to compile (default: auto; "all", "none" and "auto" are valid)')
 option('enable-gtk-doc', type: 'boolean', value: false, description: 'use gtk-doc to build documentation')
 option('profile', type: 'combo', choices: ['default', 'development'], value: 'default', description: 'Build 
profiles')
diff --git a/src/plugins/meson.build b/src/plugins/meson.build
index 01a2ed2ed..e78e70e28 100644
--- a/src/plugins/meson.build
+++ b/src/plugins/meson.build
@@ -41,7 +41,6 @@ if plugins_option != 'none'
     'autoload-subtitles',
     'im-status',
     'mpris',
-    'open-directory',
     'properties',
     'recent',
     'rotation',
@@ -62,6 +61,12 @@ if plugins_option != 'none'
       'samplepython'
     ]
   endif
+
+  if have_libportal
+    plugins += [
+      'open-directory'
+    ]
+  endif
 endif
 
 pylint_flags = ['-d', 'C0111', '-d', 'W0511', '-d', 'F0401', '-d', 'C0326' ]
diff --git a/src/plugins/open-directory/meson.build b/src/plugins/open-directory/meson.build
index 23cfd0ff6..604c8c1a7 100644
--- a/src/plugins/open-directory/meson.build
+++ b/src/plugins/open-directory/meson.build
@@ -8,6 +8,7 @@ shared_module(
   include_directories: plugins_incs,
   dependencies: plugins_deps + [
     gio_dep,
+    libportal_dep,
   ],
   c_args: plugins_cflags,
   install: true,
diff --git a/src/plugins/open-directory/totem-open-directory.c 
b/src/plugins/open-directory/totem-open-directory.c
index 3106be9bc..fd316cdf7 100644
--- a/src/plugins/open-directory/totem-open-directory.c
+++ b/src/plugins/open-directory/totem-open-directory.c
@@ -23,6 +23,7 @@
 #include <libpeas/peas-extension-base.h>
 #include <libpeas/peas-object-module.h>
 #include <libpeas/peas-activatable.h>
+#include <libportal-gtk3/portal-gtk3.h>
 
 #include "totem-plugin.h"
 
@@ -32,67 +33,45 @@
 typedef struct {
        PeasExtensionBase parent;
 
-       TotemObject *totem;
-       char        *mrl;
+       TotemObject   *totem;
+       XdpPortal     *portal;
+       GCancellable  *cancellable;
+       char          *mrl;
        GSimpleAction *action;
 } TotemOpenDirectoryPlugin;
 
 TOTEM_PLUGIN_REGISTER(TOTEM_TYPE_OPEN_DIRECTORY_PLUGIN, TotemOpenDirectoryPlugin, 
totem_open_directory_plugin)
 
-static char *
-get_notification_id (void)
+static void
+open_directory_cb (GObject *object,
+                  GAsyncResult *result,
+                  gpointer data)
 {
-       return g_strdup_printf ("%s_TIME%ld",
-                               "totem",
-                               g_get_monotonic_time () / G_TIME_SPAN_SECOND);
+       XdpPortal *portal = XDP_PORTAL (object);
+       g_autoptr(GError) error = NULL;
+       gboolean res;
+
+       res = xdp_portal_open_directory_finish (portal, result, &error);
+       if (!res) {
+               if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+                       g_warning ("Failed to show directory: %s", error->message);
+       }
 }
 
 static void
 totem_open_directory_plugin_open (GSimpleAction       *action,
-                            GVariant            *parameter,
-                            TotemOpenDirectoryPlugin *pi)
+                                 GVariant            *parameter,
+                                 TotemOpenDirectoryPlugin *pi)
 {
-
-
-       GError *error = NULL;
-       GDBusProxy *proxy;
-       gchar* notification_id;
-       GVariantBuilder *builder;
-       GVariant *dbus_arguments;
+       XdpParent *parent;
 
        g_assert (pi->mrl != NULL);
 
-       proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
-                                              G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
-                                              NULL, /* GDBusInterfaceInfo */
-                                              "org.freedesktop.FileManager1",
-                                              "/org/freedesktop/FileManager1",
-                                              "org.freedesktop.FileManager1",
-                                              NULL, /* GCancellable */
-                                              &error);
-       if (proxy == NULL) {
-               g_warning ("Could not contact file manager: %s", error->message);
-               g_error_free (error);
-               return;
-       }
-
-       notification_id = get_notification_id();
-
-       builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
-       g_variant_builder_add (builder, "s", pi->mrl);
-       dbus_arguments = g_variant_new ("(ass)", builder, notification_id);
-       g_variant_builder_unref (builder);
-       g_free(notification_id);
-
-       if (g_dbus_proxy_call_sync (proxy,
-                               "ShowItems", dbus_arguments,
-                               G_DBUS_CALL_FLAGS_NONE,
-                               -1, NULL, &error) == FALSE) {
-               g_warning ("Could not get file manager to display file: %s", error->message);
-               g_error_free (error);
-       }
-
-       g_object_unref (proxy);
+       parent = xdp_parent_new_gtk (totem_object_get_main_window (pi->totem));
+       xdp_portal_open_directory (pi->portal, parent, pi->mrl,
+                                  XDP_OPEN_URI_FLAG_NONE, pi->cancellable,
+                                  open_directory_cb, NULL);
+       xdp_parent_free (parent);
 }
 
 static void
@@ -157,12 +136,9 @@ impl_activate (PeasActivatable *plugin)
        GMenuItem *item;
        char *mrl;
 
-       /* FIXME: This plugin will stop working if the file is outside
-        * what the Flatpak has access to
-       if (g_file_test ("/.flatpak-info", G_FILE_TEST_EXISTS))
-               return; */
-
        pi->totem = g_object_get_data (G_OBJECT (plugin), "object");
+       pi->portal = xdp_portal_new ();
+       pi->cancellable = g_cancellable_new ();
 
        g_signal_connect (pi->totem,
                          "file-opened",
@@ -194,6 +170,11 @@ impl_deactivate (PeasActivatable *plugin)
 {
        TotemOpenDirectoryPlugin *pi = TOTEM_OPEN_DIRECTORY_PLUGIN (plugin);
 
+       if (pi->cancellable != NULL) {
+               g_cancellable_cancel (pi->cancellable);
+               g_clear_object (&pi->cancellable);
+       }
+
        g_signal_handlers_disconnect_by_func (pi->totem, totem_open_directory_file_opened, plugin);
        g_signal_handlers_disconnect_by_func (pi->totem, totem_open_directory_file_closed, plugin);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]