Fancy: disallow editing for empty stylesheets
[claws.git] / src / plugins / fancy / fancy_prefs.c
index 533cbee211b6c5c40e1a3e7da7f34a5601a40b8b..3ab1f555c0545068dbe57bc89152db153e103a53 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-2014 the Claws Mail Team
  * == Fancy Plugin ==
- * This file Copyright (C) 2009-2013 Salvatore De Paolis
+ * This file Copyright (C) 2009-2014 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);
@@ -160,10 +165,10 @@ static void remote_content_set_labels_cb(GtkWidget *button, FancyPrefsPage *pref
        model = gtk_combo_box_get_model(GTK_COMBO_BOX(prefs_page->open_external));
        if (gtk_tree_model_get_iter_first (model, &iter)) {
                if (remote_enabled)
-                       gtk_list_store_set(model, &iter, COMBOBOX_TEXT,
+                       gtk_list_store_set(GTK_LIST_STORE(model), &iter, COMBOBOX_TEXT,
                                           _("Open in viewer (remote content is enabled)"), -1);
                else
-                       gtk_list_store_set(model, &iter, COMBOBOX_TEXT,
+                       gtk_list_store_set(GTK_LIST_STORE(model), &iter, COMBOBOX_TEXT,
                                           _("Do nothing (remote content is disabled)"), -1);
        }
 
@@ -190,6 +195,10 @@ 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);
@@ -202,8 +211,6 @@ static void create_fancy_prefs_page(PrefsPage *page, GtkWindow *window,
        gnome_proxy_checkbox = gtk_check_button_new_with_label(_("Use GNOME's proxy settings"));        
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gnome_proxy_checkbox),
                                     fancy_prefs.enable_gnome_proxy);
-       if (fancy_prefs.enable_gnome_proxy)
-               gtk_widget_set_sensitive(proxy_checkbox, FALSE);
        gtk_box_pack_start(GTK_BOX(vbox_proxy), gnome_proxy_checkbox, FALSE, FALSE, 0);
        gtk_widget_show(gnome_proxy_checkbox);
        g_signal_connect(G_OBJECT(gnome_proxy_checkbox), "toggled",
@@ -211,6 +218,10 @@ static void create_fancy_prefs_page(PrefsPage *page, GtkWindow *window,
 #endif
        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)
+               gtk_widget_set_sensitive(proxy_checkbox, FALSE);
+#endif
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(proxy_checkbox),
                                     fancy_prefs.enable_proxy);
        prefs_set_proxy_entry_sens(proxy_checkbox, GTK_ENTRY(proxy_str));
@@ -219,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, TRUE, TRUE, 0);
+       gtk_box_pack_start(GTK_BOX(block), proxy_str, FALSE, TRUE, 0);
        gtk_box_pack_start(GTK_BOX(vbox_proxy), block, FALSE, FALSE, 0);
        gtk_widget_show_all(vbox_proxy);
 
@@ -279,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;
@@ -291,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",
@@ -298,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)));
@@ -367,6 +442,7 @@ 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));
+               fancy_prefs.stylesheet = pref_get_pref_from_entry(GTK_ENTRY(prefs_page->stylesheet));
 
                save_fancy_prefs(page);
 }