2009-03-16 [paul] 3.7.1cvs17
[claws.git] / src / prefs_customheader.c
index 34a9e0865a383a8a906f63eae5774186a3f84a0c..ccd66a88bee1abadd97432d15adc733f7e447134 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
+ * Copyright (C) 1999-2009 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,
@@ -13,8 +13,8 @@
  * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
@@ -47,6 +47,7 @@
 #include "alertpanel.h"
 #include "base64.h"
 #include "filesel.h"
+#include "combobox.h"
 
 enum {
        CUSTHDR_STRING,         /*!< display string managed by list store */
@@ -63,6 +64,7 @@ static struct CustomHdr {
        GtkWidget *hdr_combo;
        GtkWidget *hdr_entry;
        GtkWidget *val_entry;
+       GtkWidget *preview;
        GtkWidget *list_view;
 } customhdr;
 
@@ -149,6 +151,7 @@ static void prefs_custom_header_create(void)
        GtkWidget *arrow;
        GtkWidget *add_btn;
        GtkWidget *del_btn;
+       GtkWidget *preview;
 
        GtkWidget *ch_hbox;
        GtkWidget *ch_scrolledwin;
@@ -160,7 +163,7 @@ static void prefs_custom_header_create(void)
 
        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);
@@ -209,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", "Face", "X-Face", "X-Operating-System",
-                              NULL);
 
        val_label = gtk_label_new (_("Value"));
        gtk_widget_show (val_label);
@@ -304,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;
@@ -357,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;
@@ -554,10 +559,17 @@ static void prefs_custom_header_list_view_set_row(PrefsAccount *ac)
 #define B64_BUFFSIZE           77
 static void prefs_custom_header_val_from_file_cb(void)
 {
-       gchar *filename = filesel_select_file_open(_("Choose file"), NULL);
+       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;
@@ -586,7 +598,7 @@ static void prefs_custom_header_val_from_file_cb(void)
                                }
                                if (g_ascii_strcasecmp("png", gdk_pixbuf_format_get_name(format))) {
                                        alertpanel_error(_("The image isn't in the correct format (PNG)."));
-                                       printf("%s\n", gdk_pixbuf_format_get_name(format));
+                                       g_print("%s\n", gdk_pixbuf_format_get_name(format));
                                        g_free(filename);
                                        return;
                                }
@@ -595,19 +607,25 @@ static void prefs_custom_header_val_from_file_cb(void)
                                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)."));
-                                       printf("%s\n", gdk_pixbuf_format_get_name(format));
+                                       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 || strlen(tmp) == 0){
+                               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] == ' ') {
@@ -649,6 +667,9 @@ static void prefs_custom_header_val_from_file_cb(void)
                        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."));
@@ -832,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, prefs_common.enable_rules_hint);
+       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);
@@ -870,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)
@@ -886,7 +910,39 @@ 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 (!g_strcmp0("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);
+#if GLIB_CHECK_VERSION(2,10,0)
+               g_object_ref_sink (G_OBJECT(preview));
+#else
+               gtk_object_ref (G_OBJECT(preview));
+               gtk_object_sink (G_OBJECT(preview));
+#endif
+       } 
+#if HAVE_LIBCOMPFACE
+else if (!g_strcmp0("X-Face", ch->name)) {
+               GdkColor color;
+               color.pixel = 0;
+               preview = GTK_IMAGE(xface_get_from_header(ch->value, 
+                                         &color, 
+                                         mainwindow_get_mainwindow()->window->window));        
+               pixbuf = gtk_image_get_pixbuf(preview);
+               gtk_image_set_from_pixbuf (GTK_IMAGE(customhdr.preview), pixbuf);
+               gtk_widget_show(customhdr.preview);
+#if GLIB_CHECK_VERSION(2,10,0)
+               g_object_ref_sink (G_OBJECT(preview));
+#else
+               gtk_object_ref (G_OBJECT(preview));
+               gtk_object_sink (G_OBJECT(preview));
+#endif
+       } 
+#endif
+else {
+               gtk_widget_hide(customhdr.preview);
+       }
        return TRUE;
 }