Blame gio/gapplicationimpl-dbus.c

Packit ae235b
/*
Packit ae235b
 * Copyright © 2010 Codethink 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
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Authors: Ryan Lortie <desrt@desrt.ca>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gapplicationimpl.h"
Packit ae235b
Packit ae235b
#include "gactiongroup.h"
Packit ae235b
#include "gactiongroupexporter.h"
Packit ae235b
#include "gremoteactiongroup.h"
Packit ae235b
#include "gdbusactiongroup-private.h"
Packit ae235b
#include "gapplication.h"
Packit ae235b
#include "gfile.h"
Packit ae235b
#include "gdbusconnection.h"
Packit ae235b
#include "gdbusintrospection.h"
Packit ae235b
#include "gdbuserror.h"
Packit ae235b
#include "glib/gstdio.h"
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
Packit ae235b
#include "gapplicationcommandline.h"
Packit ae235b
#include "gdbusmethodinvocation.h"
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include "gunixinputstream.h"
Packit ae235b
#include "gunixfdlist.h"
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/* DBus Interface definition {{{1 */
Packit ae235b
Packit ae235b
/* For documentation of these interfaces, see
Packit ae235b
 * https://wiki.gnome.org/Projects/GLib/GApplication/DBusAPI
Packit ae235b
 */
Packit ae235b
static const gchar org_gtk_Application_xml[] =
Packit ae235b
  "<node>"
Packit ae235b
    "<interface name='org.gtk.Application'>"
Packit ae235b
      "<method name='Activate'>"
Packit ae235b
        "<arg type='a{sv}' name='platform-data' direction='in'/>"
Packit ae235b
      "</method>"
Packit ae235b
      "<method name='Open'>"
Packit ae235b
        "<arg type='as' name='uris' direction='in'/>"
Packit ae235b
        "<arg type='s' name='hint' direction='in'/>"
Packit ae235b
        "<arg type='a{sv}' name='platform-data' direction='in'/>"
Packit ae235b
      "</method>"
Packit ae235b
      "<method name='CommandLine'>"
Packit ae235b
        "<arg type='o' name='path' direction='in'/>"
Packit ae235b
        "<arg type='aay' name='arguments' direction='in'/>"
Packit ae235b
        "<arg type='a{sv}' name='platform-data' direction='in'/>"
Packit ae235b
        "<arg type='i' name='exit-status' direction='out'/>"
Packit ae235b
      "</method>"
Packit ae235b
    "<property name='Busy' type='b' access='read'/>"
Packit ae235b
    "</interface>"
Packit ae235b
  "</node>";
Packit ae235b
Packit ae235b
static GDBusInterfaceInfo *org_gtk_Application;
Packit ae235b
Packit ae235b
static const gchar org_freedesktop_Application_xml[] =
Packit ae235b
  "<node>"
Packit ae235b
    "<interface name='org.freedesktop.Application'>"
Packit ae235b
      "<method name='Activate'>"
Packit ae235b
        "<arg type='a{sv}' name='platform-data' direction='in'/>"
Packit ae235b
      "</method>"
Packit ae235b
      "<method name='Open'>"
Packit ae235b
        "<arg type='as' name='uris' direction='in'/>"
Packit ae235b
        "<arg type='a{sv}' name='platform-data' direction='in'/>"
Packit ae235b
      "</method>"
Packit ae235b
      "<method name='ActivateAction'>"
Packit ae235b
        "<arg type='s' name='action-name' direction='in'/>"
Packit ae235b
        "<arg type='av' name='parameter' direction='in'/>"
Packit ae235b
        "<arg type='a{sv}' name='platform-data' direction='in'/>"
Packit ae235b
      "</method>"
Packit ae235b
    "</interface>"
Packit ae235b
  "</node>";
Packit ae235b
Packit ae235b
static GDBusInterfaceInfo *org_freedesktop_Application;
Packit ae235b
Packit ae235b
static const gchar org_gtk_private_CommandLine_xml[] =
Packit ae235b
  "<node>"
Packit ae235b
    "<interface name='org.gtk.private.CommandLine'>"
Packit ae235b
      "<method name='Print'>"
Packit ae235b
        "<arg type='s' name='message' direction='in'/>"
Packit ae235b
      "</method>"
Packit ae235b
      "<method name='PrintError'>"
Packit ae235b
        "<arg type='s' name='message' direction='in'/>"
Packit ae235b
      "</method>"
Packit ae235b
    "</interface>"
Packit ae235b
  "</node>";
