fix bug 4239, 'Preferences: Text Options Header Display modal is not modal' (sic)
[claws.git] / src / prefs_customheader.c
index 48823d3accb1bec6e518d9aa4268d8fb9ff73e59..c60328bfba4f11f1c63efebce36445757316e228 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2009 Hiroyuki Yamamoto and the Claws Mail team
+ * 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
@@ -19,6 +19,7 @@
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #include "defs.h"
@@ -45,9 +46,9 @@
 #include "utils.h"
 #include "gtkutils.h"
 #include "alertpanel.h"
-#include "base64.h"
 #include "filesel.h"
 #include "combobox.h"
+#include "file-utils.h"
 
 enum {
        CUSTHDR_STRING,         /*!< display string managed by list store */
@@ -125,6 +126,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)
@@ -166,8 +168,8 @@ static void prefs_custom_header_create(void)
        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);
+       gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
 
        vbox = gtk_vbox_new (FALSE, 6);
        gtk_widget_show (vbox);
@@ -217,7 +219,6 @@ static void prefs_custom_header_create(void)
        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);
 
        val_label = gtk_label_new (_("Value"));
        gtk_widget_show (val_label);
@@ -231,9 +232,8 @@ static void prefs_custom_header_create(void)
        gtk_table_attach (GTK_TABLE (table1), val_entry, 1, 2, 1, 2,
                          GTK_EXPAND | GTK_SHRINK | GTK_FILL,
                          0, 0, 0);
-       gtk_widget_set_size_request (val_entry, 200, -1);
 
-       val_btn = gtk_button_new_with_label (_("From file..."));
+       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,
@@ -251,7 +251,6 @@ static void prefs_custom_header_create(void)
        arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
        gtk_widget_show (arrow);
        gtk_box_pack_start (GTK_BOX (reg_hbox), arrow, FALSE, FALSE, 0);
-       gtk_widget_set_size_request (arrow, -1, 16);
 
        btn_hbox = gtk_hbox_new (TRUE, 4);
        gtk_widget_show (btn_hbox);
@@ -277,7 +276,6 @@ static void prefs_custom_header_create(void)
        gtk_box_pack_start (GTK_BOX (vbox1), ch_hbox, TRUE, TRUE, 0);
 
        ch_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
-       gtk_widget_set_size_request (ch_scrolledwin, -1, 200);
        gtk_widget_show (ch_scrolledwin);
        gtk_box_pack_start (GTK_BOX (ch_hbox), ch_scrolledwin, TRUE, TRUE, 0);
        gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (ch_scrolledwin),
@@ -333,8 +331,8 @@ void prefs_custom_header_read_config(PrefsAccount *ac)
 
        rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
                             CUSTOM_HEADER_RC, NULL);
-       if ((fp = g_fopen(rcpath, "rb")) == NULL) {
-               if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
+       if ((fp = claws_fopen(rcpath, "rb")) == NULL) {
+               if (ENOENT != errno) FILE_OP_ERROR(rcpath, "claws_fopen");
                g_free(rcpath);
                ac->customhdr_list = NULL;
                return;
@@ -348,7 +346,7 @@ void prefs_custom_header_read_config(PrefsAccount *ac)
                custom_header_free(ch);
        }
 
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
+       while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
                ch = custom_header_read_str(buf);
                if (ch) {
                        if (ch->account_id == ac->account_id) {
@@ -359,7 +357,7 @@ void prefs_custom_header_read_config(PrefsAccount *ac)
                }
        }
 
-       fclose(fp);
+       claws_fclose(fp);
 }
 
 static void prefs_custom_header_write_config(PrefsAccount *ac)
@@ -378,12 +376,12 @@ static void prefs_custom_header_write_config(PrefsAccount *ac)
        rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
                             CUSTOM_HEADER_RC, NULL);
 
