950863abb3de87514d385fe89a83e15de07b70ca
[claws.git] / src / folder_item_prefs.c
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
35 FolderItemPrefs tmp_prefs;
36
37 static PrefParam param[] = {
38         {"enable_default_to", "", &tmp_prefs.enable_default_to, P_BOOL,
39          NULL, NULL, NULL},
40         {"default_to", "", &tmp_prefs.default_to, P_STRING,
41          NULL, NULL, NULL},
42         {"enable_default_reply_to", "", &tmp_prefs.enable_default_reply_to, P_BOOL,
43          NULL, NULL, NULL},
44         {"default_reply_to", "", &tmp_prefs.default_reply_to, P_STRING,
45          NULL, NULL, NULL},
46         {"enable_default_cc", "", &tmp_prefs.enable_default_cc, P_BOOL,
47          NULL, NULL, NULL},
48         {"default_cc", "", &tmp_prefs.default_cc, P_STRING,
49          NULL, NULL, NULL},
50         {"enable_default_bcc", "", &tmp_prefs.enable_default_bcc, P_BOOL,
51          NULL, NULL, NULL},
52         {"default_bcc", "", &tmp_prefs.default_bcc, P_STRING,
53          NULL, NULL, NULL},
54         {"enable_default_replyto", "", &tmp_prefs.enable_default_replyto, P_BOOL,
55          NULL, NULL, NULL},
56         {"default_replyto", "", &tmp_prefs.default_replyto, P_STRING,
57          NULL, NULL, NULL},
58         {"enable_simplify_subject", "", &tmp_prefs.enable_simplify_subject, P_BOOL,
59          NULL, NULL, NULL},
60 #ifndef G_OS_WIN32
61         {"simplify_subject_regexp", "", &tmp_prefs.simplify_subject_regexp, P_STRING,
62          NULL, NULL, NULL},
63 #endif
64         {"enable_folder_chmod", "", &tmp_prefs.enable_folder_chmod, P_BOOL,
65          NULL, NULL, NULL},
66         {"folder_chmod", "", &tmp_prefs.folder_chmod, P_INT,
67          NULL, NULL, NULL},
68         {"enable_default_account", "", &tmp_prefs.enable_default_account, P_BOOL,
69          NULL, NULL, NULL},
70         {"default_account", NULL, &tmp_prefs.default_account, P_INT,
71          NULL, NULL, NULL},
72 #if USE_ENCHANT
73         {"enable_default_dictionary", "", &tmp_prefs.enable_default_dictionary, P_BOOL,
74          NULL, NULL, NULL},
75         {"default_dictionary", NULL, &tmp_prefs.default_dictionary, P_STRING,
76          NULL, NULL, NULL},
77         {"enable_default_alt_dictionary", "", &tmp_prefs.enable_default_alt_dictionary, P_BOOL,
78          NULL, NULL, NULL},
79         {"default_alt_dictionary", NULL, &tmp_prefs.default_alt_dictionary, P_STRING,
80          NULL, NULL, NULL},
81 #endif   
82         {"save_copy_to_folder", NULL, &tmp_prefs.save_copy_to_folder, P_BOOL,
83          NULL, NULL, NULL},
84         {"folder_color", "", &tmp_prefs.color, P_INT,
85          NULL, NULL, NULL},
86         {"enable_processing", "FALSE", &tmp_prefs.enable_processing, P_BOOL,
87          NULL, NULL, NULL},
88         {"enable_processing_when_opening", "TRUE", &tmp_prefs.enable_processing_when_opening, P_BOOL,
89          NULL, NULL, NULL},
90         {"newmailcheck", "TRUE", &tmp_prefs.newmailcheck, P_BOOL,
91          NULL, NULL, NULL},
92         {"offlinesync", "FALSE", &tmp_prefs.offlinesync, P_BOOL,
93          NULL, NULL, NULL},
94         {"offlinesync_days", "0", &tmp_prefs.offlinesync_days, P_INT,
95          NULL, NULL, NULL},
96         {"remove_old_bodies", "FALSE", &tmp_prefs.remove_old_bodies, P_BOOL,
97          NULL, NULL, NULL},
98         {"promote_html_part", "0", &tmp_prefs.promote_html_part, P_ENUM,
99          NULL, NULL, NULL},
100
101         {"compose_with_format", "FALSE", &tmp_prefs.compose_with_format, P_BOOL,
102          NULL, NULL, NULL},
103         {"compose_override_from_format", NULL, &tmp_prefs.compose_override_from_format, P_STRING,
104          NULL, NULL, NULL},
105         {"compose_subject_format", NULL, &tmp_prefs.compose_subject_format, P_STRING,
106          NULL, NULL, NULL},
107         {"compose_body_format", NULL, &tmp_prefs.compose_body_format, P_STRING,
108          NULL, NULL, NULL},
109         {"reply_with_format", "FALSE", &tmp_prefs.reply_with_format, P_BOOL,
110          NULL, NULL, NULL},
111         {"reply_override_from_format", NULL, &tmp_prefs.reply_override_from_format, P_STRING,
112          NULL, NULL, NULL},
113         {"reply_quotemark", NULL, &tmp_prefs.reply_quotemark, P_STRING,
114          NULL, NULL, NULL},
115         {"reply_body_format", NULL, &tmp_prefs.reply_body_format, P_STRING,
116          NULL, NULL, NULL},
117         {"forward_with_format", "FALSE", &tmp_prefs.forward_with_format, P_BOOL,
118          NULL, NULL, NULL},
119         {"forward_override_from_format", NULL, &tmp_prefs.forward_override_from_format, P_STRING,
120          NULL, NULL, NULL},
121         {"forward_quotemark", NULL, &tmp_prefs.forward_quotemark, P_STRING,
122          NULL, NULL, NULL},
123         {"forward_body_format", NULL, &tmp_prefs.forward_body_format, P_STRING,
124          NULL, NULL, NULL},
125         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
126 };
127
128 static FolderItemPrefs *folder_item_prefs_clear(FolderItemPrefs *prefs);
129
130 void folder_item_prefs_read_config(FolderItem * item)
131 {
132         gchar * id;
133         gchar *rcpath;
134
135         id = folder_item_get_identifier(item);
136         folder_item_prefs_clear(&tmp_prefs);
137         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, FOLDERITEM_RC, NULL);
138         prefs_read_config(param, id, rcpath, NULL);
139         g_free(id);
140         g_free(rcpath);
141
142         *item->prefs = tmp_prefs;
143 }
144
145 void folder_item_prefs_save_config(FolderItem * item)
146 {       
147         gchar * id;
148
149         tmp_prefs = * item->prefs;
150
151         id = folder_item_get_identifier(item);
152         if (id == NULL)
153                 return;
154
155         debug_print("saving prefs for %s\n", id);
156         prefs_write_config(param, id, FOLDERITEM_RC);
157         g_free(id);
158 }
159
160 static gboolean folder_item_prefs_save_config_func(GNode *node, gpointer data)
161 {
162         FolderItem *item = (FolderItem *) node->data;
163         folder_item_prefs_save_config(item);
164         return FALSE;
165 }
166
167 void folder_item_prefs_save_config_recursive(FolderItem * item)
168 {       
169         g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL,
170                         -1, folder_item_prefs_save_config_func, NULL);
171 }
172
173 void folder_prefs_save_config_recursive(Folder *folder)
174 {       
175         g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
176                         -1, folder_item_prefs_save_config_func, NULL);
177 }
178
179 static FolderItemPrefs *folder_item_prefs_clear(FolderItemPrefs *prefs)
180 {
181         prefs->enable_default_to = FALSE;
182         prefs->default_to = NULL;
183         prefs->enable_default_reply_to = FALSE;
184         prefs->default_reply_to = NULL;
185         prefs->enable_default_cc = FALSE;
186         prefs->default_cc = NULL;
187         prefs->enable_default_bcc = FALSE;
188         prefs->default_bcc = NULL;
189         prefs->enable_default_replyto = FALSE;
190         prefs->default_replyto = NULL;
191         prefs->enable_simplify_subject = FALSE;
192 #ifndef G_OS_WIN32
193         prefs->simplify_subject_regexp = NULL;
194 #endif
195         prefs->enable_folder_chmod = FALSE;
196         prefs->folder_chmod = 0;
197         prefs->enable_default_account = FALSE;
198         prefs->default_account = 0;
199 #if USE_ENCHANT
200         prefs->enable_default_dictionary = FALSE;
201         prefs->default_dictionary = NULL;
202         prefs->enable_default_alt_dictionary = FALSE;
203         prefs->default_alt_dictionary = NULL;
204 #endif
205         prefs->save_copy_to_folder = FALSE;
206         prefs->color = 0;
207
208         prefs->enable_processing = TRUE;
209         prefs->enable_processing_when_opening = TRUE;
210         prefs->processing = NULL;
211
212         prefs->newmailcheck = TRUE;
213         prefs->offlinesync = FALSE;
214         prefs->offlinesync_days = 0;
215         prefs->remove_old_bodies = FALSE;
216         prefs->promote_html_part = HTML_PROMOTE_DEFAULT;
217
218         prefs->compose_with_format = FALSE;
219         prefs->compose_subject_format = NULL;
220         prefs->compose_body_format = NULL;
221         prefs->compose_override_from_format = NULL;
222         prefs->reply_with_format = FALSE;
223         prefs->reply_quotemark = NULL;
224         prefs->reply_body_format = NULL;
225         prefs->reply_override_from_format = NULL;
226         prefs->forward_with_format = FALSE;
227         prefs->forward_quotemark = NULL;
228         prefs->forward_body_format = NULL;
229         prefs->forward_override_from_format = NULL;
230         return prefs;
231 }
232
233 FolderItemPrefs * folder_item_prefs_new(void)
234 {
235         FolderItemPrefs * prefs;
236
237         prefs = g_new0(FolderItemPrefs, 1);
238
239         return folder_item_prefs_clear(prefs);
240 }
241
242 void folder_item_prefs_free(FolderItemPrefs * prefs)
243 {
244         g_free(prefs->default_to);
245         g_free(prefs->default_reply_to);
246         g_free(prefs->default_cc);
247         g_free(prefs->default_bcc);
248         g_free(prefs->default_replyto);
249         g_free(prefs->compose_subject_format);
250         g_free(prefs->compose_body_format);
251         g_free(prefs->compose_override_from_format);
252         g_free(prefs->reply_quotemark);
253         g_free(prefs->reply_body_format);
254         g_free(prefs->reply_override_from_format);
255         g_free(prefs->forward_quotemark);
256         g_free(prefs->forward_body_format);
257         g_free(prefs->forward_override_from_format);
258         g_free(prefs);
259 }
260
261 #define SAFE_STRING(str) \
262         (str) ? (str) : ""
263
264 void folder_item_prefs_copy_prefs(FolderItem * src, FolderItem * dest)
265 {
266         GSList *tmp_prop_list = NULL, *tmp;
267         folder_item_prefs_read_config(src);
268
269         tmp_prefs.directory                     = g_strdup(src->prefs->directory);
270         tmp_prefs.enable_processing             = src->prefs->enable_processing;
271         tmp_prefs.enable_processing_when_opening = src->prefs->enable_processing_when_opening;
272         tmp_prefs.newmailcheck                  = src->prefs->newmailcheck;
273         tmp_prefs.offlinesync                   = src->prefs->offlinesync;
274         tmp_prefs.offlinesync_days              = src->prefs->offlinesync_days;
275         tmp_prefs.remove_old_bodies             = src->prefs->remove_old_bodies;
276         tmp_prefs.promote_html_part             = src->prefs->promote_html_part;
277
278         prefs_matcher_read_config();
279
280         for (tmp = src->prefs->processing; tmp != NULL && tmp->data != NULL;) {
281                 FilteringProp *prop = (FilteringProp *)tmp->data;
282                 
283                 tmp_prop_list = g_slist_append(tmp_prop_list,
284                                            filteringprop_copy(prop));
285                 tmp = tmp->next;
286         }
287         tmp_prefs.processing                    = tmp_prop_list;
288         
289         tmp_prefs.request_return_receipt        = src->prefs->request_return_receipt;
290         tmp_prefs.enable_default_to             = src->prefs->enable_default_to;
291         tmp_prefs.default_to                    = g_strdup(src->prefs->default_to);
292         tmp_prefs.enable_default_reply_to       = src->prefs->enable_default_reply_to;
293         tmp_prefs.default_reply_to              = g_strdup(src->prefs->default_reply_to);
294         tmp_prefs.enable_default_cc             = src->prefs->enable_default_cc;
295         tmp_prefs.default_cc                    = g_strdup(src->prefs->default_cc);
296         tmp_prefs.enable_default_bcc            = src->prefs->enable_default_bcc;
297         tmp_prefs.default_bcc                   = g_strdup(src->prefs->default_bcc);
298         tmp_prefs.enable_default_replyto                = src->prefs->enable_default_replyto;
299         tmp_prefs.default_replyto                       = g_strdup(src->prefs->default_replyto);
300         tmp_prefs.enable_simplify_subject       = src->prefs->enable_simplify_subject;
301 #ifndef G_OS_WIN32
302         tmp_prefs.simplify_subject_regexp       = g_strdup(src->prefs->simplify_subject_regexp);
303 #endif
304         tmp_prefs.enable_folder_chmod           = src->prefs->enable_folder_chmod;
305         tmp_prefs.folder_chmod                  = src->prefs->folder_chmod;
306         tmp_prefs.enable_default_account        = src->prefs->enable_default_account;
307         tmp_prefs.default_account               = src->prefs->default_account;
308 #if USE_ENCHANT
309         tmp_prefs.enable_default_dictionary     = src->prefs->enable_default_dictionary;
310         tmp_prefs.default_dictionary            = g_strdup(src->prefs->default_dictionary);
311         tmp_prefs.enable_default_alt_dictionary = src->prefs->enable_default_alt_dictionary;
312         tmp_prefs.default_alt_dictionary        = g_strdup(src->prefs->default_alt_dictionary);
313 #endif
314         tmp_prefs.save_copy_to_folder           = src->prefs->save_copy_to_folder;
315         tmp_prefs.color                         = src->prefs->color;
316
317         tmp_prefs.compose_with_format = src->prefs->compose_with_format;
318         tmp_prefs.compose_subject_format = g_strdup(src->prefs->compose_subject_format);
319         tmp_prefs.compose_body_format = g_strdup(src->prefs->compose_body_format);
320         tmp_prefs.compose_override_from_format = g_strdup(src->prefs->compose_override_from_format);
321         tmp_prefs.reply_with_format = src->prefs->reply_with_format;
322         tmp_prefs.reply_quotemark = g_strdup(src->prefs->reply_quotemark);
323         tmp_prefs.reply_body_format = g_strdup(src->prefs->reply_body_format);
324         tmp_prefs.reply_override_from_format = g_strdup(src->prefs->reply_override_from_format);
325         tmp_prefs.forward_with_format = src->prefs->forward_with_format;
326         tmp_prefs.forward_quotemark = g_strdup(src->prefs->forward_quotemark);
327         tmp_prefs.forward_body_format = g_strdup(src->prefs->forward_body_format);
328         tmp_prefs.forward_override_from_format = g_strdup(src->prefs->forward_override_from_format);
329
330         *dest->prefs = tmp_prefs;
331         folder_item_prefs_save_config(dest);
332
333         dest->collapsed = src->collapsed;
334         dest->thread_collapsed = src->thread_collapsed;
335         dest->threaded  = src->threaded;
336         dest->ret_rcpt  = src->ret_rcpt;
337         dest->hide_read_msgs = src->hide_read_msgs;
338         dest->hide_del_msgs = src->hide_del_msgs;
339         dest->hide_read_threads = src->hide_read_threads;
340         dest->sort_key  = src->sort_key;
341         dest->sort_type = src->sort_type;
342
343 }