From 37a46435c1e3fb934c11f681f6a50178ffd61d00 Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Mon, 30 May 2011 13:55:47 +0200 Subject: [PATCH] add tp_channel_destroy_async() --- docs/reference/telepathy-glib-sections.txt | 2 + telepathy-glib/channel.c | 95 ++++++++++++++++++++++++++++ telepathy-glib/channel.h | 8 +++ tests/dbus/channel.c | 31 +++++++++ 4 files changed, 136 insertions(+), 0 deletions(-) diff --git a/docs/reference/telepathy-glib-sections.txt b/docs/reference/telepathy-glib-sections.txt index b66f70e..cc25ba4 100644 --- a/docs/reference/telepathy-glib-sections.txt +++ b/docs/reference/telepathy-glib-sections.txt @@ -3078,6 +3078,8 @@ tp_channel_leave_async tp_channel_leave_finish tp_channel_close_async tp_channel_close_finish +tp_channel_destroy_async +tp_channel_destroy_finish tp_channel_get_feature_quark_chat_states tp_channel_get_feature_quark_core diff --git a/telepathy-glib/channel.c b/telepathy-glib/channel.c index 724b17a..328e76a 100644 --- a/telepathy-glib/channel.c +++ b/telepathy-glib/channel.c @@ -2382,3 +2382,98 @@ tp_channel_close_finish (TpChannel *self, { _tp_implement_finish_void (self, tp_channel_close_async) } + +static void +channel_destroy_cb (TpChannel *channel, + const GError *error, + gpointer user_data, + GObject *weak_object) +{ + GSimpleAsyncResult *result = user_data; + + if (error != NULL) + { + DEBUG ("Destroy() failed: %s", error->message); + + if (tp_proxy_get_invalidated (channel) != NULL) + { + DEBUG ("Proxy has been invalidated; succeed"); + goto succeed; + } + + DEBUG ("Close channel then"); + + tp_cli_channel_call_close (channel, -1, channel_close_cb, result, + NULL, NULL); + return; + } + + DEBUG ("Destroy() succeeded"); + +succeed: + g_simple_async_result_complete (result); + g_object_unref (result); +} + +/** + * tp_channel_destroy_async: + * @self: a #TpChannel + * @callback: a callback to call when we left the channel + * @user_data: data to pass to @callback + * + * Destroy channel @self. + * If @self doesn't implement #TP_IFACE_QUARK_CHANNEL_INTERFACE_DESTROYABLE + * or if for any reason we can't destroy the channel, we close it. + * + * When the channel has been destroyed or closed, @callback will be called. + * You can then call tp_channel_destroy_finish() to get the result of + * the operation. + * + * Since: 0.15.UNRELEASED + */ +void +tp_channel_destroy_async (TpChannel *self, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GSimpleAsyncResult *result; + + g_return_if_fail (TP_IS_CHANNEL (self)); + + result = g_simple_async_result_new (G_OBJECT (self), callback, + user_data, tp_channel_destroy_async); + + if (tp_proxy_is_prepared (self, TP_CHANNEL_FEATURE_CORE) && + !tp_proxy_has_interface_by_id (self, + TP_IFACE_QUARK_CHANNEL_INTERFACE_DESTROYABLE)) + { + DEBUG ("Channel doesn't implement Destroy; fallback to Close()"); + + tp_cli_channel_call_close (self, -1, channel_close_cb, result, + NULL, NULL); + return; + } + + tp_cli_channel_interface_destroyable_call_destroy (self, -1, + channel_destroy_cb, result, NULL, NULL); +} + +/** + * tp_channel_destroy_finish: + * @self: a #TpChannel + * @result: a #GAsyncResult + * @error: a #GError to fill + * + * Finishes to leave a channel. + * + * Returns: %TRUE if the channel has been destroyed or closed; %FALSE otherwise + * + * Since: 0.15.UNRELEASED + */ +gboolean +tp_channel_destroy_finish (TpChannel *self, + GAsyncResult *result, + GError **error) +{ + _tp_implement_finish_void (self, tp_channel_destroy_async) +} diff --git a/telepathy-glib/channel.h b/telepathy-glib/channel.h index 440ae05..62da201 100644 --- a/telepathy-glib/channel.h +++ b/telepathy-glib/channel.h @@ -152,6 +152,14 @@ gboolean tp_channel_close_finish (TpChannel *self, GAsyncResult *result, GError **error); +void tp_channel_destroy_async (TpChannel *self, + GAsyncReadyCallback callback, + gpointer user_data); + +gboolean tp_channel_destroy_finish (TpChannel *self, + GAsyncResult *result, + GError **error); + G_END_DECLS #include diff --git a/tests/dbus/channel.c b/tests/dbus/channel.c index fab026a..397faf3 100644 --- a/tests/dbus/channel.c +++ b/tests/dbus/channel.c @@ -412,6 +412,34 @@ test_close_room (Test *test, check_not_removed (test->chan_room_service); } +static void +channel_destroy_cb (GObject *source, + GAsyncResult *result, + gpointer user_data) +{ + Test *test = user_data; + + tp_channel_destroy_finish (TP_CHANNEL (source), result, &test->error); + + test->wait--; + if (test->wait <= 0) + g_main_loop_quit (test->mainloop); +} + +static void +test_destroy (Test *test, + gconstpointer data G_GNUC_UNUSED) +{ + g_assert (tp_proxy_get_invalidated (test->channel_contact) == NULL); + + tp_channel_destroy_async (test->channel_contact, channel_destroy_cb, test); + + g_main_loop_run (test->mainloop); + g_assert_no_error (test->error); + + g_assert (tp_proxy_get_invalidated (test->channel_contact) != NULL); +} + int main (int argc, char **argv) @@ -442,5 +470,8 @@ main (int argc, g_test_add ("/channel/close/room", Test, NULL, setup, test_close_room, teardown); + g_test_add ("/channel/destroy", Test, NULL, setup, + test_destroy, teardown); + return g_test_run (); } -- 1.7.4.1