Blame gio/gopenuriportal.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 *
Packit ae235b
 * Copyright 2017 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <sys/stat.h>
Packit ae235b
#include <fcntl.h>
Packit ae235b
#include <errno.h>
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#include "gopenuriportal.h"
Packit ae235b
#include "xdp-dbus.h"
Packit ae235b
#include "gstdio.h"
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include "gunixfdlist.h"
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifndef O_PATH
Packit ae235b
#define O_PATH 0
Packit ae235b
#endif
Packit ae235b
#ifndef O_CLOEXEC
Packit ae235b
#define O_CLOEXEC 0
Packit ae235b
#else
Packit ae235b
#define HAVE_O_CLOEXEC 1
Packit ae235b
#endif
Packit ae235b
Packit ae235b
Packit ae235b
static GXdpOpenURI *openuri;
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
init_openuri_portal (void)
Packit ae235b
{
Packit ae235b
  static gsize openuri_inited = 0;
Packit ae235b
Packit ae235b
  if (g_once_init_enter (&openuri_inited))
Packit ae235b
    {
Packit ae235b
      GError *error = NULL;
Packit ae235b
      GDBusConnection *connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
Packit ae235b
Packit ae235b
      if (connection != NULL)
Packit ae235b
        {
Packit ae235b
          openuri = gxdp_open_uri_proxy_new_sync (connection, 0,
Packit ae235b
                                                  "org.freedesktop.portal.Desktop",
Packit ae235b
                                                  "/org/freedesktop/portal/desktop",
Packit ae235b
                                                  NULL, &error);
Packit ae235b
          if (openuri == NULL)
Packit ae235b
            {
Packit ae235b
              g_warning ("Cannot create document portal proxy: %s", error->message);
Packit ae235b
              g_error_free (error);
Packit ae235b
            }
Packit ae235b
Packit ae235b
          g_object_unref (connection);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          g_warning ("Cannot connect to session bus when initializing document portal: %s",
Packit ae235b
                     error->message);
Packit ae235b
          g_error_free (error);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_once_init_leave (&openuri_inited, 1);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return openuri != NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
g_openuri_portal_open_uri (const char  *uri,
Packit ae235b
                           const char  *parent_window,
Packit ae235b
                           GError     **error)
Packit ae235b
{
Packit ae235b
  GFile *file = NULL;
Packit ae235b
  GVariantBuilder opt_builder;
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  if (!init_openuri_portal ())
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED,
Packit ae235b
                   "OpenURI portal is not available");
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_variant_builder_init (&opt_builder, G_VARIANT_TYPE_VARDICT);
Packit ae235b
Packit ae235b
  file = g_file_new_for_uri (uri);
Packit ae235b
  if (g_file_is_native (file))
Packit ae235b
    {
Packit ae235b
      char *path = NULL;
Packit ae235b
      GUnixFDList *fd_list = NULL;
Packit ae235b
      int fd, fd_id, errsv;
Packit ae235b
Packit ae235b
      path = g_file_get_path (file);
Packit ae235b
Packit ae235b
      fd = g_open (path, O_PATH | O_CLOEXEC);
Packit ae235b
      errsv = errno;
Packit ae235b
      if (fd == -1)
Packit ae235b
        {
Packit ae235b
          g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
Packit ae235b
                       "Failed to open '%s'", path);
Packit ae235b
          return FALSE;
Packit ae235b
        }
Packit ae235b
Packit ae235b
#ifndef HAVE_O_CLOEXEC
Packit ae235b
      fcntl (fd, F_SETFD, FD_CLOEXEC);
Packit ae235b
#endif
Packit ae235b
      fd_list = g_unix_fd_list_new_from_array (&fd, 1);
Packit ae235b
      fd = -1;
Packit ae235b
      fd_id = 0;
Packit ae235b
Packit ae235b
      res = gxdp_open_uri_call_open_file_sync (openuri,
Packit ae235b
                                               parent_window ? parent_window : "",
Packit ae235b
                                               g_variant_new ("h", fd_id),
Packit ae235b
                                               g_variant_builder_end (&opt_builder),
Packit ae235b
                                               fd_list,
Packit ae235b
                                               NULL,
Packit ae235b
                                               NULL,
Packit ae235b
                                               NULL,
Packit ae235b
                                               error);
Packit ae235b
      g_free (path);
Packit ae235b
      g_object_unref (fd_list);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      res = gxdp_open_uri_call_open_uri_sync (openuri,
Packit ae235b
                                              parent_window ? parent_window : "",
Packit ae235b
                                              uri,
Packit ae235b
                                              g_variant_builder_end (&opt_builder),
Packit ae235b
                                              NULL,
Packit ae235b
                                              NULL,
Packit ae235b
                                              error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_object_unref (file);
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
enum {
Packit ae235b
  XDG_DESKTOP_PORTAL_SUCCESS   = 0,
Packit ae235b
  XDG_DESKTOP_PORTAL_CANCELLED = 1,
Packit ae235b
  XDG_DESKTOP_PORTAL_FAILED    = 2
Packit ae235b
};
Packit ae235b
Packit ae235b
static void
Packit ae235b
response_received (GDBusConnection *connection,
Packit ae235b
                   const char      *sender_name,
Packit ae235b
                   const char      *object_path,
Packit ae235b
                   const char      *interface_name,
Packit ae235b
                   const char      *signal_name,
Packit ae235b
                   GVariant        *parameters,
Packit ae235b
                   gpointer         user_data)
Packit ae235b
{
Packit ae235b
  GTask *task = user_data;
Packit ae235b
  guint32 response;
Packit ae235b
  guint signal_id;
Packit ae235b
Packit ae235b
  signal_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (task), "signal-id"));
Packit ae235b
  g_dbus_connection_signal_unsubscribe (connection, signal_id);
Packit ae235b
Packit ae235b
  g_variant_get (parameters, "(u@a{sv})", &response, NULL);
Packit ae235b
Packit ae235b
  switch (response)
Packit ae235b
    {
Packit ae235b
    case XDG_DESKTOP_PORTAL_SUCCESS:
Packit ae235b
      g_task_return_boolean (task, TRUE);
Packit ae235b
      break;
Packit ae235b
    case XDG_DESKTOP_PORTAL_CANCELLED:
Packit ae235b
      g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_CANCELLED, "Launch cancelled");
Packit ae235b
      break;
Packit ae235b
    case XDG_DESKTOP_PORTAL_FAILED:
Packit ae235b
    default:
Packit ae235b
      g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED, "Launch failed");
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
open_call_done (GObject      *source,
Packit ae235b
                GAsyncResult *result,
Packit ae235b
                gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GXdpOpenURI *openuri = GXDP_OPEN_URI (source);
Packit ae235b
  GDBusConnection *connection;
Packit ae235b
  GTask *task = user_data;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  gboolean open_file;
Packit ae235b
  gboolean res;
Packit ae235b
  char *path;
Packit ae235b
  const char *handle;
Packit ae235b
  guint signal_id;
Packit ae235b
Packit ae235b
  connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (openuri));
Packit ae235b
  open_file = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (task), "open-file"));
