Remove unused Meson option for enchant.
[pidgin-git.git] / libpurple / accountopt.c
blobefafd5b37216c1c8d61e4be74202fad54cdc4e74
1 /* purple
3 * Purple is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 #include "internal.h"
23 #include "accountopt.h"
24 #include "util.h"
25 #include "glibcompat.h"
28 * An option for an account.
30 * This is set by protocols, and appears in the account settings
31 * dialogs.
33 struct _PurpleAccountOption
35 PurplePrefType type; /* The type of value. */
37 char *text; /* The text that will appear to the user. */
38 char *pref_name; /* The name of the associated preference. */
40 union
42 gboolean boolean; /* The default boolean value. */
43 int integer; /* The default integer value. */
44 char *string; /* The default string value. */
45 GList *list; /* The default list value. */
47 } default_value;
49 union
51 struct
53 gboolean masked; /* Whether the value entered should
54 * be obscured from view (for
55 * passwords and similar options)
57 GSList *hints; /* List of hinted values */
58 } string;
59 } params;
63 * A username split.
65 * This is used by some protocols to separate the fields of the username
66 * into more human-readable components.
68 struct _PurpleAccountUserSplit
70 char *text; /* The text that will appear to the user. */
71 char *default_value; /* The default value. */
72 char field_sep; /* The field separator. */
73 gboolean reverse; /* TRUE if the separator should be found
74 starting a the end of the string, FALSE
75 otherwise */
76 gboolean constant;
80 PurpleAccountOption *
81 purple_account_option_new(PurplePrefType type, const char *text,
82 const char *pref_name)
84 PurpleAccountOption *option;
86 g_return_val_if_fail(type != PURPLE_PREF_NONE, NULL);
87 g_return_val_if_fail(text != NULL, NULL);
88 g_return_val_if_fail(pref_name != NULL, NULL);
90 option = g_new0(PurpleAccountOption, 1);
92 option->type = type;
93 option->text = g_strdup(text);
94 option->pref_name = g_strdup(pref_name);
96 return option;
99 PurpleAccountOption *
100 purple_account_option_bool_new(const char *text, const char *pref_name,
101 gboolean default_value)
103 PurpleAccountOption *option;
105 option = purple_account_option_new(PURPLE_PREF_BOOLEAN, text, pref_name);
107 if (option == NULL)
108 return NULL;
110 option->default_value.boolean = default_value;
112 return option;
115 PurpleAccountOption *
116 purple_account_option_int_new(const char *text, const char *pref_name,
117 int default_value)
119 PurpleAccountOption *option;
121 option = purple_account_option_new(PURPLE_PREF_INT, text, pref_name);
123 if (option == NULL)
124 return NULL;
126 option->default_value.integer = default_value;
128 return option;
131 PurpleAccountOption *
132 purple_account_option_string_new(const char *text, const char *pref_name,
133 const char *default_value)
135 PurpleAccountOption *option;
137 option = purple_account_option_new(PURPLE_PREF_STRING, text, pref_name);
139 if (option == NULL)
140 return NULL;
142 option->default_value.string = g_strdup(default_value);
144 return option;
147 PurpleAccountOption *
148 purple_account_option_list_new(const char *text, const char *pref_name,
149 GList *list)
151 PurpleAccountOption *option;
153 option = purple_account_option_new(PURPLE_PREF_STRING_LIST, text, pref_name);
155 if (option == NULL)
156 return NULL;
158 option->default_value.list = list;
160 return option;
163 static void
164 purple_account_option_list_free(gpointer data, gpointer user_data)
166 PurpleKeyValuePair *kvp = data;
168 g_free(kvp->value);
169 g_free(kvp->key);
170 g_free(kvp);
173 void
174 purple_account_option_destroy(PurpleAccountOption *option)
176 g_return_if_fail(option != NULL);
178 g_free(option->text);
179 g_free(option->pref_name);
181 if (option->type == PURPLE_PREF_STRING)
183 g_free(option->default_value.string);
184 g_slist_free_full(option->params.string.hints, &g_free);
186 else if (option->type == PURPLE_PREF_STRING_LIST)
188 g_list_free_full(option->default_value.list,
189 (GDestroyNotify)purple_account_option_list_free);
192 g_free(option);
195 void
196 purple_account_option_set_default_bool(PurpleAccountOption *option,
197 gboolean value)
199 g_return_if_fail(option != NULL);
200 g_return_if_fail(option->type == PURPLE_PREF_BOOLEAN);
202 option->default_value.boolean = value;
205 void
206 purple_account_option_set_default_int(PurpleAccountOption *option, int value)
208 g_return_if_fail(option != NULL);
209 g_return_if_fail(option->type == PURPLE_PREF_INT);
211 option->default_value.integer = value;
214 void
215 purple_account_option_set_default_string(PurpleAccountOption *option,
216 const char *value)
218 g_return_if_fail(option != NULL);
219 g_return_if_fail(option->type == PURPLE_PREF_STRING);
221 g_free(option->default_value.string);
222 option->default_value.string = g_strdup(value);
225 void
226 purple_account_option_string_set_masked(PurpleAccountOption *option, gboolean masked)
228 g_return_if_fail(option != NULL);
229 g_return_if_fail(option->type == PURPLE_PREF_STRING);
231 option->params.string.masked = masked;
234 void
235 purple_account_option_string_set_hints(PurpleAccountOption *option, GSList *hints)
237 g_return_if_fail(option != NULL);
238 g_return_if_fail(option->type == PURPLE_PREF_STRING);
240 g_slist_free_full(option->params.string.hints, &g_free);
241 option->params.string.hints = hints;
244 void
245 purple_account_option_set_list(PurpleAccountOption *option, GList *values)
247 g_return_if_fail(option != NULL);
248 g_return_if_fail(option->type == PURPLE_PREF_STRING_LIST);
250 g_list_free_full(option->default_value.list,
251 (GDestroyNotify)purple_account_option_list_free);
253 option->default_value.list = values;
256 void
257 purple_account_option_add_list_item(PurpleAccountOption *option,
258 const char *key, const char *value)
260 PurpleKeyValuePair *kvp;
262 g_return_if_fail(option != NULL);
263 g_return_if_fail(key != NULL);
264 g_return_if_fail(value != NULL);
265 g_return_if_fail(option->type == PURPLE_PREF_STRING_LIST);
267 kvp = g_new0(PurpleKeyValuePair, 1);
268 kvp->key = g_strdup(key);
269 kvp->value = g_strdup(value);
271 option->default_value.list = g_list_append(option->default_value.list,
272 kvp);
275 PurplePrefType
276 purple_account_option_get_pref_type(const PurpleAccountOption *option)
278 g_return_val_if_fail(option != NULL, PURPLE_PREF_NONE);
280 return option->type;
283 const char *
284 purple_account_option_get_text(const PurpleAccountOption *option)
286 g_return_val_if_fail(option != NULL, NULL);
288 return option->text;
291 const char *
292 purple_account_option_get_setting(const PurpleAccountOption *option)
294 g_return_val_if_fail(option != NULL, NULL);
296 return option->pref_name;
299 gboolean
300 purple_account_option_get_default_bool(const PurpleAccountOption *option)
302 g_return_val_if_fail(option != NULL, FALSE);
303 g_return_val_if_fail(option->type == PURPLE_PREF_BOOLEAN, FALSE);
305 return option->default_value.boolean;
309 purple_account_option_get_default_int(const PurpleAccountOption *option)
311 g_return_val_if_fail(option != NULL, -1);
312 g_return_val_if_fail(option->type == PURPLE_PREF_INT, -1);
314 return option->default_value.integer;
317 const char *
318 purple_account_option_get_default_string(const PurpleAccountOption *option)
320 g_return_val_if_fail(option != NULL, NULL);
321 g_return_val_if_fail(option->type == PURPLE_PREF_STRING, NULL);
323 return option->default_value.string;
326 const char *
327 purple_account_option_get_default_list_value(const PurpleAccountOption *option)
329 PurpleKeyValuePair *kvp;
331 g_return_val_if_fail(option != NULL, NULL);
332 g_return_val_if_fail(option->type == PURPLE_PREF_STRING_LIST, NULL);
334 if (option->default_value.list == NULL)
335 return NULL;
337 kvp = option->default_value.list->data;
339 return (kvp ? kvp->value : NULL);
342 gboolean
343 purple_account_option_string_get_masked(const PurpleAccountOption *option)
345 g_return_val_if_fail(option != NULL, FALSE);
346 g_return_val_if_fail(option->type == PURPLE_PREF_STRING, FALSE);
348 return option->params.string.masked;
351 const GSList *
352 purple_account_option_string_get_hints(const PurpleAccountOption *option)
354 g_return_val_if_fail(option != NULL, FALSE);
355 g_return_val_if_fail(option->type == PURPLE_PREF_STRING, FALSE);
357 return option->params.string.hints;
360 GList *
361 purple_account_option_get_list(const PurpleAccountOption *option)
363 g_return_val_if_fail(option != NULL, NULL);
364 g_return_val_if_fail(option->type == PURPLE_PREF_STRING_LIST, NULL);
366 return option->default_value.list;
369 /**************************************************************************
370 * Account User Split API
371 **************************************************************************/
372 PurpleAccountUserSplit *
373 purple_account_user_split_new(const char *text, const char *default_value,
374 char sep)
376 PurpleAccountUserSplit *split;
378 g_return_val_if_fail(text != NULL, NULL);
379 g_return_val_if_fail(sep != 0, NULL);
381 split = g_new0(PurpleAccountUserSplit, 1);
383 split->text = g_strdup(text);
384 split->field_sep = sep;
385 split->default_value = g_strdup(default_value);
386 split->reverse = TRUE;
388 return split;
391 void
392 purple_account_user_split_destroy(PurpleAccountUserSplit *split)
394 g_return_if_fail(split != NULL);
396 g_free(split->text);
397 g_free(split->default_value);
398 g_free(split);
401 const char *
402 purple_account_user_split_get_text(const PurpleAccountUserSplit *split)
404 g_return_val_if_fail(split != NULL, NULL);
406 return split->text;
409 const char *
410 purple_account_user_split_get_default_value(const PurpleAccountUserSplit *split)
412 g_return_val_if_fail(split != NULL, NULL);
414 return split->default_value;
417 char
418 purple_account_user_split_get_separator(const PurpleAccountUserSplit *split)
420 g_return_val_if_fail(split != NULL, 0);
422 return split->field_sep;
425 gboolean
426 purple_account_user_split_get_reverse(const PurpleAccountUserSplit *split)
428 g_return_val_if_fail(split != NULL, FALSE);
430 return split->reverse;
433 void
434 purple_account_user_split_set_reverse(PurpleAccountUserSplit *split, gboolean reverse)
436 g_return_if_fail(split != NULL);
438 split->reverse = reverse;
441 gboolean
442 purple_account_user_split_is_constant(const PurpleAccountUserSplit *split)
444 g_return_val_if_fail(split != NULL, FALSE);
446 return split->constant;
449 void
450 purple_account_user_split_set_constant(PurpleAccountUserSplit *split,
451 gboolean constant)
453 g_return_if_fail(split != NULL);
455 split->constant = constant;