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

static void check_username_as_nickname_toggled(GtkToggleButton *caller, gpointer data);
static void check_enable_irc_toggled(GtkToggleButton *caller, gpointer data);

void
ftd_gui_config_irc_new(FtdGuiConfig *config)
{
    FtdGuiConfigIRC *irc;
    GtkWidget *table1;
    GtkWidget *label2;
    GtkWidget *label3;
    GtkWidget *label1;

    g_assert(config);

    irc = g_malloc0(sizeof(FtdGuiConfigIRC));
    config->irc = irc;

    irc->widget = gtk_vbox_new(FALSE, 10);
    gtk_widget_show(irc->widget);

    irc->checkEnableIRCClient = gtk_check_button_new_with_mnemonic ("Activeer IRC Client (herstart vereist)");
    gtk_widget_show (irc->checkEnableIRCClient);
    gtk_box_pack_start (GTK_BOX (irc->widget), irc->checkEnableIRCClient, FALSE, FALSE, 0);

    if (ftd_config_get()->toonIRCClient) {
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (irc->checkEnableIRCClient), TRUE);
    }

    g_signal_connect(G_OBJECT(irc->checkEnableIRCClient), "toggled", G_CALLBACK(check_enable_irc_toggled), irc);

    irc->frameSettings = gtk_frame_new (NULL);
    if (ftd_config_get()->toonIRCClient) {
        gtk_widget_show (irc->frameSettings);
    }
    gtk_box_pack_start (GTK_BOX (irc->widget), irc->frameSettings, FALSE, FALSE, 0);
    gtk_container_set_border_width (GTK_CONTAINER (irc->frameSettings), 5);

    table1 = gtk_table_new (3, 2, FALSE);
    gtk_widget_show (table1);
    gtk_container_add (GTK_CONTAINER (irc->frameSettings), table1);
    gtk_container_set_border_width (GTK_CONTAINER (table1), 5);
    gtk_table_set_row_spacings (GTK_TABLE (table1), 5);
    gtk_table_set_col_spacings (GTK_TABLE (table1), 5);

    label2 = gtk_label_new ("Nickname :");
    gtk_widget_show (label2);
    gtk_table_attach (GTK_TABLE (table1), label2, 0, 1, 0, 1,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 0);
    gtk_misc_set_alignment (GTK_MISC (label2), 0, 0.5);

    irc->entryNickname = gtk_entry_new ();
    gtk_widget_show (irc->entryNickname);
    gtk_table_attach (GTK_TABLE (table1), irc->entryNickname, 1, 2, 0, 1,
                        (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                        (GtkAttachOptions) (0), 0, 0);
    if (!ftd_config_get()->ircNickname) {
        gtk_widget_set_sensitive (irc->entryNickname, FALSE);
    } else {
        gtk_entry_set_text(GTK_ENTRY(irc->entryNickname), ftd_string_value(ftd_config_get()->ircNickname));
    }

    label3 = gtk_label_new ("Alternatieve nickname :");
    gtk_widget_show (label3);
    gtk_table_attach (GTK_TABLE (table1), label3, 0, 1, 1, 2,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 0);
    gtk_misc_set_alignment (GTK_MISC (label3), 0, 0.5);

    irc->entryAltNickname = gtk_entry_new ();
    gtk_widget_show (irc->entryAltNickname);
    gtk_table_attach (GTK_TABLE (table1), irc->entryAltNickname, 1, 2, 1, 2,
                        (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                        (GtkAttachOptions) (0), 0, 0);
    gtk_entry_set_text(GTK_ENTRY(irc->entryAltNickname), ftd_string_value(ftd_config_get()->ircAltNickname));

    irc->checkUsernameAsNickname = gtk_check_button_new_with_mnemonic ("Gebruik FTD gebruikersnaam als nickname");
    gtk_widget_show (irc->checkUsernameAsNickname);
    gtk_table_attach (GTK_TABLE (table1), irc->checkUsernameAsNickname, 0, 2, 2, 3,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 0);
    if (!ftd_config_get()->ircNickname) {
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (irc->checkUsernameAsNickname), TRUE);
    }

    g_signal_connect(G_OBJECT(irc->checkUsernameAsNickname), "toggled", G_CALLBACK(check_username_as_nickname_toggled), irc);

    label1 = gtk_label_new ("IRC Instellingen");
    gtk_widget_show (label1);
    gtk_frame_set_label_widget (GTK_FRAME (irc->frameSettings), label1);
    gtk_label_set_use_markup (GTK_LABEL (label1), TRUE);
}

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

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

static void
check_username_as_nickname_toggled(GtkToggleButton *caller, gpointer data)
{
    FtdGuiConfigIRC *irc = (FtdGuiConfigIRC *) data;

    if (gtk_toggle_button_get_active(caller)) {
        // We zijn ingedrukt
        gtk_widget_set_sensitive (irc->entryNickname, FALSE);
        gtk_entry_set_text(GTK_ENTRY(irc->entryNickname), "");
    } else {
        // We zijn NIET ingedrukt
        gtk_widget_set_sensitive (irc->entryNickname, TRUE);
    }
}

static void
check_enable_irc_toggled(GtkToggleButton *caller, gpointer data)
{
    FtdGuiConfigIRC *irc = (FtdGuiConfigIRC *) data;

    if (gtk_toggle_button_get_active(caller)) {
        // We zijn ingedrukt
        gtk_widget_show(irc->frameSettings);
    } else {
        // We zijn NIET ingedrukt
        gtk_widget_hide(irc->frameSettings);
    }
}
