Use GLib's implementation of Base64 instead of our own.
[claws.git] / src / prefs_customheader.c
index e7f52664f498bca66358e92c7180d800251d0ab0..a82e0ecc24292804ffa475bc3cec7176bedd598c 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2005 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #include "defs.h"
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <gtk/gtk.h>
 #include <gdk/gdkkeysyms.h>
 #include <stdio.h>
@@ -31,7 +33,6 @@
 #include <string.h>
 #include <errno.h>
 
-#include "intl.h"
 #include "main.h"
 #include "prefs_gtk.h"
 #include "prefs_customheader.h"
@@ -45,6 +46,8 @@
 #include "utils.h"
 #include "gtkutils.h"
 #include "alertpanel.h"
+#include "filesel.h"
+#include "combobox.h"
 
 enum {
        CUSTHDR_STRING,         /*!< display string managed by list store */
@@ -61,6 +64,7 @@ static struct CustomHdr {
        GtkWidget *hdr_combo;
        GtkWidget *hdr_entry;
        GtkWidget *val_entry;
+       GtkWidget *preview;
        GtkWidget *list_view;
 } customhdr;
 
@@ -73,6 +77,7 @@ static void prefs_custom_header_list_view_set_row     (PrefsAccount *ac);
 
 /* callback functions */
 static void prefs_custom_header_add_cb         (void);
+static void prefs_custom_header_val_from_file_cb(void);
 static void prefs_custom_header_delete_cb      (void);
 static void prefs_custom_header_up             (void);
 static void prefs_custom_header_down           (void);
@@ -120,6 +125,7 @@ void prefs_custom_header_open(PrefsAccount *ac)
        cur_ac = ac;
 
        gtk_widget_show(customhdr.window);
+       gtk_window_set_modal(GTK_WINDOW(customhdr.window), TRUE);
 }
 
 static void prefs_custom_header_create(void)
@@ -139,12 +145,14 @@ static void prefs_custom_header_create(void)
        GtkWidget *hdr_combo;
        GtkWidget *val_label;
        GtkWidget *val_entry;
+       GtkWidget *val_btn;
 
        GtkWidget *reg_hbox;
        GtkWidget *btn_hbox;
        GtkWidget *arrow;
        GtkWidget *add_btn;
        GtkWidget *del_btn;
+       GtkWidget *preview;
 
        GtkWidget *ch_hbox;
        GtkWidget *ch_scrolledwin;
@@ -154,22 +162,19 @@ static void prefs_custom_header_create(void)
        GtkWidget *up_btn;
        GtkWidget *down_btn;
 
-       gchar *title[1];
-
        debug_print("Creating custom header setting window...\n");
 
-       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+       window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_customheader");
        gtk_container_set_border_width (GTK_CONTAINER (window), 8);
        gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
-       gtk_window_set_modal (GTK_WINDOW (window), TRUE);
        gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
 
        vbox = gtk_vbox_new (FALSE, 6);
        gtk_widget_show (vbox);
        gtk_container_add (GTK_CONTAINER (window), vbox);
 
-       gtkut_stock_button_set_create(&confirm_area, &ok_btn, GTK_STOCK_OK,
-                                     &cancel_btn, GTK_STOCK_CANCEL,
+       gtkut_stock_button_set_create(&confirm_area, &cancel_btn, GTK_STOCK_CANCEL,
+                                     &ok_btn, GTK_STOCK_OK,
                                      NULL, NULL);
        gtk_widget_show (confirm_area);
        gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
@@ -193,7 +198,7 @@ static void prefs_custom_header_create(void)
        gtk_box_pack_start (GTK_BOX (vbox), vbox1, TRUE, TRUE, 0);
        gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
 
-       table1 = gtk_table_new (2, 2, FALSE);
+       table1 = gtk_table_new (3, 2, FALSE);
        gtk_widget_show (table1);
        gtk_box_pack_start (GTK_BOX (vbox1), table1,
                            FALSE, FALSE, 0);
@@ -207,15 +212,12 @@ static void prefs_custom_header_create(void)
                          0, 0, 0);
        gtk_misc_set_alignment (GTK_MISC (hdr_label), 0, 0.5);
        
