2011-04-08 [colin] 3.7.8cvs75
[claws.git] / src / folder_item_prefs.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2011 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         if (id == NULL)
150                 return;
151
152         debug_print("saving prefs for %s\n", id);
153         prefs_write_config(param, id, FOLDERITEM_RC);
154         g_free(id);
155 }
156
157 static gboolean folder_item_prefs_save_config_func(GNode *node, gpointer data)
158 {
159         FolderItem *item = (FolderItem *) node->data;
160         folder_item_prefs_save_config(item);
161         return FALSE;
162 }
163
164 void folder_item_prefs_save_config_recursive(FolderItem * item)
165 {       
166         g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL,
167                         -1, folder_item_prefs_save_config_func, NULL);
168 }
169
170 void folder_prefs_save_config_recursive(Folder *folder)
171 {       
172         g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
173                         -1, folder_item_prefs_save_config_func, NULL);
174 }
175
176 static FolderItemPrefs *folder_item_prefs_clear(FolderItemPrefs *prefs)
177 {
178         prefs->enable_default_to = FALSE;
179         prefs->default_to = NULL;
180         prefs->enable_default_reply_to = FALSE;
181         prefs->default_reply_to = NULL;
182         prefs->enable_default_cc = FALSE;
183         prefs->default_cc = NULL;
184         prefs->enable_default_bcc = FALSE;
185         prefs->default_bcc = NULL;
186         prefs->enable_default_replyto = FALSE;
187         prefs->default_replyto = NULL;
188         prefs->enable_simplify_subject = FALSE;
189 #ifndef G_OS_WIN32
190         prefs->simplify_subject_regexp = NULL;
191 #endif
192         prefs->enable_folder_chmod = FALSE;
193         prefs->folder_chmod = 0;
194         prefs->enable_default_account = FALSE;
195         prefs->default_account = 0;
196 #if USE_ENCHANT
197         prefs->enable_default_dictionary = FALSE;
198         prefs->default_dictionary = NULL;
199         prefs->enable_default_alt_dictionary = FALSE;
200         prefs->default_alt_dictionary = NULL;
201 #endif
202         prefs->save_copy_to_folder = FALSE;
203         prefs->color = 0;
204
205         prefs->enable_processing = TRUE;
206         prefs->enable_processing_when_opening = TRUE;
207         prefs->processing = NULL;
208
209         prefs->newmailcheck = TRUE;
210         prefs->offlinesync = FALSE;
211         prefs->offlinesync_days = 0;
212         prefs->remove_old_bodies = FALSE;
213
214         prefs->compose_with_format = FALSE;
215         prefs->compose_subject_format = NULL;
216         prefs->compose_body_format = NULL;
217         prefs->compose_override_from_format = NULL;
218         prefs->reply_with_format = FALSE;
219         prefs->reply_quotemark = NULL;
220         prefs->reply_body_format = NULL;
221         prefs->reply_override_from_format = NULL;
222         prefs->forward_with_format = FALSE;
223         prefs->forward_quotemark = NULL;
224         prefs->forward_body_format = NULL;
225         prefs->forward_override_from_format = NULL;
226         return prefs;
227 }
228
229 FolderItemPrefs * folder_item_prefs_new(void)
230 {
231         FolderItemPrefs * prefs;
232
233         prefs = g_new0(FolderItemPrefs, 1);
234
235         return folder_item_prefs_clear(prefs);
236 }
237
238 void folder_item_prefs_free(FolderItemPrefs * prefs)
239 {
240         g_free(prefs->default_to);
241         g_free(prefs->default_reply_to);
242         g_free(prefs->default_cc);
243         g_free(prefs->default_bcc);
244         g_free(prefs->default_replyto);
245         g_free(prefs->compose_subject_format);
246         g_free(prefs->compose_body_format);
247         g_free(prefs->compose_override_from_format);
248         g_free(prefs->reply_quotemark);
249         g_free(prefs->reply_body_format);
250         g_free(prefs->reply_override_from_format);
251         g_free(prefs->forward_quotemark);
252         g_free(prefs->forward_body_format);
253         g_free(prefs->forward_override_from_format);
254         g_free(prefs);
255 }
256
257 #define SAFE_STRING(str) \
258         (str) ? (str) : ""
259
260 void folder_item_prefs_copy_prefs(FolderItem * src, FolderItem * dest)
261 {
262         GSList *tmp_prop_list = NULL, *tmp;
263         folder_item_prefs_read_config(src);
264
265         tmp_prefs.directory                     = g_strdup(src->prefs->directory);
266         tmp_prefs.enable_processing             = src->prefs->enable_processing;
267         tmp_prefs.enable_processing_when_opening = src->prefs->enable_processing_when_opening;
268         tmp_prefs.newmailcheck                  = src->prefs->newmailcheck;
269         tmp_prefs.offlinesync                   = src->prefs->offlinesync;
270         tmp_prefs.offlinesync_days              = src->prefs->offlinesync_days;
271         tmp_prefs.remove_old_bodies             = src->prefs->remove_old_bodies;
272
273         prefs_matcher_read_config();
274
275         for (tmp = src->prefs->processing; tmp != NULL && tmp->data != NULL;) {
276                 FilteringProp *prop = (FilteringProp *)tmp->data;
277                 
278                 tmp_prop_list = g_slist_append(tmp_prop_list,
279                                            filteringprop_copy(prop));
280                 tmp = tmp->next;
281         }
282         tmp_prefs.processing                    = tmp_prop_list;
283         
284         tmp_prefs.request_return_receipt        = src->prefs->request_return_receipt;
285         tmp_prefs.enable_default_to             = src->prefs->enable_default_to;
286         tmp_prefs.default_to                    = g_strdup(src->prefs->default_to);
287         tmp_prefs.enable_default_reply_to       = src->prefs->enable_default_reply_to;
288         tmp_prefs.default_reply_to              = g_strdup(src->prefs->default_reply_to);
289         tmp_prefs.enable_default_cc             = src->prefs->enable_default_cc;
290         tmp_prefs.default_cc                    = g_strdup(src->prefs->default_cc);
291         tmp_prefs.enable_default_bcc            = src->prefs->enable_default_bcc;
292         tmp_prefs.default_bcc                   = g_strdup(src->prefs->default_bcc);
293         tmp_prefs.enable_default_replyto                = src->prefs->enable_default_replyto;
294         tmp_prefs.default_replyto                       = g_strdup(src->prefs->default_replyto);
295         tmp_prefs.enable_simplify_subject       = src->prefs->enable_simplify_subject;
296 #ifndef G_OS_WIN32
297         tmp_prefs.simplify_subject_regexp       = g_strdup(src->prefs->simplify_subject_regexp);
298 #endif
299         tmp_prefs.enable_folder_chmod           = src->prefs->enable_folder_chmod;
300         tmp_prefs.folder_chmod                  = src->prefs->folder_chmod;
301         tmp_prefs.enable_default_account        = src->prefs->enable_default_account;
302         tmp_prefs.default_account               = src->prefs->default_account;
303 #if USE_ENCHANT
304         tmp_prefs.enable_default_dictionary     = src->prefs->enable_default_dictionary;
305         tmp_prefs.default_dictionary            = g_strdup(src->prefs->default_dictionary);
306         tmp_prefs.enable_default_alt_dictionary = src->prefs->enable_default_alt_dictionary;
307         tmp_prefs.default_alt_dictionary        = g_strdup(src->prefs->default_alt_dictionary);
308 #endif
309         tmp_prefs.save_copy_to_folder           = src->prefs->save_copy_to_folder;
310         tmp_prefs.color                         = src->prefs->color;
311
312         tmp_prefs.compose_with_format = src->prefs->compose_with_format;
313         tmp_prefs.compose_subject_format = g_strdup(src->prefs->compose_subject_format);
314         tmp_prefs.compose_body_format = g_strdup(src->prefs->compose_body_format);
315         tmp_prefs.compose_override_from_format = g_strdup(src->prefs->compose_override_from_format);
316         tmp_prefs.reply_with_format = src->prefs->reply_with_format;
317         tmp_prefs.reply_quotemark = g_strdup(src->prefs->reply_quotemark);
318         tmp_prefs.reply_body_format = g_strdup(src->prefs->reply_body_format);
319         tmp_prefs.reply_override_from_format = g_strdup(src->prefs->reply_override_from_format);
320         tmp_prefs.forward_with_format = src->prefs->forward_with_format;
321         tmp_prefs.forward_quotemark = g_strdup(src->prefs->forward_quotemark);
322         tmp_prefs.forward_body_format = g_strdup(src->prefs->forward_body_format);
323         tmp_prefs.forward_override_from_format = g_strdup(src->prefs->forward_override_from_format);
324
325         *dest->prefs = tmp_prefs;
326         folder_item_prefs_save_config(dest);
327
328         dest->collapsed = src->collapsed;
329         dest->thread_collapsed = src->thread_collapsed;
330         dest->threaded  = src->threaded;
331         dest->ret_rcpt  = src->ret_rcpt;
332         dest->hide_read_msgs = src->hide_read_msgs;
333         dest->hide_del_msgs = src->hide_del_msgs;
334         dest->sort_key  = src->sort_key;
335         dest->sort_type = src->sort_type;
336
337 }