Merged pidgin/main into default
[pidgin-git.git] / finch / gntprefs.c
blob742ddd667f275046685ba9783002c27e57c71bba
1 /* finch
3 * Finch 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
5 * source distribution.
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
21 #include "finch.h"
22 #include <internal.h>
24 #include <prefs.h>
25 #include <savedstatuses.h>
27 #include "gntprefs.h"
28 #include "gntrequest.h"
30 #include "gnt.h"
31 #include "gntwidget.h"
33 #include <string.h>
35 static struct {
36 GList *freestrings; /* strings to be freed when the pref-window is closed */
37 gboolean showing;
38 GntWidget *window;
39 GntWidget *keyring_window;
40 } pref_request;
42 void finch_prefs_init()
44 purple_prefs_add_none("/finch");
46 purple_prefs_add_none("/finch/plugins");
47 purple_prefs_add_path_list("/finch/plugins/loaded", NULL);
48 purple_prefs_add_path_list("/finch/plugins/seen", NULL);
50 purple_prefs_add_none("/finch/conversations");
51 purple_prefs_add_bool("/finch/conversations/timestamps", TRUE);
52 purple_prefs_add_bool("/finch/conversations/notify_typing", FALSE);
54 purple_prefs_add_none("/finch/filelocations");
55 purple_prefs_add_path("/finch/filelocations/last_save_folder", "");
56 purple_prefs_add_path("/finch/filelocations/last_save_folder", "");
59 void finch_prefs_update_old()
63 typedef struct
65 PurplePrefType type;
66 const char *pref;
67 const char *label;
68 GList *(*lv)(void); /* If the value is to be selected from a number of choices */
69 } Prefs;
71 static GList *
72 get_log_options(void)
74 return purple_log_logger_get_options();
77 static GList *
78 get_idle_options(void)
80 GList *list = NULL;
81 list = g_list_append(list, (char *)_("Based on keyboard use"));
82 list = g_list_append(list, "system");
83 list = g_list_append(list, (char*)_("From last sent message"));
84 list = g_list_append(list, "purple");
85 list = g_list_append(list, (char*)_("Never"));
86 list = g_list_append(list, "never");
87 return list;
90 static GList *
91 get_status_titles(void)
93 GList *list = NULL;
94 GList *iter;
95 for (iter = purple_savedstatuses_get_all(); iter; iter = iter->next) {
96 char *str;
97 if (purple_savedstatus_is_transient(iter->data))
98 continue;
99 str = g_strdup_printf("%ld", purple_savedstatus_get_creation_time(iter->data));
100 list = g_list_append(list, (char*)purple_savedstatus_get_title(iter->data));
101 list = g_list_append(list, str);
102 pref_request.freestrings = g_list_prepend(pref_request.freestrings, str);
104 return list;
107 static PurpleRequestField *
108 get_pref_field(Prefs *prefs)
110 PurpleRequestField *field = NULL;
112 if (prefs->lv == NULL)
114 switch (prefs->type)
116 case PURPLE_PREF_BOOLEAN:
117 field = purple_request_field_bool_new(prefs->pref, _(prefs->label),
118 purple_prefs_get_bool(prefs->pref));
119 break;
120 case PURPLE_PREF_INT:
121 field = purple_request_field_int_new(prefs->pref, _(prefs->label),
122 purple_prefs_get_int(prefs->pref), INT_MIN, INT_MAX);
123 break;
124 case PURPLE_PREF_STRING:
125 field = purple_request_field_string_new(prefs->pref, _(prefs->label),
126 purple_prefs_get_string(prefs->pref), FALSE);
127 break;
128 default:
129 break;
132 else
134 GList *list = prefs->lv(), *iter;
135 if (list)
136 field = purple_request_field_list_new(prefs->pref, _(prefs->label));
137 for (iter = list; iter; iter = iter->next)
139 gboolean select = FALSE;
140 const char *data = iter->data;
141 int idata;
142 iter = iter->next;
143 switch (prefs->type)
145 case PURPLE_PREF_BOOLEAN:
146 if (sscanf(iter->data, "%d", &idata) != 1)
147 idata = FALSE;
148 if (purple_prefs_get_bool(prefs->pref) == idata)
149 select = TRUE;
150 break;
151 case PURPLE_PREF_INT:
152 if (sscanf(iter->data, "%d", &idata) != 1)
153 idata = 0;
154 if (purple_prefs_get_int(prefs->pref) == idata)
155 select = TRUE;
156 break;
157 case PURPLE_PREF_STRING:
158 if (purple_strequal(purple_prefs_get_string(prefs->pref), iter->data))
159 select = TRUE;
160 break;
161 default:
162 break;
164 purple_request_field_list_add_icon(field, data, NULL, iter->data);
165 if (select)
166 purple_request_field_list_add_selected(field, data);
168 g_list_free(list);
170 return field;
173 static Prefs blist[] =
175 {PURPLE_PREF_BOOLEAN, "/finch/blist/idletime", N_("Show Idle Time"), NULL},
176 {PURPLE_PREF_BOOLEAN, "/finch/blist/showoffline", N_("Show Offline Buddies"), NULL},
177 {PURPLE_PREF_NONE, NULL, NULL, NULL}
180 static Prefs convs[] =
182 {PURPLE_PREF_BOOLEAN, "/finch/conversations/timestamps", N_("Show Timestamps"), NULL},
183 {PURPLE_PREF_BOOLEAN, "/finch/conversations/notify_typing", N_("Notify buddies when you are typing"), NULL},
184 {PURPLE_PREF_NONE, NULL, NULL, NULL}
187 static Prefs logging[] =
189 {PURPLE_PREF_STRING, "/purple/logging/format", N_("Log format"), get_log_options},
190 {PURPLE_PREF_BOOLEAN, "/purple/logging/log_ims", N_("Log IMs"), NULL},
191 {PURPLE_PREF_BOOLEAN, "/purple/logging/log_chats", N_("Log chats"), NULL},
192 {PURPLE_PREF_BOOLEAN, "/purple/logging/log_system", N_("Log status change events"), NULL},
193 {PURPLE_PREF_NONE, NULL, NULL, NULL},
196 static Prefs keyring[] =
198 {PURPLE_PREF_STRING, "/purple/keyring/active", N_("Active keyring"), purple_keyring_get_options},
199 {PURPLE_PREF_NONE, NULL, NULL, NULL}
202 static Prefs idle[] =
204 {PURPLE_PREF_STRING, "/purple/away/idle_reporting", N_("Report Idle time"), get_idle_options},
205 {PURPLE_PREF_BOOLEAN, "/purple/away/away_when_idle", N_("Change status when idle"), NULL},
206 {PURPLE_PREF_INT, "/purple/away/mins_before_away", N_("Minutes before changing status"), NULL},
207 {PURPLE_PREF_INT, "/purple/savedstatus/idleaway", N_("Change status to"), get_status_titles},
208 {PURPLE_PREF_NONE, NULL, NULL, NULL},
211 static void
212 free_strings(void)
214 g_list_foreach(pref_request.freestrings, (GFunc)g_free, NULL);
215 g_list_free(pref_request.freestrings);
216 pref_request.freestrings = NULL;
217 pref_request.showing = FALSE;
220 static void
221 save_cb(void *data, PurpleRequestFields *allfields)
223 finch_request_save_in_prefs(data, allfields);
224 free_strings();
227 static void
228 add_pref_group(PurpleRequestFields *fields, const char *title, Prefs *prefs)
230 PurpleRequestField *field;
231 PurpleRequestFieldGroup *group;
232 int i;
234 group = purple_request_field_group_new(title);
235 purple_request_fields_add_group(fields, group);
236 for (i = 0; prefs[i].pref; i++)
238 field = get_pref_field(prefs + i);
239 if (field)
240 purple_request_field_group_add_field(group, field);
244 void finch_prefs_show_all()
246 PurpleRequestFields *fields;
248 if (pref_request.showing) {
249 gnt_window_present(pref_request.window);
250 return;
253 if (pref_request.keyring_window != NULL)
254 purple_request_close(PURPLE_REQUEST_FIELDS,
255 pref_request.keyring_window);
257 fields = purple_request_fields_new();
259 add_pref_group(fields, _("Buddy List"), blist);
260 add_pref_group(fields, _("Conversations"), convs);
261 add_pref_group(fields, _("Keyring"), keyring);
262 add_pref_group(fields, _("Logging"), logging);
263 add_pref_group(fields, _("Idle"), idle);
265 pref_request.showing = TRUE;
266 pref_request.window = purple_request_fields(NULL, _("Preferences"), NULL, NULL, fields,
267 _("Save"), G_CALLBACK(save_cb), _("Cancel"), free_strings,
268 NULL, NULL);
271 static void
272 finch_prefs_keyring_save(void *data, PurpleRequestFields *fields)
274 pref_request.keyring_window = NULL;
276 purple_keyring_apply_settings(NULL, fields);
279 static void
280 finch_prefs_keyring_cancel(void)
282 pref_request.keyring_window = NULL;
285 void finch_prefs_show_keyring(void)
287 PurpleRequestFields *fields;
289 if (pref_request.keyring_window != NULL) {
290 gnt_window_present(pref_request.keyring_window);
291 return;
294 fields = purple_keyring_read_settings();
295 if (fields == NULL) {
296 purple_notify_info(NULL, _("Keyring settings"),
297 _("Selected keyring doesn't allow any configuration"),
298 NULL, NULL);
299 return;
302 pref_request.keyring_window = purple_request_fields(NULL,
303 _("Keyring settings"), NULL, NULL, fields,
304 _("Save"), G_CALLBACK(finch_prefs_keyring_save),
305 _("Cancel"), G_CALLBACK(finch_prefs_keyring_cancel),
306 NULL, NULL);