[Telepathy-commits] [telepathy-gabble/master] implement org.laptop.Telepathy.Buddy.SearchByProperties()

Guillaume Desmottes guillaume.desmottes at collabora.co.uk
Fri Sep 26 10:02:06 PDT 2008


20080521094715-7fe3f-1dcbfc25e4baa22225aa648899374bbe787f0c6c.gz
---
 src/conn-olpc.c                         |   74 ++++++++++++++++++++++++++++++-
 tests/twisted/olpc/olpc-buddy-search.py |   71 +++++++++++++++++++++++++++--
 2 files changed, 139 insertions(+), 6 deletions(-)

diff --git a/src/conn-olpc.c b/src/conn-olpc.c
index 42a1b3c..f8311b2 100644
--- a/src/conn-olpc.c
+++ b/src/conn-olpc.c
@@ -3154,6 +3154,78 @@ olpc_buddy_request_random (GabbleSvcOLPCBuddy *iface,
   lm_message_unref (query);
 }
 
+static void
+olpc_buddy_search_by_properties (GabbleSvcOLPCBuddy *iface,
+                                 GHashTable *properties,
+                                 DBusGMethodInvocation *context)
+{
+  GabbleConnection *conn = GABBLE_CONNECTION (iface);
+  LmMessage *query;
+  LmMessageNode *properties_node;
+  gchar *id_str;
+  gchar *object_path;
+  guint id;
+  GabbleOlpcBuddyView *view;
+
+  if (!check_gadget_buddy (conn, context))
+    return;
+
+  view = create_buddy_view (conn);
+  if (view == NULL)
+    {
+      GError error = { TP_ERRORS, TP_ERROR_NETWORK_ERROR,
+        "can't create view" };
+
+      DEBUG ("%s", error.message);
+      dbus_g_method_return_error (context, &error);
+      return;
+    }
+
+  g_object_get (view,
+      "id", &id,
+      "object-path", &object_path,
+      NULL);
+
+  id_str = g_strdup_printf ("%u", id);
+
+  query = lm_message_build_with_sub_type (conn->olpc_gadget_buddy,
+      LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_GET,
+      '(', "query", "",
+          '@', "xmlns", NS_OLPC_BUDDY,
+          '@', "id", id_str,
+          '(', "buddy", "",
+            '(', "properties", "",
+              '*', &properties_node,
+              '@', "xmlns", NS_OLPC_BUDDY_PROPS,
+            ')',
+          ')',
+      ')',
+      NULL);
+
+  g_free (id_str);
+
+  lm_message_node_add_children_from_properties (properties_node, properties,
+      "property");
+
+  if (!_gabble_connection_send_with_reply (conn, query,
+        buddy_query_result_cb, G_OBJECT (view), NULL, NULL))
+    {
+      GError error = { TP_ERRORS, TP_ERROR_NETWORK_ERROR,
+        "Failed to send buddy search query to server" };
+
+      DEBUG ("%s", error.message);
+      dbus_g_method_return_error (context, &error);
+      lm_message_unref (query);
+      g_free (object_path);
+      return;
+    }
+
+  gabble_svc_olpc_buddy_return_from_search_by_properties (context, object_path);
+
+  g_free (object_path);
+  lm_message_unref (query);
+}
+
 void
 olpc_buddy_iface_init (gpointer g_iface,
                        gpointer iface_data)
@@ -3163,6 +3235,6 @@ olpc_buddy_iface_init (gpointer g_iface,
 #define IMPLEMENT(x) gabble_svc_olpc_buddy_implement_##x (\
     klass, olpc_buddy_##x)
   IMPLEMENT(request_random);
-  /* TODO: implement SearchByProperties */
+  IMPLEMENT(search_by_properties);
 #undef IMPLEMENT
 }
diff --git a/tests/twisted/olpc/olpc-buddy-search.py b/tests/twisted/olpc/olpc-buddy-search.py
index fd47e79..cdc0462 100644
--- a/tests/twisted/olpc/olpc-buddy-search.py
+++ b/tests/twisted/olpc/olpc-buddy-search.py
@@ -111,6 +111,9 @@ def test(q, bus, conn, stream):
 
     event = q.expect('stream-iq', to='gadget.localhost',
             query_ns=NS_OLPC_BUDDY)
