/* ====================================================================
 * FTD4Linux Software License, Version 1.0
 *
 * Copyright (c) 2000-2004 The FTD4Linux Development Team.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        FTD4Linux Development Team (http://www.ftd4linux.2y.net/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "FTD" and "FTD4Linux" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact alguno@ftd4linux.2y.net.
 *
 * 5. Products derived from this software may not be called "FTD4Linux",
 *    nor may "FTD4Linux" appear in their name, without prior written
 *    permission of the FTD4Linux Development Team.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE FTD4LINUX DEVELOPMENT TEAM OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <ctype.h>
#include "ftd_gui_config.h"
#include "ftd_gui_compat_macros.h"

static void     ftd_gui_config_settings_groupFilter(GtkWidget *, gpointer);
static void     ftd_gui_config_settings_htmlRenderer(GtkWidget *, gpointer);

void
ftd_gui_config_settings_new(FtdGuiConfig *config, gboolean show_html_renderers)
{
    FtdGuiConfigSettings *settings;
    FtdList *plugins, *list;
    int i;
    GtkWidget *hbox;
    GtkWidget *label;

    g_assert(config);

    settings = g_malloc(sizeof(FtdGuiConfigSettings));
    config->settings = settings;

    settings->widget = gtk_vbox_new (FALSE, 10);
    gtk_widget_show (settings->widget);
    gtk_container_set_border_width (GTK_CONTAINER (settings->widget), 5);

    settings->toonInfoBijEenZoekresultaat = gtk_check_button_new_with_mnemonic ("Toon direct FTD posting info indien bij een\nzoekactie maar 1 resultaat is");
    gtk_widget_show (settings->toonInfoBijEenZoekresultaat);
    gtk_box_pack_start (GTK_BOX (settings->widget), settings->toonInfoBijEenZoekresultaat, FALSE, FALSE, 0);

    settings->updatenBijOpstarten = gtk_check_button_new_with_mnemonic ("Update de lijst met FTD postings bij het opstarten");
    gtk_widget_show (settings->updatenBijOpstarten);
    gtk_box_pack_start (GTK_BOX (settings->widget), settings->updatenBijOpstarten, FALSE, FALSE, 0);

    settings->startMaximized = gtk_check_button_new_with_mnemonic ("Start OpenFTD gemaximaliseerd");
    gtk_widget_show (settings->startMaximized);
    gtk_box_pack_start (GTK_BOX (settings->widget), settings->startMaximized, FALSE, FALSE, 0);

    settings->toonStatusBijLaden = gtk_check_button_new_with_mnemonic ("Toon status venster tijdens laden van HTML pagina");
    gtk_widget_show (settings->toonStatusBijLaden);
    gtk_box_pack_start (GTK_BOX (settings->widget), settings->toonStatusBijLaden, FALSE, FALSE, 0);

    settings->toonCategoriseerdeLijstBijKnopNieuw = gtk_check_button_new_with_mnemonic ("Toon categoriseerde lijst indien op de knop 'Nieuw' gedrukt wordt");
    gtk_widget_show (settings->toonCategoriseerdeLijstBijKnopNieuw);
    gtk_box_pack_start (GTK_BOX (settings->widget), settings->toonCategoriseerdeLijstBijKnopNieuw, FALSE, FALSE, 0);

#ifndef DARWIN
    settings->trayIcon = gtk_check_button_new_with_mnemonic("Activeer tray icon functionaliteit");
    gtk_widget_show(settings->trayIcon);
    gtk_box_pack_start (GTK_BOX (settings->widget), settings->trayIcon, FALSE, FALSE, 0);
#endif

    settings->toonGUIGroupFilter = gtk_check_button_new_with_mnemonic("Toon nieuwsgroepenfilter in hoofdvenster");
    gtk_widget_show(settings->toonGUIGroupFilter);
    gtk_box_pack_start (GTK_BOX (settings->widget), settings->toonGUIGroupFilter, FALSE, FALSE, 0);

    settings->groupFilter = gtk_check_button_new_with_mnemonic("Activeer groepsfilter functionaliteit");
    gtk_widget_show(settings->groupFilter);
    gtk_box_pack_start (GTK_BOX (settings->widget), settings->groupFilter, FALSE, FALSE, 0);

    settings->recursiveUpdate = gtk_check_button_new_with_mnemonic("Activeer recursieve update functionaliteit");
    gtk_widget_show(settings->recursiveUpdate);
    gtk_box_pack_start (GTK_BOX (settings->widget), settings->recursiveUpdate, FALSE, FALSE, 0);

    settings->geenTabbedBrowsing = gtk_check_button_new_with_mnemonic("Schakel tabbed browsing uit voor de postinfo");
    gtk_widget_show(settings->geenTabbedBrowsing);
    gtk_box_pack_start (GTK_BOX (settings->widget), settings->geenTabbedBrowsing, FALSE, FALSE, 0);

    settings->toonWatcherNotifications = gtk_check_button_new_with_mnemonic("Toon OpenFTD Watcher notifications");
    gtk_widget_show(settings->toonWatcherNotifications);
    gtk_box_pack_start (GTK_BOX (settings->widget), settings->toonWatcherNotifications, FALSE, FALSE, 0);
#ifndef HAVE_LIBNOTIFY
    gtk_widget_set_sensitive(settings->toonWatcherNotifications, FALSE);
#endif

    settings->openPostinfoOpAchtergrond = gtk_check_button_new_with_mnemonic("Open de postinformatie op de achtergrond");
    gtk_widget_show(settings->openPostinfoOpAchtergrond);
    gtk_box_pack_start (GTK_BOX (settings->widget), settings->openPostinfoOpAchtergrond, FALSE, FALSE, 0);

    if (show_html_renderers) {
        settings->gebruikFontFamily = gtk_check_button_new_with_mnemonic("Gebruik oude lettertype-instellingen (herstart vereist)");
        gtk_widget_show(settings->gebruikFontFamily);
        gtk_box_pack_start (GTK_BOX (settings->widget), settings->gebruikFontFamily, FALSE, FALSE, 0);
        gtk_widget_set_sensitive(settings->gebruikFontFamily, FALSE);       // Alleen bij de mozilla backend mag deze optie aan staan

        hbox = gtk_hbox_new(TRUE, 0);
        gtk_widget_show(hbox);
        gtk_box_pack_start (GTK_BOX (settings->widget), hbox, FALSE, FALSE, 0);

        label = gtk_label_new("HTML Renderer (herstart vereist) :");
        gtk_widget_show(label);
        gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);

        settings->htmlRenderer = gtk_combo_box_new_text();
        gtk_widget_show(settings->htmlRenderer);
        gtk_box_pack_start (GTK_BOX (hbox), settings->htmlRenderer, TRUE, TRUE, 5);

        plugins = ftd_gui_config_get_html_renderers();
        list = plugins;
        if (plugins == NULL) {
            gtk_combo_box_append_text(GTK_COMBO_BOX(settings->htmlRenderer), "Geen bruikbare HTML renderers gevonden!");
            gtk_combo_box_set_active(GTK_COMBO_BOX(settings->htmlRenderer), 0);
        }

        i = 0;
        while (list) {
            FtdGuiConfigHTMLRenderer *item = ftd_list_value(list);

            gtk_combo_box_append_text(GTK_COMBO_BOX(settings->htmlRenderer), item->name);

            if (i == 0) {
                gtk_combo_box_set_active(GTK_COMBO_BOX(settings->htmlRenderer), i);
            }

            if (strcmp(ftd_string_value(ftd_config_get()->htmlRenderer), item->name) == 0) {
                gtk_combo_box_set_active(GTK_COMBO_BOX(settings->htmlRenderer), i);

                if (strcmp(item->name, "Mozilla") == 0) {
                    gtk_widget_set_sensitive(settings->gebruikFontFamily, TRUE);       // Alleen bij de mozilla backend mag deze optie aan staan
                }
            }

            i++;
            list = ftd_list_next(list);
        }

        ftd_gui_config_free_html_renderers(plugins);
    }

    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(config->settings->toonInfoBijEenZoekresultaat), ftd_config_get()->toonInfoBijEenZoekresultaat);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(config->settings->updatenBijOpstarten), ftd_config_get()->updatenBijOpstarten);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(config->settings->startMaximized), ftd_config_get()->startMaximized);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(config->settings->toonStatusBijLaden), ftd_config_get()->toonStatusBijLaden);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(config->settings->toonCategoriseerdeLijstBijKnopNieuw), ftd_config_get()->toonCategoriseerdeLijstBijKnopNieuw);
#ifndef DARWIN
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(config->settings->trayIcon), ftd_config_get()->trayIcon);
#endif
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(config->settings->toonGUIGroupFilter), ftd_config_get()->toonGUIGroupFilter);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(config->settings->groupFilter), ftd_config_get()->groupFilter);
    if (show_html_renderers) {
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(config->settings->gebruikFontFamily), ftd_config_get()->gebruikFontFamily);
    }
#ifdef HAVE_LIBNOTIFY
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(config->settings->toonWatcherNotifications), ftd_config_get()->toonWatcherNotifications);
#endif
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(config->settings->recursiveUpdate), ftd_config_get()->recursiveUpdate);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(config->settings->geenTabbedBrowsing), ftd_config_get()->geenTabbedBrowsing);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(config->settings->openPostinfoOpAchtergrond), ftd_config_get()->openPostinfoOpAchtergrond);

    g_signal_connect((gpointer)config->settings->groupFilter, "toggled",
                     G_CALLBACK(ftd_gui_config_settings_groupFilter), config);

    if (show_html_renderers) {
        g_signal_connect((gpointer)config->settings->htmlRenderer, "changed",
                         G_CALLBACK(ftd_gui_config_settings_htmlRenderer), config);
    }
}

void
ftd_gui_config_settings_free(FtdGuiConfig *config)
{
    g_assert(config);

    g_free(config->settings);
    config->settings = NULL;
}

static void
ftd_gui_config_settings_groupFilter(GtkWidget *caller, gpointer data)
{
    FtdGuiConfig *config = (FtdGuiConfig *) data;

    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(config->settings->groupFilter))) {
        GtkWidget *label;
        ftd_gui_config_groupfilter_new(config);
        label = gtk_label_new("Groepsfilter");
        gtk_widget_show(label);
        gtk_notebook_append_page(GTK_NOTEBOOK(config->notebook), config->groupfilter->widget, label);
    } else {
        ftd_gui_config_groupfilter_free(config);
    }
}

static void
ftd_gui_config_settings_htmlRenderer(GtkWidget *caller, gpointer data)
{
    FtdGuiConfig *config = (FtdGuiConfig *) data;
    char *text = gtk_combo_box_get_active_text(GTK_COMBO_BOX(caller));

    if (strcmp(text, "Mozilla") == 0) {
        gtk_widget_set_sensitive(config->settings->gebruikFontFamily, TRUE);
    } else {
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(config->settings->gebruikFontFamily), FALSE);
        gtk_widget_set_sensitive(config->settings->gebruikFontFamily, FALSE);
    }
}
