gtk-launch.c 6.05 KB
Newer Older
Tomáš Bžatek's avatar
Tomáš Bžatek committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/* GTK - The GIMP Toolkit
 *
 * Copyright (C) 2012 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Tomas Bzatek <tbzatek@redhat.com>
 */

#include <config.h>

#include <stdio.h>
#include <unistd.h>
#include <locale.h>
#include <errno.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <gio/gio.h>
Christoph Reiter's avatar
Christoph Reiter committed
31
#if defined(HAVE_GIO_UNIX) && !defined(__APPLE__)
Tomáš Bžatek's avatar
Tomáš Bžatek committed
32
#include <gio/gdesktopappinfo.h>
Hib Eris's avatar
Hib Eris committed
33
#endif
Tomáš Bžatek's avatar
Tomáš Bžatek committed
34 35
#include <gtk.h>

36
static gboolean show_version;
Tomáš Bžatek's avatar
Tomáš Bžatek committed
37 38 39
static gchar **args = NULL;

static GOptionEntry entries[] = {
40
  { "version", 0, 0, G_OPTION_ARG_NONE, &show_version, N_("Show program version"), NULL },
Tomáš Bžatek's avatar
Tomáš Bžatek committed
41 42 43 44 45 46 47 48 49 50 51
  { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &args, NULL, NULL },
  { NULL}
};

int
main (int argc, char *argv[])
{
  GError *error = NULL;
  GOptionContext *context = NULL;
  gchar *summary;
  gchar *app_name;
Hib Eris's avatar
Hib Eris committed
52
#ifdef G_OS_UNIX
Tomáš Bžatek's avatar
Tomáš Bžatek committed
53
  gchar *desktop_file_name;
54
  gchar *bus_name = NULL;
Hib Eris's avatar
Hib Eris committed
55 56
#endif
  GAppInfo *info = NULL;
Tomáš Bžatek's avatar
Tomáš Bžatek committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  GAppLaunchContext *launch_context;
  GList *l;
  GFile *f;

  setlocale (LC_ALL, "");

#ifdef ENABLE_NLS
  bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
  textdomain (GETTEXT_PACKAGE);
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
#endif
#endif

  /* Translators: this message will appear immediately after the */
72
  /* usage string - Usage: COMMAND [OPTION...] <THIS_MESSAGE>    */
Tomáš Bžatek's avatar
Tomáš Bžatek committed
73
  context =
74
    g_option_context_new (_("APPLICATION [URI...] — launch an APPLICATION"));
Tomáš Bžatek's avatar
Tomáš Bžatek committed
75 76 77

  /* Translators: this message will appear after the usage string */
  /* and before the list of options.                              */
78 79
  summary = _("Launch an application (specified by its desktop file name),\n"
              "optionally passing one or more URIs as arguments.");
Tomáš Bžatek's avatar
Tomáš Bžatek committed
80 81 82 83 84 85 86 87 88 89 90 91
  g_option_context_set_summary (context, summary);
  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
  g_option_context_add_group (context, gtk_get_option_group (FALSE));

  g_option_context_parse (context, &argc, &argv, &error);

  g_option_context_free (context);

  if (error != NULL)
    {
      g_printerr (_("Error parsing commandline options: %s\n"), error->message);
      g_printerr ("\n");
92
      g_printerr (_("Try \"%s --help\" for more information."), g_get_prgname ());
Tomáš Bžatek's avatar
Tomáš Bžatek committed
93
      g_printerr ("\n");
94
      g_error_free (error);
Tomáš Bžatek's avatar
Tomáš Bžatek committed
95 96 97
      return 1;
    }

98 99 100 101 102 103 104 105 106
  if (show_version)
    {
      g_print ("%d.%d.%d\n",
               gtk_get_major_version (),
               gtk_get_minor_version (),
               gtk_get_micro_version ());
      return 0;
    }

Tomáš Bžatek's avatar
Tomáš Bžatek committed
107 108 109 110 111 112
  if (!args)
    {
      /* Translators: the %s is the program name. This error message */
      /* means the user is calling gtk-launch without any argument.  */
      g_printerr (_("%s: missing application name"), g_get_prgname ());
      g_printerr ("\n");
113
      g_printerr (_("Try \"%s --help\" for more information."), g_get_prgname ());
Tomáš Bžatek's avatar
Tomáš Bžatek committed
114 115 116 117 118 119 120 121
      g_printerr ("\n");
      return 1;
    }


  gtk_init (&argc, &argv);

  app_name = *args;
Christoph Reiter's avatar
Christoph Reiter committed
122
#if defined(HAVE_GIO_UNIX) && !defined(__APPLE__)
123
  bus_name = g_strdup (app_name);
Tomáš Bžatek's avatar
Tomáš Bžatek committed
124
  if (g_str_has_suffix (app_name, ".desktop"))
125 126 127 128
    {
      desktop_file_name = g_strdup (app_name);
      bus_name[strlen (bus_name) - strlen(".desktop")] = '\0';
    }
129
  else
130 131 132 133 134 135
    {
      desktop_file_name = g_strconcat (app_name, ".desktop", NULL);
    }

  if (!g_dbus_is_name (bus_name))
    g_clear_pointer (&bus_name, g_free);
Hib Eris's avatar
Hib Eris committed
136
  info = G_APP_INFO (g_desktop_app_info_new (desktop_file_name));
Tomáš Bžatek's avatar
Tomáš Bžatek committed
137
  g_free (desktop_file_name);
Hib Eris's avatar
Hib Eris committed
138 139 140 141
#else
#warning Please add support for creating AppInfo from id for your OS
  g_printerr (_("Creating AppInfo from id not supported on non unix operating systems"));
#endif
Tomáš Bžatek's avatar
Tomáš Bžatek committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  args++;

  if (!info)
    {
      /* Translators: the first %s is the program name, the second one */
      /* is the application name.                                      */
      g_printerr (_("%s: no such application %s"),
                  g_get_prgname (), app_name);
      g_printerr ("\n");
      return 2;
    }

  l = NULL;
  for (; *args; args++)
    {
      f = g_file_new_for_commandline_arg (*args);
      l = g_list_append (l, f);
    }

  launch_context = (GAppLaunchContext*) gdk_display_get_app_launch_context (gdk_display_get_default ());
Hib Eris's avatar
Hib Eris committed
162
  if (!g_app_info_launch (info, l, launch_context, &error))
Tomáš Bžatek's avatar
Tomáš Bžatek committed
163 164 165 166 167 168 169 170 171
    {
       /* Translators: the first %s is the program name, the second one  */
       /* is the error message.                                          */
       g_printerr (_("%s: error launching application: %s\n"),
                   g_get_prgname (), error->message);
       return 3;
    }
  g_object_unref (info);
  g_object_unref (launch_context);
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

#ifdef G_OS_UNIX
  if (bus_name != NULL)
    {
      GDBusConnection *connection;
      gchar *object_path, *p;

      connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);

      object_path = g_strdup_printf ("/%s", bus_name);
      for (p = object_path; *p != '\0'; p++)
          if (*p == '.')
              *p = '/';

      if (connection)
        g_dbus_connection_call_sync (connection,
                                     bus_name,
                                     object_path,
                                     "org.freedesktop.DBus.Peer",
                                     "Ping",
                                     NULL, NULL,
                                     G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
      g_clear_pointer (&object_path, g_free);
      g_clear_object (&connection);
      g_clear_pointer (&bus_name, g_free);
    }
#endif
Tomáš Bžatek's avatar
Tomáš Bžatek committed
199 200 201 202
  g_list_free_full (l, g_object_unref);

  return 0;
}