2004-11-09 [colin] 0.9.12cvs144.1
[claws.git] / src / folder_item_prefs.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto and the Sylpheed-Claws 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 "intl.h"
29 #include "defs.h"
30 #include "folder.h"
31 #include "utils.h"
32 #include "prefs_gtk.h"
33 #include "filtering.h"
34 #include "folder_item_prefs.h"
35
36 FolderItemPrefs tmp_prefs;
37
38 static PrefParam param[] = {
39         {"sort_by_number", "FALSE", &tmp_prefs.sort_by_number, P_BOOL,
40          NULL, NULL, NULL},
41         {"sort_by_size", "FALSE", &tmp_prefs.sort_by_size, P_BOOL,
42          NULL, NULL, NULL},
43         {"sort_by_date", "FALSE", &tmp_prefs.sort_by_date, P_BOOL,
44          NULL, NULL, NULL},
45         {"sort_by_from", "FALSE", &tmp_prefs.sort_by_from, P_BOOL,
46          NULL, NULL, NULL},
47         {"sort_by_subject", "FALSE", &tmp_prefs.sort_by_subject, P_BOOL,
48          NULL, NULL, NULL},
49         {"sort_by_score", "FALSE", &tmp_prefs.sort_by_score, P_BOOL,
50          NULL, NULL, NULL},
51         {"sort_descending", "FALSE", &tmp_prefs.sort_descending, P_BOOL,
52          NULL, NULL, NULL},
53         /*{"enable_thread", "TRUE", &tmp_prefs.enable_thread, P_BOOL,
54          NULL, NULL, NULL},*/
55 #if 0
56         {"hide_score", "-9999", &tmp_prefs.kill_score, P_INT,
57          NULL, NULL, NULL},
58         {"important_score", "1", &tmp_prefs.important_score, P_INT,
59          NULL, NULL, NULL},
60 #endif
61         /* MIGRATION */  
62         {"request_return_receipt", "", &tmp_prefs.request_return_receipt, P_BOOL,
63          NULL, NULL, NULL},
64         {"enable_default_to", "", &tmp_prefs.enable_default_to, P_BOOL,
65          NULL, NULL, NULL},
66         {"default_to", "", &tmp_prefs.default_to, P_STRING,
67          NULL, NULL, NULL},
68         {"enable_default_reply_to", "", &tmp_prefs.enable_default_reply_to, P_BOOL,
69          NULL, NULL, NULL},
70         {"default_reply_to", "", &tmp_prefs.default_reply_to, P_STRING,
71          NULL, NULL, NULL},
72         {"enable_simplify_subject", "", &tmp_prefs.enable_simplify_subject, P_BOOL,
73          NULL, NULL, NULL},
74         {"simplify_subject_regexp", "", &tmp_prefs.simplify_subject_regexp, P_STRING,
75          NULL, NULL, NULL},
76         {"enable_folder_chmod", "", &tmp_prefs.enable_folder_chmod, P_BOOL,
77          NULL, NULL, NULL},
78         {"folder_chmod", "", &tmp_prefs.folder_chmod, P_INT,
79          NULL, NULL, NULL},
80         {"enable_default_account", "", &tmp_prefs.enable_default_account, P_BOOL,
81          NULL, NULL, NULL},
82         {"default_account", NULL, &tmp_prefs.default_account, P_INT,
83          NULL, NULL, NULL},
84 #if USE_ASPELL
85         {"enable_default_dictionary", "", &tmp_prefs.enable_default_dictionary, P_BOOL,
86          NULL, NULL, NULL},
87         {"default_dictionary", NULL, &tmp_prefs.default_dictionary, P_STRING,
88          NULL, NULL, NULL},
89 #endif   
90         {"save_copy_to_folder", NULL, &tmp_prefs.save_copy_to_folder, P_BOOL,
91          NULL, NULL, NULL},
92         {"folder_color", "", &tmp_prefs.color, P_INT,
93          NULL, NULL, NULL},
94         {"enable_processing", "FALSE", &tmp_prefs.enable_processing, P_BOOL,
95          NULL, NULL, NULL},
96         {"newmailcheck", "TRUE", &tmp_prefs.newmailcheck, 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
107         id = folder_item_get_identifier(item);
108         folder_item_prefs_clear(&tmp_prefs);
109         prefs_read_config(param, id, FOLDERITEM_RC);
110         g_free(id);
111
112         *item->prefs = tmp_prefs;
113
114         /*
115          * MIGRATION: next lines are migration code. the idea is that
116          *            if used regularly, claws folder config ends up
117          *            in the same file as sylpheed-main
118          */
119
120         item->ret_rcpt = tmp_prefs.request_return_receipt ? TRUE : FALSE;
121
122         /* MIGRATION: 0.7.8main+ has persistent sort order. claws had the sort
123          *            order in different members, which is ofcourse a little
124          *            bit phoney. */
125         if (item->sort_key == SORT_BY_NONE) {
126                 item->sort_key  = (tmp_prefs.sort_by_number  ? SORT_BY_NUMBER  :
127                                    tmp_prefs.sort_by_size    ? SORT_BY_SIZE    :
128                                    tmp_prefs.sort_by_date    ? SORT_BY_DATE    :
129                                    tmp_prefs.sort_by_from    ? SORT_BY_FROM    :
130                                    tmp_prefs.sort_by_subject ? SORT_BY_SUBJECT :
131                                    tmp_prefs.sort_by_score   ? SORT_BY_SCORE   :
132                                                                  SORT_BY_NONE);
133                 item->sort_type = tmp_prefs.sort_descending ? SORT_DESCENDING : SORT_ASCENDING;
134         }                                                               
135 }
136
137 void folder_item_prefs_save_config(FolderItem * item)
138 {       
139         gchar * id;
140
141         tmp_prefs = * item->prefs;
142
143         id = folder_item_get_identifier(item);
144
145         prefs_write_config(param, id, FOLDERITEM_RC);
146         g_free(id);
147
148         /* MIGRATION: make sure migrated items are not saved
149          */
150 }
151
152 void folder_item_prefs_set_config(FolderItem * item,
153                                   int sort_type, gint sort_mode)
154 {
155         g_assert(item);
156         g_warning("folder_item_prefs_set_config() should never be called\n");
157         item->sort_key  = sort_type;
158         item->sort_type = sort_mode;
159 }
160
161 static FolderItemPrefs *folder_item_prefs_clear(FolderItemPrefs *prefs)
162 {
163         prefs->sort_by_number = FALSE;
164         prefs->sort_by_size = FALSE;
165         prefs->sort_by_date = FALSE;
166         prefs->sort_by_from = FALSE;
167         prefs->sort_by_subject = FALSE;
168         prefs->sort_by_score = FALSE;
169         prefs->sort_descending = FALSE;
170
171         prefs->request_return_receipt = FALSE;
172         prefs->enable_default_to = FALSE;
173         prefs->default_to = NULL;
174         prefs->enable_default_reply_to = FALSE;
175         prefs->default_reply_to = NULL;
176         prefs->enable_simplify_subject = FALSE;
177         prefs->simplify_subject_regexp = NULL;
178         prefs->enable_folder_chmod = FALSE;
179         prefs->folder_chmod = 0;
180         prefs->enable_default_account = FALSE;
181         prefs->default_account = 0;
182 #if USE_ASPELL
183         prefs->enable_default_dictionary = FALSE;
184         prefs->default_dictionary = NULL;
185 #endif
186         prefs->save_copy_to_folder = FALSE;
187         prefs->color = 0;
188
189         prefs->enable_processing = TRUE;
190         prefs->processing = NULL;
191
192         prefs->newmailcheck = TRUE;
193
194         return prefs;
195 }
196
197 FolderItemPrefs * folder_item_prefs_new(void)
198 {
199         FolderItemPrefs * prefs;
200
201         prefs = g_new0(FolderItemPrefs, 1);
202
203         return folder_item_prefs_clear(prefs);
204 }
205
206 void folder_item_prefs_free(FolderItemPrefs * prefs)
207 {
208         if (prefs->default_to) 
209                 g_free(prefs->default_to);
210         if (prefs->default_reply_to) 
211                 g_free(prefs->default_reply_to);
212         g_free(prefs);
213 }
214
215 gint folder_item_prefs_get_sort_mode(FolderItem * item)
216 {
217         g_assert(item != NULL);
218         g_warning("folder_item_prefs_get_sort_mode() should never be called\n");
219         return item->sort_key;
220 }
221
222 gint folder_item_prefs_get_sort_type(FolderItem * item)
223 {
224         g_assert(item != NULL);
225         g_warning("folder_item_prefs_get_sort_type() should never be called\n");
226         return item->sort_type;
227 }
228
229 #define SAFE_STRING(str) \
230         (str) ? (str) : ""
231
232 void folder_item_prefs_copy_prefs(FolderItem * src, FolderItem * dest)
233 {
234         GSList *tmp_prop_list = NULL, *tmp;
235         folder_item_prefs_read_config(src);
236
237         tmp_prefs.directory                     = g_strdup(src->prefs->directory);
238         tmp_prefs.sort_by_number                = src->prefs->sort_by_number;
239         tmp_prefs.sort_by_size                  = src->prefs->sort_by_size;
240         tmp_prefs.sort_by_date                  = src->prefs->sort_by_date;
241         tmp_prefs.sort_by_from                  = src->prefs->sort_by_from;
242         tmp_prefs.sort_by_subject               = src->prefs->sort_by_subject;
243         tmp_prefs.sort_by_score                 = src->prefs->sort_by_score;
244         tmp_prefs.sort_descending               = src->prefs->sort_descending;
245         tmp_prefs.enable_thread                 = src->prefs->enable_thread;
246         tmp_prefs.enable_processing             = src->prefs->enable_processing;
247     tmp_prefs.newmailcheck             = src->prefs->newmailcheck;
248
249         prefs_matcher_read_config();
250
251         for (tmp = src->prefs->processing; tmp != NULL && tmp->data != NULL;) {
252                 FilteringProp *prop = (FilteringProp *)tmp->data;
253                 
254                 tmp_prop_list = g_slist_append(tmp_prop_list,
255                                            filteringprop_copy(prop));
256                 tmp = tmp->next;
257         }
258         tmp_prefs.processing                    = tmp_prop_list;
259         
260         tmp_prefs.request_return_receipt        = src->prefs->request_return_receipt;
261         tmp_prefs.enable_default_to             = src->prefs->enable_default_to;
262         tmp_prefs.default_to                    = g_strdup(src->prefs->default_to);
263         tmp_prefs.enable_default_reply_to       = src->prefs->enable_default_reply_to;
264         tmp_prefs.default_reply_to              = src->prefs->default_reply_to;
265         tmp_prefs.enable_simplify_subject       = src->prefs->enable_simplify_subject;
266         tmp_prefs.simplify_subject_regexp       = g_strdup(src->prefs->simplify_subject_regexp);
267         tmp_prefs.enable_folder_chmod           = src->prefs->enable_folder_chmod;
268         tmp_prefs.folder_chmod                  = src->prefs->folder_chmod;
269         tmp_prefs.enable_default_account        = src->prefs->enable_default_account;
270         tmp_prefs.default_account               = src->prefs->default_account;
271 #if USE_ASPELL
272         tmp_prefs.enable_default_dictionary     = src->prefs->enable_default_dictionary;
273         tmp_prefs.default_dictionary            = g_strdup(src->prefs->default_dictionary);
274 #endif
275         tmp_prefs.save_copy_to_folder           = src->prefs->save_copy_to_folder;
276         tmp_prefs.color                         = src->prefs->color;
277
278         *dest->prefs = tmp_prefs;
279         folder_item_prefs_save_config(dest);
280 }