320420dfc372d8827ebf8f7e873d7e67b746ba3f
[claws.git] / src / folder_item_prefs.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 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 /* alfons - all folder item specific settings should migrate into 
21  * folderlist.xml!!! the old folderitemrc file will only serve for a few 
22  * versions (for compatibility) */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include <glib.h>
29 #include <glib/gi18n.h>
30
31 #include "defs.h"
32 #include "folder.h"
33 #include "utils.h"
34 #include "prefs_gtk.h"
35 #include "filtering.h"
36 #include "folder_item_prefs.h"
37
38 FolderItemPrefs tmp_prefs;
39
40 static PrefParam param[] = {
41         {"sort_by_number", "FALSE", &tmp_prefs.sort_by_number, P_BOOL,
42          NULL, NULL, NULL},
43         {"sort_by_size", "FALSE", &tmp_prefs.sort_by_size, P_BOOL,
44          NULL, NULL, NULL},
45         {"sort_by_date", "FALSE", &tmp_prefs.sort_by_date, P_BOOL,
46          NULL, NULL, NULL},
47         {"sort_by_from", "FALSE", &tmp_prefs.sort_by_from, P_BOOL,
48          NULL, NULL, NULL},
49         {"sort_by_subject", "FALSE", &tmp_prefs.sort_by_subject, P_BOOL,
50          NULL, NULL, NULL},
51         {"sort_by_score", "FALSE", &tmp_prefs.sort_by_score, P_BOOL,
52          NULL, NULL, NULL},
53         {"sort_descending", "FALSE", &tmp_prefs.sort_descending, P_BOOL,
54          NULL, NULL, NULL},
55         /* MIGRATION */  
56         {"request_return_receipt", "", &tmp_prefs.request_return_receipt, P_BOOL,
57          NULL, NULL, NULL},
58         {"enable_default_to", "", &tmp_prefs.enable_default_to, P_BOOL,
59          NULL, NULL, NULL},
60         {"default_to", "", &tmp_prefs.default_to, P_STRING,
61          NULL, NULL, NULL},
62         {"enable_default_reply_to", "", &tmp_prefs.enable_default_reply_to, P_BOOL,
63          NULL, NULL, NULL},
64         {"default_reply_to", "", &tmp_prefs.default_reply_to, P_STRING,
65          NULL, NULL, NULL},
66         {"enable_simplify_subject", "", &tmp_prefs.enable_simplify_subject, P_BOOL,
67          NULL, NULL, NULL},
68         {"simplify_subject_regexp", "", &tmp_prefs.simplify_subject_regexp, P_STRING,
69          NULL, NULL, NULL},
70         {"enable_folder_chmod", "", &tmp_prefs.enable_folder_chmod, P_BOOL,
71          NULL, NULL, NULL},
72         {"folder_chmod", "", &tmp_prefs.folder_chmod, P_INT,
73          NULL, NULL, NULL},
74         {"enable_default_account", "", &tmp_prefs.enable_default_account, P_BOOL,
75          NULL, NULL, NULL},
76         {"default_account", NULL, &tmp_prefs.default_account, P_INT,
77          NULL, NULL, NULL},
78 #if USE_ASPELL
79         {"enable_default_dictionary", "", &tmp_prefs.enable_default_dictionary, P_BOOL,
80          NULL, NULL, NULL},
81         {"default_dictionary", NULL, &tmp_prefs.default_dictionary, P_STRING,
82          NULL, NULL, NULL},
83         {"enable_default_alt_dictionary", "", &tmp_prefs.enable_default_alt_dictionary, P_BOOL,
84          NULL, NULL, NULL},
85         {"default_alt_dictionary", NULL, &tmp_prefs.default_alt_dictionary, P_STRING,
86          NULL, NULL, NULL},
87 #endif   
88         {"save_copy_to_folder", NULL, &tmp_prefs.save_copy_to_folder, P_BOOL,
89          NULL, NULL, NULL},
90         {"folder_color", "", &tmp_prefs.color, P_INT,
91          NULL, NULL, NULL},
92         {"enable_processing", "FALSE", &tmp_prefs.enable_processing, P_BOOL,
93          NULL, NULL, NULL},
94         {"newmailcheck", "TRUE", &tmp_prefs.newmailcheck, P_BOOL,
95          NULL, NULL, NULL},
96         {"offlinesync", "FALSE", &tmp_prefs.offlinesync, P_BOOL,
97          NULL, NULL, NULL},
98         {"offlinesync_days", "0", &tmp_prefs.offlinesync_days, P_INT,
99          NULL, NULL, NULL},
100         {"remove_old_bodies", "FALSE", &tmp_prefs.remove_old_bodies, P_BOOL,
101          NULL, NULL, NULL},
102
103         {"compose_with_format", "FALSE", &tmp_prefs.compose_with_format, P_BOOL,
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_quotemark", NULL, &tmp_prefs.reply_quotemark, P_STRING,
112          NULL, NULL, NULL},
113         {"reply_body_format", NULL, &tmp_prefs.reply_body_format, P_STRING,
114          NULL, NULL, NULL},
115         {"forward_with_format", "FALSE", &tmp_prefs.forward_with_format, P_BOOL,
116          NULL, NULL, NULL},
117         {"forward_quotemark", NULL, &tmp_prefs.forward_quotemark, P_STRING,
118          NULL, NULL, NULL},
119         {"forward_body_format", NULL, &tmp_prefs.forward_body_format, P_STRING,
120          NULL, NULL, NULL},
121         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
122 };
123
124 static FolderItemPrefs *folder_item_prefs_clear(FolderItemPrefs *prefs);
125
126 void folder_item_prefs_read_config(FolderItem * item)
127 {
128         gchar * id;
129         gchar *rcpath;
130
131         id = folder_item_get_identifier(item);
132         folder_item_prefs_clear(&tmp_prefs);
133         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, FOLDERITEM_RC, NULL);
134         prefs_read_config(param, id, rcpath, NULL);
135         g_free(id);
136         g_free(rcpath);
137
138         *item->prefs = tmp_prefs;
139
140         /*
141          * MIGRATION: next lines are migration code. the idea is that
142          *            if used regularly, claws folder config ends up
143          *            in the same file as sylpheed-main
144          */
145
146         item->ret_rcpt = tmp_prefs.request_return_receipt ? TRUE : FALSE;
147
148         /* MIGRATION: 0.7.8main+ has persistent sort order. claws had the sort
149          *            order in different members, which is ofcourse a little
150          *            bit phoney. */
151         if (item->sort_key == SORT_BY_NONE) {
152                 item->sort_key  = (tmp_prefs.sort_by_number  ? SORT_BY_NUMBER  :
153                                    tmp_prefs.sort_by_size    ? SORT_BY_SIZE    :
154                                    tmp_prefs.sort_by_date    ? SORT_BY_DATE    :
155                                    tmp_prefs.sort_by_from    ? SORT_BY_FROM    :
156                                    tmp_prefs.sort_by_subject ? SORT_BY_SUBJECT :
157                                    tmp_prefs.sort_by_score   ? SORT_BY_SCORE   :
158                                                                  SORT_BY_NONE);
159                 item->sort_type = tmp_prefs.sort_descending ? SORT_DESCENDING : SORT_ASCENDING;
160         }                                                               
161 }
162
163 void folder_item_prefs_save_config(FolderItem * item)
164 {       
165         gchar * id;
166
167         tmp_prefs = * item->prefs;
168
169         id = folder_item_get_identifier(item);
170         debug_print("saving prefs for %s\n", id?id:"(null)");
171         prefs_write_config(param, id, FOLDERITEM_RC);
172         g_free(id);
173
174         /* MIGRATION: make sure migrated items are not saved
175          */
176 }
177
178 static gboolean folder_item_prefs_save_config_func(GNode *node, gpointer data)
179 {
180         FolderItem *item = (FolderItem *) node->data;
181         folder_item_prefs_save_config(item);
182         return FALSE;
183 }
184
185 void folder_item_prefs_save_config_recursive(FolderItem * item)
186 {       
187         g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL,
188                         -1, folder_item_prefs_save_config_func, NULL);
189 }
190
191 static FolderItemPrefs *folder_item_prefs_clear(FolderItemPrefs *prefs)
192 {
193         prefs->sort_by_number = FALSE;
194         prefs->sort_by_size = FALSE;
195         prefs->sort_by_date = FALSE;
196         prefs->sort_by_from = FALSE;
197         prefs->sort_by_subject = FALSE;
198         prefs->sort_by_score = FALSE;
199         prefs->sort_descending = FALSE;
200
201         prefs->request_return_receipt = FALSE;
202         prefs->enable_default_to = FALSE;
203         prefs->default_to = NULL;
204         prefs->enable_default_reply_to = FALSE;
205         prefs->default_reply_to = NULL;
206         prefs->enable_simplify_subject = FALSE;
207         prefs->simplify_subject_regexp = NULL;
208         prefs->enable_folder_chmod = FALSE;
209         prefs->folder_chmod = 0;
210         prefs->enable_default_account = FALSE;
211         prefs->default_account = 0;
212 #if USE_ASPELL
213         prefs->enable_default_dictionary = FALSE;
214         prefs->default_dictionary = NULL;
215         prefs->enable_default_alt_dictionary = FALSE;
216         prefs->default_alt_dictionary = NULL;
217 #endif
218         prefs->save_copy_to_folder = FALSE;
219         prefs->color = 0;
220
221         prefs->enable_processing = TRUE;
222         prefs->processing = NULL;
223
224         prefs->newmailcheck = TRUE;
225         prefs->offlinesync = FALSE;
226         prefs->offlinesync_days = 0;
227         prefs->remove_old_bodies = FALSE;
228
229         prefs->compose_with_format = FALSE;
230         prefs->compose_subject_format = NULL;
231         prefs->compose_body_format = NULL;
232         prefs->reply_with_format = FALSE;
233         prefs->reply_quotemark = NULL;
234         prefs->reply_body_format = NULL;
235         prefs->forward_with_format = FALSE;
236         prefs->forward_quotemark = NULL;
237         prefs->forward_body_format = NULL;
238
239         return prefs;
240 }
241
242 FolderItemPrefs * folder_item_prefs_new(void)
243 {
244         FolderItemPrefs * prefs;
245
246         prefs = g_new0(FolderItemPrefs, 1);
247
248         return folder_item_prefs_clear(prefs);
249 }
250
251 void folder_item_prefs_free(FolderItemPrefs * prefs)
252 {
253         g_free(prefs->default_to);
254         g_free(prefs->default_reply_to);
255         g_free(prefs->compose_subject_format);
256         g_free(prefs->compose_body_format);
257         g_free(prefs->reply_quotemark);
258         g_free(prefs->reply_body_format);
259         g_free(prefs->forward_quotemark);
260         g_free(prefs->forward_body_format);
261         g_free(prefs);
262 }
263
264 #define SAFE_STRING(str) \
265         (str) ? (str) : ""
266
267 void folder_item_prefs_copy_prefs(FolderItem * src, FolderItem * dest)
268 {
269         GSList *tmp_prop_list = NULL, *tmp;
270         folder_item_prefs_read_config(src);
271
272         tmp_prefs.directory                     = g_strdup(src->prefs->directory);
273         tmp_prefs.sort_by_number                = src->prefs->sort_by_number;
274         tmp_prefs.sort_by_size                  = src->prefs->sort_by_size;
275         tmp_prefs.sort_by_date                  = src->prefs->sort_by_date;
276         tmp_prefs.sort_by_from                  = src->prefs->sort_by_from;
277         tmp_prefs.sort_by_subject               = src->prefs->sort_by_subject;
278         tmp_prefs.sort_by_score                 = src->prefs->sort_by_score;
279         tmp_prefs.sort_descending               = src->prefs->sort_descending;
280         tmp_prefs.enable_thread                 = src->prefs->enable_thread;
281         tmp_prefs.enable_processing             = src->prefs->enable_processing;
282         tmp_prefs.newmailcheck                  = src->prefs->newmailcheck;
283         tmp_prefs.offlinesync                   = src->prefs->offlinesync;
284         tmp_prefs.offlinesync_days              = src->prefs->offlinesync_days;
285         tmp_prefs.remove_old_bodies             = src->prefs->remove_old_bodies;
286
287         prefs_matcher_read_config();
288
289         for (tmp = src->prefs->processing; tmp != NULL && tmp->data != NULL;) {
290                 FilteringProp *prop = (FilteringProp *)tmp->data;
291                 
292                 tmp_prop_list = g_slist_append(tmp_prop_list,
293                                            filteringprop_copy(prop));
294                 tmp = tmp->next;
295         }
296         tmp_prefs.processing                    = tmp_prop_list;
297         
298         tmp_prefs.request_return_receipt        = src->prefs->request_return_receipt;
299         tmp_prefs.enable_default_to             = src->prefs->enable_default_to;
300         tmp_prefs.default_to                    = g_strdup(src->prefs->default_to);
301         tmp_prefs.enable_default_reply_to       = src->prefs->enable_default_reply_to;
302         tmp_prefs.default_reply_to              = g_strdup(src->prefs->default_reply_to);
303         tmp_prefs.enable_simplify_subject       = src->prefs->enable_simplify_subject;
304         tmp_prefs.simplify_subject_regexp       = g_strdup(src->prefs->simplify_subject_regexp);
305         tmp_prefs.enable_folder_chmod           = src->prefs->enable_folder_chmod;
306         tmp_prefs.folder_chmod                  = src->prefs->folder_chmod;
307         tmp_prefs.enable_default_account        = src->prefs->enable_default_account;
308         tmp_prefs.default_account               = src->prefs->default_account;
309 #if USE_ASPELL
310         tmp_prefs.enable_default_dictionary     = src->prefs->enable_default_dictionary;
311         tmp_prefs.default_dictionary            = g_strdup(src->prefs->default_dictionary);
312         tmp_prefs.enable_default_alt_dictionary = src->prefs->enable_default_alt_dictionary;
313         tmp_prefs.default_alt_dictionary        = g_strdup(src->prefs->default_alt_dictionary);
314 #endif
315         tmp_prefs.save_copy_to_folder           = src->prefs->save_copy_to_folder;
316         tmp_prefs.color                         = src->prefs->color;
317
318         tmp_prefs.compose_with_format = src->prefs->compose_with_format;
319         tmp_prefs.compose_subject_format = g_strdup(src->prefs->compose_subject_format);
320         tmp_prefs.compose_body_format = g_strdup(src->prefs->compose_body_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.forward_with_format = src->prefs->forward_with_format;
325         tmp_prefs.forward_quotemark = g_strdup(src->prefs->forward_quotemark);
326         tmp_prefs.forward_body_format = g_strdup(src->prefs->forward_body_format);
327
328         *dest->prefs = tmp_prefs;
329         folder_item_prefs_save_config(dest);
330 }