marking.c
changeset 0 aad5201e3413
child 1 c8473c43ce72
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/marking.c	Wed Nov 11 11:07:19 2009 +0200
@@ -0,0 +1,193 @@
+
+/* Copyright 2009 Myhailo Danylenko
+
+This file is part of @PROJECT@
+
+@PROJECT@ is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>. */
+
+#include <glib.h>
+#include <gmodule.h>
+#include <string.h>
+
+#include "hbuf.h"
+#include "screen.h"
+#include "commands.h"
+#include "compl.h"
+#include "utils.h"
+#include "logprint.h"
+
+static GSList *marked_jids = NULL;
+
+// mark set jid
+// mark clear jid
+// mark toggle jid
+// marked clear
+// marked do cmd
+static void do_mark (char *arg)
+{
+	char     **args   = split_arg (arg, 2, 1);
+	char      *cmd    = NULL;
+	char      *jid    = NULL;
+	gboolean   jfree  = FALSE;
+	int        marked = -1;
+
+	if (args[0]) {
+		cmd = args[0];
+
+		if (args[1])
+			jid = jidtodisp (args[1]);
+	} else
+		cmd = "set";
+	
+	if (!strcmp (cmd, "set")) {
+
+		if (!g_slist_find_custom (marked_jids, jid ? jid : CURRENT_JID, (GCompareFunc) g_strcmp0)) {
+			marked_jids = g_slist_append (marked_jids, jid ? jid : g_strdup (CURRENT_JID));
+			marked = 1;
+		} else
+			jfree = TRUE;
+
+	} else if (!strcmp (cmd, "clear")) {
+
+		GSList *mel = g_slist_find_custom (marked_jids, jid ? jid : CURRENT_JID, (GCompareFunc) g_strcmp0);
+
+		if (mel) {
+			g_free (mel->data);
+			marked_jids = g_slist_delete_link (marked_jids, mel);
+			marked = 0;
+		}
+
+		jfree = TRUE;
+
+	} else if (!strcmp (cmd, "toggle")) {
+
+		GSList *mel = g_slist_find_custom (marked_jids, jid ? jid : CURRENT_JID, (GCompareFunc) g_strcmp0);
+
+		if (mel) {
+			g_free (mel->data);
+			marked_jids = g_slist_delete_link (marked_jids, mel);
+			marked = 0;
+			jfree  = TRUE;
+		} else {
+			marked_jids = g_slist_append (marked_jids, jid ? jid : g_strdup (CURRENT_JID));
+			marked = 1;
+		}
+	}
+
+	if (marked == 1)
+		scr_WriteIncomingMessage (jid ? jid : CURRENT_JID, "Marked", 0, HBB_PREFIX_INFO|HBB_PREFIX_NOFLAG, 0);
+	else if (marked == 0)
+		scr_WriteIncomingMessage (jid ? jid : CURRENT_JID, "Mark cleared", 0, HBB_PREFIX_INFO|HBB_PREFIX_NOFLAG, 0);
+
+	if (jfree && jid)
+		g_free (jid);
+
+	free_arg_lst (args);
+}
+
+void do_marked (char *arg)
+{
+	char **args = split_arg (arg, 2, 1);
+	char  *cmd = args[0];
+
+	if (!cmd)
+		cmd = "list";
+
+	if (!strcmp (cmd, "list")) {
+
+		GSList *mel;
+
+		scr_LogPrint (LPRINT_NORMAL, "Marked jids:");
+		for (mel = marked_jids; mel; mel = mel->next) {
+			char *jid = (char *) mel->data;
+			scr_LogPrint (LPRINT_NORMAL, " * %s", jid);
+		}
+
+	} else if (!strcmp (cmd, "clear")) {
+
+		GSList *mel;
+
+		for (mel = marked_jids; mel; mel = mel->next)
+			g_free (mel->data);
+
+		g_slist_free (marked_jids);
+		marked_jids = NULL;
+
+	} else if (!strcmp (cmd, "do")) {
+		char *format = args[1];
+
+		if (!format)
+			scr_LogPrint (LPRINT_NORMAL, "You must specify action to do with marked jids");
+		else {
+			char     *pos   = format;
+			gboolean  found = FALSE;
+			gboolean  error = FALSE;
+
+			for (pos = strchr (pos, '%'); pos; pos = strchr (pos, '%')) {
+				if (*(pos + 1) == 's')
+					found = TRUE;
+				else if (*(pos + 1) == '%')
+					++pos;
+				else {
+					scr_LogPrint (LPRINT_NORMAL, "Action string must not contain any other %%-sequesnces!");
+					error = TRUE;
+					break;
+				}
+
+				++pos;
+			}
+
+			if (!found)
+				scr_LogPrint (LPRINT_NORMAL, "Action string must contain one %%s!");
+			else if (!error) {
+				GSList *mel;
+
+				for (mel = marked_jids; mel; mel = mel->next) {
+					char *jid     = (char *) mel->data;
+					char *command = g_strdup_printf (format, jid);
+					process_command (command, TRUE);
+					g_free (command);
+				}
+			}
+		}
+	}
+
+	free_arg_lst (args);
+}
+
+gchar *g_module_check_init (GModule *module)
+{
+	cmd_add ("mark", "", 0, COMPL_JID, do_mark, NULL);
+	cmd_add ("marked", "", 0, COMPL_CMD, do_marked, NULL);
+
+	return NULL;
+}
+
+void g_module_unload (GModule *module)
+{
+	cmd_del ("mark");
+	cmd_del ("marked");
+
+	{
+		GSList *mel;
+
+		for (mel = marked_jids; mel; mel = mel->next)
+			g_free (mel->data);
+
+		g_slist_free (marked_jids);
+		marked_jids = NULL;
+	}
+}
+
+/* The End */