[glib/wip/actions: 1/11] add GActionGroup D-Bus exporter



commit a87a1ee9ca683b6bca45b40d702b417f9d5609b1
Author: Ryan Lortie <desrt desrt ca>
Date:   Wed Jun 29 01:02:21 2011 +0100

    add GActionGroup D-Bus exporter

 gio/Makefile.am            |    1 +
 gio/gactiongroupexporter.c |  578 ++++++++++++++++++++++++++++++++++++++++++++
 gio/gactiongroupexporter.h |   44 ++++
 3 files changed, 623 insertions(+), 0 deletions(-)
---
diff --git a/gio/Makefile.am b/gio/Makefile.am
index 0a127a6..4996aff 100644
--- a/gio/Makefile.am
+++ b/gio/Makefile.am
@@ -141,6 +141,7 @@ application_sources = \
 	gsimpleactiongroup.c			\
 	gaction.c				\
 	gsimpleaction.c				\
+	gactiongroupexporter.c			\
 	gapplicationcommandline.c		\
 	gapplicationimpl.h			\
 	gapplicationimpl-dbus.c			\
diff --git a/gio/gactiongroupexporter.c b/gio/gactiongroupexporter.c
new file mode 100644
index 0000000..0cccc00
--- /dev/null
+++ b/gio/gactiongroupexporter.c
@@ -0,0 +1,578 @@
+/*
+ * Copyright  2010 Codethink Limited
+ * Copyright  2011 Canonical Limited
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the licence 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Authors: Ryan Lortie <desrt desrt ca>
+ */
+
+#include "config.h"
+
+#include "gactiongroupexporter.h"
+
+#include "gdbusmethodinvocation.h"
+#include "gdbusintrospection.h"
+#include "gdbusconnection.h"
+#include "gactiongroup.h"
+#include "gdbuserror.h"
+
+/**
+ * SECTION:gactiongroupexporter
+ * @title: GActionGroup exporter
+ * @short_description: Export #GActionGroup<!-- -->s on D-Bus
+ * @see_also: #GActionGroup
+ *
+ * #GActionGroupExporter exports a #GActionGroup on D-Bus.  The D-Bus
+ * interface that is used is a private implementation detail.
+ **/
+
+G_GNUC_INTERNAL GVariant *
+g_action_group_describe_action (GActionGroup *action_group,
+                                const gchar  *name)
+{
+  const GVariantType *type;
+  GVariantBuilder builder;
+  gboolean enabled;
+
+  g_variant_builder_init (&builder, G_VARIANT_TYPE ("(bgg)"));
+
+  enabled = g_action_group_get_action_enabled (action_group, name);
+  g_variant_builder_add (&builder, "b", enabled);
+
+  if ((type = g_action_group_get_action_parameter_type (action_group, name)))
+    {
+      gchar *str = g_variant_type_dup_string (type);
+      g_variant_builder_add (&builder, "g", str);
+      g_free (str);
+    }
+  else
+    g_variant_builder_add (&builder, "g", "");
+
+  if ((type = g_action_group_get_action_state_type (action_group, name)))
+    {
+      gchar *str = g_variant_type_dup_string (type);
+      g_variant_builder_add (&builder, "g", str);
+      g_free (str);
+    }
+  else
+    g_variant_builder_add (&builder, "g", "");
+
+  return g_variant_builder_end (&builder);
+}
+
+/* Using XML saves us dozens of relocations vs. using the introspection
+ * structure types.  We only need to burn cycles and memory if we
+ * actually use the exporter -- not in every single app using GIO.
+ *
+ * It's also a lot easier to read. :)
+ */
+const char org_gtk_Actions_xml[] =
+  "<node>"
+  "  <interface name='org.gtk.Actions'>"
+  "    <method name='List'>"
+  "      <arg type='as' name='list' direction='out'>"
+  "    </method>"
+  "    <method name='Describe'>"
+  "      <arg type='s' name='action_name' direction='in'>"
+  "      <arg type='(bgg)' name='description' direction='out'>"
+  "    </method>"
+  "    <method name='DescribeAll'>"
+  "      <arg type='a{s(bgg)}' name='descriptions' direction='out'/>"
+  "    </method>"
+  "    <method name='Activate'>"
+  "      <arg type='s' name='action_name' direction='in'/>"
+  "      <arg type='av' name='parameter' direction='in'/>"
+  "      <arg type='a{sv}' name='platform_data' direction='in'/>"
+  "    </method>"
+  "    <method name='SetState'>"
+  "      <arg type='s' name='action_name' direction='in'/>"
+  "      <arg type='v' name='value' direction='in'/>"
+  "      <arg type='a{sv}' name='platform_data' direction='in'/>"
+  "    </method>"
+  "    <signal name='Changed'>"
+  "      <arg type='as' name='removals'/>"
+  "      <arg type='a{sb}' name='enable_changes'/>"
+  "      <arg type='a{sv}' name='state_changes'/>"
+  "      <arg type='a{s(bgg)}' name='additions'/>"
+  "    </signal>"
+  "  </interface>"
+  "</node>";
+
+static GDBusInterfaceInfo *org_gtk_Actions;
+static GHashTable *exported_groups;
+
+typedef struct
+{
+  GActionGroup    *action_group;
+  GDBusConnection *connection;
+  gchar           *object_path;
+  guint            registration_id;
+  GHashTable      *pending_changes;
+  guint            pending_id;
+  gulong           signal_ids[4];
+} GActionGroupExporter;
+
+#define ACTION_ADDED_EVENT             (1u<<0)
+#define ACTION_REMOVED_EVENT           (1u<<1)
+#define ACTION_STATE_CHANGED_EVENT     (1u<<2)
+#define ACTION_ENABLED_CHANGED_EVENT   (1u<<3)
+
+static gboolean
+g_action_group_exporter_dispatch_events (gpointer user_data)
+{
+  GActionGroupExporter *exporter = user_data;
+  GVariantBuilder removes;
+  GVariantBuilder enabled_changes;
+  GVariantBuilder state_changes;
+  GVariantBuilder adds;
+  GHashTableIter iter;
+  gpointer value;
+  gpointer key;
+
+  g_variant_builder_init (&removes, G_VARIANT_TYPE_STRING_ARRAY);
+  g_variant_builder_init (&enabled_changes, G_VARIANT_TYPE ("a{sb}"));
+  g_variant_builder_init (&state_changes, G_VARIANT_TYPE ("a{sv}"));
+  g_variant_builder_init (&adds, G_VARIANT_TYPE ("a{s(bgg)}"));
+
+  g_hash_table_iter_init (&iter, exporter->pending_changes);
+  while (g_hash_table_iter_next (&iter, &key, &value))
+    {
+      guint events = GPOINTER_TO_INT (value);
+      const gchar *name = key;
+
+      /* Adds and removes are incompatible with enabled or state
+       * changes, but we must report at least one event.
+       */
+      g_assert (((ACTION_ENABLED_CHANGED_EVENT |
+                  ACTION_STATE_CHANGED_EVENT) == 0) !=
+                ((ACTION_REMOVED_EVENT | ACTION_ADDED_EVENT) == 0));
+
+      if (events & ACTION_REMOVED_EVENT)
+        g_variant_builder_add (&removes, "s", name);
+
+      if (events & ACTION_ENABLED_CHANGED_EVENT)
+        {
+          gboolean enabled;
+
+          enabled = g_action_group_get_action_enabled (exporter->action_group, name);
+          g_variant_builder_add (&enabled_changes, "{sb}", name, enabled);
+        }
+
+      if (events & ACTION_STATE_CHANGED_EVENT)
+        {
+          GVariant *state;
+
+          state = g_action_group_get_action_state (exporter->action_group, name);
+          g_variant_builder_add (&state_changes, "{sv}", name, state);
+          g_variant_unref (state);
+        }
+
+      if (events & ACTION_ADDED_EVENT)
+        {
+          GVariant *description;
+
+          description = g_action_group_describe_action (exporter->action_group, name);
+          g_variant_builder_add (&state_changes, "{s@(bgg)}", name, description);
+        }
+    }
+
+  g_dbus_connection_emit_signal (exporter->connection, NULL, exporter->object_path,
+                                 "org.gtk.Actions", "Changed",
+                                 g_variant_new ("(asa{sb}a{sv}a{s(bgg)})",
+                                                &removes, &enabled_changes,
+                                                &state_changes, &adds),
+                                 NULL);
+
+  exporter->pending_id = 0;
+
+  return FALSE;
+}
+
+static guint
+g_action_group_exporter_get_events (GActionGroupExporter *exporter,
+                                    const gchar          *name)
+{
+  return (gsize) g_hash_table_lookup (exporter->pending_changes, name);
+}
+
+static void
+g_action_group_exporter_set_events (GActionGroupExporter *exporter,
+                                    const gchar          *name,
+                                    guint                 events)
+{
+  gboolean have_events;
+  gboolean is_queued;
+
+  if (events != 0)
+    g_hash_table_insert (exporter->pending_changes, g_strdup (name), GINT_TO_POINTER (events));
+  else
+    g_hash_table_remove (exporter->pending_changes, name);
+
+  have_events = g_hash_table_size (exporter->pending_changes) > 0;
+  is_queued = exporter->pending_id > 0;
+
+  if (have_events && !is_queued)
+    exporter->pending_id = g_idle_add (g_action_group_exporter_dispatch_events, exporter);
+
+  if (!have_events && is_queued)
+    {
+      g_source_remove (exporter->pending_id);
+      exporter->pending_id = 0;
+    }
+}
+
+static void
+g_action_group_exporter_action_added (GActionGroup *action_group,
+                                      const gchar  *action_name,
+                                      gpointer      user_data)
+{
+  GActionGroupExporter *exporter = user_data;
+  guint event_mask;
+
+  event_mask = g_action_group_exporter_get_events (exporter, action_name);
+
+  /* The action is new, so we should not have any pending
+   * enabled-changed or state-changed signals for it.
+   */
+  g_assert (~event_mask & (ACTION_STATE_CHANGED_EVENT |
+                           ACTION_ENABLED_CHANGED_EVENT));
+
+  event_mask |= ACTION_ADDED_EVENT;
+
+  g_action_group_exporter_set_events (exporter, action_name, event_mask);
+}
+
+static void
+g_action_group_exporter_action_removed (GActionGroup *action_group,
+                                        const gchar  *action_name,
+                                        gpointer      user_data)
+{
+  GActionGroupExporter *exporter = user_data;
+  guint event_mask;
+
+  event_mask = g_action_group_exporter_get_events (exporter, action_name);
+
+  /* If the add event for this is still queued then just cancel it since
+   * it's gone now.
+   *
+   * If the event was freshly added, there should not have been any
+   * enabled or state changed events.
+   */
+  if (event_mask & ACTION_ADDED_EVENT)
+    {
+      g_assert (~event_mask & ~(ACTION_STATE_CHANGED_EVENT | ACTION_ENABLED_CHANGED_EVENT));
+      event_mask &= ~ACTION_ADDED_EVENT;
+    }
+
+  /* Otherwise, queue a remove event.  Drop any state or enabled changes
+   * that were queued before the remove. */
+  else
+    {
+      event_mask |= ACTION_REMOVED_EVENT;
+      event_mask &= ~(ACTION_STATE_CHANGED_EVENT | ACTION_ENABLED_CHANGED_EVENT);
+    }
+
+  g_action_group_exporter_set_events (exporter, action_name, event_mask);
+}
+
+static void
+g_action_group_exporter_action_state_changed (GActionGroup *action_group,
+                                              const gchar  *action_name,
+                                              GVariant     *value,
+                                              gpointer      user_data)
+{
+  GActionGroupExporter *exporter = user_data;
+  guint event_mask;
+
+  event_mask = g_action_group_exporter_get_events (exporter, action_name);
+
+  /* If it was removed, it must have been added back.  Otherwise, why
+   * are we hearing about changes?
+   */
+  g_assert (~event_mask & ACTION_REMOVED_EVENT ||
+            event_mask & ACTION_ADDED_EVENT);
+
+  /* If it is freshly added, don't also bother with the state change
+   * signal since the updated state will be sent as part of the pending
+   * add message.
+   */
+  if (~event_mask & ACTION_ADDED_EVENT)
+    event_mask |= ACTION_STATE_CHANGED_EVENT;
+}
+
+static void
+g_action_group_exporter_action_enabled_changed (GActionGroup *action_group,
+                                                const gchar  *action_name,
+                                                gboolean      enabled,
+                                                gpointer      user_data)
+{
+  GActionGroupExporter *exporter = user_data;
+  guint event_mask;
+
+  event_mask = g_action_group_exporter_get_events (exporter, action_name);
+
+  /* Reasoning as above. */
+  g_assert (~event_mask & ACTION_REMOVED_EVENT ||
+            event_mask & ACTION_ADDED_EVENT);
+
+  if (~event_mask & ACTION_ADDED_EVENT)
+    event_mask |= ACTION_ENABLED_CHANGED_EVENT;
+}
+
+static void
+org_gtk_Actions_method_call (GDBusConnection       *connection,
+                             const gchar           *sender,
+                             const gchar           *object_path,
+                             const gchar           *interface_name,
+                             const gchar           *method_name,
+                             GVariant              *parameters,
+                             GDBusMethodInvocation *invocation,
+                             gpointer               user_data)
+{
+  GActionGroupExporter *exporter = user_data;
+  GVariant *result = NULL;
+
+  if (g_str_equal (method_name, "List"))
+    {
+      gchar **list;
+
+      list = g_action_group_list_actions (exporter->action_group);
+      result = g_variant_new ("(^as)", list);
+      g_strfreev (list);
+    }
+
+  else if (g_str_equal (method_name, "Describe"))
+    {
+      const gchar *name;
+
+      g_variant_get (parameters, "(&s)", &name);
+      result = g_action_group_describe_action (exporter->action_group, name);
+    }
+
+  else if (g_str_equal (method_name, "DescribeAll"))
+    {
+      GVariantBuilder builder;
+      gchar **list;
+      gint i;
+
+      list = g_action_group_list_actions (exporter->action_group);
+      g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{s(bgg)}"));
+      for (i = 0; list[i]; i++)
+        {
+          const gchar *name = list[i];
+          GVariant *description;
+
+          description = g_action_group_describe_action (exporter->action_group, name);
+          g_variant_builder_add (&builder, "{s@(bgg)}", name, description);
+        }
+      result = g_variant_new ("(a{s(bgg)})", &builder);
+      g_strfreev (list);
+    }
+
+  else if (g_str_equal (method_name, "Activate"))
+    {
+      GVariant *parameter = NULL;
+      GVariantIter *iter;
+      const gchar *name;
+
+      g_variant_get (parameters, "(&sav)", &name, &iter);
+      g_variant_iter_next (iter, "v", &parameter);
+      g_variant_iter_free (iter);
+
+      g_action_group_activate_action (exporter->action_group, name, parameter);
+      if (parameter)
+        g_variant_unref (parameter);
+    }
+
+  else if (g_str_equal (method_name, "SetState"))
+    {
+      const gchar *name;
+      GVariant *state;
+
+      g_variant_get (parameters, "(&sv)", &name, &state);
+      g_action_group_change_action_state (exporter->action_group, name, state);
+      g_variant_unref (state);
+    }
+
+  else
+    g_assert_not_reached ();
+
+  g_dbus_method_invocation_return_value (invocation, result);
+}
+
+/**
+ * g_action_group_exporter_export:
+ * @connection: a #GDBusConnection
+ * @object_path: a D-Bus object path
+ * @action_group: a #GActionGroup
+ * @error: a pointer to a %NULL #GError, or %NULL
+ *
+ * Exports @action_group on @connection at @object_path.
+ *
+ * The implemented D-Bus API should be considered private.  It is
+ * subject to change in the future.
+ *
+ * A given action group can only be exported on one object path and an
+ * object_path can only have one action group exported on it.  If either
+ * constraint is violated, the export will fail and %FALSE will be
+ * returned (with @error set accordingly).
+ *
+ * Use g_action_group_exporter_stop() to stop exporting @action_group,
+ * or g_action_group_exporter_query() to find out if and where a given
+ * action group is exported.
+ *
+ * Returns: %TRUE if the export is successful, or %FALSE (with @error
+ *          set) in the event of a failure.
+ **/
+gboolean
+g_action_group_exporter_export (GDBusConnection  *connection,
+                                const gchar      *object_path,
+                                GActionGroup     *action_group,
+                                GError          **error)
+{
+  const GDBusInterfaceVTable vtable = {
+    org_gtk_Actions_method_call
+  };
+  GActionGroupExporter *exporter;
+
+  if G_UNLIKELY (exported_groups == NULL)
+    exported_groups = g_hash_table_new (NULL, NULL);
+
+  if G_UNLIKELY (org_gtk_Actions == NULL)
+    {
+      GDBusNodeInfo *info;
+
+      info = g_dbus_node_info_new_for_xml (org_gtk_Actions_xml, NULL);
+      org_gtk_Actions = g_dbus_node_info_lookup_interface (info, "org.gtk.Actions");
+      g_assert (org_gtk_Actions != NULL);
+      g_dbus_interface_info_ref (org_gtk_Actions);
+      g_dbus_node_info_unref (info);
+    }
+
+  if G_UNLIKELY (g_hash_table_lookup (exported_groups, action_group))
+    {
+      g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FILE_EXISTS,
+                   "The given GActionGroup has already been exported");
+      return FALSE;
+    }
+
+  exporter = g_slice_new (GActionGroupExporter);
+  exporter->registration_id = g_dbus_connection_register_object (connection, object_path, org_gtk_Actions,
+                                                                 &vtable, exporter, NULL, error);
+
+  if (exporter->registration_id == 0)
+    {
+      g_slice_free (GActionGroupExporter, exporter);
+      return FALSE;
+    }
+
+  g_hash_table_insert (exported_groups, action_group, exporter);
+  exporter->action_group = g_object_ref (action_group);
+  exporter->connection = g_object_ref (connection);
+  exporter->object_path = g_strdup (object_path);
+
+  exporter->signal_ids[0] =
+    g_signal_connect (action_group, "action-added",
+                      G_CALLBACK (g_action_group_exporter_action_added), exporter);
+  exporter->signal_ids[1] =
+    g_signal_connect (action_group, "action-removed",
+                      G_CALLBACK (g_action_group_exporter_action_removed), exporter);
+  exporter->signal_ids[2] =
+    g_signal_connect (action_group, "action-state-changed",
+                      G_CALLBACK (g_action_group_exporter_action_state_changed), exporter);
+  exporter->signal_ids[3] =
+    g_signal_connect (action_group, "action-enabled-changed",
+                      G_CALLBACK (g_action_group_exporter_action_enabled_changed), exporter);
+
+  return TRUE;
+}
+/**
+ * g_action_group_exporter_stop:
+ * @action_group: a #GActionGroup
+ *
+ * Stops the export of @action_group.
+ *
+ * This reverses the effect of a previous call to
+ * g_action_group_exporter_export() for @action_group.
+ *
+ * Returns: %TRUE if an export was stopped or %FALSE if @action_group
+ *          was not exported in the first place
+ **/
+gboolean
+g_action_group_exporter_stop (GActionGroup *action_group)
+{
+  GActionGroupExporter *exporter;
+  gint i;
+
+  if G_UNLIKELY (exported_groups == NULL)
+    return FALSE;
+
+  exporter = g_hash_table_lookup (exported_groups, action_group);
+  if G_UNLIKELY (exporter == NULL)
+    return FALSE;
+
+  g_dbus_connection_unregister_object (exporter->connection, exporter->registration_id);
+  for (i = 0; i < G_N_ELEMENTS (exporter->signal_ids); i++)
+    g_signal_handler_disconnect (exporter->action_group, exporter->signal_ids[i]);
+  g_object_unref (exporter->connection);
+  g_object_unref (exporter->action_group);
+  g_free (exporter->object_path);
+
+  g_slice_free (GActionGroupExporter, exporter);
+
+  return TRUE;
+}
+
+/**
+ * g_action_group_exporter_query:
+ * @action_group: a #GActionGroup
+ * @connection: (out): the #GDBusConnection used for exporting
+ * @object_path: (out): the object path used for exporting
+ *
+ * Queries if and where @action_group is exported.
+ *
+ * If @action_group is exported, %TRUE is returned.  If @connection is
+ * non-%NULL then it is set to the #GDBusConnection used for the export.
+ * If @object_path is non-%NULL then it is set to the object path.
+ *
+ * If the @action_group is not exported, %FALSE is returned and
+ * @connection and @object_path remain unmodified.
+ *
+ * Returns: %TRUE if @action_group was exported, else %FALSE
+ **/
+gboolean
+g_action_group_exporter_query (GActionGroup     *action_group,
+                               GDBusConnection **connection,
+                               const gchar     **object_path)
+{
+  GActionGroupExporter *exporter;
+
+  if (exported_groups == NULL)
+    return FALSE;
+
+  exporter = g_hash_table_lookup (exported_groups, action_group);
+  if (exporter == NULL)
+    return FALSE;
+
+  if (connection)
+    *connection = exporter->connection;
+
+  if (object_path)
+    *object_path = exporter->object_path;
+
+  return TRUE;
+}
diff --git a/gio/gactiongroupexporter.h b/gio/gactiongroupexporter.h
new file mode 100644
index 0000000..7bfea66
--- /dev/null
+++ b/gio/gactiongroupexporter.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright  2010 Codethink Limited
+ * Copyright  2011 Canonical Limited
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the licence 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Authors: Ryan Lortie <desrt desrt ca>
+ */
+
+
+#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
+#error "Only <gio/gio.h> can be included directly."
+#endif
+
+#ifndef __G_ACTION_GROUP_EXPORTER_H__
+#define __G_ACTION_GROUP_EXPORTER_H__
+
+#include <gio/giotypes.h>
+
+gboolean                g_action_group_exporter_export                  (GDBusConnection  *connection,
+                                                                         const gchar      *object_path,
+                                                                         GActionGroup     *action_group,
+                                                                         GError          **error);
+
+gboolean                g_action_group_exporter_stop                    (GActionGroup     *action_group);
+
+gboolean                g_action_group_exporter_query                   (GActionGroup     *action_group,
+                                                                         GDBusConnection **connection,
+                                                                         const gchar     **object_path);
+
+#endif /* __G_ACTION_GROUP_EXPORTER_H__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]