Example #1
0
GDBusClient *g_dbus_client_new(DBusConnection *connection,
					const char *service, const char *path)
{
	GDBusClient *client;
	unsigned int i;

	if (connection == NULL)
		return NULL;

	client = g_try_new0(GDBusClient, 1);
	if (client == NULL)
		return NULL;

	if (dbus_connection_add_filter(connection, message_filter,
						client, NULL) == FALSE) {
		g_free(client);
		return NULL;
	}

	client->dbus_conn = dbus_connection_ref(connection);
	client->service_name = g_strdup(service);
	client->base_path = g_strdup(path);

	client->match_rules = g_ptr_array_sized_new(1);
	g_ptr_array_set_free_func(client->match_rules, g_free);

	client->watch = g_dbus_add_service_watch(connection, service,
						service_connect,
						service_disconnect,
						client, NULL);
	client->added_watch = g_dbus_add_signal_watch(connection, service,
						"/",
						DBUS_INTERFACE_OBJECT_MANAGER,
						"InterfacesAdded",
						interfaces_added,
						client, NULL);
	client->removed_watch = g_dbus_add_signal_watch(connection, service,
						"/",
						DBUS_INTERFACE_OBJECT_MANAGER,
						"InterfacesRemoved",
						interfaces_removed,
						client, NULL);
	g_ptr_array_add(client->match_rules, g_strdup_printf("type='signal',"
				"sender='%s',path_namespace='%s'",
				client->service_name, client->base_path));

	for (i = 0; i < client->match_rules->len; i++) {
		modify_match(client->dbus_conn, "AddMatch",
				g_ptr_array_index(client->match_rules, i));
	}

	return g_dbus_client_ref(client);
}
Example #2
0
void g_dbus_client_unref(GDBusClient *client)
{
	unsigned int i;

	if (client == NULL)
		return;

	if (__sync_sub_and_fetch(&client->ref_count, 1) > 0)
		return;

	if (client->pending_call != NULL) {
		dbus_pending_call_cancel(client->pending_call);
		dbus_pending_call_unref(client->pending_call);
	}

	if (client->get_objects_call != NULL) {
		dbus_pending_call_cancel(client->get_objects_call);
		dbus_pending_call_unref(client->get_objects_call);
	}

	for (i = 0; i < client->match_rules->len; i++) {
		modify_match(client->dbus_conn, "RemoveMatch",
				g_ptr_array_index(client->match_rules, i));
	}

	g_ptr_array_free(client->match_rules, TRUE);

	dbus_connection_remove_filter(client->dbus_conn,
						message_filter, client);

	g_list_free_full(client->proxy_list, proxy_free);

	/*
	 * Don't call disconn_func twice if disconnection
	 * was previously reported.
	 */
	if (client->disconn_func && client->connected)
		client->disconn_func(client->dbus_conn, client->disconn_data);

	g_dbus_remove_watch(client->dbus_conn, client->watch);
	g_dbus_remove_watch(client->dbus_conn, client->added_watch);
	g_dbus_remove_watch(client->dbus_conn, client->removed_watch);

	dbus_connection_unref(client->dbus_conn);

	g_free(client->service_name);
	g_free(client->base_path);
	g_free(client->root_path);

	g_free(client);
}