2011-10-22 [colin] 3.7.10cvs41
[claws.git] / src / prefs_send.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 "gtk/menu.h"
37
38 #include "gtk/gtkutils.h"
39 #include "gtk/prefswindow.h"
40 #include "gtk/combobox.h"
41
42 #include "manage_window.h"
43
44 typedef struct _SendPage
45 {
46         PrefsPage page;
47
48         GtkWidget *window;
49
50         GtkWidget *checkbtn_savemsg;
51         GtkWidget *checkbtn_confirm_send_queued_messages;
52         GtkWidget *checkbtn_never_send_retrcpt;
53         GtkWidget *checkbtn_senddialog;
54         GtkWidget *combobox_charset;
55         GtkWidget *combobox_encoding_method;
56 } SendPage;
57
58 static gchar * prefs_common_charset_set_data_from_optmenu(GtkWidget *widget)
59 {
60         GtkTreeModel *model;
61         GtkTreeIter iter;
62         gchar *data = NULL;
63
64         cm_return_val_if_fail(widget != NULL, NULL);
65
66         cm_return_val_if_fail(gtk_combo_box_get_active_iter(
67                                 GTK_COMBO_BOX(widget), &iter), NULL);
68
69         model = gtk_combo_box_get_model(GTK_COMBO_BOX(widget));
70
71         gtk_tree_model_get(model, &iter, COMBOBOX_DATA, &data, -1);
72
73         return data;
74 }
75
76 typedef struct _combobox_sel_by_data_ctx {
77         GtkComboBox *combobox;
78         gchar *data;
79 } ComboboxSelCtx;
80
81 static gboolean _select_by_data_func(GtkTreeModel *model, GtkTreePath *path,
82                 GtkTreeIter *iter, ComboboxSelCtx *ctx)
83 {
84         GtkComboBox *combobox = ctx->combobox;
85         gchar *data = ctx->data;
86         gchar *curdata; 
87
88         gtk_tree_model_get(GTK_TREE_MODEL(model), iter, COMBOBOX_DATA, &curdata, -1);
89         if ( data != NULL && curdata != NULL && !strcmp(data, curdata) ) {
90                 gtk_combo_box_set_active_iter(combobox, iter);
91                 return TRUE;
92         }
93
94         return FALSE;
95 }
96
97 static void prefs_common_charset_set_optmenu(GtkWidget *widget, gchar *data)
98 {
99         GtkComboBox *combobox = GTK_COMBO_BOX(widget);
100         GtkTreeModel *model;
101         ComboboxSelCtx *ctx = NULL;
102         cm_return_if_fail(combobox != NULL);
103
104         model = gtk_combo_box_get_model(combobox);
105
106         ctx = g_new(ComboboxSelCtx,
107                         sizeof(ComboboxSelCtx));
108         ctx->combobox = combobox;
109         ctx->data = data;
110
111         gtk_tree_model_foreach(model, (GtkTreeModelForeachFunc)_select_by_data_func, ctx);
112         g_free(ctx);
113 }
114
115 static gboolean _combobox_separator_func(GtkTreeModel *model,
116                 GtkTreeIter *iter, gpointer data)
117 {
118         gchar *txt = NULL;
119
120         cm_return_val_if_fail(model != NULL, FALSE);
121
122         gtk_tree_model_get(model, iter, COMBOBOX_TEXT, &txt, -1);
123
124         if( txt == NULL )
125                 return TRUE;
126         return FALSE;
127 }
128
129 static void prefs_send_create_widget(PrefsPage *_page, GtkWindow *window, 
130                                   gpointer data)
131 {
132         SendPage *prefs_send = (SendPage *) _page;
133         
134         GtkWidget *vbox1;
135         GtkWidget *vbox2;
136         GtkWidget *checkbtn_savemsg;
137         GtkWidget *label_outcharset;
138         GtkWidget *combobox_charset;
139         GtkListStore *optmenu;
140         GtkTreeIter iter;
141         GtkCellRenderer *rend;
142         GtkWidget *combobox_encoding;
143         GtkWidget *label_encoding;
144         GtkWidget *checkbtn_senddialog;
145         GtkWidget *checkbtn_confirm_send_queued_messages;
146         GtkWidget *checkbtn_never_send_retrcpt;
147         GtkWidget *table;
148
149         vbox1 = gtk_vbox_new (FALSE, VSPACING);
150         gtk_widget_show (vbox1);
151         gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
152
153         vbox2 = gtk_vbox_new (FALSE, 0);
154         gtk_widget_show (vbox2);
155         gtk_box_pack_start (GTK_BOX (vbox1), vbox2, FALSE, FALSE, 0);
156
157         PACK_CHECK_BUTTON(vbox2, checkbtn_savemsg,
158                         _("Save sent messages to Sent folder"));
159
160         PACK_CHECK_BUTTON(vbox2, checkbtn_confirm_send_queued_messages,
161                         _("Confirm before sending queued messages"));
162
163         PACK_CHECK_BUTTON(vbox2, checkbtn_never_send_retrcpt,
164                         _("Never send Return Receipts"));
165
166         PACK_CHECK_BUTTON(vbox2, checkbtn_senddialog,
167                         _("Show send dialog"));
168
169         table = gtk_table_new(2, 2, FALSE);
170         gtk_widget_show(table);
171         gtk_container_add (GTK_CONTAINER (vbox1), table);
172         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
173         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
174
175         label_outcharset = gtk_label_new (_("Outgoing encoding"));
176         gtk_widget_show (label_outcharset);
177         gtk_table_attach(GTK_TABLE(table), label_outcharset, 0, 1, 1, 2,
178                         (GtkAttachOptions) (GTK_FILL),
179                         (GtkAttachOptions) (0), 0, 0);
180         gtk_label_set_justify(GTK_LABEL(label_outcharset), GTK_JUSTIFY_RIGHT);
181         gtk_misc_set_alignment(GTK_MISC(label_outcharset), 1, 0.5);
182
183         optmenu = gtk_list_store_new(2,
184                         G_TYPE_STRING,          /* Menu label */
185                         G_TYPE_STRING);         /* Actual charset data string */
186
187         combobox_charset = gtk_combo_box_new_with_model(
188                         GTK_TREE_MODEL(optmenu));
189         rend = gtk_cell_renderer_text_new();
190         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox_charset), rend, TRUE);
191         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combobox_charset), rend,
192                         "text", COMBOBOX_TEXT,
193                         NULL);
194
195         gtk_combo_box_set_row_separator_func(GTK_COMBO_BOX(combobox_charset),
196                         (GtkTreeViewRowSeparatorFunc)_combobox_separator_func, NULL, NULL);
197
198         gtk_widget_show (combobox_charset);
199         CLAWS_SET_TIP(combobox_charset,
200                              _("If 'Automatic' is selected, the optimal encoding"
201                                " for the current locale will be used"));
202         gtk_table_attach(GTK_TABLE(table), combobox_charset, 1, 2, 1, 2,
203                         (GtkAttachOptions) (GTK_FILL),
204                         (GtkAttachOptions) (0), 0, 0);
205
206 #define SET_MENUITEM(str, data) \
207 { \
208         gtk_list_store_append(optmenu, &iter); \
209         gtk_list_store_set(optmenu, &iter, \
210                         COMBOBOX_TEXT, str, \
211                         COMBOBOX_DATA, data, \
212                         -1); \
213 }
214
215         SET_MENUITEM(_("Automatic (Recommended)"),       CS_AUTO);
216         SET_MENUITEM(NULL, NULL);
217         SET_MENUITEM(_("7bit ASCII (US-ASCII)"),         CS_US_ASCII);
218         SET_MENUITEM(_("Unicode (UTF-8)"),               CS_UTF_8);
219         SET_MENUITEM(NULL, NULL);
220         SET_MENUITEM(_("Western European (ISO-8859-1)"),  CS_ISO_8859_1);
221         SET_MENUITEM(_("Western European (ISO-8859-15)"), CS_ISO_8859_15);
222         SET_MENUITEM(NULL, NULL);
223         SET_MENUITEM(_("Central European (ISO-8859-2)"), CS_ISO_8859_2);
224         SET_MENUITEM(NULL, NULL);
225         SET_MENUITEM(_("Baltic (ISO-8859-13)"),          CS_ISO_8859_13);
226         SET_MENUITEM(_("Baltic (ISO-8859-4)"),           CS_ISO_8859_4);
227         SET_MENUITEM(NULL, NULL);
228         SET_MENUITEM(_("Greek (ISO-8859-7)"),            CS_ISO_8859_7);
229         SET_MENUITEM(NULL, NULL);
230         SET_MENUITEM(_("Hebrew (ISO-8859-8)"),           CS_ISO_8859_8);
231         SET_MENUITEM(_("Hebrew (Windows-1255)"),         CS_WINDOWS_1255);
232         SET_MENUITEM(NULL, NULL);
233         SET_MENUITEM(_("Arabic (ISO-8859-6)"),           CS_ISO_8859_6);
234         SET_MENUITEM(_("Arabic (Windows-1256)"),         CS_WINDOWS_1256);
235         SET_MENUITEM(NULL, NULL);
236         SET_MENUITEM(_("Turkish (ISO-8859-9)"),          CS_ISO_8859_9);
237         SET_MENUITEM(NULL, NULL);
238         SET_MENUITEM(_("Cyrillic (ISO-8859-5)"),         CS_ISO_8859_5);
239         SET_MENUITEM(_("Cyrillic (KOI8-R)"),             CS_KOI8_R);
240         SET_MENUITEM(_("Cyrillic (KOI8-U)"),             CS_KOI8_U);
241         SET_MENUITEM(_("Cyrillic (Windows-1251)"),       CS_WINDOWS_1251);
242         SET_MENUITEM(NULL, NULL);
243         SET_MENUITEM(_("Japanese (ISO-2022-JP)"),        CS_ISO_2022_JP);
244 #if 0
245         SET_MENUITEM(_("Japanese (EUC-JP)"),             CS_EUC_JP);
246         SET_MENUITEM(_("Japanese (Shift_JIS)"),          CS_SHIFT_JIS);
247 #endif /* 0 */
248         SET_MENUITEM(NULL, NULL);
249         SET_MENUITEM(_("Simplified Chinese (GB18030)"),  CS_GB18030);
250         SET_MENUITEM(_("Simplified Chinese (GB2312)"),   CS_GB2312);
251         SET_MENUITEM(_("Simplified Chinese (GBK)"),      CS_GBK);
252         SET_MENUITEM(_("Traditional Chinese (Big5)"),    CS_BIG5);
253 #if 0
254         SET_MENUITEM(_("Traditional Chinese (EUC-TW)"),  CS_EUC_TW);
255         SET_MENUITEM(_("Chinese (ISO-2022-CN)"),         CS_ISO_2022_CN);
256 #endif /* 0 */
257         SET_MENUITEM(NULL, NULL);
258         SET_MENUITEM(_("Korean (EUC-KR)"),               CS_EUC_KR);
259         SET_MENUITEM(NULL, NULL);
260         SET_MENUITEM(_("Thai (TIS-620)"),                CS_TIS_620);
261         SET_MENUITEM(_("Thai (Windows-874)"),            CS_WINDOWS_874);
262
263 #undef SET_MENUITEM
264
265         label_encoding = gtk_label_new (_("Transfer encoding"));
266         gtk_widget_show (label_encoding);
267         gtk_table_attach(GTK_TABLE(table), label_encoding, 0, 1, 2, 3,
268                         (GtkAttachOptions) (GTK_FILL),
269                         (GtkAttachOptions) (0), 0, 0);
270         gtk_label_set_justify(GTK_LABEL(label_encoding), GTK_JUSTIFY_RIGHT);
271         gtk_misc_set_alignment(GTK_MISC(label_encoding), 1, 0.5);
272
273         combobox_encoding = gtkut_sc_combobox_create(NULL, FALSE);
274         gtk_widget_show (combobox_encoding);
275         CLAWS_SET_TIP(combobox_encoding,
276                              _("Specify Content-Transfer-Encoding used when"
277                                " message body contains non-ASCII characters"));
278         gtk_table_attach(GTK_TABLE(table), combobox_encoding, 1, 2, 2, 3,
279                         (GtkAttachOptions) (GTK_FILL),
280                         (GtkAttachOptions) (0), 0, 0);
281
282         optmenu = GTK_LIST_STORE(gtk_combo_box_get_model(
283                                 GTK_COMBO_BOX(combobox_encoding)));
284
285         COMBOBOX_ADD(optmenu, _("Automatic"),    CTE_AUTO);
286         COMBOBOX_ADD(optmenu, NULL, 0);
287         COMBOBOX_ADD(optmenu, "base64",          CTE_BASE64);
288         COMBOBOX_ADD(optmenu, "quoted-printable", CTE_QUOTED_PRINTABLE);
289         COMBOBOX_ADD(optmenu, "8bit",            CTE_8BIT);
290
291         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_savemsg),
292                 prefs_common.savemsg);
293         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_confirm_send_queued_messages),
294                 prefs_common.confirm_send_queued_messages);
295         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_never_send_retrcpt),
296                 prefs_common.never_send_retrcpt);
297         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_senddialog),
298                 !prefs_common.send_dialog_invisible);
299         prefs_common_charset_set_optmenu(combobox_charset, 
300                 prefs_common.outgoing_charset);
301         combobox_select_by_data(GTK_COMBO_BOX(combobox_encoding),
302                 prefs_common.encoding_method);
303         
304         prefs_send->window                      = GTK_WIDGET(window);
305         
306         prefs_send->checkbtn_savemsg = checkbtn_savemsg;
307         prefs_send->checkbtn_confirm_send_queued_messages = checkbtn_confirm_send_queued_messages;
308         prefs_send->checkbtn_never_send_retrcpt = checkbtn_never_send_retrcpt;
309         prefs_send->checkbtn_senddialog = checkbtn_senddialog;
310         prefs_send->combobox_charset = combobox_charset;
311         prefs_send->combobox_encoding_method = combobox_encoding;
312
313         prefs_send->page.widget = vbox1;
314 }
315
316 static void prefs_send_save(PrefsPage *_page)
317 {
318         SendPage *page = (SendPage *) _page;
319
320         prefs_common.savemsg = gtk_toggle_button_get_active(
321                 GTK_TOGGLE_BUTTON(page->checkbtn_savemsg));
322         prefs_common.confirm_send_queued_messages = gtk_toggle_button_get_active(
323                 GTK_TOGGLE_BUTTON(page->checkbtn_confirm_send_queued_messages));
324         prefs_common.never_send_retrcpt = gtk_toggle_button_get_active(
325                 GTK_TOGGLE_BUTTON(page->checkbtn_never_send_retrcpt));
326         prefs_common.send_dialog_invisible = !gtk_toggle_button_get_active(
327                 GTK_TOGGLE_BUTTON(page->checkbtn_senddialog));
328
329         g_free(prefs_common.outgoing_charset);
330         prefs_common.outgoing_charset = prefs_common_charset_set_data_from_optmenu(
331                 page->combobox_charset);
332         prefs_common.encoding_method =
333                 combobox_get_active_data(GTK_COMBO_BOX(page->combobox_encoding_method));
334 }
335
336 static void prefs_send_destroy_widget(PrefsPage *_page)
337 {
338 }
339
340 SendPage *prefs_send;
341
342 void prefs_send_init(void)
343 {
344         SendPage *page;
345         static gchar *path[3];
346
347         path[0] = _("Mail Handling");
348         path[1] = _("Sending");
349         path[2] = NULL;
350
351         page = g_new0(SendPage, 1);
352         page->page.path = path;
353         page->page.create_widget = prefs_send_create_widget;
354         page->page.destroy_widget = prefs_send_destroy_widget;
355         page->page.save_page = prefs_send_save;
356         page->page.weight = 195.0;
357         prefs_gtk_register_page((PrefsPage *) page);
358         prefs_send = page;
359 }
360
361 void prefs_send_done(void)
362 {
363         prefs_gtk_unregister_page((PrefsPage *) prefs_send);
364         g_free(prefs_send);
365 }