2006-04-06 [mones] 2.1.0cvs7
[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 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         /* 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 #endif   
84         {"save_copy_to_folder", NULL, &tmp_prefs.save_copy_to_folder, P_BOOL,
85          NULL, NULL, NULL},
86         {"folder_color", "", &tmp_prefs.color, P_INT,
87          NULL, NULL, NULL},
88         {"enable_processing", "FALSE", &tmp_prefs.enable_processing, P_BOOL,
89          NULL, NULL, NULL},
90         {"newmailcheck", "TRUE", &tmp_prefs.newmailcheck, P_BOOL,
91          NULL, NULL, NULL},
92         {"offlinesync", "FALSE", &tmp_prefs.offlinesync, P_BOOL,
93          NULL, NULL, NULL},
94         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
95 };
96
97 static FolderItemPrefs *folder_item_prefs_clear(FolderItemPrefs *prefs);
98
99 void folder_item_prefs_read_config(FolderItem * item)
100 {
101         gchar * id;
102         gchar *rcpath;
103
104         id = folder_item_get_identifier(item);
105         folder_item_prefs_clear(&tmp_prefs);
106         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, FOLDERITEM_RC, NULL);
107         prefs_read_config(param, id, rcpath, NULL);
108         g_free(id);
109         g_free(rcpath);
110
111         *item->prefs = tmp_prefs;
112
113         /*
114          * MIGRATION: next lines are migration code. the idea is that
115          *            if used regularly, claws folder config ends up
116          *            in the same file as sylpheed-main
117          */
118
119         item->ret_rcpt = tmp_prefs.request_return_receipt ? TRUE : FALSE;
120
121         /* MIGRATION: 0.7.8main+ has persistent sort order. claws had the sort
122          *            order in different members, which is ofcourse a little
123          *            bit phoney. */
124         if (item->sort_key == SORT_BY_NONE) {
125                 item->sort_key  = (tmp_prefs.sort_by_number  ? SORT_BY_NUMBER  :
126                                    tmp_prefs.sort_by_size    ? SORT_BY_SIZE    :
127                                    tmp_prefs.sort_by_date    ? SORT_BY_DATE    :
128                                    tmp_prefs.sort_by_from    ? SORT_BY_FROM    :
129                                    tmp_prefs.sort_by_subject ? SORT_BY_SUBJECT :
130                                    tmp_prefs.sort_by_score   ? SORT_BY_SCORE   :
131                                                                  SORT_BY_NONE);
132                 item->sort_type = tmp_prefs.sort_descending ? SORT_DESCENDING : SORT_ASCENDING;
133         }                                                               
134 }
135
136 void folder_item_prefs_save_config(FolderItem * item)
137 {       
138         gchar * id;
139
140         tmp_prefs = * item->prefs;
141
142         id = folder_item_get_identifier(item);
143
144         prefs_write_config(param, id, FOLDERITEM_RC);
145         g_free(id);
146
147         /* MIGRATION: make sure migrated items are not saved
148          */
149 }
150
151 void folder_item_prefs_set_config(FolderItem * item,
152                                   int sort_type, gint sort_mode)
153 {
154         g_assert(item);
155         g_warning("folder_item_prefs_set_config() should never be called\n");
156         item->sort_key  = sort_type;
157         item->sort_type = sort_mode;
158 }
159
160 static FolderItemPrefs *folder_item_prefs_clear(FolderItemPrefs *prefs)
161 {
162         prefs->sort_by_number = FALSE;
163         prefs->sort_by_size = FALSE;
164         prefs->sort_by_date = FALSE;
165         prefs->sort_by_from = FALSE;
166         prefs->sort_by_subject = FALSE;
167         prefs->sort_by_score = FALSE;
168         prefs->sort_descending = FALSE;
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->processing = NULL;
190
191         prefs->newmailcheck = TRUE;
192         prefs->offlinesync = FALSE;
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         g_free(prefs->default_to);
209         g_free(prefs->default_reply_to);
210         g_free(prefs);
211 }
212
213 gint folder_item_prefs_get_sort_mode(FolderItem * item)
214 {
215         g_assert(item != NULL);
216         g_warning("folder_item_prefs_get_sort_mode() should never be called\n");
217         return item->sort_key;
218 }
219
220 gint folder_item_prefs_get_sort_type(FolderItem * item)
221 {
222         g_assert(item != NULL);
223         g_warning("folder_item_prefs_get_sort_type() should never be called\n");
224         return item->sort_type;
225 }
226
227 #define SAFE_STRING(str) \
228         (str) ? (str) : ""
229
230 void folder_item_prefs_copy_prefs(FolderItem * src, FolderItem * dest)
231 {
232         GSList *tmp_prop_list = NULL, *tmp;
233         folder_item_prefs_read_config(src);
234
235         tmp_prefs.directory                     = g_strdup(src->prefs->directory);
236         tmp_prefs.sort_by_number                = src->prefs->sort_by_number;
237         tmp_prefs.sort_by_size                  = src->prefs->sort_by_size;
238         tmp_prefs.sort_by_date                  = src->prefs->sort_by_date;
239         tmp_prefs.sort_by_from                  = src->prefs->sort_by_from;
240         tmp_prefs.sort_by_subject               = src->prefs->sort_by_subject;
241         tmp_prefs.sort_by_score                 = src->prefs->sort_by_score;
242         tmp_prefs.sort_descending               = src->prefs->sort_descending;
243         tmp_prefs.enable_thread                 = src->prefs->enable_thread;
244         tmp_prefs.enable_processing             = src->prefs->enable_processing;
245         tmp_prefs.newmailcheck                  = src->prefs->newmailcheck;
246         tmp_prefs.offlinesync                  = src->prefs->offlinesync;
247
248         prefs_matcher_read_config();
249
250         for (tmp = src->prefs->processing; tmp != NULL && tmp->data != NULL;) {
251                 FilteringProp *prop = (FilteringProp *)tmp->data;
252                 
253                 tmp_prop_list = g_slist_append(tmp_prop_list,
254                                            filteringprop_copy(prop));
255                 tmp = tmp->next;
256         }
257         tmp_prefs.processing                    = tmp_prop_list;
258         
259         tmp_prefs.request_return_receipt        = src->prefs->request_return_receipt;
260         tmp_prefs.enable_default_to             = src->prefs->enable_default_to;
261         tmp_prefs.default_to                    = g_strdup(src->prefs->default_to);
262         tmp_prefs.enable_default_reply_to       = src->prefs->enable_default_reply_to;
263         tmp_prefs.default_reply_to              = g_strdup(src->prefs->default_reply_to);
264         tmp_prefs.enable_simplify_subject       = src->prefs->enable_simplify_subject;
265         tmp_prefs.simplify_subject_regexp       = g_strdup(src->prefs->simplify_subject_regexp);
266         tmp_prefs.enable_folder_chmod           = src->prefs->enable_folder_chmod;
267         tmp_prefs.folder_chmod                  = src->prefs->folder_chmod;
268         tmp_prefs.enable_default_account        = src->prefs->enable_default_account;
269         tmp_prefs.default_account               = src->prefs->default_account;
270 #if USE_ASPELL
271         tmp_prefs.enable_default_dictionary     = src->prefs->enable_default_dictionary;
272         tmp_prefs.default_dictionary            = g_strdup(src->prefs->default_dictionary);
273 #endif
274         tmp_prefs.save_copy_to_folder           = src->prefs->save_copy_to_folder;
275         tmp_prefs.color                         = src->prefs->color;
276
277         *dest->prefs = tmp_prefs;
278         folder_item_prefs_save_config(dest);
279 }