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