Merged in default (pull request #594)
[pidgin-git.git] / libpurple / pluginpref.c
blob80beb25f1c685ac4342f4823d0dd86d3f7b512fc
1 /**
2 * purple
4 * Purple is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
26 #include <glib.h>
28 #include "debug.h"
29 #include "internal.h"
30 #include "pluginpref.h"
31 #include "prefs.h"
33 struct _PurplePluginPrefFrame
35 GList *prefs;
38 struct _PurplePluginPref
40 char *name;
41 char *label;
43 PurplePluginPrefType type;
45 int min;
46 int max;
47 GList *choices;
48 unsigned int max_length;
49 gboolean masked;
50 PurpleStringFormatType format;
53 PurplePluginPrefFrame *
54 purple_plugin_pref_frame_new()
56 PurplePluginPrefFrame *frame;
58 frame = g_new0(PurplePluginPrefFrame, 1);
60 return frame;
63 void
64 purple_plugin_pref_frame_destroy(PurplePluginPrefFrame *frame)
66 g_return_if_fail(frame != NULL);
68 g_list_free_full(frame->prefs, (GDestroyNotify)purple_plugin_pref_destroy);
69 g_free(frame);
72 void
73 purple_plugin_pref_frame_add(PurplePluginPrefFrame *frame, PurplePluginPref *pref)
75 g_return_if_fail(frame != NULL);
76 g_return_if_fail(pref != NULL);
78 frame->prefs = g_list_append(frame->prefs, pref);
81 GList *
82 purple_plugin_pref_frame_get_prefs(PurplePluginPrefFrame *frame)
84 g_return_val_if_fail(frame != NULL, NULL);
85 g_return_val_if_fail(frame->prefs != NULL, NULL);
87 return frame->prefs;
90 PurplePluginPref *
91 purple_plugin_pref_new()
93 PurplePluginPref *pref;
95 pref = g_new0(PurplePluginPref, 1);
97 return pref;
100 PurplePluginPref *
101 purple_plugin_pref_new_with_name(const char *name)
103 PurplePluginPref *pref;
105 g_return_val_if_fail(name != NULL, NULL);
107 pref = g_new0(PurplePluginPref, 1);
108 pref->name = g_strdup(name);
110 return pref;
113 PurplePluginPref *
114 purple_plugin_pref_new_with_label(const char *label)
116 PurplePluginPref *pref;
118 g_return_val_if_fail(label != NULL, NULL);
120 pref = g_new0(PurplePluginPref, 1);
121 pref->label = g_strdup(label);
123 return pref;
126 PurplePluginPref *
127 purple_plugin_pref_new_with_name_and_label(const char *name, const char *label)
129 PurplePluginPref *pref;
131 g_return_val_if_fail(name != NULL, NULL);
132 g_return_val_if_fail(label != NULL, NULL);
134 pref = g_new0(PurplePluginPref, 1);
135 pref->name = g_strdup(name);
136 pref->label = g_strdup(label);
138 return pref;
141 void
142 purple_plugin_pref_destroy(PurplePluginPref *pref)
144 GList *tmp;
145 g_return_if_fail(pref != NULL);
147 g_free(pref->name);
148 g_free(pref->label);
149 tmp = pref->choices;
150 while(tmp) {
151 g_free(tmp->data);
152 /* Remove the string, and the data entries */
153 tmp = g_list_delete_link(tmp, tmp);
154 tmp = g_list_delete_link(tmp, tmp);
156 g_free(pref);
159 void
160 purple_plugin_pref_set_name(PurplePluginPref *pref, const char *name)
162 g_return_if_fail(pref != NULL);
163 g_return_if_fail(name != NULL);
165 g_free(pref->name);
166 pref->name = g_strdup(name);
169 const char *
170 purple_plugin_pref_get_name(PurplePluginPref *pref)
172 g_return_val_if_fail(pref != NULL, NULL);
174 return pref->name;
177 void
178 purple_plugin_pref_set_label(PurplePluginPref *pref, const char *label)
180 g_return_if_fail(pref != NULL);
181 g_return_if_fail(label != NULL);
183 g_free(pref->label);
184 pref->label = g_strdup(label);
187 const char *
188 purple_plugin_pref_get_label(PurplePluginPref *pref)
190 g_return_val_if_fail(pref != NULL, NULL);
192 return pref->label;
195 void
196 purple_plugin_pref_set_bounds(PurplePluginPref *pref, int min, int max)
198 int tmp;
200 g_return_if_fail(pref != NULL);
201 g_return_if_fail(pref->name != NULL);
203 if (purple_prefs_get_pref_type(pref->name) != PURPLE_PREF_INT)
205 purple_debug_warning("pluginpref",
206 "purple_plugin_pref_set_bounds: %s is not an integer pref\n",
207 pref->name);
208 return;
211 if (min > max)
213 tmp = min;
214 min = max;
215 max = tmp;
218 pref->min = min;
219 pref->max = max;
222 void purple_plugin_pref_get_bounds(PurplePluginPref *pref, int *min, int *max)
224 g_return_if_fail(pref != NULL);
225 g_return_if_fail(pref->name != NULL);
227 if (purple_prefs_get_pref_type(pref->name) != PURPLE_PREF_INT)
229 purple_debug_warning("pluginpref",
230 "purple_plugin_pref_get_bounds: %s is not an integer pref\n",
231 pref->name);
232 return;
235 *min = pref->min;
236 *max = pref->max;
239 void
240 purple_plugin_pref_set_pref_type(PurplePluginPref *pref, PurplePluginPrefType type)
242 g_return_if_fail(pref != NULL);
244 pref->type = type;
247 PurplePluginPrefType
248 purple_plugin_pref_get_pref_type(PurplePluginPref *pref)
250 g_return_val_if_fail(pref != NULL, PURPLE_PLUGIN_PREF_NONE);
252 return pref->type;
255 void
256 purple_plugin_pref_add_choice(PurplePluginPref *pref, const char *label, gpointer choice)
258 g_return_if_fail(pref != NULL);
259 g_return_if_fail(label != NULL);
260 g_return_if_fail(choice || purple_prefs_get_pref_type(pref->name) == PURPLE_PREF_INT);
262 pref->choices = g_list_append(pref->choices, g_strdup(label));
263 pref->choices = g_list_append(pref->choices, choice);
266 GList *
267 purple_plugin_pref_get_choices(PurplePluginPref *pref)
269 g_return_val_if_fail(pref != NULL, NULL);
271 return pref->choices;
274 void
275 purple_plugin_pref_set_max_length(PurplePluginPref *pref, unsigned int max_length)
277 g_return_if_fail(pref != NULL);
279 pref->max_length = max_length;
282 unsigned int
283 purple_plugin_pref_get_max_length(PurplePluginPref *pref)
285 g_return_val_if_fail(pref != NULL, 0);
287 return pref->max_length;
290 void
291 purple_plugin_pref_set_masked(PurplePluginPref *pref, gboolean masked)
293 g_return_if_fail(pref != NULL);
295 pref->masked = masked;
298 gboolean
299 purple_plugin_pref_get_masked(PurplePluginPref *pref)
301 g_return_val_if_fail(pref != NULL, FALSE);
303 return pref->masked;
306 void
307 purple_plugin_pref_set_format_type(PurplePluginPref *pref, PurpleStringFormatType format)
309 g_return_if_fail(pref != NULL);
310 g_return_if_fail(pref->type == PURPLE_PLUGIN_PREF_STRING_FORMAT);
312 pref->format = format;
315 PurpleStringFormatType
316 purple_plugin_pref_get_format_type(PurplePluginPref *pref)
318 g_return_val_if_fail(pref != NULL, 0);
320 if (pref->type != PURPLE_PLUGIN_PREF_STRING_FORMAT)
321 return PURPLE_STRING_FORMAT_TYPE_NONE;
323 return pref->format;