diff --git a/src/perl/common/Irssi.xs b/src/perl/common/Irssi.xs index 5e28bed8..53278dcb 100644 --- a/src/perl/common/Irssi.xs +++ b/src/perl/common/Irssi.xs @@ -8,10 +8,12 @@ void init() CODE: perl_api_version_check("Irssi"); + perl_settings_init(); void deinit() CODE: + perl_settings_deinit(); BOOT: irssi_boot(Channel); diff --git a/src/perl/common/Settings.xs b/src/perl/common/Settings.xs index eeb30c39..39d52033 100644 --- a/src/perl/common/Settings.xs +++ b/src/perl/common/Settings.xs @@ -1,4 +1,69 @@ #include "module.h" +#include "misc.h" + +static GHashTable *perl_settings; + +static void perl_settings_add(const char *key) +{ + PERL_SCRIPT_REC *script; + GSList *list; + + script = perl_script_find_package(perl_get_package()); + g_return_if_fail(script != NULL); + + list = g_hash_table_lookup(perl_settings, script); + list = g_slist_append(list, g_strdup(key)); + g_hash_table_insert(perl_settings, script, list); +} + +static void perl_settings_remove(const char *key) +{ + PERL_SCRIPT_REC *script; + GSList *list, *pos; + + script = perl_script_find_package(perl_get_package()); + g_return_if_fail(script != NULL); + + list = g_hash_table_lookup(perl_settings, script); + pos = gslist_find_icase_string(list, key); + if (pos != NULL) { + list = g_slist_remove(list, pos->data); + g_hash_table_insert(perl_settings, script, list); + } +} + +static void perl_settings_free(PERL_SCRIPT_REC *script, GSList *list) +{ + g_slist_foreach(list, (GFunc) g_free, NULL); + g_slist_free(list); +} + +static void sig_script_destroyed(PERL_SCRIPT_REC *script) +{ + GSList *list; + + list = g_hash_table_lookup(perl_settings, script); + if (list != NULL) { + g_slist_foreach(list, (GFunc) settings_remove, NULL); + perl_settings_free(script, list); + g_hash_table_remove(perl_settings, script); + } +} + +void perl_settings_init(void) +{ + perl_settings = g_hash_table_new((GHashFunc) g_direct_hash, + (GCompareFunc) g_direct_equal); + signal_add("script destroyed", (SIGNAL_FUNC) sig_script_destroyed); +} + +void perl_settings_deinit(void) +{ + signal_remove("script destroyed", (SIGNAL_FUNC) sig_script_destroyed); + + g_hash_table_foreach(perl_settings, (GHFunc) perl_settings_free, NULL); + g_hash_table_destroy(perl_settings); +} MODULE = Irssi::Settings PACKAGE = Irssi PROTOTYPES: ENABLE @@ -24,19 +89,31 @@ settings_add_str(section, key, def) char *section char *key char *def +CODE: + perl_settings_add(key); + settings_add_str(section, key, def); void settings_add_int(section, key, def) char *section char *key int def +CODE: + perl_settings_add(key); + settings_add_int(section, key, def); void settings_add_bool(section, key, def) char *section char *key int def +CODE: + perl_settings_add(key); + settings_add_bool(section, key, def); void settings_remove(key) char *key +CODE: + perl_settings_remove(key); + settings_remove(key);