summaryrefslogtreecommitdiff
path: root/libpurple/purpleaccountoption.c
blob: b0ed9ac1d9b23f7f4fc42a0b2cd5cee54d7015c0 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* purple
 *
 * Purple is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 */

#include "prefs.h"
#include "purpleaccountoption.h"
#include "purplekeyvaluepair.h"

/*
 * An option for an account.
 *
 * This is set by protocols, and appears in the account settings
 * dialogs.
 */
struct _PurpleAccountOption
{
	PurplePrefType type;    /* The type of value.                     */

	gchar *text;            /* The text that will appear to the user. */
	gchar *pref_name;       /* The name of the associated preference. */

	union
	{
		gboolean boolean;   /* The default boolean value.             */
		gint integer;       /* The default integer value.             */
		gchar *string;      /* The default string value.              */
		GList *list;        /* The default list value.                */

	} default_value;

	union
	{
		struct
		{
			gboolean masked; /* Whether the value entered should
			                  *   be obscured from view (for
			                  *   passwords and similar options)
			                  */
			GSList *hints;    /* List of hinted values */
		} string;
	} params;
};

G_DEFINE_BOXED_TYPE(
	PurpleAccountOption,
	purple_account_option,
	purple_account_option_copy,
	purple_account_option_destroy
);

PurpleAccountOption *
purple_account_option_new(PurplePrefType type, const char *text,
						const char *pref_name)
{
	PurpleAccountOption *option;

	g_return_val_if_fail(type      != PURPLE_PREF_NONE, NULL);
	g_return_val_if_fail(text      != NULL,           NULL);
	g_return_val_if_fail(pref_name != NULL,           NULL);

	option = g_new0(PurpleAccountOption, 1);

	option->type      = type;
	option->text      = g_strdup(text);
	option->pref_name = g_strdup(pref_name);

	return option;
}

PurpleAccountOption *
purple_account_option_copy(PurpleAccountOption *option) {
	PurpleAccountOption *opt = NULL;

	g_return_val_if_fail(option, NULL);

	opt = g_new0(PurpleAccountOption, 1);
	*opt = *option;

	opt->text = g_strdup(option->text);
	opt->pref_name = g_strdup(option->pref_name);

	if(opt->type == PURPLE_PREF_STRING) {
		opt->default_value.string = g_strdup(option->default_value.string);
		opt->params.string.hints = g_slist_copy(option->params.string.hints);
	}

	return opt;
}

PurpleAccountOption *
purple_account_option_bool_new(const char *text, const char *pref_name,
							 gboolean default_value)
{
	PurpleAccountOption *option;

	option = purple_account_option_new(PURPLE_PREF_BOOLEAN, text, pref_name);

	if (option == NULL)
		return NULL;

	option->default_value.boolean = default_value;

	return option;
}

PurpleAccountOption *
purple_account_option_int_new(const char *text, const char *pref_name,
							int default_value)
{
	PurpleAccountOption *option;

	option = purple_account_option_new(PURPLE_PREF_INT, text, pref_name);

	if (option == NULL)
		return NULL;

	option->default_value.integer = default_value;

	return option;
}

PurpleAccountOption *
purple_account_option_string_new(const char *text, const char *pref_name,
							   const char *default_value)
{
	PurpleAccountOption *option;

	option = purple_account_option_new(PURPLE_PREF_STRING, text, pref_name);

	if (option == NULL)
		return NULL;

	option->default_value.string = g_strdup(default_value);

	return option;
}

PurpleAccountOption *
purple_account_option_list_new(const char *text, const char *pref_name,
							 GList *list)
{
	PurpleAccountOption *option;

	option = purple_account_option_new(PURPLE_PREF_STRING_LIST, text, pref_name);

	if (option == NULL)
		return NULL;

	option->default_value.list = list;

	return option;
}

void
purple_account_option_destroy(PurpleAccountOption *option)
{
	g_return_if_fail(option != NULL);

	g_free(option->text);
	g_free(option->pref_name);

	if (option->type == PURPLE_PREF_STRING)
	{
		g_free(option->default_value.string);
		g_slist_free_full(option->params.string.hints, &g_free);
	}
	else if (option->type == PURPLE_PREF_STRING_LIST)
	{
		g_list_free_full(option->default_value.list,
				 (GDestroyNotify)purple_key_value_pair_free);
	}

	g_free(option);
}

void
purple_account_option_set_default_bool(PurpleAccountOption *option,
									 gboolean value)
{
	g_return_if_fail(option != NULL);
	g_return_if_fail(option->type == PURPLE_PREF_BOOLEAN);

	option->default_value.boolean = value;
}

