/*
 *  Copyright (C) 2004 Morten Fjord-Larsen
 *
 *  $Id: passwordchange.c,v 1.4 2004/05/05 21:50:17 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 "passwordfile.h"
#include "passwordlist.h"
#include "msgdialog.h"

static GladeXML *xml;

static GtkWidget *password_change_dialog;

void password_change_dialog_setup()
{
}


void password_change_dialog_display()
{
    xml = glade_xml_new(PACKAGE_DATA_DIR"/change-password-dialog.glade",
	    NULL, NULL);

    password_change_dialog = glade_xml_get_widget(xml,"change_password_dialog");
    g_assert(password_change_dialog != NULL);

    glade_xml_signal_autoconnect(xml);
    gtk_widget_show(password_change_dialog);
}


void password_change_dialog_on_entering_text()
{
}


void password_change_dialog_on_okbutton_activate()
{
    GtkEntry *old_entry, *entry, *entry2;
    const gchar *old_password, *password1, *password2;

    old_entry = GTK_ENTRY(glade_xml_get_widget(xml, "old_password_entry"));
    g_assert(old_entry != NULL);

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

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

    old_password = gtk_entry_get_text(old_entry);
    g_assert(old_password != NULL);


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

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

    if (strlen(password1) < 1) {
	gtk_widget_grab_focus(GTK_WIDGET(entry));
	msg_dialog_display(_("Empty passwords not allowed!"));
    }
    else if (strcmp(password_list_get_password(), old_password)) {
	gtk_widget_grab_focus(GTK_WIDGET(old_entry));
	msg_dialog_display(_("Incorrect password! Please try again."));
    } 
    else if (strcmp(password1, password2)) {
	gtk_widget_grab_focus(GTK_WIDGET(entry));
	msg_dialog_display(_("The entered passwords do not match. Please try again."));
    } else {
	password_list_set_password(password1);
	password_list_set_dirty(TRUE);
	gtk_widget_destroy(password_change_dialog);
    }
}

