1 /**
2  * @file gntprefs.c GNT Preferences API
3  * @ingroup finch
4  */
5 
6 /* finch
7  *
8  * Finch is the legal property of its developers, whose names are too numerous
9  * to list here.  Please refer to the COPYRIGHT file distributed with this
10  * source distribution.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
25  */
26 #include "finch.h"
27 #include <internal.h>
28 
29 #include <prefs.h>
30 #include <savedstatuses.h>
31 
32 #include "gntprefs.h"
33 #include "gntrequest.h"
34 
35 #include "gnt.h"
36 #include "gntwidget.h"
37 
38 #include <string.h>
39 
40 static struct {
41 	GList *freestrings;  /* strings to be freed when the pref-window is closed */
42 	gboolean showing;
43 	GntWidget *window;
44 } pref_request;
45 
finch_prefs_init()46 void finch_prefs_init()
47 {
48 	purple_prefs_add_none("/finch");
49 
50 	purple_prefs_add_none("/finch/plugins");
51 	purple_prefs_add_path_list("/finch/plugins/loaded", NULL);
52 	purple_prefs_add_path_list("/finch/plugins/seen", NULL);
53 
54 	purple_prefs_add_none("/finch/conversations");
55 	purple_prefs_add_bool("/finch/conversations/timestamps", TRUE);
56 	purple_prefs_add_bool("/finch/conversations/notify_typing", FALSE);
57 
58 	purple_prefs_add_none("/finch/filelocations");
59 	purple_prefs_add_path("/finch/filelocations/last_save_folder", "");
60 	purple_prefs_add_path("/finch/filelocations/last_save_folder", "");
61 }
62 
finch_prefs_update_old()63 void finch_prefs_update_old()
64 {
65 	const char *str = NULL;
66 
67 	purple_prefs_rename("/gaim/gnt", "/finch");
68 	purple_prefs_rename("/purple/gnt", "/finch");
69 
70 	if ((str = purple_prefs_get_string("/purple/away/idle_reporting")) &&
71 			purple_strequal(str, "gaim"))
72 		purple_prefs_set_string("/purple/away/idle_reporting", "purple");
73 }
74 
75 typedef struct
76 {
77 	PurplePrefType type;
78 	const char *pref;
79 	const char *label;
80 	GList *(*lv)(void);   /* If the value is to be selected from a number of choices */
81 } Prefs;
82 
83 static GList *
get_log_options(void)84 get_log_options(void)
85 {
86 	return purple_log_logger_get_options();
87 }
88 
89 static GList *
get_idle_options(void)90 get_idle_options(void)
91 {
92 	GList *list = NULL;
93 	list = g_list_append(list, (char *)_("Based on keyboard use"));
94 	list = g_list_append(list, "system");
95 	list = g_list_append(list, (char*)_("From last sent message"));
96 	list = g_list_append(list, "purple");
97 	list = g_list_append(list, (char*)_("Never"));
98 	list = g_list_append(list, "never");
99 	return list;
100 }
101 
102 static GList *
get_status_titles(void)103 get_status_titles(void)
104 {
105 	GList *list = NULL;
106 	GList *iter;
107 	for (iter = purple_savedstatuses_get_all(); iter; iter = iter->next) {
108 		char *str;
109 		if (purple_savedstatus_is_transient(iter->data))
110 			continue;
111 		str = g_strdup_printf("%ld", purple_savedstatus_get_creation_time(iter->data));
112 		list = g_list_append(list, (char*)purple_savedstatus_get_title(iter->data));
113 		list = g_list_append(list, str);
114 		pref_request.freestrings = g_list_prepend(pref_request.freestrings, str);
115 	}
116 	return list;
117 }
118 
119 static PurpleRequestField *
get_pref_field(Prefs * prefs)120 get_pref_field(Prefs *prefs)
121 {
122 	PurpleRequestField *field = NULL;
123 
124 	if (prefs->lv == NULL)
125 	{
126 		switch (prefs->type)
127 		{
128 			case PURPLE_PREF_BOOLEAN:
129 				field = purple_request_field_bool_new(prefs->pref, _(prefs->label),
130 						purple_prefs_get_bool(prefs->pref));
131 				break;
132 			case PURPLE_PREF_INT:
133 				field = purple_request_field_int_new(prefs->pref, _(prefs->label),
134 						purple_prefs_get_int(prefs->pref));
135 				break;
136 			case PURPLE_PREF_STRING:
137 				field = purple_request_field_string_new(prefs->pref, _(prefs->label),
138 						purple_prefs_get_string(prefs->pref), FALSE);
139 				break;
140 			default:
141 				break;
142 		}
143 	}
144 	else
145 	{
146 		GList *list = prefs->lv(), *iter;
147 		if (list)
148 			field = purple_request_field_list_new(prefs->pref, _(prefs->label));
149 		for (iter = list; iter; iter = iter->next)
150 		{
151 			gboolean select = FALSE;
152 			const char *data = iter->data;
153 			int idata;
154 			iter = iter->next;
155 			switch (prefs->type)
156 			{
157 				case PURPLE_PREF_BOOLEAN:
158 					if (sscanf(iter->data, "%d", &idata) != 1)
159 						idata = FALSE;
160 					if (purple_prefs_get_bool(prefs->pref) == idata)
161 						select = TRUE;
162 					break;
163 				case PURPLE_PREF_INT:
164 					if (sscanf(iter->data, "%d", &idata) != 1)
165 						idata = 0;
166 					if (purple_prefs_get_int(prefs->pref) == idata)
167 						select = TRUE;
168 					break;
169 				case PURPLE_PREF_STRING:
170 					if (purple_strequal(purple_prefs_get_string(prefs->pref), iter->data))
171 						select = TRUE;
172 					break;
173 				default:
174 					break;
175 			}
176 			purple_request_field_list_add_icon(field, data, NULL, iter->data);
177 			if (select)
178 				purple_request_field_list_add_selected(field, data);
179 		}
180 		g_list_free(list);
181 	}
182 	return field;
183 }
184 
185 static Prefs blist[] =
186 {
187 	{PURPLE_PREF_BOOLEAN, "/finch/blist/idletime", N_("Show Idle Time"), NULL},
188 	{PURPLE_PREF_BOOLEAN, "/finch/blist/showoffline", N_("Show Offline Buddies"), NULL},
189 	{PURPLE_PREF_NONE, NULL, NULL, NULL}
190 };
191 
192 static Prefs convs[] =
193 {
194 	{PURPLE_PREF_BOOLEAN, "/finch/conversations/timestamps", N_("Show Timestamps"), NULL},
195 	{PURPLE_PREF_BOOLEAN, "/finch/conversations/notify_typing", N_("Notify buddies when you are typing"), NULL},
196 	{PURPLE_PREF_NONE, NULL, NULL, NULL}
197 };
198 
199 static Prefs logging[] =
200 {
201 	{PURPLE_PREF_STRING, "/purple/logging/format", N_("Log format"), get_log_options},
202 	{PURPLE_PREF_BOOLEAN, "/purple/logging/log_ims", N_("Log IMs"), NULL},
203 	{PURPLE_PREF_BOOLEAN, "/purple/logging/log_chats", N_("Log chats"), NULL},
204 	{PURPLE_PREF_BOOLEAN, "/purple/logging/log_system", N_("Log status change events"), NULL},
205 	{PURPLE_PREF_NONE, NULL, NULL, NULL},
206 };
207 
208 static Prefs idle[] =
209 {
210 	{PURPLE_PREF_STRING, "/purple/away/idle_reporting", N_("Report Idle time"), get_idle_options},
211 	{PURPLE_PREF_BOOLEAN, "/purple/away/away_when_idle", N_("Change status when idle"), NULL},
212 	{PURPLE_PREF_INT, "/purple/away/mins_before_away", N_("Minutes before changing status"), NULL},
213 	{PURPLE_PREF_INT, "/purple/savedstatus/idleaway", N_("Change status to"), get_status_titles},
214 	{PURPLE_PREF_NONE, NULL, NULL, NULL},
215 };
216 
217 static void
free_strings(void)218 free_strings(void)
219 {
220 	g_list_foreach(pref_request.freestrings, (GFunc)g_free, NULL);
221 	g_list_free(pref_request.freestrings);
222 	pref_request.freestrings = NULL;
223 	pref_request.showing = FALSE;
224 }
225 
226 static void
save_cb(void * data,PurpleRequestFields * allfields)227 save_cb(void *data, PurpleRequestFields *allfields)
228 {
229 	finch_request_save_in_prefs(data, allfields);
230 	free_strings();
231 }
232 
233 static void
add_pref_group(PurpleRequestFields * fields,const char * title,Prefs * prefs)234 add_pref_group(PurpleRequestFields *fields, const char *title, Prefs *prefs)
235 {
236 	PurpleRequestField *field;
237 	PurpleRequestFieldGroup *group;
238 	int i;
239 
240 	group = purple_request_field_group_new(title);
241 	purple_request_fields_add_group(fields, group);
242 	for (i = 0; prefs[i].pref; i++)
243 	{
244 		field = get_pref_field(prefs + i);
245 		if (field)
246 			purple_request_field_group_add_field(group, field);
247 	}
248 }
249 
finch_prefs_show_all()250 void finch_prefs_show_all()
251 {
252 	PurpleRequestFields *fields;
253 
254 	if (pref_request.showing) {
255 		gnt_window_present(pref_request.window);
256 		return;
257 	}
258 
259 	fields = purple_request_fields_new();
260 
261 	add_pref_group(fields, _("Buddy List"), blist);
262 	add_pref_group(fields, _("Conversations"), convs);
263 	add_pref_group(fields, _("Logging"), logging);
264 	add_pref_group(fields, _("Idle"), idle);
265 
266 	pref_request.showing = TRUE;
267 	pref_request.window = purple_request_fields(NULL, _("Preferences"), NULL, NULL, fields,
268 			_("Save"), G_CALLBACK(save_cb), _("Cancel"), free_strings,
269 			NULL, NULL, NULL,
270 			NULL);
271 }
272 
273