Add disco_cancel_request
authorMyhailo Danylenko <isbear@ukrpost.net>
Sun, 31 Jan 2010 20:44:05 +0200
changeset 25 05469ec9c2e0
parent 24 220a7d7b8be0
child 26 91987ea19f24
Add disco_cancel_request Though, it will work consistently only with presence of lm_connection_unregister_reply_handler.
disco.c
disco.h
--- a/disco.c	Wed Jan 20 21:43:22 2010 +0200
+++ b/disco.c	Sun Jan 31 20:44:05 2010 +0200
@@ -275,12 +275,12 @@
 	return;
 }
 
-void disco_info_request (const gchar *jid, const gchar *dnode, disco_info_handler_t handler, gpointer userdata, GDestroyNotify notify)
+gpointer disco_info_request (const gchar *jid, const gchar *dnode, disco_info_handler_t handler, gpointer userdata, GDestroyNotify notify)
 {
 	if (!handler || !xmpp_is_online ()) {
 		if (notify)
 			notify (userdata);
-		return;
+		return NULL;
 	}
 
 	if (0 && !dnode) { // FIXME: no way to get identity(ies) from caps
@@ -299,15 +299,16 @@
 				handler (identities, features, userdata);
 				if (notify)
 					notify (userdata);
-				return;
+				return NULL;
 			}
 		}
 	}
 
 	{ // send request
-		LmMessage     *request;
-		LmMessageNode *node;
-		GError        *error   = NULL;
+		LmMessage        *request;
+		LmMessageNode    *node;
+		LmMessageHandler *lhandler;
+		GError           *error    = NULL;
 
 		request = lm_message_new_with_sub_type (jid, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_GET);
 		node    = lm_message_get_node (request);
@@ -317,8 +318,9 @@
 			lm_message_node_set_attribute (node, "node", dnode);
 
 		{
-			disco_info_reply_handler_t *cb       = g_new (disco_info_reply_handler_t, 1);
-			LmMessageHandler           *lhandler = lm_message_handler_new (disco_info_reply_handler, cb, disco_info_reply_handler_destroy_notify);
+			disco_info_reply_handler_t *cb = g_new (disco_info_reply_handler_t, 1);
+			
+			lhandler = lm_message_handler_new (disco_info_reply_handler, cb, disco_info_reply_handler_destroy_notify);
 
 			cb -> handler = handler;
 			cb -> data    = userdata;
@@ -329,6 +331,7 @@
 			lm_connection_send_with_reply (lconnection, request, lhandler, &error);
 
 			if (error) {
+				// XXX destroy handler and return NULL?
 				scr_LogPrint (LPRINT_DEBUG, "disco: Error sending disco request: %s.", error -> message);
 				g_error_free (error);
 			}
@@ -337,23 +340,24 @@
 		}
 
 		lm_message_unref (request);
+
+		return lhandler;
 	}
-
-	return;
 }
 
-void disco_items_request (const gchar *jid, const gchar *dnode, disco_items_handler_t handler, gpointer userdata, GDestroyNotify notify)
+gpointer disco_items_request (const gchar *jid, const gchar *dnode, disco_items_handler_t handler, gpointer userdata, GDestroyNotify notify)
 {
 	if (!handler || !xmpp_is_online ()) {
 		if (notify)
 			notify (userdata);
-		return;
+		return NULL;
 	}
 
 	{ // send request
-		LmMessage     *request;
-		LmMessageNode *node;
-		GError        *error   = NULL;
+		LmMessage        *request;
+		LmMessageNode    *node;
+		GError           *error    = NULL;
+		LmMessageHandler *lhandler;
 
 		request = lm_message_new_with_sub_type (jid, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_GET);
 		node    = lm_message_get_node (request);
@@ -364,7 +368,8 @@
 
 		{
 			disco_items_reply_handler_t *cb       = g_new (disco_items_reply_handler_t, 1);
-			LmMessageHandler            *lhandler = lm_message_handler_new (disco_items_reply_handler, cb, disco_items_reply_handler_destroy_notify);
+			
+			lhandler = lm_message_handler_new (disco_items_reply_handler, cb, disco_items_reply_handler_destroy_notify);
 
 			cb -> handler = handler;
 			cb -> data    = userdata;
@@ -375,6 +380,7 @@
 			lm_connection_send_with_reply (lconnection, request, lhandler, &error);
 
 			if (error) {
+				// XXX destroy handler and return NULL?
 				scr_LogPrint (LPRINT_DEBUG, "disco: Error sending disco request: %s.", error -> message);
 				g_error_free (error);
 			}
@@ -383,6 +389,26 @@
 		}
 
 		lm_message_unref (request);
+
+		return lhandler;
+	}
+}
+
+void disco_cancel_request (gpointer id)
+{
+	GSList *hel;
+
+	for (hel = reply_handlers; hel; hel = hel -> next) {
+		if (hel -> data == id) {
+			LmMessageHandler *handler = id;
+			reply_handlers = g_slist_remove (reply_handlers, handler);
+			lm_message_handler_invalidate (handler);
+#ifdef HAVE_LM_CONNECTION_UNREGISTER_REPLY_HANDLER
+			if (lconnection)
+				lm_connection_unregister_reply_handler (lconnection, handler);
+#endif
+			return;
+		}
 	}
 
 	return;
--- a/disco.h	Wed Jan 20 21:43:22 2010 +0200
+++ b/disco.h	Sun Jan 31 20:44:05 2010 +0200
@@ -41,8 +41,9 @@
 typedef void (*disco_info_handler_t) (GSList *identities, GSList *features, gpointer userdata);
 typedef void (*disco_items_handler_t) (GSList *items, gpointer userdata);
 
-void disco_info_request  (const gchar *jid, const gchar *node, disco_info_handler_t  handler, gpointer userdata, GDestroyNotify notify);
-void disco_items_request (const gchar *jid, const gchar *node, disco_items_handler_t handler, gpointer userdata, GDestroyNotify notify);
+gpointer disco_info_request   (const gchar *jid, const gchar *node, disco_info_handler_t  handler, gpointer userdata, GDestroyNotify notify);
+gpointer disco_items_request  (const gchar *jid, const gchar *node, disco_items_handler_t handler, gpointer userdata, GDestroyNotify notify);
+void     disco_cancel_request (gpointer id);
 
 #endif