diff options
Diffstat (limited to 'src/prefs_template.c')
| -rw-r--r-- | src/prefs_template.c | 685 |
1 files changed, 685 insertions, 0 deletions
diff --git a/src/prefs_template.c b/src/prefs_template.c new file mode 100644 index 0000000..a6a0b4b --- /dev/null +++ b/src/prefs_template.c | |||
| @@ -0,0 +1,685 @@ | |||
| 1 | /* | ||
| 2 | * Sylpheed templates subsystem | ||
| 3 | * Copyright (C) 2001 Alexander Barinov | ||
| 4 | * Copyright (C) 2001-2015 Hiroyuki Yamamoto | ||
| 5 | * | ||
| 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 2 of the License, or | ||
| 9 | * (at your option) any later version. | ||
| 10 | * | ||
| 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. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU General Public License | ||
| 17 | * along with this program; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include "defs.h" | ||
| 22 | |||
| 23 | #include <glib.h> | ||
| 24 | #include <glib/gi18n.h> | ||
| 25 | #include <gtk/gtk.h> | ||
| 26 | #include <gdk/gdkkeysyms.h> | ||
| 27 | #include <string.h> | ||
| 28 | #include <dirent.h> | ||
| 29 | #include <sys/stat.h> | ||
| 30 | |||
| 31 | #include "template.h" | ||
| 32 | #include "main.h" | ||
| 33 | #include "inc.h" | ||
| 34 | #include "utils.h" | ||
| 35 | #include "gtkutils.h" | ||
| 36 | #include "alertpanel.h" | ||
| 37 | #include "manage_window.h" | ||
| 38 | #include "prefs_common.h" | ||
| 39 | #include "prefs_common_dialog.h" | ||
| 40 | #include "compose.h" | ||
| 41 | #include "mainwindow.h" | ||
| 42 | #include "addr_compl.h" | ||
| 43 | #include "quote_fmt.h" | ||
| 44 | #include "plugin.h" | ||
| 45 | |||
| 46 | static struct Templates { | ||
| 47 | GtkWidget *window; | ||
| 48 | GtkWidget *clist_tmpls; | ||
| 49 | GtkWidget *entry_name; | ||
| 50 | GtkWidget *entry_to; | ||
| 51 | GtkWidget *entry_cc; | ||
| 52 | GtkWidget *entry_bcc; | ||
| 53 | GtkWidget *entry_replyto; | ||
| 54 | GtkWidget *entry_subject; | ||
| 55 | GtkWidget *text_value; | ||
| 56 | GtkWidget *confirm_area; | ||
| 57 | GtkWidget *ok_btn; | ||
| 58 | GtkWidget *cancel_btn; | ||
| 59 | |||
| 60 | gboolean entry_modified; | ||
| 61 | gboolean list_modified; | ||
| 62 | } templates; | ||
| 63 | |||
| 64 | /* widget creating functions */ | ||
| 65 | static void prefs_template_window_create (void); | ||
| 66 | static void prefs_template_window_setup (void); | ||
| 67 | static void prefs_template_clear (void); | ||
| 68 | |||
| 69 | static GSList *prefs_template_get_list (void); | ||
| 70 | |||
| 71 | /* callbacks */ | ||
| 72 | static gint prefs_template_deleted_cb (GtkWidget *widget, | ||
| 73 | GdkEventAny *event, | ||
| 74 | gpointer data); | ||
| 75 | static gboolean prefs_template_key_pressed_cb (GtkWidget *widget, | ||
| 76 | GdkEventKey *event, | ||
| 77 | gpointer data); | ||
| 78 | static void prefs_template_changed_cb (GtkEditable *editable, | ||
| 79 | gpointer data); | ||
| 80 | static void prefs_template_cancel_cb (void); | ||
| 81 | static void prefs_template_ok_cb (void); | ||
| 82 | static void prefs_template_select_cb (GtkCList *clist, | ||
| 83 | gint row, | ||
| 84 | gint column, | ||
| 85 | GdkEvent *event); | ||
| 86 | static void prefs_template_register_cb (void); | ||
| 87 | static void prefs_template_substitute_cb (void); | ||
| 88 | static void prefs_template_delete_cb (void); | ||
| 89 | static void prefs_template_up_cb (void); | ||
| 90 | static void prefs_template_down_cb (void); | ||
| 91 | |||
| 92 | /* Called from mainwindow.c */ | ||
| 93 | void prefs_template_open(void) | ||
| 94 | { | ||
| 95 | inc_lock(); | ||
| 96 | |||
| 97 | if (!templates.window) | ||
| 98 | prefs_template_window_create(); | ||
| 99 | |||
| 100 | prefs_template_window_setup(); | ||
| 101 | gtk_widget_show(templates.window); | ||
| 102 | |||
| 103 | syl_plugin_signal_emit("prefs-template-open", templates.window); | ||
| 104 | } | ||
| 105 | |||
| 106 | #define ADD_ENTRY(entry, str, row) \ | ||
| 107 | { \ | ||
| 108 | label1 = gtk_label_new(str); \ | ||
| 109 | gtk_widget_show(label1); \ | ||
| 110 | gtk_table_attach(GTK_TABLE(table), label1, 0, 1, row, (row + 1), \ | ||
| 111 | GTK_FILL, 0, 0, 0); \ | ||
| 112 | gtk_misc_set_alignment(GTK_MISC(label1), 1, 0.5); \ | ||
| 113 | \ | ||
| 114 | entry = gtk_entry_new(); \ | ||
| 115 | gtk_widget_show(entry); \ | ||
| 116 | gtk_table_attach(GTK_TABLE(table), entry, 1, 2, row, (row + 1), \ | ||
| 117 | GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); \ | ||
| 118 | g_signal_connect(G_OBJECT(entry), "changed", \ | ||
| 119 | G_CALLBACK(prefs_template_changed_cb), NULL); \ | ||
| 120 | } | ||
| 121 | |||
| 122 | static void prefs_template_window_create(void) | ||
| 123 | { | ||
| 124 | /* window structure ;) */ | ||
| 125 | GtkWidget *window; | ||
| 126 | GtkWidget *vpaned; | ||
| 127 | GtkWidget *vbox1; | ||
| 128 | GtkWidget *hbox1; | ||
| 129 | GtkWidget *label1; | ||
| 130 | GtkWidget *entry_name; | ||
| 131 | GtkWidget *table; | ||
| 132 | GtkWidget *entry_to; | ||
| 133 | GtkWidget *entry_cc; | ||
| 134 | GtkWidget *entry_bcc; | ||
| 135 | GtkWidget *entry_replyto; | ||
| 136 | GtkWidget *entry_subject; | ||
| 137 | GtkWidget *scroll2; | ||
| 138 | GtkWidget *text_value; | ||
| 139 | GtkWidget *vbox2; | ||
| 140 | GtkWidget *hbox2; | ||
| 141 | GtkWidget *arrow1; | ||
| 142 | GtkWidget *hbox3; | ||
| 143 | GtkWidget *reg_btn; | ||
| 144 | GtkWidget *subst_btn; | ||
| 145 | GtkWidget *del_btn; | ||
| 146 | GtkWidget *desc_btn; | ||
| 147 | GtkWidget *hbox4; | ||
| 148 | GtkWidget *scroll1; | ||
| 149 | GtkWidget *clist_tmpls; | ||
| 150 | GtkWidget *vbox3; | ||
| 151 | GtkWidget *vbox4; | ||
| 152 | GtkWidget *up_btn; | ||
| 153 | GtkWidget *down_btn; | ||
| 154 | GtkWidget *confirm_area; | ||
| 155 | GtkWidget *ok_btn; | ||
| 156 | GtkWidget *cancel_btn; | ||
| 157 | |||
| 158 | GtkTextBuffer *buffer; | ||
| 159 | |||
| 160 | gchar *title[1]; | ||
| 161 | |||
| 162 | /* main window */ | ||
| 163 | window = gtk_window_new(GTK_WINDOW_TOPLEVEL); | ||
| 164 | gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); | ||
| 165 | gtk_window_set_modal(GTK_WINDOW(window), TRUE); | ||
| 166 | gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, FALSE); | ||
| 167 | |||
| 168 | /* vpaned to separate template settings from templates list */ | ||
| 169 | vpaned = gtk_vpaned_new(); | ||
| 170 | gtk_widget_show(vpaned); | ||
| 171 | gtk_container_add(GTK_CONTAINER(window), vpaned); | ||
| 172 | |||
| 173 | /* vbox to handle template name and content */ | ||
| 174 | vbox1 = gtk_vbox_new(FALSE, 6); | ||
| 175 | gtk_widget_show(vbox1); | ||
| 176 | gtk_container_set_border_width(GTK_CONTAINER(vbox1), 8); | ||
| 177 | gtk_paned_pack1(GTK_PANED(vpaned), vbox1, FALSE, FALSE); | ||
| 178 | |||
| 179 | hbox1 = gtk_hbox_new(FALSE, 8); | ||
| 180 | gtk_widget_show(hbox1); | ||
| 181 | gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0); | ||
| 182 | |||
| 183 | label1 = gtk_label_new(_("Template name")); | ||
| 184 | gtk_widget_show(label1); | ||
| 185 | gtk_box_pack_start(GTK_BOX(hbox1), label1, FALSE, FALSE, 0); | ||
| 186 | |||
| 187 | entry_name = gtk_entry_new(); | ||
| 188 | gtk_widget_show(entry_name); | ||
| 189 | gtk_box_pack_start(GTK_BOX(hbox1), entry_name, TRUE, TRUE, 0); | ||
| 190 | g_signal_connect(G_OBJECT(entry_name), "changed", | ||
| 191 | G_CALLBACK(prefs_template_changed_cb), NULL); | ||
| 192 | |||
| 193 | /* table for headers */ | ||
| 194 | table = gtk_table_new(5, 2, FALSE); | ||
| 195 | gtk_widget_show(table); | ||
| 196 | gtk_box_pack_start(GTK_BOX(vbox1), table, FALSE, FALSE, 0); | ||
| 197 | gtk_table_set_row_spacings(GTK_TABLE(table), 4); | ||
| 198 | gtk_table_set_col_spacings(GTK_TABLE(table), 4); | ||
| 199 | |||
| 200 | ADD_ENTRY(entry_to, _("To:"), 0); | ||
| 201 | address_completion_register_entry(GTK_ENTRY(entry_to)); | ||
| 202 | ADD_ENTRY(entry_cc, _("Cc:"), 1); | ||
| 203 | address_completion_register_entry(GTK_ENTRY(entry_cc)); | ||
| 204 | ADD_ENTRY(entry_bcc, _("Bcc:"), 2); | ||
| 205 | address_completion_register_entry(GTK_ENTRY(entry_bcc)); | ||
| 206 | ADD_ENTRY(entry_replyto, _("Reply-To:"), 3); | ||
| 207 | address_completion_register_entry(GTK_ENTRY(entry_replyto)); | ||
| 208 | ADD_ENTRY(entry_subject, _("Subject:"), 4); | ||
| 209 | |||
| 210 | #undef ADD_ENTRY | ||
| 211 | |||
| 212 | /* template content */ | ||
| 213 | scroll2 = gtk_scrolled_window_new(NULL, NULL); | ||
| 214 | gtk_widget_show(scroll2); | ||
| 215 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll2), | ||
| 216 | GTK_POLICY_NEVER, | ||
| 217 | GTK_POLICY_AUTOMATIC); | ||
| 218 | gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll2), | ||
| 219 | GTK_SHADOW_IN); | ||
| 220 | gtk_box_pack_start(GTK_BOX(vbox1), scroll2, TRUE, TRUE, 0); | ||
| 221 | |||
| 222 | text_value = gtk_text_view_new(); | ||
| 223 | gtk_widget_show(text_value); | ||
| 224 | gtk_widget_set_size_request | ||
| 225 | (text_value, | ||
| 226 | 360 * gtkut_get_dpi_multiplier(), | ||
| 227 | 120 * gtkut_get_dpi_multiplier()); | ||
| 228 | gtk_container_add(GTK_CONTAINER(scroll2), text_value); | ||
| 229 | gtk_text_view_set_editable(GTK_TEXT_VIEW(text_value), TRUE); | ||
| 230 | buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_value)); | ||
| 231 | g_signal_connect(G_OBJECT(buffer), "changed", | ||
| 232 | G_CALLBACK(prefs_template_changed_cb), NULL); | ||
| 233 | |||
| 234 | /* vbox for buttons and templates list */ | ||
| 235 | vbox2 = gtk_vbox_new(FALSE, 6); | ||
| 236 | gtk_widget_show(vbox2); | ||
| 237 | gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8); | ||
| 238 | gtk_paned_pack2(GTK_PANED(vpaned), vbox2, TRUE, FALSE); | ||
| 239 | |||
| 240 | /* register | substitute | delete */ | ||
| 241 | hbox2 = gtk_hbox_new(FALSE, 4); | ||
| 242 | gtk_widget_show(hbox2); | ||
| 243 | gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0); | ||
| 244 | |||
| 245 | arrow1 = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT); | ||
| 246 | gtk_widget_show(arrow1); | ||
| 247 | gtk_box_pack_start(GTK_BOX(hbox2), arrow1, FALSE, FALSE, 0); | ||
| 248 | gtk_widget_set_size_request(arrow1, -1, | ||
| 249 | 16 * gtkut_get_dpi_multiplier()); | ||
| 250 | |||
| 251 | hbox3 = gtk_hbox_new(TRUE, 4); | ||
| 252 | gtk_widget_show(hbox3); | ||
| 253 | gtk_box_pack_start(GTK_BOX(hbox2), hbox3, FALSE, FALSE, 0); | ||
| 254 | |||
| 255 | reg_btn = gtk_button_new_with_label(_("Register")); | ||
| 256 | gtk_widget_show(reg_btn); | ||
| 257 | gtk_box_pack_start(GTK_BOX(hbox3), reg_btn, FALSE, TRUE, 0); | ||
| 258 | g_signal_connect(G_OBJECT (reg_btn), "clicked", | ||
| 259 | G_CALLBACK (prefs_template_register_cb), NULL); | ||
| 260 | |||
| 261 | subst_btn = gtk_button_new_with_label(_(" Substitute ")); | ||
| 262 | gtk_widget_show(subst_btn); | ||
| 263 | gtk_box_pack_start(GTK_BOX(hbox3), subst_btn, FALSE, TRUE, 0); | ||
| 264 | g_signal_connect(G_OBJECT(subst_btn), "clicked", | ||
| 265 | G_CALLBACK(prefs_template_substitute_cb), NULL); | ||
| 266 | |||
| 267 | del_btn = gtk_button_new_with_label(_("Delete")); | ||
| 268 | gtk_widget_show(del_btn); | ||
| 269 | gtk_box_pack_start(GTK_BOX(hbox3), del_btn, FALSE, TRUE, 0); | ||
| 270 | g_signal_connect(G_OBJECT(del_btn), "clicked", | ||
| 271 | G_CALLBACK(prefs_template_delete_cb), NULL); | ||
| 272 | |||
| 273 | desc_btn = gtk_button_new_with_label(_(" Symbols ")); | ||
| 274 | gtk_widget_show(desc_btn); | ||
| 275 | gtk_box_pack_end(GTK_BOX(hbox2), desc_btn, FALSE, FALSE, 0); | ||
| 276 | g_signal_connect(G_OBJECT(desc_btn), "clicked", | ||
| 277 | G_CALLBACK(prefs_quote_description), NULL); | ||
| 278 | |||
| 279 | /* templates list */ | ||
| 280 | hbox4 = gtk_hbox_new(FALSE, 8); | ||
| 281 | gtk_widget_show(hbox4); | ||
| 282 | gtk_box_pack_start(GTK_BOX(vbox2), hbox4, TRUE, TRUE, 0); | ||
| 283 | |||
| 284 | scroll1 = gtk_scrolled_window_new(NULL, NULL); | ||
| 285 | gtk_widget_show(scroll1); | ||
| 286 | gtk_box_pack_start(GTK_BOX(hbox4), scroll1, TRUE, TRUE, 0); | ||
| 287 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1), | ||
| 288 | GTK_POLICY_AUTOMATIC, | ||
| 289 | GTK_POLICY_AUTOMATIC); | ||
| 290 | |||
| 291 | title[0] = _("Registered templates"); | ||
| 292 | clist_tmpls = gtk_clist_new_with_titles(1, title); | ||
| 293 | gtk_widget_show(clist_tmpls); | ||
| 294 | gtk_widget_set_size_request(scroll1, -1, | ||
| 295 | 160 * gtkut_get_dpi_multiplier()); | ||
| 296 | gtk_container_add(GTK_CONTAINER(scroll1), clist_tmpls); | ||
| 297 | gtk_clist_set_column_width(GTK_CLIST(clist_tmpls), 0, 80); | ||
| 298 | gtk_clist_set_selection_mode(GTK_CLIST(clist_tmpls), | ||
| 299 | GTK_SELECTION_BROWSE); | ||
| 300 | gtkut_clist_set_redraw(GTK_CLIST(clist_tmpls)); | ||
| 301 | GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist_tmpls)->column[0].button, | ||
| 302 | GTK_CAN_FOCUS); | ||
| 303 | g_signal_connect(G_OBJECT (clist_tmpls), "select_row", | ||
| 304 | G_CALLBACK (prefs_template_select_cb), NULL); | ||
| 305 | |||
| 306 | vbox3 = gtk_vbox_new(TRUE, 0); | ||
| 307 | gtk_widget_show(vbox3); | ||
| 308 | gtk_box_pack_start(GTK_BOX(hbox4), vbox3, FALSE, FALSE, 0); | ||
| 309 | |||
| 310 | vbox4 = gtk_vbox_new(TRUE, 8); | ||
| 311 | gtk_widget_show(vbox4); | ||
| 312 | gtk_box_pack_start(GTK_BOX(vbox3), vbox4, TRUE, FALSE, 0); | ||
| 313 | |||
| 314 | up_btn = gtk_button_new_with_label(_("Up")); | ||
| 315 | gtk_widget_show(up_btn); | ||
| 316 | gtk_box_pack_start(GTK_BOX(vbox4), up_btn, FALSE, FALSE, 0); | ||
| 317 | g_signal_connect(G_OBJECT (up_btn), "clicked", | ||
| 318 | G_CALLBACK (prefs_template_up_cb), NULL); | ||
| 319 | |||
| 320 | down_btn = gtk_button_new_with_label(_("Down")); | ||
| 321 | gtk_widget_show(down_btn); | ||
| 322 | gtk_box_pack_start(GTK_BOX(vbox4), down_btn, FALSE, FALSE, 0); | ||
| 323 | g_signal_connect(G_OBJECT (down_btn), "clicked", | ||
| 324 | G_CALLBACK (prefs_template_down_cb), NULL); | ||
| 325 | |||
| 326 | /* ok | cancel */ | ||
| 327 | gtkut_stock_button_set_create(&confirm_area, &ok_btn, GTK_STOCK_OK, | ||
| 328 | &cancel_btn, GTK_STOCK_CANCEL, | ||
| 329 | NULL, NULL); | ||
| 330 | gtk_widget_show(confirm_area); | ||
| 331 | gtk_box_pack_end(GTK_BOX(vbox2), confirm_area, FALSE, FALSE, 0); | ||
| 332 | gtk_widget_grab_default(ok_btn); | ||
| 333 | |||
| 334 | gtk_window_set_title(GTK_WINDOW(window), _("Templates")); | ||
| 335 | |||
| 336 | g_signal_connect(G_OBJECT(window), "delete_event", | ||
| 337 | G_CALLBACK(prefs_template_deleted_cb), NULL); | ||
| 338 | g_signal_connect(G_OBJECT(window), "key_press_event", | ||
| 339 | G_CALLBACK(prefs_template_key_pressed_cb), NULL); | ||
| 340 | MANAGE_WINDOW_SIGNALS_CONNECT(window); | ||
| 341 | g_signal_connect(G_OBJECT(ok_btn), "clicked", | ||
| 342 | G_CALLBACK(prefs_template_ok_cb), NULL); | ||
| 343 | g_signal_connect(G_OBJECT(cancel_btn), "clicked", | ||
| 344 | G_CALLBACK(prefs_template_cancel_cb), NULL); | ||
| 345 | |||
| 346 | address_completion_start(window); | ||
| 347 | |||
| 348 | templates.window = window; | ||
| 349 | templates.clist_tmpls = clist_tmpls; | ||
| 350 | templates.entry_name = entry_name; | ||
| 351 | templates.entry_to = entry_to; | ||
| 352 | templates.entry_cc = entry_cc; | ||
| 353 | templates.entry_bcc = entry_bcc; | ||
| 354 | templates.entry_replyto = entry_replyto; | ||
| 355 | templates.entry_subject = entry_subject; | ||
| 356 | templates.text_value = text_value; | ||
| 357 | templates.confirm_area = confirm_area; | ||
| 358 | templates.ok_btn = ok_btn; | ||
| 359 | templates.cancel_btn = cancel_btn; | ||
| 360 | templates.entry_modified = FALSE; | ||
| 361 | templates.list_modified = FALSE; | ||
| 362 | } | ||
| 363 | |||
| 364 | static void prefs_template_window_setup(void) | ||
| 365 | { | ||
| 366 | GtkCList *clist = GTK_CLIST(templates.clist_tmpls); | ||
| 367 | GSList *tmpl_list; | ||
| 368 | GSList *cur; | ||
| 369 | gchar *title[1]; | ||
| 370 | gint row; | ||
| 371 | Template *tmpl; | ||
| 372 | |||
| 373 | gtkut_box_set_reverse_order(GTK_BOX(templates.confirm_area), | ||
| 374 | !prefs_common.comply_gnome_hig); | ||
| 375 | manage_window_set_transient(GTK_WINDOW(templates.window)); | ||
| 376 | gtk_widget_grab_focus(templates.ok_btn); | ||
| 377 | |||
| 378 | gtk_clist_freeze(clist); | ||
| 379 | gtk_clist_clear(clist); | ||
| 380 | |||
| 381 | title[0] = _("(New)"); | ||
| 382 | row = gtk_clist_append(clist, title); | ||
| 383 | gtk_clist_set_row_data(clist, row, NULL); | ||
| 384 | |||
| 385 | tmpl_list = template_read_config(); | ||
| 386 | |||
| 387 | for (cur = tmpl_list; cur != NULL; cur = cur->next) { | ||
| 388 | tmpl = (Template *)cur->data; | ||
| 389 | title[0] = tmpl->name; | ||
| 390 | row = gtk_clist_append(clist, title); | ||
| 391 | gtk_clist_set_row_data(clist, row, tmpl); | ||
| 392 | } | ||
| 393 | |||
| 394 | g_slist_free(tmpl_list); | ||
| 395 | |||
| 396 | gtk_clist_thaw(clist); | ||
| 397 | |||
| 398 | templates.entry_modified = FALSE; | ||
| 399 | templates.list_modified = FALSE; | ||
| 400 | } | ||
| 401 | |||
| 402 | static void prefs_template_clear(void) | ||
| 403 | { | ||
| 404 | Template *tmpl; | ||
| 405 | gint row = 1; | ||
| 406 | |||
| 407 | while ((tmpl = gtk_clist_get_row_data | ||
| 408 | (GTK_CLIST(templates.clist_tmpls), row)) != NULL) { | ||
| 409 | template_free(tmpl); | ||
| 410 | row++; | ||
| 411 | } | ||
| 412 | |||
| 413 | gtk_clist_clear(GTK_CLIST(templates.clist_tmpls)); | ||
| 414 | } | ||
| 415 | |||
| 416 | static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event, | ||
| 417 | gpointer data) | ||
| 418 | { | ||
| 419 | prefs_template_cancel_cb(); | ||
| 420 | return TRUE; | ||
| 421 | } | ||
| 422 | |||
| 423 | static gboolean prefs_template_key_pressed_cb(GtkWidget *widget, | ||
| 424 | GdkEventKey *event, gpointer data) | ||
| 425 | { | ||
| 426 | if (event && event->keyval == GDK_Escape) | ||
| 427 | prefs_template_cancel_cb(); | ||
| 428 | return FALSE; | ||
| 429 | } | ||
| 430 | |||
| 431 | static void prefs_template_changed_cb(GtkEditable *editable, gpointer data) | ||
| 432 | { | ||
| 433 | templates.entry_modified = TRUE; | ||
| 434 | } | ||
| 435 | |||
| 436 | static void prefs_template_ok_cb(void) | ||
| 437 | { | ||
| 438 | GSList *tmpl_list; | ||
| 439 | |||
| 440 | if (templates.entry_modified) { | ||
| 441 | if (alertpanel(_("Template is modified"), | ||
| 442 | _("Current modification is not applied. Finish without saving it?"), | ||
| 443 | GTK_STOCK_YES, GTK_STOCK_NO, NULL) | ||
| 444 | != G_ALERTDEFAULT) | ||
| 445 | return; | ||
| 446 | } | ||
| 447 | |||
| 448 | tmpl_list = prefs_template_get_list(); | ||
| 449 | template_set_config(tmpl_list); | ||
| 450 | compose_reflect_prefs_all(); | ||
| 451 | gtk_clist_clear(GTK_CLIST(templates.clist_tmpls)); | ||
| 452 | gtk_widget_hide(templates.window); | ||
| 453 | main_window_popup(main_window_get()); | ||
| 454 | inc_unlock(); | ||
| 455 | } | ||
| 456 | |||
| 457 | static void prefs_template_cancel_cb(void) | ||
| 458 | { | ||
| 459 | if (templates.entry_modified || templates.list_modified) { | ||
| 460 | if (alertpanel(_("Templates are modified"), | ||
| 461 | _("Really discard modification to templates?"), | ||
| 462 | GTK_STOCK_YES, GTK_STOCK_NO, NULL) | ||
| 463 | != G_ALERTDEFAULT) | ||
| 464 | return; | ||
| 465 | } | ||
| 466 | |||
| 467 | prefs_template_clear(); | ||
| 468 | gtk_widget_hide(templates.window); | ||
| 469 | main_window_popup(main_window_get()); | ||
| 470 | inc_unlock(); | ||
| 471 | } | ||
| 472 | |||
| 473 | static void prefs_template_select_cb(GtkCList *clist, gint row, gint column, | ||
| 474 | GdkEvent *event) | ||
| 475 | { | ||
| 476 | Template *tmpl; | ||
| 477 | Template tmpl_def; | ||
| 478 | GtkTextBuffer *buffer; | ||
| 479 | GtkTextIter iter; | ||
| 480 | |||
| 481 | tmpl_def.name = _("Template"); | ||
| 482 | tmpl_def.subject = ""; | ||
| 483 | tmpl_def.to = ""; | ||
| 484 | tmpl_def.cc = ""; | ||
| 485 | tmpl_def.bcc = ""; | ||
| 486 | tmpl_def.replyto = ""; | ||
| 487 | tmpl_def.value = ""; | ||
| 488 | |||
| 489 | if (!(tmpl = gtk_clist_get_row_data(clist, row))) | ||
| 490 | tmpl = &tmpl_def; | ||
| 491 | |||
| 492 | gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name); | ||
| 493 | gtk_entry_set_text(GTK_ENTRY(templates.entry_to), | ||
| 494 | tmpl->to ? tmpl->to : ""); | ||
| 495 | gtk_entry_set_text(GTK_ENTRY(templates.entry_cc), | ||
| 496 | tmpl->cc ? tmpl->cc : ""); | ||
| 497 | gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc), | ||
| 498 | tmpl->bcc ? tmpl->bcc : ""); | ||
| 499 | gtk_entry_set_text(GTK_ENTRY(templates.entry_replyto), | ||
| 500 | tmpl->replyto ? tmpl->replyto : ""); | ||
| 501 | gtk_entry_set_text(GTK_ENTRY(templates.entry_subject), | ||
| 502 | tmpl->subject ? tmpl->subject : ""); | ||
| 503 | |||
| 504 | buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value)); | ||
| 505 | gtk_text_buffer_set_text(buffer, "", 0); | ||
| 506 | gtk_text_buffer_get_start_iter(buffer, &iter); | ||
| 507 | gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1); | ||
| 508 | |||
| 509 | templates.entry_modified = FALSE; | ||
| 510 | } | ||
| 511 | |||
| 512 | static GSList *prefs_template_get_list(void) | ||
| 513 | { | ||
| 514 | gint row = 1; | ||
| 515 | GSList *tmpl_list = NULL; | ||
| 516 | Template *tmpl; | ||
| 517 | |||
| 518 | while ((tmpl = gtk_clist_get_row_data | ||
| 519 | (GTK_CLIST(templates.clist_tmpls), row)) != NULL) { | ||
| 520 | tmpl->tmplid = row; | ||
| 521 | tmpl_list = g_slist_append(tmpl_list, tmpl); | ||
| 522 | row++; | ||
| 523 | } | ||
| 524 | |||
| 525 | return tmpl_list; | ||
| 526 | } | ||
| 527 | |||
| 528 | static gint prefs_template_clist_set_row(gint row) | ||
| 529 | { | ||
| 530 | GtkCList *clist = GTK_CLIST(templates.clist_tmpls); | ||
| 531 | Template *tmpl; | ||
| 532 | Template *tmp_tmpl; | ||
| 533 | GtkTextBuffer *buffer; | ||
| 534 | GtkTextIter start, end; | ||
| 535 | gchar *name; | ||
| 536 | gchar *to; | ||
| 537 | gchar *cc; | ||
| 538 | gchar *bcc; | ||
| 539 | gchar *replyto; | ||
| 540 | gchar *subject; | ||
| 541 | gchar *value; | ||
| 542 | gchar *title[1]; | ||
| 543 | |||
| 544 | g_return_val_if_fail(row != 0, -1); | ||
| 545 | |||
| 546 | buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value)); | ||
| 547 | gtk_text_buffer_get_start_iter(buffer, &start); | ||
| 548 | gtk_text_buffer_get_end_iter(buffer, &end); | ||
| 549 | value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE); | ||
| 550 | |||
| 551 | if (value && *value != '\0') { | ||
| 552 | gchar *parsed_buf; | ||
| 553 | MsgInfo dummyinfo; | ||
| 554 | |||
| 555 | memset(&dummyinfo, 0, sizeof(MsgInfo)); | ||
| 556 | quote_fmt_init(&dummyinfo, NULL, NULL); | ||
| 557 | quote_fmt_scan_string(value); | ||
| 558 | quote_fmt_parse(); | ||
| 559 | parsed_buf = quote_fmt_get_buffer(); | ||
| 560 | if (!parsed_buf) { | ||
| 561 | alertpanel_error(_("Template format error.")); | ||
| 562 | g_free(value); | ||
| 563 | return -1; | ||
| 564 | } | ||
| 565 | } | ||
| 566 | |||
| 567 | name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name), | ||
| 568 | 0, -1); | ||
| 569 | to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to), 0, -1); | ||
| 570 | cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc), 0, -1); | ||
| 571 | bcc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_bcc), 0, -1); | ||
| 572 | replyto = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_replyto), | ||
| 573 | 0, -1); | ||
| 574 | subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject), | ||
| 575 | 0, -1); | ||
| 576 | |||
| 577 | #define NULLIFY_IF_EMPTY(val) \ | ||
| 578 | if (val && *val == '\0') { \ | ||
| 579 | g_free(val); \ | ||
| 580 | val = NULL; \ | ||
| 581 | } | ||
| 582 | |||
| 583 | NULLIFY_IF_EMPTY(to); | ||
| 584 | NULLIFY_IF_EMPTY(cc); | ||
| 585 | NULLIFY_IF_EMPTY(bcc); | ||
| 586 | NULLIFY_IF_EMPTY(replyto); | ||
| 587 | NULLIFY_IF_EMPTY(subject); | ||
| 588 | |||
| 589 | #undef NULLIFY_IF_EMPTY | ||
| 590 | |||
| 591 | tmpl = g_new(Template, 1); | ||
| 592 | tmpl->name = name; | ||
| 593 | tmpl->to = to; | ||
| 594 | tmpl->cc = cc; | ||
| 595 | tmpl->bcc = bcc; | ||
| 596 | tmpl->replyto = replyto; | ||
| 597 | tmpl->subject = subject; | ||
| 598 | tmpl->value = value; | ||
| 599 | |||
| 600 | title[0] = name; | ||
| 601 | |||
| 602 | if (row < 0) { | ||
| 603 | row = gtk_clist_append(clist, title); | ||
| 604 | } else { | ||
| 605 | gtk_clist_set_text(clist, row, 0, name); | ||
| 606 | tmp_tmpl = gtk_clist_get_row_data(clist, row); | ||
| 607 | if (tmp_tmpl) | ||
| 608 | template_free(tmp_tmpl); | ||
| 609 | } | ||
| 610 | |||
| 611 | gtk_clist_set_row_data(clist, row, tmpl); | ||
| 612 | templates.entry_modified = FALSE; | ||
| 613 | templates.list_modified = TRUE; | ||
| 614 | |||
| 615 | return row; | ||
| 616 | } | ||
| 617 | |||
| 618 | static void prefs_template_register_cb(void) | ||
| 619 | { | ||
| 620 | prefs_template_clist_set_row(-1); | ||
| 621 | } | ||
| 622 | |||
| 623 | static void prefs_template_substitute_cb(void) | ||
| 624 | { | ||
| 625 | GtkCList *clist = GTK_CLIST(templates.clist_tmpls); | ||
| 626 | Template *tmpl; | ||
| 627 | gint row; | ||
| 628 | |||
| 629 | if (!clist->selection) return; | ||
| 630 | |||
| 631 | row = GPOINTER_TO_INT(clist->selection->data); | ||
| 632 | if (row == 0) return; | ||
| 633 | |||
| 634 | tmpl = gtk_clist_get_row_data(clist, row); | ||
| 635 | if (!tmpl) return; | ||
| 636 | |||
| 637 | prefs_template_clist_set_row(row); | ||
| 638 | } | ||
| 639 | |||
| 640 | static void prefs_template_delete_cb(void) | ||
| 641 | { | ||
| 642 | GtkCList *clist = GTK_CLIST(templates.clist_tmpls); | ||
| 643 | Template *tmpl; | ||
| 644 | gint row; | ||
| 645 | |||
| 646 | if (!clist->selection) return; | ||
| 647 | row = GPOINTER_TO_INT(clist->selection->data); | ||
| 648 | if (row == 0) return; | ||
| 649 | |||
| 650 | if (alertpanel(_("Delete template"), | ||
| 651 | _("Do you really want to delete this template?"), | ||
| 652 | GTK_STOCK_YES, GTK_STOCK_NO, NULL) != G_ALERTDEFAULT) | ||
| 653 | return; | ||
| 654 | |||
| 655 | tmpl = gtk_clist_get_row_data(clist, row); | ||
| 656 | template_free(tmpl); | ||
| 657 | gtk_clist_remove(clist, row); | ||
| 658 | templates.list_modified = TRUE; | ||
| 659 | } | ||
| 660 | |||
| 661 | static void prefs_template_up_cb(void) | ||
| 662 | { | ||
| 663 | GtkCList *clist = GTK_CLIST(templates.clist_tmpls); | ||
| 664 | gint row; | ||
| 665 | |||
| 666 | if (!clist->selection) return; | ||
| 667 | row = GPOINTER_TO_INT(clist->selection->data); | ||
| 668 | if (row > 1) { | ||
| 669 | gtk_clist_row_move(clist, row, row - 1); | ||
| 670 | templates.list_modified = TRUE; | ||
| 671 | } | ||
| 672 | } | ||
| 673 | |||
| 674 | static void prefs_template_down_cb(void) | ||
| 675 | { | ||
| 676 | GtkCList *clist = GTK_CLIST(templates.clist_tmpls); | ||
| 677 | gint row; | ||
| 678 | |||
| 679 | if (!clist->selection) return; | ||
| 680 | row = GPOINTER_TO_INT(clist->selection->data); | ||
| 681 | if (row > 0 && row < clist->rows - 1) { | ||
| 682 | gtk_clist_row_move(clist, row, row + 1); | ||
| 683 | templates.list_modified = TRUE; | ||
| 684 | } | ||
| 685 | } | ||
