[glib] Action group exporter: clean up the API



commit f7886d6adbef560ea9dcc2ced3f327c927899752
Author: Ryan Lortie <desrt desrt ca>
Date:   Fri Dec 2 15:04:53 2011 -0500

    Action group exporter: clean up the API
    
    Make it look more like a typical GDBusConnection API with integer
    registration ID and corresponding unexport call.  Kill the 'query' call.

 gio/gactiongroupexporter.c  |  127 ++++++++++---------------------------------
 gio/gactiongroupexporter.h  |   15 ++---
 gio/gapplicationimpl-dbus.c |   12 ++--
 gio/tests/actions.c         |   10 ++--
 4 files changed, 45 insertions(+), 119 deletions(-)
---
diff --git a/gio/gactiongroupexporter.c b/gio/gactiongroupexporter.c
index 1ad5a6b..d0601a9 100644
--- a/gio/gactiongroupexporter.c
+++ b/gio/gactiongroupexporter.c
@@ -211,14 +211,12 @@ const char org_gtk_Actions_xml[] =
   "</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];
@@ -547,6 +545,21 @@ org_gtk_Actions_method_call (GDBusConnection       *connection,
   g_dbus_method_invocation_return_value (invocation, result);
 }
 
+static void
+g_action_group_exporter_free (gpointer user_data)
+{
+  GActionGroupExporter *exporter = user_data;
+  gint i;
+
+  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);
+}
+
 /**
  * g_action_group_dbus_export_start:
  * @connection: a #GDBusConnection
@@ -571,19 +584,17 @@ org_gtk_Actions_method_call (GDBusConnection       *connection,
  * Returns: %TRUE if the export is successful, or %FALSE (with @error
  *          set) in the event of a failure.
  **/
