add 'Default From:' to folder prefs
[claws.git] / src / folder_item_prefs.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2021 the Claws Mail team and 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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include <glib.h>
26 #include <glib/gi18n.h>
27
28 #include "defs.h"
29 #include "folder.h"
30 #include "utils.h"
31 #include "prefs_gtk.h"
32 #include "filtering.h"
33 #include "folder_item_prefs.h"
34 #include "prefs_migration.h"
35
36 FolderItemPrefs tmp_prefs;
37
38 static PrefParam param[] = {
39         {"enable_default_from", "", &tmp_prefs.enable_default_from, P_BOOL,
40          NULL, NULL, NULL},
41         {"default_from", "", &tmp_prefs.default_from, P_STRING,
42          NULL, NULL, NULL},
43         {"enable_default_to", "", &tmp_prefs.enable_default_to, P_BOOL,
44          NULL, NULL, NULL},
45         {"default_to", "", &tmp_prefs.default_to, P_STRING,
46          NULL, NULL, NULL},
47         {"enable_default_reply_to", "", &tmp_prefs.enable_default_reply_to, P_BOOL,
48          NULL, NULL, NULL},
49         {"default_reply_to", "", &tmp_prefs.default_reply_to, P_STRING,
50          NULL, NULL, NULL},
51         {"enable_default_cc", "", &tmp_prefs.enable_default_cc, P_BOOL,
52          NULL, NULL, NULL},
53         {"default_cc", "", &tmp_prefs.default_cc, P_STRING,
54          NULL, NULL, NULL},
55         {"enable_default_bcc", "", &tmp_prefs.enable_default_bcc, P_BOOL,
56          NULL, NULL, NULL},
57         {"default_bcc", "", &tmp_prefs.default_bcc, P_STRING,
58          NULL, NULL, NULL},
59         {"enable_default_replyto", "", &tmp_prefs.enable_default_replyto, P_BOOL,
60          NULL, NULL, NULL},
61         {"default_replyto", "", &tmp_prefs.default_replyto, P_STRING,
62          NULL, NULL, NULL},
63         {"enable_simplify_subject", "", &tmp_prefs.enable_simplify_subject, P_BOOL,
64          NULL, NULL, NULL},
65         {"simplify_subject_regexp", "", &tmp_prefs.simplify_subject_regexp, P_STRING,
66          NULL, NULL, NULL},
67         {"enable_folder_chmod", "", &tmp_prefs.enable_folder_chmod, P_BOOL,
68          NULL, NULL, NULL},
69         {"folder_chmod", "", &tmp_prefs.folder_chmod, P_INT,
70          NULL, NULL, NULL},
71         {"enable_default_account", "", &tmp_prefs.enable_default_account, P_BOOL,
72          NULL, NULL, NULL},
73         {"default_account", NULL, &tmp_prefs.default_account, P_INT,
74          NULL, NULL, NULL},
75 #if USE_ENCHANT
76         {"enable_default_dictionary", "", &tmp_prefs.enable_default_dictionary, P_BOOL,
77          NULL, NULL, NULL},
78         {"default_dictionary", NULL, &tmp_prefs.default_dictionary, P_STRING,
79          NULL, NULL, NULL},
80         {"enable_default_alt_dictionary", "", &tmp_prefs.enable_default_alt_dictionary, P_BOOL,
81          NULL, NULL, NULL},
82         {"default_alt_dictionary", NULL, &tmp_prefs.default_alt_dictionary, P_STRING,
83          NULL, NULL, NULL},
84 #endif   
85         {"always_sign", "0", &tmp_prefs.always_sign, P_ENUM,
86          NULL, NULL, NULL},
87         {"always_encrypt", "0", &tmp_prefs.always_encrypt, P_ENUM,
88          NULL, NULL, NULL},
89         {"save_copy_to_folder", NULL, &tmp_prefs.save_copy_to_folder, P_BOOL,
90          NULL, NULL, NULL},
91         {"folder_color", "#000000", &tmp_prefs.color, P_COLOR,
92          NULL, NULL, NULL},
93         {"enable_processing", "FALSE", &tmp_prefs.enable_processing, P_BOOL,
94          NULL, NULL, NULL},
95         {"enable_processing_when_opening", "TRUE", &tmp_prefs.enable_processing_when_opening, P_BOOL,
96          NULL, NULL, NULL},
97         {"newmailcheck", "TRUE", &tmp_prefs.newmailcheck, P_BOOL,
98          NULL, NULL, NULL},
99         {"offlinesync", "FALSE", &tmp_prefs.offlinesync, P_BOOL,
100          NULL, NULL, NULL},
101         {"offlinesync_days", "0", &tmp_prefs.offlinesync_days, P_INT,
102          NULL, NULL, NULL},
103         {"remove_old_bodies", "FALSE", &tmp_prefs.remove_old_bodies, P_BOOL,
104          NULL, NULL, NULL},
105         {"promote_html_part", "0", &tmp_prefs.promote_html_part, P_ENUM,
106          NULL, NULL, NULL},
107         {"skip_on_goto_unread_or_new", "FALSE", &tmp_prefs.skip_on_goto_unread_or_new, P_BOOL,
108          NULL, NULL, NULL},
109
110         {"compose_with_format", "FALSE", &tmp_prefs.compose_with_format, P_BOOL,
111          NULL, NULL, NULL},
112         {"compose_override_from_format", NULL, &tmp_prefs.compose_override_from_format, P_STRING,
113          NULL, NULL, NULL},
114         {"compose_subject_format", NULL, &tmp_prefs.compose_subject_format, P_STRING,
115          NULL, NULL, NULL},
116         {"compose_body_format", NULL, &tmp_prefs.compose_body_format, P_STRING,
117          NULL, NULL, NULL},
118         {"reply_with_format", "FALSE", &tmp_prefs.reply_with_format, P_BOOL,
119          NULL, NULL, NULL},
120         {"reply_override_from_format", NULL, &tmp_prefs.reply_override_from_format, P_STRING,
121          NULL, NULL, NULL},
122         {"reply_quotemark", NULL, &tmp_prefs.reply_quotemark, P_STRING,
123          NULL, NULL, NULL},
124         {"reply_body_format", NULL, &tmp_prefs.reply_body_format, P_STRING,
125          NULL, NULL, NULL},
126         {"forward_with_format", "FALSE", &tmp_prefs.forward_with_format, P_BOOL,
127          NULL, NULL, NULL},
128         {"forward_override_from_format", NULL, &tmp_prefs.forward_override_from_format, P_STRING,
129          NULL, NULL, NULL},
130         {"forward_quotemark", NULL, &tmp_prefs.forward_quotemark, P_STRING,
131          NULL, NULL, NULL},
132         {"forward_body_format", NULL, &tmp_prefs.forward_body_format, P_STRING,
133          NULL, NULL, NULL},
134         {"config_version", "-1", &tmp_prefs.config_version, P_INT,
135          NULL, NULL, NULL},
136         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
137 };
138
139 static FolderItemPrefs *folder_item_prefs_clear(FolderItemPrefs *prefs);
140
141 void folder_item_prefs_read_config(FolderItem * item)
142 {
143         gchar * id;
144         gchar *rcpath;
145
146         id = folder_item_get_identifier(item);
147         folder_item_prefs_clear(&tmp_prefs);
148         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, FOLDERITEM_RC, NULL);
149         prefs_read_config(param, id, rcpath, NULL);
150         g_free(id);
151         g_free(rcpath);
152
153         *item->prefs = tmp_prefs;
154
155         prefs_update_config_version_folder_item(item);
156 }
157
158 void folder_item_prefs_save_config(FolderItem * item)
159 {       
160         gchar * id;
161
162         tmp_prefs = * item->prefs;
163
164         id = folder_item_get_identifier(item);
165         if (id == NULL)
166                 return;
167
168         debug_print("saving prefs for %s\n", id);
169         prefs_write_config(param, id, FOLDERITEM_RC);
170         g_free(id);
171 }
172
173 static gboolean folder_item_prefs_save_config_func(GNode *node, gpointer data)
174 {
175         FolderItem *item = (FolderItem *) node->data;
176         folder_item_prefs_save_config(item);
177         return FALSE;
178 }
179
180 void folder_item_prefs_save_config_recursive(FolderItem * item)
181 {       
182         g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL,
183                         -1, folder_item_prefs_save_config_func, NULL);
184 }
185
186 void folder_prefs_save_config_recursive(Folder *folder)
187 {       
188         g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
189                         -1, folder_item_prefs_save_config_func, NULL);
190 }
191
192 static FolderItemPrefs *folder_item_prefs_clear(FolderItemPrefs *prefs)
193 {
194         prefs->enable_default_from = FALSE;
195         prefs->default_from = NULL;
196         prefs->enable_default_to = FALSE;
197         prefs->default_to = NULL;
198         prefs->enable_default_reply_to = FALSE;
199         prefs->default_reply_to = NULL;
200         prefs->enable_default_cc = FALSE;
201         prefs->default_cc = NULL;
202         prefs->enable_default_bcc = FALSE;
203         prefs->default_bcc = NULL;
204         prefs->enable_default_replyto = FALSE;
205         prefs->default_replyto = NULL;
206         prefs->enable_simplify_subject = FALSE;
207         prefs->simplify_subject_regexp = NULL;
208         prefs->enable_folder_chmod = FALSE;
209         prefs->folder_chmod = 0;
210         prefs->enable_default_account = FALSE;
211         prefs->default_account = 0;
212 #if USE_ENCHANT
213         prefs->enable_default_dictionary = FALSE;
214         prefs->default_dictionary = NULL;
215         prefs->enable_default_alt_dictionary = FALSE;
216         prefs->default_alt_dictionary = NULL;
217 #endif
218         prefs->always_sign = SIGN_OR_ENCRYPT_DEFAULT;
219         prefs->always_encrypt = SIGN_OR_ENCRYPT_DEFAULT;
220         prefs->save_copy_to_folder = FALSE;
221         prefs->color = 0;
222
223         prefs->enable_processing = FALSE;
224         prefs->enable_processing_when_opening = FALSE;
225         prefs->processing = NULL;
226
227         prefs->newmailcheck = TRUE;
228         prefs->offlinesync = FALSE;
229         prefs->offlinesync_days = 0;
230         prefs->remove_old_bodies = FALSE;
231         prefs->promote_html_part = HTML_PROMOTE_DEFAULT;
232         prefs->skip_on_goto_unread_or_new = FALSE;
233
234         prefs->compose_with_format = FALSE;
235         prefs->compose_subject_format = NULL;
236         prefs->compose_body_format = NULL;
237         prefs->compose_override_from_format = NULL;
238         prefs->reply_with_format = FALSE;
239         prefs->reply_quotemark = NULL;
240         prefs->reply_body_format = NULL;
241         prefs->reply_override_from_format = NULL;
242         prefs->forward_with_format = FALSE;
243         prefs->forward_quotemark = NULL;
244         prefs->forward_body_format = NULL;
245         prefs->forward_override_from_format = NULL;
246         return prefs;
247 }
248
249 FolderItemPrefs * folder_item_prefs_new(void)
250 {
251         FolderItemPrefs * prefs;
252
253         prefs = g_new0(FolderItemPrefs, 1);
254
255         return folder_item_prefs_clear(prefs);
256 }
257
258 void folder_item_prefs_free(FolderItemPrefs * prefs)
259 {
260         g_free(prefs->default_from);
261         g_free(prefs->default_to);
262         g_free(prefs->default_reply_to);
263         g_free(prefs->default_cc);
264         g_free(prefs->default_bcc);
265         g_free(prefs->default_replyto);
266         g_free(prefs->compose_subject_format);
267         g_free(prefs->compose_body_format);
268         g_free(prefs->compose_override_from_format);
269         g_free(prefs->reply_quotemark);
270         g_free(prefs->reply_body_format);
271         g_free(prefs->reply_override_from_format);
272         g_free(prefs->forward_quotemark);
273         g_free(prefs->forward_body_format);
274         g_free(prefs->forward_override_from_format);
275         g_free(prefs);
276 }
277
278 #define SAFE_STRING(str) \
279         (str) ? (str) : ""
280
281 void folder_item_prefs_copy_prefs(FolderItem * src, FolderItem * dest)
282 {
283         GSList *tmp_prop_list = NULL, *tmp;
284         folder_item_prefs_read_config(src);
285
286         tmp_prefs.directory                     = g_strdup(src->prefs->directory);
287         tmp_prefs.enable_processing             = src->prefs->enable_processing;
288         tmp_prefs.enable_processing_when_opening = src->prefs->enable_processing_when_opening;
289         tmp_prefs.newmailcheck                  = src->prefs->newmailcheck;
290         tmp_prefs.offlinesync                   = src->prefs->offlinesync;
291         tmp_prefs.offlinesync_days              = src->prefs->offlinesync_days;
292         tmp_prefs.remove_old_bodies             = src->prefs->remove_old_bodies;
293         tmp_prefs.promote_html_part             = src->prefs->promote_html_part;
294         tmp_prefs.skip_on_goto_unread_or_new    = src->prefs->skip_on_goto_unread_or_new;
295
296         prefs_matcher_read_config();
297
298         for (tmp = src->prefs->processing; tmp != NULL && tmp->data != NULL;) {
299                 FilteringProp *prop = (FilteringProp *)tmp->data;
300                 
301                 tmp_prop_list = g_slist_append(tmp_prop_list,
302                                            filteringprop_copy(prop));
303                 tmp = tmp->next;
304         }
305         tmp_prefs.processing                    = tmp_prop_list;
306         
307         tmp_prefs.request_return_receipt        = src->prefs->request_return_receipt;
308         tmp_prefs.enable_default_from           = src->prefs->enable_default_from;
309         tmp_prefs.default_from                  = g_strdup(src->prefs->default_from);
310         tmp_prefs.enable_default_to             = src->prefs->enable_default_to;
311         tmp_prefs.default_to                    = g_strdup(src->prefs->default_to);
312         tmp_prefs.enable_default_reply_to       = src->prefs->enable_default_reply_to;
313         tmp_prefs.default_reply_to              = g_strdup(src->prefs->default_reply_to);
314         tmp_prefs.enable_default_cc             = src->prefs->enable_default_cc;
315         tmp_prefs.default_cc                    = g_strdup(src->prefs->default_cc);
316         tmp_prefs.enable_default_bcc            = src->prefs->enable_default_bcc;
317         tmp_prefs.default_bcc                   = g_strdup(src->prefs->default_bcc);
318         tmp_prefs.enable_default_replyto                = src->prefs->enable_default_replyto;
319         tmp_prefs.default_replyto                       = g_strdup(src->prefs->default_replyto);
320         tmp_prefs.enable_simplify_subject       = src->prefs->enable_simplify_subject;
321         tmp_prefs.simplify_subject_regexp       = g_strdup(src->prefs->simplify_subject_regexp);
322         tmp_prefs.enable_folder_chmod           = src->prefs->enable_folder_chmod;
323         tmp_prefs.folder_chmod                  = src->prefs->folder_chmod;
324         tmp_prefs.enable_default_account        = src->prefs->enable_default_account;
325         tmp_prefs.default_account               = src->prefs->default_account;
326 #if USE_ENCHANT
327         tmp_prefs.enable_default_dictionary     = src->prefs->enable_default_dictionary;
328         tmp_prefs.default_dictionary            = g_strdup(src->prefs->default_dictionary);
329         tmp_prefs.enable_default_alt_dictionary = src->prefs->enable_default_alt_dictionary;
330         tmp_prefs.default_alt_dictionary        = g_strdup(src->prefs->default_alt_dictionary);
331 #endif
332         tmp_prefs.always_sign           = src->prefs->always_sign;
333         tmp_prefs.always_encrypt    = src->prefs->always_encrypt;
334         tmp_prefs.save_copy_to_folder           = src->prefs->save_copy_to_folder;
335         tmp_prefs.color                         = src->prefs->color;
336
337         tmp_prefs.compose_with_format = src->prefs->compose_with_format;
338         tmp_prefs.compose_subject_format = g_strdup(src->prefs->compose_subject_format);
339         tmp_prefs.compose_body_format = g_strdup(src->prefs->compose_body_format);
340         tmp_prefs.compose_override_from_format = g_strdup(src->prefs->compose_override_from_format);
341         tmp_prefs.reply_with_format = src->prefs->reply_with_format;
342         tmp_prefs.reply_quotemark = g_strdup(src->prefs->reply_quotemark);
343         tmp_prefs.reply_body_format = g_strdup(src->prefs->reply_body_format);
344         tmp_prefs.reply_override_from_format = g_strdup(src->prefs->reply_override_from_format);
345         tmp_prefs.forward_with_format = src->prefs->forward_with_format;
346         tmp_prefs.forward_quotemark = g_strdup(src->prefs->forward_quotemark);
347         tmp_prefs.forward_body_format = g_strdup(src->prefs->forward_body_format);
348         tmp_prefs.forward_override_from_format = g_strdup(src->prefs->forward_override_from_format);
349
350         *dest->prefs = tmp_prefs;
351         folder_item_prefs_save_config(dest);
352         prefs_matcher_write_config();
353
354         dest->collapsed = src->collapsed;
355         dest->thread_collapsed = src->thread_collapsed;
356         dest->threaded  = src->threaded;
357         dest->ret_rcpt  = src->ret_rcpt;
358         dest->hide_read_msgs = src->hide_read_msgs;
359         dest->hide_del_msgs = src->hide_del_msgs;
360         dest->hide_read_threads = src->hide_read_threads;
361         dest->sort_key  = src->sort_key;
362         dest->sort_type = src->sort_type;
363 }