e3b4da01856d86f80cfa400e85bc88935dc9bc81
[claws.git] / src / prefs_customheader.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <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_gtk.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 #include "base64.h"
49 #include "filesel.h"
50 #include "combobox.h"
51
52 enum {
53         CUSTHDR_STRING,         /*!< display string managed by list store */
54         CUSTHDR_DATA,           /*!< string managed by us */
55         N_CUSTHDR_COLUMNS
56 };
57
58 static struct CustomHdr {
59         GtkWidget *window;
60
61         GtkWidget *ok_btn;
62         GtkWidget *cancel_btn;
63
64         GtkWidget *hdr_combo;
65         GtkWidget *hdr_entry;
66         GtkWidget *val_entry;
67         GtkWidget *list_view;
68 } customhdr;
69
70 /* widget creating functions */
71 static void prefs_custom_header_create  (void);
72
73 static void prefs_custom_header_set_dialog              (PrefsAccount *ac);
74 static void prefs_custom_header_set_list                (PrefsAccount *ac);
75 static void prefs_custom_header_list_view_set_row       (PrefsAccount *ac);
76
77 /* callback functions */
78 static void prefs_custom_header_add_cb          (void);
79 static void prefs_custom_header_val_from_file_cb(void);
80 static void prefs_custom_header_delete_cb       (void);
81 static void prefs_custom_header_up              (void);
82 static void prefs_custom_header_down            (void);
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 GtkListStore* prefs_custom_header_create_data_store      (void);
94
95 static void prefs_custom_header_list_view_insert_header (GtkWidget *list_view,
96                                                          GtkTreeIter *row_iter,
97                                                          gchar *header,
98                                                          gpointer data);
99
100 static GtkWidget *prefs_custom_header_list_view_create (void);
101
102 static void prefs_custom_header_create_list_view_columns        (GtkWidget *list_view);
103
104 static gboolean prefs_custom_header_selected    (GtkTreeSelection *selector,
105                                                  GtkTreeModel *model, 
106                                                  GtkTreePath *path,
107                                                  gboolean currently_selected,
108                                                  gpointer data);
109
110
111 static PrefsAccount *cur_ac = NULL;
112
113 void prefs_custom_header_open(PrefsAccount *ac)
114 {
115         if (!customhdr.window) {
116                 prefs_custom_header_create();
117         }
118
119         manage_window_set_transient(GTK_WINDOW(customhdr.window));
120         gtk_widget_grab_focus(customhdr.ok_btn);
121
122         prefs_custom_header_set_dialog(ac);
123
124         cur_ac = ac;
125
126         gtk_widget_show(customhdr.window);
127 }
128
129 static void prefs_custom_header_create(void)
130 {
131         GtkWidget *window;
132         GtkWidget *vbox;
133
134         GtkWidget *ok_btn;
135         GtkWidget *cancel_btn;
136
137         GtkWidget *confirm_area;
138
139         GtkWidget *vbox1;
140
141         GtkWidget *table1;
142         GtkWidget *hdr_label;
143         GtkWidget *hdr_combo;
144         GtkWidget *val_label;
145         GtkWidget *val_entry;
146         GtkWidget *val_btn;
147
148         GtkWidget *reg_hbox;
149         GtkWidget *btn_hbox;
150         GtkWidget *arrow;
151         GtkWidget *add_btn;
152         GtkWidget *del_btn;
153
154         GtkWidget *ch_hbox;
155         GtkWidget *ch_scrolledwin;
156         GtkWidget *list_view;
157
158         GtkWidget *btn_vbox;
159         GtkWidget *up_btn;
160         GtkWidget *down_btn;
161
162         debug_print("Creating custom header setting window...\n");
163
164         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_customheader");
165         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
166         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
167         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
168         gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
169
170         vbox = gtk_vbox_new (FALSE, 6);
171         gtk_widget_show (vbox);
172         gtk_container_add (GTK_CONTAINER (window), vbox);
173
174         gtkut_stock_button_set_create(&confirm_area, &cancel_btn, GTK_STOCK_CANCEL,
175                                       &ok_btn, GTK_STOCK_OK,
176                                       NULL, NULL);
177         gtk_widget_show (confirm_area);
178         gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
179         gtk_widget_grab_default (ok_btn);
180
181         gtk_window_set_title (GTK_WINDOW(window), _("Custom header configuration"));
182         MANAGE_WINDOW_SIGNALS_CONNECT (window);
183         g_signal_connect (G_OBJECT(window), "delete_event",
184                           G_CALLBACK(prefs_custom_header_deleted),
185                           NULL);
186         g_signal_connect (G_OBJECT(window), "key_press_event",
187                           G_CALLBACK(prefs_custom_header_key_pressed),
188                           NULL);
189         g_signal_connect (G_OBJECT(ok_btn), "clicked",
190                           G_CALLBACK(prefs_custom_header_ok), NULL);
191         g_signal_connect (G_OBJECT(cancel_btn), "clicked",
192                           G_CALLBACK(prefs_custom_header_cancel), NULL);
193
194         vbox1 = gtk_vbox_new (FALSE, VSPACING);
195         gtk_widget_show (vbox1);
196         gtk_box_pack_start (GTK_BOX (vbox), vbox1, TRUE, TRUE, 0);
197         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
198
199         table1 = gtk_table_new (3, 2, FALSE);
200         gtk_widget_show (table1);
201         gtk_box_pack_start (GTK_BOX (vbox1), table1,
202                             FALSE, FALSE, 0);
203         gtk_table_set_row_spacings (GTK_TABLE (table1), 8);
204         gtk_table_set_col_spacings (GTK_TABLE (table1), 8);
205
206         hdr_label = gtk_label_new (_("Header"));
207         gtk_widget_show (hdr_label);
208         gtk_table_attach (GTK_TABLE (table1), hdr_label, 0, 1, 0, 1,
209                           GTK_EXPAND | GTK_SHRINK | GTK_FILL,
210                           0, 0, 0);
211         gtk_misc_set_alignment (GTK_MISC (hdr_label), 0, 0.5);
212         
213         hdr_combo = combobox_text_new(TRUE, "User-Agent", "Face", "X-Face",
214                                       "X-Operating-System", NULL);
215         gtk_table_attach (GTK_TABLE (table1), hdr_combo, 0, 1, 1, 2,
216                           GTK_EXPAND | GTK_SHRINK | GTK_FILL,
217                           0, 0, 0);
218         gtk_widget_set_size_request (hdr_combo, 150, -1);
219
220         val_label = gtk_label_new (_("Value"));
221         gtk_widget_show (val_label);
222         gtk_table_attach (GTK_TABLE (table1), val_label, 1, 2, 0, 1,
223                           GTK_EXPAND | GTK_SHRINK | GTK_FILL,
224                           0, 0, 0);
225         gtk_misc_set_alignment (GTK_MISC (val_label), 0, 0.5);
226         
227         val_entry = gtk_entry_new ();
228         gtk_widget_show (val_entry);
229         gtk_table_attach (GTK_TABLE (table1), val_entry, 1, 2, 1, 2,
230                           GTK_EXPAND | GTK_SHRINK | GTK_FILL,
231                           0, 0, 0);
232         gtk_widget_set_size_request (val_entry, 200, -1);
233
234         val_btn = gtk_button_new_with_label (_("From file..."));
235         gtk_widget_show (val_btn);
236         gtk_table_attach (GTK_TABLE (table1), val_btn, 2, 3, 1, 2,
237                           GTK_EXPAND | GTK_SHRINK | GTK_FILL,
238                           0, 0, 0);
239         g_signal_connect (G_OBJECT (val_btn), "clicked",
240                           G_CALLBACK (prefs_custom_header_val_from_file_cb),
241                           NULL);
242
243         /* add / delete */
244
245         reg_hbox = gtk_hbox_new (FALSE, 4);
246         gtk_widget_show (reg_hbox);
247         gtk_box_pack_start (GTK_BOX (vbox1), reg_hbox, FALSE, FALSE, 0);
248
249         arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
250         gtk_widget_show (arrow);
251         gtk_box_pack_start (GTK_BOX (reg_hbox), arrow, FALSE, FALSE, 0);
252         gtk_widget_set_size_request (arrow, -1, 16);
253
254         btn_hbox = gtk_hbox_new (TRUE, 4);
255         gtk_widget_show (btn_hbox);
256         gtk_box_pack_start (GTK_BOX (reg_hbox), btn_hbox, FALSE, FALSE, 0);
257
258         add_btn = gtk_button_new_from_stock (GTK_STOCK_ADD);
259         gtk_widget_show (add_btn);
260         gtk_box_pack_start (GTK_BOX (btn_hbox), add_btn, FALSE, TRUE, 0);
261         g_signal_connect (G_OBJECT (add_btn), "clicked",
262                           G_CALLBACK (prefs_custom_header_add_cb),
263                           NULL);
264
265         del_btn = gtk_button_new_from_stock (GTK_STOCK_DELETE);
266         gtk_widget_show (del_btn);
267         gtk_box_pack_start (GTK_BOX (btn_hbox), del_btn, FALSE, TRUE, 0);
268         g_signal_connect (G_OBJECT (del_btn), "clicked",
269                           G_CALLBACK (prefs_custom_header_delete_cb),
270                           NULL);
271
272
273         ch_hbox = gtk_hbox_new (FALSE, 8);
274         gtk_widget_show (ch_hbox);
275         gtk_box_pack_start (GTK_BOX (vbox1), ch_hbox, TRUE, TRUE, 0);
276
277         ch_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
278         gtk_widget_set_size_request (ch_scrolledwin, -1, 200);
279         gtk_widget_show (ch_scrolledwin);
280         gtk_box_pack_start (GTK_BOX (ch_hbox), ch_scrolledwin, TRUE, TRUE, 0);
281         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (ch_scrolledwin),
282                                         GTK_POLICY_AUTOMATIC,
283                                         GTK_POLICY_AUTOMATIC);
284
285         list_view = prefs_custom_header_list_view_create();
286         gtk_widget_show (list_view);
287         gtk_container_add (GTK_CONTAINER (ch_scrolledwin), list_view);
288
289         btn_vbox = gtk_vbox_new (FALSE, 8);
290         gtk_widget_show (btn_vbox);
291         gtk_box_pack_start (GTK_BOX (ch_hbox), btn_vbox, FALSE, FALSE, 0);
292
293         up_btn = gtk_button_new_from_stock (GTK_STOCK_GO_UP);
294         gtk_widget_show (up_btn);
295         gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
296         g_signal_connect (G_OBJECT (up_btn), "clicked",
297                           G_CALLBACK (prefs_custom_header_up), NULL);
298
299         down_btn = gtk_button_new_from_stock (GTK_STOCK_GO_DOWN);
300         gtk_widget_show (down_btn);
301         gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
302         g_signal_connect (G_OBJECT (down_btn), "clicked",
303                           G_CALLBACK (prefs_custom_header_down), NULL);
304
305         gtk_widget_show_all(window);
306
307         customhdr.window     = window;
308         customhdr.ok_btn     = ok_btn;
309         customhdr.cancel_btn = cancel_btn;
310
311         customhdr.hdr_combo  = hdr_combo;
312         customhdr.hdr_entry  = GTK_BIN (hdr_combo)->child;
313         customhdr.val_entry  = val_entry;
314
315         customhdr.list_view   = list_view;
316 }
317
318 void prefs_custom_header_read_config(PrefsAccount *ac)
319 {
320         gchar *rcpath;
321         FILE *fp;
322         gchar buf[PREFSBUFSIZE];
323         CustomHeader *ch;
324
325         debug_print("Reading custom header configuration...\n");
326
327         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
328                              CUSTOM_HEADER_RC, NULL);
329         if ((fp = g_fopen(rcpath, "rb")) == NULL) {
330                 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
331                 g_free(rcpath);
332                 ac->customhdr_list = NULL;
333                 return;
334         }
335         g_free(rcpath);
336
337         /* remove all previous headers list */
338         while (ac->customhdr_list != NULL) {
339                 ch = (CustomHeader *)ac->customhdr_list->data;
340                 ac->customhdr_list = g_slist_remove(ac->customhdr_list, ch);
341                 custom_header_free(ch);
342         }
343
344         while (fgets(buf, sizeof(buf), fp) != NULL) {
345                 ch = custom_header_read_str(buf);
346                 if (ch) {
347                         if (ch->account_id == ac->account_id) {
348                                 ac->customhdr_list =
349                                         g_slist_append(ac->customhdr_list, ch);
350                         } else
351                                 custom_header_free(ch);
352                 }
353         }
354
355         fclose(fp);
356 }
357
358 static void prefs_custom_header_write_config(PrefsAccount *ac)
359 {
360         gchar *rcpath;
361         PrefFile *pfile;
362         GSList *cur;
363         gchar buf[PREFSBUFSIZE];
364         FILE * fp;
365         CustomHeader *ch;
366
367         GSList *all_hdrs = NULL;
368
369         debug_print("Writing custom header configuration...\n");
370
371         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
372                              CUSTOM_HEADER_RC, NULL);
373
374         if ((fp = g_fopen(rcpath, "rb")) == NULL) {
375                 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
376         } else {
377                 all_hdrs = NULL;
378
379                 while (fgets(buf, sizeof(buf), fp) != NULL) {
380                         ch = custom_header_read_str(buf);
381                         if (ch) {
382                                 if (ch->account_id != ac->account_id)
383                                         all_hdrs =
384                                                 g_slist_append(all_hdrs, ch);
385                                 else
386                                         custom_header_free(ch);
387                         }
388                 }
389
390                 fclose(fp);
391         }
392
393         if ((pfile = prefs_write_open(rcpath)) == NULL) {
394                 g_warning("failed to write configuration to file\n");
395                 g_free(rcpath);
396                 return;
397         }
398
399         for (cur = all_hdrs; cur != NULL; cur = cur->next) {
400                 CustomHeader *hdr = (CustomHeader *)cur->data;
401                 gchar *chstr;
402
403                 chstr = custom_header_get_str(hdr);
404                 if (fputs(chstr, pfile->fp) == EOF ||
405                     fputc('\n', pfile->fp) == EOF) {
406                         FILE_OP_ERROR(rcpath, "fputs || fputc");
407                         prefs_file_close_revert(pfile);
408                         g_free(rcpath);
409                         g_free(chstr);
410                         return;
411                 }
412                 g_free(chstr);
413         }
414
415         for (cur = ac->customhdr_list; cur != NULL; cur = cur->next) {
416                 CustomHeader *hdr = (CustomHeader *)cur->data;
417                 gchar *chstr;
418
419                 chstr = custom_header_get_str(hdr);
420                 if (fputs(chstr, pfile->fp) == EOF ||
421                     fputc('\n', pfile->fp) == EOF) {
422                         FILE_OP_ERROR(rcpath, "fputs || fputc");
423                         prefs_file_close_revert(pfile);
424                         g_free(rcpath);
425                         g_free(chstr);
426                         return;
427                 }
428                 g_free(chstr);
429         }
430
431         g_free(rcpath);
432
433         while (all_hdrs != NULL) {
434                 ch = (CustomHeader *)all_hdrs->data;
435                 all_hdrs = g_slist_remove(all_hdrs, ch);
436                 custom_header_free(ch);
437         }
438
439         if (prefs_file_close(pfile) < 0) {
440                 g_warning("failed to write configuration to file\n");
441                 return;
442         }
443 }
444
445 static void prefs_custom_header_set_dialog(PrefsAccount *ac)
446 {
447         GtkListStore *store;
448         GSList *cur;
449         
450         store = GTK_LIST_STORE(gtk_tree_view_get_model
451                                 (GTK_TREE_VIEW(customhdr.list_view)));
452         gtk_list_store_clear(store);
453
454         for (cur = ac->customhdr_list; cur != NULL; cur = cur->next) {
455                 CustomHeader *ch = (CustomHeader *)cur->data;
456                 gchar *ch_str;
457
458                 ch_str = g_strdup_printf("%s: %s", ch->name,
459                                          ch->value ? ch->value : "");
460
461                 prefs_custom_header_list_view_insert_header
462                         (customhdr.list_view, NULL, ch_str, ch);                                                 
463
464                 g_free(ch_str);
465         }
466 }
467
468 static void prefs_custom_header_set_list(PrefsAccount *ac)
469 {
470         CustomHeader *ch;
471         GtkTreeIter iter;
472         GtkListStore *store;
473
474         g_slist_free(ac->customhdr_list);
475         ac->customhdr_list = NULL;
476
477         store = GTK_LIST_STORE(gtk_tree_view_get_model
478                                 (GTK_TREE_VIEW(customhdr.list_view)));
479
480         if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter)) {
481                 do {
482                         gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
483                                            CUSTHDR_DATA, &ch,
484                                            -1);
485                         ac->customhdr_list = g_slist_append(ac->customhdr_list, ch);
486                 } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(store),
487                                                   &iter));
488         }
489 }
490
491 static void prefs_custom_header_list_view_set_row(PrefsAccount *ac)
492 {
493         CustomHeader *ch;
494         const gchar *entry_text;
495         gchar *ch_str;
496         GtkListStore *store;
497
498         store = GTK_LIST_STORE(gtk_tree_view_get_model
499                                 (GTK_TREE_VIEW(customhdr.list_view)));
500
501         entry_text = gtk_entry_get_text(GTK_ENTRY(customhdr.hdr_entry));
502         if (entry_text[0] == '\0') {
503                 alertpanel_error(_("Header name is not set."));
504                 return;
505         }
506         
507         while (*entry_text && 
508                (*entry_text == '\n' || *entry_text == '\r' || 
509                 *entry_text == '\t' || *entry_text == ' '))
510                 entry_text++;
511         
512         if (!custom_header_is_allowed(entry_text)) {
513                 alertpanel_error(_("This Header name is not allowed as a custom header."));
514                 return;
515         }
516
517         ch = g_new0(CustomHeader, 1);
518
519         ch->account_id = ac->account_id;
520
521         ch->name = g_strdup(entry_text);
522         unfold_line(ch->name);
523         g_strstrip(ch->name);
524         gtk_entry_set_text(GTK_ENTRY(customhdr.hdr_entry), ch->name);
525
526         entry_text = gtk_entry_get_text(GTK_ENTRY(customhdr.val_entry));
527         while (*entry_text && 
528                (*entry_text == '\n' || *entry_text == '\r' || 
529                 *entry_text == '\t' || *entry_text == ' '))
530                 entry_text++;
531         
532         if (entry_text[0] != '\0') {
533                 ch->value = g_strdup(entry_text);
534                 unfold_line(ch->value);
535                 g_strstrip(ch->value);
536                 gtk_entry_set_text(GTK_ENTRY(customhdr.val_entry), ch->value);
537         }
538
539         ch_str = g_strdup_printf("%s: %s", ch->name,
540                                  ch->value ? ch->value : "");
541         
542         prefs_custom_header_list_view_insert_header
543                 (customhdr.list_view, NULL, ch_str, ch);
544         
545         g_free(ch_str);
546
547         prefs_custom_header_set_list(cur_ac);
548
549 }
550
551 #define B64_LINE_SIZE           57
552 #define B64_BUFFSIZE            77
553 static void prefs_custom_header_val_from_file_cb(void)
554 {
555         gchar *filename = NULL;
556         gchar *contents = NULL;
557         const gchar *hdr = gtk_entry_get_text(GTK_ENTRY(customhdr.hdr_entry));
558         
559         if (!strcmp(hdr, "Face"))
560                 filename = filesel_select_file_open(_("Choose a PNG file"), NULL);
561         else if (!strcmp(hdr, "X-Face"))
562                 filename = filesel_select_file_open(_("Choose an XBM file"), NULL);
563         else
564                 filename = filesel_select_file_open(_("Choose a text file"), NULL);
565
566         if (!strcmp(hdr, "Face") || !strcmp(hdr, "X-Face")) {
567                 if (filename && is_file_exist(filename)) {
568                         FILE *fp = NULL;
569                         gint len;
570                         gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
571                         gchar *tmp = NULL;
572                         gint w, h;
573                         GdkPixbufFormat *format = gdk_pixbuf_get_file_info(
574                                                         filename, &w, &h);
575                         
576                         if (format == NULL) {
577                                 alertpanel_error(_("This file isn't an image."));
578                                 g_free(filename);
579                                 return;
580                         }
581                         if (w != 48 || h != 48) {
582                                 alertpanel_error(_("The chosen image isn't the correct size (48x48)."));
583                                 g_free(filename);
584                                 return; 
585                         }
586                         if (!strcmp(hdr, "Face")) {
587                                 if (get_file_size(filename) > 725) {
588                                         alertpanel_error(_("The image is too big; it must be maximum 725 bytes."));
589                                         g_free(filename);
590                                         return;
591                                 }
592                                 if (g_ascii_strcasecmp("png", gdk_pixbuf_format_get_name(format))) {
593                                         alertpanel_error(_("The image isn't in the correct format (PNG)."));
594                                         g_print("%s\n", gdk_pixbuf_format_get_name(format));
595                                         g_free(filename);
596                                         return;
597                                 }
598                         } else if (!strcmp(hdr, "X-Face")) {
599                                 gchar *tmp = NULL, *cmd = NULL;
600                                 int i = 0;
601                                 if (g_ascii_strcasecmp("xbm", gdk_pixbuf_format_get_name(format))) {
602                                         alertpanel_error(_("The image isn't in the correct format (XBM)."));
603                                         g_print("%s\n", gdk_pixbuf_format_get_name(format));
604                                         g_free(filename);
605                                         return;
606                                 }
607                                 cmd = g_strdup_printf("compface %s", filename);
608                                 tmp = get_command_output(cmd);
609                                 g_free(cmd);
610                                 if (tmp == NULL || strlen(tmp) == 0){
611                                         alertpanel_error(_("Couldn't call `compface`. Make sure it's in your $PATH."));
612                                         g_free(filename);
613                                         g_free(tmp);
614                                         return;
615                                 }
616                                 while (tmp[i]) {
617                                         gchar *tmp2 = NULL;
618                                         if (tmp[i] == ' ') {
619                                                 i++; continue;
620                                         } 
621                                         if (tmp[i] == '\r' || tmp[i] == '\n') {
622                                                 i++; continue;
623                                         }
624                                         tmp2 = contents;
625                                         contents = g_strdup_printf("%s%c",tmp2?tmp2:"", tmp[i]);
626                                         g_free(tmp2);
627                                         i++;
628                                 }
629                                 g_free(tmp);
630                                 goto settext;
631                         }
632
633                         fp = g_fopen(filename, "rb");
634                         if (!fp) {
635                                 g_free(filename);
636                                 return; 
637                         }
638
639                         while ((len = fread(inbuf, sizeof(gchar),
640                                             B64_LINE_SIZE, fp))
641                                == B64_LINE_SIZE) {
642                                 base64_encode(outbuf, inbuf, B64_LINE_SIZE);
643
644                                 tmp = contents;
645                                 contents = g_strconcat(tmp?tmp:"",outbuf, NULL);
646                                 g_free(tmp);
647                         }
648                         if (len > 0 && feof(fp)) {
649                                 tmp = contents;
650                                 base64_encode(outbuf, inbuf, len);
651                                 contents = g_strconcat(tmp?tmp:"",outbuf, NULL);
652                                 g_free(tmp);
653                         }
654                         fclose(fp);
655                 }
656         } else {
657                 if (!filename)
658                         return;
659
660                 contents = file_read_to_str(filename);
661                 if (strchr(contents, '\n') || strchr(contents,'\r')) {
662                         alertpanel_error(_("This file contains newlines."));
663                         g_free(contents);
664                         g_free(filename);
665                         return;
666                 }
667         }
668 settext:
669         if (contents && strlen(contents))
670                 gtk_entry_set_text(GTK_ENTRY(customhdr.val_entry), contents);
671         
672         g_free(contents);
673         g_free(filename);
674 }
675
676 static void prefs_custom_header_add_cb(void)
677 {
678         prefs_custom_header_list_view_set_row(cur_ac);
679 }
680
681 static void prefs_custom_header_delete_cb(void)
682 {
683         GtkTreeIter sel;
684         GtkTreeModel *model;
685         CustomHeader *ch;
686
687         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
688                                 (GTK_TREE_VIEW(customhdr.list_view)),
689                                 &model, &sel))
690                 return; 
691
692         if (alertpanel(_("Delete header"),
693                        _("Do you really want to delete this header?"),
694                        GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL) != G_ALERTALTERNATE)
695                 return;
696
697         gtk_tree_model_get(model, &sel,
698                            CUSTHDR_DATA, &ch,
699                            -1);
700         gtk_list_store_remove(GTK_LIST_STORE(model), &sel);
701
702         cur_ac->customhdr_list = g_slist_remove(cur_ac->customhdr_list, ch);
703         
704         custom_header_free(ch);
705 }
706
707 static void prefs_custom_header_up(void)
708 {
709         GtkTreePath *prev, *sel;
710         GtkTreeIter isel;
711         GtkListStore *store = NULL;
712         GtkTreeModel *model = NULL;
713         GtkTreeIter iprev;
714         
715         if (!gtk_tree_selection_get_selected
716                 (gtk_tree_view_get_selection
717                         (GTK_TREE_VIEW(customhdr.list_view)),
718                  &model,
719                  &isel))
720                 return;
721         store = (GtkListStore *)model;
722         sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
723         if (!sel)
724                 return;
725         
726         /* no move if we're at row 0... */
727         prev = gtk_tree_path_copy(sel);
728         if (!gtk_tree_path_prev(prev)) {
729                 gtk_tree_path_free(prev);
730                 gtk_tree_path_free(sel);
731                 return;
732         }
733
734         gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
735                                 &iprev, prev);
736         gtk_tree_path_free(sel);
737         gtk_tree_path_free(prev);
738
739         gtk_list_store_swap(store, &iprev, &isel);
740         prefs_custom_header_set_list(cur_ac);
741 }
742
743 static void prefs_custom_header_down(void)
744 {
745         GtkListStore *store = NULL;
746         GtkTreeModel *model = NULL;
747         GtkTreeIter next, sel;
748         
749         if (!gtk_tree_selection_get_selected
750                 (gtk_tree_view_get_selection
751                         (GTK_TREE_VIEW(customhdr.list_view)),
752                  &model,
753                  &sel))
754                 return;
755         store = (GtkListStore *)model;
756         next = sel;
757         if (!gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next)) 
758                 return;
759
760         gtk_list_store_swap(store, &next, &sel);
761         prefs_custom_header_set_list(cur_ac);
762 }
763
764 static gboolean prefs_custom_header_key_pressed(GtkWidget *widget,
765                                                 GdkEventKey *event,
766                                                 gpointer data)
767 {
768         if (event && event->keyval == GDK_Escape)
769                 prefs_custom_header_cancel();
770         return FALSE;
771 }
772
773 static void prefs_custom_header_ok(void)
774 {
775         prefs_custom_header_write_config(cur_ac);
776         gtk_widget_hide(customhdr.window);
777 }
778
779 static void prefs_custom_header_cancel(void)
780 {
781         prefs_custom_header_read_config(cur_ac); 
782         gtk_widget_hide(customhdr.window);
783 }
784
785 static gint prefs_custom_header_deleted(GtkWidget *widget, GdkEventAny *event,
786                                         gpointer data)
787 {
788         prefs_custom_header_cancel();
789         return TRUE;
790 }
791
792 static GtkListStore* prefs_custom_header_create_data_store(void)
793 {
794         return gtk_list_store_new(N_CUSTHDR_COLUMNS,
795                                   G_TYPE_STRING,        
796                                   G_TYPE_POINTER,
797                                   -1);
798 }
799
800 static void prefs_custom_header_list_view_insert_header(GtkWidget *list_view,
801                                                         GtkTreeIter *row_iter,
802                                                         gchar *header,
803                                                         gpointer data)
804 {
805         GtkTreeIter iter;
806         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
807                                         (GTK_TREE_VIEW(list_view)));
808
809         if (row_iter == NULL) {
810                 /* append new */
811                 gtk_list_store_append(list_store, &iter);
812                 gtk_list_store_set(list_store, &iter,
813                                    CUSTHDR_STRING, header,
814                                    CUSTHDR_DATA,   data,
815                                    -1);
816         } else {
817                 /* change existing */
818                 CustomHeader *old_data;
819
820                 gtk_tree_model_get(GTK_TREE_MODEL(list_store), row_iter,
821                                    CUSTHDR_DATA, &old_data,
822                                    -1);
823
824                 custom_header_free(old_data);
825                 
826                 gtk_list_store_set(list_store, row_iter,
827                                    CUSTHDR_STRING, header,
828                                    CUSTHDR_DATA, data,
829                                    -1);
830         }
831 }
832
833 static GtkWidget *prefs_custom_header_list_view_create(void)
834 {
835         GtkTreeView *list_view;
836         GtkTreeSelection *selector;
837         GtkTreeModel *model;
838
839         model = GTK_TREE_MODEL(prefs_custom_header_create_data_store());
840         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
841         g_object_unref(model);  
842         
843         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
844         
845         selector = gtk_tree_view_get_selection(list_view);
846         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
847         gtk_tree_selection_set_select_function(selector, prefs_custom_header_selected,
848                                                NULL, NULL);
849
850         /* create the columns */
851         prefs_custom_header_create_list_view_columns(GTK_WIDGET(list_view));
852
853         return GTK_WIDGET(list_view);
854 }
855
856 static void prefs_custom_header_create_list_view_columns(GtkWidget *list_view)
857 {
858         GtkTreeViewColumn *column;
859         GtkCellRenderer *renderer;
860
861         renderer = gtk_cell_renderer_text_new();
862         column = gtk_tree_view_column_new_with_attributes
863                 (_("Current custom headers"),
864                  renderer,
865                  "text", CUSTHDR_STRING,
866                  NULL);
867         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
868 }
869
870 #define ENTRY_SET_TEXT(entry, str) \
871         gtk_entry_set_text(GTK_ENTRY(entry), str ? str : "")
872
873 static gboolean prefs_custom_header_selected(GtkTreeSelection *selector,
874                                              GtkTreeModel *model, 
875                                              GtkTreePath *path,
876                                              gboolean currently_selected,
877                                              gpointer data)
878 {
879         GtkTreeIter iter;
880         CustomHeader *ch;
881         CustomHeader default_ch = { 0, "", NULL };
882
883         if (currently_selected)
884                 return TRUE;
885
886         if (!gtk_tree_model_get_iter(model, &iter, path))
887                 return TRUE;
888
889         gtk_tree_model_get(model, &iter, 
890                            CUSTHDR_DATA, &ch,
891                            -1);
892         
893         if (!ch) ch = &default_ch;
894
895         ENTRY_SET_TEXT(customhdr.hdr_entry, ch->name);
896         ENTRY_SET_TEXT(customhdr.val_entry, ch->value);
897                            
898         return TRUE;
899 }
900
901 #undef ENTRY_SET_TEXT