Fix memory leaks from results of gtk_tree_model_get().
[claws.git] / src / prefs_send.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2005-2016 Colin Leroy & 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 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #include "claws-features.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 *checkbtn_warn_empty_subj;
55         GtkWidget *checkbtn_warn_multiple_recipients;
56         GtkWidget *spinbtn_warn_multiple_recipients;
57         GtkWidget *combobox_charset;
58         GtkWidget *combobox_encoding_method;
59 } SendPage;
60
61 static gchar * prefs_common_charset_set_data_from_optmenu(GtkWidget *widget)
62 {
63         GtkTreeModel *model;
64         GtkTreeIter iter;
65         gchar *data = NULL;
66
67         cm_return_val_if_fail(widget != NULL, NULL);
68
69         cm_return_val_if_fail(gtk_combo_box_get_active_iter(
70                                 GTK_COMBO_BOX(widget), &iter), NULL);
71
72         model = gtk_combo_box_get_model(GTK_COMBO_BOX(widget));
73
74         gtk_tree_model_get(model, &iter, COMBOBOX_DATA, &data, -1);
75
76         return data;
77 }
78
79 typedef struct _combobox_sel_by_data_ctx {
80         GtkComboBox *combobox;
81         gchar *data;
82 } ComboboxSelCtx;
83
84 static gboolean _select_by_data_func(GtkTreeModel *model, GtkTreePath *path,
85                 GtkTreeIter *iter, ComboboxSelCtx *ctx)
86 {
87         GtkComboBox *combobox = ctx->combobox;
88         gchar *data = ctx->data;
89         gchar *curdata; 
90
91         gtk_tree_model_get(GTK_TREE_MODEL(model), iter, COMBOBOX_DATA, &curdata, -1);
92         if ( data != NULL && curdata != NULL && !strcmp(data, curdata) ) {
93                 gtk_combo_box_set_active_iter(combobox, iter);
94                 g_free(curdata);
95                 return TRUE;
96         }
97
98         g_free(curdata);
99         return FALSE;
100 }
101
102 static void prefs_common_charset_set_optmenu(GtkWidget *widget, gchar *data)
103 {
104         GtkComboBox *combobox = GTK_COMBO_BOX(widget);
105         GtkTreeModel *model;
106         ComboboxSelCtx *ctx = NULL;
107         cm_return_if_fail(combobox != NULL);
108
109         model = gtk_combo_box_get_model(combobox);
110
111         ctx = g_new(ComboboxSelCtx,
112                         sizeof(ComboboxSelCtx));
113         ctx->combobox = combobox;
114         ctx->data = data;
115
116         gtk_tree_model_foreach(model, (GtkTreeModelForeachFunc)_select_by_data_func, ctx);
117         g_free(ctx);
118 }
119
120 static gboolean _combobox_separator_func(GtkTreeModel *model,
121                 GtkTreeIter *iter, gpointer data)
122 {
123         gchar *txt = NULL;
124
125         cm_return_val_if_fail(model != NULL, FALSE);
126
127         gtk_tree_model_get(model, iter, COMBOBOX_TEXT, &txt, -1);
128
129         if( txt == NULL )
130                 return TRUE;
131
132         g_free(txt);
133         return FALSE;
134 }
135
136 static void checkbtn_warn_multiple_recipients_toggled(GtkToggleButton *button,
137                 gpointer user_data)
138 {
139         gboolean active = gtk_toggle_button_get_active(button);
140         GtkWidget *spin = GTK_WIDGET(user_data);
141
142         gtk_widget_set_sensitive(spin, active);
143 }
144
145 static void prefs_send_create_widget(PrefsPage *_page, GtkWindow *window, 
146                                   gpointer data)
147 {
148         SendPage *prefs_send = (SendPage *) _page;
149         GtkWidget *frame;
150         GtkWidget *vbox1, *vbox2, *hbox1;
151         GtkWidget *checkbtn_savemsg;
152         GtkWidget *label_outcharset;
153         GtkWidget *combobox_charset;
154         GtkListStore *optmenu;
155         GtkTreeIter iter;
156         GtkCellRenderer *rend;
157         GtkWidget *combobox_encoding;
158         GtkWidget *label_encoding;
159         GtkWidget *checkbtn_senddialog;
160         GtkWidget *checkbtn_confirm_send_queued_messages;
161         GtkWidget *checkbtn_never_send_retrcpt;
162         GtkWidget *checkbtn_warn_empty_subj;
163         GtkWidget *checkbtn_warn_multiple_recipients;
164         GtkWidget *spinbtn_warn_multiple_recipients;
165         GtkWidget *table;
166
167         vbox1 = gtk_vbox_new (FALSE, VSPACING);
168         gtk_widget_show (vbox1);
169         gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
170
171         /* messages frame */
172         vbox2 = gtk_vbox_new (FALSE, 0);
173         gtk_widget_show (vbox2);
174
175         PACK_CHECK_BUTTON(vbox2, checkbtn_savemsg,
176                         _("Save sent messages"));
177
178         PACK_CHECK_BUTTON(vbox2, checkbtn_never_send_retrcpt,
179                         _("Never send Return Receipts"));
180
181         /* encoding table */
182         table = gtk_table_new(2, 2, FALSE);
183         gtk_widget_show(table);
184         gtk_container_add (GTK_CONTAINER (vbox2), table);
185         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
186         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
187
188         PACK_FRAME (vbox1, frame, _("Messages"))
189         gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
190         gtk_container_add(GTK_CONTAINER(frame), vbox2);
191
192         /* interface frame */
193         vbox2 = gtk_vbox_new (FALSE, 0);
194         gtk_widget_show (vbox2);
195
196         PACK_CHECK_BUTTON(vbox2, checkbtn_confirm_send_queued_messages,
197                         _("Confirm before sending queued messages"));
198
199         PACK_CHECK_BUTTON(vbox2, checkbtn_senddialog,
200                         _("Show send dialog"));
201
202         PACK_CHECK_BUTTON(vbox2, checkbtn_warn_empty_subj,
203                         _("Warn when Subject is empty"));
204
205         hbox1 = gtk_hbox_new (FALSE, 0);
206         gtk_widget_show (hbox1);
207
208         PACK_CHECK_BUTTON(hbox1, checkbtn_warn_multiple_recipients,
209                         _("Warn when sending to more recipients than:"));
210
211         spinbtn_warn_multiple_recipients = gtk_spin_button_new_with_range(2, 999, 1);
212         gtk_widget_show(spinbtn_warn_multiple_recipients);
213         gtk_box_pack_start(GTK_BOX(hbox1), spinbtn_warn_multiple_recipients, FALSE, FALSE, 0);
214         g_signal_connect(G_OBJECT(checkbtn_warn_multiple_recipients), "toggled",
215                         G_CALLBACK(checkbtn_warn_multiple_recipients_toggled),
216                         spinbtn_warn_multiple_recipients);
217
218         gtk_box_pack_start(GTK_BOX(vbox2), hbox1, FALSE, FALSE, 0);
219
220         PACK_FRAME (vbox1, frame, _("Interface"))
221         gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
222         gtk_container_add(GTK_CONTAINER(frame), vbox2);
223
224         /* populate table within encoding sub-frame */
225         label_outcharset = gtk_label_new (_("Outgoing encoding"));
226         gtk_widget_show (label_outcharset);
227         gtk_table_attach(GTK_TABLE(table), label_outcharset, 0, 1, 1, 2,
228                         (GtkAttachOptions) (GTK_FILL),
229                         (GtkAttachOptions) (0), 0, 0);
230         gtk_label_set_justify(GTK_LABEL(label_outcharset), GTK_JUSTIFY_RIGHT);
231         gtk_misc_set_alignment(GTK_MISC(label_outcharset), 1, 0.5);
232
233         optmenu = gtk_list_store_new(2,
234                         G_TYPE_STRING,          /* Menu label */
235                         G_TYPE_STRING);         /* Actual charset data string */
236
237         combobox_charset = gtk_combo_box_new_with_model(
238                         GTK_TREE_MODEL(optmenu));
239         rend = gtk_cell_renderer_text_new();
240         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox_charset), rend, TRUE);
241         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combobox_charset), rend,
242                         "text", COMBOBOX_TEXT,
243                         NULL);
244
245         gtk_combo_box_set_row_separator_func(GTK_COMBO_BOX(combobox_charset),
246                         (GtkTreeViewRowSeparatorFunc)_combobox_separator_func, NULL, NULL);
247
248         gtk_widget_show (combobox_charset);
249         CLAWS_SET_TIP(combobox_charset,
250                              _("If 'Automatic' is selected, the optimal encoding"
251                                " for the current locale will be used"));
252         gtk_table_attach(GTK_TABLE(table), combobox_charset, 1, 2, 1, 2,
253                         (GtkAttachOptions) (GTK_FILL),
254                         (GtkAttachOptions) (0), 0, 0);
255
256 #define SET_MENUITEM(str, data) \
257 { \
258         gtk_list_store_append(optmenu, &iter); \
259         gtk_list_store_set(optmenu, &iter, \
260                         COMBOBOX_TEXT, str, \
261                         COMBOBOX_DATA, data, \
262                         -1); \
263 }
264
265         SET_MENUITEM(_("Automatic"),                     CS_AUTO);
266         SET_MENUITEM(NULL, NULL);
267         SET_MENUITEM(_("7bit ASCII (US-ASCII)"),         CS_US_ASCII);
268         SET_MENUITEM(_("Unicode (UTF-8)"),               CS_UTF_8);
269         SET_MENUITEM(NULL, NULL);
270         SET_MENUITEM(_("Western European (ISO-8859-1)"),  CS_ISO_8859_1);
271         SET_MENUITEM(_("Western European (ISO-8859-15)"), CS_ISO_8859_15);
272         SET_MENUITEM(NULL, NULL);
273         SET_MENUITEM(_("Central European (ISO-8859-2)"), CS_ISO_8859_2);
274         SET_MENUITEM(NULL, NULL);
275         SET_MENUITEM(_("Baltic (ISO-8859-13)"),          CS_ISO_8859_13);
276         SET_MENUITEM(_("Baltic (ISO-8859-4)"),           CS_ISO_8859_4);
277         SET_MENUITEM(NULL, NULL);
278         SET_MENUITEM(_("Greek (ISO-8859-7)"),            CS_ISO_8859_7);
279         SET_MENUITEM(NULL, NULL);
280         SET_MENUITEM(_("Hebrew (ISO-8859-8)"),           CS_ISO_8859_8);
281         SET_MENUITEM(_("Hebrew (Windows-1255)"),         CS_WINDOWS_1255);
282         SET_MENUITEM(NULL, NULL);
283         SET_MENUITEM(_("Arabic (ISO-8859-6)"),           CS_ISO_8859_6);
284         SET_MENUITEM(_("Arabic (Windows-1256)"),         CS_WINDOWS_1256);
285         SET_MENUITEM(NULL, NULL);
286         SET_MENUITEM(_("Turkish (ISO-8859-9)"),          CS_ISO_8859_9);
287         SET_MENUITEM(NULL, NULL);
288         SET_MENUITEM(_("Cyrillic (ISO-8859-5)"),         CS_ISO_8859_5);
289         SET_MENUITEM(_("Cyrillic (KOI8-R)"),             CS_KOI8_R);
290         SET_MENUITEM(_("Cyrillic (X-MAC-CYRILLIC)"),             CS_MACCYR);
291         SET_MENUITEM(_("Cyrillic (KOI8-U)"),             CS_KOI8_U);
292         SET_MENUITEM(_("Cyrillic (Windows-1251)"),       CS_WINDOWS_1251);
293         SET_MENUITEM(NULL, NULL);
294         SET_MENUITEM(_("Japanese (ISO-2022-JP)"),        CS_ISO_2022_JP);
295 #if 0
296         SET_MENUITEM(_("Japanese (EUC-JP)"),             CS_EUC_JP);
297         SET_MENUITEM(_("Japanese (Shift_JIS)"),          CS_SHIFT_JIS);
298 #endif /* 0 */
299         SET_MENUITEM(NULL, NULL);
300         SET_MENUITEM(_("Simplified Chinese (GB18030)"),  CS_GB18030);
301         SET_MENUITEM(_("Simplified Chinese (GB2312)"),   CS_GB2312);
302         SET_MENUITEM(_("Simplified Chinese (GBK)"),      CS_GBK);
303         SET_MENUITEM(_("Traditional Chinese (Big5)"),    CS_BIG5);
304 #if 0
305         SET_MENUITEM(_("Traditional Chinese (EUC-TW)"),  CS_EUC_TW);
306         SET_MENUITEM(_("Chinese (ISO-2022-CN)"),         CS_ISO_2022_CN);
307 #endif /* 0 */
308         SET_MENUITEM(NULL, NULL);
309         SET_MENUITEM(_("Korean (EUC-KR)"),               CS_EUC_KR);
310         SET_MENUITEM(NULL, NULL);
311         SET_MENUITEM(_("Thai (TIS-620)"),                CS_TIS_620);
312         SET_MENUITEM(_("Thai (Windows-874)"),            CS_WINDOWS_874);
313
314 #undef SET_MENUITEM
315
316         label_encoding = gtk_label_new (_("Transfer encoding"));
317         gtk_widget_show (label_encoding);
318         gtk_table_attach(GTK_TABLE(table), label_encoding, 0, 1, 2, 3,
319                         (GtkAttachOptions) (GTK_FILL),
320                         (GtkAttachOptions) (0), 0, 0);
321         gtk_label_set_justify(GTK_LABEL(label_encoding), GTK_JUSTIFY_RIGHT);
322         gtk_misc_set_alignment(GTK_MISC(label_encoding), 1, 0.5);
323
324         combobox_encoding = gtkut_sc_combobox_create(NULL, FALSE);
325         gtk_widget_show (combobox_encoding);
326         CLAWS_SET_TIP(combobox_encoding,
327                              _("Specify Content-Transfer-Encoding used when"
328                                " message body contains non-ASCII characters"));
329         gtk_table_attach(GTK_TABLE(table), combobox_encoding, 1, 2, 2, 3,
330                         (GtkAttachOptions) (GTK_FILL),
331                         (GtkAttachOptions) (0), 0, 0);
332
333         optmenu = GTK_LIST_STORE(gtk_combo_box_get_model(
334                                 GTK_COMBO_BOX(combobox_encoding)));
335
336         COMBOBOX_ADD(optmenu, _("Automatic"),    CTE_AUTO);
337         COMBOBOX_ADD(optmenu, NULL, 0);
338         COMBOBOX_ADD(optmenu, "base64",          CTE_BASE64);
339         COMBOBOX_ADD(optmenu, "quoted-printable", CTE_QUOTED_PRINTABLE);
340         COMBOBOX_ADD(optmenu, "8bit",            CTE_8BIT);
341
342         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_savemsg),
343                 prefs_common.savemsg);
344         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_confirm_send_queued_messages),
345                 prefs_common.confirm_send_queued_messages);
346         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_never_send_retrcpt),
347                 prefs_common.never_send_retrcpt);
348         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_senddialog),
349                 !prefs_common.send_dialog_invisible);
350         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_warn_empty_subj),
351                 prefs_common.warn_empty_subj);
352         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_warn_multiple_recipients), prefs_common.warn_sending_many_recipients_num > 0);
353
354         if (prefs_common.warn_sending_many_recipients_num > 0)
355                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinbtn_warn_multiple_recipients),
356                                 prefs_common.warn_sending_many_recipients_num);
357         else
358                 gtk_widget_set_sensitive(spinbtn_warn_multiple_recipients, FALSE);
359
360         prefs_common_charset_set_optmenu(combobox_charset, 
361                 prefs_common.outgoing_charset);
362         combobox_select_by_data(GTK_COMBO_BOX(combobox_encoding),
363                 prefs_common.encoding_method);
364
365         prefs_send->window = GTK_WIDGET(window);
366
367         prefs_send->checkbtn_savemsg = checkbtn_savemsg;
368         prefs_send->checkbtn_confirm_send_queued_messages = checkbtn_confirm_send_queued_messages;
369         prefs_send->checkbtn_never_send_retrcpt = checkbtn_never_send_retrcpt;
370         prefs_send->checkbtn_senddialog = checkbtn_senddialog;
371         prefs_send->checkbtn_warn_empty_subj = checkbtn_warn_empty_subj;
372         prefs_send->checkbtn_warn_multiple_recipients = checkbtn_warn_multiple_recipients;
373         prefs_send->spinbtn_warn_multiple_recipients = spinbtn_warn_multiple_recipients;
374         prefs_send->combobox_charset = combobox_charset;
375         prefs_send->combobox_encoding_method = combobox_encoding;
376
377         prefs_send->page.widget = vbox1;
378 }
379
380 static void prefs_send_save(PrefsPage *_page)
381 {
382         SendPage *page = (SendPage *) _page;
383
384         prefs_common.savemsg = gtk_toggle_button_get_active(
385                 GTK_TOGGLE_BUTTON(page->checkbtn_savemsg));
386         prefs_common.confirm_send_queued_messages = gtk_toggle_button_get_active(
387                 GTK_TOGGLE_BUTTON(page->checkbtn_confirm_send_queued_messages));
388         prefs_common.never_send_retrcpt = gtk_toggle_button_get_active(
389                 GTK_TOGGLE_BUTTON(page->checkbtn_never_send_retrcpt));
390         prefs_common.send_dialog_invisible = !gtk_toggle_button_get_active(
391                 GTK_TOGGLE_BUTTON(page->checkbtn_senddialog));
392         prefs_common.warn_empty_subj = gtk_toggle_button_get_active(
393                 GTK_TOGGLE_BUTTON(page->checkbtn_warn_empty_subj));
394
395         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_warn_multiple_recipients)))
396                 prefs_common.warn_sending_many_recipients_num =
397                         gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->spinbtn_warn_multiple_recipients));
398         else
399                 prefs_common.warn_sending_many_recipients_num = 0;
400
401         g_free(prefs_common.outgoing_charset);
402         prefs_common.outgoing_charset = prefs_common_charset_set_data_from_optmenu(
403                 page->combobox_charset);
404         prefs_common.encoding_method =
405                 combobox_get_active_data(GTK_COMBO_BOX(page->combobox_encoding_method));
406 }
407
408 static void prefs_send_destroy_widget(PrefsPage *_page)
409 {
410 }
411
412 SendPage *prefs_send;
413
414 void prefs_send_init(void)
415 {
416         SendPage *page;
417         static gchar *path[3];
418
419         path[0] = _("Mail Handling");
420         path[1] = _("Sending");
421         path[2] = NULL;
422
423         page = g_new0(SendPage, 1);
424         page->page.path = path;
425         page->page.create_widget = prefs_send_create_widget;
426         page->page.destroy_widget = prefs_send_destroy_widget;
427         page->page.save_page = prefs_send_save;
428         page->page.weight = 195.0;
429         prefs_gtk_register_page((PrefsPage *) page);
430         prefs_send = page;
431 }
432
433 void prefs_send_done(void)
434 {
435         prefs_gtk_unregister_page((PrefsPage *) prefs_send);
436         g_free(prefs_send);
437 }