Add per-folder default cc, bcc and reply-to.
[claws.git] / src / folder_item_prefs.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2009 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 #endif
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26
27 #include "defs.h"
28 #include "folder.h"
29 #include "utils.h"
30 #include "prefs_gtk.h"
31 #include "filtering.h"
32 #include "folder_item_prefs.h"
33
34 FolderItemPrefs tmp_prefs;
35
36 static PrefParam param[] = {
37         {"enable_default_to", "", &tmp_prefs.enable_default_to, P_BOOL,
38          NULL, NULL, NULL},
39         {"default_to", "", &tmp_prefs.default_to, P_STRING,
40          NULL, NULL, NULL},
41         {"enable_default_reply_to", "", &tmp_prefs.enable_default_reply_to, P_BOOL,
42          NULL, NULL, NULL},
43         {"default_reply_to", "", &tmp_prefs.default_reply_to, P_STRING,
44          NULL, NULL, NULL},
45         {"enable_default_cc", "", &tmp_prefs.enable_default_cc, P_BOOL,
46          NULL, NULL, NULL},
47         {"default_cc", "", &tmp_prefs.default_cc, P_STRING,
48          NULL, NULL, NULL},
49         {"enable_default_bcc", "", &tmp_prefs.enable_default_bcc, P_BOOL,
50          NULL, NULL, NULL},
51         {"default_bcc", "", &tmp_prefs.default_bcc, P_STRING,
52          NULL, NULL, NULL},
53         {"enable_default_replyto", "", &tmp_prefs.enable_default_replyto, P_BOOL,
54          NULL, NULL, NULL},
55         {"default_replyto", "", &tmp_prefs.default_replyto, P_STRING,
56          NULL, NULL, NULL},
57         {"enable_simplify_subject", "", &tmp_prefs.enable_simplify_subject, P_BOOL,
58          NULL, NULL, NULL},
59 #ifndef G_OS_WIN32
60         {"simplify_subject_regexp", "", &tmp_prefs.simplify_subject_regexp, P_STRING,
61          NULL, NULL, NULL},
62 #endif
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         {"save_copy_to_folder", NULL, &tmp_prefs.save_copy_to_folder, P_BOOL,
82          NULL, NULL, NULL},
83         {"folder_color", "", &tmp_prefs.color, P_INT,
84          NULL, NULL, NULL},
85         {"enable_processing", "FALSE", &tmp_prefs.enable_processing, P_BOOL,
86          NULL, NULL, NULL},
87         {"enable_processing_when_opening", "TRUE", &tmp_prefs.enable_processing_when_opening, P_BOOL,
88          NULL, NULL, NULL},
89         {"newmailcheck", "TRUE", &tmp_prefs.newmailcheck, P_BOOL,
90          NULL, NULL, NULL},
91         {"offlinesync", "FALSE", &tmp_prefs.offlinesync, P_BOOL,
92          NULL, NULL, NULL},
93         {"offlinesync_days", "0", &tmp_prefs.offlinesync_days, P_INT,
94          NULL, NULL, NULL},
95         {"remove_old_bodies", "FALSE", &tmp_prefs.remove_old_bodies, P_BOOL,
96          NULL, NULL, NULL},
97
98         {"compose_with_format", "FALSE", &tmp_prefs.compose_with_format, P_BOOL,
99          NULL, NULL, NULL},
100         {"compose_override_from_format", NULL, &tmp_prefs.compose_override_from_format, P_STRING,
101          NULL, NULL, NULL},
102         {"compose_subject_format", NULL, &tmp_prefs.compose_subject_format, P_STRING,
103          NULL, NULL, NULL},
104         {"compose_body_format", NULL, &tmp_prefs.compose_body_format, P_STRING,
105          NULL, NULL, NULL},
106         {"reply_with_format", "FALSE", &tmp_prefs.reply_with_format, P_BOOL,
107          NULL, NULL, NULL},
108         {"reply_override_from_format", NULL, &tmp_prefs.reply_override_from_format, P_STRING,
109          NULL, NULL, NULL},
110         {"reply_quotemark", NULL, &tmp_prefs.reply_quotemark, P_STRING,
111          NULL, NULL, NULL},
112         {"reply_body_format", NULL, &tmp_prefs.reply_body_format, P_STRING,
113          NULL, NULL, NULL},
114         {"forward_with_format", "FALSE", &tmp_prefs.forward_with_format, P_BOOL,
115          NULL, NULL, NULL},
116         {"forward_override_from_format", NULL, &tmp_prefs.forward_override_from_format, P_STRING,
117          NULL, NULL, NULL},
118         {"forward_quotemark", NULL, &tmp_prefs.forward_quotemark, P_STRING,
119          NULL, NULL, NULL},
120         {"forward_body_format", NULL, &tmp_prefs.forward_body_format, P_STRING,
121          NULL, NULL, NULL},
122         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
123 };
124
125 static FolderItemPrefs *folder_item_prefs_clear(FolderItemPrefs *prefs);
126
127 void folder_item_prefs_read_config(FolderItem * item)
128 {
129         gchar * id;
130         gchar *rcpath;
131
132         id = folder_item_get_identifier(item);
133         folder_item_prefs_clear(&tmp_prefs);
134         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, FOLDERITEM_RC, NULL);
135         prefs_read_config(param, id, rcpath, NULL);
136         g_free(id);
137         g_free(rcpath);
138
139         *item->prefs = tmp_prefs;
140 }
141
142 void folder_item_prefs_save_config(FolderItem * item)
143 {       
144         gchar * id;
145
146         tmp_prefs = * item->prefs;
147
148         id = folder_item_get_identifier(item);
149         debug_print("saving prefs for %s\n", id?id:"(null)");
150         prefs_write_config(param, id, FOLDERITEM_RC);
151         g_free(id);
152 }
153
154 static gboolean folder_item_prefs_save_config_func(GNode *node, gpointer data)
155 {
156         FolderItem *item = (FolderItem *) node->data;
157         folder_item_prefs_save_config(item);
158         return FALSE;
159 }
160
161 void folder_item_prefs_save_config_recursive(FolderItem * item)
162 {       
163         g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL,
164                         -1, folder_item_prefs_save_config_func, NULL);
165 }
166
167 static FolderItemPrefs *folder_item_prefs_clear(FolderItemPrefs *prefs)
168 {
169         prefs->enable_default_to = FALSE;
170         prefs->default_to = NULL;
171         prefs->enable_default_reply_to = FALSE;
172         prefs->default_reply_to = NULL;
173         prefs->enable_default_cc = FALSE;
174         prefs->default_cc = NULL;
175         prefs->enable_default_bcc = FALSE;
176         prefs->default_bcc = NULL;
177         prefs->enable_default_replyto = FALSE;
178         prefs->default_replyto = NULL;
179         prefs->enable_simplify_subject = FALSE;
180 #ifndef G_OS_WIN32
181         prefs->simplify_subject_regexp = NULL;
182 #endif
183         prefs->enable_folder_chmod = FALSE;
184         prefs->folder_chmod = 0;
185         prefs->enable_default_account = FALSE;
186         prefs->default_account = 0;
187 #if USE_ENCHANT
188         prefs->enable_default_dictionary = FALSE;
189         prefs->default_dictionary = NULL;
190         prefs->enable_default_alt_dictionary = FALSE;
191         prefs->default_alt_dictionary = NULL;
192 #endif
193         prefs->save_copy_to_folder = FALSE;
194         prefs->color = 0;
195
196         prefs->enable_processing = TRUE;
197         prefs->enable_processing_when_opening = TRUE;
198         prefs->processing = NULL;
199
200         prefs->newmailcheck = TRUE;
201         prefs->offlinesync = FALSE;
202         prefs->offlinesync_days = 0;
203         prefs->remove_old_bodies = FALSE;
204
205         prefs->compose_with_format = FALSE;
206         prefs->compose_subject_format = NULL;
207         prefs->compose_body_format = NULL;
208         prefs->compose_override_from_format = NULL;
209         prefs->reply_with_format = FALSE;
210         prefs->reply_quotemark = NULL;
211         prefs->reply_body_format = NULL;
212         prefs->reply_override_from_format = NULL;
213         prefs->forward_with_format = FALSE;
214         prefs->forward_quotemark = NULL;
215         prefs->forward_body_format = NULL;
216         prefs->forward_override_from_format = NULL;
217         return prefs;
218 }
219
220 FolderItemPrefs * folder_item_prefs_new(void)
221 {
222         FolderItemPrefs * prefs;
223
224         prefs = g_new0(FolderItemPrefs, 1);
225
226         return folder_item_prefs_clear(prefs);
227 }
228
229 void folder_item_prefs_free(FolderItemPrefs * prefs)
230 {
231         g_free(prefs->default_to);
232         g_free(prefs->default_reply_to);
233         g_free(prefs->default_cc);
234         g_free(prefs->default_bcc);
235         g_free(prefs->default_replyto);
236         g_free(prefs->compose_subject_format);
237         g_free(prefs->compose_body_format);
238         g_free(prefs->compose_override_from_format);
239         g_free(prefs->reply_quotemark);
240         g_free(prefs->reply_body_format);
241         g_free(prefs->reply_override_from_format);
242         g_free(prefs->forward_quotemark);
243         g_free(prefs->forward_body_format);
244         g_free(prefs->forward_override_from_format);
245         g_free(prefs);
246 }
247
248 #define SAFE_STRING(str) \
249         (str) ? (str) : ""
250
251 void folder_item_prefs_copy_prefs(FolderItem * src, FolderItem * dest)
252 {
253         GSList *tmp_prop_list = NULL, *tmp;
254         folder_item_prefs_read_config(src);
255
256         tmp_prefs.directory                     = g_strdup(src->prefs->directory);
257         tmp_prefs.enable_processing             = src->prefs->enable_processing;
258         tmp_prefs.enable_processing_when_opening = src->prefs->enable_processing_when_opening;
259         tmp_prefs.newmailcheck                  = src->prefs->newmailcheck;
260         tmp_prefs.offlinesync                   = src->prefs->offlinesync;
261         tmp_prefs.offlinesync_days              = src->prefs->offlinesync_days;
262         tmp_prefs.remove_old_bodies             = src->prefs->remove_old_bodies;
263
264         prefs_matcher_read_config();
265
266         for (tmp = src->prefs->processing; tmp != NULL && tmp->data != NULL;) {
267                 FilteringProp *prop = (FilteringProp *)tmp->data;
268                 
269                 tmp_prop_list = g_slist_append(tmp_prop_list,
270                                            filteringprop_copy(prop));
271                 tmp = tmp->next;
272         }
273         tmp_prefs.processing                    = tmp_prop_list;
274         
275         tmp_prefs.request_return_receipt        = src->prefs->request_return_receipt;
276         tmp_prefs.enable_default_to             = src->prefs->enable_default_to;
277         tmp_prefs.default_to                    = g_strdup(src->prefs->default_to);
278         tmp_prefs.enable_default_reply_to       = src->prefs->enable_default_reply_to;
279         tmp_prefs.default_reply_to              = g_strdup(src->prefs->default_reply_to);
280         tmp_prefs.enable_default_cc             = src->prefs->enable_default_cc;
281         tmp_prefs.default_cc                    = g_strdup(src->prefs->default_cc);
282         tmp_prefs.enable_default_bcc            = src->prefs->enable_default_bcc;
283         tmp_prefs.default_bcc                   = g_strdup(src->prefs->default_bcc);
284         tmp_prefs.enable_default_replyto                = src->prefs->enable_default_replyto;
285         tmp_prefs.default_replyto                       = g_strdup(src->prefs->default_replyto);
286         tmp_prefs.enable_simplify_subject       = src->prefs->enable_simplify_subject;
287 #ifndef G_OS_WIN32
288         tmp_prefs.simplify_subject_regexp       = g_strdup(src->prefs->simplify_subject_regexp);
289 #endif
290         tmp_prefs.enable_folder_chmod           = src->prefs->enable_folder_chmod;
291         tmp_prefs.folder_chmod                  = src->prefs->folder_chmod;
292         tmp_prefs.enable_default_account        = src->prefs->enable_default_account;
293         tmp_prefs.default_account               = src->prefs->default_account;
294 #if USE_ENCHANT
295         tmp_prefs.enable_default_dictionary     = src->prefs->enable_default_dictionary;
296         tmp_prefs.default_dictionary            = g_strdup(src->prefs->default_dictionary);
297         tmp_prefs.enable_default_alt_dictionary = src->prefs->enable_default_alt_dictionary;
298         tmp_prefs.default_alt_dictionary        = g_strdup(src->prefs->default_alt_dictionary);
299 #endif
300         tmp_prefs.save_copy_to_folder           = src->prefs->save_copy_to_folder;
301         tmp_prefs.color                         = src->prefs->color;
302
303         tmp_prefs.compose_with_format = src->prefs->compose_with_format;
304         tmp_prefs.compose_subject_format = g_strdup(src->prefs->compose_subject_format);
305         tmp_prefs.compose_body_format = g_strdup(src->prefs->compose_body_format);
306         tmp_prefs.compose_override_from_format = g_strdup(src->prefs->compose_override_from_format);
307         tmp_prefs.reply_with_format = src->prefs->reply_with_format;
308         tmp_prefs.reply_quotemark = g_strdup(src->prefs->reply_quotemark);
309         tmp_prefs.reply_body_format = g_strdup(src->prefs->reply_body_format);
310         tmp_prefs.reply_override_from_format = g_strdup(src->prefs->reply_override_from_format);
311         tmp_prefs.forward_with_format = src->prefs->forward_with_format;
312         tmp_prefs.forward_quotemark = g_strdup(src->prefs->forward_quotemark);
313         tmp_prefs.forward_body_format = g_strdup(src->prefs->forward_body_format);
314         tmp_prefs.forward_override_from_format = g_strdup(src->prefs->forward_override_from_format);
315
316         *dest->prefs = tmp_prefs;
317         folder_item_prefs_save_config(dest);
318 }