From ca9935be229c23984b0f5867247c2561e86aa64d Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Fri, 20 May 2011 11:55:44 +0200 Subject: [PATCH] add tp_text_channel_get_sms_length_async() --- docs/reference/telepathy-glib-sections.txt | 2 + telepathy-glib/text-channel.c | 148 ++++++++++++++++++++++++++++ telepathy-glib/text-channel.h | 12 +++ tests/dbus/text-channel.c | 46 +++++++++ 4 files changed, 208 insertions(+), 0 deletions(-) diff --git a/docs/reference/telepathy-glib-sections.txt b/docs/reference/telepathy-glib-sections.txt index 8bc621e..3e76e33 100644 --- a/docs/reference/telepathy-glib-sections.txt +++ b/docs/reference/telepathy-glib-sections.txt @@ -5599,6 +5599,8 @@ tp_text_channel_supports_message_type TP_TEXT_CHANNEL_FEATURE_SMS tp_text_channel_is_sms tp_text_channel_get_sms_flash +tp_text_channel_get_sms_length_async +tp_text_channel_get_sms_length_finish TP_IS_TEXT_CHANNEL TP_IS_TEXT_CHANNEL_CLASS diff --git a/telepathy-glib/text-channel.c b/telepathy-glib/text-channel.c index 7e74b18..4063868 100644 --- a/telepathy-glib/text-channel.c +++ b/telepathy-glib/text-channel.c @@ -1734,3 +1734,151 @@ tp_text_channel_get_sms_flash (TpTextChannel *self) return self->priv->sms_flash; } + +typedef struct +{ + guint chunks_required; + gint remaining_characters; + gint estimated_cost; +} GetSmsLengthReturn; + +static GetSmsLengthReturn * +get_sms_length_return_new (guint chunks_required, + gint remaining_characters, + gint estimated_cost) +{ + GetSmsLengthReturn *result = g_slice_new (GetSmsLengthReturn); + + result->chunks_required = chunks_required; + result->remaining_characters = remaining_characters; + result->estimated_cost = estimated_cost; + + return result; +} + +static void +get_sms_length_return_free (GetSmsLengthReturn *r) +{ + g_slice_free (GetSmsLengthReturn, r); +} + +static void +get_sms_length_cb (TpChannel *proxy, + guint chunks_required, + gint remaining_characters, + gint estimated_cost, + const GError *error, + gpointer user_data, + GObject *weak_object) +{ + GSimpleAsyncResult *result = user_data; + GetSmsLengthReturn *r; + + if (error != NULL) + { + DEBUG ("Failed to get SMS length: %s", error->message); + + g_simple_async_result_set_from_error (result, error); + } + + r = get_sms_length_return_new (chunks_required, remaining_characters, + estimated_cost); + + g_simple_async_result_set_op_res_gpointer (result, r, + (GDestroyNotify) get_sms_length_return_free); + + g_simple_async_result_complete (result); +} + +/** + * tp_text_channel_get_sms_length_async: + * @self: a #TpTextChannel + * @message: a #TpClientMessage + * @callback: a callback to call when the request has been satisfied + * @user_data: data to pass to @callback + * + * Starts an async call to get the number of 140 octet chunks required to + * send a #message via SMS on #self, as well as the number of remaining + * characters available in the final chunk and, if possible, + * an estimate of the cost. + * + * Once the request has been satisfied, @callback will be called. + * You can then call tp_text_channel_get_sms_length_finish() to get the + * result of the operation. + * + * Since: 0.15.UNRELEASED + */ +void +tp_text_channel_get_sms_length_async (TpTextChannel *self, + TpMessage *message, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GSimpleAsyncResult *result; + + result = g_simple_async_result_new ((GObject *) self, callback, user_data, + tp_text_channel_get_sms_length_async); + + tp_cli_channel_interface_sms_call_get_sms_length ((TpChannel *) self, -1, + message->parts, get_sms_length_cb, result, g_object_unref, + G_OBJECT (self)); +} + + +/** + * tp_text_channel_get_sms_length_finish: + * @self: a #TpTextChannel + * @result: a #GAsyncResult + * @chunks_required: (out): if not %NULL used to return + * the number of 140 octet chunks required to send the message. + * @remaining_characters: (out): if not %NULL used to return + * the number of further characters that can be fit in the final chunk. + * A negative value indicates that the message will be truncated by + * abs(@remaining_characters). + * The value #G_MININT32 indicates the message will be truncated by + * an unknown amount. + * @estimated_cost: (out): if not %NULL used to return + * the estimated cost of sending this message. + * The currency and scale of this value are the same as the + * values of the #TpConnection:balance-scale and + * #TpConnection:balance-currency properties. + * A value of -1 indicates the cost could not be estimated. + * @error: a #GError to fill + * + * Finishes an async SMS length request. + * + * Returns: %TRUE if the number of 140 octet chunks required to send + * the message has been retrieved, %FALSE otherwise. + * + * Since: 0.15.UNRELEASED + */ +gboolean +tp_text_channel_get_sms_length_finish (TpTextChannel *self, + GAsyncResult *result, + guint *chunks_required, + gint *remaining_characters, + gint *estimated_cost, + GError **error) +{ + GSimpleAsyncResult *simple = (GSimpleAsyncResult *) result; + GetSmsLengthReturn *r; + + if (g_simple_async_result_propagate_error (simple, error)) + return FALSE; + + g_return_val_if_fail (g_simple_async_result_is_valid (result, + G_OBJECT (self), tp_text_channel_get_sms_length_async), FALSE); + + r = g_simple_async_result_get_op_res_gpointer (simple); + + if (chunks_required != NULL) + *chunks_required = r->chunks_required; + + if (remaining_characters != NULL) + *remaining_characters = r->remaining_characters; + + if (estimated_cost != NULL) + *estimated_cost = r->estimated_cost; + + return TRUE; +} diff --git a/telepathy-glib/text-channel.h b/telepathy-glib/text-channel.h index a6d9e6a..5095018 100644 --- a/telepathy-glib/text-channel.h +++ b/telepathy-glib/text-channel.h @@ -127,6 +127,18 @@ gboolean tp_text_channel_is_sms (TpTextChannel *self); gboolean tp_text_channel_get_sms_flash (TpTextChannel *self); +void tp_text_channel_get_sms_length_async (TpTextChannel *self, + TpMessage *message, + GAsyncReadyCallback callback, + gpointer user_data); + +gboolean tp_text_channel_get_sms_length_finish (TpTextChannel *self, + GAsyncResult *result, + guint *chunks_required, + gint *remaining_characters, + gint *estimated_cost, + GError **error); + G_END_DECLS #endif diff --git a/tests/dbus/text-channel.c b/tests/dbus/text-channel.c index 8319f8f..68e0f56 100644 --- a/tests/dbus/text-channel.c +++ b/tests/dbus/text-channel.c @@ -684,6 +684,50 @@ test_sms_feature (Test *test, g_assert (!is_sms); } +#define MSG "Oh hi!" + +static void +get_sms_length_cb (GObject *source, + GAsyncResult *result, + gpointer user_data) +{ + Test *test = user_data; + guint chunks_required; + gint remaining_characters; + gint estimated_cost; + + tp_text_channel_get_sms_length_finish (TP_TEXT_CHANNEL (source), result, + &chunks_required, &remaining_characters, &estimated_cost, &test->error); + + g_assert_cmpuint (chunks_required, ==, strlen (MSG)); + g_assert_cmpint (remaining_characters, ==, + EXAMPLE_ECHO_2_CHANNEL_MAX_SMS_LENGTH - strlen (MSG)); + g_assert_cmpint (estimated_cost, ==, -1); + + test->wait--; + if (test->wait <= 0) + g_main_loop_quit (test->mainloop); +} + + +static void +test_get_sms_length (Test *test, + gconstpointer data G_GNUC_UNUSED) +{ + TpMessage *msg; + + msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL, MSG); + + tp_text_channel_get_sms_length_async (test->channel, msg, + get_sms_length_cb, test); + + test->wait++; + g_main_loop_run (test->mainloop); + g_assert_no_error (test->error); + + g_object_unref (msg); +} + int main (int argc, char **argv) @@ -707,6 +751,8 @@ main (int argc, test_message_sent, teardown); g_test_add ("/text-channel/sms-feature", Test, NULL, setup, test_sms_feature, teardown); + g_test_add ("/text-channel/get-sms-length", Test, NULL, setup, + test_get_sms_length, teardown); return g_test_run (); } -- 1.7.4.1