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