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.
This commit is contained in:
Ryan Lortie 2011-12-02 15:04:53 -05:00
parent 5a32769300
commit f7886d6adb
4 changed files with 45 additions and 119 deletions

View File

@ -211,14 +211,12 @@ const char org_gtk_Actions_xml[] =
"</node>"; "</node>";
static GDBusInterfaceInfo *org_gtk_Actions; static GDBusInterfaceInfo *org_gtk_Actions;
static GHashTable *exported_groups;
typedef struct typedef struct
{ {
GActionGroup *action_group; GActionGroup *action_group;
GDBusConnection *connection; GDBusConnection *connection;
gchar *object_path; gchar *object_path;
guint registration_id;
GHashTable *pending_changes; GHashTable *pending_changes;
guint pending_id; guint pending_id;
gulong signal_ids[4]; gulong signal_ids[4];
@ -547,6 +545,21 @@ org_gtk_Actions_method_call (GDBusConnection *connection,
g_dbus_method_invocation_return_value (invocation, result); 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: * g_action_group_dbus_export_start:
* @connection: a #GDBusConnection * @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 * Returns: %TRUE if the export is successful, or %FALSE (with @error
* set) in the event of a failure. * set) in the event of a failure.
**/ **/
gboolean guint
g_action_group_dbus_export_start (GDBusConnection *connection, g_dbus_connection_export_action_group (GDBusConnection *connection,
const gchar *object_path, const gchar *object_path,
GActionGroup *action_group, GActionGroup *action_group,
GError **error) GError **error)
{ {
const GDBusInterfaceVTable vtable = { const GDBusInterfaceVTable vtable = {
org_gtk_Actions_method_call org_gtk_Actions_method_call
}; };
GActionGroupExporter *exporter; GActionGroupExporter *exporter;
guint id;
if G_UNLIKELY (exported_groups == NULL)
exported_groups = g_hash_table_new (NULL, NULL);
if G_UNLIKELY (org_gtk_Actions == NULL) 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); 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 = g_slice_new (GActionGroupExporter);
exporter->registration_id = g_dbus_connection_register_object (connection, object_path, org_gtk_Actions, id = g_dbus_connection_register_object (connection, object_path, org_gtk_Actions, &vtable,
&vtable, exporter, NULL, error); exporter, g_action_group_exporter_free, error);
if (exporter->registration_id == 0) if (id == 0)
{ {
g_slice_free (GActionGroupExporter, exporter); 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_changes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
exporter->pending_id = 0; exporter->pending_id = 0;
exporter->action_group = g_object_ref (action_group); 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_signal_connect (action_group, "action-enabled-changed",
G_CALLBACK (g_action_group_exporter_action_enabled_changed), exporter); G_CALLBACK (g_action_group_exporter_action_enabled_changed), exporter);
return TRUE; return id;
} }
/**
* 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 gboolean
g_action_group_dbus_export_stop (GActionGroup *action_group) g_dbus_connection_unexport_action_group (GDBusConnection *connection,
guint export_id)
{ {
GActionGroupExporter *exporter; return g_dbus_connection_unregister_object (connection, export_id);
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;
}
/**
* 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)
{
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;
} }

View File

@ -32,16 +32,13 @@
G_BEGIN_DECLS G_BEGIN_DECLS
gboolean g_action_group_dbus_export_start (GDBusConnection *connection, guint g_dbus_connection_export_action_group (GDBusConnection *connection,
const gchar *object_path, const gchar *object_path,
GActionGroup *action_group, GActionGroup *action_group,
GError **error); GError **error);
gboolean g_action_group_dbus_export_stop (GActionGroup *action_group); gboolean g_dbus_connection_unexport_action_group (GDBusConnection *connection,
guint export_id);
gboolean g_action_group_dbus_export_query (GActionGroup *action_group,
GDBusConnection **connection,
const gchar **object_path);
G_END_DECLS G_END_DECLS

View File

@ -81,7 +81,7 @@ struct _GApplicationImpl
const gchar *bus_name; const gchar *bus_name;
gchar *object_path; gchar *object_path;
guint object_id; guint object_id;
gboolean actions_exported; guint actions_id;
gboolean primary; gboolean primary;
gpointer app; gpointer app;
}; };
@ -247,9 +247,9 @@ g_application_impl_attempt_primary (GApplicationImpl *impl,
if (impl->object_id == 0) if (impl->object_id == 0)
return FALSE; 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; return FALSE;
/* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */ /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
@ -287,10 +287,10 @@ g_application_impl_stop_primary (GApplicationImpl *impl)
impl->object_id = 0; impl->object_id = 0;
} }
if (impl->actions_exported) if (impl->actions_id)
{ {
g_action_group_dbus_export_stop (impl->app); g_dbus_connection_unexport_action_group (impl->session_bus, impl->actions_id);
impl->actions_exported = FALSE; impl->actions_id = 0;
} }
if (impl->primary) if (impl->primary)

View File

@ -527,6 +527,7 @@ test_dbus_export (void)
}; };
GError *error = NULL; GError *error = NULL;
GVariant *v; GVariant *v;
guint id;
loop = g_main_loop_new (NULL, FALSE); loop = g_main_loop_new (NULL, FALSE);
@ -535,12 +536,9 @@ test_dbus_export (void)
group = g_simple_action_group_new (); group = g_simple_action_group_new ();
g_simple_action_group_add_entries (group, entries, G_N_ELEMENTS (entries), NULL); 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_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_dbus_action_group_new (bus, g_dbus_connection_get_unique_name (bus), "/", 0, NULL, got_proxy, NULL);
g_assert_no_error (error); g_assert_no_error (error);
@ -618,12 +616,12 @@ test_dbus_export (void)
g_assert (!g_variant_get_boolean (v)); g_assert (!g_variant_get_boolean (v));
g_variant_unref (v); g_variant_unref (v);
g_action_group_dbus_export_stop (G_ACTION_GROUP (group)); g_dbus_connection_unexport_action_group (bus, id);
g_assert (!g_action_group_dbus_export_query (G_ACTION_GROUP (group), NULL, NULL));
g_object_unref (proxy); g_object_unref (proxy);
g_object_unref (group); g_object_unref (group);
g_main_loop_unref (loop); g_main_loop_unref (loop);
g_object_unref (bus);
} }
int int