f9cdd8f98251e9c60c949ebccf83dbc02c878489
[claws.git] / src / folder_item_prefs.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail 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         {"enable_default_alt_dictionary", "", &tmp_prefs.enable_default_alt_dictionary, P_BOOL,
84          NULL, NULL, NULL},
85         {"default_alt_dictionary", NULL, &tmp_prefs.default_alt_dictionary, P_STRING,
86          NULL, NULL, NULL},
87 #endif   
88         {"save_copy_to_folder", NULL, &tmp_prefs.save_copy_to_folder, P_BOOL,
89          NULL, NULL, NULL},
90         {"folder_color", "", &tmp_prefs.color, P_INT,
91          NULL, NULL, NULL},
92         {"enable_processing", "FALSE", &tmp_prefs.enable_processing, P_BOOL,
93          NULL, NULL, NULL},
94         {"newmailcheck", "TRUE", &tmp_prefs.newmailcheck, P_BOOL,
95          NULL, NULL, NULL},
96         {"offlinesync", "FALSE", &tmp_prefs.offlinesync, 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         gchar *rcpath;
107
108         id = folder_item_get_identifier(item);
109         folder_item_prefs_clear(&tmp_prefs);
110         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, FOLDERITEM_RC, NULL);
111         prefs_read_config(param, id, rcpath, NULL);
112         g_free(id);
113         g_free(rcpath);
114
115         *item->prefs = tmp_prefs;
116
117         /*
118          * MIGRATION: next lines are migration code. the idea is that
119          *            if used regularly, claws folder config ends up
120          *            in the same file as sylpheed-main
121          */
122
123         item->ret_rcpt = tmp_prefs.request_return_receipt ? TRUE : FALSE;
124
125         /* MIGRATION: 0.7.8main+ has persistent sort order. claws had the sort
126          *            order in different members, which is ofcourse a little
127          *            bit phoney. */
128         if (item->sort_key == SORT_BY_NONE) {
129                 item->sort_key  = (tmp_prefs.sort_by_number  ? SORT_BY_NUMBER  :
130                                    tmp_prefs.sort_by_size    ? SORT_BY_SIZE    :
131                                    tmp_prefs.sort_by_date    ? SORT_BY_DATE    :
132                                    tmp_prefs.sort_by_from    ? SORT_BY_FROM    :
133                                    tmp_prefs.sort_by_subject ? SORT_BY_SUBJECT :
134                                    tmp_prefs.sort_by_score   ? SORT_BY_SCORE   :
135                                                                  SORT_BY_NONE);
136                 item->sort_type = tmp_prefs.sort_descending ? SORT_DESCENDING : SORT_ASCENDING;
137         }                                                               
138 }
139
140 void folder_item_prefs_save_config(FolderItem * item)
141 {       
142         gchar * id;
143
144         tmp_prefs = * item->prefs;
145
146         id = folder_item_get_identifier(item);
147
148         prefs_write_config(param, id, FOLDERITEM_RC);
149         g_free(id);
150
151         /* MIGRATION: make sure migrated items are not saved
152          */
153 }
154
155 static FolderItemPrefs *folder_item_prefs_clear(FolderItemPrefs *prefs)
156 {
157         prefs->sort_by_number = FALSE;
158         prefs->sort_by_size = FALSE;
159         prefs->sort_by_date = FALSE;
160         prefs->sort_by_from = FALSE;
161         prefs->sort_by_subject = FALSE;
162         prefs->sort_by_score = FALSE;
163         prefs->sort_descending = FALSE;
164
165         prefs->request_return_receipt = FALSE;
166         prefs->enable_default_to = FALSE;
167         prefs->default_to = NULL;
168         prefs->enable_default_reply_to = FALSE;
169         prefs->default_reply_to = NULL;
170         prefs->enable_simplify_subject = FALSE;
171         prefs->simplify_subject_regexp = NULL;
172         prefs->enable_folder_chmod = FALSE;
173         prefs->folder_chmod = 0;
174         prefs->enable_default_account = FALSE;
175         prefs->default_account = 0;
176 #if USE_ASPELL
177         prefs->enable_default_dictionary = FALSE;
178         prefs->default_dictionary = NULL;
179         prefs->enable_default_alt_dictionary = FALSE;
180         prefs->default_alt_dictionary = NULL;
181 #endif
182         prefs->save_copy_to_folder = FALSE;
183         prefs->color = 0;
184
185         prefs->enable_processing = TRUE;
186         prefs->processing = NULL;
187
188         prefs->newmailcheck = TRUE;
189         prefs->offlinesync = FALSE;
190
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         g_free(prefs->default_to);
206         g_free(prefs->default_reply_to);
207         g_free(prefs);
208 }
209
210 #define SAFE_STRING(str) \
211         (str) ? (str) : ""
212
213 void folder_item_prefs_copy_prefs(FolderItem * src, FolderItem * dest)
214 {
215         GSList *tmp_prop_list = NULL, *tmp;
216         folder_item_prefs_read_config(src);
217
218         tmp_prefs.directory                     = g_strdup(src->prefs->directory);
219         tmp_prefs.sort_by_number                = src->prefs->sort_by_number;
220         tmp_prefs.sort_by_size                  = src->prefs->sort_by_size;
221         tmp_prefs.sort_by_date                  = src->prefs->sort_by_date;
222         tmp_prefs.sort_by_from                  = src->prefs->sort_by_from;
223         tmp_prefs.sort_by_subject               = src->prefs->sort_by_subject;
224         tmp_prefs.sort_by_score                 = src->prefs->sort_by_score;
225         tmp_prefs.sort_descending               = src->prefs->sort_descending;
226         tmp_prefs.enable_thread                 = src->prefs->enable_thread;
227         tmp_prefs.enable_processing             = src->prefs->enable_processing;
228         tmp_prefs.newmailcheck                  = src->prefs->newmailcheck;
229         tmp_prefs.offlinesync                  = src->prefs->offlinesync;
230
231         prefs_matcher_read_config();
232
233         for (tmp = src->prefs->processing; tmp != NULL && tmp->data != NULL;) {
234                 FilteringProp *prop = (FilteringProp *)tmp->data;
235                 
236                 tmp_prop_list = g_slist_append(tmp_prop_list,
237                                            filteringprop_copy(prop));
238                 tmp = tmp->next;
239         }
240         tmp_prefs.processing                    = tmp_prop_list;
241         
242         tmp_prefs.request_return_receipt        = src->prefs->request_return_receipt;
243         tmp_prefs.enable_default_to             = src->prefs->enable_default_to;
244         tmp_prefs.default_to                    = g_strdup(src->prefs->default_to);
245         tmp_prefs.enable_default_reply_to       = src->prefs->enable_default_reply_to;
246         tmp_prefs.default_reply_to              = g_strdup(src->prefs->default_reply_to);
247         tmp_prefs.enable_simplify_subject       = src->prefs->enable_simplify_subject;
248         tmp_prefs.simplify_subject_regexp       = g_strdup(src->prefs->simplify_subject_regexp);
249         tmp_prefs.enable_folder_chmod           = src->prefs->enable_folder_chmod;
250         tmp_prefs.folder_chmod                  = src->prefs->folder_chmod;
251         tmp_prefs.enable_default_account        = src->prefs->enable_default_account;
252         tmp_prefs.default_account               = src->prefs->default_account;
253 #if USE_ASPELL
254         tmp_prefs.enable_default_dictionary     = src->prefs->enable_default_dictionary;
255         tmp_prefs.default_dictionary            = g_strdup(src->prefs->default_dictionary);
256         tmp_prefs.enable_default_alt_dictionary = src->prefs->enable_default_alt_dictionary;
257         tmp_prefs.default_alt_dictionary        = g_strdup(src->prefs->default_alt_dictionary);
258 #endif
259         tmp_prefs.save_copy_to_folder           = src->prefs->save_copy_to_folder;
260         tmp_prefs.color                         = src->prefs->color;
261
262         *dest->prefs = tmp_prefs;
263         folder_item_prefs_save_config(dest);
264 }