1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24 
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 
28 #include "defs.h"
29 #include "folder.h"
30 #include "utils.h"
31 #include "prefs_gtk.h"
32 #include "filtering.h"
33 #include "folder_item_prefs.h"
34 #include "prefs_migration.h"
35 
36 FolderItemPrefs tmp_prefs;
37 
38 static PrefParam param[] = {
39 	{"enable_default_to", "", &tmp_prefs.enable_default_to, P_BOOL,
40 	 NULL, NULL, NULL},
41 	{"default_to", "", &tmp_prefs.default_to, P_STRING,
42 	 NULL, NULL, NULL},
43 	{"enable_default_reply_to", "", &tmp_prefs.enable_default_reply_to, P_BOOL,
44 	 NULL, NULL, NULL},
45 	{"default_reply_to", "", &tmp_prefs.default_reply_to, P_STRING,
46 	 NULL, NULL, NULL},
47 	{"enable_default_cc", "", &tmp_prefs.enable_default_cc, P_BOOL,
48 	 NULL, NULL, NULL},
49 	{"default_cc", "", &tmp_prefs.default_cc, P_STRING,
50 	 NULL, NULL, NULL},
51 	{"enable_default_bcc", "", &tmp_prefs.enable_default_bcc, P_BOOL,
52 	 NULL, NULL, NULL},
53 	{"default_bcc", "", &tmp_prefs.default_bcc, P_STRING,
54 	 NULL, NULL, NULL},
55 	{"enable_default_replyto", "", &tmp_prefs.enable_default_replyto, P_BOOL,
56 	 NULL, NULL, NULL},
57 	{"default_replyto", "", &tmp_prefs.default_replyto, P_STRING,
58 	 NULL, NULL, NULL},
59 	{"enable_simplify_subject", "", &tmp_prefs.enable_simplify_subject, P_BOOL,
60 	 NULL, NULL, NULL},
61 	{"simplify_subject_regexp", "", &tmp_prefs.simplify_subject_regexp, P_STRING,
62 	 NULL, NULL, NULL},
63 	{"enable_folder_chmod", "", &tmp_prefs.enable_folder_chmod, P_BOOL,
64 	 NULL, NULL, NULL},
65 	{"folder_chmod", "", &tmp_prefs.folder_chmod, P_INT,
66 	 NULL, NULL, NULL},
67 	{"enable_default_account", "", &tmp_prefs.enable_default_account, P_BOOL,
68 	 NULL, NULL, NULL},
69 	{"default_account", NULL, &tmp_prefs.default_account, P_INT,
70 	 NULL, NULL, NULL},
71 #if USE_ENCHANT
72 	{"enable_default_dictionary", "", &tmp_prefs.enable_default_dictionary, P_BOOL,
73 	 NULL, NULL, NULL},
74 	{"default_dictionary", NULL, &tmp_prefs.default_dictionary, P_STRING,
75 	 NULL, NULL, NULL},
76 	{"enable_default_alt_dictionary", "", &tmp_prefs.enable_default_alt_dictionary, P_BOOL,
77 	 NULL, NULL, NULL},
78 	{"default_alt_dictionary", NULL, &tmp_prefs.default_alt_dictionary, P_STRING,
79 	 NULL, NULL, NULL},
80 #endif
81 	{"always_sign", "0", &tmp_prefs.always_sign, P_ENUM,
82  	 NULL, NULL, NULL},
83 	{"always_encrypt", "0", &tmp_prefs.always_encrypt, P_ENUM,
84  	 NULL, NULL, NULL},
85 	{"save_copy_to_folder", NULL, &tmp_prefs.save_copy_to_folder, P_BOOL,
86 	 NULL, NULL, NULL},
87 	{"folder_color", "#000000", &tmp_prefs.color, P_COLOR,
88 	 NULL, NULL, NULL},
89 	{"enable_processing", "FALSE", &tmp_prefs.enable_processing, P_BOOL,
90 	 NULL, NULL, NULL},
91 	{"enable_processing_when_opening", "TRUE", &tmp_prefs.enable_processing_when_opening, P_BOOL,
92 	 NULL, NULL, NULL},
93 	{"newmailcheck", "TRUE", &tmp_prefs.newmailcheck, P_BOOL,
94 	 NULL, NULL, NULL},
95 	{"offlinesync", "FALSE", &tmp_prefs.offlinesync, P_BOOL,
96 	 NULL, NULL, NULL},
97 	{"offlinesync_days", "0", &tmp_prefs.offlinesync_days, P_INT,
98 	 NULL, NULL, NULL},
99 	{"remove_old_bodies", "FALSE", &tmp_prefs.remove_old_bodies, P_BOOL,
100 	 NULL, NULL, NULL},
101 	{"promote_html_part", "0", &tmp_prefs.promote_html_part, P_ENUM,
102 	 NULL, NULL, NULL},
103 
104 	{"compose_with_format", "FALSE", &tmp_prefs.compose_with_format, P_BOOL,
105 	 NULL, NULL, NULL},
106 	{"compose_override_from_format", NULL, &tmp_prefs.compose_override_from_format, P_STRING,
107 	 NULL, NULL, NULL},
108 	{"compose_subject_format", NULL, &tmp_prefs.compose_subject_format, P_STRING,
109 	 NULL, NULL, NULL},
110 	{"compose_body_format", NULL, &tmp_prefs.compose_body_format, P_STRING,
111 	 NULL, NULL, NULL},
112 	{"reply_with_format", "FALSE", &tmp_prefs.reply_with_format, P_BOOL,
113 	 NULL, NULL, NULL},
114 	{"reply_override_from_format", NULL, &tmp_prefs.reply_override_from_format, P_STRING,
115 	 NULL, NULL, NULL},
116 	{"reply_quotemark", NULL, &tmp_prefs.reply_quotemark, P_STRING,
117 	 NULL, NULL, NULL},
118 	{"reply_body_format", NULL, &tmp_prefs.reply_body_format, P_STRING,
119 	 NULL, NULL, NULL},
120 	{"forward_with_format", "FALSE", &tmp_prefs.forward_with_format, P_BOOL,
121 	 NULL, NULL, NULL},
122 	{"forward_override_from_format", NULL, &tmp_prefs.forward_override_from_format, P_STRING,
123 	 NULL, NULL, NULL},
124 	{"forward_quotemark", NULL, &tmp_prefs.forward_quotemark, P_STRING,
125 	 NULL, NULL, NULL},
126 	{"forward_body_format", NULL, &tmp_prefs.forward_body_format, P_STRING,
127 	 NULL, NULL, NULL},
128 	{"config_version", "-1", &tmp_prefs.config_version, P_INT,
129 	 NULL, NULL, NULL},
130 	{NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
131 };
132 
133 static FolderItemPrefs *folder_item_prefs_clear(FolderItemPrefs *prefs);
134 
folder_item_prefs_read_config(FolderItem * item)135 void folder_item_prefs_read_config(FolderItem * item)
136 {
137 	gchar * id;
138 	gchar *rcpath;
139 
140 	id = folder_item_get_identifier(item);
141 	folder_item_prefs_clear(&tmp_prefs);
142 	rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, FOLDERITEM_RC, NULL);
143 	prefs_read_config(param, id, rcpath, NULL);
144 	g_free(id);
145 	g_free(rcpath);
146 
147 	*item->prefs = tmp_prefs;
148 
149 	prefs_update_config_version_folder_item(item);
150 }
151 
folder_item_prefs_save_config(FolderItem * item)152 void folder_item_prefs_save_config(FolderItem * item)
153 {
154 	gchar * id;
155 
156 	tmp_prefs = * item->prefs;
157 
158 	id = folder_item_get_identifier(item);
159 	if (id == NULL)
160 		return;
161 
162 	debug_print("saving prefs for %s\n", id);
163 	prefs_write_config(param, id, FOLDERITEM_RC);
164 	g_free(id);
165 }
166 
folder_item_prefs_save_config_func(GNode * node,gpointer data)167 static gboolean folder_item_prefs_save_config_func(GNode *node, gpointer data)
168 {
169 	FolderItem *item = (FolderItem *) node->data;
170 	folder_item_prefs_save_config(item);
171 	return FALSE;
172 }
173 
folder_item_prefs_save_config_recursive(FolderItem * item)174 void folder_item_prefs_save_config_recursive(FolderItem * item)
175 {
176 	g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL,
177 			-1, folder_item_prefs_save_config_func, NULL);
178 }
179 
folder_prefs_save_config_recursive(Folder * folder)180 void folder_prefs_save_config_recursive(Folder *folder)
181 {
182 	g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
183 			-1, folder_item_prefs_save_config_func, NULL);
184 }
185 
folder_item_prefs_clear(FolderItemPrefs * prefs)186 static FolderItemPrefs *folder_item_prefs_clear(FolderItemPrefs *prefs)
187 {
188 	prefs->enable_default_to = FALSE;
189 	prefs->default_to = NULL;
190 	prefs->enable_default_reply_to = FALSE;
191 	prefs->default_reply_to = NULL;
192 	prefs->enable_default_cc = FALSE;
193 	prefs->default_cc = NULL;
194 	prefs->enable_default_bcc = FALSE;
195 	prefs->default_bcc = NULL;
196 	prefs->enable_default_replyto = FALSE;
197 	prefs->default_replyto = NULL;
198 	prefs->enable_simplify_subject = FALSE;
199 	prefs->simplify_subject_regexp = NULL;
200 	prefs->enable_folder_chmod = FALSE;
201 	prefs->folder_chmod = 0;
202 	prefs->enable_default_account = FALSE;
203 	prefs->default_account = 0;
204 #if USE_ENCHANT
205 	prefs->enable_default_dictionary = FALSE;
206 	prefs->default_dictionary = NULL;
207 	prefs->enable_default_alt_dictionary = FALSE;
208 	prefs->default_alt_dictionary = NULL;
209 #endif
210 	prefs->always_sign = SIGN_OR_ENCRYPT_DEFAULT;
211 	prefs->always_encrypt = SIGN_OR_ENCRYPT_DEFAULT;
212 	prefs->save_copy_to_folder = FALSE;
213 	prefs->color = 0;
214 
215 	prefs->enable_processing = FALSE;
216 	prefs->enable_processing_when_opening = FALSE;
217 	prefs->processing = NULL;
218 
219 	prefs->newmailcheck = TRUE;
220 	prefs->offlinesync = FALSE;
221 	prefs->offlinesync_days = 0;
222 	prefs->remove_old_bodies = FALSE;
223 	prefs->promote_html_part = HTML_PROMOTE_DEFAULT;
224 
225 	prefs->compose_with_format = FALSE;
226 	prefs->compose_subject_format = NULL;
227 	prefs->compose_body_format = NULL;
228 	prefs->compose_override_from_format = NULL;
229 	prefs->reply_with_format = FALSE;
230 	prefs->reply_quotemark = NULL;
231 	prefs->reply_body_format = NULL;
232 	prefs->reply_override_from_format = NULL;
233 	prefs->forward_with_format = FALSE;
234 	prefs->forward_quotemark = NULL;
235 	prefs->forward_body_format = NULL;
236 	prefs->forward_override_from_format = NULL;
237 	return prefs;
238 }
239 
folder_item_prefs_new(void)240 FolderItemPrefs * folder_item_prefs_new(void)
241 {
242 	FolderItemPrefs * prefs;
243 
244 	prefs = g_new0(FolderItemPrefs, 1);
245 
246 	return folder_item_prefs_clear(prefs);
247 }
248 
folder_item_prefs_free(FolderItemPrefs * prefs)249 void folder_item_prefs_free(FolderItemPrefs * prefs)
250 {
251 	g_free(prefs->default_to);
252 	g_free(prefs->default_reply_to);
253 	g_free(prefs->default_cc);
254 	g_free(prefs->default_bcc);
255 	g_free(prefs->default_replyto);
256 	g_free(prefs->compose_subject_format);
257 	g_free(prefs->compose_body_format);
258 	g_free(prefs->compose_override_from_format);
259 	g_free(prefs->reply_quotemark);
260 	g_free(prefs->reply_body_format);
261 	g_free(prefs->reply_override_from_format);
262 	g_free(prefs->forward_quotemark);
263 	g_free(prefs->forward_body_format);
264 	g_free(prefs->forward_override_from_format);
265 	g_free(prefs);
266 }
267 
268 #define SAFE_STRING(str) \
269 	(str) ? (str) : ""
270 
folder_item_prefs_copy_prefs(FolderItem * src,FolderItem * dest)271 void folder_item_prefs_copy_prefs(FolderItem * src, FolderItem * dest)
272 {
273 	GSList *tmp_prop_list = NULL, *tmp;
274 	folder_item_prefs_read_config(src);
275 
276 	tmp_prefs.directory			= g_strdup(src->prefs->directory);
277         tmp_prefs.enable_processing             = src->prefs->enable_processing;
278         tmp_prefs.enable_processing_when_opening = src->prefs->enable_processing_when_opening;
279 	tmp_prefs.newmailcheck                  = src->prefs->newmailcheck;
280 	tmp_prefs.offlinesync                   = src->prefs->offlinesync;
281 	tmp_prefs.offlinesync_days              = src->prefs->offlinesync_days;
282 	tmp_prefs.remove_old_bodies             = src->prefs->remove_old_bodies;
283 	tmp_prefs.promote_html_part             = src->prefs->promote_html_part;
284 
285 	prefs_matcher_read_config();
286 
287 	for (tmp = src->prefs->processing; tmp != NULL && tmp->data != NULL;) {
288 		FilteringProp *prop = (FilteringProp *)tmp->data;
289 
290 		tmp_prop_list = g_slist_append(tmp_prop_list,
291 					   filteringprop_copy(prop));
292 		tmp = tmp->next;
293 	}
294 	tmp_prefs.processing			= tmp_prop_list;
295 
296 	tmp_prefs.request_return_receipt	= src->prefs->request_return_receipt;
297 	tmp_prefs.enable_default_to		= src->prefs->enable_default_to;
298 	tmp_prefs.default_to			= g_strdup(src->prefs->default_to);
299 	tmp_prefs.enable_default_reply_to	= src->prefs->enable_default_reply_to;
300 	tmp_prefs.default_reply_to		= g_strdup(src->prefs->default_reply_to);
301 	tmp_prefs.enable_default_cc		= src->prefs->enable_default_cc;
302 	tmp_prefs.default_cc			= g_strdup(src->prefs->default_cc);
303 	tmp_prefs.enable_default_bcc		= src->prefs->enable_default_bcc;
304 	tmp_prefs.default_bcc			= g_strdup(src->prefs->default_bcc);
305 	tmp_prefs.enable_default_replyto		= src->prefs->enable_default_replyto;
306 	tmp_prefs.default_replyto			= g_strdup(src->prefs->default_replyto);
307 	tmp_prefs.enable_simplify_subject	= src->prefs->enable_simplify_subject;
308 	tmp_prefs.simplify_subject_regexp	= g_strdup(src->prefs->simplify_subject_regexp);
309 	tmp_prefs.enable_folder_chmod		= src->prefs->enable_folder_chmod;
310 	tmp_prefs.folder_chmod			= src->prefs->folder_chmod;
311 	tmp_prefs.enable_default_account	= src->prefs->enable_default_account;
312 	tmp_prefs.default_account		= src->prefs->default_account;
313 #if USE_ENCHANT
314 	tmp_prefs.enable_default_dictionary	= src->prefs->enable_default_dictionary;
315 	tmp_prefs.default_dictionary		= g_strdup(src->prefs->default_dictionary);
316 	tmp_prefs.enable_default_alt_dictionary	= src->prefs->enable_default_alt_dictionary;
317 	tmp_prefs.default_alt_dictionary	= g_strdup(src->prefs->default_alt_dictionary);
318 #endif
319 	tmp_prefs.always_sign    	= src->prefs->always_sign;
320 	tmp_prefs.always_encrypt    = src->prefs->always_encrypt;
321 	tmp_prefs.save_copy_to_folder		= src->prefs->save_copy_to_folder;
322 	tmp_prefs.color				= src->prefs->color;
323 
324 	tmp_prefs.compose_with_format = src->prefs->compose_with_format;
325 	tmp_prefs.compose_subject_format = g_strdup(src->prefs->compose_subject_format);
326 	tmp_prefs.compose_body_format = g_strdup(src->prefs->compose_body_format);
327 	tmp_prefs.compose_override_from_format = g_strdup(src->prefs->compose_override_from_format);
328 	tmp_prefs.reply_with_format = src->prefs->reply_with_format;
329 	tmp_prefs.reply_quotemark = g_strdup(src->prefs->reply_quotemark);
330 	tmp_prefs.reply_body_format = g_strdup(src->prefs->reply_body_format);
331 	tmp_prefs.reply_override_from_format = g_strdup(src->prefs->reply_override_from_format);
332 	tmp_prefs.forward_with_format = src->prefs->forward_with_format;
333 	tmp_prefs.forward_quotemark = g_strdup(src->prefs->forward_quotemark);
334 	tmp_prefs.forward_body_format = g_strdup(src->prefs->forward_body_format);
335 	tmp_prefs.forward_override_from_format = g_strdup(src->prefs->forward_override_from_format);
336 
337 	*dest->prefs = tmp_prefs;
338 	folder_item_prefs_save_config(dest);
339 	prefs_matcher_write_config();
340 
341 	dest->collapsed = src->collapsed;
342 	dest->thread_collapsed = src->thread_collapsed;
343 	dest->threaded  = src->threaded;
344 	dest->ret_rcpt  = src->ret_rcpt;
345 	dest->hide_read_msgs = src->hide_read_msgs;
346 	dest->hide_del_msgs = src->hide_del_msgs;
347 	dest->hide_read_threads = src->hide_read_threads;
348 	dest->sort_key  = src->sort_key;
349 	dest->sort_type = src->sort_type;
350 }
351