-gboolean
-g_action_group_dbus_export_start (GDBusConnection  *connection,
-                                  const gchar      *object_path,
-                                  GActionGroup     *action_group,
-                                  GError          **error)
+guint
+g_dbus_connection_export_action_group (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);
+  guint id;
 
   if G_UNLIKELY (org_gtk_Actions == NULL)
     {
@@ -599,24 +610,16 @@ g_action_group_dbus_export_start (GDBusConnection  *connection,
       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);
+  id = g_dbus_connection_register_object (connection, object_path, org_gtk_Actions, &vtable,
+                                          exporter, g_action_group_exporter_free, error);
 
-  if (exporter->registration_id == 0)
+  if (id == 0)
     {
       g_slice_free (GActionGroupExporter, exporter);
-      return FALSE;
+      return 0;
     }
 
-  g_hash_table_insert (exported_groups, action_group, exporter);
   exporter->pending_changes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
   exporter->pending_id = 0;
   exporter->action_group = g_object_ref (action_group);
@@ -636,84 +639,12 @@ g_action_group_dbus_export_start (GDBusConnection  *connection,
     g_signal_connect (action_group, "action-enabled-changed",
                       G_CALLBACK (g_action_group_exporter_action_enabled_changed), exporter);
 
-  return TRUE;
-}
-
-/**
- * g_action_group_dbus_export_stop:
- * @action_group: a #GActionGroup
- *
- * Stops the export of @action_group.
- *
- * This reverses the effect of a previous call to
- * g_action_group_dbus_export_start() 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_dbus_export_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_hash_table_remove (exported_groups, action_group);
-
-  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;
+  return id;
 }
 
-/**
- * g_action_group_dbus_export_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_dbus_export_query (GActionGroup     *action_group,
-                                  GDBusConnection **connection,
-                                  const gchar     **object_path)
+g_dbus_connection_unexport_action_group (GDBusConnection *connection,
+                                         guint            export_id)
 {
-  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;
+  return g_dbus_connection_unregister_object (connection, export_id);
 }
diff --git a/gio/gactiongroupexporter.h b/gio/gactiongroupexporter.h
index 6493e39..d7d2a81 100644
--- a/gio/gactiongroupexporter.h
+++ b/gio/gactiongroupexporter.h
@@ -32,16 +32,13 @@
 
 G_BEGIN_DECLS
 
-gboolean g_action_group_dbus_export_start (GDBusConnection  *connection,
-                                           const gchar      *object_path,
-                                           GActionGroup     *action_group,
-                                           GError          **error);
+guint                   g_dbus_connection_export_action_group           (GDBusConnection  *connection,
+                                                                         const gchar      *object_path,
+                                                                         GActionGroup     *action_group,
+                                                                         GError          **error);
 
-gboolean g_action_group_dbus_export_stop  (GActionGroup     *action_group);
-
-gboolean g_action_group_dbus_export_query (GActionGroup     *action_group,
-                                           GDBusConnection **connection,
-                                           const gchar     **object_path);
+gboolean                g_dbus_connection_unexport_action_group         (GDBusConnection  *connection,
+                                                                         guint             export_id);
 
 G_END_DECLS
 
diff --git a/gio/gapplicationimpl-dbus.c b/gio/gapplicationimpl-dbus.c
index 4db879a..6deef5f 100644
--- a/gio/gapplicationimpl-dbus.c
+++ b/gio/gapplicationimpl-dbus.c
@@ -81,7 +81,7 @@ struct _GApplicationImpl
   const gchar     *bus_name;
   gchar           *object_path;
   guint            object_id;
-  gboolean         actions_exported;
+  guint            actions_id;
   gboolean         primary;
   gpointer         app;
 };
@@ -247,9 +247,9 @@ g_application_impl_attempt_primary (GApplicationImpl  *impl,
   if (impl->object_id == 0)
     return FALSE;
 
-  impl->actions_exported = g_action_group_dbus_export_start (impl->session_bus, impl->object_path, impl->app, error);
+  impl->actions_id = g_dbus_connection_export_action_group (impl->session_bus, impl->object_path, impl->app, error);
 
-  if (!impl->actions_exported)
+  if (impl->actions_id == 0)
     return FALSE;
 
   /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
@@ -287,10 +287,10 @@ g_application_impl_stop_primary (GApplicationImpl *impl)
       impl->object_id = 0;
     }
 
-  if (impl->actions_exported)
+  if (impl->actions_id)
     {
-      g_action_group_dbus_export_stop (impl->app);
-      impl->actions_exported = FALSE;
+      g_dbus_connection_unexport_action_group (impl->session_bus, impl->actions_id);
+      impl->actions_id = 0;
     }
 
   if (impl->primary)
diff --git a/gio/tests/actions.c b/gio/tests/actions.c
index 3428e02..c263842 100644
--- a/gio/tests/actions.c
+++ b/gio/tests/actions.c
@@ -527,6 +527,7 @@ test_dbus_export (void)
   };
   GError *error = NULL;
   GVariant *v;
+  guint id;
 
   loop = g_main_loop_new (NULL, FALSE);
 
@@ -535,12 +536,9 @@ test_dbus_export (void)
   group = g_simple_action_group_new ();
   g_simple_action_group_add_entries (group, entries, G_N_ELEMENTS (entries), NULL);
 
-  g_action_group_dbus_export_start (bus, "/", G_ACTION_GROUP (group), &error);
+  id = g_dbus_connection_export_action_group (bus, "/", G_ACTION_GROUP (group), &error);
   g_assert_no_error (error);
 
-  g_assert (g_action_group_dbus_export_query (G_ACTION_GROUP (group), NULL, NULL));
-  proxy = NULL;
-
   g_dbus_action_group_new (bus, g_dbus_connection_get_unique_name (bus), "/", 0, NULL, got_proxy, NULL);
   g_assert_no_error (error);
 
@@ -618,12 +616,12 @@ test_dbus_export (void)
   g_assert (!g_variant_get_boolean (v));
   g_variant_unref (v);
 
-  g_action_group_dbus_export_stop (G_ACTION_GROUP (group));
-  g_assert (!g_action_group_dbus_export_query (G_ACTION_GROUP (group), NULL, NULL));
+  g_dbus_connection_unexport_action_group (bus, id);
 
   g_object_unref (proxy);
   g_object_unref (group);
   g_main_loop_unref (loop);
+  g_object_unref (bus);
 }
 
 int



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