/*
 *  Copyright (C) 2004 Morten Fjord-Larsen
 *
 *  $Id: passwordentry.c,v 1.1.1.1 2004/04/26 23:58:52 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 "passwordlist.h"
#include "generate.h"

static GladeXML *xml;


/* 
 * The right way to do this, would probably be to create a new widget 
 * derived from GtkDialog.
 */

static GtkWidget *password_entry_dialog;
static gboolean password_entry_do_edit;
static GtkEntry *title_entry;
static GtkEntry *username_entry;
static GtkEntry *password_entry;
static GtkEntry *url_entry;
static GnomeDateEdit *creation_dateedit;
static GnomeDateEdit *modification_dateedit;
static GnomeDateEdit *expiration_dateedit;
static GtkTextView *description_textview;
static GtkCheckButton *expires_checkbutton;
static GtkNotebook *notebook;

void password_entry_setup()
{
    GtkTextBuffer *buffer;

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

    password_entry_dialog = glade_xml_get_widget(xml, "password_entry_dialog");
    g_assert(password_entry_dialog != NULL);

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

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

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

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

    creation_dateedit = GNOME_DATE_EDIT(glade_xml_get_widget(xml,
		"creation_dateedit"));
    g_assert(creation_dateedit);
    modification_dateedit = GNOME_DATE_EDIT(glade_xml_get_widget(xml,
		"modification_dateedit"));
    g_assert(modification_dateedit);
    expiration_dateedit = GNOME_DATE_EDIT(glade_xml_get_widget(xml,
		"expiration_dateedit"));
    g_assert(expiration_dateedit);

    description_textview = GTK_TEXT_VIEW(glade_xml_get_widget(xml,
		"description_textview"));
    g_assert(description_textview != NULL);

    buffer = gtk_text_buffer_new(NULL);
    gtk_text_view_set_buffer(description_textview, buffer);

    expires_checkbutton = GTK_CHECK_BUTTON(glade_xml_get_widget(xml,
		"expires_checkbutton"));
    g_assert(expires_checkbutton != NULL);

    notebook = GTK_NOTEBOOK(glade_xml_get_widget(xml, "notebook"));
    g_assert(notebook != NULL);
}


void password_entry_new() 
{
    time_t current_time = time(&current_time);
    GtkTextBuffer *buffer;

    gtk_notebook_set_current_page(notebook, 0);

    gtk_entry_set_text(title_entry, "");
    gtk_entry_set_text(username_entry, "");
    gtk_entry_set_text(password_entry, "");
    gtk_entry_set_text(url_entry, "http://");

    gnome_date_edit_set_time(creation_dateedit, current_time);
    gnome_date_edit_set_time(modification_dateedit, current_time);
    gnome_date_edit_set_time(expiration_dateedit, current_time);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(expires_checkbutton), FALSE);

    buffer = gtk_text_view_get_buffer(description_textview);
    gtk_text_buffer_set_text(buffer, "", 0);

    password_entry_do_edit = FALSE;

    gtk_widget_show(password_entry_dialog);
    gtk_widget_grab_focus(GTK_WIDGET(title_entry));
}


void password_entry_on_expires_toggled (GtkToggleButton *togglebutton,
                                        gpointer user_data)
{
    gboolean active = gtk_toggle_button_get_active(togglebutton);
    gtk_widget_set_sensitive(GTK_WIDGET(expiration_dateedit), active);
}


void password_entry_edit(const password_file_entry *entry) 
{
    time_t current_time = time(&current_time);

    GtkTextBuffer *buffer;

    gtk_notebook_set_current_page(notebook, 0);

    gtk_entry_set_text(title_entry, entry->title);
    gtk_entry_set_text(username_entry, entry->username);
    gtk_entry_set_text(password_entry, entry->password);
    gtk_entry_set_text(url_entry, entry->url);

    gnome_date_edit_set_time(creation_dateedit, entry->creation_time);
    gnome_date_edit_set_time(modification_dateedit, current_time);
    if (entry->expiration_time) {
	gnome_date_edit_set_time(expiration_dateedit, entry->expiration_time);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(expires_checkbutton), 
				    TRUE);
    } else {
	gnome_date_edit_set_time(expiration_dateedit, current_time);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(expires_checkbutton), 
				    FALSE);
    }

    buffer = gtk_text_view_get_buffer(description_textview);
    g_assert(buffer != NULL);
    gtk_text_buffer_set_text(buffer, entry->description, -1);

    password_entry_do_edit = TRUE;

    gtk_widget_show(password_entry_dialog);
    gtk_widget_grab_focus(GTK_WIDGET(title_entry));
}


void password_entry_on_okbutton_activate(GtkWidget *widget)
{
    password_file_entry entry;
    GtkTextBuffer *buffer;
    GtkTextIter start, end;

    entry.title = g_strdup(gtk_entry_get_text(title_entry));
    entry.username = g_strdup(gtk_entry_get_text(username_entry));
    entry.password = g_strdup(gtk_entry_get_text(password_entry));
    entry.url = g_strdup(gtk_entry_get_text(url_entry));

    entry.creation_time = gnome_date_edit_get_time(creation_dateedit);
    entry.modification_time = gnome_date_edit_get_time(modification_dateedit);
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(expires_checkbutton)))
	entry.expiration_time = gnome_date_edit_get_time(expiration_dateedit);
    else
	entry.expiration_time = 0;

    buffer = gtk_text_view_get_buffer(description_textview);
    gtk_text_buffer_get_start_iter(buffer, &start);
    gtk_text_buffer_get_end_iter(buffer, &end);
    entry.description = gtk_text_buffer_get_text(buffer,&start,&end,FALSE);
    password_list_enter(&entry, password_entry_do_edit);
    password_file_entry_destroy(&entry);

    gtk_widget_hide(password_entry_dialog);
}


void password_entry_on_generate_button_activated()
{
    const char *password = generate_display_dialog();
    if (password != NULL)
	gtk_entry_set_text(password_entry, password);
}