Packit ae235b
Packit ae235b
static GDBusInterfaceInfo *org_gtk_private_CommandLine;
Packit ae235b
Packit ae235b
/* GApplication implementation {{{1 */
Packit ae235b
struct _GApplicationImpl
Packit ae235b
{
Packit ae235b
  GDBusConnection *session_bus;
Packit ae235b
  GActionGroup    *exported_actions;
Packit ae235b
  const gchar     *bus_name;
Packit ae235b
Packit ae235b
  gchar           *object_path;
Packit ae235b
  guint            object_id;
Packit ae235b
  guint            fdo_object_id;
Packit ae235b
  guint            actions_id;
Packit ae235b
Packit ae235b
  gboolean         properties_live;
Packit ae235b
  gboolean         primary;
Packit ae235b
  gboolean         busy;
Packit ae235b
  gboolean         registered;
Packit ae235b
  GApplication    *app;
Packit ae235b
};
Packit ae235b
Packit ae235b
Packit ae235b
static GApplicationCommandLine *
Packit ae235b
g_dbus_command_line_new (GDBusMethodInvocation *invocation);
Packit ae235b
Packit ae235b
static GVariant *
Packit ae235b
g_application_impl_get_property (GDBusConnection *connection,
Packit ae235b
                                 const gchar  *sender,
Packit ae235b
                                 const gchar  *object_path,
Packit ae235b
                                 const gchar  *interface_name,
Packit ae235b
                                 const gchar  *property_name,
Packit ae235b
                                 GError      **error,
Packit ae235b
                                 gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GApplicationImpl *impl = user_data;
Packit ae235b
Packit ae235b
  if (strcmp (property_name, "Busy") == 0)
Packit ae235b
    return g_variant_new_boolean (impl->busy);
Packit ae235b
Packit ae235b
  g_assert_not_reached ();
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
send_property_change (GApplicationImpl *impl)
Packit ae235b
{
Packit ae235b
  GVariantBuilder builder;
Packit ae235b
Packit ae235b
  g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
Packit ae235b
  g_variant_builder_add (&builder,
Packit ae235b
                         "{sv}",
Packit ae235b
                         "Busy", g_variant_new_boolean (impl->busy));
Packit ae235b
Packit ae235b
  g_dbus_connection_emit_signal (impl->session_bus,
Packit ae235b
                                 NULL,
Packit ae235b
                                 impl->object_path,
Packit ae235b
                                 "org.freedesktop.DBus.Properties",
Packit ae235b
                                 "PropertiesChanged",
Packit ae235b
                                 g_variant_new ("(sa{sv}as)",
Packit ae235b
                                                "org.gtk.Application",
Packit ae235b
                                                &builder,
Packit ae235b
                                                NULL),
Packit ae235b
                                 NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_application_impl_method_call (GDBusConnection       *connection,
Packit ae235b
                                const gchar           *sender,
Packit ae235b
                                const gchar           *object_path,
Packit ae235b
                                const gchar           *interface_name,
Packit ae235b
                                const gchar           *method_name,
Packit ae235b
                                GVariant              *parameters,
Packit ae235b
                                GDBusMethodInvocation *invocation,
Packit ae235b
                                gpointer               user_data)
Packit ae235b
{
Packit ae235b
  GApplicationImpl *impl = user_data;
Packit ae235b
  GApplicationClass *class;
Packit ae235b
Packit ae235b
  class = G_APPLICATION_GET_CLASS (impl->app);
Packit ae235b
Packit ae235b
  if (strcmp (method_name, "Activate") == 0)
Packit ae235b
    {
Packit ae235b
      GVariant *platform_data;
Packit ae235b
Packit ae235b
      /* Completely the same for both freedesktop and gtk interfaces */
Packit ae235b
Packit ae235b
      g_variant_get (parameters, "(@a{sv})", &platform_data);
Packit ae235b
Packit ae235b
      class->before_emit (impl->app, platform_data);
Packit ae235b
      g_signal_emit_by_name (impl->app, "activate");
Packit ae235b
      class->after_emit (impl->app, platform_data);
Packit ae235b
      g_variant_unref (platform_data);
Packit ae235b
Packit ae235b
      g_dbus_method_invocation_return_value (invocation, NULL);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  else if (strcmp (method_name, "Open") == 0)
Packit ae235b
    {
Packit ae235b
      GApplicationFlags flags;
Packit ae235b
      GVariant *platform_data;
Packit ae235b
      const gchar *hint;
Packit ae235b
      GVariant *array;
Packit ae235b
      GFile **files;
Packit ae235b
      gint n, i;
Packit ae235b
Packit ae235b
      flags = g_application_get_flags (impl->app);
Packit ae235b
      if ((flags & G_APPLICATION_HANDLES_OPEN) == 0)
Packit ae235b
        {
Packit ae235b
          g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_NOT_SUPPORTED, "Application does not open files");
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      /* freedesktop interface has no hint parameter */
Packit ae235b
      if (g_str_equal (interface_name, "org.freedesktop.Application"))
Packit ae235b
        {
Packit ae235b
          g_variant_get (parameters, "(@as@a{sv})", &array, &platform_data);
Packit ae235b
          hint = "";
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        g_variant_get (parameters, "(@as&s@a{sv})", &array, &hint, &platform_data);
Packit ae235b
Packit ae235b
      n = g_variant_n_children (array);
Packit ae235b
      files = g_new (GFile *, n + 1);
Packit ae235b
Packit ae235b
      for (i = 0; i < n; i++)
Packit ae235b
        {
Packit ae235b
          const gchar *uri;
Packit ae235b
Packit ae235b
          g_variant_get_child (array, i, "&s", &uri);
Packit ae235b
          files[i] = g_file_new_for_uri (uri);
Packit ae235b
        }
Packit ae235b
      g_variant_unref (array);
Packit ae235b
      files[n] = NULL;
Packit ae235b
Packit ae235b
      class->before_emit (impl->app, platform_data);
Packit ae235b
      g_signal_emit_by_name (impl->app, "open", files, n, hint);
Packit ae235b
      class->after_emit (impl->app, platform_data);
Packit ae235b
Packit ae235b
      g_variant_unref (platform_data);
Packit ae235b
Packit ae235b
      for (i = 0; i < n; i++)
Packit ae235b
        g_object_unref (files[i]);
Packit ae235b
      g_free (files);
Packit ae235b
Packit ae235b
      g_dbus_method_invocation_return_value (invocation, NULL);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  else if (strcmp (method_name, "CommandLine") == 0)
Packit ae235b
    {
Packit ae235b
      GApplicationFlags flags;
Packit ae235b
      GApplicationCommandLine *cmdline;
Packit ae235b
      GVariant *platform_data;
Packit ae235b
      int status;
Packit ae235b
Packit ae235b
      flags = g_application_get_flags (impl->app);
Packit ae235b
      if ((flags & G_APPLICATION_HANDLES_COMMAND_LINE) == 0)
Packit ae235b
        {
Packit ae235b
          g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_NOT_SUPPORTED,
Packit ae235b
                                                 "Application does not handle command line arguments");
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      /* Only on the GtkApplication interface */
Packit ae235b
Packit ae235b
      cmdline = g_dbus_command_line_new (invocation);
Packit ae235b
      platform_data = g_variant_get_child_value (parameters, 2);
Packit ae235b
      class->before_emit (impl->app, platform_data);
Packit ae235b
      g_signal_emit_by_name (impl->app, "command-line", cmdline, &status);
Packit ae235b
      g_application_command_line_set_exit_status (cmdline, status);
Packit ae235b
      class->after_emit (impl->app, platform_data);
Packit ae235b
      g_variant_unref (platform_data);
Packit ae235b
      g_object_unref (cmdline);
Packit ae235b
    }
Packit ae235b
  else if (g_str_equal (method_name, "ActivateAction"))
Packit ae235b
    {
Packit ae235b
      GVariant *parameter = NULL;
Packit ae235b
      GVariant *platform_data;
Packit ae235b
      GVariantIter *iter;
Packit ae235b
      const gchar *name;
Packit ae235b
Packit ae235b
      /* Only on the freedesktop interface */
Packit ae235b
Packit ae235b
      g_variant_get (parameters, "(&sav@a{sv})", &name, &iter, &platform_data);
Packit ae235b
      g_variant_iter_next (iter, "v", &parameter);
Packit ae235b
      g_variant_iter_free (iter);
Packit ae235b
Packit ae235b
      class->before_emit (impl->app, platform_data);
Packit ae235b
      g_action_group_activate_action (impl->exported_actions, name, parameter);
Packit ae235b
      class->after_emit (impl->app, platform_data);
Packit ae235b
Packit ae235b
      if (parameter)
Packit ae235b
        g_variant_unref (parameter);
Packit ae235b
Packit ae235b
      g_variant_unref (platform_data);
Packit ae235b
Packit ae235b
      g_dbus_method_invocation_return_value (invocation, NULL);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    g_assert_not_reached ();
Packit ae235b
}
Packit ae235b
Packit ae235b
static gchar *
Packit ae235b
application_path_from_appid (const gchar *appid)
Packit ae235b
{
Packit ae235b
  gchar *appid_path, *iter;
Packit ae235b
Packit ae235b
  if (appid == NULL)
Packit ae235b
    /* this is a private implementation detail */
Packit ae235b
    return g_strdup ("/org/gtk/Application/anonymous");
Packit ae235b
Packit ae235b
  appid_path = g_strconcat ("/", appid, NULL);
Packit ae235b
  for (iter = appid_path; *iter; iter++)
Packit ae235b
    {
Packit ae235b
      if (*iter == '.')
Packit ae235b
        *iter = '/';
Packit ae235b
Packit ae235b
      if (*iter == '-')
Packit ae235b
        *iter = '_';
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return appid_path;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Attempt to become the primary instance.
Packit ae235b
 *
Packit ae235b
 * Returns %TRUE if everything went OK, regardless of if we became the
Packit ae235b
 * primary instance or not.  %FALSE is reserved for when something went
Packit ae235b
 * seriously wrong (and @error will be set too, in that case).
Packit ae235b
 *
Packit ae235b
 * After a %TRUE return, impl->primary will be TRUE if we were
Packit ae235b
 * successful.
Packit ae235b
 */
Packit ae235b
static gboolean
Packit ae235b
g_application_impl_attempt_primary (GApplicationImpl  *impl,
Packit ae235b
                                    GCancellable      *cancellable,
Packit ae235b
                                    GError           **error)
Packit ae235b
{
Packit ae235b
  const static GDBusInterfaceVTable vtable = {
Packit ae235b
    g_application_impl_method_call,
Packit ae235b
    g_application_impl_get_property,
Packit ae235b
    NULL /* set_property */
Packit ae235b
  };
Packit ae235b
  GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
Packit ae235b
  GVariant *reply;
Packit ae235b
  guint32 rval;
Packit ae235b
Packit ae235b
  if (org_gtk_Application == NULL)
Packit ae235b
    {
Packit ae235b
      GError *error = NULL;
Packit ae235b
      GDBusNodeInfo *info;
Packit ae235b
Packit ae235b
      info = g_dbus_node_info_new_for_xml (org_gtk_Application_xml, &error);
Packit ae235b
      if G_UNLIKELY (info == NULL)
Packit ae235b
        g_error ("%s", error->message);
Packit ae235b
      org_gtk_Application = g_dbus_node_info_lookup_interface (info, "org.gtk.Application");
Packit ae235b
      g_assert (org_gtk_Application != NULL);
Packit ae235b
      g_dbus_interface_info_ref (org_gtk_Application);
Packit ae235b
      g_dbus_node_info_unref (info);
Packit ae235b
Packit ae235b
      info = g_dbus_node_info_new_for_xml (org_freedesktop_Application_xml, &error);
Packit ae235b
      if G_UNLIKELY (info == NULL)
Packit ae235b
        g_error ("%s", error->message);
Packit ae235b
      org_freedesktop_Application = g_dbus_node_info_lookup_interface (info, "org.freedesktop.Application");
Packit ae235b
      g_assert (org_freedesktop_Application != NULL);
Packit ae235b
      g_dbus_interface_info_ref (org_freedesktop_Application);
Packit ae235b
      g_dbus_node_info_unref (info);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* We could possibly have been D-Bus activated as a result of incoming
Packit ae235b
   * requests on either the application or actiongroup interfaces.
Packit ae235b
   * Because of how GDBus dispatches messages, we need to ensure that
Packit ae235b
   * both of those things are registered before we attempt to request
Packit ae235b
   * our name.
Packit ae235b
   *
Packit ae235b
   * The action group need not be populated yet, as long as it happens
Packit ae235b
   * before we return to the mainloop.  The reason for that is because
Packit ae235b
   * GDBus does the check to make sure the object exists from the worker
Packit ae235b
   * thread but doesn't actually dispatch the action invocation until we
Packit ae235b
   * hit the mainloop in this thread.  There is also no danger of
Packit ae235b
   * receiving 'activate' or 'open' signals until after 'startup' runs,
Packit ae235b
   * for the same reason.
Packit ae235b
   */
Packit ae235b
  impl->object_id = g_dbus_connection_register_object (impl->session_bus, impl->object_path,
Packit ae235b
                                                       org_gtk_Application, &vtable, impl, NULL, error);
Packit ae235b
Packit ae235b
  if (impl->object_id == 0)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  impl->fdo_object_id = g_dbus_connection_register_object (impl->session_bus, impl->object_path,
Packit ae235b
                                                           org_freedesktop_Application, &vtable, impl, NULL, error);
Packit ae235b
Packit ae235b
  if (impl->fdo_object_id == 0)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  impl->actions_id = g_dbus_connection_export_action_group (impl->session_bus, impl->object_path,
Packit ae235b
                                                            impl->exported_actions, error);
Packit ae235b
Packit ae235b
  if (impl->actions_id == 0)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  impl->registered = TRUE;
Packit ae235b
  if (!app_class->dbus_register (impl->app,
Packit ae235b
                                 impl->session_bus,
Packit ae235b
                                 impl->object_path,
Packit ae235b
                                 error))
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  if (impl->bus_name == NULL)
Packit ae235b
    {
Packit ae235b
      /* If this is a non-unique application then it is sufficient to
Packit ae235b
       * have our object paths registered. We can return now.
Packit ae235b
       *
Packit ae235b
       * Note: non-unique applications always act as primary-instance.
Packit ae235b
       */
Packit ae235b
      impl->primary = TRUE;
Packit ae235b
      return TRUE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* If this is a unique application then we need to attempt to own
Packit ae235b
   * the well-known name and fall back to remote mode (!is_primary)
Packit ae235b
   * in the case that we can't do that.
Packit ae235b
   */
Packit ae235b
  /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
Packit ae235b
  reply = g_dbus_connection_call_sync (impl->session_bus, "org.freedesktop.DBus", "/org/freedesktop/DBus",
Packit ae235b
                                       "org.freedesktop.DBus", "RequestName",
Packit ae235b
                                       g_variant_new ("(su)", impl->bus_name, 0x4), G_VARIANT_TYPE ("(u)"),
Packit ae235b
                                       0, -1, cancellable, error);
Packit ae235b
Packit ae235b
  if (reply == NULL)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  g_variant_get (reply, "(u)", &rval);
Packit ae235b
  g_variant_unref (reply);
Packit ae235b
Packit ae235b
  /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
Packit ae235b
  impl->primary = (rval != 3);
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Stop doing the things that the primary instance does.
Packit ae235b
 *
Packit ae235b
 * This should be called if attempting to become the primary instance
Packit ae235b
 * failed (in order to clean up any partial success) and should also
Packit ae235b
 * be called when freeing the GApplication.
Packit ae235b
 *
Packit ae235b
 * It is safe to call this multiple times.
Packit ae235b
 */
Packit ae235b
static void
Packit ae235b
g_application_impl_stop_primary (GApplicationImpl *impl)
Packit ae235b
{
Packit ae235b
  GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
Packit ae235b
Packit ae235b
  if (impl->registered)
Packit ae235b
    {
Packit ae235b
      app_class->dbus_unregister (impl->app,
Packit ae235b
                                  impl->session_bus,
Packit ae235b
                                  impl->object_path);
Packit ae235b
      impl->registered = FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (impl->object_id)
Packit ae235b
    {
Packit ae235b
      g_dbus_connection_unregister_object (impl->session_bus, impl->object_id);
Packit ae235b
      impl->object_id = 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (impl->fdo_object_id)
Packit ae235b
    {
Packit ae235b
      g_dbus_connection_unregister_object (impl->session_bus, impl->fdo_object_id);
Packit ae235b
      impl->fdo_object_id = 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (impl->actions_id)
Packit ae235b
    {
Packit ae235b
      g_dbus_connection_unexport_action_group (impl->session_bus, impl->actions_id);
Packit ae235b
      impl->actions_id = 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (impl->primary && impl->bus_name)
Packit ae235b
    {
Packit ae235b
      g_dbus_connection_call (impl->session_bus, "org.freedesktop.DBus",
Packit ae235b
                              "/org/freedesktop/DBus", "org.freedesktop.DBus",
Packit ae235b
                              "ReleaseName", g_variant_new ("(s)", impl->bus_name),
Packit ae235b
                              NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
Packit ae235b
      impl->primary = FALSE;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_application_impl_set_busy_state (GApplicationImpl *impl,
Packit ae235b
                                   gboolean          busy)
Packit ae235b
{
Packit ae235b
  if (impl->busy != busy)
Packit ae235b
    {
Packit ae235b
      impl->busy = busy;
Packit ae235b
      send_property_change (impl);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_application_impl_destroy (GApplicationImpl *impl)
Packit ae235b
{
Packit ae235b
  g_application_impl_stop_primary (impl);
Packit ae235b
Packit ae235b
  if (impl->session_bus)
Packit ae235b
    g_object_unref (impl->session_bus);
Packit ae235b
Packit ae235b
  g_free (impl->object_path);
Packit ae235b
Packit ae235b
  g_slice_free (GApplicationImpl, impl);
Packit ae235b
}
Packit ae235b
Packit ae235b
GApplicationImpl *
Packit ae235b
g_application_impl_register (GApplication        *application,
Packit ae235b
                             const gchar         *appid,
Packit ae235b
                             GApplicationFlags    flags,
Packit ae235b
                             GActionGroup        *exported_actions,
Packit ae235b
                             GRemoteActionGroup **remote_actions,
Packit ae235b
                             GCancellable        *cancellable,
Packit ae235b
                             GError             **error)
Packit ae235b
{
Packit ae235b
  GDBusActionGroup *actions;
Packit ae235b
  GApplicationImpl *impl;
Packit ae235b
Packit ae235b
  g_assert ((flags & G_APPLICATION_NON_UNIQUE) || appid != NULL);
Packit ae235b
Packit ae235b
  impl = g_slice_new0 (GApplicationImpl);
Packit ae235b
Packit ae235b
  impl->app = application;
Packit ae235b
  impl->exported_actions = exported_actions;
Packit ae235b
Packit ae235b
  /* non-unique applications do not attempt to acquire a bus name */
Packit ae235b
  if (~flags & G_APPLICATION_NON_UNIQUE)
Packit ae235b
    impl->bus_name = appid;
Packit ae235b
Packit ae235b
  impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, NULL);
Packit ae235b
Packit ae235b
  if (impl->session_bus == NULL)
Packit ae235b
    {
Packit ae235b
      /* If we can't connect to the session bus, proceed as a normal
Packit ae235b
       * non-unique application.
Packit ae235b
       */
Packit ae235b
      *remote_actions = NULL;
Packit ae235b
      return impl;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  impl->object_path = application_path_from_appid (appid);
Packit ae235b
Packit ae235b
  /* Only try to be the primary instance if
Packit ae235b
   * G_APPLICATION_IS_LAUNCHER was not specified.
Packit ae235b
   */
Packit ae235b
  if (~flags & G_APPLICATION_IS_LAUNCHER)
Packit ae235b
    {
Packit ae235b
      if (!g_application_impl_attempt_primary (impl, cancellable, error))
Packit ae235b
        {
Packit ae235b
          g_application_impl_destroy (impl);
Packit ae235b
          return NULL;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (impl->primary)
Packit ae235b
        return impl;
Packit ae235b
Packit ae235b
      /* We didn't make it.  Drop our service-side stuff. */
Packit ae235b
      g_application_impl_stop_primary (impl);
Packit ae235b
Packit ae235b
      if (flags & G_APPLICATION_IS_SERVICE)
Packit ae235b
        {
Packit ae235b
          g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
Packit ae235b
                       "Unable to acquire bus name '%s'", appid);
Packit ae235b
          g_application_impl_destroy (impl);
Packit ae235b
Packit ae235b
          return NULL;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* We are non-primary.  Try to get the primary's list of actions.
Packit ae235b
   * This also serves as a mechanism to ensure that the primary exists
Packit ae235b
   * (ie: DBus service files installed correctly, etc).
Packit ae235b
   */
Packit ae235b
  actions = g_dbus_action_group_get (impl->session_bus, impl->bus_name, impl->object_path);
Packit ae235b
  if (!g_dbus_action_group_sync (actions, cancellable, error))
Packit ae235b
    {
Packit ae235b
      /* The primary appears not to exist.  Fail the registration. */
Packit ae235b
      g_application_impl_destroy (impl);
Packit ae235b
      g_object_unref (actions);
Packit ae235b
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  *remote_actions = G_REMOTE_ACTION_GROUP (actions);
Packit ae235b
Packit ae235b
  return impl;
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_application_impl_activate (GApplicationImpl *impl,
Packit ae235b
                             GVariant         *platform_data)
Packit ae235b
{
Packit ae235b
  g_dbus_connection_call (impl->session_bus,
Packit ae235b
                          impl->bus_name,
Packit ae235b
                          impl->object_path,
Packit ae235b
                          "org.gtk.Application",
Packit ae235b
                          "Activate",
Packit ae235b
                          g_variant_new ("(@a{sv})", platform_data),
Packit ae235b
                          NULL, 0, -1, NULL, NULL, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_application_impl_open (GApplicationImpl  *impl,
Packit ae235b
                         GFile            **files,
Packit ae235b
                         gint               n_files,
Packit ae235b
                         const gchar       *hint,
Packit ae235b
                         GVariant          *platform_data)
Packit ae235b
{
Packit ae235b
  GVariantBuilder builder;
Packit ae235b
  gint i;
Packit ae235b
Packit ae235b
  g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
Packit ae235b
  g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
Packit ae235b
  for (i = 0; i < n_files; i++)
Packit ae235b
    {
Packit ae235b
      gchar *uri = g_file_get_uri (files[i]);
Packit ae235b
      g_variant_builder_add (&builder, "s", uri);
Packit ae235b
      g_free (uri);
Packit ae235b
    }
Packit ae235b
  g_variant_builder_close (&builder);
Packit ae235b
  g_variant_builder_add (&builder, "s", hint);
Packit ae235b
  g_variant_builder_add_value (&builder, platform_data);
Packit ae235b
Packit ae235b
  g_dbus_connection_call (impl->session_bus,
Packit ae235b
                          impl->bus_name,
Packit ae235b
                          impl->object_path,
Packit ae235b
                          "org.gtk.Application",
Packit ae235b
                          "Open",
Packit ae235b
                          g_variant_builder_end (&builder),
Packit ae235b
                          NULL, 0, -1, NULL, NULL, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_application_impl_cmdline_method_call (GDBusConnection       *connection,
Packit ae235b
                                        const gchar           *sender,
Packit ae235b
                                        const gchar           *object_path,
Packit ae235b
                                        const gchar           *interface_name,
Packit ae235b
                                        const gchar           *method_name,
Packit ae235b
                                        GVariant              *parameters,
Packit ae235b
                                        GDBusMethodInvocation *invocation,
Packit ae235b
                                        gpointer               user_data)
Packit ae235b
{
Packit ae235b
  const gchar *message;
Packit ae235b
Packit ae235b
  g_variant_get_child (parameters, 0, "&s", &message);
Packit ae235b
Packit ae235b
  if (strcmp (method_name, "Print") == 0)
Packit ae235b
    g_print ("%s", message);
Packit ae235b
  else if (strcmp (method_name, "PrintError") == 0)
Packit ae235b
    g_printerr ("%s", message);
Packit ae235b
  else
Packit ae235b
    g_assert_not_reached ();
Packit ae235b
Packit ae235b
  g_dbus_method_invocation_return_value (invocation, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  GMainLoop *loop;
Packit ae235b
  int status;
Packit ae235b
} CommandLineData;
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_application_impl_cmdline_done (GObject      *source,
Packit ae235b
                                 GAsyncResult *result,
Packit ae235b
                                 gpointer      user_data)
Packit ae235b
{
Packit ae235b
  CommandLineData *data = user_data;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GVariant *reply;
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  reply = g_dbus_connection_call_with_unix_fd_list_finish (G_DBUS_CONNECTION (source), NULL, result, &error);
Packit ae235b
#else
Packit ae235b
  reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source), result, &error);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
Packit ae235b
  if (reply != NULL)
Packit ae235b
    {
Packit ae235b
      g_variant_get (reply, "(i)", &data->status);
Packit ae235b
      g_variant_unref (reply);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_printerr ("%s\n", error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
      data->status = 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_main_loop_quit (data->loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
g_application_impl_command_line (GApplicationImpl    *impl,
Packit ae235b
                                 const gchar * const *arguments,
Packit ae235b
                                 GVariant            *platform_data)
Packit ae235b
{
Packit ae235b
  const static GDBusInterfaceVTable vtable = {
Packit ae235b
    g_application_impl_cmdline_method_call
Packit ae235b
  };
Packit ae235b
  const gchar *object_path = "/org/gtk/Application/CommandLine";
Packit ae235b
  GMainContext *context;
Packit ae235b
  CommandLineData data;
Packit ae235b
  guint object_id;
Packit ae235b
Packit ae235b
  context = g_main_context_new ();
Packit ae235b
  data.loop = g_main_loop_new (context, FALSE);
Packit ae235b
  g_main_context_push_thread_default (context);
Packit ae235b
Packit ae235b
  if (org_gtk_private_CommandLine == NULL)
Packit ae235b
    {
Packit ae235b
      GError *error = NULL;
Packit ae235b
      GDBusNodeInfo *info;
Packit ae235b
Packit ae235b
      info = g_dbus_node_info_new_for_xml (org_gtk_private_CommandLine_xml, &error);
Packit ae235b
      if G_UNLIKELY (info == NULL)
Packit ae235b
        g_error ("%s", error->message);
Packit ae235b
      org_gtk_private_CommandLine = g_dbus_node_info_lookup_interface (info, "org.gtk.private.CommandLine");
Packit ae235b
      g_assert (org_gtk_private_CommandLine != NULL);
Packit ae235b
      g_dbus_interface_info_ref (org_gtk_private_CommandLine);
Packit ae235b
      g_dbus_node_info_unref (info);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  object_id = g_dbus_connection_register_object (impl->session_bus, object_path,
Packit ae235b
                                                 org_gtk_private_CommandLine,
Packit ae235b
                                                 &vtable, &data, NULL, NULL);
Packit ae235b
  /* In theory we should try other paths... */
Packit ae235b
  g_assert (object_id != 0);
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  {
Packit ae235b
    GError *error = NULL;
Packit ae235b
    GUnixFDList *fd_list;
Packit ae235b
Packit ae235b
    /* send along the stdin in case
Packit ae235b
     * g_application_command_line_get_stdin_data() is called
Packit ae235b
     */
Packit ae235b
    fd_list = g_unix_fd_list_new ();
Packit ae235b
    g_unix_fd_list_append (fd_list, 0, &error);
Packit ae235b
    g_assert_no_error (error);
Packit ae235b
Packit ae235b
    g_dbus_connection_call_with_unix_fd_list (impl->session_bus, impl->bus_name, impl->object_path,
Packit ae235b
                                              "org.gtk.Application", "CommandLine",
Packit ae235b
                                              g_variant_new ("(o^aay@a{sv})", object_path, arguments, platform_data),
Packit ae235b
                                              G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, fd_list, NULL,
Packit ae235b
                                              g_application_impl_cmdline_done, &data);
Packit ae235b
    g_object_unref (fd_list);
Packit ae235b
  }
Packit ae235b
#else
Packit ae235b
  g_dbus_connection_call (impl->session_bus, impl->bus_name, impl->object_path,
Packit ae235b
                          "org.gtk.Application", "CommandLine",
Packit ae235b
                          g_variant_new ("(o^aay@a{sv})", object_path, arguments, platform_data),
Packit ae235b
                          G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
Packit ae235b
                          g_application_impl_cmdline_done, &data);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  g_main_loop_run (data.loop);
Packit ae235b
Packit ae235b
  g_main_context_pop_thread_default (context);
Packit ae235b
  g_main_context_unref (context);
Packit ae235b
  g_main_loop_unref (data.loop);
Packit ae235b
Packit ae235b
  return data.status;
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_application_impl_flush (GApplicationImpl *impl)
Packit ae235b
{
Packit ae235b
  if (impl->session_bus)
Packit ae235b
    g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
GDBusConnection *
Packit ae235b
g_application_impl_get_dbus_connection (GApplicationImpl *impl)
Packit ae235b
{
Packit ae235b
  return impl->session_bus;
Packit ae235b
}
Packit ae235b
Packit ae235b
const gchar *
Packit ae235b
g_application_impl_get_dbus_object_path (GApplicationImpl *impl)
Packit ae235b
{
Packit ae235b
  return impl->object_path;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* GDBusCommandLine implementation {{{1 */
Packit ae235b
Packit ae235b
typedef GApplicationCommandLineClass GDBusCommandLineClass;
Packit ae235b
static GType g_dbus_command_line_get_type (void);
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  GApplicationCommandLine  parent_instance;
Packit ae235b
  GDBusMethodInvocation   *invocation;
Packit ae235b
Packit ae235b
  GDBusConnection *connection;
Packit ae235b
  const gchar     *bus_name;
Packit ae235b
  const gchar     *object_path;
Packit ae235b
} GDBusCommandLine;
Packit ae235b
Packit ae235b
Packit ae235b
G_DEFINE_TYPE (GDBusCommandLine,
Packit ae235b
               g_dbus_command_line,
Packit ae235b
               G_TYPE_APPLICATION_COMMAND_LINE)
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
Packit ae235b
                                   const gchar             *message)
Packit ae235b
{
Packit ae235b
  GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
Packit ae235b
Packit ae235b
  g_dbus_connection_call (gdbcl->connection,
Packit ae235b
                          gdbcl->bus_name,
Packit ae235b
                          gdbcl->object_path,
Packit ae235b
                          "org.gtk.private.CommandLine", "Print",
Packit ae235b
                          g_variant_new ("(s)", message),
Packit ae235b
                          NULL, 0, -1, NULL, NULL, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
Packit ae235b
                                      const gchar             *message)
Packit ae235b
{
Packit ae235b
  GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
Packit ae235b
Packit ae235b
  g_dbus_connection_call (gdbcl->connection,
Packit ae235b
                          gdbcl->bus_name,
Packit ae235b
                          gdbcl->object_path,
Packit ae235b
                          "org.gtk.private.CommandLine", "PrintError",
Packit ae235b
                          g_variant_new ("(s)", message),
Packit ae235b
                          NULL, 0, -1, NULL, NULL, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GInputStream *
Packit ae235b
g_dbus_command_line_get_stdin (GApplicationCommandLine *cmdline)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
Packit ae235b
  GInputStream *result = NULL;
Packit ae235b
  GDBusMessage *message;
Packit ae235b
  GUnixFDList *fd_list;
Packit ae235b
Packit ae235b
  message = g_dbus_method_invocation_get_message (gdbcl->invocation);
Packit ae235b
  fd_list = g_dbus_message_get_unix_fd_list (message);
Packit ae235b
Packit ae235b
  if (fd_list && g_unix_fd_list_get_length (fd_list))
Packit ae235b
    {
Packit ae235b
      gint *fds, n_fds, i;
Packit ae235b
Packit ae235b
      fds = g_unix_fd_list_steal_fds (fd_list, &n_fds);
Packit ae235b
      result = g_unix_input_stream_new (fds[0], TRUE);
Packit ae235b
      for (i = 1; i < n_fds; i++)
Packit ae235b
        (void) g_close (fds[i], NULL);
Packit ae235b
      g_free (fds);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
#else
Packit ae235b
  return NULL;
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_command_line_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
Packit ae235b
  GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
Packit ae235b
  gint status;
Packit ae235b
Packit ae235b
  status = g_application_command_line_get_exit_status (cmdline);
Packit ae235b
Packit ae235b
  g_dbus_method_invocation_return_value (gdbcl->invocation,
Packit ae235b
                                         g_variant_new ("(i)", status));
Packit ae235b
  g_object_unref (gdbcl->invocation);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_dbus_command_line_parent_class)
Packit ae235b
    ->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_command_line_init (GDBusCommandLine *gdbcl)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
Packit ae235b
{
Packit ae235b
  GObjectClass *object_class = G_OBJECT_CLASS (class);
Packit ae235b
Packit ae235b
  object_class->finalize = g_dbus_command_line_finalize;
Packit ae235b
  class->printerr_literal = g_dbus_command_line_printerr_literal;
Packit ae235b
  class->print_literal = g_dbus_command_line_print_literal;
Packit ae235b
  class->get_stdin = g_dbus_command_line_get_stdin;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GApplicationCommandLine *
Packit ae235b
g_dbus_command_line_new (GDBusMethodInvocation *invocation)
Packit ae235b
{
Packit ae235b
  GDBusCommandLine *gdbcl;
Packit ae235b
  GVariant *args;
Packit ae235b
  GVariant *arguments, *platform_data;
Packit ae235b
Packit ae235b
  args = g_dbus_method_invocation_get_parameters (invocation);
Packit ae235b
Packit ae235b
  arguments = g_variant_get_child_value (args, 1);
Packit ae235b
  platform_data = g_variant_get_child_value (args, 2);
Packit ae235b
  gdbcl = g_object_new (g_dbus_command_line_get_type (),
Packit ae235b
                        "arguments", arguments,
Packit ae235b
                        "platform-data", platform_data,
Packit ae235b
                        NULL);
Packit ae235b
  g_variant_unref (arguments);
Packit ae235b
  g_variant_unref (platform_data);
Packit ae235b
Packit ae235b
  gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
Packit ae235b
  gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
Packit ae235b
  g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
Packit ae235b
  gdbcl->invocation = g_object_ref (invocation);
Packit ae235b
Packit ae235b
  return G_APPLICATION_COMMAND_LINE (gdbcl);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Epilogue {{{1 */
Packit ae235b
Packit ae235b
/* vim:set foldmethod=marker: */