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