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