4 Copyright (C) 1999-2000 Timo Sirainen
6 This program 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.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "module-formats.h"
26 #include "lib-config/iconfig.h"
28 #include "printtext.h"
32 #ifdef HAVE_NL_LANGINFO
33 # include <langinfo.h>
36 #define SLIST_FOREACH(var, head) \
37 for ((var) = (head); \
39 (var) = g_slist_next((var)))
42 char *recode_fallback = NULL;
43 char *recode_out_default = NULL;
44 char *term_charset = NULL;
46 static const char *fe_recode_get_target (WI_ITEM_REC *witem)
48 if (witem && (witem->type == module_get_uniq_id_str("WINDOW ITEM TYPE", "QUERY")
49 || witem->type == module_get_uniq_id_str("WINDOW ITEM TYPE", "CHANNEL")))
50 return window_item_get_target(witem);
52 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_NOT_CHANNEL_OR_QUERY);
56 static int fe_recode_compare_func (CONFIG_NODE *node1, CONFIG_NODE *node2)
58 return strcmp(node1->key, node2->key);
62 static void fe_recode_cmd (const char *data, SERVER_REC *server, WI_ITEM_REC *witem)
65 command_runsub("recode", data, server, witem);
67 CONFIG_NODE *conversions;
69 GSList *sorted = NULL;
71 conversions = iconfig_node_traverse("conversions", FALSE);
73 for (tmp = conversions ? config_node_first(conversions->value) : NULL;
75 tmp = config_node_next(tmp)) {
76 CONFIG_NODE *node = tmp->data;
78 if (node->type == NODE_TYPE_KEY)
79 sorted = g_slist_insert_sorted(sorted, node, (GCompareFunc) fe_recode_compare_func);
82 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_RECODE_HEADER);
83 SLIST_FOREACH(tmp, sorted)
85 CONFIG_NODE *node = tmp->data;
86 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_RECODE_LINE, node->key, node->value);
93 /* SYNTAX: RECODE ADD [[<tag>/]<target>] <charset> */
94 static void fe_recode_add_cmd (const char *data, SERVER_REC *server, WI_ITEM_REC *witem)
102 if (! cmd_get_params(data, &free_arg, 2, &first, &second))
106 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
112 target = fe_recode_get_target(witem);
117 if (is_valid_charset(charset)) {
118 iconfig_set_str("conversions", target, charset);
119 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_CONVERSION_ADDED, target, charset);
121 signal_emit("error command", 2, GINT_TO_POINTER(CMDERR_INVALID_CHARSET), charset);
123 cmd_params_free(free_arg);
126 /* SYNTAX: RECODE REMOVE [<target>] */
127 static void fe_recode_remove_cmd (const char *data, SERVER_REC *server, WI_ITEM_REC *witem)
132 if (! cmd_get_params(data, &free_arg, 1, &target))
136 target = fe_recode_get_target(witem);
141 if (iconfig_get_str("conversions", target, NULL) == NULL)
142 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_CONVERSION_NOT_FOUND, target);
144 iconfig_set_str("conversions", target, NULL);
145 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_CONVERSION_REMOVED, target);
149 cmd_params_free(free_arg);
152 static void read_settings(void)
154 /* preserve the valid values */
155 char *old_term_charset = g_strdup(term_charset);
156 char *old_recode_fallback = g_strdup(recode_fallback);
157 char *old_recode_out_default = g_strdup(recode_out_default);
159 if (settings_get_bool("recode_transliterate")) {
160 /* check if transliterations are supported in this system */
161 if (!is_valid_charset("ASCII")) {
162 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
163 TXT_CONVERSION_NO_TRANSLITS);
164 settings_set_bool("recode_transliterate", FALSE);
169 g_free(recode_fallback);
170 recode_fallback = g_strdup(settings_get_str("recode_fallback"));
171 if (!is_valid_charset(recode_fallback)) {
172 signal_emit("error command", 2, GINT_TO_POINTER(CMDERR_INVALID_CHARSET), recode_fallback);
173 g_free(recode_fallback);
174 recode_fallback = is_valid_charset(old_recode_fallback) ? g_strdup(old_recode_fallback) : "ISO8859-1";
175 settings_set_str("recode_fallback", recode_fallback);
179 g_free(term_charset);
180 term_charset = g_strdup(settings_get_str("term_charset"));
181 if (!is_valid_charset(term_charset)) {
182 g_free(term_charset);
183 #if defined (HAVE_NL_LANGINFO) && defined(CODESET)
184 term_charset = is_valid_charset(old_term_charset) ? g_strdup(old_term_charset) :
185 *nl_langinfo(CODESET) != '\0' ? g_strdup(nl_langinfo(CODESET)) :
188 term_charset = is_valid_charset(old_term_charset) ? g_strdup(old_term_charset) : "ISO8859-1";
190 settings_set_str("term_charset", term_charset);
191 /* FIXME: move the check of term_charset into fe-text/term.c
192 it breaks the proper term_input_type
193 setup and reemitting of the signal is kludgy */
194 if (g_strcasecmp(term_charset, old_term_charset) != 0)
195 signal_emit("setup changed", 0);
198 if (recode_out_default)
199 g_free(recode_out_default);
200 recode_out_default = g_strdup(settings_get_str("recode_out_default_charset"));
201 if (recode_out_default != NULL && *recode_out_default != '\0' &&
202 !is_valid_charset(recode_out_default)) {
203 signal_emit("error command", 2, GINT_TO_POINTER(CMDERR_INVALID_CHARSET), recode_out_default);
204 g_free(recode_out_default);
205 recode_out_default = is_valid_charset(old_recode_out_default) ? g_strdup(old_recode_out_default) : NULL;
206 settings_set_str("recode_out_default_charset", recode_out_default);
209 g_free(old_term_charset);
210 g_free(old_recode_fallback);
211 g_free(old_recode_out_default);
215 void fe_recode_init (void)
217 /* FIXME: print this is not supported instead */
219 command_bind("recode", NULL, (SIGNAL_FUNC) fe_recode_cmd);
220 command_bind("recode add", NULL, (SIGNAL_FUNC) fe_recode_add_cmd);
221 command_bind("recode remove", NULL, (SIGNAL_FUNC) fe_recode_remove_cmd);
222 signal_add("setup changed", (SIGNAL_FUNC) read_settings);
227 void fe_recode_deinit (void)
229 /* FIXME: print this is not supported instead */
231 command_unbind("recode", (SIGNAL_FUNC) fe_recode_cmd);
232 command_unbind("recode add", (SIGNAL_FUNC) fe_recode_add_cmd);
233 command_unbind("recode remove", (SIGNAL_FUNC) fe_recode_remove_cmd);
234 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);