/*
 *  Copyright (C) 2004 Morten Fjord-Larsen
 *
 *  $Id: passworddialog.c,v 1.2 2004/04/30 11:11:04 fjord Exp $
 *
 *  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
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gnome.h>
#include <glade/glade.h>

#include "passworddialog.h"
#include "rundialog.h"
#include "msgdialog.h"

static GladeXML *xml;

void password_dialog_display(PasswordDialogCheckPassword check)
{
    GtkWidget *dialog;

    g_return_if_fail(check != NULL);

    xml = glade_xml_new(PACKAGE_DATA_DIR"/master-password-dialog.glade",
	    	        NULL, NULL);

    glade_xml_signal_autoconnect(xml);

    dialog = glade_xml_get_widget(xml, "master_password_dialog");
    gtk_object_set_user_data(GTK_OBJECT(dialog), check);
    gtk_widget_show(dialog);
}


void password_create_dialog_display(PasswordDialogCheckPassword callback) 
{
    GtkWidget *dialog;

    xml = glade_xml_new(PACKAGE_DATA_DIR"/create-password-dialog.glade",
	    	        NULL, NULL);

    glade_xml_signal_autoconnect(xml);

    dialog = glade_xml_get_widget(xml, "create_password_dialog");
    gtk_object_set_user_data(GTK_OBJECT(dialog), callback);
    gtk_widget_show(dialog);
}


void password_create_dialog_on_okbutton_clicked()
{
    GtkWidget *dialog;
    GtkEntry *entry, *entry2;
    const gchar *password1;
    const gchar *password2;

    dialog = glade_xml_get_widget(xml,"create_password_dialog");

    entry = GTK_ENTRY(glade_xml_get_widget(xml, "create_password_entry"));
    g_assert(entry != NULL);

    entry2 = GTK_ENTRY(glade_xml_get_widget(xml, "create_password_entry2"));
    g_assert(entry2 != NULL);

    password1 = gtk_entry_get_text(entry);
    g_assert(password1 != NULL);

    password2 = gtk_entry_get_text(entry2);
    g_assert(password2 != NULL);

    if (strcmp(password1, password2)) {
	msg_dialog_display(_("The entered passwords are not identical. Please try again."));
    } else {
	PasswordDialogCheckPassword callback;
	callback = gtk_object_get_user_data(GTK_OBJECT(dialog));
	g_assert(callback != NULL);
	callback(password1);
	gtk_widget_destroy(dialog);
    }
}


void password_dialog_on_okbutton_clicked (GtkEntry *password_entry) 
{
    GtkWidget *master_password_dialog;
    const gchar *password;
    PasswordDialogCheckPassword check;

    g_return_if_fail(password_entry != NULL);

    master_password_dialog = gtk_widget_get_toplevel(GTK_WIDGET(password_entry));

    check = gtk_object_get_user_data(GTK_OBJECT(master_password_dialog));
    g_assert(check != NULL);

    password = gtk_entry_get_text(password_entry);
    g_assert(password != NULL);

    if (!check(password)) {
	msg_dialog_display(_("Incorrect password!"));
	gtk_widget_grab_focus(GTK_WIDGET(password_entry));
    } else {
	gtk_widget_destroy(master_password_dialog);
    }
}
