marking.c
changeset 0 aad5201e3413
child 1 c8473c43ce72
equal deleted inserted replaced
-1:000000000000 0:aad5201e3413
       
     1 
       
     2 /* Copyright 2009 Myhailo Danylenko
       
     3 
       
     4 This file is part of @PROJECT@
       
     5 
       
     6 @PROJECT@ is free software: you can redistribute it and/or modify
       
     7 it under the terms of the GNU General Public License as published by
       
     8 the Free Software Foundation, either version 2 of the License, or
       
     9 (at your option) any later version.
       
    10 
       
    11 This program is distributed in the hope that it will be useful,
       
    12 but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14 GNU General Public License for more details.
       
    15 
       
    16 You should have received a copy of the GNU General Public License
       
    17 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
       
    18 
       
    19 #include <glib.h>
       
    20 #include <gmodule.h>
       
    21 #include <string.h>
       
    22 
       
    23 #include "hbuf.h"
       
    24 #include "screen.h"
       
    25 #include "commands.h"
       
    26 #include "compl.h"
       
    27 #include "utils.h"
       
    28 #include "logprint.h"
       
    29 
       
    30 static GSList *marked_jids = NULL;
       
    31 
       
    32 // mark set jid
       
    33 // mark clear jid
       
    34 // mark toggle jid
       
    35 // marked clear
       
    36 // marked do cmd
       
    37 static void do_mark (char *arg)
       
    38 {
       
    39 	char     **args   = split_arg (arg, 2, 1);
       
    40 	char      *cmd    = NULL;
       
    41 	char      *jid    = NULL;
       
    42 	gboolean   jfree  = FALSE;
       
    43 	int        marked = -1;
       
    44 
       
    45 	if (args[0]) {
       
    46 		cmd = args[0];
       
    47 
       
    48 		if (args[1])
       
    49 			jid = jidtodisp (args[1]);
       
    50 	} else
       
    51 		cmd = "set";
       
    52 	
       
    53 	if (!strcmp (cmd, "set")) {
       
    54 
       
    55 		if (!g_slist_find_custom (marked_jids, jid ? jid : CURRENT_JID, (GCompareFunc) g_strcmp0)) {
       
    56 			marked_jids = g_slist_append (marked_jids, jid ? jid : g_strdup (CURRENT_JID));
       
    57 			marked = 1;
       
    58 		} else
       
    59 			jfree = TRUE;
       
    60 
       
    61 	} else if (!strcmp (cmd, "clear")) {
       
    62 
       
    63 		GSList *mel = g_slist_find_custom (marked_jids, jid ? jid : CURRENT_JID, (GCompareFunc) g_strcmp0);
       
    64 
       
    65 		if (mel) {
       
    66 			g_free (mel->data);
       
    67 			marked_jids = g_slist_delete_link (marked_jids, mel);
       
    68 			marked = 0;
       
    69 		}
       
    70 
       
    71 		jfree = TRUE;
       
    72 
       
    73 	} else if (!strcmp (cmd, "toggle")) {
       
    74 
       
    75 		GSList *mel = g_slist_find_custom (marked_jids, jid ? jid : CURRENT_JID, (GCompareFunc) g_strcmp0);
       
    76 
       
    77 		if (mel) {
       
    78 			g_free (mel->data);
       
    79 			marked_jids = g_slist_delete_link (marked_jids, mel);
       
    80 			marked = 0;
       
    81 			jfree  = TRUE;
       
    82 		} else {
       
    83 			marked_jids = g_slist_append (marked_jids, jid ? jid : g_strdup (CURRENT_JID));
       
    84 			marked = 1;
       
    85 		}
       
    86 	}
       
    87 
       
    88 	if (marked == 1)
       
    89 		scr_WriteIncomingMessage (jid ? jid : CURRENT_JID, "Marked", 0, HBB_PREFIX_INFO|HBB_PREFIX_NOFLAG, 0);
       
    90 	else if (marked == 0)
       
    91 		scr_WriteIncomingMessage (jid ? jid : CURRENT_JID, "Mark cleared", 0, HBB_PREFIX_INFO|HBB_PREFIX_NOFLAG, 0);
       
    92 
       
    93 	if (jfree && jid)
       
    94 		g_free (jid);
       
    95 
       
    96 	free_arg_lst (args);
       
    97 }
       
    98 
       
    99 void do_marked (char *arg)
       
   100 {
       
   101 	char **args = split_arg (arg, 2, 1);
       
   102 	char  *cmd = args[0];
       
   103 
       
   104 	if (!cmd)
       
   105 		cmd = "list";
       
   106 
       
   107 	if (!strcmp (cmd, "list")) {
       
   108 
       
   109 		GSList *mel;
       
   110 
       
   111 		scr_LogPrint (LPRINT_NORMAL, "Marked jids:");
       
   112 		for (mel = marked_jids; mel; mel = mel->next) {
       
   113 			char *jid = (char *) mel->data;
       
   114 			scr_LogPrint (LPRINT_NORMAL, " * %s", jid);
       
   115 		}
       
   116 
       
   117 	} else if (!strcmp (cmd, "clear")) {
       
   118 
       
   119 		GSList *mel;
       
   120 
       
   121 		for (mel = marked_jids; mel; mel = mel->next)
       
   122 			g_free (mel->data);
       
   123 
       
   124 		g_slist_free (marked_jids);
       
   125 		marked_jids = NULL;
       
   126 
       
   127 	} else if (!strcmp (cmd, "do")) {
       
   128 		char *format = args[1];
       
   129 
       
   130 		if (!format)
       
   131 			scr_LogPrint (LPRINT_NORMAL, "You must specify action to do with marked jids");
       
   132 		else {
       
   133 			char     *pos   = format;
       
   134 			gboolean  found = FALSE;
       
   135 			gboolean  error = FALSE;
       
   136 
       
   137 			for (pos = strchr (pos, '%'); pos; pos = strchr (pos, '%')) {
       
   138 				if (*(pos + 1) == 's')
       
   139 					found = TRUE;
       
   140 				else if (*(pos + 1) == '%')
       
   141 					++pos;
       
   142 				else {
       
   143 					scr_LogPrint (LPRINT_NORMAL, "Action string must not contain any other %%-sequesnces!");
       
   144 					error = TRUE;
       
   145 					break;
       
   146 				}
       
   147 
       
   148 				++pos;
       
   149 			}
       
   150 
       
   151 			if (!found)
       
   152 				scr_LogPrint (LPRINT_NORMAL, "Action string must contain one %%s!");
       
   153 			else if (!error) {
       
   154 				GSList *mel;
       
   155 
       
   156 				for (mel = marked_jids; mel; mel = mel->next) {
       
   157 					char *jid     = (char *) mel->data;
       
   158 					char *command = g_strdup_printf (format, jid);
       
   159 					process_command (command, TRUE);
       
   160 					g_free (command);
       
   161 				}
       
   162 			}
       
   163 		}
       
   164 	}
       
   165 
       
   166 	free_arg_lst (args);
       
   167 }
       
   168 
       
   169 gchar *g_module_check_init (GModule *module)
       
   170 {
       
   171 	cmd_add ("mark", "", 0, COMPL_JID, do_mark, NULL);
       
   172 	cmd_add ("marked", "", 0, COMPL_CMD, do_marked, NULL);
       
   173 
       
   174 	return NULL;
       
   175 }
       
   176 
       
   177 void g_module_unload (GModule *module)
       
   178 {
       
   179 	cmd_del ("mark");
       
   180 	cmd_del ("marked");
       
   181 
       
   182 	{
       
   183 		GSList *mel;
       
   184 
       
   185 		for (mel = marked_jids; mel; mel = mel->next)
       
   186 			g_free (mel->data);
       
   187 
       
   188 		g_slist_free (marked_jids);
       
   189 		marked_jids = NULL;
       
   190 	}
       
   191 }
       
   192 
       
   193 /* The End */