void
purple_account_option_set_default_int(PurpleAccountOption *option, int value)
{
	g_return_if_fail(option != NULL);
	g_return_if_fail(option->type == PURPLE_PREF_INT);

	option->default_value.integer = value;
}

void
purple_account_option_set_default_string(PurpleAccountOption *option,
									   const char *value)
{
	g_return_if_fail(option != NULL);
	g_return_if_fail(option->type == PURPLE_PREF_STRING);

	g_free(option->default_value.string);
	option->default_value.string = g_strdup(value);
}

void
purple_account_option_string_set_masked(PurpleAccountOption *option, gboolean masked)
{
	g_return_if_fail(option != NULL);
	g_return_if_fail(option->type == PURPLE_PREF_STRING);

	option->params.string.masked = masked;
}

void
purple_account_option_string_set_hints(PurpleAccountOption *option, GSList *hints)
{
	g_return_if_fail(option != NULL);
	g_return_if_fail(option->type == PURPLE_PREF_STRING);

	g_slist_free_full(option->params.string.hints, &g_free);
	option->params.string.hints = hints;
}

void
purple_account_option_set_list(PurpleAccountOption *option, GList *values)
{
	g_return_if_fail(option != NULL);
	g_return_if_fail(option->type == PURPLE_PREF_STRING_LIST);

	g_list_free_full(option->default_value.list,
			 (GDestroyNotify)purple_key_value_pair_free);

	option->default_value.list = values;
}

void
purple_account_option_add_list_item(PurpleAccountOption *option,
								  const char *key, const char *value)
{
	PurpleKeyValuePair *kvp;

	g_return_if_fail(option != NULL);
	g_return_if_fail(key    != NULL);
	g_return_if_fail(value  != NULL);
	g_return_if_fail(option->type == PURPLE_PREF_STRING_LIST);

	kvp = purple_key_value_pair_new_full(key, g_strdup(value), g_free);

	option->default_value.list = g_list_append(option->default_value.list,
											   kvp);
}

PurplePrefType
purple_account_option_get_pref_type(const PurpleAccountOption *option)
{
	g_return_val_if_fail(option != NULL, PURPLE_PREF_NONE);

	return option->type;
}

const char *
purple_account_option_get_text(const PurpleAccountOption *option)
{
	g_return_val_if_fail(option != NULL, NULL);

	return option->text;
}

const char *
purple_account_option_get_setting(const PurpleAccountOption *option)
{
	g_return_val_if_fail(option != NULL, NULL);

	return option->pref_name;
}

gboolean
purple_account_option_get_default_bool(const PurpleAccountOption *option)
{
	g_return_val_if_fail(option != NULL, FALSE);
	g_return_val_if_fail(option->type == PURPLE_PREF_BOOLEAN, FALSE);

	return option->default_value.boolean;
}

int
purple_account_option_get_default_int(const PurpleAccountOption *option)
{
	g_return_val_if_fail(option != NULL, -1);
	g_return_val_if_fail(option->type == PURPLE_PREF_INT, -1);

	return option->default_value.integer;
}

const char *
purple_account_option_get_default_string(const PurpleAccountOption *option)
{
	g_return_val_if_fail(option != NULL, NULL);
	g_return_val_if_fail(option->type == PURPLE_PREF_STRING, NULL);

	return option->default_value.string;
}

const char *
purple_account_option_get_default_list_value(const PurpleAccountOption *option)
{
	PurpleKeyValuePair *kvp;

	g_return_val_if_fail(option != NULL, NULL);
	g_return_val_if_fail(option->type == PURPLE_PREF_STRING_LIST, NULL);

	if (option->default_value.list == NULL)
		return NULL;

	kvp = option->default_value.list->data;

	return (kvp ? kvp->value : NULL);
}

gboolean
purple_account_option_string_get_masked(const PurpleAccountOption *option)
{
	g_return_val_if_fail(option != NULL, FALSE);
	g_return_val_if_fail(option->type == PURPLE_PREF_STRING, FALSE);

	return option->params.string.masked;
}

const GSList *
purple_account_option_string_get_hints(const PurpleAccountOption *option)
{
	g_return_val_if_fail(option != NULL, NULL);
	g_return_val_if_fail(option->type == PURPLE_PREF_STRING, NULL);

	return option->params.string.hints;
}

GList *
purple_account_option_get_list(const PurpleAccountOption *option)
{
	g_return_val_if_fail(option != NULL, NULL);
	g_return_val_if_fail(option->type == PURPLE_PREF_STRING_LIST, NULL);

	return option->default_value.list;
}