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