summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon McVittie <simon.mcvittie@collabora.co.uk>2013-10-28 13:02:13 +0000
committerSimon McVittie <simon.mcvittie@collabora.co.uk>2013-11-11 16:49:03 +0000
commit689c4297ff8cd7cf971ba4bf46ad605a3149f6b4 (patch)
tree7246bc44b23ec3cb1a7fb5a544c3d93bd7edab73
parent9cdd0c501a3c3d919468652f1ed3fad0202cb910 (diff)
tp_protocol_normalize_contact_async: add and test
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=71048 Reviewed-by: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
-rw-r--r--docs/reference/telepathy-glib-sections.txt3
-rw-r--r--telepathy-glib/protocol.c78
-rw-r--r--telepathy-glib/protocol.h12
-rw-r--r--tests/dbus/protocol-objects.c34
4 files changed, 127 insertions, 0 deletions
diff --git a/docs/reference/telepathy-glib-sections.txt b/docs/reference/telepathy-glib-sections.txt
index fa640d4d4..4c6679df2 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
+<SUBSECTION>
tp_protocol_get_avatar_requirements
<SUBSECTION>
tp_cli_protocol_call_identify_account
diff --git a/telepathy-glib/protocol.c b/telepathy-glib/protocol.c
index b634a7f0b..fc5adfa3c 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 bc36c5abc..162390734 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 <telepathy-glib/_gen/tp-cli-protocol.h>
diff --git a/tests/dbus/protocol-objects.c b/tests/dbus/protocol-objects.c
index 827e843da..248805728 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 ();
}