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
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
23 #include "accountopt.h"
25 #include "glibcompat.h"
28 * An option for an account.
30 * This is set by protocols, and appears in the account settings
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. */
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. */
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 */
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
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);
93 option
->text
= g_strdup(text
);
94 option
->pref_name
= g_strdup(pref_name
);
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
);
110 option
->default_value
.boolean
= default_value
;
115 PurpleAccountOption
*
116 purple_account_option_int_new(const char *text
, const char *pref_name
,
119 PurpleAccountOption
*option
;
121 option
= purple_account_option_new(PURPLE_PREF_INT
, text
, pref_name
);
126 option
->default_value
.integer
= default_value
;
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
);
142 option
->default_value
.string
= g_strdup(default_value
);
147 PurpleAccountOption
*
148 purple_account_option_list_new(const char *text
, const char *pref_name
,
151 PurpleAccountOption
*option
;
153 option
= purple_account_option_new(PURPLE_PREF_STRING_LIST
, text
, pref_name
);
158 option
->default_value
.list
= list
;
164 purple_account_option_list_free(gpointer data
, gpointer user_data
)
166 PurpleKeyValuePair
*kvp
= data
;
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
);
196 purple_account_option_set_default_bool(PurpleAccountOption
*option
,
199 g_return_if_fail(option
!= NULL
);
200 g_return_if_fail(option
->type
== PURPLE_PREF_BOOLEAN
);
202 option
->default_value
.boolean
= value
;
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
;
215 purple_account_option_set_default_string(PurpleAccountOption
*option
,
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
);
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
;
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
;
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
;
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
,
276 purple_account_option_get_pref_type(const PurpleAccountOption
*option
)
278 g_return_val_if_fail(option
!= NULL
, PURPLE_PREF_NONE
);
284 purple_account_option_get_text(const PurpleAccountOption
*option
)
286 g_return_val_if_fail(option
!= NULL
, NULL
);
292 purple_account_option_get_setting(const PurpleAccountOption
*option
)
294 g_return_val_if_fail(option
!= NULL
, NULL
);
296 return option
->pref_name
;
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
;
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
;
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
)
337 kvp
= option
->default_value
.list
->data
;
339 return (kvp
? kvp
->value
: NULL
);
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
;
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
;
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
,
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
;
392 purple_account_user_split_destroy(PurpleAccountUserSplit
*split
)
394 g_return_if_fail(split
!= NULL
);
397 g_free(split
->default_value
);
402 purple_account_user_split_get_text(const PurpleAccountUserSplit
*split
)
404 g_return_val_if_fail(split
!= NULL
, NULL
);
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
;
418 purple_account_user_split_get_separator(const PurpleAccountUserSplit
*split
)
420 g_return_val_if_fail(split
!= NULL
, 0);
422 return split
->field_sep
;
426 purple_account_user_split_get_reverse(const PurpleAccountUserSplit
*split
)
428 g_return_val_if_fail(split
!= NULL
, FALSE
);
430 return split
->reverse
;
434 purple_account_user_split_set_reverse(PurpleAccountUserSplit
*split
, gboolean reverse
)
436 g_return_if_fail(split
!= NULL
);
438 split
->reverse
= reverse
;
442 purple_account_user_split_is_constant(const PurpleAccountUserSplit
*split
)
444 g_return_val_if_fail(split
!= NULL
, FALSE
);
446 return split
->constant
;
450 purple_account_user_split_set_constant(PurpleAccountUserSplit
*split
,
453 g_return_if_fail(split
!= NULL
);
455 split
->constant
= constant
;