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

void
ftd_gui_config_plugin_new(FtdGuiConfig *config)
{
    GtkWidget 	*frame4;
    GtkWidget 	*button;
    GtkWidget 	*label6;
    GtkWidget	*label17;
    GtkWidget   *table;
    GtkWidget   *align;

    FtdGuiConfigPlugin *plugin;
    g_assert(config);

    plugin = g_malloc(sizeof(FtdGuiConfigPlugin));
    config->plugin = plugin;
    config->plugin->filesel = NULL;
    
    plugin->widget = gtk_vbox_new (FALSE, 0);
    gtk_widget_show (plugin->widget); 
    gtk_container_set_border_width (GTK_CONTAINER (plugin->widget), 5);
    
    frame4 = gtk_frame_new (NULL);
    gtk_widget_show (frame4);
    gtk_box_pack_start (GTK_BOX (plugin->widget), frame4, FALSE, FALSE, 0);

    label6 = gtk_label_new ("Authenticatie Plugin:");
    gtk_widget_show (label6);
    gtk_frame_set_label_widget (GTK_FRAME (frame4), label6);
    gtk_label_set_justify (GTK_LABEL (label6), GTK_JUSTIFY_LEFT);
    
    table = gtk_table_new(2,2,FALSE);
    gtk_widget_show(table);
    gtk_container_add (GTK_CONTAINER (frame4), table);

    align = gtk_alignment_new(0,1,0,0);
    label17 = gtk_label_new ("Plugin locatie:");
    gtk_widget_show (label17);
    gtk_container_add (GTK_CONTAINER (align), label17);
    gtk_widget_show (align);
    gtk_table_attach(GTK_TABLE(table), align, 0,1,0,1,
                     (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) NULL, 3, 3);
    
    plugin->authLocation = gtk_entry_new();
    gtk_widget_show(plugin->authLocation);
    gtk_table_attach(GTK_TABLE(table), plugin->authLocation, 1,2,0,1,
                     (GtkAttachOptions) NULL, (GtkAttachOptions) NULL, 3, 3);
    if (ftd_config_get()->authPlugin) {
        gtk_entry_set_text(GTK_ENTRY(plugin->authLocation), 
			ftd_string_value(ftd_config_get()->authPlugin));
    }
    button = gtk_button_new_with_label("...");
    gtk_widget_show(button);
    gtk_table_attach(GTK_TABLE(table), button, 2,3,0,1,
                     (GtkAttachOptions) NULL, (GtkAttachOptions) NULL, 3, 3);

    g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (ftd_gui_config_plugin_callback_find), config);
}

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

    g_free(config->plugin);
    config->plugin = NULL;
}
 
void
ftd_gui_config_plugin_callback_find(GtkWidget *caller, gpointer data)
{
    FtdGuiConfig *config = (FtdGuiConfig *) data;

    g_assert(caller);
    g_assert(config);

    config->plugin->filesel = gtk_file_selection_new("Selecteer authenticatie plugin.");
    g_signal_connect(GTK_FILE_SELECTION(config->plugin->filesel)->ok_button, "clicked", G_CALLBACK(ftd_gui_config_plugin_callback_find_ok), config);
    g_signal_connect(GTK_FILE_SELECTION(config->plugin->filesel)->cancel_button, "clicked", G_CALLBACK(ftd_gui_config_plugin_callback_find_close), config);
    gtk_widget_show(config->plugin->filesel);

    /* dammit. nu kunnen we niet meer modal zijn.. */
    gtk_window_set_modal(GTK_WINDOW(config->window), FALSE);
}


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

    g_assert(caller);
    g_assert(config);

    gtk_widget_destroy(config->plugin->filesel);
    config->plugin->filesel = NULL;

    /* gelijk modal terug pakken */
    gtk_window_set_modal(GTK_WINDOW(config->window), TRUE);
}

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

    g_assert(caller);
    g_assert(config);

    gtk_entry_set_text(GTK_ENTRY(config->plugin->authLocation), gtk_file_selection_get_filename(GTK_FILE_SELECTION(config->plugin->filesel)));
    gtk_widget_destroy(config->plugin->filesel);
    config->plugin->filesel = NULL;

    /* gelijk modal terug pakken */
    gtk_window_set_modal(GTK_WINDOW(config->window), TRUE);
}
