Made Fancy's custom stylesheet pref work on Windows
[claws.git] / src / plugins / fancy / fancy_prefs.c
index eae11c6e66924943a801d6bf465168cedfc09a17..a2ca78196350e26c3f5eb2c8367f21fc71b105e3 100644 (file)
@@ -1,8 +1,8 @@
 /*
  * Claws Mail -- A GTK+ based, lightweight, and fast e-mail client
- * Copyright(C) 1999-2013 the Claws Mail Team
+ * Copyright(C) 1999-2015 the Claws Mail Team
  * == Fancy Plugin ==
- * This file Copyright (C) 2009-2013 Salvatore De Paolis
+ * This file Copyright (C) 2009-2015 Salvatore De Paolis
  * <iwkse@claws-mail.org> 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
@@ -29,8 +29,6 @@
 #include "version.h"
 #include "claws.h"
 #include "plugin.h"
-
-
 #include "gtkutils.h"
 #include "utils.h"
 #include "prefs.h"
@@ -39,6 +37,7 @@
 #include "prefswindow.h"
 #include "combobox.h"
 #include "addressbook.h"
+#include "filesel.h"
 
 #include "fancy_prefs.h"
 
@@ -66,6 +65,7 @@ struct _FancyPrefsPage {
 #endif
        GtkWidget *proxy_checkbox;
        GtkWidget *proxy_str;
+       GtkWidget *stylesheet;
 };
 
 static PrefParam param[] = {
@@ -91,11 +91,16 @@ static PrefParam param[] = {
                NULL, NULL, NULL},
                {"proxy_server", "http://SERVERNAME:PORT", &fancy_prefs.proxy_str, P_STRING,
                NULL, NULL, NULL},
+               {"stylesheet", "", &fancy_prefs.stylesheet, P_STRING, NULL, NULL, NULL},
                {0,0,0,0}
 };
 
 static FancyPrefsPage fancy_prefs_page;
 
+static void fancy_prefs_stylesheet_browse_cb   (GtkWidget *widget, gpointer data);
+static void fancy_prefs_stylesheet_edit_cb     (GtkWidget *widget, gpointer data);
+static void fancy_prefs_stylesheet_changed_cb  (GtkWidget *widget, gpointer data);
+
 static void create_fancy_prefs_page     (PrefsPage *page, GtkWindow *window, gpointer   data);
 static void destroy_fancy_prefs_page    (PrefsPage *page);
 static void save_fancy_prefs_page       (PrefsPage *page);
@@ -190,12 +195,16 @@ static void create_fancy_prefs_page(PrefsPage *page, GtkWindow *window,
        GtkWidget *enable_scripts;
        GtkWidget *enable_plugins;
        GtkWidget *enable_java;
+       GtkWidget *stylesheet_label;
+       GtkWidget *stylesheet;
+       GtkWidget *stylesheet_browse_button;
+       GtkWidget *stylesheet_edit_button;
 
        vbox = gtk_vbox_new(FALSE, 3);
        gtk_container_set_border_width(GTK_CONTAINER(vbox), VBOX_BORDER);
        gtk_widget_show(vbox);
 
-       GtkWidget *block = gtk_hbox_new(FALSE, FALSE);
+       GtkWidget *block = gtk_hbox_new(FALSE, 5);
 
        vbox_proxy = gtkut_get_options_frame(vbox, &frame_proxy, _("Proxy"));
 #ifdef HAVE_LIBSOUP_GNOME
@@ -207,7 +216,7 @@ static void create_fancy_prefs_page(PrefsPage *page, GtkWindow *window,
        g_signal_connect(G_OBJECT(gnome_proxy_checkbox), "toggled",
                         G_CALLBACK(prefs_disable_fancy_proxy), block);
 #endif
-       proxy_checkbox = gtk_check_button_new_with_label(_("Use proxy:"));
+       proxy_checkbox = gtk_check_button_new_with_label(_("Use proxy"));
        proxy_str = gtk_entry_new();
 #ifdef HAVE_LIBSOUP_GNOME
        if (fancy_prefs.enable_gnome_proxy)
@@ -221,7 +230,7 @@ static void create_fancy_prefs_page(PrefsPage *page, GtkWindow *window,
        pref_set_entry_from_pref(GTK_ENTRY(proxy_str), fancy_prefs.proxy_str);
 
        gtk_box_pack_start(GTK_BOX(block), proxy_checkbox, FALSE, FALSE, 0);
-       gtk_box_pack_start(GTK_BOX(block), proxy_str, FALSE, TRUE, 0);
+       gtk_box_pack_start(GTK_BOX(block), proxy_str, TRUE, TRUE, 0);
        gtk_box_pack_start(GTK_BOX(vbox_proxy), block, FALSE, FALSE, 0);
        gtk_widget_show_all(vbox_proxy);
 
@@ -264,7 +273,7 @@ static void create_fancy_prefs_page(PrefsPage *page, GtkWindow *window,
        gtk_widget_show(enable_plugins);
 
        GtkWidget *hbox_ext = gtk_hbox_new(FALSE, 8);
-       GtkWidget *open_external_label = gtk_label_new(_("When clicking on a link, by default:"));
+       GtkWidget *open_external_label = gtk_label_new(_("When clicking on a link, by default"));
        GtkWidget *optmenu_open_external = gtkut_sc_combobox_create(NULL, FALSE);
        GtkListStore *menu = GTK_LIST_STORE(gtk_combo_box_get_model(
                                GTK_COMBO_BOX(optmenu_open_external)));
@@ -281,6 +290,37 @@ static void create_fancy_prefs_page(PrefsPage *page, GtkWindow *window,
 
        combobox_select_by_data(GTK_COMBO_BOX(optmenu_open_external),
                        fancy_prefs.open_external);
+       
+       GtkWidget *hbox_css = gtk_hbox_new(FALSE, 8);
+       gtk_widget_show(hbox_css);
+       gtk_box_pack_start(GTK_BOX(vbox), hbox_css, FALSE, FALSE, 0);
+
+       CLAWS_SET_TIP(hbox_css, _("The CSS in this file will be applied to all HTML parts"));
+
+       stylesheet_label = gtk_label_new(_("Stylesheet"));
+       gtk_widget_show(stylesheet_label);
+       gtk_box_pack_start(GTK_BOX(hbox_css), stylesheet_label, FALSE, FALSE, 0);
+       
+       stylesheet = gtk_entry_new();
+       gtk_widget_show(stylesheet);
+       gtk_box_pack_start(GTK_BOX(hbox_css), stylesheet, TRUE, TRUE, 0);
+
+       stylesheet_browse_button = gtkut_get_browse_file_btn(_("Bro_wse"));
+       gtk_widget_show(stylesheet_browse_button);
+       gtk_box_pack_start(GTK_BOX(hbox_css), stylesheet_browse_button, FALSE, FALSE, 0);
+       g_signal_connect(G_OBJECT(stylesheet_browse_button), "clicked",
+                        G_CALLBACK(fancy_prefs_stylesheet_browse_cb), stylesheet);
+
+       stylesheet_edit_button = gtk_button_new_from_stock(GTK_STOCK_EDIT);
+       gtk_widget_show (stylesheet_edit_button);
+       gtk_box_pack_start(GTK_BOX(hbox_css), stylesheet_edit_button, FALSE, FALSE, 0);
+       g_signal_connect(G_OBJECT(stylesheet_edit_button), "clicked",
+                        G_CALLBACK(fancy_prefs_stylesheet_edit_cb), stylesheet);
+       g_signal_connect(G_OBJECT(stylesheet), "changed",
+                        G_CALLBACK(fancy_prefs_stylesheet_changed_cb), stylesheet_edit_button);
+       pref_set_entry_from_pref(GTK_ENTRY(stylesheet), fancy_prefs.stylesheet);
+       g_signal_emit_by_name(G_OBJECT(stylesheet), "changed", stylesheet_edit_button);
+
 
 #ifdef HAVE_LIBSOUP_GNOME
        prefs_page->gnome_proxy_checkbox = gnome_proxy_checkbox;
@@ -293,6 +333,7 @@ static void create_fancy_prefs_page(PrefsPage *page, GtkWindow *window,
        prefs_page->enable_plugins = enable_plugins;
        prefs_page->enable_java = enable_java;
        prefs_page->open_external = optmenu_open_external;
+       prefs_page->stylesheet = stylesheet;
        prefs_page->page.widget = vbox;
 
        g_signal_connect(G_OBJECT(prefs_page->enable_remote_content), "toggled",
@@ -300,6 +341,38 @@ static void create_fancy_prefs_page(PrefsPage *page, GtkWindow *window,
        remote_content_set_labels_cb(NULL, prefs_page);
 }
 
+static void fancy_prefs_stylesheet_browse_cb(GtkWidget *widget, gpointer data)
+{
+       gchar *filename;
+       gchar *utf8_filename;
+       GtkEntry *dest = GTK_ENTRY(data);
+
+       filename = filesel_select_file_open(_("Select stylesheet"), NULL);
+       if (!filename) return;
+
+       utf8_filename = g_filename_to_utf8(filename, -1, NULL, NULL, NULL);
+       if (!utf8_filename) {
+               g_warning("fancy_prefs_stylesheet_browse_cb(): failed to convert character set.");
+               utf8_filename = g_strdup(filename);
+       }
+       gtk_entry_set_text(GTK_ENTRY(dest), utf8_filename);
+       g_free(utf8_filename);
+}
+
+static void fancy_prefs_stylesheet_edit_cb(GtkWidget *widget, gpointer data)
+{
+       const gchar *stylesheet = gtk_entry_get_text(GTK_ENTRY(data));
+       if (!is_file_exist(stylesheet))
+               str_write_to_file(stylesheet, "");
+       open_txt_editor(stylesheet, prefs_common_get_ext_editor_cmd());
+}
+
+static void fancy_prefs_stylesheet_changed_cb(GtkWidget *widget, gpointer data)
+{
+       const gchar *stylesheet = gtk_entry_get_text(GTK_ENTRY(widget));
+       gtk_widget_set_sensitive(GTK_WIDGET(data), (*stylesheet)? TRUE: FALSE);
+}
+
 static void prefs_set_proxy_entry_sens(GtkWidget *button, GtkEntry *entry_str) {
        gtk_widget_set_sensitive(GTK_WIDGET(entry_str),
                                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
@@ -334,7 +407,7 @@ static void save_fancy_prefs(PrefsPage *page)
                return;
        
        if (prefs_write_param(param, pref_file->fp) < 0) {
-               g_warning("failed to write Fancy Plugin configuration\n");
+               g_warning("failed to write Fancy Plugin configuration");
                prefs_file_close_revert(pref_file);
                return;
        }
@@ -369,6 +442,15 @@ static void save_fancy_prefs_page(PrefsPage *page)
                fancy_prefs.enable_proxy = gtk_toggle_button_get_active
                                (GTK_TOGGLE_BUTTON(prefs_page->proxy_checkbox));
                fancy_prefs.proxy_str = pref_get_pref_from_entry(GTK_ENTRY(prefs_page->proxy_str));
+#ifdef G_OS_WIN32
+               /* pref_get_pref_from_entry() escapes the backslashes in strings,
+                * we do not want that, since this entry contains a Windows path.
+                * Let's just strdup it. */
+               fancy_prefs.stylesheet = g_strdup(gtk_entry_get_text(
+                                       GTK_ENTRY(prefs_page->stylesheet)));
+#else
+               fancy_prefs.stylesheet = pref_get_pref_from_entry(GTK_ENTRY(prefs_page->stylesheet));
+#endif
 
                save_fancy_prefs(page);
 }