+    query = event.stanza.firstChildElement()
+    assert query.name == 'query'
+    assert query['id'] == '0'
     random = xpath.queryForNodes('/iq/query/random', event.stanza)
     assert len(random) == 1
     assert random[0]['max'] == '3'
@@ -131,8 +134,8 @@ def test(q, bus, conn, stream):
 
     event = q.expect('dbus-return', method='RequestRandom')
     view_path = event.value[0]
-    view = bus.get_object(conn.bus_name, view_path)
-    view_iface = dbus.Interface(view, 'org.laptop.Telepathy.BuddyView')
+    view0 = bus.get_object(conn.bus_name, view_path)
+    view0_iface = dbus.Interface(view0, 'org.laptop.Telepathy.BuddyView')
 
     event = q.expect('dbus-signal', signal='PropertiesChanged')
     handle, props = event.args
@@ -146,13 +149,71 @@ def test(q, bus, conn, stream):
     handle = added[0]
     assert conn.InspectHandles(1, [handle])[0] == 'bob at localhost'
 
-    call_async(q, view_iface, 'Close')
-    event = q.expect('stream-message', to='gadget.localhost')
+    # buddy search
+    props = {'color': '#AABBCC,#001122'}
+    call_async(q, buddy_iface, 'SearchByProperties', props)
+
+    iq_event, return_event = q.expect_many(
+        EventPattern('stream-iq', to='gadget.localhost', query_ns=NS_OLPC_BUDDY),
+        EventPattern('dbus-return', method='SearchByProperties'))
+
+    properties = xpath.queryForNodes('/iq/query/buddy/properties/property', iq_event.stanza)
+    query = iq_event.stanza.firstChildElement()
+    assert query.name == 'query'
+    assert query['id'] == '1'
+    assert len(properties) == 1
+    property = properties[0]
+    assert property['type'] == 'str'
+    assert property['name'] == 'color'
+    assert property.children == ['#AABBCC,#001122']
+
+    # reply to request
+    reply = make_result_iq(stream, iq_event.stanza)
+    reply['from'] = 'gadget.localhost'
+    reply['to'] = 'alice at localhost'
+    query = xpath.queryForNodes('/iq/query', reply)[0]
+    buddy = query.addElement((None, "buddy"))
+    buddy['jid'] = 'charles at localhost'
+    properties = buddy.addElement((NS_OLPC_BUDDY_PROPS, "properties"))
+    property = properties.addElement((None, "property"))
+    property['type'] = 'str'
+    property['name'] = 'color'
+    property.addContent('#AABBCC,#001122')
+    stream.send(reply)
+
+    view_path = return_event.value[0]
+    view1 = bus.get_object(conn.bus_name, view_path)
+    view1_iface = dbus.Interface(view1, 'org.laptop.Telepathy.BuddyView')
+
+    event = q.expect('dbus-signal', signal='PropertiesChanged')
+    handle, props = event.args
+    assert conn.InspectHandles(1, [handle])[0] == 'charles at localhost'
+    assert props == {'color': '#AABBCC,#001122'}
+
+    event = q.expect('dbus-signal', signal='MembersChanged')
+    msg, added, removed, lp, rp, actor, reason = event.args
+    assert (removed, lp, rp) == ([], [], [])
+    assert len(added) == 1
+    handle = added[0]
+    assert conn.InspectHandles(1, [handle])[0] == 'charles at localhost'
+
+    # close view 0
+    call_async(q, view0_iface, 'Close')
+    event, _ = q.expect_many(
+        EventPattern('stream-message', to='gadget.localhost'),
+        EventPattern('dbus-return', method='Close'))
     close = xpath.queryForNodes('/message/close', event.stanza)
     assert len(close) == 1
     assert close[0]['id'] == '0'
 
-    event = q.expect('dbus-return', method='Close')
+    # close view 1
+    call_async(q, view1_iface, 'Close')
+    event, _ = q.expect_many(
+        EventPattern('stream-message', to='gadget.localhost'),
+        EventPattern('dbus-return', method='Close'))
+    close = xpath.queryForNodes('/message/close', event.stanza)
+    assert len(close) == 1
+    assert close[0]['id'] == '1'
 
 if __name__ == '__main__':
     exec_test(test)
-- 
1.5.6.5




More information about the Telepathy-commits mailing list