4 Copyright (C) 1999 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
22 #include "module-formats.h"
27 #include "lib-config/iconfig.h"
31 #include "printtext.h"
34 static void set_print(SETTINGS_REC *rec)
37 char value_int[MAX_INT_STRLEN];
40 case SETTING_TYPE_BOOLEAN:
41 value = settings_get_bool(rec->key) ? "ON" : "OFF";
43 case SETTING_TYPE_INT:
44 ltoa(value_int, settings_get_int(rec->key));
47 case SETTING_TYPE_STRING:
48 case SETTING_TYPE_TIME:
49 case SETTING_TYPE_LEVEL:
50 case SETTING_TYPE_SIZE:
51 value = settings_get_str(rec->key);
56 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_SET_ITEM,
60 static void set_boolean(const char *key, const char *value)
62 if (g_strcasecmp(value, "ON") == 0)
63 settings_set_bool(key, TRUE);
64 else if (g_strcasecmp(value, "OFF") == 0)
65 settings_set_bool(key, FALSE);
66 else if (g_strcasecmp(value, "TOGGLE") == 0)
67 settings_set_bool(key, !settings_get_bool(key));
69 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_NOT_TOGGLE);
72 /* SYNTAX: SET [-clear | -default] [<key> [<value>]] */
73 static void cmd_set(char *data)
77 const char *last_section;
80 int found, clear, set_default;
82 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST | PARAM_FLAG_OPTIONS,
83 "set", &optlist, &key, &value))
86 clear = g_hash_table_lookup(optlist, "clear") != NULL;
87 set_default = g_hash_table_lookup(optlist, "default") != NULL;
90 clear = set_default = FALSE;
92 last_section = ""; found = 0;
93 sets = settings_get_sorted();
94 for (tmp = sets; tmp != NULL; tmp = tmp->next) {
95 SETTINGS_REC *rec = tmp->data;
97 if (((clear || set_default || *value != '\0') && g_strcasecmp(rec->key, key) != 0) ||
98 (*value == '\0' && *key != '\0' && stristr(rec->key, key) == NULL))
101 if (strcmp(last_section, rec->section) != 0) {
103 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
104 TXT_SET_TITLE, rec->section);
105 last_section = rec->section;
108 if (clear || set_default || *value != '\0') {
109 /* change the setting */
111 case SETTING_TYPE_BOOLEAN:
113 settings_set_bool(key, FALSE);
114 else if (set_default)
115 settings_set_bool(key, rec->default_value.v_bool);
117 set_boolean(key, value);
119 case SETTING_TYPE_INT:
120 settings_set_int(key, clear ? 0 :
121 set_default ? rec->default_value.v_int :
124 case SETTING_TYPE_STRING:
125 settings_set_str(key, clear ? "" :
126 set_default ? rec->default_value.v_string :
129 case SETTING_TYPE_TIME:
130 if (!settings_set_time(key, clear ? "0" :
131 set_default ? rec->default_value.v_string : value))
132 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
135 case SETTING_TYPE_LEVEL:
136 if (!settings_set_level(key, clear ? "" :
137 set_default ? rec->default_value.v_string : value))
138 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
141 case SETTING_TYPE_SIZE:
142 if (!settings_set_size(key, clear ? "0" :
143 set_default ? rec->default_value.v_string : value))
144 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
148 signal_emit("setup changed", 0);
154 if (clear || *value != '\0')
160 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
161 TXT_SET_UNKNOWN, key);
164 cmd_params_free(free_arg);
167 /* SYNTAX: TOGGLE <key> [on|off|toggle] */
168 static void cmd_toggle(const char *data)
174 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &key, &value))
177 if (*key == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
179 type = settings_get_type(key);
181 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_SET_UNKNOWN, key);
182 else if (type != SETTING_TYPE_BOOLEAN)
183 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_SET_NOT_BOOLEAN, key);
185 set_boolean(key, *value != '\0' ? value : "TOGGLE");
186 set_print(settings_get_record(key));
187 signal_emit("setup changed", 0);
190 cmd_params_free(free_arg);
193 static int config_key_compare(CONFIG_NODE *node1, CONFIG_NODE *node2)
195 return g_strcasecmp(node1->key, node2->key);
198 static void show_aliases(const char *alias)
204 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_HEADER);
206 node = iconfig_node_traverse("aliases", FALSE);
207 tmp = node == NULL ? NULL : config_node_first(node->value);
209 /* first get the list of aliases sorted */
211 aliaslen = strlen(alias);
212 for (; tmp != NULL; tmp = config_node_next(tmp)) {
213 CONFIG_NODE *node = tmp->data;
215 if (node->type != NODE_TYPE_KEY)
218 if (aliaslen != 0 && g_strncasecmp(node->key, alias, aliaslen) != 0)
221 list = g_slist_insert_sorted(list, node, (GCompareFunc) config_key_compare);
224 /* print the aliases */
225 for (tmp = list; tmp != NULL; tmp = tmp->next) {
226 CONFIG_NODE *node = tmp->data;
228 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_LINE,
229 node->key, node->value);
233 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_FOOTER);
236 static void alias_remove(const char *alias)
238 if (iconfig_get_str("aliases", alias, NULL) == NULL)
239 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_NOT_FOUND, alias);
241 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_REMOVED, alias);
242 iconfig_set_str("aliases", alias, NULL);
244 signal_emit("alias removed", 1, alias);
248 /* SYNTAX: ALIAS [[-]<alias> [<command>]] */
249 static void cmd_alias(const char *data)
254 g_return_if_fail(data != NULL);
256 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &alias, &value))
260 if (alias[1] != '\0') alias_remove(alias+1);
261 } else if (*alias == '\0' || *value == '\0')
264 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_ADDED, alias);
265 iconfig_set_str("aliases", alias, value);
266 signal_emit("alias added", 2, alias, value);
268 cmd_params_free(free_arg);
271 /* SYNTAX: UNALIAS <alias> */
272 static void cmd_unalias(const char *data)
277 g_return_if_fail(data != NULL);
279 if (!cmd_get_params(data, &free_arg, 1, &alias))
281 if (*alias == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
284 cmd_params_free(free_arg);
287 /* SYNTAX: RELOAD [<file>] */
288 static void cmd_reload(const char *data)
292 fname = *data == '\0' ? get_irssi_config() : data;
294 if (settings_reread(fname)) {
295 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
296 TXT_CONFIG_RELOADED, fname);
300 static void settings_save_fe(const char *fname)
302 if (settings_save(fname, FALSE /* not autosaved */)) {
303 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
304 TXT_CONFIG_SAVED, fname);
308 static void settings_save_confirm(const char *line, char *fname)
310 if (i_toupper(line[0]) == 'Y')
311 settings_save_fe(fname);
315 /* SYNTAX: SAVE [<file>] */
316 static void cmd_save(const char *data)
319 char *format, *fname;
322 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
323 "save", &optlist, &fname))
327 fname = mainconfig->fname;
329 if (!irssi_config_is_changed(fname))
330 settings_save_fe(fname);
332 /* config file modified outside irssi */
333 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
334 TXT_CONFIG_MODIFIED, fname);
336 format = format_get_text(MODULE_NAME, NULL, NULL, NULL,
337 TXT_OVERWRITE_CONFIG);
338 keyboard_entry_redirect((SIGNAL_FUNC) settings_save_confirm,
339 format, 0, g_strdup(fname));
343 cmd_params_free(free_arg);
346 static void settings_clean_confirm(const char *line)
348 if (i_toupper(line[0]) == 'Y')
349 settings_clean_invalid();
352 static void sig_settings_errors(const char *msg)
354 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "%s", msg);
355 keyboard_entry_redirect((SIGNAL_FUNC) settings_clean_confirm,
356 "Remove unknown settings from config file (Y/n)?",
360 void fe_settings_init(void)
362 command_bind("set", NULL, (SIGNAL_FUNC) cmd_set);
363 command_bind("toggle", NULL, (SIGNAL_FUNC) cmd_toggle);
364 command_bind("alias", NULL, (SIGNAL_FUNC) cmd_alias);
365 command_bind("unalias", NULL, (SIGNAL_FUNC) cmd_unalias);
366 command_bind("reload", NULL, (SIGNAL_FUNC) cmd_reload);
367 command_bind("save", NULL, (SIGNAL_FUNC) cmd_save);
368 command_set_options("set", "clear default");
370 signal_add("settings errors", (SIGNAL_FUNC) sig_settings_errors);
373 void fe_settings_deinit(void)
375 command_unbind("set", (SIGNAL_FUNC) cmd_set);
376 command_unbind("toggle", (SIGNAL_FUNC) cmd_toggle);
377 command_unbind("alias", (SIGNAL_FUNC) cmd_alias);
378 command_unbind("unalias", (SIGNAL_FUNC) cmd_unalias);
379 command_unbind("reload", (SIGNAL_FUNC) cmd_reload);
380 command_unbind("save", (SIGNAL_FUNC) cmd_save);
382 signal_remove("settings errors", (SIGNAL_FUNC) sig_settings_errors);