Packit ae235b
Packit ae235b
  if (open_file)
Packit ae235b
    res = gxdp_open_uri_call_open_file_finish (openuri, &path, NULL, result, &error);
Packit ae235b
  else
Packit ae235b
    res = gxdp_open_uri_call_open_uri_finish (openuri, &path, result, &error);
Packit ae235b
Packit ae235b
  if (!res)
Packit ae235b
    {
Packit ae235b
      g_task_return_error (task, error);
Packit ae235b
      g_object_unref (task);
Packit ae235b
      g_free (path);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  handle = (const char *)g_object_get_data (G_OBJECT (task), "handle");
Packit ae235b
  if (strcmp (handle, path) != 0)
Packit ae235b
    {
Packit ae235b
      signal_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (task), "signal-id"));
Packit ae235b
      g_dbus_connection_signal_unsubscribe (connection, signal_id);
Packit ae235b
Packit ae235b
      signal_id = g_dbus_connection_signal_subscribe (connection,
Packit ae235b
                                                      "org.freedesktop.portal.Desktop",
Packit ae235b
                                                      "org.freedesktop.portal.Request",
Packit ae235b
                                                      "Response",
Packit ae235b
                                                      path,
Packit ae235b
                                                      NULL,
Packit ae235b
                                                      G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE,
Packit ae235b
                                                      response_received,
Packit ae235b
                                                      task,
Packit ae235b
                                                      NULL);
Packit ae235b
      g_object_set_data (G_OBJECT (task), "signal-id", GINT_TO_POINTER (signal_id));
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_openuri_portal_open_uri_async (const char          *uri,
Packit ae235b
                                 const char          *parent_window,
Packit ae235b
                                 GCancellable        *cancellable,
Packit ae235b
                                 GAsyncReadyCallback  callback,
Packit ae235b
                                 gpointer             user_data)
