From 92a8194e918876567b2dc46a4098879e5fe46654 Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Wed, 20 Jul 2011 18:56:23 +0100 Subject: [PATCH] Contacts: support Aliasing --- src/idle-connection.c | 31 +++++++++++++++++++++++++++++++ tests/twisted/contacts.py | 23 +++++++++++++++++++++++ 2 files changed, 54 insertions(+), 0 deletions(-) diff --git a/src/idle-connection.c b/src/idle-connection.c index 902d376..c2813a5 100644 --- a/src/idle-connection.c +++ b/src/idle-connection.c @@ -219,6 +219,10 @@ static gboolean idle_connection_hton(IdleConnection *obj, const gchar *input, gc static void idle_connection_ntoh(IdleConnection *obj, const gchar *input, gchar **output); static void _send_with_priority(IdleConnection *conn, const gchar *msg, guint priority); +static void conn_aliasing_fill_contact_attributes ( + GObject *obj, + const GArray *contacts, + GHashTable *attributes_hash); static void idle_connection_init(IdleConnection *obj) { IdleConnectionPrivate *priv = IDLE_CONNECTION_GET_PRIVATE(obj); @@ -237,6 +241,9 @@ idle_connection_constructed (GObject *object) self->parser = g_object_new (IDLE_TYPE_PARSER, "connection", self, NULL); idle_contact_info_init (self); + tp_contacts_mixin_add_contact_attributes_iface (object, + TP_IFACE_CONNECTION_INTERFACE_ALIASING, + conn_aliasing_fill_contact_attributes); } static void idle_connection_set_property(GObject *obj, guint prop_id, const GValue *value, GParamSpec *pspec) { @@ -1191,6 +1198,30 @@ gimme_an_alias ( } static void +conn_aliasing_fill_contact_attributes ( + GObject *obj, + const GArray *contacts, + GHashTable *attributes_hash) +{ + IdleConnection *self = IDLE_CONNECTION (obj); + TpHandleRepoIface *repo = tp_base_connection_get_handles ( + TP_BASE_CONNECTION (self), TP_HANDLE_TYPE_CONTACT); + guint i; + + for (i = 0; i < contacts->len; i++) + { + TpHandle handle = g_array_index (contacts, TpHandle, i); + const gchar *alias = gimme_an_alias (repo, handle); + + g_assert (alias != NULL); + + tp_contacts_mixin_set_contact_attribute (attributes_hash, + handle, TP_IFACE_CONNECTION_INTERFACE_ALIASING"/alias", + tp_g_value_slice_new_string (alias)); + } +} + +static void idle_connection_get_aliases ( TpSvcConnectionInterfaceAliasing *iface, const GArray *handles, diff --git a/tests/twisted/contacts.py b/tests/twisted/contacts.py index 8c6d404..96a3ec3 100644 --- a/tests/twisted/contacts.py +++ b/tests/twisted/contacts.py @@ -13,6 +13,10 @@ def test(q, bus, conn, stream): interfaces = conn.Properties.Get(cs.CONN, "Interfaces") assertContains(cs.CONN_IFACE_CONTACTS, interfaces) + attr_ifaces = conn.Properties.Get(cs.CONN_IFACE_CONTACTS, + "ContactAttributeInterfaces") + assertContains(cs.CONN_IFACE_ALIASING, attr_ifaces) + brillana, miriam = conn.RequestHandles(cs.HT_CONTACT, ["brillana", "miriam"]) @@ -23,6 +27,25 @@ def test(q, bus, conn, stream): assertContains(cs.CONN + "/contact-id", brillana_attrs) assertEquals("brillana", brillana_attrs[cs.CONN + "/contact-id"]) + # Test grabbing some aliases! Neither contact is known to have any + # particular capitalization so they should be lowercase. + attrs = conn.Contacts.GetContactAttributes([brillana, miriam], + [cs.CONN_IFACE_ALIASING], True) + assertContains(cs.CONN_IFACE_ALIASING + "/alias", attrs[brillana]) + assertEquals("brillana", attrs[brillana][cs.CONN_IFACE_ALIASING + "/alias"]) + assertEquals("miriam", attrs[miriam][cs.CONN_IFACE_ALIASING + "/alias"]) + + # Brillana sends us a message! We learn that she's basically 14 and uses + # stupid capitalization on the internet. + bRiL = 'bRiLlAnA' + stream.sendMessage('PRIVMSG', stream.nick, ':hai!!!', prefix=bRiL) + + # We don't actually care about the message; the important bit is that her + # alias changes. + q.expect('dbus-signal', signal='AliasesChanged', args=[[(brillana, bRiL)]]) + attrs = conn.Contacts.GetContactAttributes([brillana], + [cs.CONN_IFACE_ALIASING], True) + assertEquals(bRiL, attrs[brillana][cs.CONN_IFACE_ALIASING + "/alias"]) if __name__ == '__main__': exec_test(test) -- 1.7.5.4