From 726e4173951c47757adaaf97368af862bdd3e2e8 Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Mon, 17 Oct 2011 10:31:42 +0200 Subject: add high level API to block/unblock contacts https://bugs.freedesktop.org/show_bug.cgi?id=41801 --- docs/reference/telepathy-glib-sections.txt | 5 + telepathy-glib/connection-contact-list.c | 122 +++++++++++++++++++ telepathy-glib/connection-contact-list.h | 20 ++++ tests/dbus/Makefile.am | 6 + tests/dbus/contact-list-clt.c | 185 +++++++++++++++++++++++++++++ 5 files changed, 338 insertions(+) create mode 100644 tests/dbus/contact-list-clt.c diff --git a/docs/reference/telepathy-glib-sections.txt b/docs/reference/telepathy-glib-sections.txt index c57f66c1..a3ab164c 100644 --- a/docs/reference/telepathy-glib-sections.txt +++ b/docs/reference/telepathy-glib-sections.txt @@ -3716,6 +3716,11 @@ tp_connection_remove_group_async tp_connection_remove_group_finish tp_connection_rename_group_async tp_connection_rename_group_finish + +tp_connection_block_contacts_async +tp_connection_block_contacts_finish +tp_connection_unblock_contacts_async +tp_connection_unblock_contacts_finish tp_cli_connection_callback_for_connect tp_cli_connection_call_connect diff --git a/telepathy-glib/connection-contact-list.c b/telepathy-glib/connection-contact-list.c index daf28a6d..8dc7ef5f 100644 --- a/telepathy-glib/connection-contact-list.c +++ b/telepathy-glib/connection-contact-list.c @@ -1385,3 +1385,125 @@ tp_connection_rename_group_finish (TpConnection *self, { generic_finish (rename_group); } + +/* ContactBlocking */ + +/** + * tp_connection_block_contacts_async: + * @self: a #TpConnection + * @n_contacts: the number of contacts in @contacts (must be at least 1) + * @contacts: (array length=n_contacts): An array of #TpContact objects to + * block + * @report_abusive: if %TRUE, in addition to blocking, report these contacts as + * abusive to the server administrators, if supported, see + * #TpConnection:can-report-abusive + * @callback: a callback to call when the operation finishes + * @user_data: data to pass to @callback + * + * Direct the server to block @contacts. + * + * Since: 0.UNRELEASED + */ +void +tp_connection_block_contacts_async (TpConnection *self, + guint n_contacts, + TpContact * const *contacts, + gboolean report_abusive, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GSimpleAsyncResult *result; + GArray *handles; + gboolean supplied_contacts_are_valid; + + g_return_if_fail (TP_IS_CONNECTION (self)); + + supplied_contacts_are_valid = _tp_contacts_to_handles (self, n_contacts, + contacts, &handles); + g_return_if_fail (supplied_contacts_are_valid); + + result = g_simple_async_result_new ((GObject *) self, callback, user_data, + tp_connection_block_contacts_async); + + tp_cli_connection_interface_contact_blocking_call_block_contacts (self, -1, + handles, report_abusive, generic_callback, result, g_object_unref, NULL); + g_array_unref (handles); +} + +/** + * tp_connection_block_contacts_finish: + * @self: a #TpConnection + * @result: a #GAsyncResult + * @error: a #GError to fill + * + * Finishes tp_connection_block_contacts_async() + * + * Returns: %TRUE if the operation was successful, otherwise %FALSE. + * + * Since: 0.UNRELEASED + */ +gboolean +tp_connection_block_contacts_finish (TpConnection *self, + GAsyncResult *result, + GError **error) +{ + generic_finish (block_contacts); +} + +/** + * tp_connection_unblock_contacts_async: + * @self: a #TpConnection + * @n_contacts: the number of contacts in @contacts (must be at least 1) + * @contacts: (array length=n_contacts): An array of #TpContact objects to + * block + * @callback: a callback to call when the operation finishes + * @user_data: data to pass to @callback + * + * Direct the server to unblock @contacts. + * + * Since: 0.UNRELEASED + */ +void +tp_connection_unblock_contacts_async (TpConnection *self, + guint n_contacts, + TpContact * const *contacts, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GSimpleAsyncResult *result; + GArray *handles; + gboolean supplied_contacts_are_valid; + + g_return_if_fail (TP_IS_CONNECTION (self)); + + supplied_contacts_are_valid = _tp_contacts_to_handles (self, n_contacts, + contacts, &handles); + g_return_if_fail (supplied_contacts_are_valid); + + result = g_simple_async_result_new ((GObject *) self, callback, user_data, + tp_connection_unblock_contacts_async); + + tp_cli_connection_interface_contact_blocking_call_unblock_contacts (self, -1, + handles, generic_callback, result, g_object_unref, NULL); + g_array_unref (handles); +} + +/** + * tp_connection_unblock_contacts_finish: + * @self: a #TpConnection + * @result: a #GAsyncResult + * @error: a #GError to fill + * + * Finishes tp_connection_unblock_contacts_async() + * + * Returns: %TRUE if the operation was successful, otherwise %FALSE. + * + * Since: 0.UNRELEASED + */ +gboolean +tp_connection_unblock_contacts_finish (TpConnection *self, + GAsyncResult *result, + GError **error) +{ + generic_finish (unblock_contacts); +} diff --git a/telepathy-glib/connection-contact-list.h b/telepathy-glib/connection-contact-list.h index 8f74cbd9..48acf440 100644 --- a/telepathy-glib/connection-contact-list.h +++ b/telepathy-glib/connection-contact-list.h @@ -138,6 +138,26 @@ gboolean tp_connection_rename_group_finish (TpConnection *self, GAsyncResult *result, GError **error); +/* ContactBlocking */ + +void tp_connection_block_contacts_async (TpConnection *self, + guint n_contacts, + TpContact * const *contacts, + gboolean report_abusive, + GAsyncReadyCallback callback, + gpointer user_data); +gboolean tp_connection_block_contacts_finish (TpConnection *self, + GAsyncResult *result, + GError **error); + +void tp_connection_unblock_contacts_async (TpConnection *self, + guint n_contacts, + TpContact * const *contacts, + GAsyncReadyCallback callback, + gpointer user_data); +gboolean tp_connection_unblock_contacts_finish (TpConnection *self, + GAsyncResult *result, + GError **error); G_END_DECLS #endif diff --git a/tests/dbus/Makefile.am b/tests/dbus/Makefile.am index 3f1e8e62..122ef232 100644 --- a/tests/dbus/Makefile.am +++ b/tests/dbus/Makefile.am @@ -26,6 +26,7 @@ noinst_PROGRAMS = \ test-connection-interests \ test-connection-getinterfaces-failure \ test-contact-lists \ + test-contact-list-clt \ test-contacts \ test-contacts-bug-19101 \ test-contacts-mixin \ @@ -116,6 +117,11 @@ test_contact_lists_LDADD = \ $(LDADD) \ $(top_builddir)/examples/cm/contactlist/libexample-cm-contactlist.la +test_contact_list_clt_SOURCES = contact-list-clt.c +test_contact_list_clt_LDADD = \ + $(LDADD) \ + $(top_builddir)/examples/cm/contactlist/libexample-cm-contactlist.la + test_connection_balance_SOURCES = connection-balance.c test_connection_bug_18845_SOURCES = connection-bug-18845.c diff --git a/tests/dbus/contact-list-clt.c b/tests/dbus/contact-list-clt.c new file mode 100644 index 00000000..a5278153 --- /dev/null +++ b/tests/dbus/contact-list-clt.c @@ -0,0 +1,185 @@ +/* Tests of TpTextChannel + * + * Copyright © 2010 Collabora Ltd. + * + * Copying and distribution of this file, with or without modification, + * are permitted in any medium without royalty provided the copyright + * notice and this notice are preserved. + */ + +#include + +#include +#include + +#include "examples/cm/contactlist/conn.h" + +#include "tests/lib/util.h" + +typedef struct { + GMainLoop *mainloop; + TpDBusDaemon *dbus; + + /* Service side objects */ + TpBaseConnection *base_connection; + TpHandleRepoIface *contact_repo; + + /* Client side objects */ + TpConnection *connection; + TpTextChannel *channel; + TpTextChannel *sms_channel; + + GError *error /* initialized where needed */; + gint wait; +} Test; + +static void +setup (Test *test, + gconstpointer data) +{ + gchar *conn_name, *conn_path; + GQuark conn_features[] = { TP_CONNECTION_FEATURE_CONNECTED, 0 }; + + test->mainloop = g_main_loop_new (NULL, FALSE); + test->dbus = tp_tests_dbus_daemon_dup_or_die (); + + test->error = NULL; + + /* Create (service and client sides) connection objects */ + test->base_connection = tp_tests_object_new_static_class ( + EXAMPLE_TYPE_CONTACT_LIST_CONNECTION, + "account", "me@test.com", + "simulation-delay", 0, + "protocol", "test", + NULL); + + g_assert (tp_base_connection_register (test->base_connection, "example", + &conn_name, &conn_path, &test->error)); + g_assert_no_error (test->error); + + test->connection = tp_connection_new (test->dbus, conn_name, conn_path, + &test->error); + g_assert_no_error (test->error); + + test->contact_repo = tp_base_connection_get_handles (test->base_connection, + TP_HANDLE_TYPE_CONTACT); + g_assert (test->contact_repo != NULL); + + /* Connect the connection */ + tp_cli_connection_call_connect (test->connection, -1, NULL, NULL, NULL, NULL); + tp_tests_proxy_run_until_prepared (test->connection, conn_features); + + g_free (conn_name); + g_free (conn_path); +} + +static void +teardown (Test *test, + gconstpointer data) +{ + g_clear_error (&test->error); + + tp_clear_object (&test->dbus); + g_main_loop_unref (test->mainloop); + test->mainloop = NULL; + + tp_cli_connection_run_disconnect (test->connection, -1, &test->error, NULL); + g_assert_no_error (test->error); + + g_object_unref (test->connection); + g_object_unref (test->base_connection); +} + +static void +block_contacts_cb (GObject *source, + GAsyncResult *result, + gpointer user_data) +{ + Test *test = user_data; + + tp_connection_block_contacts_finish (TP_CONNECTION (source), result, + &test->error); + g_assert_no_error (test->error); + + test->wait--; + if (test->wait <= 0) + g_main_loop_quit (test->mainloop); +} + +static void +unblock_contacts_cb (GObject *source, + GAsyncResult *result, + gpointer user_data) +{ + Test *test = user_data; + + tp_connection_unblock_contacts_finish (TP_CONNECTION (source), result, + &test->error); + g_assert_no_error (test->error); + + test->wait--; + if (test->wait <= 0) + g_main_loop_quit (test->mainloop); +} + +static void +test_block_unblock (Test *test, + gconstpointer data G_GNUC_UNUSED) +{ + TpHandle handle; + TpContact *alice, *bob; + GPtrArray *arr; + + /* Create contacts */ + handle = tp_handle_ensure (test->contact_repo, "alice", NULL, &test->error); + g_assert_no_error (test->error); + + alice = tp_connection_dup_contact_if_possible (test->connection, handle, + "alice"); + g_assert (alice != NULL); + + handle = tp_handle_ensure (test->contact_repo, "bob", NULL, &test->error); + g_assert_no_error (test->error); + + bob = tp_connection_dup_contact_if_possible (test->connection, handle, "bob"); + g_assert (bob != NULL); + + arr = g_ptr_array_sized_new (2); + g_ptr_array_add (arr, alice); + g_ptr_array_add (arr, bob); + + /* Block contacts */ + tp_connection_block_contacts_async (test->connection, + arr->len, (TpContact * const *) arr->pdata, FALSE, + block_contacts_cb, test); + + test->wait = 1; + g_main_loop_run (test->mainloop); + g_assert_no_error (test->error); + + /* Unblock contacts */ + tp_connection_unblock_contacts_async (test->connection, + arr->len, (TpContact * const *) arr->pdata, + unblock_contacts_cb, test); + + test->wait = 1; + g_main_loop_run (test->mainloop); + g_assert_no_error (test->error); + + g_object_unref (alice); + g_object_unref (bob); + g_ptr_array_unref (arr); +} + +int +main (int argc, + char **argv) +{ + tp_tests_init (&argc, &argv); + g_test_bug_base ("http://bugs.freedesktop.org/show_bug.cgi?id="); + + g_test_add ("/contact-list-clt/blocking/block-unblock", Test, NULL, setup, + test_block_unblock, teardown); + + return g_test_run (); +} -- cgit v1.2.3