From c458b36f5f4c533566af79dc3a362fae4164f978 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Mon, 28 Oct 2013 13:02:13 +0000 Subject: [PATCH 05/15] tp_protocol_normalize_contact_async: add and test --- docs/reference/telepathy-glib-sections.txt | 3 ++ telepathy-glib/protocol.c | 78 ++++++++++++++++++++++++++++++ telepathy-glib/protocol.h | 12 +++++ tests/dbus/protocol-objects.c | 34 +++++++++++++ 4 files changed, 127 insertions(+) diff --git a/docs/reference/telepathy-glib-sections.txt b/docs/reference/telepathy-glib-sections.txt index fa640d4..4c6679d 100644 --- a/docs/reference/telepathy-glib-sections.txt +++ b/docs/reference/telepathy-glib-sections.txt @@ -6173,6 +6173,9 @@ tp_protocol_get_english_name tp_protocol_get_icon_name tp_protocol_get_vcard_field tp_protocol_get_authentication_types +tp_protocol_normalize_contact_async +tp_protocol_normalize_contact_finish + tp_protocol_get_avatar_requirements tp_cli_protocol_call_identify_account diff --git a/telepathy-glib/protocol.c b/telepathy-glib/protocol.c index b634a7f..fc5adfa 100644 --- a/telepathy-glib/protocol.c +++ b/telepathy-glib/protocol.c @@ -44,6 +44,7 @@ #include "telepathy-glib/capabilities-internal.h" #include "telepathy-glib/debug-internal.h" #include "telepathy-glib/proxy-internal.h" +#include "telepathy-glib/util-internal.h" #include "telepathy-glib/_gen/tp-cli-protocol-body.h" @@ -1784,3 +1785,80 @@ tp_protocol_get_cm_name (TpProtocol *self) return self->priv->cm_name; } + +/* + * Handle the result from a tp_cli_protocol_* function that + * returns one string. user_data is a #GTask. + */ +static void +tp_protocol_async_string_cb (TpProxy *proxy, + const gchar *normalized, + const GError *error, + gpointer user_data, + GObject *weak_object G_GNUC_UNUSED) +{ + if (error == NULL) + g_task_return_pointer (user_data, g_strdup (normalized), g_free); + else + g_task_return_error (user_data, g_error_copy (error)); +} + +/** + * tp_protocol_normalize_contact_async: + * @self: a protocol + * @contact: a contact identifier, possibly invalid + * @cancellable: (allow-none): may be used to cancel the async request + * @callback: (scope async): a callback to call when + * the request is satisfied + * @user_data: (closure) (allow-none): data to pass to @callback + * + * Perform best-effort offline contact normalization. This does syntactic + * normalization (e.g. transforming case-insensitive text to lower-case), + * but does not query servers or anything similar. + * + * Since: 0.UNRELEASED + */ +void +tp_protocol_normalize_contact_async (TpProtocol *self, + const gchar *contact, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GTask *task; + + g_return_if_fail (TP_IS_PROTOCOL (self)); + g_return_if_fail (contact != NULL); + /* this makes no sense to call for its side-effects */ + g_return_if_fail (callback != NULL); + + task = g_task_new (self, cancellable, callback, user_data); + g_task_set_source_tag (task, tp_protocol_normalize_contact_async); + + tp_cli_protocol_call_normalize_contact (self, -1, contact, + tp_protocol_async_string_cb, task, g_object_unref, NULL); +} + +/** + * tp_protocol_normalize_contact_finish: + * @self: a protocol + * @result: a #GAsyncResult + * @error: a #GError to fill + * + * Interpret the result of tp_protocol_normalize_contact_async(). + * + * Returns: (transfer full): the normalized form of @contact, + * or %NULL on error + * Since: 0.UNRELEASED + */ +gchar * +tp_protocol_normalize_contact_finish (TpProtocol *self, + GAsyncResult *result, + GError **error) +{ + g_return_val_if_fail (g_task_is_valid (result, self), NULL); + g_return_val_if_fail (g_async_result_is_tagged (result, + tp_protocol_normalize_contact_async), NULL); + + return g_task_propagate_pointer (G_TASK (result), error); +} diff --git a/telepathy-glib/protocol.h b/telepathy-glib/protocol.h index bc36c5a..1623907 100644 --- a/telepathy-glib/protocol.h +++ b/telepathy-glib/protocol.h @@ -125,6 +125,18 @@ TpCapabilities *tp_protocol_get_capabilities (TpProtocol *self); _TP_AVAILABLE_IN_0_16 TpAvatarRequirements * tp_protocol_get_avatar_requirements (TpProtocol *self); +_TP_AVAILABLE_IN_UNRELEASED +void tp_protocol_normalize_contact_async (TpProtocol *self, + const gchar *contact, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data); + +_TP_AVAILABLE_IN_UNRELEASED +gchar *tp_protocol_normalize_contact_finish (TpProtocol *self, + GAsyncResult *result, + GError **error); + G_END_DECLS #include diff --git a/tests/dbus/protocol-objects.c b/tests/dbus/protocol-objects.c index 827e843..2488057 100644 --- a/tests/dbus/protocol-objects.c +++ b/tests/dbus/protocol-objects.c @@ -535,6 +535,38 @@ test_protocol_object_from_file (Test *test, check_avatar_requirements (req); } +static void +test_normalize (Test *test, + gconstpointer data G_GNUC_UNUSED) +{ + GAsyncResult *result = NULL; + gchar *s; + + tp_tests_proxy_run_until_prepared (test->cm, NULL); + test->protocol = g_object_ref ( + tp_connection_manager_get_protocol_object (test->cm, "example")); + + tp_protocol_normalize_contact_async (test->protocol, + "MiXeDcAsE", NULL, tp_tests_result_ready_cb, &result); + tp_tests_run_until_result (&result); + s = tp_protocol_normalize_contact_finish (test->protocol, result, + &test->error); + g_assert_no_error (test->error); + g_assert_cmpstr (s, ==, "mixedcase"); + g_clear_object (&result); + g_free (s); + + tp_protocol_normalize_contact_async (test->protocol, + "", NULL, tp_tests_result_ready_cb, &result); + tp_tests_run_until_result (&result); + s = tp_protocol_normalize_contact_finish (test->protocol, result, + &test->error); + g_assert_error (test->error, TP_ERROR, TP_ERROR_INVALID_HANDLE); + g_assert_cmpstr (s, ==, NULL); + g_clear_object (&result); + g_clear_error (&test->error); +} + int main (int argc, char **argv) @@ -558,6 +590,8 @@ main (int argc, test_protocol_object_old, teardown); g_test_add ("/protocol-objects/object-from-file", Test, NULL, setup, test_protocol_object_from_file, teardown); + g_test_add ("/protocol-objects/normalize", Test, NULL, setup, + test_normalize, teardown); return tp_tests_run_with_bus (); } -- 1.8.4.rc3