/* ====================================================================
 * 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 <stdio.h>
#include "ftd_gui_config.h"
#include "ftd_gui_errhandler.h"

void
ftd_gui_signup_new(FtdGuiConfig *config)
{
    GtkWidget *vbox4;
    GtkWidget *label16;
    GtkWidget *table2;
    GtkWidget *label13;
    GtkWidget *label14;
    GtkWidget *label15;
    FtdGuiSignup *signup = (FtdGuiSignup *) g_malloc(sizeof(FtdGuiSignup));

    g_assert(config);

    config->signup = signup;

    signup->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (signup->window), "Nieuw FTD account aanmaken");
    gtk_window_set_position (GTK_WINDOW (signup->window), GTK_WIN_POS_CENTER_ALWAYS);
    gtk_window_set_modal (GTK_WINDOW (signup->window), TRUE);
    gtk_window_set_resizable (GTK_WINDOW (signup->window), FALSE);
    gtk_window_set_transient_for(GTK_WINDOW (signup->window), GTK_WINDOW(config->window));

    vbox4 = gtk_vbox_new (FALSE, 0);
    gtk_widget_show (vbox4);
    gtk_container_add (GTK_CONTAINER (signup->window), vbox4);
    gtk_container_set_border_width (GTK_CONTAINER (vbox4), 3);

    label16 = gtk_label_new ("Door onderstaand formulier in te vullen kan\n"
                             "je een nieuw FTD account aanmaken.");
    gtk_widget_show (label16);
    gtk_box_pack_start (GTK_BOX (vbox4), label16, TRUE, TRUE, 0);
    gtk_label_set_justify (GTK_LABEL (label16), GTK_JUSTIFY_CENTER);
    gtk_misc_set_padding (GTK_MISC (label16), 0, 5);

    table2 = gtk_table_new (3, 2, TRUE);
    gtk_widget_show (table2);
    gtk_box_pack_start (GTK_BOX (vbox4), table2, FALSE, TRUE, 0);
    gtk_container_set_border_width (GTK_CONTAINER (table2), 5);

    label13 = gtk_label_new ("Gebruikersnaam :");
    gtk_widget_show (label13);
    gtk_table_attach (GTK_TABLE (table2), label13, 0, 1, 0, 1,
                     (GtkAttachOptions) (GTK_FILL),
                     (GtkAttachOptions) (0), 0, 0);
    gtk_misc_set_alignment (GTK_MISC (label13), 0, 0.5);
    gtk_misc_set_padding (GTK_MISC (label13), 0, 10);

    label14 = gtk_label_new ("Wachtwoord");
    gtk_widget_show (label14);
    gtk_table_attach (GTK_TABLE (table2), label14, 0, 1, 1, 2,
                     (GtkAttachOptions) (GTK_FILL),
                     (GtkAttachOptions) (0), 0, 0);
    gtk_misc_set_alignment (GTK_MISC (label14), 0, 0.5);
    gtk_misc_set_padding (GTK_MISC (label14), 0, 10);

    label15 = gtk_label_new ("E-mailadres :");
    gtk_widget_show (label15);
    gtk_table_attach (GTK_TABLE (table2), label15, 0, 1, 2, 3,
                     (GtkAttachOptions) (GTK_FILL),
                     (GtkAttachOptions) (0), 0, 0);
    gtk_misc_set_alignment (GTK_MISC (label15), 0, 0.5);
    gtk_misc_set_padding (GTK_MISC (label15), 0, 10);

    signup->txtUsername = gtk_entry_new ();
    gtk_widget_show (signup->txtUsername);
    gtk_table_attach (GTK_TABLE (table2), signup->txtUsername, 1, 2, 0, 1,
                     (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                     (GtkAttachOptions) (GTK_FILL), 0, 2);

    signup->txtPassword = gtk_entry_new ();
    gtk_widget_show (signup->txtPassword);
    gtk_table_attach (GTK_TABLE (table2), signup->txtPassword, 1, 2, 1, 2,
                     (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                     (GtkAttachOptions) (GTK_FILL), 0, 0);
    gtk_entry_set_visibility (GTK_ENTRY (signup->txtPassword), FALSE);
    gtk_entry_set_activates_default (GTK_ENTRY (signup->txtPassword), TRUE);

    signup->txtEmail = gtk_entry_new ();
    gtk_widget_show (signup->txtEmail);
    gtk_table_attach (GTK_TABLE (table2), signup->txtEmail, 1, 2, 2, 3,
                     (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                     (GtkAttachOptions) (GTK_FILL), 0, 0);

    signup->btnRegistreer = gtk_button_new_with_mnemonic ("Registreer nieuw FTD account");
    gtk_widget_show (signup->btnRegistreer);
    gtk_box_pack_start (GTK_BOX (vbox4), signup->btnRegistreer, TRUE, TRUE, 0);

    gtk_widget_show(signup->window);

    g_signal_connect (G_OBJECT(signup->btnRegistreer), "clicked", G_CALLBACK(ftd_gui_signup_register), config);
}

gboolean
ftd_gui_signup_register(GtkWidget *caller, gpointer data)
{
    const gchar *username;
    const gchar *password;
    FtdGuiConfig *config = (FtdGuiConfig *) data;
    const gchar *user;
    const gchar *pass;
    const gchar *email;
    int retval;
    char *errmsg = NULL;

    g_assert(config);

    user = gtk_entry_get_text(GTK_ENTRY(config->signup->txtUsername));
    pass = gtk_entry_get_text(GTK_ENTRY(config->signup->txtPassword));
    email = gtk_entry_get_text(GTK_ENTRY(config->signup->txtEmail));

    retval = ftd_signup_register(user, pass, email, &errmsg);
    if (retval) {
        ftd_gui_errhandler(config->window, FTD_ONDERDEEL_SIGNUP, retval, errmsg);
        return FALSE;
    }

    /* Het aanmaken van een nieuw FTD account is gelukt.
       Vernietig het signup venster en vul de nieuwe user/pass
       in op het configuratievenster. */
    username = gtk_entry_get_text(GTK_ENTRY(config->signup->txtUsername));
    password = gtk_entry_get_text(GTK_ENTRY(config->signup->txtPassword));
    gtk_entry_set_text(GTK_ENTRY(config->user->username), username);
    gtk_entry_set_text(GTK_ENTRY(config->user->password), password);
    gtk_widget_destroy(config->signup->window);
    g_free(config->signup);
    config->signup = NULL;

    return FALSE;
}

