Blame gio/gapplication-tool.c

Packit ae235b
/*
Packit ae235b
 * Copyright © 2013 Canonical Limited
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: Ryan Lortie <desrt@desrt.ca>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <gio/gdesktopappinfo.h>
Packit ae235b
Packit ae235b
#include <glib/gi18n.h>
Packit ae235b
#include <gio/gio.h>
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
#include <locale.h>
Packit ae235b
Packit ae235b
struct help_topic
Packit ae235b
{
Packit ae235b
  const gchar *command;
Packit ae235b
  const gchar *summary;
Packit ae235b
  const gchar *description;
Packit ae235b
  const gchar *synopsis;
Packit ae235b
};
Packit ae235b
Packit ae235b
struct help_substvar
Packit ae235b
{
Packit ae235b
  const gchar *var;
Packit ae235b
  const gchar *description;
Packit ae235b
};
Packit ae235b
Packit ae235b
static const struct help_topic topics[] = {
Packit ae235b
  { "help",         N_("Print help"),
Packit ae235b
                    N_("Print help"),
Packit ae235b
                    N_("[COMMAND]")
Packit ae235b
  },
Packit ae235b
  { "version",      N_("Print version"),
Packit ae235b
                    N_("Print version information and exit")
Packit ae235b
  },
Packit ae235b
  { "list-apps",    N_("List applications"),
Packit ae235b
                    N_("List the installed D-Bus activatable applications (by .desktop files)")
Packit ae235b
  },
Packit ae235b
  { "launch",       N_("Launch an application"),
Packit ae235b
                    N_("Launch the application (with optional files to open)"),
Packit ae235b
                    N_("APPID [FILE…]")
Packit ae235b
  },
Packit ae235b
  { "action",       N_("Activate an action"),
Packit ae235b
                    N_("Invoke an action on the application"),
Packit ae235b
                    N_("APPID ACTION [PARAMETER]")
Packit ae235b
  },
Packit ae235b
  { "list-actions", N_("List available actions"),
Packit ae235b
                    N_("List static actions for an application (from .desktop file)"),
Packit ae235b
                    N_("APPID")
Packit ae235b
  }
Packit ae235b
};
Packit ae235b
Packit ae235b
static const struct help_substvar substvars[] = {
Packit ae235b
  { N_("COMMAND"),   N_("The command to print detailed help for")                             },
Packit ae235b
  { N_("APPID"),     N_("Application identifier in D-Bus format (eg: org.example.viewer)")    },
Packit ae235b
  { N_("FILE"),      N_("Optional relative or absolute filenames, or URIs to open")           },
Packit ae235b
  { N_("ACTION"),    N_("The action name to invoke")                                          },
Packit ae235b
  { N_("PARAMETER"), N_("Optional parameter to the action invocation, in GVariant format")    }
Packit ae235b
};
Packit ae235b
Packit ae235b
static int
Packit ae235b
app_help (gboolean     requested,
Packit ae235b
          const gchar *command)
Packit ae235b
{
Packit ae235b
  const struct help_topic *topic = NULL;
Packit ae235b
  GString *string;
Packit ae235b
Packit ae235b
  string = g_string_new (NULL);
Packit ae235b
Packit ae235b
  if (command)
Packit ae235b
    {
Packit ae235b
      gint i;
Packit ae235b
Packit ae235b
      for (i = 0; i < G_N_ELEMENTS (topics); i++)
Packit ae235b
        if (g_str_equal (topics[i].command, command))
Packit ae235b
          topic = &topics[i];
Packit ae235b
Packit ae235b
      if (!topic)
Packit ae235b
        {
Packit ae235b
          g_string_printf (string, _("Unknown command %s\n\n"), command);
Packit ae235b
          requested = FALSE;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_string_append (string, _("Usage:\n"));
Packit ae235b
Packit ae235b
  if (topic)
Packit ae235b
    {
Packit ae235b
      gint maxwidth;
Packit ae235b
      gint i;
Packit ae235b
Packit ae235b
      g_string_append_printf (string, "\n  %s %s %s\n\n", "gapplication",
Packit ae235b
                              topic->command, topic->synopsis ? _(topic->synopsis) : "");
Packit ae235b
      g_string_append_printf (string, "%s\n\n", _(topic->description));
Packit ae235b
Packit ae235b
      if (topic->synopsis)
Packit ae235b
        {
Packit ae235b
          g_string_append (string, _("Arguments:\n"));
Packit ae235b
Packit ae235b
          maxwidth = 0;
Packit ae235b
          for (i = 0; i < G_N_ELEMENTS (substvars); i++)
Packit ae235b
            if (strstr (topic->synopsis, substvars[i].var))
Packit ae235b
              maxwidth = MAX(maxwidth, strlen (_(substvars[i].var)));
Packit ae235b
Packit ae235b
          for (i = 0; i < G_N_ELEMENTS (substvars); i++)
Packit ae235b
            if (strstr (topic->synopsis, substvars[i].var))
Packit ae235b
              g_string_append_printf (string, "  %-*.*s   %s\n", maxwidth, maxwidth,
Packit ae235b
                                      _(substvars[i].var), _(substvars[i].description));
Packit ae235b
          g_string_append (string, "\n");
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      gint maxwidth;
Packit ae235b
      gint i;
Packit ae235b
Packit ae235b
      g_string_append_printf (string, "\n  %s %s %s\n\n", "gapplication", _("COMMAND"), _("[ARGS…]"));
Packit ae235b
      g_string_append_printf (string, _("Commands:\n"));
Packit ae235b
Packit ae235b
      maxwidth = 0;
Packit ae235b
      for (i = 0; i < G_N_ELEMENTS (topics); i++)
Packit ae235b
        maxwidth = MAX(maxwidth, strlen (topics[i].command));
Packit ae235b
Packit ae235b
      for (i = 0; i < G_N_ELEMENTS (topics); i++)
Packit ae235b
        g_string_append_printf (string, "  %-*.*s   %s\n", maxwidth, maxwidth,
Packit ae235b
                                topics[i].command, _(topics[i].summary));
Packit ae235b
Packit ae235b
      g_string_append (string, "\n");
Packit ae235b
      /* Translators: do not translate 'help', but please translate 'COMMAND'. */
