2007-10-22 [colin] 3.0.2cvs96
[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 || *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                                 if (strstr(tmp, "compface:")) {
617                                         alertpanel_error(_("Compface error: %s"), tmp);
618                                         g_free(filename);
619                                         g_free(tmp);
620                                         return;
621                                 }
622                                 while (tmp[i]) {
623                                         gchar *tmp2 = NULL;
624                                         if (tmp[i] == ' ') {
625                                                 i++; continue;
626                                         } 
627                                         if (tmp[i] == '\r' || tmp[i] == '\n') {
628                                                 i++; continue;
629                                         }
630                                         tmp2 = contents;
631                                         contents = g_strdup_printf("%s%c",tmp2?tmp2:"", tmp[i]);
632                                         g_free(tmp2);
633                                         i++;
634                                 }
635                                 g_free(tmp);
636                                 goto settext;
637                         }
638
639                         fp = g_fopen(filename, "rb");
640                         if (!fp) {
641                                 g_free(filename);
642                                 return; 
643                         }
644
645                         while ((len = fread(inbuf, sizeof(gchar),
646                                             B64_LINE_SIZE, fp))
647                                == B64_LINE_SIZE) {
648                                 base64_encode(outbuf, inbuf, B64_LINE_SIZE);
649
650                                 tmp = contents;
651                                 contents = g_strconcat(tmp?tmp:"",outbuf, NULL);
652                                 g_free(tmp);
653                         }
654                         if (len > 0 && feof(fp)) {
655                                 tmp = contents;
656                                 base64_encode(outbuf, inbuf, len);
657                                 contents = g_strconcat(tmp?tmp:"",outbuf, NULL);
658                                 g_free(tmp);
659                         }
660                         fclose(fp);
661                 }
662         } else {
663                 if (!filename)
664                         return;
665
666                 contents = file_read_to_str(filename);
667                 if (strchr(contents, '\n') || strchr(contents,'\r')) {
668                         alertpanel_error(_("This file contains newlines."));
669                         g_free(contents);
670                         g_free(filename);
671                         return;
672                 }
673         }
674 settext:
675         if (contents && strlen(contents))
676                 gtk_entry_set_text(GTK_ENTRY(customhdr.val_entry), contents);
677         
678         g_free(contents);
679         g_free(filename);
680 }
681
682 static void prefs_custom_header_add_cb(void)
683 {
684         prefs_custom_header_list_view_set_row(cur_ac);
685 }
686
687 static void prefs_custom_header_delete_cb(void)
688 {
689         GtkTreeIter sel;
690         GtkTreeModel *model;
691         CustomHeader *ch;
692
693         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
694                                 (GTK_TREE_VIEW(customhdr.list_view)),
695                                 &model, &sel))
696                 return; 
697
698         if (alertpanel(_("Delete header"),
699                        _("Do you really want to delete this header?"),
700                        GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL) != G_ALERTALTERNATE)
701                 return;
702
703         gtk_tree_model_get(model, &sel,
704                            CUSTHDR_DATA, &ch,
705                            -1);
706         gtk_list_store_remove(GTK_LIST_STORE(model), &sel);
707
708         cur_ac->customhdr_list = g_slist_remove(cur_ac->customhdr_list, ch);
709         
710         custom_header_free(ch);
711 }
712
713 static void prefs_custom_header_up(void)
714 {
715         GtkTreePath *prev, *sel;
716         GtkTreeIter isel;
717         GtkListStore *store = NULL;
718         GtkTreeModel *model = NULL;
719         GtkTreeIter iprev;
720         
721         if (!gtk_tree_selection_get_selected
722                 (gtk_tree_view_get_selection
723                         (GTK_TREE_VIEW(customhdr.list_view)),
724                  &model,
725                  &isel))
726                 return;
727         store = (GtkListStore *)model;
728         sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
729         if (!sel)
730                 return;
731         
732         /* no move if we're at row 0... */
733         prev = gtk_tree_path_copy(sel);
734         if (!gtk_tree_path_prev(prev)) {
735                 gtk_tree_path_free(prev);
736                 gtk_tree_path_free(sel);
737                 return;
738         }
739
740         gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
741                                 &iprev, prev);
742         gtk_tree_path_free(sel);
743         gtk_tree_path_free(prev);
744
745         gtk_list_store_swap(store, &iprev, &isel);
746         prefs_custom_header_set_list(cur_ac);
747 }
748
749 static void prefs_custom_header_down(void)
750 {
751         GtkListStore *store = NULL;
752         GtkTreeModel *model = NULL;
753         GtkTreeIter next, sel;
754         
755         if (!gtk_tree_selection_get_selected
756                 (gtk_tree_view_get_selection
757                         (GTK_TREE_VIEW(customhdr.list_view)),
758                  &model,
759                  &sel))
760                 return;
761         store = (GtkListStore *)model;
762         next = sel;
763         if (!gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next)) 
764                 return;
765
766         gtk_list_store_swap(store, &next, &sel);
767         prefs_custom_header_set_list(cur_ac);
768 }
769
770 static gboolean prefs_custom_header_key_pressed(GtkWidget *widget,
771                                                 GdkEventKey *event,
772                                                 gpointer data)
773 {
774         if (event && event->keyval == GDK_Escape)
775                 prefs_custom_header_cancel();
776         return FALSE;
777 }
778
779 static void prefs_custom_header_ok(void)
780 {
781         prefs_custom_header_write_config(cur_ac);
782         gtk_widget_hide(customhdr.window);
783 }
784
785 static void prefs_custom_header_cancel(void)
786 {
787         prefs_custom_header_read_config(cur_ac); 
788         gtk_widget_hide(customhdr.window);
789 }
790
791 static gint prefs_custom_header_deleted(GtkWidget *widget, GdkEventAny *event,
792                                         gpointer data)
793 {
794         prefs_custom_header_cancel();
795         return TRUE;
796 }
797
798 static GtkListStore* prefs_custom_header_create_data_store(void)
799 {
800         return gtk_list_store_new(N_CUSTHDR_COLUMNS,
801                                   G_TYPE_STRING,        
802                                   G_TYPE_POINTER,
803                                   -1);
804 }
805
806 static void prefs_custom_header_list_view_insert_header(GtkWidget *list_view,
807                                                         GtkTreeIter *row_iter,
808                                                         gchar *header,
809                                                         gpointer data)
810 {
811         GtkTreeIter iter;
812         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
813                                         (GTK_TREE_VIEW(list_view)));
814
815         if (row_iter == NULL) {
816                 /* append new */
817                 gtk_list_store_append(list_store, &iter);
818                 gtk_list_store_set(list_store, &iter,
819                                    CUSTHDR_STRING, header,
820                                    CUSTHDR_DATA,   data,
821                                    -1);
822         } else {
823                 /* change existing */
824                 CustomHeader *old_data;
825
826                 gtk_tree_model_get(GTK_TREE_MODEL(list_store), row_iter,
827                                    CUSTHDR_DATA, &old_data,
828                                    -1);
829
830                 custom_header_free(old_data);
831                 
832                 gtk_list_store_set(list_store, row_iter,
833                                    CUSTHDR_STRING, header,
834                                    CUSTHDR_DATA, data,
835                                    -1);
836         }
837 }
838
839 static GtkWidget *prefs_custom_header_list_view_create(void)
840 {
841         GtkTreeView *list_view;
842         GtkTreeSelection *selector;
843         GtkTreeModel *model;
844
845         model = GTK_TREE_MODEL(prefs_custom_header_create_data_store());
846         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
847         g_object_unref(model);  
848         
849         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
850         
851         selector = gtk_tree_view_get_selection(list_view);
852         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
853         gtk_tree_selection_set_select_function(selector, prefs_custom_header_selected,
854                                                NULL, NULL);
855
856         /* create the columns */
857         prefs_custom_header_create_list_view_columns(GTK_WIDGET(list_view));
858
859         return GTK_WIDGET(list_view);
860 }
861
862 static void prefs_custom_header_create_list_view_columns(GtkWidget *list_view)
863 {
864         GtkTreeViewColumn *column;
865         GtkCellRenderer *renderer;
866
867         renderer = gtk_cell_renderer_text_new();
868         column = gtk_tree_view_column_new_with_attributes
869                 (_("Current custom headers"),
870                  renderer,
871                  "text", CUSTHDR_STRING,
872                  NULL);
873         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
874 }
875
876 #define ENTRY_SET_TEXT(entry, str) \
877         gtk_entry_set_text(GTK_ENTRY(entry), str ? str : "")
878
879 static gboolean prefs_custom_header_selected(GtkTreeSelection *selector,
880                                              GtkTreeModel *model, 
881                                              GtkTreePath *path,
882                                              gboolean currently_selected,
883                                              gpointer data)
884 {
885         GtkTreeIter iter;
886         CustomHeader *ch;
887         CustomHeader default_ch = { 0, "", NULL };
888
889         if (currently_selected)
890                 return TRUE;
891
892         if (!gtk_tree_model_get_iter(model, &iter, path))
893                 return TRUE;
894
895         gtk_tree_model_get(model, &iter, 
896                            CUSTHDR_DATA, &ch,
897                            -1);
898         
899         if (!ch) ch = &default_ch;
900
901         ENTRY_SET_TEXT(customhdr.hdr_entry, ch->name);
902         ENTRY_SET_TEXT(customhdr.val_entry, ch->value);
903                            
904         return TRUE;
905 }
906
907 #undef ENTRY_SET_TEXT