From 938cf3da1653efeabbdfac8ad9d2ea39240e5a34 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Mon, 28 Oct 2013 13:17:22 +0000 Subject: [PATCH 06/15] tp_protocol_identify_account_async: add and test To make the test a little more interesting and a little more realistic, we normalize the 'account' parameter to lower-case. --- docs/reference/telepathy-glib-sections.txt | 2 + examples/cm/echo-message-parts/protocol.c | 2 +- telepathy-glib/protocol.c | 70 ++++++++++++++++++++++++++++++ telepathy-glib/protocol.h | 12 +++++ tests/dbus/protocol-objects.c | 58 +++++++++++++++++++++++++ 5 files changed, 143 insertions(+), 1 deletion(-) diff --git a/docs/reference/telepathy-glib-sections.txt b/docs/reference/telepathy-glib-sections.txt index 4c6679d..df10421 100644 --- a/docs/reference/telepathy-glib-sections.txt +++ b/docs/reference/telepathy-glib-sections.txt @@ -6173,6 +6173,8 @@ tp_protocol_get_english_name tp_protocol_get_icon_name tp_protocol_get_vcard_field tp_protocol_get_authentication_types +tp_protocol_identify_account_async +tp_protocol_identify_account_finish tp_protocol_normalize_contact_async tp_protocol_normalize_contact_finish diff --git a/examples/cm/echo-message-parts/protocol.c b/examples/cm/echo-message-parts/protocol.c index 4512216..ae8894a 100644 --- a/examples/cm/echo-message-parts/protocol.c +++ b/examples/cm/echo-message-parts/protocol.c @@ -115,7 +115,7 @@ identify_account (TpBaseProtocol *self G_GNUC_UNUSED, const gchar *account = tp_asv_get_string (asv, "account"); if (account != NULL) - return g_strdup (account); + return g_utf8_strdown (account, -1); g_set_error (error, TP_ERROR, TP_ERROR_INVALID_ARGUMENT, "'account' parameter not given"); diff --git a/telepathy-glib/protocol.c b/telepathy-glib/protocol.c index fc5adfa..7296db8 100644 --- a/telepathy-glib/protocol.c +++ b/telepathy-glib/protocol.c @@ -45,6 +45,7 @@ #include "telepathy-glib/debug-internal.h" #include "telepathy-glib/proxy-internal.h" #include "telepathy-glib/util-internal.h" +#include "telepathy-glib/variant-util-internal.h" #include "telepathy-glib/_gen/tp-cli-protocol-body.h" @@ -1862,3 +1863,72 @@ tp_protocol_normalize_contact_finish (TpProtocol *self, return g_task_propagate_pointer (G_TASK (result), error); } + +/** + * tp_protocol_identify_account_async: + * @self: a protocol + * @vardict: the account parameters as a #GVariant of + * type %G_VARIANT_TYPE_VARDICT. If it is floating, ownership will + * be taken, as if via g_variant_ref_sink(). + * @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 + * + * Return a string that could identify the account with the given + * parameters. In most protocols that string is a normalized 'account' + * parameter, but some protocols have more complex requirements; + * for instance, on IRC, the 'account' (nickname) is insufficient, + * and must be combined with a server or network name. + * + * Since: 0.UNRELEASED + */ +void +tp_protocol_identify_account_async (TpProtocol *self, + GVariant *vardict, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GTask *task; + GHashTable *asv; + + g_return_if_fail (TP_IS_PROTOCOL (self)); + g_return_if_fail (vardict != NULL); + g_return_if_fail (g_variant_is_of_type (vardict, G_VARIANT_TYPE_VARDICT)); + /* 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_identify_account_async); + g_variant_ref_sink (vardict); + asv = _tp_asv_from_vardict (vardict); + tp_cli_protocol_call_identify_account (self, -1, asv, + tp_protocol_async_string_cb, task, g_object_unref, NULL); + g_hash_table_unref (asv); + g_variant_unref (vardict); +} + +/** + * tp_protocol_identify_account_finish: + * @self: a protocol + * @result: a #GAsyncResult + * @error: a #GError to fill + * + * Interpret the result of tp_protocol_identify_account_async(). + * + * Returns: (transfer full): a string identifying the account, + * or %NULL on error + * Since: 0.UNRELEASED + */ +gchar * +tp_protocol_identify_account_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_identify_account_async), NULL); + + return g_task_propagate_pointer (G_TASK (result), error); +} diff --git a/telepathy-glib/protocol.h b/telepathy-glib/protocol.h index 1623907..05f7781 100644 --- a/telepathy-glib/protocol.h +++ b/telepathy-glib/protocol.h @@ -137,6 +137,18 @@ gchar *tp_protocol_normalize_contact_finish (TpProtocol *self, GAsyncResult *result, GError **error); +_TP_AVAILABLE_IN_UNRELEASED +void tp_protocol_identify_account_async (TpProtocol *self, + GVariant *vardict, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data); + +_TP_AVAILABLE_IN_UNRELEASED +gchar *tp_protocol_identify_account_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 2488057..fee4afe 100644 --- a/tests/dbus/protocol-objects.c +++ b/tests/dbus/protocol-objects.c @@ -567,6 +567,62 @@ test_normalize (Test *test, g_clear_error (&test->error); } +static void +test_id (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_identify_account_async (test->protocol, + g_variant_new_parsed ("{ 'account': <'Hello'> }"), + NULL, tp_tests_result_ready_cb, &result); + tp_tests_run_until_result (&result); + s = tp_protocol_identify_account_finish (test->protocol, result, + &test->error); + g_assert_no_error (test->error); + g_assert_cmpstr (s, ==, "hello"); + g_clear_object (&result); + g_free (s); + + tp_protocol_identify_account_async (test->protocol, + g_variant_new_parsed ("{ 'account': <'Hello'>, 'unknown-param': <42> }"), + NULL, tp_tests_result_ready_cb, &result); + tp_tests_run_until_result (&result); + s = tp_protocol_identify_account_finish (test->protocol, result, + &test->error); + g_assert_error (test->error, TP_ERROR, TP_ERROR_INVALID_ARGUMENT); + g_assert_cmpstr (s, ==, NULL); + g_clear_object (&result); + g_clear_error (&test->error); + + tp_protocol_identify_account_async (test->protocol, + g_variant_new_parsed ("@a{sv} {}"), + NULL, tp_tests_result_ready_cb, &result); + tp_tests_run_until_result (&result); + s = tp_protocol_identify_account_finish (test->protocol, result, + &test->error); + g_assert_error (test->error, TP_ERROR, TP_ERROR_INVALID_ARGUMENT); + g_assert_cmpstr (s, ==, NULL); + g_clear_object (&result); + g_clear_error (&test->error); + + tp_protocol_identify_account_async (test->protocol, + g_variant_new_parsed ("@a{sv} { 'account': <''> }"), + NULL, tp_tests_result_ready_cb, &result); + tp_tests_run_until_result (&result); + s = tp_protocol_identify_account_finish (test->protocol, result, + &test->error); + g_assert_error (test->error, TP_ERROR, TP_ERROR_INVALID_ARGUMENT); + g_assert_cmpstr (s, ==, NULL); + g_clear_object (&result); + g_clear_error (&test->error); +} + int main (int argc, char **argv) @@ -592,6 +648,8 @@ main (int argc, test_protocol_object_from_file, teardown); g_test_add ("/protocol-objects/normalize", Test, NULL, setup, test_normalize, teardown); + g_test_add ("/protocol-objects/id", Test, NULL, setup, + test_id, teardown); return tp_tests_run_with_bus (); } -- 1.8.4.rc3