/* ====================================================================
 * 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 "ftd4linux.h"
#include "ftd_gui_config.h"

void
ftd_gui_config_badwords_new(FtdGuiConfig *config)
{
    FtdGuiConfigBadwords *badwords;
    GtkWidget *hbox1;
    GtkWidget *vbox1;
    GtkWidget *btnToevoegenBadword;
    GtkWidget *scrolledwindow1;
    GtkWidget *label1;
    GtkCellRenderer *cellRenderer;
    GtkTreeViewColumn *column;
    GtkTreeSelection *select;
    
    g_assert(config);
    
    badwords = g_malloc(sizeof(FtdGuiConfigBadwords));
    config->badwords = badwords;
    badwords->selectedWord = NULL;
    
    badwords->widget = gtk_frame_new("Bad Words");
    gtk_widget_show (badwords->widget);
    
    vbox1 = gtk_vbox_new (FALSE, 0);
    gtk_container_add(GTK_CONTAINER (badwords->widget), vbox1);
    gtk_widget_show (vbox1);

    gtk_container_set_border_width (GTK_CONTAINER (badwords->widget), 5);
    gtk_container_set_border_width (GTK_CONTAINER (vbox1), 5);
    
    hbox1 = gtk_hbox_new (FALSE, 0);
    gtk_widget_show (hbox1);
    gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, TRUE, 5);

    label1 = gtk_label_new("Bad word : ");
    gtk_widget_show (label1);
    gtk_box_pack_start (GTK_BOX (hbox1), label1, FALSE, FALSE, 0);
    
    badwords->entryBadword = gtk_entry_new ();
    gtk_widget_show (badwords->entryBadword);
    gtk_box_pack_start (GTK_BOX (hbox1), badwords->entryBadword, FALSE, FALSE, 0);
    
    btnToevoegenBadword = gtk_button_new_with_mnemonic ("Toevoegen");
    gtk_widget_show (btnToevoegenBadword);
    gtk_box_pack_start (GTK_BOX (hbox1), btnToevoegenBadword, FALSE, TRUE, 3);

    scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
    gtk_widget_show (scrolledwindow1);
    gtk_box_pack_start (GTK_BOX (vbox1), scrolledwindow1, TRUE, TRUE, 5);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow1), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
    gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwindow1), GTK_SHADOW_IN);
    
    badwords->treestoreBadwords = gtk_tree_store_new(1, G_TYPE_STRING);
        
    badwords->treeviewBadwords = gtk_tree_view_new_with_model(GTK_TREE_MODEL(badwords->treestoreBadwords));
    gtk_container_add (GTK_CONTAINER (scrolledwindow1), badwords->treeviewBadwords);
    cellRenderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes ("Bad words", cellRenderer, "text", 0, NULL);
    gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
    gtk_tree_view_insert_column (GTK_TREE_VIEW (badwords->treeviewBadwords), column, -1);
    gtk_widget_show (badwords->treeviewBadwords);
    
    select = gtk_tree_view_get_selection(GTK_TREE_VIEW (badwords->treeviewBadwords));
    gtk_tree_selection_set_mode (select, GTK_SELECTION_SINGLE);
    g_signal_connect (G_OBJECT (select), "changed",
                      G_CALLBACK (ftd_gui_config_badwords_select), config);

    badwords->btnVerwijderBadword = gtk_button_new_with_mnemonic ("Verwijderen");
    gtk_widget_show (badwords->btnVerwijderBadword);
    gtk_widget_set_sensitive(badwords->btnVerwijderBadword, FALSE);
    gtk_box_pack_start (GTK_BOX (vbox1), badwords->btnVerwijderBadword, FALSE, FALSE, 0);
    
    g_signal_connect((gpointer) btnToevoegenBadword, "clicked",
        G_CALLBACK(ftd_gui_config_badwords_toevoegen), config);
    g_signal_connect((gpointer) badwords->btnVerwijderBadword, "clicked",
        G_CALLBACK(ftd_gui_config_badwords_verwijder), config);
        
    ftd_gui_config_badwords_fill(config);
}

void
ftd_gui_config_badwords_free(FtdGuiConfig *config)
{
    g_assert(config);
 
    g_free(config->badwords);
    config->badwords = NULL;    
}

void
ftd_gui_config_badwords_fill(FtdGuiConfig *config)
{
    FtdList *list;
    GtkTreeStore *treeStore;
    GtkTreeIter treeIter;
    int ret = 0;
    
    g_assert(config);
    
    treeStore = config->badwords->treestoreBadwords;
    gtk_tree_store_clear(treeStore);
    
    list = ftd_config_get()->badwords;
    while (list) {
        FtdString *badword = ftd_list_value(list);
        gtk_tree_store_insert (treeStore, &treeIter, NULL, ret);
        gtk_tree_store_set (treeStore, &treeIter,
                        0, ftd_string_value(badword),
                        -1);
        list = ftd_list_next(list);
    }
}

void
ftd_gui_config_badwords_select(GtkTreeSelection *selection, gpointer data)
{
    FtdGuiConfig *config = (FtdGuiConfig *) data;
    GtkTreeModel *model;
    GtkTreeIter iter;
    
    g_assert(config);
    g_assert(config->badwords);
    
    config->badwords->selectedWord = NULL;
    
    if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
        gchar *listword;
        FtdList *list;
        
        gtk_tree_model_get(model, &iter, 0, &listword, -1);
        list = ftd_config_get()->badwords;
        while (list) {
            FtdString *badword = ftd_list_value(list);
            if (strcmp(listword, ftd_string_value(badword)) == 0) {
                config->badwords->selectedWord = badword;
            }
            list = ftd_list_next(list);
        }
        g_free(listword);
        gtk_widget_set_sensitive(config->badwords->btnVerwijderBadword, TRUE);
    } else {
        gtk_widget_set_sensitive(config->badwords->btnVerwijderBadword, FALSE);
    }
}

gboolean
ftd_gui_config_badwords_toevoegen(GtkWidget *caller, gpointer data)
{
    FtdGuiConfig *config = (FtdGuiConfig *) data;
    G_CONST_RETURN gchar *newWord;
    FtdList *list;
    
    g_assert(config);
    g_assert(config->badwords);
    
    newWord = gtk_entry_get_text(GTK_ENTRY(config->badwords->entryBadword));

    if (!strcmp(newWord, "")) {
        GtkWidget *dialog;
        dialog = gtk_message_dialog_new (GTK_WINDOW (config->window),
                                  GTK_DIALOG_DESTROY_WITH_PARENT,
                                  GTK_MESSAGE_ERROR,
                                  GTK_BUTTONS_CLOSE,
                                  "Bad word is leeg");
        gtk_dialog_run (GTK_DIALOG (dialog));
        gtk_widget_destroy (dialog);
        return FALSE;
    }
    
    // Controleer of het badword niet reeds voorkomt in de lijst
    list = ftd_config_get()->badwords;
    while (list) {
        FtdString *badword = ftd_list_value(list);
        if (strcasecmp(ftd_string_value(badword), newWord) == 0) {
            GtkWidget *dialog;
            dialog = gtk_message_dialog_new (GTK_WINDOW (config->window),
                                             GTK_DIALOG_DESTROY_WITH_PARENT,
                                             GTK_MESSAGE_ERROR,
                                             GTK_BUTTONS_CLOSE,
                                             "Ingevoerde woord komt reeds voor in de 'bad words' lijst");
            gtk_dialog_run (GTK_DIALOG (dialog));
            gtk_widget_destroy (dialog);
            return FALSE;
        }
        list = ftd_list_next(list);
    }

    ftd_config_get()->badwords = ftd_list_append(ftd_config_get()->badwords, ftd_string_new(newWord));
    gtk_entry_set_text(GTK_ENTRY(config->badwords->entryBadword), "");
    ftd_gui_config_badwords_fill(config);
        
    return FALSE;
}

gboolean
ftd_gui_config_badwords_verwijder(GtkWidget *caller, gpointer data)
{
    FtdGuiConfig *config = (FtdGuiConfig *) data;
    FtdString *selectedWord;
    
    g_assert(config);
    
    selectedWord = config->badwords->selectedWord;
    
    if (!selectedWord) {
        GtkWidget *dialog;
        dialog = gtk_message_dialog_new (GTK_WINDOW (config->window),
                                  GTK_DIALOG_DESTROY_WITH_PARENT,
                                  GTK_MESSAGE_ERROR,
                                  GTK_BUTTONS_CLOSE,
                                  "Er is geen 'bad word' geselecteerd");
        gtk_dialog_run (GTK_DIALOG (dialog));
        gtk_widget_destroy (dialog);
        return FALSE;
    }
    
    ftd_config_get()->badwords = ftd_list_remove(ftd_config_get()->badwords, selectedWord);
    ftd_string_free(selectedWord);
    ftd_gui_config_badwords_fill(config);
    
    return FALSE;
}
