summaryrefslogtreecommitdiff
path: root/src/perl/common/Settings.xs
blob: 11a48bb619ceb43a238d02748ec322715ca0c761 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#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

int
settings_get_time(key)
       char *key

int
settings_get_level(key)
       char *key

int
settings_get_size(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

int
settings_set_time(key, value)
	char *key
	char *value

int
settings_set_level(key, value)
	char *key
	char *value

int
settings_set_size(key, value)
	char *key
	char *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_add_time(section, key, def)
	char *section
	char *key
	char *def
CODE:
        perl_settings_add(key);
	settings_add_time_module(MODULE_NAME"/scripts", section, key, def);

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

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

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