diff options
author | Danielle Madeley <danielle.madeley@collabora.co.uk> | 2010-08-12 13:02:52 +1000 |
---|---|---|
committer | Danielle Madeley <danielle.madeley@collabora.co.uk> | 2010-08-12 13:02:52 +1000 |
commit | ca02b76bdaa8d4e784e17582d1fed3aad22d3fb2 (patch) | |
tree | 5a7c1f1e5590f813425a4af42e454bf649a531b1 | |
parent | b6ccd9e76887bdd33b63f93b3d7655d43377fb8a (diff) |
Port glib_jabber_muc to TpAccount
-rw-r--r-- | docs/examples/glib_jabber_muc/example.c | 673 |
1 files changed, 313 insertions, 360 deletions
diff --git a/docs/examples/glib_jabber_muc/example.c b/docs/examples/glib_jabber_muc/example.c index d5d67de..cd56b5d 100644 --- a/docs/examples/glib_jabber_muc/example.c +++ b/docs/examples/glib_jabber_muc/example.c @@ -2,454 +2,407 @@ #include <glib.h> -#include <telepathy-glib/connection-manager.h> -#include <telepathy-glib/connection.h> -#include <telepathy-glib/channel.h> -#include <telepathy-glib/interfaces.h> -#include <telepathy-glib/gtypes.h> -#include <telepathy-glib/util.h> -#include <telepathy-glib/enums.h> -#include <telepathy-glib/debug.h> +#include <telepathy-glib/telepathy-glib.h> static GMainLoop *loop = NULL; -static TpDBusDaemon *bus_daemon = NULL; -static TpConnection *conn = NULL; static void handle_error (const GError *error) { - if (error) - { - g_print ("ERROR: %s\n", error->message); - tp_cli_connection_call_disconnect (conn, -1, NULL, - NULL, NULL, NULL); - } + if (error) + { + g_print ("ERROR: %s\n", error->message); + g_main_loop_quit (loop); + } } /* begin ex.channel.contactlist.user-defined.glib */ static void -new_channels_cb (TpConnection *conn, - const GPtrArray *channels, - gpointer user_data, - GObject *weak_obj) +new_channels_cb (TpConnection *conn, + const GPtrArray *channels, + gpointer user_data, + GObject *weak_obj) { - GError *error = NULL; - - /* channels has the D-Bus type a(oa{sv}), which decomposes to: - * - a GPtrArray containing a GValueArray for each channel - * - each GValueArray contains - * - an object path - * - an a{sv} map - */ - - int i; - for (i = 0; i < channels->len; i++) - { - GValueArray *channel = g_ptr_array_index (channels, i); - char *object_path; - GHashTable *map; - - tp_value_array_unpack (channel, 2, - &object_path, - &map); - - const char *type = tp_asv_get_string (map, - TP_IFACE_CHANNEL ".ChannelType"); - const char *id = tp_asv_get_string (map, - TP_IFACE_CHANNEL ".TargetID"); - - g_print ("New channel %s: %s\n", type, id); - } + int i; + GError *error = NULL; + + /* channels has the D-Bus type a(oa{sv}), which decomposes to: + * - a GPtrArray containing a GValueArray for each channel + * - each GValueArray contains + * - an object path + * - an a{sv} map + */ + + for (i = 0; i < channels->len; i++) + { + GValueArray *channel = g_ptr_array_index (channels, i); + char *object_path; + GHashTable *map; + const char *type, *id; + + tp_value_array_unpack (channel, 2, + &object_path, + &map); + + type = tp_asv_get_string (map, TP_PROP_CHANNEL_CHANNEL_TYPE); + id = tp_asv_get_string (map, TP_PROP_CHANNEL_TARGET_ID); + + g_print ("New channel %s: %s\n", type, id); + } } /* end ex.channel.contactlist.user-defined.glib */ static void -get_channels_cb (TpProxy *proxy, - const GValue *value, - const GError *in_error, - gpointer user_data, - GObject *weak_obj) +get_channels_cb (TpProxy *conn, + const GValue *value, + const GError *in_error, + gpointer user_data, + GObject *weak_obj) { - handle_error (in_error); + handle_error (in_error); - g_return_if_fail (G_VALUE_HOLDS (value, - TP_ARRAY_TYPE_CHANNEL_DETAILS_LIST)); + g_return_if_fail (G_VALUE_HOLDS (value, + TP_ARRAY_TYPE_CHANNEL_DETAILS_LIST)); - GPtrArray *channels = g_value_get_boxed (value); + GPtrArray *channels = g_value_get_boxed (value); - new_channels_cb (conn, channels, user_data, weak_obj); + new_channels_cb (TP_CONNECTION (conn), channels, user_data, weak_obj); } static void -create_roomlist_cb (TpConnection *conn, - const char *object_path, - GHashTable *props, - const GError *in_error, - gpointer user_data, - GObject *weak_obj) +create_roomlist_cb (TpConnection *conn, + const char *object_path, + GHashTable *props, + const GError *in_error, + gpointer user_data, + GObject *weak_obj) { - handle_error (in_error); - GError *error = NULL; + handle_error (in_error); + GError *error = NULL; - g_print (" > create_roomlist_cb (%s)\n", object_path); + g_print (" > create_roomlist_cb (%s)\n", object_path); - TpChannel *channel = tp_channel_new_from_properties (conn, - object_path, props, &error); - handle_error (error); + TpChannel *channel = tp_channel_new_from_properties (conn, + object_path, props, &error); + handle_error (error); - tp_asv_dump (props); + tp_asv_dump (props); - /* we didn't really want this channel anyway */ - tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL); - g_object_unref (channel); + /* we didn't really want this channel anyway */ + tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL); + g_object_unref (channel); } static void -list_properties_cb (TpProxy *channel, - const GPtrArray *available_properties, - const GError *in_error, - gpointer user_data, - GObject *weak_obj) +list_properties_cb (TpProxy *channel, + const GPtrArray *available_properties, + const GError *in_error, + gpointer user_data, + GObject *weak_obj) { - handle_error (in_error); + GArray *req; + int i; - g_print (" > list_properties_cb\n"); + handle_error (in_error); - GArray *req = g_array_sized_new (FALSE, FALSE, sizeof (guint), - available_properties->len); + g_print (" > list_properties_cb\n"); - /* @available_properties is a GPtrArray of GValueArray structs - * of signature (ussu) */ - int i; - for (i = 0; i < available_properties->len; i++) - { - GValueArray *prop = g_ptr_array_index (available_properties, i); + req = g_array_sized_new (FALSE, FALSE, sizeof (guint), + available_properties->len); - guint id, flags; - const char *name, *sig; + /* @available_properties is a GPtrArray of GValueArray structs + * of signature (ussu) */ + for (i = 0; i < available_properties->len; i++) + { + GValueArray *prop = g_ptr_array_index (available_properties, i); + guint id, flags; + const char *name, *sig; - tp_value_array_unpack (prop, 4, - &id, - &name, - &sig, - &flags); + tp_value_array_unpack (prop, 4, + &id, + &name, + &sig, + &flags); - g_print ("%u %s (%s) %x\n", id, name, sig, flags); + g_print ("%u %s (%s) %x\n", id, name, sig, flags); - /* pack the readable properties into a GArray */ - if (flags & TP_PROPERTY_FLAG_READ) - { - req = g_array_append_val (req, id); - } - } + /* pack the readable properties into a GArray */ + if (flags & TP_PROPERTY_FLAG_READ) + { + req = g_array_append_val (req, id); + } + } + + // FIXME: what happens to req ? } + static void -get_capabilities (TpConnection *connection, - const GPtrArray *capabilities, - const GError *error, - gpointer user_data, - GObject *weak_obj) +get_capabilities (TpConnection *connection, + const GPtrArray *capabilities, + const GError *in_error, + gpointer user_data, + GObject *weak_obj) { - if (error) - { - g_print ("ERROR: %s\n", error->message); - return; - } - - g_print ("get capabilities\n"); - - int i; - for (i = 0; i < capabilities->len; i++) - { - GValueArray *values = g_ptr_array_index (capabilities, i); - - g_print (" - %u :: %s\n", - g_value_get_uint (g_value_array_get_nth (values, 0)), - g_value_get_string (g_value_array_get_nth (values, 1))); - } + int i; + + if (in_error != NULL) + { + g_print ("ERROR: %s\n", in_error->message); + return; + } + + g_print ("get capabilities\n"); + + for (i = 0; i < capabilities->len; i++) + { + GValueArray *values = g_ptr_array_index (capabilities, i); + + g_print (" - %u :: %s\n", + g_value_get_uint (g_value_array_get_nth (values, 0)), + g_value_get_string (g_value_array_get_nth (values, 1))); + } } + static void get_contact_capabilities (TpConnection *connection, - GHashTable *capabilities, - const GError *error, - gpointer user_data, - GObject *weak_obj) + GHashTable *capabilities, + const GError *in_error, + gpointer user_data, + GObject *weak_obj) { - if (error) - { - g_print ("ERROR: %s\n", error->message); - return; - } - - g_print ("get contact capabilities\n"); - - GHashTableIter iter; - gpointer k; - GPtrArray *v; - g_hash_table_iter_init (&iter, capabilities); - while (g_hash_table_iter_next (&iter, &k, (gpointer) &v)) - { - int handle = GPOINTER_TO_INT (k); - int i; - - g_print ("h = %i\n", handle); - - for (i = 0; i < v->len; i++) - { - g_print (" - Requestable channel type %i\n", i + 1); - - GValueArray *values = g_ptr_array_index (v, i); - tp_asv_dump (g_value_get_boxed (g_value_array_get_nth (values, 0))); - } - } -} + GHashTableIter iter; + gpointer k; + GPtrArray *v; -static void -muc_channel_ready (TpChannel *channel, - const GError *in_error, - gpointer user_data) -{ - g_print ("MUC channel (%s) ready\n", - tp_channel_get_identifier (channel)); - - /* exciting things about MUC channels are stored as Telepathy - * Properties (not D-Bus properties). This interface is a little - * awkward. - * First we need to get a list of available properties */ - tp_cli_properties_interface_call_list_properties (channel, -1, - list_properties_cb, NULL, NULL, NULL); - - const TpIntSet *members = tp_channel_group_get_members (channel); - GArray *handles = tp_intset_to_array (members); - - tp_cli_connection_interface_contact_capabilities_call_get_contact_capabilities (tp_channel_borrow_connection (channel), - -1, handles, - get_contact_capabilities, - NULL, NULL, NULL); - - tp_cli_connection_interface_capabilities_call_get_capabilities (tp_channel_borrow_connection (channel), - -1, handles, - get_capabilities, - NULL, NULL, NULL); - - g_array_free (handles, TRUE); -} + if (in_error != NULL) + { + g_print ("ERROR: %s\n", in_error->message); + return; + } -static void -create_muc_cb (TpConnection *conn, - gboolean yours, - const char *object_path, - GHashTable *props, - const GError *in_error, - gpointer user_data, - GObject *weak_obj) -{ - handle_error (in_error); - GError *error = NULL; + g_print ("get contact capabilities\n"); - g_print (" > create_muc_cb (%s)\n", object_path); + g_hash_table_iter_init (&iter, capabilities); + while (g_hash_table_iter_next (&iter, &k, (gpointer) &v)) + { + int handle = GPOINTER_TO_INT (k); + int i; - TpChannel *channel = tp_channel_new_from_properties (conn, - object_path, props, &error); - handle_error (error); + g_print ("h = %i\n", handle); - tp_channel_call_when_ready (channel, muc_channel_ready, NULL); -} + for (i = 0; i < v->len; i++) + { + GValueArray *values; -static void -conn_ready (TpConnection *conn, - const GError *in_error, - gpointer user_data) -{ - GError *error = NULL; - - g_print (" > conn_ready\n"); - - handle_error (in_error); - - /* check if the Requests interface is available */ - if (tp_proxy_has_interface_by_id (conn, - TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS)) - { - /* request the current channels */ - tp_cli_dbus_properties_call_get (conn, -1, - TP_IFACE_CONNECTION_INTERFACE_REQUESTS, - "Channels", - get_channels_cb, - NULL, NULL, NULL); - - /* notify of all new channels */ - tp_cli_connection_interface_requests_connect_to_new_channels ( - conn, new_channels_cb, - NULL, NULL, NULL, &error); - handle_error (error); - - /* begin example.channel.roomlist.requestglib */ - /* request a RoomList channel */ - GHashTable *map = tp_asv_new ( - TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_ROOM_LIST, - TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_UINT, TP_HANDLE_TYPE_NONE, - /* we omit TargetHandle because it's anonymous */ - NULL); - - tp_cli_connection_interface_requests_call_create_channel ( - conn, -1, map, - create_roomlist_cb, - NULL, NULL, NULL); - - g_hash_table_destroy (map); - /* end example.channel.roomlist.requestglib */ - - /* make a connection to a MUC channel */ - map = tp_asv_new ( - TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT, - TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_UINT, TP_HANDLE_TYPE_ROOM, - TP_IFACE_CHANNEL ".TargetID", G_TYPE_STRING, "test@conference.collabora.co.uk", - NULL); - - tp_cli_connection_interface_requests_call_ensure_channel ( - conn, -1, map, - create_muc_cb, - NULL, NULL, NULL); - - g_hash_table_destroy (map); - } + g_print (" - Requestable channel type %i\n", i + 1); + + values = g_ptr_array_index (v, i); + tp_asv_dump (g_value_get_boxed (g_value_array_get_nth (values, 0))); + } + } } + static void -status_changed_cb (TpConnection *conn, - guint status, - guint reason, - gpointer user_data, - GObject *weak_object) +_muc_channel_ready (GObject *channel, + GAsyncResult *res, + gpointer user_data) { - if (status == TP_CONNECTION_STATUS_DISCONNECTED) - { - g_print ("Disconnected\n"); - g_main_loop_quit (loop); - } - else if (status == TP_CONNECTION_STATUS_CONNECTED) - { - g_print ("Connected\n"); - } + const TpIntSet *members; + GArray *handles; + GError *error = NULL; + + if (!tp_proxy_prepare_finish (channel, res, &error)) + { + handle_error (error); + } + + g_print ("MUC channel (%s) ready\n", + tp_channel_get_identifier (TP_CHANNEL (channel))); + + /* exciting things about MUC channels are stored as Telepathy + * Properties (not D-Bus properties). This interface is a little + * awkward. + * First we need to get a list of available properties */ + tp_cli_properties_interface_call_list_properties (channel, -1, + list_properties_cb, NULL, NULL, NULL); + + members = tp_channel_group_get_members (TP_CHANNEL (channel)); + handles = tp_intset_to_array (members); + + tp_cli_connection_interface_contact_capabilities_call_get_contact_capabilities ( + tp_channel_borrow_connection (TP_CHANNEL (channel)), + -1, handles, + get_contact_capabilities, + NULL, NULL, NULL); + + tp_cli_connection_interface_capabilities_call_get_capabilities ( + tp_channel_borrow_connection (TP_CHANNEL (channel)), + -1, handles, + get_capabilities, + NULL, NULL, NULL); + + g_array_free (handles, TRUE); } + static void -request_connection_cb (TpConnectionManager *cm, - const char *bus_name, - const char *object_path, - const GError *in_error, - gpointer user_data, - GObject *weak_object) +create_muc_cb (TpConnection *conn, + gboolean yours, + const char *object_path, + GHashTable *props, + const GError *in_error, + gpointer user_data, + GObject *weak_obj) { - GError *error = NULL; + GError *error = NULL; - if (in_error) g_error ("%s", in_error->message); + handle_error (in_error); - conn = tp_connection_new (bus_daemon, bus_name, object_path, &error); - if (error) g_error ("%s", error->message); + g_print (" > create_muc_cb (%s)\n", object_path); - tp_connection_call_when_ready (conn, conn_ready, NULL); + TpChannel *channel = tp_channel_new_from_properties (conn, + object_path, props, &error); + handle_error (error); - tp_cli_connection_connect_to_status_changed (conn, status_changed_cb, - NULL, NULL, NULL, &error); - handle_error (error); - - /* initiate the connection */ - tp_cli_connection_call_connect (conn, -1, NULL, NULL, NULL, NULL); + tp_proxy_prepare_async (channel, NULL, _muc_channel_ready, NULL); } + static void -cm_ready (TpConnectionManager *cm, - const GError *in_error, - gpointer user_data, - GObject *weak_obj) +_conn_ready (GObject *conn, + GAsyncResult *res, + gpointer user_data) { - char *username = (char *) user_data; - - g_print (" > cm_ready\n"); - - if (in_error) g_error ("%s", in_error->message); + GError *error = NULL; + + if (!tp_proxy_prepare_finish (conn, res, &error)) + { + handle_error (error); + } + + g_print (" > conn_ready\n"); + + /* check if the Requests interface is available */ + if (tp_proxy_has_interface_by_id (conn, + TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS)) + { + /* request the current channels */ + tp_cli_dbus_properties_call_get (conn, -1, + TP_IFACE_CONNECTION_INTERFACE_REQUESTS, + "Channels", + get_channels_cb, + NULL, NULL, NULL); + + /* notify of all new channels */ + tp_cli_connection_interface_requests_connect_to_new_channels ( + TP_CONNECTION (conn), new_channels_cb, + NULL, NULL, NULL, &error); + handle_error (error); + + /* begin example.channel.roomlist.requestglib */ + /* request a RoomList channel */ + GHashTable *map = tp_asv_new ( + TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_ROOM_LIST, + TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_NONE, + /* we omit TargetHandle because it's anonymous */ + NULL); + + tp_cli_connection_interface_requests_call_create_channel ( + TP_CONNECTION (conn), -1, map, + create_roomlist_cb, + NULL, NULL, NULL); + + g_hash_table_destroy (map); + /* end example.channel.roomlist.requestglib */ + + /* make a connection to a MUC channel */ + map = tp_asv_new ( + TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT, + TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM, + TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, "test@conference.collabora.co.uk", + NULL); + + tp_cli_connection_interface_requests_call_ensure_channel ( + TP_CONNECTION (conn), -1, map, + create_muc_cb, + NULL, NULL, NULL); + + g_hash_table_destroy (map); + } +} - const TpConnectionManagerProtocol *prot = tp_connection_manager_get_protocol (cm, "jabber"); - if (!prot) g_error ("Protocol is not supported"); - /* list the parameters */ - TpConnectionManagerParam *ptr; - for (ptr = prot->params; ptr->name; ptr++) - { - g_print (" - %s (%s)\n", ptr->name, ptr->dbus_signature); - } +static void +_account_ready (GObject *account, + GAsyncResult *res, + gpointer user_data) +{ + TpConnection *conn; + GError *error = NULL; - char *password = getpass ("Password: "); + if (!tp_proxy_prepare_finish (account, res, &error)) + { + handle_error (error); + } - /* request a new connection */ - GHashTable *parameters = tp_asv_new ( - "account", G_TYPE_STRING, username, - "password", G_TYPE_STRING, password, - "require-encryption", G_TYPE_BOOLEAN, TRUE, - "ignore-ssl-errors", G_TYPE_BOOLEAN, TRUE, - "fallback-conference-server", G_TYPE_STRING, "conference.collabora.co.uk", - NULL); + g_print (" > account ready\n"); - tp_cli_connection_manager_call_request_connection (cm, -1, - "jabber", - parameters, - request_connection_cb, - NULL, NULL, NULL); + /* get the connection */ + conn = tp_account_get_connection (TP_ACCOUNT (account)); - g_hash_table_destroy (parameters); + /* prepare the connection */ + tp_proxy_prepare_async (conn, NULL, _conn_ready, NULL); } -static void -interrupt_cb (int signal) -{ - g_print ("Interrupt\n"); - /* disconnect */ - tp_cli_connection_call_disconnect (conn, -1, NULL, NULL, NULL, NULL); -} int -main (int argc, char **argv) +main (int argc, + char **argv) { - GError *error = NULL; + TpDBusDaemon *dbus; + TpAccount *account; + char *account_path; + GError *error = NULL; + + g_type_init (); - g_type_init (); + if (argc != 2) + { + g_error ("Must provide an account!"); + } - if (argc != 2) - { - g_error ("Must provide username!"); - } - char *username = argv[1]; + /* create a main loop */ + loop = g_main_loop_new (NULL, FALSE); - /* create a main loop */ - loop = g_main_loop_new (NULL, FALSE); + /* acquire a connection to the D-Bus daemon */ + dbus = tp_dbus_daemon_dup (&error); + if (dbus == NULL) + { + handle_error (error); + } - /* acquire a connection to the D-Bus daemon */ - bus_daemon = tp_dbus_daemon_dup (&error); - if (bus_daemon == NULL) - { - g_error ("%s", error->message); - } + /* get the complete path of the account */ + account_path = g_strconcat (TP_ACCOUNT_OBJECT_PATH_BASE, argv[1], NULL); - /* we want to request the gabble CM */ - TpConnectionManager *cm = tp_connection_manager_new (bus_daemon, - "gabble", NULL, &error); - if (error) g_error ("%s", error->message); + /* get the account */ + account = tp_account_new (dbus, account_path, &error); + if (account == NULL) + { + handle_error (error); + } - tp_connection_manager_call_when_ready (cm, cm_ready, - username, NULL, NULL); + g_free (account_path); - /* set up a signal handler */ - struct sigaction sa = { 0 }; - sa.sa_handler = interrupt_cb; - sigaction (SIGINT, &sa, NULL); + /* prepare the account */ + tp_proxy_prepare_async (account, NULL, _account_ready, NULL); - g_main_loop_run (loop); + g_main_loop_run (loop); - g_object_unref (bus_daemon); + g_object_unref (dbus); + g_object_unref (account); - return 0; + return 0; } |