8cbc9a61b6e73734f3a222556bf193cfff511c85
[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_simplify_subject", "", &tmp_prefs.enable_simplify_subject, P_BOOL,
46          NULL, NULL, NULL},
47 #ifndef G_OS_WIN32
48         {"simplify_subject_regexp", "", &tmp_prefs.simplify_subject_regexp, P_STRING,
49          NULL, NULL, NULL},
50 #endif
51         {"enable_folder_chmod", "", &tmp_prefs.enable_folder_chmod, P_BOOL,
52          NULL, NULL, NULL},
53         {"folder_chmod", "", &tmp_prefs.folder_chmod, P_INT,
54          NULL, NULL, NULL},
55         {"enable_default_account", "", &tmp_prefs.enable_default_account, P_BOOL,
56          NULL, NULL, NULL},
57         {"default_account", NULL, &tmp_prefs.default_account, P_INT,
58          NULL, NULL, NULL},
59 #if USE_ENCHANT
60         {"enable_default_dictionary", "", &tmp_prefs.enable_default_dictionary, P_BOOL,
61          NULL, NULL, NULL},
62         {"default_dictionary", NULL, &tmp_prefs.default_dictionary, P_STRING,
63          NULL, NULL, NULL},
64         {"enable_default_alt_dictionary", "", &tmp_prefs.enable_default_alt_dictionary, P_BOOL,
65          NULL, NULL, NULL},
66         {"default_alt_dictionary", NULL, &tmp_prefs.default_alt_dictionary, P_STRING,
67          NULL, NULL, NULL},
68 #endif   
69         {"save_copy_to_folder", NULL, &tmp_prefs.save_copy_to_folder, P_BOOL,
70          NULL, NULL, NULL},
71         {"folder_color", "", &tmp_prefs.color, P_INT,
72          NULL, NULL, NULL},
73         {"enable_processing", "FALSE", &tmp_prefs.enable_processing, P_BOOL,
74          NULL, NULL, NULL},
75         {"enable_processing_when_opening", "TRUE", &tmp_prefs.enable_processing_when_opening, P_BOOL,
76          NULL, NULL, NULL},
77         {"newmailcheck", "TRUE", &tmp_prefs.newmailcheck, P_BOOL,
78          NULL, NULL, NULL},
79         {"offlinesync", "FALSE", &tmp_prefs.offlinesync, P_BOOL,
80          NULL, NULL, NULL},
81         {"offlinesync_days", "0", &tmp_prefs.offlinesync_days, P_INT,
82          NULL, NULL, NULL},
83         {"remove_old_bodies", "FALSE", &tmp_prefs.remove_old_bodies, P_BOOL,
84          NULL, NULL, NULL},
85
86         {"compose_with_format", "FALSE", &tmp_prefs.compose_with_format, P_BOOL,
87          NULL, NULL, NULL},
88         {"compose_override_from_format", NULL, &tmp_prefs.compose_override_from_format, P_STRING,
89          NULL, NULL, NULL},
90         {"compose_subject_format", NULL, &tmp_prefs.compose_subject_format, P_STRING,
91          NULL, NULL, NULL},
92         {"compose_body_format", NULL, &tmp_prefs.compose_body_format, P_STRING,
93          NULL, NULL, NULL},
94         {"reply_with_format", "FALSE", &tmp_prefs.reply_with_format, P_BOOL,
95          NULL, NULL, NULL},
96         {"reply_override_from_format", NULL, &tmp_prefs.reply_override_from_format, P_STRING,
97          NULL, NULL, NULL},
98         {"reply_quotemark", NULL, &tmp_prefs.reply_quotemark, P_STRING,
99          NULL, NULL, NULL},
100         {"reply_body_format", NULL, &tmp_prefs.reply_body_format, P_STRING,
101          NULL, NULL, NULL},
102         {"forward_with_format", "FALSE", &tmp_prefs.forward_with_format, P_BOOL,
103          NULL, NULL, NULL},
104         {"forward_override_from_format", NULL, &tmp_prefs.forward_override_from_format, P_STRING,
105          NULL, NULL, NULL},
106         {"forward_quotemark", NULL, &tmp_prefs.forward_quotemark, P_STRING,
107          NULL, NULL, NULL},
108         {"forward_body_format", NULL, &tmp_prefs.forward_body_format, P_STRING,
109          NULL, NULL, NULL},
110         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
111 };
112
113 static FolderItemPrefs *folder_item_prefs_clear(FolderItemPrefs *prefs);
114
115 void folder_item_prefs_read_config(FolderItem * item)
116 {
117         gchar * id;
118         gchar *rcpath;
119
120         id = folder_item_get_identifier(item);
121         folder_item_prefs_clear(&tmp_prefs);
122         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, FOLDERITEM_RC, NULL);
123         prefs_read_config(param, id, rcpath, NULL);
124         g_free(id);
125         g_free(rcpath);
126
127         *item->prefs = tmp_prefs;
128 }
129
130 void folder_item_prefs_save_config(FolderItem * item)
131 {       
132         gchar * id;
133
134         tmp_prefs = * item->prefs;
135
136         id = folder_item_get_identifier(item);
137         debug_print("saving prefs for %s\n", id?id:"(null)");
138         prefs_write_config(param, id, FOLDERITEM_RC);
139         g_free(id);
140 }
141
142 static gboolean folder_item_prefs_save_config_func(GNode *node, gpointer data)
143 {
144         FolderItem *item = (FolderItem *) node->data;
145         folder_item_prefs_save_config(item);
146         return FALSE;
147 }
148
149 void folder_item_prefs_save_config_recursive(FolderItem * item)
150 {       
151         g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL,
152                         -1, folder_item_prefs_save_config_func, NULL);
153 }
154
155 static FolderItemPrefs *folder_item_prefs_clear(FolderItemPrefs *prefs)
156 {
157         prefs->enable_default_to = FALSE;
158         prefs->default_to = NULL;
159         prefs->enable_default_reply_to = FALSE;
160         prefs->default_reply_to = NULL;
161         prefs->enable_simplify_subject = FALSE;
162 #ifndef G_OS_WIN32
163         prefs->simplify_subject_regexp = NULL;
164 #endif
165         prefs->enable_folder_chmod = FALSE;
166         prefs->folder_chmod = 0;
167         prefs->enable_default_account = FALSE;
168         prefs->default_account = 0;
169 #if USE_ENCHANT
170         prefs->enable_default_dictionary = FALSE;
171         prefs->default_dictionary = NULL;
172         prefs->enable_default_alt_dictionary = FALSE;
173         prefs->default_alt_dictionary = NULL;
174 #endif
175         prefs->save_copy_to_folder = FALSE;
176         prefs->color = 0;
177
178         prefs->enable_processing = TRUE;
179         prefs->enable_processing_when_opening = TRUE;
180         prefs->processing = NULL;
181
182         prefs->newmailcheck = TRUE;
183         prefs->offlinesync = FALSE;
184         prefs->offlinesync_days = 0;
185         prefs->remove_old_bodies = FALSE;
186
187         prefs->compose_with_format = FALSE;
188         prefs->compose_subject_format = NULL;
189         prefs->compose_body_format = NULL;
190         prefs->compose_override_from_format = NULL;
191         prefs->reply_with_format = FALSE;
192         prefs->reply_quotemark = NULL;
193         prefs->reply_body_format = NULL;
194         prefs->reply_override_from_format = NULL;
195         prefs->forward_with_format = FALSE;
196         prefs->forward_quotemark = NULL;
197         prefs->forward_body_format = NULL;
198         prefs->forward_override_from_format = NULL;
199         return prefs;
200 }
201
202 FolderItemPrefs * folder_item_prefs_new(void)
203 {
204         FolderItemPrefs * prefs;
205
206         prefs = g_new0(FolderItemPrefs, 1);
207
208         return folder_item_prefs_clear(prefs);
209 }
210
211 void folder_item_prefs_free(FolderItemPrefs * prefs)
212 {
213         g_free(prefs->default_to);
214         g_free(prefs->default_reply_to);
215         g_free(prefs->compose_subject_format);
216         g_free(prefs->compose_body_format);
217         g_free(prefs->compose_override_from_format);
218         g_free(prefs->reply_quotemark);
219         g_free(prefs->reply_body_format);
220         g_free(prefs->reply_override_from_format);
221         g_free(prefs->forward_quotemark);
222         g_free(prefs->forward_body_format);
223         g_free(prefs->forward_override_from_format);
224         g_free(prefs);
225 }
226
227 #define SAFE_STRING(str) \
228         (str) ? (str) : ""
229
230 void folder_item_prefs_copy_prefs(FolderItem * src, FolderItem * dest)
231 {
232         GSList *tmp_prop_list = NULL, *tmp;
233         folder_item_prefs_read_config(src);
234
235         tmp_prefs.directory                     = g_strdup(src->prefs->directory);
236         tmp_prefs.enable_processing             = src->prefs->enable_processing;
237         tmp_prefs.enable_processing_when_opening = src->prefs->enable_processing_when_opening;
238         tmp_prefs.newmailcheck                  = src->prefs->newmailcheck;
239         tmp_prefs.offlinesync                   = src->prefs->offlinesync;
240         tmp_prefs.offlinesync_days              = src->prefs->offlinesync_days;
241         tmp_prefs.remove_old_bodies             = src->prefs->remove_old_bodies;
242
243         prefs_matcher_read_config();
244
245         for (tmp = src->prefs->processing; tmp != NULL && tmp->data != NULL;) {
246                 FilteringProp *prop = (FilteringProp *)tmp->data;
247                 
248                 tmp_prop_list = g_slist_append(tmp_prop_list,
249                                            filteringprop_copy(prop));
250                 tmp = tmp->next;
251         }
252         tmp_prefs.processing                    = tmp_prop_list;
253         
254         tmp_prefs.request_return_receipt        = src->prefs->request_return_receipt;
255         tmp_prefs.enable_default_to             = src->prefs->enable_default_to;
256         tmp_prefs.default_to                    = g_strdup(src->prefs->default_to);
257         tmp_prefs.enable_default_reply_to       = src->prefs->enable_default_reply_to;
258         tmp_prefs.default_reply_to              = g_strdup(src->prefs->default_reply_to);
259         tmp_prefs.enable_simplify_subject       = src->prefs->enable_simplify_subject;
260 #ifndef G_OS_WIN32
261         tmp_prefs.simplify_subject_regexp       = g_strdup(src->prefs->simplify_subject_regexp);
262 #endif
263         tmp_prefs.enable_folder_chmod           = src->prefs->enable_folder_chmod;
264         tmp_prefs.folder_chmod                  = src->prefs->folder_chmod;
265         tmp_prefs.enable_default_account        = src->prefs->enable_default_account;
266         tmp_prefs.default_account               = src->prefs->default_account;
267 #if USE_ENCHANT
268         tmp_prefs.enable_default_dictionary     = src->prefs->enable_default_dictionary;
269         tmp_prefs.default_dictionary            = g_strdup(src->prefs->default_dictionary);
270         tmp_prefs.enable_default_alt_dictionary = src->prefs->enable_default_alt_dictionary;
271         tmp_prefs.default_alt_dictionary        = g_strdup(src->prefs->default_alt_dictionary);
272 #endif
273         tmp_prefs.save_copy_to_folder           = src->prefs->save_copy_to_folder;
274         tmp_prefs.color                         = src->prefs->color;
275
276         tmp_prefs.compose_with_format = src->prefs->compose_with_format;
277         tmp_prefs.compose_subject_format = g_strdup(src->prefs->compose_subject_format);
278         tmp_prefs.compose_body_format = g_strdup(src->prefs->compose_body_format);
279         tmp_prefs.compose_override_from_format = g_strdup(src->prefs->compose_override_from_format);
280         tmp_prefs.reply_with_format = src->prefs->reply_with_format;
281         tmp_prefs.reply_quotemark = g_strdup(src->prefs->reply_quotemark);
282         tmp_prefs.reply_body_format = g_strdup(src->prefs->reply_body_format);
283         tmp_prefs.reply_override_from_format = g_strdup(src->prefs->reply_override_from_format);
284         tmp_prefs.forward_with_format = src->prefs->forward_with_format;
285         tmp_prefs.forward_quotemark = g_strdup(src->prefs->forward_quotemark);
286         tmp_prefs.forward_body_format = g_strdup(src->prefs->forward_body_format);
287         tmp_prefs.forward_override_from_format = g_strdup(src->prefs->forward_override_from_format);
288
289         *dest->prefs = tmp_prefs;
290         folder_item_prefs_save_config(dest);
291 }