pidgin: e6749b91: Provide alternative find_by_jid that doe...

darkrain42 at pidgin.im darkrain42 at pidgin.im
Tue Apr 28 17:22:12 EDT 2009


-----------------------------------------------------------------
Revision: e6749b9134fe75c4907ed91f0e5df9a1c6f16d51
Ancestor: 804c33ea1801eb94c86168d56164585ac1456ca9
Author: darkrain42 at pidgin.im
Date: 2009-04-27T23:07:01
Branch: im.pidgin.pidgin
URL: http://d.pidgin.im/viewmtn/revision/info/e6749b9134fe75c4907ed91f0e5df9a1c6f16d51

Modified files:
        libpurple/protocols/jabber/jingle/session.c

ChangeLog: 

Provide alternative find_by_jid that doesn't use g_hash_table_find

g_hash_table_find was added in 2.4.0

-------------- next part --------------
============================================================
--- libpurple/protocols/jabber/jingle/session.c	2f680423065e3af5e307c6921db1256c2522c874
+++ libpurple/protocols/jabber/jingle/session.c	7ac5d8d1e756b43bea072745ddee591530485a25
@@ -363,17 +363,18 @@ jingle_session_find_by_sid(JabberStream 
 			  g_hash_table_lookup(js->sessions, sid) : NULL;
 }
 
+#if GLIB_CHECK_VERSION(2,4,0)
 static gboolean find_by_jid_ghr(gpointer key,
 		gpointer value, gpointer user_data)
 {
 	JingleSession *session = (JingleSession *)value;
 	const gchar *jid = user_data;
-	gboolean use_bare = strchr(jid, '/') == NULL;
+	gboolean use_bare = g_utf8_strchr(jid, -1, '/') == NULL;
 	gchar *remote_jid = jingle_session_get_remote_jid(session);
 	gchar *cmp_jid = use_bare ? jabber_get_bare_jid(remote_jid)
 				  : g_strdup(remote_jid);
 	g_free(remote_jid);
-	if (!strcmp(jid, cmp_jid)) {
+	if (g_str_equal(jid, cmp_jid)) {
 		g_free(cmp_jid);
 		return TRUE;
 	}
@@ -382,12 +383,58 @@ static gboolean find_by_jid_ghr(gpointer
 	return FALSE;
 }
 
+#else /* GLIB_CHECK_VERSION 2.4.0 */
+
+/* Ugly code; g_hash_table_find version above is much nicer */
+struct session_find_jid
+{
+	const gchar *jid;
+	JingleSession *ret;
+	gboolean use_bare;
+};
+
+static void find_by_jid_ghr(gpointer key, gpointer value, gpointer user_data)
+{
+	JingleSession *session = (JingleSession *)value;
+	struct session_find_jid *data = user_data;
+	gchar *remote_jid;
+	gchar *cmp_jid;
+
+	if (data->ret != NULL)
+		return;
+
+	remote_jid = jingle_session_get_remote_jid(session);
+	cmp_jid = data->use_bare ? jabber_get_bare_jid(remote_jid)
+				: g_strdup(remote_jid);
+	g_free(remote_jid);
+
+	if (g_str_equal(data->jid, cmp_jid))
+		data->ret = session;
+
+	g_free(cmp_jid);
+}
+#endif /* GLIB_CHECK_VERSION 2.4.0 */
+
 JingleSession *
 jingle_session_find_by_jid(JabberStream *js, const gchar *jid)
 {
+#if GLIB_CHECK_VERSION(2,4,0)
 	return js->sessions != NULL ?
 			g_hash_table_find(js->sessions,
 			find_by_jid_ghr, (gpointer)jid) : NULL; 
+#else
+	struct session_find_jid data;
+
+	if (js->sessions == NULL)
+		return NULL;
+
+	data.jid = jid;
+	data.ret = NULL;
+	data.use_bare = g_utf8_strchr(jid, -1, '/') == NULL;
+
+	g_hash_table_foreach(js->sessions, find_by_jid_ghr, &data);
+	return data.ret;
+#endif
 }
 
 static xmlnode *


More information about the Commits mailing list