-       hdr_combo = gtk_combo_new ();
-       gtk_widget_show (hdr_combo);
+       hdr_combo = combobox_text_new(TRUE, "User-Agent", "Face", "X-Face",
+                                     "X-Operating-System", NULL);
        gtk_table_attach (GTK_TABLE (table1), hdr_combo, 0, 1, 1, 2,
                          GTK_EXPAND | GTK_SHRINK | GTK_FILL,
                          0, 0, 0);
        gtk_widget_set_size_request (hdr_combo, 150, -1);
-       gtkut_combo_set_items (GTK_COMBO (hdr_combo),
-                              "User-Agent", "X-Face", "X-Operating-System",
-                              NULL);
 
        val_label = gtk_label_new (_("Value"));
        gtk_widget_show (val_label);
@@ -231,6 +233,15 @@ static void prefs_custom_header_create(void)
                          0, 0, 0);
        gtk_widget_set_size_request (val_entry, 200, -1);
 
+       val_btn = gtkut_get_browse_file_btn(_("Bro_wse"));
+       gtk_widget_show (val_btn);
+       gtk_table_attach (GTK_TABLE (table1), val_btn, 2, 3, 1, 2,
+                         GTK_EXPAND | GTK_SHRINK | GTK_FILL,
+                         0, 0, 0);
+       g_signal_connect (G_OBJECT (val_btn), "clicked",
+                         G_CALLBACK (prefs_custom_header_val_from_file_cb),
+                         NULL);
+
        /* add / delete */
 
        reg_hbox = gtk_hbox_new (FALSE, 4);
@@ -253,7 +264,7 @@ static void prefs_custom_header_create(void)
                          G_CALLBACK (prefs_custom_header_add_cb),
                          NULL);
 
-       del_btn = gtk_button_new_from_stock (GTK_STOCK_REMOVE);
+       del_btn = gtk_button_new_from_stock (GTK_STOCK_DELETE);
        gtk_widget_show (del_btn);
        gtk_box_pack_start (GTK_BOX (btn_hbox), del_btn, FALSE, TRUE, 0);
        g_signal_connect (G_OBJECT (del_btn), "clicked",
@@ -293,14 +304,19 @@ static void prefs_custom_header_create(void)
        g_signal_connect (G_OBJECT (down_btn), "clicked",
                          G_CALLBACK (prefs_custom_header_down), NULL);
 
+       preview = gtk_image_new ();
+       gtk_widget_show (preview);
+       gtk_box_pack_start (GTK_BOX (btn_vbox), preview, FALSE, FALSE, 0);
+
        gtk_widget_show_all(window);
 
        customhdr.window     = window;
        customhdr.ok_btn     = ok_btn;
        customhdr.cancel_btn = cancel_btn;
+       customhdr.preview = preview;
 
        customhdr.hdr_combo  = hdr_combo;
-       customhdr.hdr_entry  = GTK_COMBO (hdr_combo)->entry;
+       customhdr.hdr_entry  = gtk_bin_get_child(GTK_BIN((hdr_combo)));
        customhdr.val_entry  = val_entry;
 
        customhdr.list_view   = list_view;
@@ -317,7 +333,7 @@ void prefs_custom_header_read_config(PrefsAccount *ac)
 
        rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
                             CUSTOM_HEADER_RC, NULL);
