2007-05-30 [wwp] 2.9.2cvs20
[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 2 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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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
99         {"compose_with_format", "FALSE", &tmp_prefs.compose_with_format, P_BOOL,
100          NULL, NULL, NULL},
101         {"compose_subject_format", NULL, &tmp_prefs.compose_subject_format, P_STRING,
102          NULL, NULL, NULL},
103         {"compose_body_format", NULL, &tmp_prefs.compose_body_format, P_STRING,
104          NULL, NULL, NULL},
105         {"reply_with_format", "FALSE", &tmp_prefs.reply_with_format, P_BOOL,
106          NULL, NULL, NULL},
107         {"reply_quotemark", NULL, &tmp_prefs.reply_quotemark, P_STRING,
108          NULL, NULL, NULL},
109         {"reply_body_format", NULL, &tmp_prefs.reply_body_format, P_STRING,
110          NULL, NULL, NULL},
111         {"forward_with_format", "FALSE", &tmp_prefs.forward_with_format, P_BOOL,
112          NULL, NULL, NULL},
113         {"forward_quotemark", NULL, &tmp_prefs.forward_quotemark, P_STRING,
114          NULL, NULL, NULL},
115         {"forward_body_format", NULL, &tmp_prefs.forward_body_format, P_STRING,
116          NULL, NULL, NULL},
117         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
118 };
119
120 static FolderItemPrefs *folder_item_prefs_clear(FolderItemPrefs *prefs);
121
122 void folder_item_prefs_read_config(FolderItem * item)
123 {
124         gchar * id;
125         gchar *rcpath;
126
127         id = folder_item_get_identifier(item);
128         folder_item_prefs_clear(&tmp_prefs);
129         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, FOLDERITEM_RC, NULL);
130         prefs_read_config(param, id, rcpath, NULL);
131         g_free(id);
132         g_free(rcpath);
133
134         *item->prefs = tmp_prefs;
135
136         /*
137          * MIGRATION: next lines are migration code. the idea is that
138          *            if used regularly, claws folder config ends up
139          *            in the same file as sylpheed-main
140          */
141
142         item->ret_rcpt = tmp_prefs.request_return_receipt ? TRUE : FALSE;
143
144         /* MIGRATION: 0.7.8main+ has persistent sort order. claws had the sort
145          *            order in different members, which is ofcourse a little
146          *            bit phoney. */
147         if (item->sort_key == SORT_BY_NONE) {
148                 item->sort_key  = (tmp_prefs.sort_by_number  ? SORT_BY_NUMBER  :
149                                    tmp_prefs.sort_by_size    ? SORT_BY_SIZE    :
150                                    tmp_prefs.sort_by_date    ? SORT_BY_DATE    :
151                                    tmp_prefs.sort_by_from    ? SORT_BY_FROM    :
152                                    tmp_prefs.sort_by_subject ? SORT_BY_SUBJECT :
153                                    tmp_prefs.sort_by_score   ? SORT_BY_SCORE   :
154                                                                  SORT_BY_NONE);
155                 item->sort_type = tmp_prefs.sort_descending ? SORT_DESCENDING : SORT_ASCENDING;
156         }                                                               
157 }
158
159 void folder_item_prefs_save_config(FolderItem * item)
160 {       
161         gchar * id;
162
163         tmp_prefs = * item->prefs;
164
165         id = folder_item_get_identifier(item);
166
167         prefs_write_config(param, id, FOLDERITEM_RC);
168         g_free(id);
169
170         /* MIGRATION: make sure migrated items are not saved
171          */
172 }
173
174 static FolderItemPrefs *folder_item_prefs_clear(FolderItemPrefs *prefs)
175 {
176         prefs->sort_by_number = FALSE;
177         prefs->sort_by_size = FALSE;
178         prefs->sort_by_date = FALSE;
179         prefs->sort_by_from = FALSE;
180         prefs->sort_by_subject = FALSE;
181         prefs->sort_by_score = FALSE;
182         prefs->sort_descending = FALSE;
183
184         prefs->request_return_receipt = FALSE;
185         prefs->enable_default_to = FALSE;
186         prefs->default_to = NULL;
187         prefs->enable_default_reply_to = FALSE;
188         prefs->default_reply_to = NULL;
189         prefs->enable_simplify_subject = FALSE;
190         prefs->simplify_subject_regexp = NULL;
191         prefs->enable_folder_chmod = FALSE;
192         prefs->folder_chmod = 0;
193         prefs->enable_default_account = FALSE;
194         prefs->default_account = 0;
195 #if USE_ASPELL
196         prefs->enable_default_dictionary = FALSE;
197         prefs->default_dictionary = NULL;
198         prefs->enable_default_alt_dictionary = FALSE;
199         prefs->default_alt_dictionary = NULL;
200 #endif
201         prefs->save_copy_to_folder = FALSE;
202         prefs->color = 0;
203
204         prefs->enable_processing = TRUE;
205         prefs->processing = NULL;
206
207         prefs->newmailcheck = TRUE;
208         prefs->offlinesync = FALSE;
209
210         prefs->compose_with_format = FALSE;
211         prefs->compose_subject_format = NULL;
212         prefs->compose_body_format = NULL;
213         prefs->reply_with_format = FALSE;
214         prefs->reply_quotemark = NULL;
215         prefs->reply_body_format = NULL;
216         prefs->forward_with_format = FALSE;
217         prefs->forward_quotemark = NULL;
218         prefs->forward_body_format = NULL;
219
220         return prefs;
221 }
222
223 FolderItemPrefs * folder_item_prefs_new(void)
224 {
225         FolderItemPrefs * prefs;
226
227         prefs = g_new0(FolderItemPrefs, 1);
228
229         return folder_item_prefs_clear(prefs);
230 }
231
232 void folder_item_prefs_free(FolderItemPrefs * prefs)
233 {
234         g_free(prefs->default_to);
235         g_free(prefs->default_reply_to);
236         g_free(prefs->compose_subject_format);
237         g_free(prefs->compose_body_format);
238         g_free(prefs->reply_quotemark);
239         g_free(prefs->reply_body_format);
240         g_free(prefs->forward_quotemark);
241         g_free(prefs->forward_body_format);
242         g_free(prefs);
243 }
244
245 #define SAFE_STRING(str) \
246         (str) ? (str) : ""
247
248 void folder_item_prefs_copy_prefs(FolderItem * src, FolderItem * dest)
249 {
250         GSList *tmp_prop_list = NULL, *tmp;
251         folder_item_prefs_read_config(src);
252
253         tmp_prefs.directory                     = g_strdup(src->prefs->directory);
254         tmp_prefs.sort_by_number                = src->prefs->sort_by_number;
255         tmp_prefs.sort_by_size                  = src->prefs->sort_by_size;
256         tmp_prefs.sort_by_date                  = src->prefs->sort_by_date;
257         tmp_prefs.sort_by_from                  = src->prefs->sort_by_from;
258         tmp_prefs.sort_by_subject               = src->prefs->sort_by_subject;
259         tmp_prefs.sort_by_score                 = src->prefs->sort_by_score;
260         tmp_prefs.sort_descending               = src->prefs->sort_descending;
261         tmp_prefs.enable_thread                 = src->prefs->enable_thread;
262         tmp_prefs.enable_processing             = src->prefs->enable_processing;
263         tmp_prefs.newmailcheck                  = src->prefs->newmailcheck;
264         tmp_prefs.offlinesync                  = src->prefs->offlinesync;
265
266         prefs_matcher_read_config();
267
268         for (tmp = src->prefs->processing; tmp != NULL && tmp->data != NULL;) {
269                 FilteringProp *prop = (FilteringProp *)tmp->data;
270                 
271                 tmp_prop_list = g_slist_append(tmp_prop_list,
272                                            filteringprop_copy(prop));
273                 tmp = tmp->next;
274         }
275         tmp_prefs.processing                    = tmp_prop_list;
276         
277         tmp_prefs.request_return_receipt        = src->prefs->request_return_receipt;
278         tmp_prefs.enable_default_to             = src->prefs->enable_default_to;
279         tmp_prefs.default_to                    = g_strdup(src->prefs->default_to);
280         tmp_prefs.enable_default_reply_to       = src->prefs->enable_default_reply_to;
281         tmp_prefs.default_reply_to              = g_strdup(src->prefs->default_reply_to);
282         tmp_prefs.enable_simplify_subject       = src->prefs->enable_simplify_subject;
283         tmp_prefs.simplify_subject_regexp       = g_strdup(src->prefs->simplify_subject_regexp);
284         tmp_prefs.enable_folder_chmod           = src->prefs->enable_folder_chmod;
285         tmp_prefs.folder_chmod                  = src->prefs->folder_chmod;
286         tmp_prefs.enable_default_account        = src->prefs->enable_default_account;
287         tmp_prefs.default_account               = src->prefs->default_account;
288 #if USE_ASPELL
289         tmp_prefs.enable_default_dictionary     = src->prefs->enable_default_dictionary;
290         tmp_prefs.default_dictionary            = g_strdup(src->prefs->default_dictionary);
291         tmp_prefs.enable_default_alt_dictionary = src->prefs->enable_default_alt_dictionary;
292         tmp_prefs.default_alt_dictionary        = g_strdup(src->prefs->default_alt_dictionary);
293 #endif
294         tmp_prefs.save_copy_to_folder           = src->prefs->save_copy_to_folder;
295         tmp_prefs.color                         = src->prefs->color;
296
297         tmp_prefs.compose_with_format = src->prefs->compose_with_format;
298         tmp_prefs.compose_subject_format = g_strdup(src->prefs->compose_subject_format);
299         tmp_prefs.compose_body_format = g_strdup(src->prefs->compose_body_format);
300         tmp_prefs.reply_with_format = src->prefs->reply_with_format;
301         tmp_prefs.reply_quotemark = g_strdup(src->prefs->reply_quotemark);
302         tmp_prefs.reply_body_format = g_strdup(src->prefs->reply_body_format);
303         tmp_prefs.forward_with_format = src->prefs->forward_with_format;
304         tmp_prefs.forward_quotemark = g_strdup(src->prefs->forward_quotemark);
305         tmp_prefs.forward_body_format = g_strdup(src->prefs->forward_body_format);
306
307         *dest->prefs = tmp_prefs;
308         folder_item_prefs_save_config(dest);
309 }