/**
 * File:	modules/AutoinstGeneral.ycp
 * Package:	Autoyast
 * Summary:	Configuration of general settings for autoyast
 * Authors:	Anas Nashif (nashif@suse.de)
 *
 * $Id: AutoinstGeneral.ycp 21218 2005-02-04 13:44:55Z jsuchome $
 */

{
    module "AutoinstGeneral";
    textdomain "autoinst";

    import "AutoInstall";
    import "AutoinstConfig";
    import "Summary";
    import "Keyboard";
    import "Mouse";
    import "Language";
    import "Keyboard";
    import "Timezone";
    import "Misc";
    import "Profile";


    //
    // Show proposal and ask user for confirmation to go on with auto-installation
    // Similar to interactive mode, without letting use change settings
    // Interactive mode implies confirmation as well..
    //
    global boolean Confirm = true;

    //
    // Keyboard
    //
    global map keyboard = $[];

    //
    // Language
    //
    global string language = "";

    //
    // Mouse, if not autoprobed
    //
    global map  mouse = $[];

    //
    // Clock Settings
    //
    global map Clock = $[];

    //
    // Mode Settings
    //
    global map mode = $[];


    /**
     * Constructor
     */
    global define void AutoinstGeneral ()
    {
        return;
    }

    /* default value of settings modified */
    global boolean modified = false;


    /**
     * Function sets internal variable, which indicates, that any
     * settings were modified, to "true"
     */
    global define void SetModified () {
        modified = true;
    }

    /**
     * Functions which returns if the settings were modified
     * @return boolean  settings were modified
     */
    global define boolean GetModified () {
        return modified;
    }

    /**
     * Summary of configuration
     * @return string Formatted summary
     */
    global define string Summary()
    {
	string language_name		= "";
	string keyboard_name		= "";
	string mouse_name		= "";

	if ( mouse["id"]:"" != "" && mouse["id"]:"" != "probe")
	{
	    Mouse::Set(mouse["id"]:"");
	    mouse_name = Mouse::MakeProposal(false, false);
	}
	else
	{
	    mouse_name = "probe";
	}

	if (language != "")
	{
	    Language::Set(language);
	    language_name= Language::GetName ();
	}

	if (keyboard  != $[])
	{
	    Keyboard::current_kbd = "";
	    y2milestone("keymap: %1", keyboard["keymap"]:"");
	    Keyboard::SetLanguage(keyboard["keymap"]:"");
	    keyboard_name = Keyboard::Name();
	}

	string hwclock = (Clock["hwclock"]:""  == "UTC") ?  _("UTC") : _("Local time");

	string summary = "";
	summary = Summary::AddHeader(summary, _("Language"));
	summary = Summary::AddLine(summary, (language_name != "") ?
				   language_name : Summary::NotConfigured());

	summary = Summary::AddHeader(summary, _("Keyboard"));
	summary = Summary::AddLine(summary, (keyboard_name != "") ?
				   keyboard_name : Summary::NotConfigured());

	summary = Summary::AddHeader(summary, _("Time Zone"));
	summary = Summary::AddLine(summary, ( Clock["timezone"]:"" != "" && Clock["timezone"]:"" != nil ) ?
				   Clock["timezone"]:"" : Summary::NotConfigured());

	summary = Summary::AddHeader(summary, _("Hardware clock"));
	summary = Summary::AddLine(summary,  hwclock );

	summary = Summary::AddHeader(summary, _("Mouse"));
	summary = Summary::AddLine(summary, (mouse_name != "") ?
				   mouse_name : Summary::NotConfigured());

	summary = Summary::AddHeader(summary, _("Confirm installation?"));
	summary = Summary::AddLine(summary, (mode["confirm"]:true) ?
				   _("Yes") : _("No"));

        /*
	summary = Summary::AddHeader(summary, _("Reboot after installation"));
	summary = Summary::AddLine(summary, (mode["reboot"]:false) ?
				   _("Yes") : _("No"));
                                   */

	summary = Summary::AddHeader(summary, _("Start System without Reboot"));
	summary = Summary::AddLine(summary, (mode["forceboot"]:false) ?
				   _("Yes") : _("No"));


	return summary;
    }

    /**
     * Import Configuration
     * @param map settings
     * @return booelan
     */
    global define boolean Import (map settings)
    {
        SetModified ();
        language =	settings["language"]:"";
        keyboard =      settings["keyboard"]:$[];;
        Clock	=	settings["clock"]:$[];
        mouse =	        settings["mouse"]:$[];
        mode  =	        settings["mode"]:$[];
        return true;
    }


    /**
     * Export Configuration
     * @return map
     */
    global define map Export ()
    {
        map general = $[];

        general["language"] = language ;
        if (haskey(keyboard, "keyboard_values") || Keyboard::ExpertSettingsChanged )
        {
            keyboard["keyboard_values"] = Keyboard::GetExpertValues();
        }

        general["keyboard"] = keyboard;
        general["clock"] = Clock;
        general["mouse"] = mouse;
        general["mode"] = mode;
        return general;
    }


    /**
     * Write General  Configuration
     * @return boolean true on success
     */
    global define boolean Write()
    {
	y2milestone("keymap -> %1", keyboard["keymap"]:"" );
	y2milestone("timezone -> %1", Clock["timezone"]:"" );

	AutoinstConfig::Confirm = mode["confirm"]:true;
	AutoinstConfig::ForceBoot = mode["forceboot"]:false;
	AutoinstConfig::Halt = mode["halt"]:false;
	AutoinstConfig::RebootMsg = mode["rebootmsg"]:false;

	Keyboard::default_kbd = keyboard["keymap"]:"";
	Keyboard::SetConsole( keyboard["keymap"]:"" );
	Keyboard::SetX11( keyboard["keymap"]:"" );

	//
	// Timezone
	//
	if (Clock["timezone"]:"" != "")
	{
	    Timezone::Set( Clock["timezone"]:"", true );
	}

	//
	// Hardware Clock
	//

	Timezone::hwclock =  (Clock["hwclock"]:"" == "UTC" || Clock["hwclock"]:"" == "GMT") ? "-u" : "--localtime";

	//
	// mouse
	//

	if ( mouse["id"]:"" != "probe" && mouse["id"]:"" != "")
	{
	    Mouse::Set(mouse["id"]:"");
	}
	else if (haskey(mouse,"device"))
	{
	    //
	    // Otherwise, try to find the mouse id from the DB using data supplied by user,
	    // at least the device is needed.
	    //
	    string device = mouse["device"]:"none";
	    integer wheels = mouse["wheels"]:0;

	    map<string, list> mice = (map<string, list>)Misc::ReadAlternateFile ("mouse_db.ycp", "mouse_raw.ycp");

	    foreach(string f,list g, mice, ``{
		map data = g[1]:$[];
		if ( data["wheels"]:0 == wheels  && data["device"]:"" == device)
		{
		    mouse["id"] =  f;
		}
	    });
	     Mouse::Set(mouse["id"]:"");
	}
    }

}
