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 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
);
199 purple_account_option_set_default_bool(PurpleAccountOption
*option
,
202 g_return_if_fail(option
!= NULL
);
203 g_return_if_fail(option
->type
== PURPLE_PREF_BOOLEAN
);
205 option
->default_value
.boolean
= value
;
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
;
218 purple_account_option_set_default_string(PurpleAccountOption
*option
,
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
);
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
;
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
;
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
;
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
,
282 purple_account_option_get_pref_type(const PurpleAccountOption
*option
)
284 g_return_val_if_fail(option
!= NULL
, PURPLE_PREF_NONE
);
290 purple_account_option_get_text(const PurpleAccountOption
*option
)
292 g_return_val_if_fail(option
!= NULL
, NULL
);
298 purple_account_option_get_setting(const PurpleAccountOption
*option
)
300 g_return_val_if_fail(option
!= NULL
, NULL
);
302 return option
->pref_name
;
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
;
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
;
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
)
343 kvp
= option
->default_value
.list
->data
;
345 return (kvp
? kvp
->value
: NULL
);
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
;
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
;
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
,
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
;
398 purple_account_user_split_destroy(PurpleAccountUserSplit
*split
)
400 g_return_if_fail(split
!= NULL
);
403 g_free(split
->default_value
);
408 purple_account_user_split_get_text(const PurpleAccountUserSplit
*split
)
410 g_return_val_if_fail(split
!= NULL
, NULL
);
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
;
424 purple_account_user_split_get_separator(const PurpleAccountUserSplit
*split
)
426 g_return_val_if_fail(split
!= NULL
, 0);
428 return split
->field_sep
;
432 purple_account_user_split_get_reverse(const PurpleAccountUserSplit
*split
)
434 g_return_val_if_fail(split
!= NULL
, FALSE
);
436 return split
->reverse
;
440 purple_account_user_split_set_reverse(PurpleAccountUserSplit
*split
, gboolean reverse
)
442 g_return_if_fail(split
!= NULL
);
444 split
->reverse
= reverse
;
448 purple_account_user_split_is_constant(const PurpleAccountUserSplit
*split
)
450 g_return_val_if_fail(split
!= NULL
, FALSE
);
452 return split
->constant
;
456 purple_account_user_split_set_constant(PurpleAccountUserSplit
*split
,
459 g_return_if_fail(split
!= NULL
);
461 split
->constant
= constant
;