Fix some functions descriptions
[pidgin-git.git] / libpurple / accountopt.c
blob81c67760606cd4b7217ee9e047d155da5ce5b0f4
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 if (option->default_value.list != NULL)
190 g_list_foreach(option->default_value.list, purple_account_option_list_free, NULL);
191 g_list_free(option->default_value.list);
195 g_free(option);
198 void
199 purple_account_option_set_default_bool(PurpleAccountOption *option,
200 gboolean value)
202 g_return_if_fail(option != NULL);
203 g_return_if_fail(option->type == PURPLE_PREF_BOOLEAN);
205 option->default_value.boolean = value;
208 void
209 purple_account_option_set_default_int(PurpleAccountOption *option, int value)
211 g_return_if_fail(option != NULL);
212 g_return_if_fail(option->type == PURPLE_PREF_INT);
214 option->default_value.integer = value;
217 void
218 purple_account_option_set_default_string(PurpleAccountOption *option,
219 const char *value)
221 g_return_if_fail(option != NULL);
222 g_return_if_fail(option->type == PURPLE_PREF_STRING);
224 g_free(option->default_value.string);
225 option->default_value.string = g_strdup(value);
228 void
229 purple_account_option_string_set_masked(PurpleAccountOption *option, gboolean masked)
231 g_return_if_fail(option != NULL);
232 g_return_if_fail(option->type == PURPLE_PREF_STRING);
234 option->params.string.masked = masked;
237 void
238 purple_account_option_string_set_hints(PurpleAccountOption *option, GSList *hints)
240 g_return_if_fail(option != NULL);
241 g_return_if_fail(option->type == PURPLE_PREF_STRING);
243 g_slist_free_full(option->params.string.hints, &g_free);
244 option->params.string.hints = hints;
247 void
248 purple_account_option_set_list(PurpleAccountOption *option, GList *values)
250 g_return_if_fail(option != NULL);
251 g_return_if_fail(option->type == PURPLE_PREF_STRING_LIST);
253 if (option->default_value.list != NULL)
255 g_list_foreach(option->default_value.list, purple_account_option_list_free, NULL);
256 g_list_free(option->default_value.list);
259 option->default_value.list = values;
262 void
263 purple_account_option_add_list_item(PurpleAccountOption *option,
264 const char *key, const char *value)
266 PurpleKeyValuePair *kvp;
268 g_return_if_fail(option != NULL);
269 g_return_if_fail(key != NULL);
270 g_return_if_fail(value != NULL);
271 g_return_if_fail(option->type == PURPLE_PREF_STRING_LIST);
273 kvp = g_new0(PurpleKeyValuePair, 1);
274 kvp->key = g_strdup(key);
275 kvp->value = g_strdup(value);
277 option->default_value.list = g_list_append(option->default_value.list,
278 kvp);
281 PurplePrefType
282 purple_account_option_get_pref_type(const PurpleAccountOption *option)
284 g_return_val_if_fail(option != NULL, PURPLE_PREF_NONE);
286 return option->type;
289 const char *
290 purple_account_option_get_text(const PurpleAccountOption *option)
292 g_return_val_if_fail(option != NULL, NULL);
294 return option->text;
297 const char *
298 purple_account_option_get_setting(const PurpleAccountOption *option)
300 g_return_val_if_fail(option != NULL, NULL);
302 return option->pref_name;
305 gboolean
306 purple_account_option_get_default_bool(const PurpleAccountOption *option)
308 g_return_val_if_fail(option != NULL, FALSE);
309 g_return_val_if_fail(option->type == PURPLE_PREF_BOOLEAN, FALSE);
311 return option->default_value.boolean;
315 purple_account_option_get_default_int(const PurpleAccountOption *option)
317 g_return_val_if_fail(option != NULL, -1);
318 g_return_val_if_fail(option->type == PURPLE_PREF_INT, -1);
320 return option->default_value.integer;
323 const char *
324 purple_account_option_get_default_string(const PurpleAccountOption *option)
326 g_return_val_if_fail(option != NULL, NULL);
327 g_return_val_if_fail(option->type == PURPLE_PREF_STRING, NULL);
329 return option->default_value.string;
332 const char *
333 purple_account_option_get_default_list_value(const PurpleAccountOption *option)
335 PurpleKeyValuePair *kvp;
337 g_return_val_if_fail(option != NULL, NULL);
338 g_return_val_if_fail(option->type == PURPLE_PREF_STRING_LIST, NULL);
340 if (option->default_value.list == NULL)
341 return NULL;
343 kvp = option->default_value.list->data;
345 return (kvp ? kvp->value : NULL);
348 gboolean
349 purple_account_option_string_get_masked(const PurpleAccountOption *option)
351 g_return_val_if_fail(option != NULL, FALSE);
352 g_return_val_if_fail(option->type == PURPLE_PREF_STRING, FALSE);
354 return option->params.string.masked;
357 const GSList *
358 purple_account_option_string_get_hints(const PurpleAccountOption *option)
360 g_return_val_if_fail(option != NULL, FALSE);
361 g_return_val_if_fail(option->type == PURPLE_PREF_STRING, FALSE);
363 return option->params.string.hints;
366 GList *
367 purple_account_option_get_list(const PurpleAccountOption *option)
369 g_return_val_if_fail(option != NULL, NULL);
370 g_return_val_if_fail(option->type == PURPLE_PREF_STRING_LIST, NULL);
372 return option->default_value.list;
375 /**************************************************************************
376 * Account User Split API
377 **************************************************************************/
378 PurpleAccountUserSplit *
379 purple_account_user_split_new(const char *text, const char *default_value,
380 char sep)
382 PurpleAccountUserSplit *split;
384 g_return_val_if_fail(text != NULL, NULL);
385 g_return_val_if_fail(sep != 0, NULL);
387 split = g_new0(PurpleAccountUserSplit, 1);
389 split->text = g_strdup(text);
390 split->field_sep = sep;
391 split->default_value = g_strdup(default_value);
392 split->reverse = TRUE;
394 return split;
397 void
398 purple_account_user_split_destroy(PurpleAccountUserSplit *split)
400 g_return_if_fail(split != NULL);
402 g_free(split->text);
403 g_free(split->default_value);
404 g_free(split);
407 const char *
408 purple_account_user_split_get_text(const PurpleAccountUserSplit *split)
410 g_return_val_if_fail(split != NULL, NULL);
412 return split->text;
415 const char *
416 purple_account_user_split_get_default_value(const PurpleAccountUserSplit *split)
418 g_return_val_if_fail(split != NULL, NULL);
420 return split->default_value;
423 char
424 purple_account_user_split_get_separator(const PurpleAccountUserSplit *split)
426 g_return_val_if_fail(split != NULL, 0);
428 return split->field_sep;
431 gboolean
432 purple_account_user_split_get_reverse(const PurpleAccountUserSplit *split)
434 g_return_val_if_fail(split != NULL, FALSE);
436 return split->reverse;
439 void
440 purple_account_user_split_set_reverse(PurpleAccountUserSplit *split, gboolean reverse)
442 g_return_if_fail(split != NULL);
444 split->reverse = reverse;
447 gboolean
448 purple_account_user_split_is_constant(const PurpleAccountUserSplit *split)
450 g_return_val_if_fail(split != NULL, FALSE);
452 return split->constant;
455 void
456 purple_account_user_split_set_constant(PurpleAccountUserSplit *split,
457 gboolean constant)
459 g_return_if_fail(split != NULL);
461 split->constant = constant;