[glib/portal] Add portal support to g_app_info_launch_default_for_uri



commit 46c09c035fe6467dc551e443e59e13fd44656095
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Jun 21 08:17:16 2016 -0400

    Add portal support to g_app_info_launch_default_for_uri
    
    We need to patch in the portal support at a high enough
    level that GAppInfo is not involved - a sandboxed app may
    not be able to see any applications, so it can just launch
    the defaults.

 gio/gappinfo.c |   69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 69 insertions(+), 0 deletions(-)
---
diff --git a/gio/gappinfo.c b/gio/gappinfo.c
index bfcc85b..f0a4967 100644
--- a/gio/gappinfo.c
+++ b/gio/gappinfo.c
@@ -20,9 +20,13 @@
 
 #include "config.h"
 
+#include <unistd.h>
+
 #include "gappinfo.h"
 #include "gappinfoprivate.h"
 #include "gcontextspecificgroup.h"
+#include "gdbusconnection.h"
+#include "gdbusmessage.h"
 
 #include "glibintl.h"
 #include <gioerror.h>
@@ -665,6 +669,68 @@ g_app_info_should_show (GAppInfo *appinfo)
   return (* iface->should_show) (appinfo);
 }
 
+static gboolean
+should_use_portal (void)
+{
+  const char *use_portal;
+  char *path;
+
+  path = g_strdup_printf ("/run/user/%d/flatpak-info", getuid());
+  if (g_file_test (path, G_FILE_TEST_EXISTS))
+    use_portal = "1";
+  else
+    {
+      use_portal = g_getenv ("GTK_USE_PORTAL");
+      if (!use_portal)
+        use_portal = "";
+    }
+  g_free (path);
+
+  return g_str_equal (use_portal, "1");
+}
+
+static gboolean
+launch_default_with_portal (const char         *uri,
+                            GAppLaunchContext  *context,
+                            GError            **error)
+{
+  GDBusConnection *bus;
+  GDBusMessage *message;
+  GVariantBuilder opt_builder;
+  const char *parent_window = NULL;
+
+  bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
+
+  if (context)
+    {
+      char **env;
+      env = g_app_launch_context_get_environment (context);
+      parent_window = g_environ_getenv (env, "PARENT_WINDOW_ID");
+      g_strfreev (env);
+    }
+
+  g_variant_builder_init (&opt_builder, G_VARIANT_TYPE_VARDICT);
+
+  message = g_dbus_message_new_method_call ("org.freedesktop.portal.Desktop",
+                                            "/org/freedesktop/portal/desktop",
+                                            "org.freedesktop.portal.AppChooser",
+                                            "OpenURI");
+
+  g_variant_builder_init (&opt_builder, G_VARIANT_TYPE_VARDICT);
+
+  g_dbus_message_set_body (message, g_variant_new ("(ss a{sv})",
+                                                   parent_window ? parent_window : "",
+                                                   uri,
+                                                   g_variant_builder_end (&opt_builder)));
+
+  g_dbus_connection_send_message (bus,
+                                  message,
+                                  G_DBUS_SEND_MESSAGE_FLAGS_NONE,
+                                  NULL,
+                                  NULL);
+  return TRUE;
+}
+
 /**
  * g_app_info_launch_default_for_uri:
  * @uri: the uri to show
@@ -688,6 +754,9 @@ g_app_info_launch_default_for_uri (const char         *uri,
   GList l;
   gboolean res;
 
+  if (should_use_portal ())
+    return launch_default_with_portal (uri, launch_context, error);
+
   /* g_file_query_default_handler() calls
    * g_app_info_get_default_for_uri_scheme() too, but we have to do it
    * here anyway in case GFile can't parse @uri correctly.


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