Remove useless comparison
[pidgin-git.git] / libpurple / accountopt.c
blob726f385f5efa27ae849cff1054ba039766114a2d
1 /**
2 * @file accountopt.c Account Options API
3 * @ingroup core
4 */
6 /* purple
8 * Purple is the legal property of its developers, whose names are too numerous
9 * to list here. Please refer to the COPYRIGHT file distributed with this
10 * source distribution.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 #include "internal.h"
28 #include "accountopt.h"
29 #include "util.h"
31 PurpleAccountOption *
32 purple_account_option_new(PurplePrefType type, const char *text,
33 const char *pref_name)
35 PurpleAccountOption *option;
37 g_return_val_if_fail(type != PURPLE_PREF_NONE, NULL);
38 g_return_val_if_fail(text != NULL, NULL);
39 g_return_val_if_fail(pref_name != NULL, NULL);
41 option = g_new0(PurpleAccountOption, 1);
43 option->type = type;
44 option->text = g_strdup(text);
45 option->pref_name = g_strdup(pref_name);
47 return option;
50 PurpleAccountOption *
51 purple_account_option_bool_new(const char *text, const char *pref_name,
52 gboolean default_value)
54 PurpleAccountOption *option;
56 option = purple_account_option_new(PURPLE_PREF_BOOLEAN, text, pref_name);
58 if (option == NULL)
59 return NULL;
61 option->default_value.boolean = default_value;
63 return option;
66 PurpleAccountOption *
67 purple_account_option_int_new(const char *text, const char *pref_name,
68 int default_value)
70 PurpleAccountOption *option;
72 option = purple_account_option_new(PURPLE_PREF_INT, text, pref_name);
74 if (option == NULL)
75 return NULL;
77 option->default_value.integer = default_value;
79 return option;
82 PurpleAccountOption *
83 purple_account_option_string_new(const char *text, const char *pref_name,
84 const char *default_value)
86 PurpleAccountOption *option;
88 option = purple_account_option_new(PURPLE_PREF_STRING, text, pref_name);
90 if (option == NULL)
91 return NULL;
93 option->default_value.string = g_strdup(default_value);
95 return option;
98 PurpleAccountOption *
99 purple_account_option_list_new(const char *text, const char *pref_name,
100 GList *list)
102 PurpleAccountOption *option;
104 option = purple_account_option_new(PURPLE_PREF_STRING_LIST, text, pref_name);
106 if (option == NULL)
107 return NULL;
109 option->default_value.list = list;
111 return option;
114 static void
115 purple_account_option_list_free(gpointer data, gpointer user_data)
117 PurpleKeyValuePair *kvp = data;
119 g_free(kvp->value);
120 g_free(kvp->key);
121 g_free(kvp);
124 void
125 purple_account_option_destroy(PurpleAccountOption *option)
127 g_return_if_fail(option != NULL);
129 g_free(option->text);
130 g_free(option->pref_name);
132 if (option->type == PURPLE_PREF_STRING)
134 g_free(option->default_value.string);
136 else if (option->type == PURPLE_PREF_STRING_LIST)
138 if (option->default_value.list != NULL)
140 g_list_foreach(option->default_value.list, purple_account_option_list_free, NULL);
141 g_list_free(option->default_value.list);
145 g_free(option);
148 void
149 purple_account_option_set_default_bool(PurpleAccountOption *option,
150 gboolean value)
152 g_return_if_fail(option != NULL);
153 g_return_if_fail(option->type == PURPLE_PREF_BOOLEAN);
155 option->default_value.boolean = value;
158 void
159 purple_account_option_set_default_int(PurpleAccountOption *option, int value)
161 g_return_if_fail(option != NULL);
162 g_return_if_fail(option->type == PURPLE_PREF_INT);
164 option->default_value.integer = value;
167 void
168 purple_account_option_set_default_string(PurpleAccountOption *option,
169 const char *value)
171 g_return_if_fail(option != NULL);
172 g_return_if_fail(option->type == PURPLE_PREF_STRING);
174 g_free(option->default_value.string);
175 option->default_value.string = g_strdup(value);
178 void
179 purple_account_option_set_masked(PurpleAccountOption *option, gboolean masked)
181 g_return_if_fail(option != NULL);
182 g_return_if_fail(option->type == PURPLE_PREF_STRING);
184 option->masked = masked;
188 void
189 purple_account_option_set_list(PurpleAccountOption *option, GList *values)
191 g_return_if_fail(option != NULL);
192 g_return_if_fail(option->type == PURPLE_PREF_STRING_LIST);
194 if (option->default_value.list != NULL)
196 g_list_foreach(option->default_value.list, purple_account_option_list_free, NULL);
197 g_list_free(option->default_value.list);
200 option->default_value.list = values;
203 void
204 purple_account_option_add_list_item(PurpleAccountOption *option,
205 const char *key, const char *value)
207 PurpleKeyValuePair *kvp;
209 g_return_if_fail(option != NULL);
210 g_return_if_fail(key != NULL);
211 g_return_if_fail(value != NULL);
212 g_return_if_fail(option->type == PURPLE_PREF_STRING_LIST);
214 kvp = g_new0(PurpleKeyValuePair, 1);
215 kvp->key = g_strdup(key);
216 kvp->value = g_strdup(value);
218 option->default_value.list = g_list_append(option->default_value.list,
219 kvp);
222 PurplePrefType
223 purple_account_option_get_type(const PurpleAccountOption *option)
225 g_return_val_if_fail(option != NULL, PURPLE_PREF_NONE);
227 return option->type;
230 const char *
231 purple_account_option_get_text(const PurpleAccountOption *option)
233 g_return_val_if_fail(option != NULL, NULL);
235 return option->text;
238 const char *
239 purple_account_option_get_setting(const PurpleAccountOption *option)
241 g_return_val_if_fail(option != NULL, NULL);
243 return option->pref_name;
246 gboolean
247 purple_account_option_get_default_bool(const PurpleAccountOption *option)
249 g_return_val_if_fail(option != NULL, FALSE);
250 g_return_val_if_fail(option->type == PURPLE_PREF_BOOLEAN, FALSE);
252 return option->default_value.boolean;
256 purple_account_option_get_default_int(const PurpleAccountOption *option)
258 g_return_val_if_fail(option != NULL, -1);
259 g_return_val_if_fail(option->type == PURPLE_PREF_INT, -1);
261 return option->default_value.integer;
264 const char *
265 purple_account_option_get_default_string(const PurpleAccountOption *option)
267 g_return_val_if_fail(option != NULL, NULL);
268 g_return_val_if_fail(option->type == PURPLE_PREF_STRING, NULL);
270 return option->default_value.string;
273 const char *
274 purple_account_option_get_default_list_value(const PurpleAccountOption *option)
276 PurpleKeyValuePair *kvp;
278 g_return_val_if_fail(option != NULL, NULL);
279 g_return_val_if_fail(option->type == PURPLE_PREF_STRING_LIST, NULL);
281 if (option->default_value.list == NULL)
282 return NULL;
284 kvp = option->default_value.list->data;
286 return (kvp ? kvp->value : NULL);
289 gboolean
290 purple_account_option_get_masked(const PurpleAccountOption *option)
292 g_return_val_if_fail(option != NULL, FALSE);
293 g_return_val_if_fail(option->type == PURPLE_PREF_STRING, FALSE);
295 return option->masked;
298 GList *
299 purple_account_option_get_list(const PurpleAccountOption *option)
301 g_return_val_if_fail(option != NULL, NULL);
302 g_return_val_if_fail(option->type == PURPLE_PREF_STRING_LIST, NULL);
304 return option->default_value.list;
307 /**************************************************************************
308 * Account User Split API
309 **************************************************************************/
310 PurpleAccountUserSplit *
311 purple_account_user_split_new(const char *text, const char *default_value,
312 char sep)
314 PurpleAccountUserSplit *split;
316 g_return_val_if_fail(text != NULL, NULL);
317 g_return_val_if_fail(sep != 0, NULL);
319 split = g_new0(PurpleAccountUserSplit, 1);
321 split->text = g_strdup(text);
322 split->field_sep = sep;
323 split->default_value = g_strdup(default_value);
324 split->reverse = TRUE;
326 return split;
329 void
330 purple_account_user_split_destroy(PurpleAccountUserSplit *split)
332 g_return_if_fail(split != NULL);
334 g_free(split->text);
335 g_free(split->default_value);
336 g_free(split);
339 const char *
340 purple_account_user_split_get_text(const PurpleAccountUserSplit *split)
342 g_return_val_if_fail(split != NULL, NULL);
344 return split->text;
347 const char *
348 purple_account_user_split_get_default_value(const PurpleAccountUserSplit *split)
350 g_return_val_if_fail(split != NULL, NULL);
352 return split->default_value;
355 char
356 purple_account_user_split_get_separator(const PurpleAccountUserSplit *split)
358 g_return_val_if_fail(split != NULL, 0);
360 return split->field_sep;
363 gboolean
364 purple_account_user_split_get_reverse(const PurpleAccountUserSplit *split)
366 g_return_val_if_fail(split != NULL, FALSE);
368 return split->reverse;
371 void
372 purple_account_user_split_set_reverse(PurpleAccountUserSplit *split, gboolean reverse)
374 g_return_if_fail(split != NULL);
376 split->reverse = reverse;