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