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 "purpleaccountoption.h"
26 #include "glibcompat.h"
29 * An option for an account.
31 * This is set by protocols, and appears in the account settings
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. */
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. */
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 */
65 purple_account_option
,
66 purple_account_option_copy
,
67 purple_account_option_destroy
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);
83 option
->text
= g_strdup(text
);
84 option
->pref_name
= g_strdup(pref_name
);
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);
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
);
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
);
120 option
->default_value
.boolean
= default_value
;
125 PurpleAccountOption
*
126 purple_account_option_int_new(const char *text
, const char *pref_name
,
129 PurpleAccountOption
*option
;
131 option
= purple_account_option_new(PURPLE_PREF_INT
, text
, pref_name
);
136 option
->default_value
.integer
= default_value
;
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
);
152 option
->default_value
.string
= g_strdup(default_value
);
157 PurpleAccountOption
*
158 purple_account_option_list_new(const char *text
, const char *pref_name
,
161 PurpleAccountOption
*option
;
163 option
= purple_account_option_new(PURPLE_PREF_STRING_LIST
, text
, pref_name
);
168 option
->default_value
.list
= list
;
174 purple_account_option_list_free(gpointer data
, gpointer user_data
)
176 PurpleKeyValuePair
*kvp
= data
;
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
);
206 purple_account_option_set_default_bool(PurpleAccountOption
*option
,
209 g_return_if_fail(option
!= NULL
);
210 g_return_if_fail(option
->type
== PURPLE_PREF_BOOLEAN
);
212 option
->default_value
.boolean
= value
;
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
;
225 purple_account_option_set_default_string(PurpleAccountOption
*option
,
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
);
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
;
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
;
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
;
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
,
286 purple_account_option_get_pref_type(const PurpleAccountOption
*option
)
288 g_return_val_if_fail(option
!= NULL
, PURPLE_PREF_NONE
);
294 purple_account_option_get_text(const PurpleAccountOption
*option
)
296 g_return_val_if_fail(option
!= NULL
, NULL
);
302 purple_account_option_get_setting(const PurpleAccountOption
*option
)
304 g_return_val_if_fail(option
!= NULL
, NULL
);
306 return option
->pref_name
;
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
;
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
;
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
)
347 kvp
= option
->default_value
.list
->data
;
349 return (kvp
? kvp
->value
: NULL
);
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
;
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
;
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
;