gtk-launch.c 4.63 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>
Hib Eris's avatar
Hib Eris committed
31
#ifdef G_OS_UNIX
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <gtk.h>

static gchar **args = NULL;

static GOptionEntry entries[] = {
  { 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
50
#ifdef G_OS_UNIX
Tomáš Bžatek's avatar
Tomáš Bžatek committed
51
  gchar *desktop_file_name;
Hib Eris's avatar
Hib Eris committed
52 53
#endif
  GAppInfo *info = NULL;
Tomáš Bžatek's avatar
Tomáš Bžatek committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  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 */
69
  /* usage string - Usage: COMMAND [OPTION]… <THIS_MESSAGE>    */
Tomáš Bžatek's avatar
Tomáš Bžatek committed
70
  context =
71
    g_option_context_new (_("APPLICATION [URI…] — launch an APPLICATION with URI."));
Tomáš Bžatek's avatar
Tomáš Bžatek committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

  /* Translators: this message will appear after the usage string */
  /* and before the list of options.                              */
  summary = _("Launch specified application by its desktop file info\n"
              "optionally passing list of URIs as arguments.");
  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");
      g_printerr (_("Try \"%s --help\" for more information."),
                  g_get_prgname ());
      g_printerr ("\n");
      g_error_free(error);
      return 1;
    }

  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");
      g_printerr (_("Try \"%s --help\" for more information."),
                  g_get_prgname ());
      g_printerr ("\n");
      return 1;
    }


  gtk_init (&argc, &argv);

  app_name = *args;
Hib Eris's avatar
Hib Eris committed
112
#ifdef G_OS_UNIX
Tomáš Bžatek's avatar
Tomáš Bžatek committed
113 114 115 116
  if (g_str_has_suffix (app_name, ".desktop"))
    desktop_file_name = g_strdup (app_name);
  else 
    desktop_file_name = g_strconcat (app_name, ".desktop", NULL);
Hib Eris's avatar
Hib Eris committed
117
  info = G_APP_INFO (g_desktop_app_info_new (desktop_file_name));
Tomáš Bžatek's avatar
Tomáš Bžatek committed
118
  g_free (desktop_file_name);
Hib Eris's avatar
Hib Eris committed
119 120 121 122
#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
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  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
143
  if (!g_app_info_launch (info, l, launch_context, &error))
Tomáš Bžatek's avatar
Tomáš Bžatek committed
144 145 146 147 148 149 150 151 152 153 154 155 156
    {
       /* 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);
  g_list_free_full (l, g_object_unref);

  return 0;
}