root / src / prefs_customheader.c @ 478
History | View | Annotate | Download (16.6 kB)
| 1 | /*
|
|---|---|
| 2 | * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client |
| 3 | * Copyright (C) 1999-2005 Hiroyuki Yamamoto |
| 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 2 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, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | #ifdef HAVE_CONFIG_H
|
| 21 | # include "config.h" |
| 22 | #endif
|
| 23 | |
| 24 | #include "defs.h" |
| 25 | |
| 26 | #include <glib.h> |
| 27 | #include <glib/gi18n.h> |
| 28 | #include <gtk/gtk.h> |
| 29 | #include <gdk/gdkkeysyms.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <errno.h> |
| 34 | |
| 35 | #include "main.h" |
| 36 | #include "prefs.h" |
| 37 | #include "prefs_customheader.h" |
| 38 | #include "prefs_common.h" |
| 39 | #include "prefs_account.h" |
| 40 | #include "mainwindow.h" |
| 41 | #include "foldersel.h" |
| 42 | #include "manage_window.h" |
| 43 | #include "customheader.h" |
| 44 | #include "folder.h" |
| 45 | #include "utils.h" |
| 46 | #include "gtkutils.h" |
| 47 | #include "alertpanel.h" |
| 48 | |
| 49 | static struct CustomHdr { |
| 50 | GtkWidget *window; |
| 51 | |
| 52 | GtkWidget *ok_btn; |
| 53 | GtkWidget *cancel_btn; |
| 54 | |
| 55 | GtkWidget *hdr_combo; |
| 56 | GtkWidget *hdr_entry; |
| 57 | GtkWidget *val_entry; |
| 58 | GtkWidget *customhdr_clist; |
| 59 | } customhdr; |
| 60 | |
| 61 | /* widget creating functions */
|
| 62 | static void prefs_custom_header_create (void); |
| 63 | |
| 64 | static void prefs_custom_header_set_dialog (PrefsAccount *ac); |
| 65 | static void prefs_custom_header_set_list (PrefsAccount *ac); |
| 66 | static gint prefs_custom_header_clist_set_row (PrefsAccount *ac,
|
| 67 | gint row); |
| 68 | |
| 69 | /* callback functions */
|
| 70 | static void prefs_custom_header_add_cb (void); |
| 71 | static void prefs_custom_header_delete_cb (void); |
| 72 | static void prefs_custom_header_up (void); |
| 73 | static void prefs_custom_header_down (void); |
| 74 | static void prefs_custom_header_select (GtkCList *clist, |
| 75 | gint row, |
| 76 | gint column, |
| 77 | GdkEvent *event); |
| 78 | |
| 79 | static void prefs_custom_header_row_moved (GtkCList *clist, |
| 80 | gint source_row, |
| 81 | gint dest_row, |
| 82 | gpointer data); |
| 83 | |
| 84 | static gboolean prefs_custom_header_key_pressed (GtkWidget *widget,
|
| 85 | GdkEventKey *event, |
| 86 | gpointer data); |
| 87 | static void prefs_custom_header_ok (void); |
| 88 | static void prefs_custom_header_cancel (void); |
| 89 | static gint prefs_custom_header_deleted (GtkWidget *widget,
|
| 90 | GdkEventAny *event, |
| 91 | gpointer data); |
| 92 | |
| 93 | static PrefsAccount *cur_ac = NULL; |
| 94 | |
| 95 | void prefs_custom_header_open(PrefsAccount *ac)
|
| 96 | {
|
| 97 | if (!customhdr.window) {
|
| 98 | prefs_custom_header_create(); |
| 99 | } |
| 100 | |
| 101 | manage_window_set_transient(GTK_WINDOW(customhdr.window)); |
| 102 | gtk_widget_grab_focus(customhdr.ok_btn); |
| 103 | |
| 104 | prefs_custom_header_set_dialog(ac); |
| 105 | |
| 106 | cur_ac = ac; |
| 107 | |
| 108 | gtk_widget_show(customhdr.window); |
| 109 | } |
| 110 | |
| 111 | static void prefs_custom_header_create(void) |
| 112 | {
|
| 113 | GtkWidget *window; |
| 114 | GtkWidget *vbox; |
| 115 | |
| 116 | GtkWidget *ok_btn; |
| 117 | GtkWidget *cancel_btn; |
| 118 | |
| 119 | GtkWidget *confirm_area; |
| 120 | |
| 121 | GtkWidget *vbox1; |
| 122 | |
| 123 | GtkWidget *table1; |
| 124 | GtkWidget *hdr_label; |
| 125 | GtkWidget *hdr_combo; |
| 126 | GtkWidget *val_label; |
| 127 | GtkWidget *val_entry; |
| 128 | |
| 129 | GtkWidget *reg_hbox; |
| 130 | GtkWidget *btn_hbox; |
| 131 | GtkWidget *arrow; |
| 132 | GtkWidget *add_btn; |
| 133 | GtkWidget *del_btn; |
| 134 | |
| 135 | GtkWidget *ch_hbox; |
| 136 | GtkWidget *ch_scrolledwin; |
| 137 | GtkWidget *customhdr_clist; |
| 138 | |
| 139 | GtkWidget *btn_vbox; |
| 140 | GtkWidget *up_btn; |
| 141 | GtkWidget *down_btn; |
| 142 | |
| 143 | gchar *title[1];
|
| 144 | |
| 145 | debug_print("Creating custom header setting window...\n");
|
| 146 | |
| 147 | window = gtk_window_new (GTK_WINDOW_TOPLEVEL); |
| 148 | gtk_container_set_border_width (GTK_CONTAINER (window), 8);
|
| 149 | gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER); |
| 150 | gtk_window_set_modal (GTK_WINDOW (window), TRUE); |
| 151 | gtk_window_set_policy (GTK_WINDOW (window), FALSE, TRUE, FALSE); |
| 152 | |
| 153 | vbox = gtk_vbox_new (FALSE, 6);
|
| 154 | gtk_widget_show (vbox); |
| 155 | gtk_container_add (GTK_CONTAINER (window), vbox); |
| 156 | |
| 157 | gtkut_stock_button_set_create(&confirm_area, &ok_btn, GTK_STOCK_OK, |
| 158 | &cancel_btn, GTK_STOCK_CANCEL, |
| 159 | NULL, NULL); |
| 160 | gtk_widget_show (confirm_area); |
| 161 | gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
|
| 162 | gtk_widget_grab_default (ok_btn); |
| 163 | |
| 164 | gtk_window_set_title (GTK_WINDOW(window), _("Custom header setting"));
|
| 165 | MANAGE_WINDOW_SIGNALS_CONNECT (window); |
| 166 | g_signal_connect (G_OBJECT(window), "delete_event",
|
| 167 | G_CALLBACK(prefs_custom_header_deleted), |
| 168 | NULL);
|
| 169 | g_signal_connect (G_OBJECT(window), "key_press_event",
|
| 170 | G_CALLBACK(prefs_custom_header_key_pressed), |
| 171 | NULL);
|
| 172 | g_signal_connect (G_OBJECT(ok_btn), "clicked",
|
| 173 | G_CALLBACK(prefs_custom_header_ok), NULL);
|
| 174 | g_signal_connect (G_OBJECT(cancel_btn), "clicked",
|
| 175 | G_CALLBACK(prefs_custom_header_cancel), NULL);
|
| 176 | |
| 177 | vbox1 = gtk_vbox_new (FALSE, VSPACING); |
| 178 | gtk_widget_show (vbox1); |
| 179 | gtk_box_pack_start (GTK_BOX (vbox), vbox1, TRUE, TRUE, 0);
|
| 180 | gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
|
| 181 | |
| 182 | table1 = gtk_table_new (2, 2, FALSE); |
| 183 | gtk_widget_show (table1); |
| 184 | gtk_box_pack_start (GTK_BOX (vbox1), table1, |
| 185 | FALSE, FALSE, 0);
|
| 186 | gtk_table_set_row_spacings (GTK_TABLE (table1), 8);
|
| 187 | gtk_table_set_col_spacings (GTK_TABLE (table1), 8);
|
| 188 | |
| 189 | hdr_label = gtk_label_new (_("Header"));
|
| 190 | gtk_widget_show (hdr_label); |
| 191 | gtk_table_attach (GTK_TABLE (table1), hdr_label, 0, 1, 0, 1, |
| 192 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
| 193 | 0, 0, 0); |
| 194 | gtk_misc_set_alignment (GTK_MISC (hdr_label), 0, 0.5); |
| 195 | |
| 196 | hdr_combo = gtk_combo_new (); |
| 197 | gtk_widget_show (hdr_combo); |
| 198 | gtk_table_attach (GTK_TABLE (table1), hdr_combo, 0, 1, 1, 2, |
| 199 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
| 200 | 0, 0, 0); |
| 201 | gtk_widget_set_size_request (hdr_combo, 150, -1); |
| 202 | gtkut_combo_set_items (GTK_COMBO (hdr_combo), |
| 203 | "User-Agent", "X-Face", "X-Operating-System", |
| 204 | NULL);
|
| 205 | |
| 206 | val_label = gtk_label_new (_("Value"));
|
| 207 | gtk_widget_show (val_label); |
| 208 | gtk_table_attach (GTK_TABLE (table1), val_label, 1, 2, 0, 1, |
| 209 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
| 210 | 0, 0, 0); |
| 211 | gtk_misc_set_alignment (GTK_MISC (val_label), 0, 0.5); |
| 212 | |
| 213 | val_entry = gtk_entry_new (); |
| 214 | gtk_widget_show (val_entry); |
| 215 | gtk_table_attach (GTK_TABLE (table1), val_entry, 1, 2, 1, 2, |
| 216 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
| 217 | 0, 0, 0); |
| 218 | gtk_widget_set_size_request (val_entry, 200, -1); |
| 219 | |
| 220 | /* add / delete */
|
| 221 | |
| 222 | reg_hbox = gtk_hbox_new (FALSE, 4);
|
| 223 | gtk_widget_show (reg_hbox); |
| 224 | gtk_box_pack_start (GTK_BOX (vbox1), reg_hbox, FALSE, FALSE, 0);
|
| 225 | |
| 226 | arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT); |
| 227 | gtk_widget_show (arrow); |
| 228 | gtk_box_pack_start (GTK_BOX (reg_hbox), arrow, FALSE, FALSE, 0);
|
| 229 | gtk_widget_set_size_request (arrow, -1, 16); |
| 230 | |
| 231 | btn_hbox = gtk_hbox_new (TRUE, 4);
|
| 232 | gtk_widget_show (btn_hbox); |
| 233 | gtk_box_pack_start (GTK_BOX (reg_hbox), btn_hbox, FALSE, FALSE, 0);
|
| 234 | |
| 235 | add_btn = gtk_button_new_with_label (_("Add"));
|
| 236 | gtk_widget_show (add_btn); |
| 237 | gtk_box_pack_start (GTK_BOX (btn_hbox), add_btn, FALSE, TRUE, 0);
|
| 238 | g_signal_connect (G_OBJECT (add_btn), "clicked",
|
| 239 | G_CALLBACK (prefs_custom_header_add_cb), NULL);
|
| 240 | |
| 241 | del_btn = gtk_button_new_with_label (_(" Delete "));
|
| 242 | gtk_widget_show (del_btn); |
| 243 | gtk_box_pack_start (GTK_BOX (btn_hbox), del_btn, FALSE, TRUE, 0);
|
| 244 | g_signal_connect (G_OBJECT (del_btn), "clicked",
|
| 245 | G_CALLBACK (prefs_custom_header_delete_cb), NULL);
|
| 246 | |
| 247 | |
| 248 | ch_hbox = gtk_hbox_new (FALSE, 8);
|
| 249 | gtk_widget_show (ch_hbox); |
| 250 | gtk_box_pack_start (GTK_BOX (vbox1), ch_hbox, TRUE, TRUE, 0);
|
| 251 | |
| 252 | ch_scrolledwin = gtk_scrolled_window_new (NULL, NULL); |
| 253 | gtk_widget_set_size_request (ch_scrolledwin, -1, 200); |
| 254 | gtk_widget_show (ch_scrolledwin); |
| 255 | gtk_box_pack_start (GTK_BOX (ch_hbox), ch_scrolledwin, TRUE, TRUE, 0);
|
| 256 | gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (ch_scrolledwin), |
| 257 | GTK_POLICY_AUTOMATIC, |
| 258 | GTK_POLICY_AUTOMATIC); |
| 259 | |
| 260 | title[0] = _("Custom headers"); |
| 261 | customhdr_clist = gtk_clist_new_with_titles(1, title);
|
| 262 | gtk_widget_show (customhdr_clist); |
| 263 | gtk_container_add (GTK_CONTAINER (ch_scrolledwin), customhdr_clist); |
| 264 | gtk_clist_set_column_width (GTK_CLIST (customhdr_clist), 0, 80); |
| 265 | gtk_clist_set_selection_mode (GTK_CLIST (customhdr_clist), |
| 266 | GTK_SELECTION_BROWSE); |
| 267 | gtk_clist_set_reorderable (GTK_CLIST (customhdr_clist), TRUE); |
| 268 | gtk_clist_set_use_drag_icons (GTK_CLIST (customhdr_clist), FALSE); |
| 269 | GTK_WIDGET_UNSET_FLAGS (GTK_CLIST (customhdr_clist)->column[0].button,
|
| 270 | GTK_CAN_FOCUS); |
| 271 | g_signal_connect (G_OBJECT (customhdr_clist), "select_row",
|
| 272 | G_CALLBACK (prefs_custom_header_select), NULL);
|
| 273 | g_signal_connect_after |
| 274 | (G_OBJECT (customhdr_clist), "row_move",
|
| 275 | G_CALLBACK (prefs_custom_header_row_moved), NULL);
|
| 276 | |
| 277 | btn_vbox = gtk_vbox_new (FALSE, 8);
|
| 278 | gtk_widget_show (btn_vbox); |
| 279 | gtk_box_pack_start (GTK_BOX (ch_hbox), btn_vbox, FALSE, FALSE, 0);
|
| 280 | |
| 281 | up_btn = gtk_button_new_with_label (_("Up"));
|
| 282 | gtk_widget_show (up_btn); |
| 283 | gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
|
| 284 | g_signal_connect (G_OBJECT (up_btn), "clicked",
|
| 285 | G_CALLBACK (prefs_custom_header_up), NULL);
|
| 286 | |
| 287 | down_btn = gtk_button_new_with_label (_("Down"));
|
| 288 | gtk_widget_show (down_btn); |
| 289 | gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
|
| 290 | g_signal_connect (G_OBJECT (down_btn), "clicked",
|
| 291 | G_CALLBACK (prefs_custom_header_down), NULL);
|
| 292 | |
| 293 | gtk_widget_show_all(window); |
| 294 | |
| 295 | customhdr.window = window; |
| 296 | customhdr.ok_btn = ok_btn; |
| 297 | customhdr.cancel_btn = cancel_btn; |
| 298 | |
| 299 | customhdr.hdr_combo = hdr_combo; |
| 300 | customhdr.hdr_entry = GTK_COMBO (hdr_combo)->entry; |
| 301 | customhdr.val_entry = val_entry; |
| 302 | |
| 303 | customhdr.customhdr_clist = customhdr_clist; |
| 304 | } |
| 305 | |
| 306 | void prefs_custom_header_read_config(PrefsAccount *ac)
|
| 307 | {
|
| 308 | gchar *rcpath; |
| 309 | FILE *fp; |
| 310 | gchar buf[PREFSBUFSIZE]; |
| 311 | CustomHeader *ch; |
| 312 | |
| 313 | debug_print("Reading custom header configuration...\n");
|
| 314 | |
| 315 | rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, |
| 316 | CUSTOM_HEADER_RC, NULL);
|
| 317 | if ((fp = g_fopen(rcpath, "rb")) == NULL) { |
| 318 | if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen"); |
| 319 | g_free(rcpath); |
| 320 | ac->customhdr_list = NULL;
|
| 321 | return;
|
| 322 | } |
| 323 | g_free(rcpath); |
| 324 | |
| 325 | /* remove all previous headers list */
|
| 326 | while (ac->customhdr_list != NULL) { |
| 327 | ch = (CustomHeader *)ac->customhdr_list->data; |
| 328 | custom_header_free(ch); |
| 329 | ac->customhdr_list = g_slist_remove(ac->customhdr_list, ch); |
| 330 | } |
| 331 | |
| 332 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 333 | ch = custom_header_read_str(buf); |
| 334 | if (ch) {
|
| 335 | if (ch->account_id == ac->account_id) {
|
| 336 | ac->customhdr_list = |
| 337 | g_slist_append(ac->customhdr_list, ch); |
| 338 | } else
|
| 339 | custom_header_free(ch); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | fclose(fp); |
| 344 | } |
| 345 | |
| 346 | void prefs_custom_header_write_config(PrefsAccount *ac)
|
| 347 | {
|
| 348 | gchar *rcpath; |
| 349 | PrefFile *pfile; |
| 350 | GSList *cur; |
| 351 | gchar buf[PREFSBUFSIZE]; |
| 352 | FILE * fp; |
| 353 | CustomHeader *ch; |
| 354 | |
| 355 | GSList *all_hdrs = NULL;
|
| 356 | |
| 357 | debug_print("Writing custom header configuration...\n");
|
| 358 | |
| 359 | rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, |
| 360 | CUSTOM_HEADER_RC, NULL);
|
| 361 | |
| 362 | if ((fp = g_fopen(rcpath, "rb")) == NULL) { |
| 363 | if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen"); |
| 364 | } else {
|
| 365 | all_hdrs = NULL;
|
| 366 | |
| 367 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 368 | ch = custom_header_read_str(buf); |
| 369 | if (ch) {
|
| 370 | if (ch->account_id != ac->account_id)
|
| 371 | all_hdrs = |
| 372 | g_slist_append(all_hdrs, ch); |
| 373 | else
|
| 374 | custom_header_free(ch); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | fclose(fp); |
| 379 | } |
| 380 | |
| 381 | if ((pfile = prefs_file_open(rcpath)) == NULL) { |
| 382 | g_warning(_("failed to write configuration to file\n"));
|
| 383 | g_free(rcpath); |
| 384 | return;
|
| 385 | } |
| 386 | |
| 387 | for (cur = all_hdrs; cur != NULL; cur = cur->next) { |
| 388 | CustomHeader *hdr = (CustomHeader *)cur->data; |
| 389 | gchar *chstr; |
| 390 | |
| 391 | chstr = custom_header_get_str(hdr); |
| 392 | if (fputs(chstr, pfile->fp) == EOF || |
| 393 | fputc('\n', pfile->fp) == EOF) { |
| 394 | FILE_OP_ERROR(rcpath, "fputs || fputc");
|
| 395 | prefs_file_close_revert(pfile); |
| 396 | g_free(rcpath); |
| 397 | g_free(chstr); |
| 398 | return;
|
| 399 | } |
| 400 | g_free(chstr); |
| 401 | } |
| 402 | |
| 403 | for (cur = ac->customhdr_list; cur != NULL; cur = cur->next) { |
| 404 | CustomHeader *hdr = (CustomHeader *)cur->data; |
| 405 | gchar *chstr; |
| 406 | |
| 407 | chstr = custom_header_get_str(hdr); |
| 408 | if (fputs(chstr, pfile->fp) == EOF || |
| 409 | fputc('\n', pfile->fp) == EOF) { |
| 410 | FILE_OP_ERROR(rcpath, "fputs || fputc");
|
| 411 | prefs_file_close_revert(pfile); |
| 412 | g_free(rcpath); |
| 413 | g_free(chstr); |
| 414 | return;
|
| 415 | } |
| 416 | g_free(chstr); |
| 417 | } |
| 418 | |
| 419 | g_free(rcpath); |
| 420 | |
| 421 | while (all_hdrs != NULL) { |
| 422 | ch = (CustomHeader *)all_hdrs->data; |
| 423 | custom_header_free(ch); |
| 424 | all_hdrs = g_slist_remove(all_hdrs, ch); |
| 425 | } |
| 426 | |
| 427 | if (prefs_file_close(pfile) < 0) { |
| 428 | g_warning(_("failed to write configuration to file\n"));
|
| 429 | return;
|
| 430 | } |
| 431 | } |
| 432 | |
| 433 | static void prefs_custom_header_set_dialog(PrefsAccount *ac) |
| 434 | {
|
| 435 | GtkCList *clist = GTK_CLIST(customhdr.customhdr_clist); |
| 436 | GSList *cur; |
| 437 | gchar *ch_str[1];
|
| 438 | gint row; |
| 439 | |
| 440 | gtk_clist_freeze(clist); |
| 441 | gtk_clist_clear(clist); |
| 442 | |
| 443 | for (cur = ac->customhdr_list; cur != NULL; cur = cur->next) { |
| 444 | CustomHeader *ch = (CustomHeader *)cur->data; |
| 445 | |
| 446 | ch_str[0] = g_strdup_printf("%s: %s", ch->name, |
| 447 | ch->value ? ch->value : "");
|
| 448 | row = gtk_clist_append(clist, ch_str); |
| 449 | gtk_clist_set_row_data(clist, row, ch); |
| 450 | |
| 451 | g_free(ch_str[0]);
|
| 452 | } |
| 453 | |
| 454 | gtk_clist_thaw(clist); |
| 455 | } |
| 456 | |
| 457 | static void prefs_custom_header_set_list(PrefsAccount *ac) |
| 458 | {
|
| 459 | gint row = 0;
|
| 460 | CustomHeader *ch; |
| 461 | |
| 462 | g_slist_free(ac->customhdr_list); |
| 463 | ac->customhdr_list = NULL;
|
| 464 | |
| 465 | while ((ch = gtk_clist_get_row_data
|
| 466 | (GTK_CLIST(customhdr.customhdr_clist), row)) != NULL) {
|
| 467 | ac->customhdr_list = g_slist_append(ac->customhdr_list, ch); |
| 468 | row++; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | static gint prefs_custom_header_clist_set_row(PrefsAccount *ac, gint row)
|
| 473 | {
|
| 474 | GtkCList *clist = GTK_CLIST(customhdr.customhdr_clist); |
| 475 | CustomHeader *ch; |
| 476 | const gchar *entry_text;
|
| 477 | gchar *ch_str[1];
|
| 478 | |
| 479 | entry_text = gtk_entry_get_text(GTK_ENTRY(customhdr.hdr_entry)); |
| 480 | if (entry_text[0] == '\0') { |
| 481 | alertpanel_error(_("Header name is not set."));
|
| 482 | return -1; |
| 483 | } |
| 484 | |
| 485 | ch = g_new0(CustomHeader, 1);
|
| 486 | |
| 487 | ch->account_id = ac->account_id; |
| 488 | |
| 489 | ch->name = g_strdup(entry_text); |
| 490 | unfold_line(ch->name); |
| 491 | g_strstrip(ch->name); |
| 492 | gtk_entry_set_text(GTK_ENTRY(customhdr.hdr_entry), ch->name); |
| 493 | |
| 494 | entry_text = gtk_entry_get_text(GTK_ENTRY(customhdr.val_entry)); |
| 495 | if (entry_text[0] != '\0') { |
| 496 | ch->value = g_strdup(entry_text); |
| 497 | unfold_line(ch->value); |
| 498 | g_strstrip(ch->value); |
| 499 | gtk_entry_set_text(GTK_ENTRY(customhdr.val_entry), ch->value); |
| 500 | } |
| 501 | |
| 502 | ch_str[0] = g_strdup_printf("%s: %s", ch->name, |
| 503 | ch->value ? ch->value : "");
|
| 504 | |
| 505 | if (row < 0) |
| 506 | row = gtk_clist_append(clist, ch_str); |
| 507 | else {
|
| 508 | CustomHeader *tmp_ch; |
| 509 | |
| 510 | gtk_clist_set_text(clist, row, 0, ch_str[0]); |
| 511 | tmp_ch = gtk_clist_get_row_data(clist, row); |
| 512 | if (tmp_ch)
|
| 513 | custom_header_free(tmp_ch); |
| 514 | } |
| 515 | |
| 516 | gtk_clist_set_row_data(clist, row, ch); |
| 517 | |
| 518 | g_free(ch_str[0]);
|
| 519 | |
| 520 | prefs_custom_header_set_list(cur_ac); |
| 521 | |
| 522 | return row;
|
| 523 | } |
| 524 | |
| 525 | static void prefs_custom_header_add_cb(void) |
| 526 | {
|
| 527 | prefs_custom_header_clist_set_row(cur_ac, -1);
|
| 528 | } |
| 529 | |
| 530 | static void prefs_custom_header_delete_cb(void) |
| 531 | {
|
| 532 | GtkCList *clist = GTK_CLIST(customhdr.customhdr_clist); |
| 533 | CustomHeader *ch; |
| 534 | gint row; |
| 535 | |
| 536 | if (!clist->selection) return; |
| 537 | row = GPOINTER_TO_INT(clist->selection->data); |
| 538 | |
| 539 | if (alertpanel(_("Delete header"), |
| 540 | _("Do you really want to delete this header?"),
|
| 541 | GTK_STOCK_YES, GTK_STOCK_NO, NULL) != G_ALERTDEFAULT)
|
| 542 | return;
|
| 543 | |
| 544 | ch = gtk_clist_get_row_data(clist, row); |
| 545 | custom_header_free(ch); |
| 546 | gtk_clist_remove(clist, row); |
| 547 | cur_ac->customhdr_list = g_slist_remove(cur_ac->customhdr_list, ch); |
| 548 | } |
| 549 | |
| 550 | static void prefs_custom_header_up(void) |
| 551 | {
|
| 552 | GtkCList *clist = GTK_CLIST(customhdr.customhdr_clist); |
| 553 | gint row; |
| 554 | |
| 555 | if (!clist->selection) return; |
| 556 | |
| 557 | row = GPOINTER_TO_INT(clist->selection->data); |
| 558 | if (row > 0) |
| 559 | gtk_clist_row_move(clist, row, row - 1);
|
| 560 | } |
| 561 | |
| 562 | static void prefs_custom_header_down(void) |
| 563 | {
|
| 564 | GtkCList *clist = GTK_CLIST(customhdr.customhdr_clist); |
| 565 | gint row; |
| 566 | |
| 567 | if (!clist->selection) return; |
| 568 | |
| 569 | row = GPOINTER_TO_INT(clist->selection->data); |
| 570 | if (row >= 0 && row < clist->rows - 1) |
| 571 | gtk_clist_row_move(clist, row, row + 1);
|
| 572 | } |
| 573 | |
| 574 | #define ENTRY_SET_TEXT(entry, str) \
|
| 575 | gtk_entry_set_text(GTK_ENTRY(entry), str ? str : "")
|
| 576 | |
| 577 | static void prefs_custom_header_select(GtkCList *clist, gint row, gint column, |
| 578 | GdkEvent *event) |
| 579 | {
|
| 580 | CustomHeader *ch; |
| 581 | CustomHeader default_ch = { 0, "", NULL };
|
| 582 | |
| 583 | ch = gtk_clist_get_row_data(clist, row); |
| 584 | if (!ch) ch = &default_ch;
|
| 585 | |
| 586 | ENTRY_SET_TEXT(customhdr.hdr_entry, ch->name); |
| 587 | ENTRY_SET_TEXT(customhdr.val_entry, ch->value); |
| 588 | } |
| 589 | |
| 590 | #undef ENTRY_SET_TEXT
|
| 591 | |
| 592 | static void prefs_custom_header_row_moved(GtkCList *clist, gint source_row, |
| 593 | gint dest_row, gpointer data) |
| 594 | {
|
| 595 | prefs_custom_header_set_list(cur_ac); |
| 596 | } |
| 597 | |
| 598 | static gboolean prefs_custom_header_key_pressed(GtkWidget *widget,
|
| 599 | GdkEventKey *event, |
| 600 | gpointer data) |
| 601 | {
|
| 602 | if (event && event->keyval == GDK_Escape)
|
| 603 | prefs_custom_header_cancel(); |
| 604 | return FALSE;
|
| 605 | } |
| 606 | |
| 607 | static void prefs_custom_header_ok(void) |
| 608 | {
|
| 609 | prefs_custom_header_write_config(cur_ac); |
| 610 | gtk_widget_hide(customhdr.window); |
| 611 | } |
| 612 | |
| 613 | static void prefs_custom_header_cancel(void) |
| 614 | {
|
| 615 | prefs_custom_header_read_config(cur_ac); |
| 616 | gtk_widget_hide(customhdr.window); |
| 617 | } |
| 618 | |
| 619 | static gint prefs_custom_header_deleted(GtkWidget *widget, GdkEventAny *event,
|
| 620 | gpointer data) |
| 621 | {
|
| 622 | prefs_custom_header_cancel(); |
| 623 | return TRUE;
|
| 624 | } |