-       if ((fp = g_fopen(rcpath, "rb")) == NULL) {
-               if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
+       if ((fp = claws_fopen(rcpath, "rb")) == NULL) {
+               if (ENOENT != errno) FILE_OP_ERROR(rcpath, "claws_fopen");
        } else {
                all_hdrs = NULL;
 
-               while (fgets(buf, sizeof(buf), fp) != NULL) {
+               while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
                        ch = custom_header_read_str(buf);
                        if (ch) {
                                if (ch->account_id != ac->account_id)
@@ -394,11 +392,11 @@ static void prefs_custom_header_write_config(PrefsAccount *ac)
                        }
                }
 
-               fclose(fp);
+               claws_fclose(fp);
        }
 
        if ((pfile = prefs_write_open(rcpath)) == NULL) {
-               g_warning("failed to write configuration to file\n");
+               g_warning("failed to write configuration to file");
                g_free(rcpath);
                return;
        }
@@ -408,9 +406,9 @@ static void prefs_custom_header_write_config(PrefsAccount *ac)
                gchar *chstr;
 
                chstr = custom_header_get_str(hdr);
-               if (fputs(chstr, pfile->fp) == EOF ||
-                   fputc('\n', pfile->fp) == EOF) {
-                       FILE_OP_ERROR(rcpath, "fputs || fputc");
+               if (claws_fputs(chstr, pfile->fp) == EOF ||
+                   claws_fputc('\n', pfile->fp) == EOF) {
+                       FILE_OP_ERROR(rcpath, "claws_fputs || claws_fputc");
                        prefs_file_close_revert(pfile);
                        g_free(rcpath);
                        g_free(chstr);
@@ -424,9 +422,9 @@ static void prefs_custom_header_write_config(PrefsAccount *ac)
                gchar *chstr;
 
                chstr = custom_header_get_str(hdr);
-               if (fputs(chstr, pfile->fp) == EOF ||
-                   fputc('\n', pfile->fp) == EOF) {
-                       FILE_OP_ERROR(rcpath, "fputs || fputc");
+               if (claws_fputs(chstr, pfile->fp) == EOF ||
+                   claws_fputc('\n', pfile->fp) == EOF) {
+                       FILE_OP_ERROR(rcpath, "claws_fputs || claws_fputc");
                        prefs_file_close_revert(pfile);
                        g_free(rcpath);
                        g_free(chstr);
@@ -444,7 +442,7 @@ static void prefs_custom_header_write_config(PrefsAccount *ac)
        }
 
        if (prefs_file_close(pfile) < 0) {
-               g_warning("failed to write configuration to file\n");
+               g_warning("failed to write configuration to file");
                return;
        }
 }
@@ -500,10 +498,6 @@ 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') {
@@ -574,7 +568,7 @@ static void prefs_custom_header_val_from_file_cb(void)
                if (filename && is_file_exist(filename)) {
                        FILE *fp = NULL;
                        gint len;
-                       gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
+                       gchar inbuf[B64_LINE_SIZE], *outbuf;
                        gchar *tmp = NULL;
                        gint w, h;
                        GdkPixbufFormat *format = gdk_pixbuf_get_file_info(
@@ -643,28 +637,30 @@ static void prefs_custom_header_val_from_file_cb(void)
                                goto settext;
                        }
 
-                       fp = g_fopen(filename, "rb");
+                       fp = claws_fopen(filename, "rb");
                        if (!fp) {
                                g_free(filename);
                                return; 
                        }
 
-                       while ((len = fread(inbuf, sizeof(gchar),
+                       while ((len = claws_fread(inbuf, sizeof(gchar),
                                            B64_LINE_SIZE, fp))
                               == B64_LINE_SIZE) {
-                               base64_encode(outbuf, inbuf, 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)) {
+                       if (len > 0 && claws_feof(fp)) {
                                tmp = contents;
-                               base64_encode(outbuf, inbuf, len);
+                               outbuf = g_base64_encode(inbuf, len);
                                contents = g_strconcat(tmp?tmp:"",outbuf, NULL);
+                               g_free(outbuf);
                                g_free(tmp);
                        }
-                       fclose(fp);
+                       claws_fclose(fp);
                }
        } else {
                if (!filename)
@@ -704,7 +700,7 @@ static void prefs_custom_header_delete_cb(void)
 
        if (alertpanel(_("Delete header"),
                       _("Do you really want to delete this header?"),
-                      GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL) != G_ALERTALTERNATE)
+                      GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, ALERTFOCUS_FIRST) != G_ALERTALTERNATE)
                return;
 
        gtk_tree_model_get(model, &sel,
@@ -778,7 +774,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;
 }
@@ -787,12 +783,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,
@@ -910,34 +908,20 @@ 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)) {
+       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 (!strcmp2("X-Face", ch->name)) {
-               GdkColor color;
-               color.pixel = 0;
-               preview = GTK_IMAGE(xface_get_from_header(ch->value, 
-                                         &color, 
-                                         mainwindow_get_mainwindow()->window->window));        
+else if (!g_strcmp0("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);
-#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 {