Use new commands interface
authorMyhailo Danylenko <isbear@ukrpost.net>
Wed, 07 Apr 2010 22:02:05 +0300
changeset 10 b0511d0cd16e
parent 9 da83916efe80
child 11 32564a4369f2
Use new commands interface * Use id to delete commands * Add /templatecmd to safe commands list
config.h.in
templatecmd.c
--- a/config.h.in	Sun Apr 04 16:47:00 2010 +0300
+++ b/config.h.in	Wed Apr 07 22:02:05 2010 +0300
@@ -2,6 +2,15 @@
 #ifndef LOCAL_CONFIG_H
 #define LOCAL_CONFIG_H
 
+#include <mcabber/config.h>
+
+#if MCABBER_BRANCH_EXPERIMENTAL && MCABBER_API_VERSION >= 23
+#  define HAVE_CMD_ID
+#  if MCABBER_API_VERSION >= 24
+#    define HAVE_CMD_SAFE
+#  endif
+#endif
+
 #define PROJECT_VERSION ( "${PROJECT_VERSION}" )
 
 #endif
--- a/templatecmd.c	Sun Apr 04 16:47:00 2010 +0300
+++ b/templatecmd.c	Wed Apr 07 22:02:05 2010 +0300
@@ -35,7 +35,13 @@
 
 static module_info_t info_templatecmd_experimental = {
 	.branch      = "experimental",
-	.api         = 15,
+#ifndef HAVE_CMD_ID
+	.api         = 20,
+#elif defined HAVE_CMD_SAFE
+	.api         = 24,
+#else
+	.api         = 23,
+#endif
 	.version     = PROJECT_VERSION,
 	.description = DESCRIPTION,
 	.requires    = NULL,
@@ -57,11 +63,21 @@
 
 typedef struct {
 	gchar    *name;
+#ifdef HAVE_CMD_ID
+	gpointer  id;
+#endif
 	gchar    *template;
 	int       maxarg;
 	gboolean  stararg;
 } tcmd_t;
 
+#ifdef HAVE_CMD_ID
+static gpointer tcmd_cmid = NULL;
+#ifdef HAVE_CMD_SAFE
+static gboolean tcmd_set_safe = FALSE;
+#endif
+#endif
+
 static GSList *template_commands = NULL;
 
 static void tcmd_callback (char *arg, gpointer udata)
@@ -167,7 +183,12 @@
 
 			if (!*astart) { // delete tcmd
 				if (template_command) {
+#ifndef HAVE_CMD_ID
 					cmd_del (template_command -> name);
+#else
+					if (template_command -> id)
+						cmd_del (template_command -> id);
+#endif
 					template_commands = g_slist_remove (template_commands, template_command);
 					g_free (template_command -> name);
 					g_free (template_command -> template);
@@ -210,7 +231,11 @@
 
 			template_commands = g_slist_append (template_commands, template_command);
 
+#ifndef HAVE_CMD_ID
 			cmd_add (template_command -> name, "", 0, 0, (void (*) (char *arg)) tcmd_callback, template_command);
+#else
+			template_command -> id = cmd_add (template_command -> name, "", 0, 0, (void (*) (char *arg)) tcmd_callback, template_command);
+#endif
 		}
 	}
 }
@@ -218,18 +243,39 @@
 
 static void tcmd_init (void)
 {
+#ifndef HAVE_CMD_ID
 	cmd_add ("templatecmd", "", COMPL_CMD, COMPL_CMD, do_templatecmd, NULL);
+#else
+	tcmd_cmid = cmd_add ("templatecmd", "", COMPL_CMD, COMPL_CMD, do_templatecmd, NULL);
+#ifdef HAVE_CMD_SAFE
+	tcmd_set_safe = cmd_set_safe ("templatecmd", TRUE);
+#endif
+#endif
 }
 
 static void tcmd_uninit (void)
 {
 	GSList *tel;
 
+#ifndef HAVE_CMD_ID
 	cmd_del ("templatecmd");
+#else
+	if (tcmd_cmid)
+		cmd_del (tcmd_cmid);
+#ifdef HAVE_CMD_SAFE
+	if (tcmd_set_safe)
+		cmd_set_safe ("templatecmd", FALSE);
+#endif
+#endif
 
 	for (tel = template_commands; tel; tel = tel -> next) {
 		tcmd_t *template_command = (tcmd_t *) tel -> data;
+#ifndef HAVE_CMD_ID
 		cmd_del (template_command -> name);
+#else
+		if (template_command -> id)
+			cmd_del (template_command -> id);
+#endif
 		g_free (template_command -> name);
 		g_free (template_command -> template);
 		g_free (template_command);