/* GADMIN-SAMBA, an easy to use GTK+ frontend for the SAMBA file and print server.
 * Copyright (C) 2006 - 2011 Magnus Loef <magnus-swe@telia.com> 
 *
 * 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 3 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 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
*/



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include "gettext.h"
#include "widgets.h"
#include "make_settings_buttons.h"
#include "make_settings_entries.h"
#include "make_settings_combos.h"
#include "make_settings_spinbuttons.h"
#include "make_settings_checkbuttons.h"
#include "create_user_settings.h"
#include "populate_shell_combo.h"
#include "add_change_user.h"
#include "delete_user.h"
#include "functions.h"
#include "file_chooser.h"

gulong account_disabled_toggled_signal;



void select_home_path_clicked(struct w *widgets)
{
    gchar *path = NULL;

    path = get_dialog_path_selection("DIR", "/home", "None");
    if( path!=NULL )
    {
        gtk_entry_set_text(GTK_ENTRY(widgets->user_set_entry[4]), path);
        g_free(path);
    }
}


void create_user_settings(struct w *widgets)
{
    GtkTooltips *tooltips;
    GtkWidget *hbuttonbox;
    GtkWidget *delete_user_button;
    GtkWidget *apply_user_button;

    /* For the custom new user button */
    GtkWidget *new_user_button;
    GtkWidget *new_user_hbox;
    GtkWidget *new_user_image;
    GtkWidget *new_user_label;
    GtkWidget *new_user_alignment;

    int a = 0;
    int b = 1;
    int ent  = 0;
    int comb = 0;
    int check = 0;

    tooltips = gtk_tooltips_new();

    /* Account locked checkbutton and label */
    widgets->user_set_checkbutton[check] = gtk_check_button_new();
    widgets->acclock_label = gtk_label_new(_("Account locked:"));

    /* The delete, add and apply user buttons */
    delete_user_button = gtk_button_new_from_stock(GTK_STOCK_DELETE);
    /* Custom new user button begin */
    new_user_button = gtk_button_new();
    new_user_alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
    gtk_container_add(GTK_CONTAINER(new_user_button), new_user_alignment);
    new_user_hbox = gtk_hbox_new(FALSE, 2);
    gtk_container_add(GTK_CONTAINER(new_user_alignment), new_user_hbox);
    new_user_image = gtk_image_new_from_stock("gtk-add", GTK_ICON_SIZE_BUTTON);

    gtk_box_pack_start(GTK_BOX(new_user_hbox), new_user_image, TRUE, TRUE, 0);
    new_user_label = gtk_label_new_with_mnemonic(_("New user"));

    gtk_box_pack_start(GTK_BOX(new_user_hbox), new_user_label, TRUE, TRUE, 0);
    gtk_label_set_justify(GTK_LABEL(new_user_label), GTK_JUSTIFY_LEFT);
    /* Custom new user button end */

    /* The apply button */
    apply_user_button  = gtk_button_new_from_stock(GTK_STOCK_APPLY);

    hbuttonbox = gtk_hbutton_box_new();

    gtk_box_pack_start(GTK_BOX(hbuttonbox), widgets->acclock_label, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(hbuttonbox), widgets->user_set_checkbutton[check], TRUE, TRUE, 0);
    check++;

    gtk_box_pack_start(GTK_BOX(hbuttonbox), delete_user_button, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(hbuttonbox), new_user_button, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(hbuttonbox), apply_user_button, TRUE, TRUE, 0);

    gtk_table_attach(GTK_TABLE(widgets->user_set_table), hbuttonbox, 0, 3, a, b, 0, 0, 5, 5);
    a++; b++;

    
    g_signal_connect_swapped(G_OBJECT(delete_user_button), "clicked",
			     G_CALLBACK(delete_user), widgets);

    g_signal_connect_swapped(G_OBJECT(apply_user_button), "clicked",
			     G_CALLBACK(add_change_user), widgets);

    g_signal_connect_swapped(G_OBJECT(new_user_button), "clicked",
			     G_CALLBACK(clear_user_profile), widgets);


    /* Fetch the gulong as well as hooking up the connect signal. Borked like this in 2006 ? */
    account_disabled_toggled_signal = g_signal_connect_swapped(G_OBJECT(widgets->user_set_checkbutton[0]),
				    "clicked", G_CALLBACK(account_locked_checkbutton_clicked), widgets);


    /* Username */	     /* Max length and input 350 chars */
    GtkWidget *username_button;
    widgets->user_set_entry[ent] = gtk_entry_new();
    username_button = make_button_with_entry(GTK_TABLE(widgets->user_set_table),
                        		     widgets->user_set_entry[ent],
                    			     "gtk-refresh",
					     _("Username:"),  // fixed, Quax
					     _("Create a randomized username"),  // fixed, Quax
					     0,1,a,b,350);

    gtk_tooltips_set_tip(tooltips, widgets->user_set_entry[ent], _("The name of the user."), NULL);
    a++; b++; ent++;

    g_signal_connect_swapped(G_OBJECT(username_button), "clicked",
			     G_CALLBACK(randomize_username), widgets);

    /* Password  */
    GtkWidget *password_button;
    widgets->user_set_entry[ent] = gtk_entry_new();
    password_button = make_button_with_entry(GTK_TABLE(widgets->user_set_table),
                        		     widgets->user_set_entry[ent],
                    			     "gtk-refresh",
					     _("Password:"),  // fixed, Quax
					     _("Create a randomized password"),  // fixed, Quax
					     0,1,a,b,350);
    gtk_tooltips_set_tip(tooltips, widgets->user_set_entry[ent], _("The users password."), NULL);
    a++; b++; ent++;

    g_signal_connect_swapped(G_OBJECT(password_button), "clicked",
			     G_CALLBACK(randomize_password), widgets);


    /* Group */
    widgets->user_set_entry[ent] = make_entry_with_label(GTK_TABLE(widgets->user_set_table), _(" Group: "), 0,1,a,b,350);
    gtk_tooltips_set_tip(tooltips, widgets->user_set_entry[ent], _("The group the user belongs to"), NULL);
    a++; b++; ent++;


    /* Comment */
    widgets->user_set_entry[ent] = make_entry_with_label(GTK_TABLE(widgets->user_set_table), _(" Comment: "), 0,1,a,b,350);
    gtk_tooltips_set_tip(tooltips, widgets->user_set_entry[ent], _("A comment about the user"), NULL);
    a++; b++; ent++;


    /* Home directory */
    GtkWidget *homedir_button;
    widgets->user_set_entry[ent] = gtk_entry_new();
    homedir_button = make_button_with_entry(GTK_TABLE(widgets->user_set_table),
                        		     widgets->user_set_entry[ent],
                    			     "gtk-refresh",
					     _("Home directory:"),  // fixed, Quax
					     _("Choose a home directory for this user"),  // fixed, Quax
					     0,1,a,b,350);
    gtk_tooltips_set_tip(tooltips, widgets->user_set_entry[ent], _("The users home directory, IE: /home/UserName or /dev/null to skip making a homedirectory."), NULL);
    a++; b++; ent++;
    g_signal_connect_swapped(G_OBJECT(homedir_button), "clicked",
			     G_CALLBACK(select_home_path_clicked), widgets);


    widgets->user_set_combo[comb] = make_label_textcombo_label(GTK_TABLE(widgets->user_set_table), _(" Shell: "), 0,1,a,b, 80);
    populate_shell_combo(widgets->user_set_combo[comb]);
    a++; b++; comb++;


    gtk_widget_show_all(widgets->main_window);
}
