Free resources
authorMyhailo Danylenko <isbear@ukrpost.net>
Sun, 01 Nov 2009 05:35:01 +0200
changeset 3 c409300aa352
parent 2 59145167eea0
child 4 b18eee368baa
Free resources * register handlers and free on exit * forgot to unref message and handler * fix for empty feature/identity list showing
disco.c
--- a/disco.c	Sat Oct 31 14:08:49 2009 +0200
+++ b/disco.c	Sun Nov 01 05:35:01 2009 +0200
@@ -33,10 +33,15 @@
 #include "screen.h"
 #include "hbuf.h"
 
+static GSList *disco_handlers = NULL;
+static guint   disco_cid      = 0;
+
 static LmHandlerResult disco_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer udata)
 {
 	int info_request = (int) udata;
 
+	disco_handlers = g_slist_remove (disco_handlers, handler);
+
 	switch (lm_message_get_sub_type (message)) {
 	case LM_MESSAGE_SUB_TYPE_RESULT:
 
@@ -84,9 +89,9 @@
 						}
 					}
 
-					if (identities->str)
+					if (identities->len)
 						g_string_append_printf (info, "\n  Identities:%s", identities->str);
-					if (features->str)
+					if (features->len)
 						g_string_append_printf (info, "\n  Features:%s", features->str);
 
 					g_string_free (identities, TRUE);
@@ -206,22 +211,50 @@
 
 		lm_connection_send_with_reply (lconnection, request, handler, NULL);
 
+		lm_message_handler_unref (handler);
+		lm_message_unref (request);
+		g_free (dnode);
 		g_free (to);
-		g_free (dnode);
 	}
 }
 
 const gchar *g_module_check_init(GModule *module)
 {
-	// TODO completion
+	disco_handlers = NULL;
 
-	cmd_add ("disco", "", 0, COMPL_JID, do_disco, NULL);
+	// completion
+	disco_cid = compl_new_category ();
+	if (disco_cid) {
+			compl_add_category_word (disco_cid, "info");
+			compl_add_category_word (disco_cid, "items");
+	}
+
+	// command
+	cmd_add ("disco", "", disco_cid, COMPL_JID, do_disco, NULL);
+
 	return NULL;
 }
 
 void g_module_unload(GModule *module)
 {
+	GSList *hel;
+
+	// let's hope, that after invalidation, lm will remove and free unreffed by us handler
+	for (hel = disco_handlers; hel; hel = hel->next) {
+		LmMessageHandler *handler = (LmMessageHandler *) hel->data;
+		lm_message_handler_invalidate (handler);
+	}
+
+	// free list
+	g_slist_free (disco_handlers);
+	disco_handlers = NULL;
+
+	// command
 	cmd_del ("disco");
+
+	// completion
+	if (disco_cid)
+			compl_del_category (disco_cid);
 }
 
 /* vim: se ts=4: */