Fix wrong free, and fix useless g_slist_concat
[claws.git] / src / prefs_quote.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2005-2012 Colin Leroy <colin@colino.net> & 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 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 "defs.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include <glib.h>
31 #include <glib/gi18n.h>
32 #include <gtk/gtk.h>
33 #include <gdk/gdkkeysyms.h>
34
35 #include "prefs_common.h"
36 #include "prefs_gtk.h"
37 #include "prefs_template.h"
38 #include "alertpanel.h"
39
40 #include "gtk/gtkutils.h"
41 #include "gtk/prefswindow.h"
42
43 #include "manage_window.h"
44 #include "quote_fmt.h"
45
46 typedef struct _QuotePage
47 {
48         PrefsPage page;
49
50         GtkWidget *window;
51
52         GtkWidget *checkbtn_compose_with_format;
53         GtkWidget *entry_subject;
54         GtkWidget *text_format;
55         GtkWidget *entry_quotemark;
56         GtkWidget *text_quotefmt;
57         GtkWidget *entry_fw_quotemark;
58         GtkWidget *text_fw_quotefmt;
59         GtkWidget *btn_quotedesc;
60 } QuotePage;
61
62 QuotePage *prefs_quote;
63
64 static void prefs_quote_set_default_new_msg_fmt(void)
65 {
66         cm_return_if_fail(prefs_quote->text_format != NULL);
67
68         pref_set_textview_from_pref(GTK_TEXT_VIEW(prefs_quote->text_format),
69                 _("Hello,\\n"));
70 }
71
72 static void prefs_quote_set_default_reply_fmt(void)
73 {
74         cm_return_if_fail(prefs_quote->text_quotefmt != NULL);
75
76         pref_set_textview_from_pref(GTK_TEXT_VIEW(prefs_quote->text_quotefmt),
77                 _("On %d\\n%f wrote:\\n\\n%q"));
78 }
79
80 static void prefs_quote_set_default_forward_fmt(void)
81 {
82         cm_return_if_fail(prefs_quote->text_fw_quotefmt != NULL);
83
84         pref_set_textview_from_pref(GTK_TEXT_VIEW(prefs_quote->text_fw_quotefmt),
85                 _("\\n\\nBegin forwarded message:\\n\\n"
86                 "?d{Date: %d\\n}?f{From: %f\\n}?t{To: %t\\n}?c{Cc: %c\\n}"
87                 "?n{Newsgroups: %n\\n}?s{Subject: %s\\n}\\n\\n%M"));
88 }
89
90 static void prefs_quote_create_widget(PrefsPage *_page, GtkWindow *window, 
91                                   gpointer data)
92 {
93         QuotePage *prefs_quote = (QuotePage *) _page;
94         
95         GtkWidget *vbox;
96         GtkWidget *vbox2;
97         GtkWidget *notebook;
98
99         vbox = gtk_vbox_new (FALSE, 0);
100         gtk_widget_show (vbox);
101
102         notebook = gtk_notebook_new();
103         gtk_widget_show(notebook);
104         gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 0);
105
106         /* new message */
107         vbox2 = gtk_vbox_new (FALSE, VSPACING);
108         gtk_widget_show (vbox2);
109         gtk_container_set_border_width (GTK_CONTAINER (vbox2), VBOX_BORDER);
110         
111         quotefmt_create_new_msg_fmt_widgets(
112                                 window,
113                                 vbox2,
114                                 &prefs_quote->checkbtn_compose_with_format,
115                                 NULL,
116                                 &prefs_quote->entry_subject,
117                                 &prefs_quote->text_format,
118                                 TRUE, prefs_quote_set_default_new_msg_fmt);
119         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), vbox2, gtk_label_new(_("Compose")));
120
121         /* reply */
122         vbox2 = gtk_vbox_new (FALSE, VSPACING);
123         gtk_widget_show (vbox2);
124         gtk_container_set_border_width (GTK_CONTAINER (vbox2), VBOX_BORDER);
125         
126         quotefmt_create_reply_fmt_widgets(
127                                 window,
128                                 vbox2,
129                                 NULL,
130                                 NULL,
131                                 &prefs_quote->entry_quotemark,
132                                 &prefs_quote->text_quotefmt,
133                                 TRUE, prefs_quote_set_default_reply_fmt);
134         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), vbox2, gtk_label_new(_("Reply")));
135
136         /* forward */
137         vbox2 = gtk_vbox_new (FALSE, VSPACING);
138         gtk_widget_show (vbox2);
139         gtk_container_set_border_width (GTK_CONTAINER (vbox2), VBOX_BORDER);
140         
141         quotefmt_create_forward_fmt_widgets(
142                                 window,
143                                 vbox2,
144                                 NULL,
145                                 NULL,
146                                 &prefs_quote->entry_fw_quotemark,
147                                 &prefs_quote->text_fw_quotefmt,
148                                 TRUE, prefs_quote_set_default_forward_fmt);
149         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), vbox2, gtk_label_new(_("Forward")));
150
151         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_quote->checkbtn_compose_with_format),
152                         prefs_common.compose_with_format);
153         pref_set_entry_from_pref(GTK_ENTRY(prefs_quote->entry_subject),
154                         prefs_common.compose_subject_format);
155         if (prefs_common.compose_body_format)
156                 pref_set_textview_from_pref(GTK_TEXT_VIEW(prefs_quote->text_format),
157                                 prefs_common.compose_body_format);
158         else
159                 prefs_quote_set_default_new_msg_fmt();
160
161         gtk_entry_set_text(GTK_ENTRY(prefs_quote->entry_quotemark), 
162                         prefs_common.quotemark?prefs_common.quotemark:"");
163         if (prefs_common.quotefmt)
164                 pref_set_textview_from_pref(GTK_TEXT_VIEW(prefs_quote->text_quotefmt),
165                                 prefs_common.quotefmt);
166         else
167                 prefs_quote_set_default_reply_fmt();
168
169         gtk_entry_set_text(GTK_ENTRY(prefs_quote->entry_fw_quotemark), 
170                         prefs_common.fw_quotemark?prefs_common.fw_quotemark:"");
171         if (prefs_common.fw_quotefmt)
172                 pref_set_textview_from_pref(GTK_TEXT_VIEW(prefs_quote->text_fw_quotefmt),
173                                 prefs_common.fw_quotefmt);
174         else
175                 prefs_quote_set_default_forward_fmt();
176
177         prefs_quote->window             = GTK_WIDGET(window);
178         prefs_quote->page.widget = vbox;
179 }
180
181 static void prefs_quote_save(PrefsPage *_page)
182 {
183         QuotePage *page = (QuotePage *) _page;
184         
185         g_free(prefs_common.compose_subject_format); 
186         prefs_common.compose_subject_format = NULL;
187         g_free(prefs_common.compose_body_format); 
188         prefs_common.compose_body_format = NULL;
189         g_free(prefs_common.quotefmt); 
190         prefs_common.quotefmt = NULL;
191         g_free(prefs_common.fw_quotefmt); 
192         prefs_common.fw_quotefmt = NULL;
193         g_free(prefs_common.quotemark); 
194         prefs_common.quotemark = NULL;
195         g_free(prefs_common.fw_quotemark); 
196         prefs_common.fw_quotemark = NULL;
197         
198         prefs_common.compose_with_format =
199                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_compose_with_format));
200         prefs_common.compose_subject_format = pref_get_pref_from_entry(
201                         GTK_ENTRY(page->entry_subject));
202         prefs_common.compose_body_format = pref_get_pref_from_textview(
203                         GTK_TEXT_VIEW(page->text_format));
204         quotefmt_check_new_msg_formats(prefs_common.compose_with_format,
205                                                                    NULL,
206                                                                    prefs_common.compose_subject_format,
207                                                                    prefs_common.compose_body_format);
208
209         prefs_common.quotemark = gtk_editable_get_chars(
210                         GTK_EDITABLE(page->entry_quotemark), 0, -1);
211         prefs_common.quotefmt = pref_get_pref_from_textview(
212                         GTK_TEXT_VIEW(page->text_quotefmt));
213         quotefmt_check_reply_formats(TRUE,
214                                                                  NULL,
215                                                                  prefs_common.quotemark,
216                                                                  prefs_common.quotefmt);
217
218         prefs_common.fw_quotemark = gtk_editable_get_chars(
219                         GTK_EDITABLE(page->entry_fw_quotemark), 0, -1);
220         prefs_common.fw_quotefmt = pref_get_pref_from_textview(
221                         GTK_TEXT_VIEW(page->text_fw_quotefmt));
222         quotefmt_check_forward_formats(TRUE,
223                                                                    NULL,
224                                                                    prefs_common.fw_quotemark,
225                                                                    prefs_common.fw_quotefmt);
226 }
227
228 static void prefs_quote_destroy_widget(PrefsPage *_page)
229 {
230 }
231
232 void prefs_quote_init(void)
233 {
234         QuotePage *page;
235         static gchar *path[3];
236
237         path[0] = _("Compose");
238         path[1] = _("Templates");
239         path[2] = NULL;
240
241         page = g_new0(QuotePage, 1);
242         page->page.path = path;
243         page->page.create_widget = prefs_quote_create_widget;
244         page->page.destroy_widget = prefs_quote_destroy_widget;
245         page->page.save_page = prefs_quote_save;
246         page->page.weight = 185.0;
247         prefs_gtk_register_page((PrefsPage *) page);
248         prefs_quote = page;
249 }
250
251 void prefs_quote_done(void)
252 {
253         prefs_gtk_unregister_page((PrefsPage *) prefs_quote);
254         g_free(prefs_quote);
255 }