From 68b98381b746ec9dd9e0538a4ebd11db5e4c026d Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Wed, 22 Jun 2011 12:33:44 +0200 Subject: [PATCH] add tp_text_channel_ack_all_pending_messages_async() (#38559) --- docs/reference/telepathy-glib-sections.txt | 2 + telepathy-glib/text-channel.c | 74 ++++++++++++++++++++++++++++ telepathy-glib/text-channel.h | 8 +++ tests/dbus/text-channel.c | 69 ++++++++++++++++++++++++++ 4 files changed, 153 insertions(+), 0 deletions(-) diff --git a/docs/reference/telepathy-glib-sections.txt b/docs/reference/telepathy-glib-sections.txt index 62b7e7e..936c0e5 100644 --- a/docs/reference/telepathy-glib-sections.txt +++ b/docs/reference/telepathy-glib-sections.txt @@ -5616,6 +5616,8 @@ tp_text_channel_ack_messages_async tp_text_channel_ack_messages_finish tp_text_channel_ack_message_async tp_text_channel_ack_message_finish +tp_text_channel_ack_all_pending_messages_async +tp_text_channel_ack_all_pending_messages_finish tp_text_channel_set_chat_state_async tp_text_channel_set_chat_state_finish tp_text_channel_supports_message_type diff --git a/telepathy-glib/text-channel.c b/telepathy-glib/text-channel.c index 948f885..c04e1b8 100644 --- a/telepathy-glib/text-channel.c +++ b/telepathy-glib/text-channel.c @@ -1897,3 +1897,77 @@ tp_text_channel_get_sms_length_finish (TpTextChannel *self, return TRUE; } + +static void +ack_all_pending_messages_cb (GObject *source, + GAsyncResult *result, + gpointer user_data) +{ + TpTextChannel *self = (TpTextChannel *) source; + GSimpleAsyncResult *main_result = user_data; + GError *error = NULL; + + if (!tp_text_channel_ack_messages_finish (self, result, &error)) + { + g_simple_async_result_take_error (main_result, error); + } + + g_simple_async_result_complete (main_result); + g_object_unref (main_result); +} + +/** + * tp_text_channel_ack_all_pending_messages_async: + * @self: a #TpTextChannel + * @callback: a callback to call when the messages have been acked + * @user_data: data to pass to @callback + * + * Acknowledge all the pending messages. This is equivalent of calling + * tp_text_channel_ack_messages_async() with the list of #TpSignalledMessage + * returned by tp_text_channel_get_pending_messages(). + * + * Once the messages have been acked, @callback will be called. + * You can then call tp_text_channel_ack_all_pending_messages_finish() to get + * the result of the operation. + * + * See tp_text_channel_ack_message_async() about acknowledging messages. + * + * Since: 0.15.UNRELEASED + */ +void +tp_text_channel_ack_all_pending_messages_async (TpTextChannel *self, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GSimpleAsyncResult *result; + GList *messages; + + result = g_simple_async_result_new (G_OBJECT (self), callback, + user_data, tp_text_channel_ack_all_pending_messages_async); + + messages = g_queue_peek_head_link (self->priv->pending_messages); + + tp_text_channel_ack_messages_async (self, messages, + ack_all_pending_messages_cb, result); +} + +/** + * tp_text_channel_ack_all_pending_messages_finish: + * @self: a #TpTextChannel + * @result: a #GAsyncResult + * @error: a #GError to fill + * + * Finishes to ack all the pending of messages. + * + * Returns: %TRUE if the messages have been acked, %FALSE otherwise. + * + * Since: 0.15.UNRELEASED + */ +gboolean +tp_text_channel_ack_all_pending_messages_finish (TpTextChannel *self, + GAsyncResult *result, + GError **error) +{ + _tp_implement_finish_void (self, + tp_text_channel_ack_all_pending_messages_async) +} diff --git a/telepathy-glib/text-channel.h b/telepathy-glib/text-channel.h index 50a3951..4d9fec5 100644 --- a/telepathy-glib/text-channel.h +++ b/telepathy-glib/text-channel.h @@ -110,6 +110,14 @@ gboolean tp_text_channel_ack_message_finish (TpTextChannel *self, GAsyncResult *result, GError **error); +void tp_text_channel_ack_all_pending_messages_async (TpTextChannel *self, + GAsyncReadyCallback callback, + gpointer user_data); + +gboolean tp_text_channel_ack_all_pending_messages_finish (TpTextChannel *self, + GAsyncResult *result, + GError **error); + void tp_text_channel_set_chat_state_async (TpTextChannel *self, TpChannelChatState state, GAsyncReadyCallback callback, diff --git a/tests/dbus/text-channel.c b/tests/dbus/text-channel.c index 50e72dd..2f1b216 100644 --- a/tests/dbus/text-channel.c +++ b/tests/dbus/text-channel.c @@ -728,6 +728,73 @@ test_get_sms_length (Test *test, g_object_unref (msg); } +static void +all_pending_messages_acked_cb (GObject *source, + GAsyncResult *result, + gpointer user_data) +{ + Test *test = user_data; + + tp_text_channel_ack_all_pending_messages_finish (TP_TEXT_CHANNEL (source), + result, &test->error); + + test->wait--; + if (test->wait <= 0) + g_main_loop_quit (test->mainloop); +} + +static void +test_ack_all_pending_messages (Test *test, + gconstpointer data G_GNUC_UNUSED) +{ + GQuark features[] = { TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES, 0 }; + GList *messages; + TpMessage *msg; + + /* Send a first message */ + msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL, + "Badger"); + + tp_text_channel_send_message_async (test->channel, msg, 0, + send_message_cb, test); + + g_object_unref (msg); + + /* Send a second message */ + msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL, + "Snake"); + + tp_text_channel_send_message_async (test->channel, msg, 0, + send_message_cb, test); + + g_object_unref (msg); + + test->wait = 2; + g_main_loop_run (test->mainloop); + g_assert_no_error (test->error); + + tp_proxy_prepare_async (test->channel, features, + proxy_prepare_cb, test); + + g_main_loop_run (test->mainloop); + g_assert_no_error (test->error); + + messages = tp_text_channel_get_pending_messages (test->channel); + g_assert_cmpuint (g_list_length (messages), ==, 2); + + tp_text_channel_ack_all_pending_messages_async (test->channel, + all_pending_messages_acked_cb, test); + + g_main_loop_run (test->mainloop); + g_assert_no_error (test->error); + + g_list_free (messages); + + /* Messages have been acked so there is no pending messages */ + messages = tp_text_channel_get_pending_messages (test->channel); + g_assert_cmpuint (g_list_length (messages), ==, 0); +} + int main (int argc, char **argv) @@ -753,6 +820,8 @@ main (int argc, test_sms_feature, teardown); g_test_add ("/text-channel/get-sms-length", Test, NULL, setup, test_get_sms_length, teardown); + g_test_add ("/text-channel/ack-all-pending-messages", Test, NULL, setup, + test_ack_all_pending_messages, teardown); return g_test_run (); } -- 1.7.4.1