Fix CID 1596595: Resource leak
[claws.git] / src / folder_item_prefs.c
blobc6fc6f02450e6190f4c1e4b4fc73a1e09329f22d
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
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.
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.
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/>.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #include "claws-features.h"
23 #endif
25 #include <glib.h>
26 #include <glib/gi18n.h>
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"
36 FolderItemPrefs tmp_prefs;
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},
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}
139 static FolderItemPrefs *folder_item_prefs_clear(FolderItemPrefs *prefs);
141 void folder_item_prefs_read_config(FolderItem * item)
143 gchar * id;
144 gchar *rcpath;
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);
153 *item->prefs = tmp_prefs;
155 prefs_update_config_version_folder_item(item);
158 void folder_item_prefs_save_config(FolderItem * item)
160 gchar * id;
162 tmp_prefs = * item->prefs;
164 id = folder_item_get_identifier(item);
165 if (id == NULL)
166 return;
168 debug_print("saving prefs for %s\n", id);
169 prefs_write_config(param, id, FOLDERITEM_RC);
170 g_free(id);
173 static gboolean folder_item_prefs_save_config_func(GNode *node, gpointer data)
175 FolderItem *item = (FolderItem *) node->data;
176 folder_item_prefs_save_config(item);
177 return FALSE;
180 void folder_item_prefs_save_config_recursive(FolderItem * item)
182 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL,
183 -1, folder_item_prefs_save_config_func, NULL);
186 void folder_prefs_save_config_recursive(Folder *folder)
188 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
189 -1, folder_item_prefs_save_config_func, NULL);
192 static FolderItemPrefs *folder_item_prefs_clear(FolderItemPrefs *prefs)
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;
222 prefs->enable_processing = FALSE;
223 prefs->enable_processing_when_opening = FALSE;
224 prefs->processing = NULL;
225 prefs->color.red = 0;
226 prefs->color.green = 0;
227 prefs->color.blue = 0;
228 prefs->color.alpha = 1;
230 prefs->newmailcheck = TRUE;
231 prefs->offlinesync = FALSE;
232 prefs->offlinesync_days = 0;
233 prefs->remove_old_bodies = FALSE;
234 prefs->promote_html_part = HTML_PROMOTE_DEFAULT;
235 prefs->skip_on_goto_unread_or_new = FALSE;
237 prefs->compose_with_format = FALSE;
238 prefs->compose_subject_format = NULL;
239 prefs->compose_body_format = NULL;
240 prefs->compose_override_from_format = NULL;
241 prefs->reply_with_format = FALSE;
242 prefs->reply_quotemark = NULL;
243 prefs->reply_body_format = NULL;
244 prefs->reply_override_from_format = NULL;
245 prefs->forward_with_format = FALSE;
246 prefs->forward_quotemark = NULL;
247 prefs->forward_body_format = NULL;
248 prefs->forward_override_from_format = NULL;
249 return prefs;
252 FolderItemPrefs * folder_item_prefs_new(void)
254 FolderItemPrefs * prefs;
256 prefs = g_new0(FolderItemPrefs, 1);
258 return folder_item_prefs_clear(prefs);
261 void folder_item_prefs_free(FolderItemPrefs * prefs)
263 g_free(prefs->default_from);
264 g_free(prefs->default_to);
265 g_free(prefs->default_reply_to);
266 g_free(prefs->default_cc);
267 g_free(prefs->default_bcc);
268 g_free(prefs->default_replyto);
269 g_free(prefs->compose_subject_format);
270 g_free(prefs->compose_body_format);
271 g_free(prefs->compose_override_from_format);
272 g_free(prefs->reply_quotemark);
273 g_free(prefs->reply_body_format);
274 g_free(prefs->reply_override_from_format);
275 g_free(prefs->forward_quotemark);
276 g_free(prefs->forward_body_format);
277 g_free(prefs->forward_override_from_format);
278 g_free(prefs);
281 #define SAFE_STRING(str) \
282 (str) ? (str) : ""
284 void folder_item_prefs_copy_prefs(FolderItem * src, FolderItem * dest)
286 GSList *tmp_prop_list = NULL, *tmp;
287 folder_item_prefs_read_config(src);
289 tmp_prefs.directory = g_strdup(src->prefs->directory);
290 tmp_prefs.enable_processing = src->prefs->enable_processing;
291 tmp_prefs.enable_processing_when_opening = src->prefs->enable_processing_when_opening;
292 tmp_prefs.newmailcheck = src->prefs->newmailcheck;
293 tmp_prefs.offlinesync = src->prefs->offlinesync;
294 tmp_prefs.offlinesync_days = src->prefs->offlinesync_days;
295 tmp_prefs.remove_old_bodies = src->prefs->remove_old_bodies;
296 tmp_prefs.promote_html_part = src->prefs->promote_html_part;
297 tmp_prefs.skip_on_goto_unread_or_new = src->prefs->skip_on_goto_unread_or_new;
299 prefs_matcher_read_config();
301 for (tmp = src->prefs->processing; tmp != NULL && tmp->data != NULL;) {
302 FilteringProp *prop = (FilteringProp *)tmp->data;
304 tmp_prop_list = g_slist_append(tmp_prop_list,
305 filteringprop_copy(prop));
306 tmp = tmp->next;
308 tmp_prefs.processing = tmp_prop_list;
310 tmp_prefs.request_return_receipt = src->prefs->request_return_receipt;
311 tmp_prefs.enable_default_from = src->prefs->enable_default_from;
312 tmp_prefs.default_from = g_strdup(src->prefs->default_from);
313 tmp_prefs.enable_default_to = src->prefs->enable_default_to;
314 tmp_prefs.default_to = g_strdup(src->prefs->default_to);
315 tmp_prefs.enable_default_reply_to = src->prefs->enable_default_reply_to;
316 tmp_prefs.default_reply_to = g_strdup(src->prefs->default_reply_to);
317 tmp_prefs.enable_default_cc = src->prefs->enable_default_cc;
318 tmp_prefs.default_cc = g_strdup(src->prefs->default_cc);
319 tmp_prefs.enable_default_bcc = src->prefs->enable_default_bcc;
320 tmp_prefs.default_bcc = g_strdup(src->prefs->default_bcc);
321 tmp_prefs.enable_default_replyto = src->prefs->enable_default_replyto;
322 tmp_prefs.default_replyto = g_strdup(src->prefs->default_replyto);
323 tmp_prefs.enable_simplify_subject = src->prefs->enable_simplify_subject;
324 tmp_prefs.simplify_subject_regexp = g_strdup(src->prefs->simplify_subject_regexp);
325 tmp_prefs.enable_folder_chmod = src->prefs->enable_folder_chmod;
326 tmp_prefs.folder_chmod = src->prefs->folder_chmod;
327 tmp_prefs.enable_default_account = src->prefs->enable_default_account;
328 tmp_prefs.default_account = src->prefs->default_account;
329 #if USE_ENCHANT
330 tmp_prefs.enable_default_dictionary = src->prefs->enable_default_dictionary;
331 tmp_prefs.default_dictionary = g_strdup(src->prefs->default_dictionary);
332 tmp_prefs.enable_default_alt_dictionary = src->prefs->enable_default_alt_dictionary;
333 tmp_prefs.default_alt_dictionary = g_strdup(src->prefs->default_alt_dictionary);
334 #endif
335 tmp_prefs.always_sign = src->prefs->always_sign;
336 tmp_prefs.always_encrypt = src->prefs->always_encrypt;
337 tmp_prefs.save_copy_to_folder = src->prefs->save_copy_to_folder;
338 tmp_prefs.color = src->prefs->color;
340 tmp_prefs.compose_with_format = src->prefs->compose_with_format;
341 tmp_prefs.compose_subject_format = g_strdup(src->prefs->compose_subject_format);
342 tmp_prefs.compose_body_format = g_strdup(src->prefs->compose_body_format);
343 tmp_prefs.compose_override_from_format = g_strdup(src->prefs->compose_override_from_format);
344 tmp_prefs.reply_with_format = src->prefs->reply_with_format;
345 tmp_prefs.reply_quotemark = g_strdup(src->prefs->reply_quotemark);
346 tmp_prefs.reply_body_format = g_strdup(src->prefs->reply_body_format);
347 tmp_prefs.reply_override_from_format = g_strdup(src->prefs->reply_override_from_format);
348 tmp_prefs.forward_with_format = src->prefs->forward_with_format;
349 tmp_prefs.forward_quotemark = g_strdup(src->prefs->forward_quotemark);
350 tmp_prefs.forward_body_format = g_strdup(src->prefs->forward_body_format);
351 tmp_prefs.forward_override_from_format = g_strdup(src->prefs->forward_override_from_format);
353 *dest->prefs = tmp_prefs;
354 folder_item_prefs_save_config(dest);
355 prefs_matcher_write_config();
357 dest->collapsed = src->collapsed;
358 dest->thread_collapsed = src->thread_collapsed;
359 dest->threaded = src->threaded;
360 dest->ret_rcpt = src->ret_rcpt;
361 dest->hide_read_msgs = src->hide_read_msgs;
362 dest->hide_del_msgs = src->hide_del_msgs;
363 dest->hide_read_threads = src->hide_read_threads;
364 dest->sort_key = src->sort_key;
365 dest->sort_type = src->sort_type;