Blame gio/gio-tool-open.c

Packit ae235b
/*
Packit ae235b
 * Copyright 2015 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
 * Author: Matthias Clasen <mclasen@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <gio/gio.h>
Packit ae235b
Packit ae235b
#if defined(G_OS_UNIX) && !defined(HAVE_COCOA)
Packit ae235b
#include <gio/gdesktopappinfo.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include <gi18n.h>
Packit ae235b
Packit ae235b
#include "gio-tool.h"
Packit ae235b
Packit ae235b
Packit ae235b
static const GOptionEntry entries[] = {
Packit ae235b
  { NULL }
Packit ae235b
};
Packit ae235b
Packit ae235b
#if defined(G_OS_UNIX) && !defined(HAVE_COCOA)
Packit ae235b
static gboolean
Packit ae235b
get_bus_name_and_path_from_uri (const char *uri,
Packit ae235b
                                char **bus_name_out,
Packit ae235b
                                char **object_path_out)
Packit ae235b
{
Packit ae235b
  GAppInfo *app_info = NULL;
Packit ae235b
  char *bus_name = NULL;
Packit ae235b
  char *object_path = NULL;
Packit ae235b
  char *uri_scheme;
Packit ae235b
  const char *filename;
Packit ae235b
  char *basename = NULL;
Packit ae235b
  char *p;
Packit ae235b
  gboolean got_name = FALSE;
Packit ae235b
Packit ae235b
  uri_scheme = g_uri_parse_scheme (uri);
Packit ae235b
  if (uri_scheme && uri_scheme[0] != '\0')
Packit ae235b
    app_info = g_app_info_get_default_for_uri_scheme (uri_scheme);
Packit ae235b
  g_free (uri_scheme);
Packit ae235b
Packit ae235b
  if (app_info == NULL)
Packit ae235b
    {
Packit ae235b
      GFile *file;
Packit ae235b
Packit ae235b
      file = g_file_new_for_uri (uri);
Packit ae235b
      app_info = g_file_query_default_handler (file, NULL, NULL);
Packit ae235b
      g_object_unref (file);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (app_info == NULL || !G_IS_DESKTOP_APP_INFO (app_info) ||
Packit ae235b
      !g_desktop_app_info_get_boolean (G_DESKTOP_APP_INFO (app_info), "DBusActivatable"))
Packit ae235b
    goto out;
Packit ae235b
Packit ae235b
  filename = g_desktop_app_info_get_filename (G_DESKTOP_APP_INFO (app_info));
Packit ae235b
  if (filename == NULL)
Packit ae235b
    goto out;
Packit ae235b
Packit ae235b
  basename = g_path_get_basename (filename);
Packit ae235b
  if (!g_str_has_suffix (basename, ".desktop"))
Packit ae235b
    goto out;
Packit ae235b
Packit ae235b
  basename[strlen (basename) - strlen (".desktop")] = '\0';
Packit ae235b
  if (!g_dbus_is_name (basename))
Packit ae235b
    goto out;
Packit ae235b
Packit ae235b
  bus_name = g_strdup (basename);
Packit ae235b
  object_path = g_strdup_printf ("/%s", bus_name);
Packit ae235b
  for (p = object_path; *p != '\0'; p++)
Packit ae235b
    if (*p == '.')
Packit ae235b
      *p = '/';
Packit ae235b
Packit ae235b
  *bus_name_out = g_steal_pointer (&bus_name);
Packit ae235b
  *object_path_out = g_steal_pointer (&object_path);
Packit ae235b
  got_name = TRUE;
Packit ae235b
Packit ae235b
out:
Packit ae235b
  g_clear_object (&app_info);
Packit ae235b
  g_clear_pointer (&basename, g_free);
Packit ae235b
Packit ae235b
  return got_name;
Packit ae235b
}
Packit ae235b
#endif
Packit ae235b
Packit ae235b
int
Packit ae235b
handle_open (int argc, char *argv[], gboolean do_help)
Packit ae235b
{
Packit ae235b
  GOptionContext *context;
Packit ae235b
  gchar *param;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  int i;
Packit ae235b
  gboolean success;
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  g_set_prgname ("gio open");
Packit ae235b
Packit ae235b
  /* Translators: commandline placeholder */