-       if ((fp = fopen(rcpath, "rb")) == NULL) {
+       if ((fp = g_fopen(rcpath, "rb")) == NULL) {
                if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
                g_free(rcpath);
                ac->customhdr_list = NULL;
@@ -328,8 +344,8 @@ void prefs_custom_header_read_config(PrefsAccount *ac)
        /* remove all previous headers list */
        while (ac->customhdr_list != NULL) {
                ch = (CustomHeader *)ac->customhdr_list->data;
-               custom_header_free(ch);
                ac->customhdr_list = g_slist_remove(ac->customhdr_list, ch);
+               custom_header_free(ch);
        }
 
        while (fgets(buf, sizeof(buf), fp) != NULL) {
@@ -346,7 +362,7 @@ void prefs_custom_header_read_config(PrefsAccount *ac)
        fclose(fp);
 }
 
-void prefs_custom_header_write_config(PrefsAccount *ac)
+static void prefs_custom_header_write_config(PrefsAccount *ac)
 {
        gchar *rcpath;
        PrefFile *pfile;
@@ -362,7 +378,7 @@ void prefs_custom_header_write_config(PrefsAccount *ac)
        rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
                             CUSTOM_HEADER_RC, NULL);
 
-       if ((fp = fopen(rcpath, "rb")) == NULL) {
+       if ((fp = g_fopen(rcpath, "rb")) == NULL) {
                if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
        } else {
                all_hdrs = NULL;
@@ -423,8 +439,8 @@ void prefs_custom_header_write_config(PrefsAccount *ac)
 
        while (all_hdrs != NULL) {
                ch = (CustomHeader *)all_hdrs->data;
-               custom_header_free(ch);
                all_hdrs = g_slist_remove(all_hdrs, ch);
+               custom_header_free(ch);
        }
 
        if (prefs_file_close(pfile) < 0) {
@@ -484,16 +500,18 @@ static void prefs_custom_header_list_view_set_row(PrefsAccount *ac)
        CustomHeader *ch;
        const gchar *entry_text;
        gchar *ch_str;
-       GtkListStore *store;
-
-       store = GTK_LIST_STORE(gtk_tree_view_get_model
-                               (GTK_TREE_VIEW(customhdr.list_view)));
 
        entry_text = gtk_entry_get_text(GTK_ENTRY(customhdr.hdr_entry));
        if (entry_text[0] == '\0') {
                alertpanel_error(_("Header name is not set."));
                return;
        }
+       
+       while (*entry_text && 
+              (*entry_text == '\n' || *entry_text == '\r' || 
+               *entry_text == '\t' || *entry_text == ' '))
+               entry_text++;
+       
        if (!custom_header_is_allowed(entry_text)) {
                alertpanel_error(_("This Header name is not allowed as a custom header."));
                return;
@@ -509,6 +527,11 @@ static void prefs_custom_header_list_view_set_row(PrefsAccount *ac)
        gtk_entry_set_text(GTK_ENTRY(customhdr.hdr_entry), ch->name);
 
        entry_text = gtk_entry_get_text(GTK_ENTRY(customhdr.val_entry));
+       while (*entry_text && 
+              (*entry_text == '\n' || *entry_text == '\r' || 
+               *entry_text == '\t' || *entry_text == ' '))
+               entry_text++;
+       
        if (entry_text[0] != '\0') {
                ch->value = g_strdup(entry_text);
                unfold_line(ch->value);
@@ -528,6 +551,139 @@ static void prefs_custom_header_list_view_set_row(PrefsAccount *ac)
 
 }
 
+#define B64_LINE_SIZE          57
+#define B64_BUFFSIZE           77
+static void prefs_custom_header_val_from_file_cb(void)
+{
+       gchar *filename = NULL;
+       gchar *contents = NULL;
+       const gchar *hdr = gtk_entry_get_text(GTK_ENTRY(customhdr.hdr_entry));
+       
+       if (!strcmp(hdr, "Face"))
+               filename = filesel_select_file_open(_("Choose a PNG file"), NULL);
+       else if (!strcmp(hdr, "X-Face"))
+               filename = filesel_select_file_open(_("Choose an XBM file"), NULL);
+       else
+               filename = filesel_select_file_open(_("Choose a text file"), NULL);
+
+       if (!strcmp(hdr, "Face") || !strcmp(hdr, "X-Face")) {
+               if (filename && is_file_exist(filename)) {
+                       FILE *fp = NULL;
+                       gint len;
+                       gchar inbuf[B64_LINE_SIZE], *outbuf;
+                       gchar *tmp = NULL;
+                       gint w, h;
+                       GdkPixbufFormat *format = gdk_pixbuf_get_file_info(
+                                                       filename, &w, &h);
+                       
+                       if (format == NULL) {
+                               alertpanel_error(_("This file isn't an image."));
+                               g_free(filename);
+                               return;
+                       }
+                       if (w != 48 || h != 48) {
+                               alertpanel_error(_("The chosen image isn't the correct size (48x48)."));
+                               g_free(filename);
+                               return; 
+                       }
+                       if (!strcmp(hdr, "Face")) {
+                               if (get_file_size(filename) > 725) {
+                                       alertpanel_error(_("The image is too big; it must be maximum 725 bytes."));
+                                       g_free(filename);
+                                       return;
+                               }
+                               if (g_ascii_strcasecmp("png", gdk_pixbuf_format_get_name(format))) {
+                                       alertpanel_error(_("The image isn't in the correct format (PNG)."));
+                                       g_print("%s\n", gdk_pixbuf_format_get_name(format));
+                                       g_free(filename);
+                                       return;
+                               }
+                       } else if (!strcmp(hdr, "X-Face")) {
+                               gchar *tmp = NULL, *cmd = NULL;
+                               int i = 0;
+                               if (g_ascii_strcasecmp("xbm", gdk_pixbuf_format_get_name(format))) {
+                                       alertpanel_error(_("The image isn't in the correct format (XBM)."));
+                                       g_print("%s\n", gdk_pixbuf_format_get_name(format));
+                                       g_free(filename);
+                                       return;
+                               }
+                               cmd = g_strdup_printf("compface %s", filename);
+                               tmp = get_command_output(cmd);
+                               g_free(cmd);
+                               if (tmp == NULL || *tmp == '\0') {
+                                       alertpanel_error(_("Couldn't call `compface`. Make sure it's in your $PATH."));
+                                       g_free(filename);
+                                       g_free(tmp);
+                                       return;
+                               }
+                               if (strstr(tmp, "compface:")) {
+                                       alertpanel_error(_("Compface error: %s"), tmp);
+                                       g_free(filename);
+                                       g_free(tmp);
+                                       return;
+                               }
+                               while (tmp[i]) {
+                                       gchar *tmp2 = NULL;
+                                       if (tmp[i] == ' ') {
+                                               i++; continue;
+                                       } 
+                                       if (tmp[i] == '\r' || tmp[i] == '\n') {
+                                               i++; continue;
+                                       }
+                                       tmp2 = contents;
+                                       contents = g_strdup_printf("%s%c",tmp2?tmp2:"", tmp[i]);
+                                       g_free(tmp2);
+                                       i++;
+                               }
+                               g_free(tmp);
+                               goto settext;
+                       }
+
+                       fp = g_fopen(filename, "rb");
+                       if (!fp) {
+                               g_free(filename);
+                               return; 
+                       }
+
+                       while ((len = fread(inbuf, sizeof(gchar),
+                                           B64_LINE_SIZE, fp))
+                              == B64_LINE_SIZE) {
+                               outbuf = g_base64_encode(inbuf, B64_LINE_SIZE);
+
+                               tmp = contents;
+                               contents = g_strconcat(tmp?tmp:"",outbuf, NULL);
+                               g_free(outbuf);
+                               g_free(tmp);
+                       }
+                       if (len > 0 && feof(fp)) {
+                               tmp = contents;
+                               outbuf = g_base64_encode(inbuf, len);
+                               contents = g_strconcat(tmp?tmp:"",outbuf, NULL);
+                               g_free(outbuf);
+                               g_free(tmp);
+                       }
+                       fclose(fp);
+               }
+       } else {
+               if (!filename)
+                       return;
+
+               contents = file_read_to_str(filename);
+               if (strchr(contents, '\n') || strchr(contents,'\r')) {
+                       alertpanel_error(_("This file contains newlines."));
+                       g_free(contents);
+                       g_free(filename);
+                       return;
+               }
+       }
+settext:
+       if (contents && strlen(contents))
+               gtk_entry_set_text(GTK_ENTRY(customhdr.val_entry), contents);
+       
+       g_free(contents);
+       g_free(filename);
+}
+
 static void prefs_custom_header_add_cb(void)
 {
        prefs_custom_header_list_view_set_row(cur_ac);
@@ -546,7 +702,7 @@ static void prefs_custom_header_delete_cb(void)
 
        if (alertpanel(_("Delete header"),
                       _("Do you really want to delete this header?"),
-                      GTK_STOCK_YES, GTK_STOCK_NO, NULL) != G_ALERTDEFAULT)
+                      GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL) != G_ALERTALTERNATE)
                return;
 
        gtk_tree_model_get(model, &sel,
@@ -563,16 +719,17 @@ static void prefs_custom_header_up(void)
 {
        GtkTreePath *prev, *sel;
        GtkTreeIter isel;
-       GtkListStore *store;
+       GtkListStore *store = NULL;
+       GtkTreeModel *model = NULL;
        GtkTreeIter iprev;
        
        if (!gtk_tree_selection_get_selected
                (gtk_tree_view_get_selection
                        (GTK_TREE_VIEW(customhdr.list_view)),
-                (GtkTreeModel **) &store,      
+                &model,
                 &isel))
                return;
-
+       store = (GtkListStore *)model;
        sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
        if (!sel)
                return;
@@ -596,16 +753,17 @@ static void prefs_custom_header_up(void)
 
 static void prefs_custom_header_down(void)
 {
-       GtkListStore *store;
+       GtkListStore *store = NULL;
+       GtkTreeModel *model = NULL;
        GtkTreeIter next, sel;
        
        if (!gtk_tree_selection_get_selected
                (gtk_tree_view_get_selection
                        (GTK_TREE_VIEW(customhdr.list_view)),
-                (GtkTreeModel **) &store,
+                &model,
                 &sel))
                return;
-
+       store = (GtkListStore *)model;
        next = sel;
        if (!gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next)) 
                return;
@@ -618,7 +776,7 @@ static gboolean prefs_custom_header_key_pressed(GtkWidget *widget,
                                                GdkEventKey *event,
                                                gpointer data)
 {
-       if (event && event->keyval == GDK_Escape)
+       if (event && event->keyval == GDK_KEY_Escape)
                prefs_custom_header_cancel();
        return FALSE;
 }
@@ -627,12 +785,14 @@ static void prefs_custom_header_ok(void)
 {
        prefs_custom_header_write_config(cur_ac);
        gtk_widget_hide(customhdr.window);
+       gtk_window_set_modal(GTK_WINDOW(customhdr.window), FALSE);
 }
 
 static void prefs_custom_header_cancel(void)
 {
        prefs_custom_header_read_config(cur_ac); 
        gtk_widget_hide(customhdr.window);
+       gtk_window_set_modal(GTK_WINDOW(customhdr.window), FALSE);
 }
 
 static gint prefs_custom_header_deleted(GtkWidget *widget, GdkEventAny *event,
@@ -693,7 +853,8 @@ static GtkWidget *prefs_custom_header_list_view_create(void)
        list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
        g_object_unref(model);  
        
-       gtk_tree_view_set_rules_hint(list_view, TRUE);
+       gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
+       gtk_tree_view_set_reorderable(list_view, TRUE);
        
        selector = gtk_tree_view_get_selection(list_view);
        gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
@@ -731,6 +892,8 @@ static gboolean prefs_custom_header_selected(GtkTreeSelection *selector,
 {
        GtkTreeIter iter;
        CustomHeader *ch;
+       GtkImage *preview;
+       GdkPixbuf *pixbuf;
        CustomHeader default_ch = { 0, "", NULL };
 
        if (currently_selected)
@@ -747,7 +910,25 @@ static gboolean prefs_custom_header_selected(GtkTreeSelection *selector,
 
        ENTRY_SET_TEXT(customhdr.hdr_entry, ch->name);
        ENTRY_SET_TEXT(customhdr.val_entry, ch->value);
-                          
+       if (!strcmp2("Face",ch->name)) {
+               preview = GTK_IMAGE(face_get_from_header (ch->value));
+               pixbuf = gtk_image_get_pixbuf(preview);
+               gtk_image_set_from_pixbuf (GTK_IMAGE(customhdr.preview), pixbuf);
+               gtk_widget_show(customhdr.preview);
+               g_object_ref_sink (G_OBJECT(preview));
+       } 
+#if HAVE_LIBCOMPFACE
+else if (!strcmp2("X-Face", ch->name)) {
+               preview = GTK_IMAGE(xface_get_from_header(ch->value));  
+               pixbuf = gtk_image_get_pixbuf(preview);
+               gtk_image_set_from_pixbuf (GTK_IMAGE(customhdr.preview), pixbuf);
+               gtk_widget_show(customhdr.preview);
+               g_object_ref_sink (G_OBJECT(preview));
+       } 
+#endif
+else {
+               gtk_widget_hide(customhdr.preview);
+       }
        return TRUE;
 }