diff options
author | Xavier Claessens <xclaesse@gmail.com> | 2011-06-13 11:46:32 +0200 |
---|---|---|
committer | Xavier Claessens <xclaesse@gmail.com> | 2011-08-04 21:42:54 +0200 |
commit | 5c71b0d607786524637e5d23ed898319c75bb5fc (patch) | |
tree | 77c04b15045b95d178a5c0e99b0b3050e1a35585 /examples/client | |
parent | 4834baa40d6270b12270d2ee7fa49230318db4e3 (diff) |
add contact-list.{c,py} example
It demonstrate how to get all prepared contacts using a factory.
Diffstat (limited to 'examples/client')
-rw-r--r-- | examples/client/Makefile.am | 3 | ||||
-rwxr-xr-x | examples/client/contact-list.c | 96 | ||||
-rwxr-xr-x | examples/client/python/contact-list.py | 31 |
3 files changed, 130 insertions, 0 deletions
diff --git a/examples/client/Makefile.am b/examples/client/Makefile.am index c4525994f..48a82f508 100644 --- a/examples/client/Makefile.am +++ b/examples/client/Makefile.am @@ -35,6 +35,9 @@ telepathy_example_approver_SOURCES = approver.c EXAMPLES += telepathy-example-text-handler telepathy_example_text_handler_SOURCES = text-handler.c +EXAMPLES += telepathy-example-contact-list +telepathy_example_contact_list_SOURCES = contact-list.c + if INSTALL_EXAMPLES bin_PROGRAMS = $(EXAMPLES) else diff --git a/examples/client/contact-list.c b/examples/client/contact-list.c new file mode 100755 index 000000000..5cda23c61 --- /dev/null +++ b/examples/client/contact-list.c @@ -0,0 +1,96 @@ +/* + * contact-list + * + * Copyright © 2011 Collabora Ltd. <http://www.collabora.co.uk/> + * + * 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 <telepathy-glib/telepathy-glib.h> +#include <telepathy-glib/debug.h> + +static void +account_manager_prepared_cb (GObject *object, + GAsyncResult *res, + gpointer user_data) +{ + TpAccountManager *manager = (TpAccountManager *) object; + GMainLoop *loop = user_data; + GList *accounts; + GError *error = NULL; + + if (!tp_proxy_prepare_finish (object, res, &error)) + { + g_print ("Error preparing AM: %s\n", error->message); + goto OUT; + } + + for (accounts = tp_account_manager_get_valid_accounts (manager); + accounts != NULL; accounts = g_list_delete_link (accounts, accounts)) + { + TpAccount *account = accounts->data; + TpConnection *connection = tp_account_get_connection (account); + GPtrArray *contacts; + guint i; + + if (connection == NULL) + continue; + + contacts = tp_connection_dup_contact_list (connection); + for (i = 0; i < contacts->len; i++) + { + TpContact *contact = g_ptr_array_index (contacts, i); + const gchar * const *groups; + + g_print ("contact %s (%s) in groups:\n", + tp_contact_get_identifier (contact), + tp_contact_get_alias (contact)); + + for (groups = tp_contact_get_contact_groups (contact); + *groups != NULL; groups++) + g_print (" %s\n", *groups); + } + g_ptr_array_unref (contacts); + } + +OUT: + g_main_loop_quit (loop); +} + +int +main (int argc, + char **argv) +{ + TpAccountManager *manager; + TpSimpleClientFactory *factory; + GMainLoop *loop; + + g_type_init (); + tp_debug_set_flags (g_getenv ("EXAMPLE_DEBUG")); + + loop = g_main_loop_new (NULL, FALSE); + + manager = tp_account_manager_dup (); + factory = tp_proxy_get_factory (manager); + tp_simple_client_factory_add_account_features_varargs (factory, + TP_ACCOUNT_FEATURE_CONNECTION, + 0); + tp_simple_client_factory_add_connection_features_varargs (factory, + TP_CONNECTION_FEATURE_CONTACT_LIST, + 0); + tp_simple_client_factory_add_contact_features_varargs (factory, + TP_CONTACT_FEATURE_ALIAS, + TP_CONTACT_FEATURE_CONTACT_GROUPS, + TP_CONTACT_FEATURE_INVALID); + + tp_proxy_prepare_async (manager, NULL, account_manager_prepared_cb, loop); + + g_main_loop_run (loop); + + g_object_unref (manager); + g_main_loop_unref (loop); + + return 0; +} diff --git a/examples/client/python/contact-list.py b/examples/client/python/contact-list.py new file mode 100755 index 000000000..8549a4ed7 --- /dev/null +++ b/examples/client/python/contact-list.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python + +import os +import gobject +gobject.threads_init() + +from gi.repository import TelepathyGLib + +def manager_prepared_cb(manager, result, loop): + manager.prepare_finish(result) + + for account in manager.get_valid_accounts(): + connection = account.get_connection() + if connection is not None: + contacts = connection.dup_contact_list() + for contact in contacts: + print "%s (%s)" % (contact.get_identifier(), contact.get_contact_groups()) + loop.quit() + +if __name__ == '__main__': + TelepathyGLib.debug_set_flags(os.getenv('EXAMPLE_DEBUG', '')) + + loop = gobject.MainLoop() + manager = TelepathyGLib.AccountManager.dup() + factory = manager.get_factory() + factory.add_account_features([TelepathyGLib.Account.get_feature_quark_connection()]) + factory.add_connection_features([TelepathyGLib.Connection.get_feature_quark_contact_list()]) + factory.add_contact_features([TelepathyGLib.ContactFeature.CONTACT_GROUPS]) + + manager.prepare_async(None, manager_prepared_cb, loop) + loop.run() |