Packit ae235b
      g_string_append_printf (string, _("Use “%s help COMMAND” to get detailed help.\n\n"), "gapplication");
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (requested)
Packit ae235b
    g_print ("%s", string->str);
Packit ae235b
  else
Packit ae235b
    g_printerr ("%s\n", string->str);
Packit ae235b
Packit ae235b
  g_string_free (string, TRUE);
Packit ae235b
Packit ae235b
  return requested ? 0 : 1;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
app_check_name (gchar       **args,
Packit ae235b
                const gchar  *command)
Packit ae235b
{
Packit ae235b
  if (args[0] == NULL)
Packit ae235b
    {
Packit ae235b
      g_printerr (_("%s command requires an application id to directly follow\n\n"), command);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_dbus_is_name (args[0]))
Packit ae235b
    {
Packit ae235b
      g_printerr (_("invalid application id: “%s”\n"), args[0]);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static int
Packit ae235b
app_no_args (const gchar *command)
Packit ae235b
{
Packit ae235b
  /* Translators: %s is replaced with a command name like 'list-actions' */
Packit ae235b
  g_printerr (_("“%s” takes no arguments\n\n"), command);
Packit ae235b
  return app_help (FALSE, command);
Packit ae235b
}
Packit ae235b
Packit ae235b
static int
Packit ae235b
app_version (gchar **args)
Packit ae235b
{
Packit ae235b
  if (g_strv_length (args))
Packit ae235b
    return app_no_args ("version");
Packit ae235b
Packit ae235b
  g_print (PACKAGE_VERSION "\n");
Packit ae235b
  return 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
static int
Packit ae235b
app_list (gchar **args)
Packit ae235b
{
Packit ae235b
  GList *apps;
Packit ae235b
Packit ae235b
  if (g_strv_length (args))
Packit ae235b
    return app_no_args ("list");
Packit ae235b
Packit ae235b
  apps = g_app_info_get_all ();
Packit ae235b
Packit ae235b
  while (apps)
Packit ae235b
    {
Packit ae235b
      GDesktopAppInfo *info = apps->data;
Packit ae235b
Packit ae235b
      if (G_IS_DESKTOP_APP_INFO (info))
Packit ae235b
        if (g_desktop_app_info_get_boolean (info, "DBusActivatable"))
Packit ae235b
          {
Packit ae235b
            const gchar *filename;
Packit ae235b
Packit ae235b
            filename = g_app_info_get_id (G_APP_INFO (info));
Packit ae235b
            if (g_str_has_suffix (filename, ".desktop"))
Packit ae235b
              {
Packit ae235b
                gchar *id;
Packit ae235b
Packit ae235b
                id = g_strndup (filename, strlen (filename) - 8);
Packit ae235b
                g_print ("%s\n", id);
Packit ae235b
                g_free (id);
Packit ae235b
              }
Packit ae235b
          }
Packit ae235b
Packit ae235b
      apps = g_list_delete_link (apps, apps);
Packit ae235b
      g_object_unref (info);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gchar *
Packit ae235b
app_path_for_id (const gchar *app_id)
Packit ae235b
{
Packit ae235b
  gchar *path;
Packit ae235b
  gint i;
Packit ae235b
Packit ae235b
  path = g_strconcat ("/", app_id, NULL);
Packit ae235b
  for (i = 0; path[i]; i++)
Packit ae235b
    {
Packit ae235b
      if (path[i] == '.')
Packit ae235b
        path[i] = '/';
Packit ae235b
      if (path[i] == '-')
Packit ae235b
        path[i] = '_';
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return path;
Packit ae235b
}
Packit ae235b
Packit ae235b
static int
Packit ae235b
app_call (const gchar *app_id,
Packit ae235b
          const gchar *method_name,
Packit ae235b
          GVariant    *parameters)
Packit ae235b
{
Packit ae235b
  GDBusConnection *session;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  gchar *object_path;
Packit ae235b
  GVariant *result;
Packit ae235b
Packit ae235b
Packit ae235b
  session = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
Packit ae235b
  if (!session)
Packit ae235b
    {
Packit ae235b
      g_variant_unref (g_variant_ref_sink (parameters));
Packit ae235b
      g_printerr (_("unable to connect to D-Bus: %s\n"), error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  object_path = app_path_for_id (app_id);
Packit ae235b
Packit ae235b
  result = g_dbus_connection_call_sync (session, app_id, object_path, "org.freedesktop.Application",
Packit ae235b
                                        method_name, parameters, G_VARIANT_TYPE_UNIT,
Packit ae235b
                                        G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
Packit ae235b
Packit ae235b
  g_free (object_path);
Packit ae235b
Packit ae235b
  if (result)
Packit ae235b
    {
Packit ae235b
      g_variant_unref (result);
Packit ae235b
      return 0;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_printerr (_("error sending %s message to application: %s\n"), method_name, error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static GVariant *
Packit ae235b
app_get_platform_data (void)
Packit ae235b
{
Packit ae235b
  GVariantBuilder builder;
Packit ae235b
  const gchar *startup_id;
Packit ae235b
Packit ae235b
  g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
Packit ae235b
Packit ae235b
  if ((startup_id = g_getenv ("DESKTOP_STARTUP_ID")))
Packit ae235b
    g_variant_builder_add (&builder, "{sv}", "desktop-startup-id", g_variant_new_string (startup_id));
Packit ae235b
Packit ae235b
  return g_variant_builder_end (&builder);
Packit ae235b
}
Packit ae235b
Packit ae235b
static int
Packit ae235b
app_action (gchar **args)
Packit ae235b
{
Packit ae235b
  GVariantBuilder params;
Packit ae235b
  const gchar *name;
Packit ae235b
Packit ae235b
  if (!app_check_name (args, "action"))
Packit ae235b
    return 1;
Packit ae235b
Packit ae235b
  if (args[1] == NULL)
Packit ae235b
    {
Packit ae235b
      g_printerr (_("action name must be given after application id\n"));
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  name = args[1];
Packit ae235b
Packit ae235b
  if (!g_action_name_is_valid (name))
Packit ae235b
    {
Packit ae235b
      g_printerr (_("invalid action name: “%s”\n"
Packit ae235b
                    "action names must consist of only alphanumerics, “-” and “.”\n"), name);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_variant_builder_init (&params, G_VARIANT_TYPE ("av"));
Packit ae235b
Packit ae235b
  if (args[2])
Packit ae235b
    {
Packit ae235b
      GError *error = NULL;
Packit ae235b
      GVariant *parameter;
Packit ae235b
Packit ae235b
      parameter = g_variant_parse (NULL, args[2], NULL, NULL, &error);
Packit ae235b
Packit ae235b
      if (!parameter)
Packit ae235b
        {
Packit ae235b
          gchar *context;
Packit ae235b
Packit ae235b
          context = g_variant_parse_error_print_context (error, args[2]);
Packit ae235b
          g_printerr (_("error parsing action parameter: %s\n"), context);
Packit ae235b
          g_variant_builder_clear (&params);
Packit ae235b
          g_error_free (error);
Packit ae235b
          g_free (context);
Packit ae235b
          return 1;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_variant_builder_add (&params, "v", parameter);
Packit ae235b
      g_variant_unref (parameter);
Packit ae235b
Packit ae235b
      if (args[3])
Packit ae235b
        {
Packit ae235b
          g_printerr (_("actions accept a maximum of one parameter\n"));
Packit ae235b
          g_variant_builder_clear (&params);
Packit ae235b
          return 1;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return app_call (args[0], "ActivateAction", g_variant_new ("(sav@a{sv})", name, &params, app_get_platform_data ()));
Packit ae235b
}
Packit ae235b
Packit ae235b
static int
Packit ae235b
app_activate (const gchar *app_id)
Packit ae235b
{
Packit ae235b
  return app_call (app_id, "Activate", g_variant_new ("(@a{sv})", app_get_platform_data ()));
Packit ae235b
}
Packit ae235b
Packit ae235b
static int
Packit ae235b
app_launch (gchar **args)
Packit ae235b
{
Packit ae235b
  GVariantBuilder files;
Packit ae235b
  gint i;
Packit ae235b
Packit ae235b
  if (!app_check_name (args, "launch"))
Packit ae235b
    return 1;
Packit ae235b
Packit ae235b
  if (args[1] == NULL)
Packit ae235b
    return app_activate (args[0]);
Packit ae235b
Packit ae235b
  g_variant_builder_init (&files, G_VARIANT_TYPE_STRING_ARRAY);
Packit ae235b
Packit ae235b
  for (i = 1; args[i]; i++)
Packit ae235b
    {
Packit ae235b
      GFile *file;
Packit ae235b
Packit ae235b
      /* "This operation never fails" */
Packit ae235b
      file = g_file_new_for_commandline_arg (args[i]);
Packit ae235b
      g_variant_builder_add_value (&files, g_variant_new_take_string (g_file_get_uri (file)));
Packit ae235b
      g_object_unref (file);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return app_call (args[0], "Open", g_variant_new ("(as@a{sv})", &files, app_get_platform_data ()));
Packit ae235b
}
Packit ae235b
Packit ae235b
static int
Packit ae235b
app_list_actions (gchar **args)
Packit ae235b
{
Packit ae235b
  const gchar * const *actions;
Packit ae235b
  GDesktopAppInfo *app_info;
Packit ae235b
  gchar *filename;
Packit ae235b
  gint i;
Packit ae235b
Packit ae235b
  if (!app_check_name (args, "list-actions"))
Packit ae235b
    return 1;
Packit ae235b
Packit ae235b
  if (args[1])
Packit ae235b
    {
Packit ae235b
      g_printerr (_("list-actions command takes only the application id"));
Packit ae235b
      app_help (FALSE, "list-actions");
Packit ae235b
    }
Packit ae235b
Packit ae235b
  filename = g_strconcat (args[0], ".desktop", NULL);
Packit ae235b
  app_info = g_desktop_app_info_new (filename);
Packit ae235b
  g_free (filename);
Packit ae235b
Packit ae235b
  if (app_info == NULL)
Packit ae235b
    {
Packit ae235b
      g_printerr (_("unable to find desktop file for application %s\n"), args[0]);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  actions = g_desktop_app_info_list_actions (app_info);
Packit ae235b
Packit ae235b
  for (i = 0; actions[i]; i++)
Packit ae235b
    g_print ("%s\n", actions[i]);
Packit ae235b
Packit ae235b
  g_object_unref (app_info);
Packit ae235b
Packit ae235b
  return 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int argc, char **argv)
Packit ae235b
{
Packit ae235b
  setlocale (LC_ALL, "");
Packit ae235b
  textdomain (GETTEXT_PACKAGE);
Packit ae235b
  bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
Packit ae235b
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
Packit ae235b
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (argc < 2)
Packit ae235b
    return app_help (TRUE, NULL);
Packit ae235b
Packit ae235b
  if (g_str_equal (argv[1], "help"))
Packit ae235b
    return app_help (TRUE, argv[2]);
Packit ae235b
Packit ae235b
  if (g_str_equal (argv[1], "version"))
Packit ae235b
    return app_version (argv + 2);
Packit ae235b
Packit ae235b
  if (g_str_equal (argv[1], "list-apps"))
Packit ae235b
    return app_list (argv + 2);
Packit ae235b
Packit ae235b
  if (g_str_equal (argv[1], "launch"))
Packit ae235b
    return app_launch (argv + 2);
Packit ae235b
Packit ae235b
  if (g_str_equal (argv[1], "action"))
Packit ae235b
    return app_action (argv + 2);
Packit ae235b
Packit ae235b
  if (g_str_equal (argv[1], "list-actions"))
Packit ae235b
    return app_list_actions (argv + 2);
Packit ae235b
Packit ae235b
  g_printerr (_("unrecognised command: %s\n\n"), argv[1]);
Packit ae235b
Packit ae235b
  return app_help (FALSE, NULL);
Packit ae235b
}