2 * Claws Mail templates subsystem
3 * Copyright (C) 2001 Alexander Barinov
4 * Copyright (C) 2001-2022 The Claws Mail team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <glib/gi18n.h>
26 #include <gdk/gdkkeysyms.h>
33 #include "prefs_gtk.h"
37 #include "alertpanel.h"
38 #include "manage_window.h"
40 #include "addr_compl.h"
41 #include "quote_fmt.h"
42 #include "prefs_common.h"
50 TEMPL_AUTO_DATA
, /*!< auto pointer */
54 static struct Templates
{
58 GtkWidget
*entry_name
;
59 GtkWidget
*entry_subject
;
60 GtkWidget
*entry_from
;
64 GtkWidget
*entry_replyto
;
65 GtkWidget
*text_value
;
68 static int modified
= FALSE
;
69 static int modified_list
= FALSE
;
78 {N_("Name"), &templates
.entry_name
, FALSE
,
79 N_("This name is used as the Menu item")},
80 {"From", &templates
.entry_from
, TRUE
,
81 N_("Override composing account's From header. This doesn't change the composing account.")},
82 {"To", &templates
.entry_to
, TRUE
, NULL
},
83 {"Cc", &templates
.entry_cc
, TRUE
, NULL
},
84 {"Bcc", &templates
.entry_bcc
, TRUE
, NULL
},
85 {"Reply-To", &templates
.entry_replyto
, TRUE
, NULL
},
86 {"Subject", &templates
.entry_subject
, FALSE
, NULL
},
87 {NULL
, NULL
, FALSE
, NULL
}
91 /* widget creating functions */
92 static void prefs_template_window_create (void);
93 static void prefs_template_window_setup (void);
95 static GSList
*prefs_template_get_list (void);
98 static gint
prefs_template_deleted_cb (GtkWidget
*widget
,
101 static gboolean
prefs_template_key_pressed_cb (GtkWidget
*widget
,
104 static gboolean
prefs_template_search_func_cb (GtkTreeModel
*model
, gint column
,
105 const gchar
*key
, GtkTreeIter
*iter
,
106 gpointer search_data
);
108 static void prefs_template_cancel_cb (gpointer action
, gpointer data
);
109 static void prefs_template_ok_cb (gpointer action
, gpointer data
);
110 static void prefs_template_register_cb (gpointer action
, gpointer data
);
111 static void prefs_template_substitute_cb (gpointer action
, gpointer data
);
112 static void prefs_template_delete_cb (gpointer action
, gpointer data
);
113 static void prefs_template_delete_all_cb (gpointer action
, gpointer data
);
114 static void prefs_template_clear_cb (gpointer action
, gpointer data
);
115 static void prefs_template_duplicate_cb (gpointer action
, gpointer data
);
116 static void prefs_template_top_cb (gpointer action
, gpointer data
);
117 static void prefs_template_up_cb (gpointer action
, gpointer data
);
118 static void prefs_template_down_cb (gpointer action
, gpointer data
);
119 static void prefs_template_bottom_cb (gpointer action
, gpointer data
);
121 static GtkListStore
* prefs_template_create_data_store (void);
122 static void prefs_template_list_view_insert_template (GtkWidget
*list_view
,
124 const gchar
*template,
126 static GtkWidget
*prefs_template_list_view_create (void);
127 static void prefs_template_create_list_view_columns (GtkWidget
*list_view
);
128 static void prefs_template_select_row(GtkTreeView
*list_view
, GtkTreePath
*path
);
130 /* Called from mainwindow.c */
131 void prefs_template_open(void)
135 prefs_template_window_create();
137 prefs_template_window_setup();
138 gtk_widget_show(templates
.window
);
139 gtk_window_set_modal(GTK_WINDOW(templates
.window
), TRUE
);
143 *\brief Save Gtk object size to prefs dataset
145 static void prefs_template_size_allocate_cb(GtkWidget
*widget
,
146 GtkAllocation
*allocation
)
148 cm_return_if_fail(allocation
!= NULL
);
150 gtk_window_get_size(GTK_WINDOW(widget
),
151 &prefs_common
.templateswin_width
, &prefs_common
.templateswin_height
);
154 static void prefs_template_window_create(void)
156 /* window structure ;) */
159 GtkWidget
*scrolled_window
;
162 GtkWidget
*table
; /* including : entry_[name|from|to|cc|bcc|replyto|subject] */
164 GtkWidget
*text_value
;
170 GtkWidget
*subst_btn
;
172 GtkWidget
*clear_btn
;
176 GtkWidget
*list_view
;
182 GtkWidget
*bottom_btn
;
183 GtkWidget
*confirm_area
;
185 GtkWidget
*cancel_btn
;
187 static GdkGeometry geometry
;
190 debug_print("Creating templates configuration window...\n");
193 window
= gtkut_window_new(GTK_WINDOW_TOPLEVEL
, "prefs_template");
194 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
195 gtk_window_set_resizable(GTK_WINDOW(window
), TRUE
);
196 gtk_window_set_type_hint(GTK_WINDOW(window
), GDK_WINDOW_TYPE_HINT_DIALOG
);
198 vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 8);
199 gtk_widget_show(vbox
);
200 gtk_container_add(GTK_CONTAINER(window
), vbox
);
201 gtk_container_set_border_width(GTK_CONTAINER(vbox
), 4);
203 scrolled_window
= gtk_scrolled_window_new (NULL
, NULL
);
204 gtk_widget_show(scrolled_window
);
205 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window
),
206 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
207 gtk_box_pack_start(GTK_BOX(vbox
), scrolled_window
, TRUE
, TRUE
, 0);
209 /* vpaned to separate template settings from templates list */
210 vpaned
= gtk_paned_new(GTK_ORIENTATION_VERTICAL
);
211 gtk_widget_show(vpaned
);
212 gtk_container_add(GTK_CONTAINER(scrolled_window
), vpaned
);
213 gtk_viewport_set_shadow_type (GTK_VIEWPORT(
214 gtk_bin_get_child(GTK_BIN(scrolled_window
))), GTK_SHADOW_NONE
);
216 /* vbox to handle template name and content */
217 vbox1
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 6);
218 gtk_widget_show(vbox1
);
219 gtk_container_set_border_width(GTK_CONTAINER(vbox1
), 8);
220 gtk_paned_pack1(GTK_PANED(vpaned
), vbox1
, FALSE
, FALSE
);
222 table
= gtk_grid_new();
223 gtk_widget_show(table
);
224 gtk_grid_set_row_spacing(GTK_GRID(table
), VSPACING_NARROW_2
);
225 gtk_grid_set_column_spacing(GTK_GRID(table
), 4);
227 gtk_box_pack_start (GTK_BOX (vbox1
), table
, FALSE
, FALSE
, 0);
229 for (i
=0; widgets_table
[i
].label
; i
++) {
233 label
= gtk_label_new( (i
!= 0) ?
234 prefs_common_translated_header_name(widgets_table
[i
].label
) :
235 _(widgets_table
[i
].label
));
236 gtk_widget_show(label
);
237 gtk_label_set_xalign(GTK_LABEL(label
), 1.0);
238 gtk_grid_attach(GTK_GRID(table
), label
, 0, i
, 1, 1);
240 *(widgets_table
[i
].entry
) = gtk_entry_new();
241 gtk_widget_show(*(widgets_table
[i
].entry
));
242 gtk_grid_attach(GTK_GRID(table
), *(widgets_table
[i
].entry
), 1, i
, 1, 1);
243 gtk_widget_set_hexpand(*(widgets_table
[i
].entry
), TRUE
);
244 gtk_widget_set_halign(*(widgets_table
[i
].entry
), GTK_ALIGN_FILL
);
245 CLAWS_SET_TIP(*(widgets_table
[i
].entry
),
246 _(widgets_table
[i
].tooltips
));
249 /* template content */
250 scroll2
= gtk_scrolled_window_new(NULL
, NULL
);
251 gtk_widget_show(scroll2
);
252 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll2
),
253 GTK_POLICY_AUTOMATIC
,
254 GTK_POLICY_AUTOMATIC
);
255 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll2
),
257 gtk_box_pack_start(GTK_BOX(vbox1
), scroll2
, TRUE
, TRUE
, 0);
259 text_value
= gtk_text_view_new();
260 if (prefs_common
.textfont
) {
261 PangoFontDescription
*font_desc
;
263 font_desc
= pango_font_description_from_string
264 (prefs_common
.textfont
);
266 gtk_widget_override_font(text_value
, font_desc
);
267 pango_font_description_free(font_desc
);
270 gtk_widget_show(text_value
);
272 gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(scroll2
), 120);
274 gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(scroll2
), 60);
276 gtk_container_add(GTK_CONTAINER(scroll2
), text_value
);
277 gtk_text_view_set_editable(GTK_TEXT_VIEW(text_value
), TRUE
);
278 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_value
), GTK_WRAP_WORD
);
280 /* vbox for buttons and templates list */
281 vbox2
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 6);
282 gtk_widget_show(vbox2
);
283 gtk_container_set_border_width(GTK_CONTAINER(vbox2
), 8);
284 gtk_paned_pack2(GTK_PANED(vpaned
), vbox2
, TRUE
, FALSE
);
286 /* register | substitute | delete */
287 hbox2
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 4);
288 gtk_widget_show(hbox2
);
289 gtk_box_pack_start(GTK_BOX(vbox2
), hbox2
, FALSE
, FALSE
, 0);
291 arrow1
= gtk_image_new_from_icon_name("pan-down-symbolic", GTK_ICON_SIZE_MENU
);
292 gtk_widget_show(arrow1
);
293 gtk_box_pack_start(GTK_BOX(hbox2
), arrow1
, FALSE
, FALSE
, 0);
295 hbox3
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 4);
296 gtk_widget_show(hbox3
);
297 gtk_box_pack_start(GTK_BOX(hbox2
), hbox3
, FALSE
, FALSE
, 0);
299 reg_btn
= gtkut_stock_button("list-add", _("_Add"));
300 gtk_widget_show(reg_btn
);
301 gtk_box_pack_start(GTK_BOX(hbox3
), reg_btn
, FALSE
, TRUE
, 0);
302 g_signal_connect(G_OBJECT (reg_btn
), "clicked",
303 G_CALLBACK (prefs_template_register_cb
), NULL
);
304 CLAWS_SET_TIP(reg_btn
,
305 _("Append the new template above to the list"));
307 subst_btn
= gtkut_stock_button("edit-redo", _("_Replace"));
308 gtk_widget_show(subst_btn
);
309 gtk_box_pack_start(GTK_BOX(hbox3
), subst_btn
, FALSE
, TRUE
, 0);
310 g_signal_connect(G_OBJECT(subst_btn
), "clicked",
311 G_CALLBACK(prefs_template_substitute_cb
),
313 CLAWS_SET_TIP(subst_btn
,
314 _("Replace the selected template in list with the template above"));
316 del_btn
= gtkut_stock_button("edit-delete", _("D_elete"));
317 gtk_widget_show(del_btn
);
318 gtk_box_pack_start(GTK_BOX(hbox3
), del_btn
, FALSE
, TRUE
, 0);
319 g_signal_connect(G_OBJECT(del_btn
), "clicked",
320 G_CALLBACK(prefs_template_delete_cb
), NULL
);
321 CLAWS_SET_TIP(del_btn
,
322 _("Delete the selected template from the list"));
324 clear_btn
= gtkut_stock_button("edit-clear", _("C_lear"));
325 gtk_widget_show (clear_btn
);
326 gtk_box_pack_start (GTK_BOX (hbox3
), clear_btn
, FALSE
, TRUE
, 0);
327 g_signal_connect(G_OBJECT (clear_btn
), "clicked",
328 G_CALLBACK(prefs_template_clear_cb
), NULL
);
329 CLAWS_SET_TIP(clear_btn
,
330 _("Clear all the input fields in the dialog"));
332 desc_btn
= gtkut_stock_button("dialog-information", _("_Information"));
333 gtk_widget_show(desc_btn
);
334 gtk_box_pack_end(GTK_BOX(hbox2
), desc_btn
, FALSE
, FALSE
, 0);
335 g_signal_connect(G_OBJECT(desc_btn
), "clicked",
336 G_CALLBACK(quote_fmt_quote_description
), window
);
337 CLAWS_SET_TIP(desc_btn
,
338 _("Show information on configuring templates"));
341 hbox4
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 8);
342 gtk_widget_show(hbox4
);
343 gtk_box_pack_start(GTK_BOX(vbox2
), hbox4
, TRUE
, TRUE
, 0);
345 scroll1
= gtk_scrolled_window_new(NULL
, NULL
);
346 gtk_widget_show(scroll1
);
347 gtk_box_pack_start(GTK_BOX(hbox4
), scroll1
, TRUE
, TRUE
, 0);
348 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1
),
349 GTK_POLICY_AUTOMATIC
,
350 GTK_POLICY_AUTOMATIC
);
352 vbox3
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 8);
353 gtk_widget_show(vbox3
);
354 gtk_box_pack_start(GTK_BOX(hbox4
), vbox3
, FALSE
, FALSE
, 0);
356 top_btn
= gtkut_stock_button("go-top", _("_Top"));
357 gtk_widget_show(top_btn
);
358 gtk_box_pack_start(GTK_BOX(vbox3
), top_btn
, FALSE
, FALSE
, 0);
359 g_signal_connect(G_OBJECT(top_btn
), "clicked",
360 G_CALLBACK(prefs_template_top_cb
), NULL
);
361 CLAWS_SET_TIP(top_btn
,
362 _("Move the selected template to the top"));
364 PACK_SPACER(vbox3
, spc_vbox
, VSPACING_NARROW_2
);
366 up_btn
= gtkut_stock_button("go-up", _("_Up"));
367 gtk_widget_show(up_btn
);
368 gtk_box_pack_start (GTK_BOX(vbox3
), up_btn
, FALSE
, FALSE
, 0);
369 g_signal_connect(G_OBJECT(up_btn
), "clicked",
370 G_CALLBACK(prefs_template_up_cb
), NULL
);
371 CLAWS_SET_TIP(up_btn
,
372 _("Move the selected template up"));
374 down_btn
= gtkut_stock_button("go-down", _("_Down"));
375 gtk_widget_show (down_btn
);
376 gtk_box_pack_start(GTK_BOX (vbox3
), down_btn
, FALSE
, FALSE
, 0);
377 g_signal_connect(G_OBJECT (down_btn
), "clicked",
378 G_CALLBACK(prefs_template_down_cb
), NULL
);
379 CLAWS_SET_TIP(down_btn
,
380 _("Move the selected template down"));
382 PACK_SPACER(vbox3
, spc_vbox
, VSPACING_NARROW_2
);
384 bottom_btn
= gtkut_stock_button("go-bottom", _("_Bottom"));
385 gtk_widget_show(bottom_btn
);
386 gtk_box_pack_start(GTK_BOX(vbox3
), bottom_btn
, FALSE
, FALSE
, 0);
387 g_signal_connect(G_OBJECT(bottom_btn
), "clicked",
388 G_CALLBACK(prefs_template_bottom_cb
), NULL
);
389 CLAWS_SET_TIP(bottom_btn
,
390 _("Move the selected template to the bottom"));
392 list_view
= prefs_template_list_view_create();
393 gtk_widget_show(list_view
);
394 gtk_widget_set_size_request(scroll1
, -1, 140);
395 gtk_container_add(GTK_CONTAINER(scroll1
), list_view
);
397 /* help | cancel | ok */
398 gtkut_stock_button_set_create_with_help(&confirm_area
, &help_btn
,
399 &cancel_btn
, NULL
, _("_Cancel"),
400 &ok_btn
, NULL
, _("_OK"),
402 gtk_widget_show(confirm_area
);
403 gtk_box_pack_end(GTK_BOX(vbox
), confirm_area
, FALSE
, FALSE
, 0);
404 gtk_widget_grab_default(ok_btn
);
406 gtk_window_set_title(GTK_WINDOW(window
), _("Template configuration"));
408 g_signal_connect(G_OBJECT(window
), "delete_event",
409 G_CALLBACK(prefs_template_deleted_cb
), NULL
);
410 g_signal_connect(G_OBJECT(window
), "size_allocate",
411 G_CALLBACK(prefs_template_size_allocate_cb
), NULL
);
412 g_signal_connect(G_OBJECT(window
), "key_press_event",
413 G_CALLBACK(prefs_template_key_pressed_cb
), NULL
);
414 MANAGE_WINDOW_SIGNALS_CONNECT(window
);
415 g_signal_connect(G_OBJECT(ok_btn
), "clicked",
416 G_CALLBACK(prefs_template_ok_cb
), NULL
);
417 g_signal_connect(G_OBJECT(cancel_btn
), "clicked",
418 G_CALLBACK(prefs_template_cancel_cb
), NULL
);
419 g_signal_connect(G_OBJECT(help_btn
), "clicked",
420 G_CALLBACK(manual_open_with_anchor_cb
),
421 MANUAL_ANCHOR_TEMPLATES
);
423 if (!geometry
.min_height
) {
424 geometry
.min_width
= 500;
425 geometry
.min_height
= 540;
428 gtk_window_set_geometry_hints(GTK_WINDOW(window
), NULL
, &geometry
,
430 gtk_window_set_default_size(GTK_WINDOW(window
),
431 prefs_common
.templateswin_width
,
432 prefs_common
.templateswin_height
);
434 templates
.window
= window
;
435 templates
.ok_btn
= ok_btn
;
436 templates
.list_view
= list_view
;
437 templates
.text_value
= text_value
;
440 static void prefs_template_reset_dialog(void)
442 GtkTextBuffer
*buffer
;
444 gtk_entry_set_text(GTK_ENTRY(templates
.entry_name
), "");
445 gtk_entry_set_text(GTK_ENTRY(templates
.entry_from
), "");
446 gtk_entry_set_text(GTK_ENTRY(templates
.entry_to
), "");
447 gtk_entry_set_text(GTK_ENTRY(templates
.entry_cc
), "");
448 gtk_entry_set_text(GTK_ENTRY(templates
.entry_bcc
), "");
449 gtk_entry_set_text(GTK_ENTRY(templates
.entry_replyto
), "");
450 gtk_entry_set_text(GTK_ENTRY(templates
.entry_subject
), "");
452 buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates
.text_value
));
453 gtk_text_buffer_set_text(buffer
, "", -1);
456 static void prefs_template_clear_list(void)
460 store
= GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
461 (templates
.list_view
)));
462 gtk_list_store_clear(store
);
464 prefs_template_list_view_insert_template(templates
.list_view
,
469 static void prefs_template_window_setup(void)
476 manage_window_set_transient(GTK_WINDOW(templates
.window
));
477 gtk_widget_grab_focus(templates
.ok_btn
);
479 prefs_template_clear_list();
481 tmpl_list
= template_read_config();
483 address_completion_start(templates
.window
);
485 for (i
=0; widgets_table
[i
].label
; i
++) {
486 if (widgets_table
[i
].compl)
487 address_completion_register_entry(
488 GTK_ENTRY(*(widgets_table
[i
].entry
)), TRUE
);
491 for (cur
= tmpl_list
; cur
!= NULL
; cur
= cur
->next
) {
492 tmpl
= (Template
*)cur
->data
;
493 prefs_template_list_view_insert_template(templates
.list_view
,
498 prefs_template_reset_dialog();
500 g_slist_free(tmpl_list
);
503 static gint
prefs_template_deleted_cb(GtkWidget
*widget
, GdkEventAny
*event
,
506 prefs_template_cancel_cb(NULL
, NULL
);
510 static gboolean
prefs_template_key_pressed_cb(GtkWidget
*widget
,
511 GdkEventKey
*event
, gpointer data
)
513 if (event
&& event
->keyval
== GDK_KEY_Escape
)
514 prefs_template_cancel_cb(NULL
, NULL
);
516 GtkWidget
*focused
= gtkut_get_focused_child(
517 GTK_CONTAINER(widget
));
518 if (focused
&& GTK_IS_EDITABLE(focused
)) {
525 static gboolean
prefs_template_search_func_cb (GtkTreeModel
*model
, gint column
, const gchar
*key
,
526 GtkTreeIter
*iter
, gpointer search_data
)
533 gtk_tree_model_get (model
, iter
, column
, &store_string
, -1);
535 if (!store_string
|| !key
) return FALSE
;
537 key_len
= strlen (key
);
538 retval
= (strncmp (key
, store_string
, key_len
) != 0);
540 g_free(store_string
);
541 debug_print("selecting row\n");
542 path
= gtk_tree_model_get_path(model
, iter
);
543 prefs_template_select_row(GTK_TREE_VIEW(templates
.list_view
), path
);
544 gtk_tree_path_free(path
);
548 static void prefs_template_address_completion_end(void)
552 for (i
=0; widgets_table
[i
].label
; i
++) {
553 if (widgets_table
[i
].compl)
554 address_completion_unregister_entry(
555 GTK_ENTRY(*(widgets_table
[i
].entry
)));
557 address_completion_end(templates
.window
);
560 static void prefs_template_ok_cb(gpointer action
, gpointer data
)
565 if (modified
&& alertpanel(_("Entry not saved"),
566 _("The entry was not saved. Close anyway?"),
567 "window-close", _("_Close"), NULL
, _("_Continue editing"),
568 NULL
, NULL
, ALERTFOCUS_SECOND
) != G_ALERTDEFAULT
) {
572 prefs_template_address_completion_end();
575 modified_list
= FALSE
;
576 tmpl_list
= prefs_template_get_list();
577 template_set_config(tmpl_list
);
578 compose_reflect_prefs_all();
579 store
= GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
580 (templates
.list_view
)));
581 gtk_list_store_clear(store
);
582 gtk_widget_destroy(templates
.window
);
586 static void prefs_template_cancel_cb(gpointer action
, gpointer data
)
590 if (modified
&& alertpanel(_("Entry not saved"),
591 _("The entry was not saved. Close anyway?"),
592 "window-close", _("_Close"), NULL
, _("_Continue editing"),
593 NULL
, NULL
, ALERTFOCUS_SECOND
) != G_ALERTDEFAULT
) {
595 } else if (modified_list
&& alertpanel(_("Templates list not saved"),
596 _("The templates list has been modified. Close anyway?"),
597 "window-close", _("_Close"), NULL
, _("_Continue editing"),
598 NULL
, NULL
, ALERTFOCUS_SECOND
) != G_ALERTDEFAULT
) {
602 prefs_template_address_completion_end();
605 modified_list
= FALSE
;
606 store
= GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
607 (templates
.list_view
)));
608 gtk_list_store_clear(store
);
609 gtk_widget_destroy(templates
.window
);
614 *\brief Request list for storage. New list is owned
617 static GSList
*prefs_template_get_list(void)
619 GSList
*tmpl_list
= NULL
;
624 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(templates
.list_view
));
625 if (!gtk_tree_model_get_iter_first(model
, &iter
))
629 gtk_tree_model_get(model
, &iter
,
636 ntmpl
= g_new(Template
, 1);
637 ntmpl
->load_filename
= NULL
;
638 ntmpl
->name
= tmpl
->name
&& *(tmpl
->name
)
639 ? g_strdup(tmpl
->name
)
641 ntmpl
->subject
= tmpl
->subject
&& *(tmpl
->subject
)
642 ? g_strdup(tmpl
->subject
)
644 ntmpl
->from
= tmpl
->from
&& *(tmpl
->from
)
645 ? g_strdup(tmpl
->from
)
647 ntmpl
->to
= tmpl
->to
&& *(tmpl
->to
)
650 ntmpl
->cc
= tmpl
->cc
&& *(tmpl
->cc
)
653 ntmpl
->bcc
= tmpl
->bcc
&& *(tmpl
->bcc
)
654 ? g_strdup(tmpl
->bcc
)
656 ntmpl
->replyto
= tmpl
->replyto
&& *(tmpl
->replyto
)
657 ? g_strdup(tmpl
->replyto
)
659 ntmpl
->value
= tmpl
->value
&& *(tmpl
->value
)
660 ? g_strdup(tmpl
->value
)
662 tmpl_list
= g_slist_append(tmpl_list
, ntmpl
);
665 } while (gtk_tree_model_iter_next(model
, &iter
));
670 gboolean
prefs_template_string_is_valid(gchar
*string
, gint
*line
, gboolean escaped_string
, gboolean email
)
672 gboolean result
= TRUE
;
673 if (string
&& *string
!= '\0') {
677 PrefsAccount
*account
= account_get_default();
679 if (escaped_string
) {
680 tmp
= malloc(strlen(string
)+1);
681 pref_get_unescaped_pref(tmp
, string
);
683 tmp
= g_strdup(string
);
685 memset(&dummyinfo
, 0, sizeof(MsgInfo
));
686 /* init dummy fields, so we can test the result of the parse */
687 dummyinfo
.date
="Sat, 30 May 2009 01:23:45 +0200";
688 dummyinfo
.fromname
="John Doe";
689 dummyinfo
.from
="John Doe <john@example.com>";
690 dummyinfo
.to
="John Doe <john@example.com>";
691 dummyinfo
.cc
="John Doe <john@example.com>";
692 dummyinfo
.msgid
="<1234john@example.com>";
693 dummyinfo
.inreplyto
="<1234john@example.com>";
694 dummyinfo
.newsgroups
="alt.test";
695 dummyinfo
.subject
="subject";
699 quote_fmt_init(&dummyinfo
, NULL
, NULL
, TRUE
, account
, FALSE
, NULL
);
701 quote_fmt_init(&dummyinfo
, NULL
, NULL
, TRUE
, account
, FALSE
);
703 quote_fmt_scan_string(tmp
);
706 parsed_buf
= quote_fmt_get_buffer();
709 *line
= quote_fmt_get_line();
710 quote_fmtlex_destroy();
713 quote_fmt_reset_vartable();
714 quote_fmtlex_destroy();
719 static gboolean
prefs_template_list_view_set_row(gint row
)
720 /* return TRUE if the row could be modified */
731 GtkTextBuffer
*buffer
;
732 GtkTextIter start
, end
;
735 buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates
.text_value
));
736 gtk_text_buffer_get_start_iter(buffer
, &start
);
737 gtk_text_buffer_get_iter_at_offset(buffer
, &end
, -1);
738 value
= gtk_text_buffer_get_text(buffer
, &start
, &end
, FALSE
);
740 if (value
&& *value
== '\0') {
744 if (!prefs_template_string_is_valid(value
, &line
, TRUE
, FALSE
)) {
745 alertpanel_error(_("The body of the template has an error at line %d."), line
);
750 name
= gtk_editable_get_chars(GTK_EDITABLE(templates
.entry_name
),
753 alertpanel_error(_("The template's name is not set."));
757 from
= gtk_editable_get_chars(GTK_EDITABLE(templates
.entry_from
),
759 to
= gtk_editable_get_chars(GTK_EDITABLE(templates
.entry_to
),
761 cc
= gtk_editable_get_chars(GTK_EDITABLE(templates
.entry_cc
),
763 bcc
= gtk_editable_get_chars(GTK_EDITABLE(templates
.entry_bcc
),
765 replyto
= gtk_editable_get_chars(GTK_EDITABLE(templates
.entry_replyto
),
767 subject
= gtk_editable_get_chars(GTK_EDITABLE(templates
.entry_subject
),
770 if (from
&& *from
== '\0') {
774 if (to
&& *to
== '\0') {
778 if (cc
&& *cc
== '\0') {
782 if (bcc
&& *bcc
== '\0') {
786 if (replyto
&& *replyto
== '\0') {
790 if (subject
&& *subject
== '\0') {
795 if (!prefs_template_string_is_valid(from
, NULL
, TRUE
, TRUE
)) {
796 alertpanel_error(_("The \"From\" field of the template contains an invalid email address."));
801 if (!prefs_template_string_is_valid(to
, NULL
, TRUE
, TRUE
)) {
802 alertpanel_error(_("The \"To\" field of the template contains an invalid email address."));
807 if (!prefs_template_string_is_valid(cc
, NULL
, TRUE
, TRUE
)) {
808 alertpanel_error(_("The \"Cc\" field of the template contains an invalid email address."));
813 if (!prefs_template_string_is_valid(bcc
, NULL
, TRUE
, TRUE
)) {
814 alertpanel_error(_("The \"Bcc\" field of the template contains an invalid email address."));
819 if (!prefs_template_string_is_valid(replyto
, NULL
, TRUE
, TRUE
)) {
820 alertpanel_error(_("The \"Reply-To\" field of the template contains an invalid email address."));
825 if (!prefs_template_string_is_valid(subject
, NULL
, TRUE
, FALSE
)) {
826 alertpanel_error(_("The \"Subject\" field of the template is invalid."));
832 tmpl
= g_new(Template
, 1);
833 tmpl
->load_filename
= NULL
;
835 tmpl
->subject
= subject
;
840 tmpl
->replyto
= replyto
;
843 prefs_template_list_view_insert_template(templates
.list_view
,
844 row
, tmpl
->name
, tmpl
);
849 static void prefs_template_register_cb(gpointer action
, gpointer data
)
851 modified
= !prefs_template_list_view_set_row(-1);
852 modified_list
= TRUE
;
855 static void prefs_template_substitute_cb(gpointer action
, gpointer data
)
862 row
= gtkut_list_view_get_selected_row(templates
.list_view
);
866 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(templates
.list_view
));
867 if (!gtk_tree_model_iter_nth_child(model
, &iter
, NULL
, row
))
870 gtk_tree_model_get(model
, &iter
, TEMPL_DATA
, &tmpl
, -1);
874 modified
= !prefs_template_list_view_set_row(row
);
875 modified_list
= TRUE
;
878 static void prefs_template_delete_cb(gpointer action
, gpointer data
)
885 row
= gtkut_list_view_get_selected_row(templates
.list_view
);
889 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(templates
.list_view
));
890 if (!gtk_tree_model_iter_nth_child(model
, &iter
, NULL
, row
))
893 gtk_tree_model_get(model
, &iter
, TEMPL_DATA
, &tmpl
, -1);
897 if (alertpanel(_("Delete template"),
898 _("Do you really want to delete this template?"),
899 NULL
, _("_Cancel"), "edit-delete", _("D_elete"), NULL
, NULL
,
900 ALERTFOCUS_FIRST
) != G_ALERTALTERNATE
)
903 gtk_list_store_remove(GTK_LIST_STORE(model
), &iter
);
904 prefs_template_reset_dialog();
905 modified_list
= TRUE
;
908 static void prefs_template_delete_all_cb(gpointer action
, gpointer data
)
910 if (alertpanel(_("Delete all templates"),
911 _("Do you really want to delete all the templates?"),
912 NULL
, _("_Cancel"), "edit-delete", _("D_elete"), NULL
, NULL
,
913 ALERTFOCUS_SECOND
) == G_ALERTDEFAULT
)
916 prefs_template_clear_list();
919 prefs_template_reset_dialog();
920 modified_list
= TRUE
;
923 static void prefs_template_duplicate_cb(gpointer action
, gpointer data
)
930 row
= gtkut_list_view_get_selected_row(templates
.list_view
);
934 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(templates
.list_view
));
935 if (!gtk_tree_model_iter_nth_child(model
, &iter
, NULL
, row
))
938 gtk_tree_model_get(model
, &iter
, TEMPL_DATA
, &tmpl
, -1);
942 modified_list
= !prefs_template_list_view_set_row(-row
-2);
945 static void prefs_template_clear_cb(gpointer action
, gpointer data
)
947 prefs_template_reset_dialog();
950 static void prefs_template_top_cb(gpointer action
, gpointer data
)
953 GtkTreeIter top
, sel
;
956 row
= gtkut_list_view_get_selected_row(templates
.list_view
);
960 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(templates
.list_view
));
962 if (!gtk_tree_model_iter_nth_child(model
, &top
, NULL
, 0)
963 || !gtk_tree_model_iter_nth_child(model
, &sel
, NULL
, row
))
966 gtk_list_store_move_after(GTK_LIST_STORE(model
), &sel
, &top
);
967 gtkut_list_view_select_row(templates
.list_view
, 1);
968 modified_list
= TRUE
;
971 static void prefs_template_up_cb(gpointer action
, gpointer data
)
974 GtkTreeIter top
, sel
;
977 row
= gtkut_list_view_get_selected_row(templates
.list_view
);
981 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(templates
.list_view
));
983 if (!gtk_tree_model_iter_nth_child(model
, &top
, NULL
, row
- 1)
984 || !gtk_tree_model_iter_nth_child(model
, &sel
, NULL
, row
))
987 gtk_list_store_swap(GTK_LIST_STORE(model
), &top
, &sel
);
988 gtkut_list_view_select_row(templates
.list_view
, row
- 1);
989 modified_list
= TRUE
;
992 static void prefs_template_down_cb(gpointer action
, gpointer data
)
995 GtkTreeIter top
, sel
;
998 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(templates
.list_view
));
999 n_rows
= gtk_tree_model_iter_n_children(model
, NULL
);
1000 row
= gtkut_list_view_get_selected_row(templates
.list_view
);
1001 if (row
< 1 || row
>= n_rows
- 1)
1004 if (!gtk_tree_model_iter_nth_child(model
, &top
, NULL
, row
)
1005 || !gtk_tree_model_iter_nth_child(model
, &sel
, NULL
, row
+ 1))
1008 gtk_list_store_swap(GTK_LIST_STORE(model
), &top
, &sel
);
1009 gtkut_list_view_select_row(templates
.list_view
, row
+ 1);
1010 modified_list
= TRUE
;
1013 static void prefs_template_bottom_cb(gpointer action
, gpointer data
)
1016 GtkTreeIter top
, sel
;
1017 GtkTreeModel
*model
;
1019 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(templates
.list_view
));
1020 n_rows
= gtk_tree_model_iter_n_children(model
, NULL
);
1021 row
= gtkut_list_view_get_selected_row(templates
.list_view
);
1022 if (row
< 1 || row
>= n_rows
- 1)
1025 if (!gtk_tree_model_iter_nth_child(model
, &top
, NULL
, row
)
1026 || !gtk_tree_model_iter_nth_child(model
, &sel
, NULL
, n_rows
- 1))
1029 gtk_list_store_move_after(GTK_LIST_STORE(model
), &top
, &sel
);
1030 gtkut_list_view_select_row(templates
.list_view
, n_rows
- 1);
1031 modified_list
= TRUE
;
1034 static GtkListStore
* prefs_template_create_data_store(void)
1036 return gtk_list_store_new(N_TEMPL_COLUMNS
,
1039 G_TYPE_AUTO_POINTER
,
1043 static void prefs_template_list_view_insert_template(GtkWidget
*list_view
,
1045 const gchar
*template,
1049 GtkTreeIter sibling
;
1050 GtkListStore
*list_store
= GTK_LIST_STORE(gtk_tree_view_get_model
1051 (GTK_TREE_VIEW(list_view
)));
1053 /* row -1 to add a new rule to store,
1054 row >=0 to change an existing row
1055 row <-1 insert a new row after (-row-2)
1058 /* modify the existing */
1059 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store
),
1062 } else if (row
< -1 ) {
1063 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store
),
1064 &sibling
, NULL
, -row
-2))
1070 gtk_list_store_append(list_store
, &iter
);
1071 gtk_list_store_set(list_store
, &iter
,
1072 TEMPL_TEXT
, template,
1075 } else if (row
< -1) {
1077 gtk_list_store_insert_after(list_store
, &iter
, &sibling
);
1078 gtk_list_store_set(list_store
, &iter
,
1079 TEMPL_TEXT
, template,
1083 /* change existing */
1085 g_auto_pointer_new_with_free(data
, (GFreeFunc
) template_free
);
1087 /* if replacing data in an existing row, the auto pointer takes care
1088 * of destroying the Template data */
1089 gtk_list_store_set(list_store
, &iter
,
1090 TEMPL_TEXT
, template,
1092 TEMPL_AUTO_DATA
, auto_data
,
1095 g_auto_pointer_free(auto_data
);
1099 static GtkActionGroup
*prefs_template_popup_action
= NULL
;
1100 static GtkWidget
*prefs_template_popup_menu
= NULL
;
1102 static GtkActionEntry prefs_template_popup_entries
[] =
1104 {"PrefsTemplatePopup", NULL
, "PrefsTemplatePopup", NULL
, NULL
, NULL
},
1105 {"PrefsTemplatePopup/Delete", NULL
, N_("_Delete"), NULL
, NULL
, G_CALLBACK(prefs_template_delete_cb
) },
1106 {"PrefsTemplatePopup/DeleteAll", NULL
, N_("Delete _all"), NULL
, NULL
, G_CALLBACK(prefs_template_delete_all_cb
) },
1107 {"PrefsTemplatePopup/Duplicate", NULL
, N_("D_uplicate"), NULL
, NULL
, G_CALLBACK(prefs_template_duplicate_cb
) },
1110 static void prefs_template_row_selected(GtkTreeSelection
*selection
,
1111 GtkTreeView
*list_view
)
1115 GtkTreeModel
*model
;
1117 if (!gtk_tree_selection_get_selected(selection
, &model
, &iter
))
1120 path
= gtk_tree_model_get_path(model
, &iter
);
1121 prefs_template_select_row(list_view
, path
);
1122 gtk_tree_path_free(path
);
1125 static gint
prefs_template_list_btn_pressed(GtkWidget
*widget
, GdkEventButton
*event
,
1126 GtkTreeView
*list_view
)
1129 /* left- or right-button click */
1130 if (event
->button
== 1 || event
->button
== 3) {
1131 GtkTreePath
*path
= NULL
;
1132 if (gtk_tree_view_get_path_at_pos( list_view
, event
->x
, event
->y
,
1133 &path
, NULL
, NULL
, NULL
)) {
1134 prefs_template_select_row(list_view
, path
);
1137 gtk_tree_path_free(path
);
1140 /* right-button click */
1141 if (event
->button
== 3) {
1142 GtkTreeModel
*model
= gtk_tree_view_get_model(list_view
);
1147 if (!prefs_template_popup_menu
) {
1148 prefs_template_popup_action
= cm_menu_create_action_group("PrefsTemplatePopup", prefs_template_popup_entries
,
1149 G_N_ELEMENTS(prefs_template_popup_entries
), (gpointer
)list_view
);
1150 MENUITEM_ADDUI("/Menus", "PrefsTemplatePopup", "PrefsTemplatePopup", GTK_UI_MANAGER_MENU
)
1151 MENUITEM_ADDUI("/Menus/PrefsTemplatePopup", "Delete", "PrefsTemplatePopup/Delete", GTK_UI_MANAGER_MENUITEM
)
1152 MENUITEM_ADDUI("/Menus/PrefsTemplatePopup", "DeleteAll", "PrefsTemplatePopup/DeleteAll", GTK_UI_MANAGER_MENUITEM
)
1153 MENUITEM_ADDUI("/Menus/PrefsTemplatePopup", "Duplicate", "PrefsTemplatePopup/Duplicate", GTK_UI_MANAGER_MENUITEM
)
1154 prefs_template_popup_menu
= gtk_menu_item_get_submenu(GTK_MENU_ITEM(
1155 gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/PrefsTemplatePopup")) );
1158 /* grey out some popup menu items if there is no selected row */
1159 row
= gtkut_list_view_get_selected_row(GTK_WIDGET(list_view
));
1160 cm_menu_set_sensitive("PrefsTemplatePopup/Delete", (row
> 0));
1161 cm_menu_set_sensitive("PrefsTemplatePopup/Duplicate", (row
> 0));
1163 /* grey out seom popup menu items if there is no row
1164 (not counting the (New) one at row 0) */
1165 non_empty
= gtk_tree_model_get_iter_first(model
, &iter
);
1167 non_empty
= gtk_tree_model_iter_next(model
, &iter
);
1168 cm_menu_set_sensitive("PrefsTemplatePopup/DeleteAll", non_empty
);
1170 gtk_menu_popup_at_pointer(GTK_MENU(prefs_template_popup_menu
), NULL
);
1176 static gboolean
prefs_template_list_popup_menu(GtkWidget
*widget
, gpointer data
)
1178 GtkTreeView
*list_view
= (GtkTreeView
*)data
;
1179 GdkEventButton event
;
1182 event
.time
= gtk_get_current_event_time();
1184 prefs_template_list_btn_pressed(NULL
, &event
, list_view
);
1189 static GtkWidget
*prefs_template_list_view_create(void)
1191 GtkTreeView
*list_view
;
1192 GtkTreeSelection
*selector
;
1193 GtkTreeModel
*model
;
1195 model
= GTK_TREE_MODEL(prefs_template_create_data_store());
1196 list_view
= GTK_TREE_VIEW(gtk_tree_view_new_with_model(model
));
1197 g_object_unref(model
);
1199 g_signal_connect(G_OBJECT(list_view
), "popup-menu",
1200 G_CALLBACK(prefs_template_list_popup_menu
), list_view
);
1201 g_signal_connect(G_OBJECT(list_view
), "button-press-event",
1202 G_CALLBACK(prefs_template_list_btn_pressed
), list_view
);
1204 gtk_tree_view_set_rules_hint(list_view
, prefs_common
.use_stripes_everywhere
);
1205 gtk_tree_view_set_reorderable(list_view
, TRUE
);
1207 selector
= gtk_tree_view_get_selection(list_view
);
1208 gtk_tree_selection_set_mode(selector
, GTK_SELECTION_BROWSE
);
1209 g_signal_connect(G_OBJECT(selector
), "changed",
1210 G_CALLBACK(prefs_template_row_selected
), list_view
);
1212 /* create the columns */
1213 prefs_template_create_list_view_columns(GTK_WIDGET(list_view
));
1215 return GTK_WIDGET(list_view
);
1218 static void prefs_template_create_list_view_columns(GtkWidget
*list_view
)
1220 GtkTreeViewColumn
*column
;
1221 GtkCellRenderer
*renderer
;
1223 renderer
= gtk_cell_renderer_text_new();
1224 column
= gtk_tree_view_column_new_with_attributes
1225 (_("Current templates"),
1229 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view
), column
);
1230 gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(list_view
), prefs_template_search_func_cb
, NULL
, NULL
);
1234 *\brief Triggered when a row has to be selected
1236 static void prefs_template_select_row(GtkTreeView
*list_view
, GtkTreePath
*path
)
1238 GtkTreeModel
*model
= gtk_tree_view_get_model(list_view
);
1239 GtkTreeSelection
*selection
;
1242 GtkTextBuffer
*buffer
;
1246 if (!model
|| !path
|| !gtk_tree_model_get_iter(model
, &titer
, path
))
1250 selection
= gtk_tree_view_get_selection(list_view
);
1251 gtk_tree_selection_select_path(selection
, path
);
1253 tmpl_def
.name
= _("Template");
1254 tmpl_def
.subject
= "";
1259 tmpl_def
.replyto
= "";
1260 tmpl_def
.value
= "";
1262 gtk_tree_model_get(model
, &titer
, TEMPL_DATA
, &tmpl
, -1);
1266 gtk_entry_set_text(GTK_ENTRY(templates
.entry_name
), tmpl
->name
);
1267 gtk_entry_set_text(GTK_ENTRY(templates
.entry_from
),
1268 tmpl
->from
? tmpl
->from
: "");
1269 gtk_entry_set_text(GTK_ENTRY(templates
.entry_to
),
1270 tmpl
->to
? tmpl
->to
: "");
1271 gtk_entry_set_text(GTK_ENTRY(templates
.entry_cc
),
1272 tmpl
->cc
? tmpl
->cc
: "");
1273 gtk_entry_set_text(GTK_ENTRY(templates
.entry_bcc
),
1274 tmpl
->bcc
? tmpl
->bcc
: "");
1275 gtk_entry_set_text(GTK_ENTRY(templates
.entry_replyto
),
1276 tmpl
->replyto
? tmpl
->replyto
: "");
1277 gtk_entry_set_text(GTK_ENTRY(templates
.entry_subject
),
1278 tmpl
->subject
? tmpl
->subject
: "");
1280 buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates
.text_value
));
1281 gtk_text_buffer_set_text(buffer
, "", -1);
1282 gtk_text_buffer_get_start_iter(buffer
, &iter
);
1283 gtk_text_buffer_insert(buffer
, &iter
, tmpl
->value
, -1);