From 62401ed129ee3eb70b71d637485e63b1c8fac054 Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Mon, 13 Jun 2011 11:46:32 +0200 Subject: [PATCH] add contact-list.{c,py} example It demonstrate how to get all prepared contacts using a factory. --- examples/client/Makefile.am | 3 + examples/client/contact-list.c | 102 ++++++++++++++++++++++++++++++++ examples/client/python/contact-list.py | 32 ++++++++++ 3 files changed, 137 insertions(+), 0 deletions(-) create mode 100755 examples/client/contact-list.c create mode 100755 examples/client/python/contact-list.py diff --git a/examples/client/Makefile.am b/examples/client/Makefile.am index c452599..48a82f5 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 0000000..f8c6b7f --- /dev/null +++ b/examples/client/contact-list.c @@ -0,0 +1,102 @@ +/* + * contact-list + * + * Copyright © 2011 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 + +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; + const GQuark account_features[] = { + TP_ACCOUNT_FEATURE_CONNECTION, + 0 }; + const GQuark connection_features[] = { + TP_CONNECTION_FEATURE_CONTACT_LIST, + 0 }; + const TpContactFeature contact_features[] = { + TP_CONTACT_FEATURE_ALIAS, + TP_CONTACT_FEATURE_CONTACT_GROUPS, + }; + + 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 (factory, + account_features); + tp_simple_client_factory_add_connection_features (factory, + connection_features); + tp_simple_client_factory_add_contact_features (factory, + G_N_ELEMENTS (contact_features), contact_features); + + 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 0000000..65c4069 --- /dev/null +++ b/examples/client/python/contact-list.py @@ -0,0 +1,32 @@ +#!/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) + + accounts = manager.get_valid_accounts() + for account in accounts: + connection = account.get_connection() + if connection != 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() -- 1.7.4.1