[telepathy-gabble/master] emit_search_result: use the xmpp_to_tp hash table instead of hard coding fields

Guillaume Desmottes guillaume.desmottes at collabora.co.uk
Wed Aug 26 09:22:06 PDT 2009


This had the side effect to to expose the "x-n-family" and "x-n-given"
info field in the search result which is probably what we want.
---
 src/search-channel.c               |   69 +++++++++++++++++++----------------
 tests/twisted/search/extended.py   |   28 ++++++++++-----
 tests/twisted/search/unextended.py |    4 ++-
 3 files changed, 59 insertions(+), 42 deletions(-)

diff --git a/src/search-channel.c b/src/search-channel.c
index 26ac460..c4b995e 100644
--- a/src/search-channel.c
+++ b/src/search-channel.c
@@ -561,16 +561,13 @@ emit_search_result (GabbleSearchChannel *chan,
                     GHashTable *info_map)
 {
   GPtrArray *info = g_ptr_array_new ();
-  gchar *jid, *first, *last, *nick, *email;
+  gchar *jid, *first = NULL, *last = NULL;
   TpHandle h = 0;
   GError *e = NULL;
+  gpointer key, value;
+  GHashTableIter iter;
 
   jid = ht_lookup_and_remove (info_map, "jid");
-  last = ht_lookup_and_remove (info_map, "last");
-  first = ht_lookup_and_remove (info_map, "first");
-  nick = ht_lookup_and_remove (info_map, "nick");
-  email = ht_lookup_and_remove (info_map, "email");
-
   if (jid == NULL)
     {
       DEBUG ("no jid; giving up");
@@ -593,6 +590,40 @@ emit_search_result (GabbleSearchChannel *chan,
     g_ptr_array_add (info, make_field ("x-telepathy-identifier", components));
   }
 
+  g_hash_table_iter_init (&iter, info_map);
+  while (g_hash_table_iter_next (&iter, &key, &value))
+    {
+      const gchar *tp_name;
+      gchar *components[] = { value, NULL };
+
+      tp_name = g_hash_table_lookup (xmpp_to_tp, key);
+      if (tp_name == NULL)
+        {
+          DEBUG ("<item> contained field we don't understand (%s); ignoring it:"
+              , (const gchar *) key);
+          continue;
+        }
+
+      if (value == NULL)
+        {
+          DEBUG ("field %s (%s) doesn't have a value; ignoring it",
+              (const gchar *) key, tp_name);
+          continue;
+        }
+
+      DEBUG ("found field %s (%s): %s", (const gchar *) key, tp_name,
+          (const gchar *) value);
+
+      g_ptr_array_add (info, make_field (tp_name, components));
+
+      if (!tp_strdiff (key, "last") ||
+          !tp_strdiff (key, "family"))
+        last = value;
+      else if (!tp_strdiff (key, "first") ||
+        !tp_strdiff (key, "given"))
+        first = value;
+    }
+
   /* Build 'n' field: Family Name, Given Name, Additional Names, Honorific
    * Prefixes, and Honorific Suffixes.
    */
@@ -609,32 +640,6 @@ emit_search_result (GabbleSearchChannel *chan,
       g_ptr_array_add (info, make_field ("n", components));
     }
 
-  /* Build 'nickname' field. */
-  if (nick != NULL)
-    {
-      gchar *components[] = { nick, NULL };
-      g_ptr_array_add (info, make_field ("nickname", components));
-    }
-
-  /* Build 'email' field */
-  if (email != NULL)
-    {
-      gchar *components[] = { email, NULL };
-      g_ptr_array_add (info, make_field ("email", components));
-    }
-
-  if (g_hash_table_size (info_map) > 0)
-    {
-      GHashTableIter iter;
-      gpointer key;
-
-      DEBUG ("<item> contained fields we don't understand; ignoring them:");
-
-      g_hash_table_iter_init (&iter, info_map);
-      while (g_hash_table_iter_next (&iter, &key, NULL))
-        DEBUG ("\t- %s has been ignored", (gchar *) key);
-    }
-
   gabble_svc_channel_type_contact_search_emit_search_result_received (chan, h, info);
 
 out:
diff --git a/tests/twisted/search/extended.py b/tests/twisted/search/extended.py
index 94ef250..d6ca434 100644
--- a/tests/twisted/search/extended.py
+++ b/tests/twisted/search/extended.py
@@ -21,11 +21,6 @@ server = 'jud.localhost'
 g_jid = 'guybrush.threepwood at lucasarts.example.com'
 f_jid = 'freddiet at pgwodehouse.example.com'
 
-g_results = { 'jid': g_jid, 'first': 'Guybrush', 'last': 'Threepwood',
-    'nick': 'Fancy Pants', 'x-gender': 'Male', 'email': g_jid }
-f_results = { 'jid': f_jid, 'first': 'Frederick', 'last': 'Threepwood',
-    'nick': 'Freddie', 'x-gender': 'Male', 'email': f_jid }
-
 def test(q, bus, conn, stream):
     conn.Connect()
     q.expect('dbus-signal', signal='StatusChanged',
@@ -116,6 +111,11 @@ def complete_search(q, bus, conn, requests, stream):
 
     terms = { 'x-n-family': 'Threepwood' }
 
+    g_results = { 'jid': g_jid, 'first': 'Guybrush', 'last': 'Threepwood',
+        'nick': 'Fancy Pants', 'x-gender': 'Male', 'email': g_jid }
+    f_results = { 'jid': f_jid, 'first': 'Frederick', 'last': 'Threepwood',
+        'nick': 'Freddie', 'x-gender': 'Male', 'email': f_jid }
+
     results = [g_results, f_results]
 
     search_fields, chan, c_search, c_props = do_one_search (q, bus, conn, requests, stream,
@@ -142,8 +142,11 @@ def complete_search(q, bus, conn, requests, stream):
         assert ("n", [], [r['last'], r['first'], "", "", ""])    in i, i_
         assert ("nickname", [], [r['nick']]) in i, i_
         assert ("email", [], [r['email']]) in i, i_
+        assert ("x-gender", [], [r['x-gender']]) in i, i_
+        assert ("x-n-family", [], [r['last']]) in i, i_
+        assert ("x-n-given", [], [r['first']]) in i, i_
 
-        assert len(i) == 4, i_
+        assert len(i) == 7, i_
 
     search_done(q, chan, c_search, c_props)
 
@@ -162,6 +165,11 @@ def complete_search2(q, bus, conn, requests, stream):
 
     terms = { 'x-n-family': 'Threepwood' }
 
+    g_results = { 'jid': g_jid, 'given': 'Guybrush', 'family': 'Threepwood',
+        'nickname': 'Fancy Pants', 'email': g_jid }
+    f_results = { 'jid': f_jid, 'given': 'Frederick', 'family': 'Threepwood',
+        'nickname': 'Freddie', 'email': f_jid }
+
     results = [g_results, f_results]
 
     search_fields, chan, c_search, c_props = do_one_search (q, bus, conn, requests, stream,
@@ -182,11 +190,13 @@ def complete_search2(q, bus, conn, requests, stream):
     for i, r in [(g_info, g_results), (f_info, f_results)]:
         i_ = pformat(unwrap(i))
         assert ("x-telepathy-identifier", [], [r['jid']]) in i, i_
-        assert ("n", [], [r['last'], r['first'], "", "", ""])    in i, i_
-        assert ("nickname", [], [r['nick']]) in i, i_
+        assert ("n", [], [r['family'], r['given'], "", "", ""])    in i, i_
+        assert ("nickname", [], [r['nickname']]) in i, i_
         assert ("email", [], [r['email']]) in i, i_
+        assert ("x-n-family", [], [r['family']]) in i, i_
+        assert ("x-n-given", [], [r['given']]) in i, i_
 
-        assert len(i) == 4, i_
+        assert len(i) == 6, i_
 
     search_done(q, chan, c_search, c_props)
 
diff --git a/tests/twisted/search/unextended.py b/tests/twisted/search/unextended.py
index ecfb1ff..93aab7e 100644
--- a/tests/twisted/search/unextended.py
+++ b/tests/twisted/search/unextended.py
@@ -82,8 +82,10 @@ def complete_search(q, bus, conn, requests, stream, server):
         assert ("n", [], [r[2], r[1], "", "", ""])    in i, i_
         assert ("nickname", [], [r[3]])               in i, i_
         assert ("email", [], [r[0]])                  in i, i_
+        assert ("x-n-family", [], [r[2]]) in i, i_
+        assert ("x-n-given", [], [r[1]]) in i, i_
 
-        assert len(i) == 4, i_
+        assert len(i) == 6, i_
 
     ssc = q.expect('dbus-signal', signal='SearchStateChanged')
     assert ssc.args[0] == cs.SEARCH_COMPLETED, ssc.args
-- 
1.5.6.5




More information about the telepathy-commits mailing list