Packit ae235b
  param = g_strdup_printf ("%s...", _("LOCATION"));
Packit ae235b
  context = g_option_context_new (param);
Packit ae235b
  g_free (param);
Packit ae235b
  g_option_context_set_help_enabled (context, FALSE);
Packit ae235b
  g_option_context_set_summary (context,
Packit ae235b
      _("Open files with the default application that\n"
Packit ae235b
        "is registered to handle files of this type."));
Packit ae235b
  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
Packit ae235b
Packit ae235b
  if (do_help)
Packit ae235b
    {
Packit ae235b
      show_help (context, NULL);
Packit ae235b
      g_option_context_free (context);
Packit ae235b
      return 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_option_context_parse (context, &argc, &argv, &error))
Packit ae235b
    {
Packit ae235b
      show_help (context, error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
      g_option_context_free (context);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (argc < 2)
Packit ae235b
    {
Packit ae235b
      show_help (context, _("No locations given"));
Packit ae235b
      g_option_context_free (context);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_option_context_free (context);
Packit ae235b
Packit ae235b
  success = TRUE;
Packit ae235b
  for (i = 1; i < argc; i++)
Packit ae235b
    {
Packit ae235b
      char *uri = NULL;
Packit ae235b
      char *uri_scheme;
Packit ae235b
Packit ae235b
      /* Workaround to handle non-URI locations. We still use the original
Packit ae235b
       * location for other cases, because GFile might modify the URI in ways
Packit ae235b
       * we don't want. See:
Packit ae235b
       * https://bugzilla.gnome.org/show_bug.cgi?id=779182 */
Packit ae235b
      uri_scheme = g_uri_parse_scheme (argv[i]);
Packit ae235b
      if (!uri_scheme || uri_scheme[0] == '\0')
Packit ae235b
        {
Packit ae235b
          GFile *file;
Packit ae235b
Packit ae235b
          file = g_file_new_for_commandline_arg (argv[i]);
Packit ae235b
          uri = g_file_get_uri (file);
Packit ae235b
          g_object_unref (file);
Packit ae235b
        }
Packit ae235b
      g_free (uri_scheme);
Packit ae235b
Packit ae235b
      res = g_app_info_launch_default_for_uri (uri ? uri : argv[i], NULL, &error);
Packit ae235b
      if (!res)
Packit ae235b
	{
Packit ae235b
          print_error ("%s: %s", uri ? uri : argv[i], error->message);
Packit ae235b
	  g_clear_error (&error);
Packit ae235b
	  success = FALSE;
Packit ae235b
	}
Packit ae235b
Packit ae235b
#if defined(G_OS_UNIX) && !defined(HAVE_COCOA)
Packit ae235b
      /* FIXME: This chunk of madness is a workaround for a dbus-daemon bug.
Packit ae235b
       * See https://bugzilla.gnome.org/show_bug.cgi?id=780296
Packit ae235b
       */
Packit ae235b
      if (res)
Packit ae235b
        {
Packit ae235b
          char *bus_name = NULL;
Packit ae235b
          char *object_path = NULL;
Packit ae235b
Packit ae235b
          if (get_bus_name_and_path_from_uri (uri ? uri : argv[i], &bus_name, &object_path))
Packit ae235b
            {
Packit ae235b
              GDBusConnection *connection;
Packit ae235b
              connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
Packit ae235b
Packit ae235b
              if (connection)
Packit ae235b
                g_dbus_connection_call_sync (connection,
Packit ae235b
                                             bus_name,
Packit ae235b
                                             object_path,
Packit ae235b
                                             "org.freedesktop.DBus.Peer",
Packit ae235b
                                             "Ping",
Packit ae235b
                                             NULL, NULL,
Packit ae235b
                                             G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
Packit ae235b
              g_clear_object (&connection);
Packit ae235b
              g_free (bus_name);
Packit ae235b
              g_free (object_path);
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
      g_free (uri);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return success ? 0 : 2;
Packit ae235b
}