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