summaryrefslogtreecommitdiff
path: root/src/perl/common/Settings.xs
blob: a7c5e6f62c8070e21a21449a019b652fb6f9736a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#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

char *
settings_get_str(key)
	char *key
CODE:
	RETVAL = (char *) settings_get_str(key);
OUTPUT:
	RETVAL

int
settings_get_int(key)
	char *key

int
settings_get_bool(key)
	char *key

void
settings_set_str(key, value)
	char *key
	char *value

void
settings_set_int(key, value)
	char *key
	int value

void
settings_set_bool(key, value)
	char *key
	int value

void
settings_add_str(section, key, def)
	char *section
	char *key
	char *def
CODE:
        perl_settings_add(key);
	settings_add_str_module(MODULE_NAME"/scripts", section, key, def);

void
settings_add_int(section, key, def)
	char *section
	char *key
	int def
CODE:
        perl_settings_add(key);
	settings_add_int_module(MODULE_NAME"/scripts", section, key, def);

void
settings_add_bool(section, key, def)
	char *section
	char *key
	int def
CODE:
        perl_settings_add(key);
	settings_add_bool_module(MODULE_NAME"/scripts", section, key, def);

void
settings_remove(key)
	char *key
CODE:
        perl_settings_remove(key);
	settings_remove(key);