Packit ae235b
{
Packit ae235b
  GDBusConnection *connection;
Packit ae235b
  GTask *task;
Packit ae235b
  GFile *file;
Packit ae235b
  GVariant *opts = NULL;
Packit ae235b
  int i;
Packit ae235b
  guint signal_id;
Packit ae235b
Packit ae235b
  if (!init_openuri_portal ())
Packit ae235b
    {
Packit ae235b
      g_task_report_new_error (NULL, callback, user_data, NULL,
Packit ae235b
                               G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED,
Packit ae235b
                               "OpenURI portal is not available");
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (openuri));
Packit ae235b
Packit ae235b
  if (callback)
Packit ae235b
    {
Packit ae235b
      GVariantBuilder opt_builder;
Packit ae235b
      char *token;
Packit ae235b
      char *sender;
Packit ae235b
      char *handle;
Packit ae235b
Packit ae235b
      task = g_task_new (NULL, cancellable, callback, user_data);
Packit ae235b
Packit ae235b
      token = g_strdup_printf ("gio%d", g_random_int_range (0, G_MAXINT));
Packit ae235b
      sender = g_strdup (g_dbus_connection_get_unique_name (connection) + 1);
Packit ae235b
      for (i = 0; sender[i]; i++)
Packit ae235b
        if (sender[i] == '.')
Packit ae235b
          sender[i] = '_';
Packit ae235b
Packit ae235b
      handle = g_strdup_printf ("/org/fredesktop/portal/desktop/request/%s/%s", sender, token);
Packit ae235b
      g_object_set_data_full (G_OBJECT (task), "handle", handle, g_free);
Packit ae235b
      g_free (sender);
Packit ae235b
Packit ae235b
      signal_id = g_dbus_connection_signal_subscribe (connection,
Packit ae235b
                                                      "org.freedesktop.portal.Desktop",
Packit ae235b
                                                      "org.freedesktop.portal.Request",
Packit ae235b
                                                      "Response",
Packit ae235b
                                                      handle,
Packit ae235b
                                                      NULL,
Packit ae235b
                                                      G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE,
Packit ae235b
                                                      response_received,
Packit ae235b
                                                      task,
Packit ae235b
                                                      NULL);
Packit ae235b
      g_object_set_data (G_OBJECT (task), "signal-id", GINT_TO_POINTER (signal_id));
Packit ae235b
Packit ae235b
      g_variant_builder_init (&opt_builder, G_VARIANT_TYPE_VARDICT);
Packit ae235b
      g_variant_builder_add (&opt_builder, "{sv}", "handle_token", g_variant_new_string (token));
Packit ae235b
      g_free (token);
Packit ae235b
Packit ae235b
      opts = g_variant_builder_end (&opt_builder);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    task = NULL;
Packit ae235b
Packit ae235b
  file = g_file_new_for_uri (uri);
Packit ae235b
  if (g_file_is_native (file))
Packit ae235b
    {
Packit ae235b
      char *path = NULL;
Packit ae235b
      GUnixFDList *fd_list = NULL;
Packit ae235b
      int fd, fd_id, errsv;
Packit ae235b
Packit ae235b
      if (task)
Packit ae235b
        g_object_set_data (G_OBJECT (task), "open-file", GINT_TO_POINTER (TRUE));
Packit ae235b
Packit ae235b
      path = g_file_get_path (file);
Packit ae235b
      fd = g_open (path, O_PATH | O_CLOEXEC);
Packit ae235b
      errsv = errno;
Packit ae235b
      if (fd == -1)
Packit ae235b
        {
Packit ae235b
          g_task_report_new_error (NULL, callback, user_data, NULL,
Packit ae235b
                                   G_IO_ERROR, g_io_error_from_errno (errsv),
Packit ae235b
                                   "OpenURI portal is not available");
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
Packit ae235b
#ifndef HAVE_O_CLOEXEC
Packit ae235b
      fcntl (fd, F_SETFD, FD_CLOEXEC);
Packit ae235b
#endif
Packit ae235b
      fd_list = g_unix_fd_list_new_from_array (&fd, 1);
Packit ae235b
      fd = -1;
Packit ae235b
      fd_id = 0;
Packit ae235b
Packit ae235b
      gxdp_open_uri_call_open_file (openuri,
Packit ae235b
                                    parent_window ? parent_window : "",
Packit ae235b
                                    g_variant_new ("h", fd_id),
Packit ae235b
                                    opts,
Packit ae235b
                                    fd_list,
Packit ae235b
                                    cancellable,
Packit ae235b
                                    task ? open_call_done : NULL,
Packit ae235b
                                    task);
Packit ae235b
      g_object_unref (fd_list);
Packit ae235b
      g_free (path);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      gxdp_open_uri_call_open_uri (openuri,
Packit ae235b
                                   parent_window ? parent_window : "",
Packit ae235b
                                   uri,
Packit ae235b
                                   opts,
Packit ae235b
                                   cancellable,
Packit ae235b
                                   task ? open_call_done : NULL,
Packit ae235b
                                   task);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_object_unref (file);
Packit ae235b
}
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
g_openuri_portal_open_uri_finish (GAsyncResult  *result,
Packit ae235b
                                  GError       **error)
Packit ae235b
{
Packit ae235b
  return g_task_propagate_boolean (G_TASK (result), error);
Packit ae235b
}