Fix an incorrect call to soup_message_set_request.
[pidgin-git.git] / libpurple / purpleaccountoption.c
blobbfa58ad8c8ffd56666584e40a73e32de0bf8bb92
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 "purpleaccountoption.h"
24 #include "prefs.h"
25 #include "util.h"
26 #include "glibcompat.h"
29 * An option for an account.
31 * This is set by protocols, and appears in the account settings
32 * dialogs.
34 struct _PurpleAccountOption
36 PurplePrefType type; /* The type of value. */
38 gchar *text; /* The text that will appear to the user. */
39 gchar *pref_name; /* The name of the associated preference. */
41 union
43 gboolean boolean; /* The default boolean value. */
44 gint integer; /* The default integer value. */
45 gchar *string; /* The default string value. */
46 GList *list; /* The default list value. */
48 } default_value;
50 union
52 struct
54 gboolean masked; /* Whether the value entered should
55 * be obscured from view (for
56 * passwords and similar options)
58 GSList *hints; /* List of hinted values */
59 } string;
60 } params;
63 G_DEFINE_BOXED_TYPE(
64 PurpleAccountOption,
65 purple_account_option,
66 purple_account_option_copy,
67 purple_account_option_destroy
70 PurpleAccountOption *
71 purple_account_option_new(PurplePrefType type, const char *text,
72 const char *pref_name)
74 PurpleAccountOption *option;
76 g_return_val_if_fail(type != PURPLE_PREF_NONE, NULL);
77 g_return_val_if_fail(text != NULL, NULL);
78 g_return_val_if_fail(pref_name != NULL, NULL);
80 option = g_new0(PurpleAccountOption, 1);
82 option->type = type;
83 option->text = g_strdup(text);
84 option->pref_name = g_strdup(pref_name);
86 return option;
89 PurpleAccountOption *
90 purple_account_option_copy(PurpleAccountOption *option) {
91 PurpleAccountOption *opt = NULL;
93 g_return_val_if_fail(option, NULL);
95 opt = g_new0(PurpleAccountOption, 1);
96 *opt = *option;
98 opt->text = g_strdup(option->text);
99 opt->pref_name = g_strdup(option->pref_name);
101 if(opt->type == PURPLE_PREF_STRING) {
102 opt->default_value.string = g_strdup(option->default_value.string);
103 opt->params.string.hints = g_slist_copy(option->params.string.hints);
106 return opt;
109 PurpleAccountOption *
110 purple_account_option_bool_new(const char *text, const char *pref_name,
111 gboolean default_value)
113 PurpleAccountOption *option;
115 option = purple_account_option_new(PURPLE_PREF_BOOLEAN, text, pref_name);
117 if (option == NULL)
118 return NULL;
120 option->default_value.boolean = default_value;
122 return option;
125 PurpleAccountOption *
126 purple_account_option_int_new(const char *text, const char *pref_name,
127 int default_value)
129 PurpleAccountOption *option;
131 option = purple_account_option_new(PURPLE_PREF_INT, text, pref_name);
133 if (option == NULL)
134 return NULL;
136 option->default_value.integer = default_value;
138 return option;
141 PurpleAccountOption *
142 purple_account_option_string_new(const char *text, const char *pref_name,
143 const char *default_value)
145 PurpleAccountOption *option;
147 option = purple_account_option_new(PURPLE_PREF_STRING, text, pref_name);
149 if (option == NULL)
150 return NULL;
152 option->default_value.string = g_strdup(default_value);
154 return option;
157 PurpleAccountOption *
158 purple_account_option_list_new(const char *text, const char *pref_name,
159 GList *list)
161 PurpleAccountOption *option;
163 option = purple_account_option_new(PURPLE_PREF_STRING_LIST, text, pref_name);
165 if (option == NULL)
166 return NULL;
168 option->default_value.list = list;
170 return option;
173 static void
174 purple_account_option_list_free(gpointer data, gpointer user_data)
176 PurpleKeyValuePair *kvp = data;
178 g_free(kvp->value);
179 g_free(kvp->key);
180 g_free(kvp);
183 void
184 purple_account_option_destroy(PurpleAccountOption *option)
186 g_return_if_fail(option != NULL);
188 g_free(option->text);
189 g_free(option->pref_name);
191 if (option->type == PURPLE_PREF_STRING)
193 g_free(option->default_value.string);
194 g_slist_free_full(option->params.string.hints, &g_free);
196 else if (option->type == PURPLE_PREF_STRING_LIST)
198 g_list_free_full(option->default_value.list,
199 (GDestroyNotify)purple_account_option_list_free);
202 g_free(option);
205 void
206 purple_account_option_set_default_bool(PurpleAccountOption *option,
207 gboolean value)
209 g_return_if_fail(option != NULL);
210 g_return_if_fail(option->type == PURPLE_PREF_BOOLEAN);
212 option->default_value.boolean = value;
215 void
216 purple_account_option_set_default_int(PurpleAccountOption *option, int value)
218 g_return_if_fail(option != NULL);
219 g_return_if_fail(option->type == PURPLE_PREF_INT);
221 option->default_value.integer = value;
224 void
225 purple_account_option_set_default_string(PurpleAccountOption *option,
226 const char *value)
228 g_return_if_fail(option != NULL);
229 g_return_if_fail(option->type == PURPLE_PREF_STRING);
231 g_free(option->default_value.string);
232 option->default_value.string = g_strdup(value);
235 void
236 purple_account_option_string_set_masked(PurpleAccountOption *option, gboolean masked)
238 g_return_if_fail(option != NULL);
239 g_return_if_fail(option->type == PURPLE_PREF_STRING);
241 option->params.string.masked = masked;
244 void
245 purple_account_option_string_set_hints(PurpleAccountOption *option, GSList *hints)
247 g_return_if_fail(option != NULL);
248 g_return_if_fail(option->type == PURPLE_PREF_STRING);
250 g_slist_free_full(option->params.string.hints, &g_free);
251 option->params.string.hints = hints;
254 void
255 purple_account_option_set_list(PurpleAccountOption *option, GList *values)
257 g_return_if_fail(option != NULL);
258 g_return_if_fail(option->type == PURPLE_PREF_STRING_LIST);
260 g_list_free_full(option->default_value.list,
261 (GDestroyNotify)purple_account_option_list_free);
263 option->default_value.list = values;
266 void
267 purple_account_option_add_list_item(PurpleAccountOption *option,
268 const char *key, const char *value)
270 PurpleKeyValuePair *kvp;
272 g_return_if_fail(option != NULL);
273 g_return_if_fail(key != NULL);
274 g_return_if_fail(value != NULL);
275 g_return_if_fail(option->type == PURPLE_PREF_STRING_LIST);
277 kvp = g_new0(PurpleKeyValuePair, 1);
278 kvp->key = g_strdup(key);
279 kvp->value = g_strdup(value);
281 option->default_value.list = g_list_append(option->default_value.list,
282 kvp);
285 PurplePrefType
286 purple_account_option_get_pref_type(const PurpleAccountOption *option)
288 g_return_val_if_fail(option != NULL, PURPLE_PREF_NONE);
290 return option->type;
293 const char *
294 purple_account_option_get_text(const PurpleAccountOption *option)
296 g_return_val_if_fail(option != NULL, NULL);
298 return option->text;
301 const char *
302 purple_account_option_get_setting(const PurpleAccountOption *option)
304 g_return_val_if_fail(option != NULL, NULL);
306 return option->pref_name;
309 gboolean
310 purple_account_option_get_default_bool(const PurpleAccountOption *option)
312 g_return_val_if_fail(option != NULL, FALSE);
313 g_return_val_if_fail(option->type == PURPLE_PREF_BOOLEAN, FALSE);
315 return option->default_value.boolean;
319 purple_account_option_get_default_int(const PurpleAccountOption *option)
321 g_return_val_if_fail(option != NULL, -1);
322 g_return_val_if_fail(option->type == PURPLE_PREF_INT, -1);
324 return option->default_value.integer;
327 const char *
328 purple_account_option_get_default_string(const PurpleAccountOption *option)
330 g_return_val_if_fail(option != NULL, NULL);
331 g_return_val_if_fail(option->type == PURPLE_PREF_STRING, NULL);
333 return option->default_value.string;
336 const char *
337 purple_account_option_get_default_list_value(const PurpleAccountOption *option)
339 PurpleKeyValuePair *kvp;
341 g_return_val_if_fail(option != NULL, NULL);
342 g_return_val_if_fail(option->type == PURPLE_PREF_STRING_LIST, NULL);
344 if (option->default_value.list == NULL)
345 return NULL;
347 kvp = option->default_value.list->data;
349 return (kvp ? kvp->value : NULL);
352 gboolean
353 purple_account_option_string_get_masked(const PurpleAccountOption *option)
355 g_return_val_if_fail(option != NULL, FALSE);
356 g_return_val_if_fail(option->type == PURPLE_PREF_STRING, FALSE);
358 return option->params.string.masked;
361 const GSList *
362 purple_account_option_string_get_hints(const PurpleAccountOption *option)
364 g_return_val_if_fail(option != NULL, NULL);
365 g_return_val_if_fail(option->type == PURPLE_PREF_STRING, NULL);
367 return option->params.string.hints;
370 GList *
371 purple_account_option_get_list(const PurpleAccountOption *option)
373 g_return_val_if_fail(option != NULL, NULL);
374 g_return_val_if_fail(option->type == PURPLE_PREF_STRING_LIST, NULL);
376 return option->default_value.list;