/*
 *
 * Module:             keyboard.ycp
 *
 * Author:             Thomas Roelz (tom@suse.de)
 *
 * Submodules:
 *
 *
 * Purpose:	configure keyboard in running system
 *
 * Modify:
 *
 *
 * $Id: keyboard.ycp 21295 2005-02-07 13:31:00Z jsuchome $
 */

{
textdomain "country";

import "CommandLine";
import "Keyboard";
import "Service";
import "Stage";
import "Wizard";

// Memorize the current keyboard
string keyboard_on_entry = Keyboard::current_kbd;

/**
 * read keyboard settings
 */
define boolean KeyboardRead () {

    // Check if this is a reconfiguration run.
    //
    if (Stage::reprobe ())
    {
	// Reprobe keyboard module to achieve same behaviour as
	// during installation.
	Keyboard::Probe();
	Keyboard::SetConsole( Keyboard::current_kbd );
	Keyboard::SetX11( Keyboard::current_kbd );

	y2milestone("Reprobed keyboard");
    }
    return true;
}

/**
 * write keyboard settings
 */
define boolean KeyboardWrite () {

    Keyboard::Save (keyboard_on_entry != Keyboard::current_kbd);
    Service::Restart ("kbd");
    return true;
}

/**
 * the keyboard configuration sequence
 */
define any KeyboardSequence () {

    KeyboardRead ();

    Wizard::CreateDialog();

    // The keyboard module should be initialized to the current keyboard from
    // sysconfig, XF86Config or probing. Either way this Keyboard will be
    // selected in the list in inst_keyboard.ycp. Now let the user choose a new
    // keyboard layout.
    //
    any result = WFM::CallFunction( "inst_keyboard", [ true, true ] );

    if (result == `next)
    {
	KeyboardWrite ();
    }
    else
    {
	y2milestone( "User cancelled --> no change" );
    }
    UI::CloseDialog();
    return result;
}

/**
 * Handler for keyboard summary
 */
define boolean KeyboardSummaryHandler (map options) {

    // summary label
    CommandLine::Print (sformat (_("Current Keyboard Layout: %1"),
	Keyboard::current_kbd));
    return false;
}

/**
 * Handler for listing keyboard layouts
 */
define boolean KeyboardListHandler (map options) {

    foreach (string code, string name, Keyboard::Selection (), {
	CommandLine::Print (sformat ("%1 (%2)", code, name));
    });
    return false;
}


/**
 * Handler for changing keyboard settings
 */
define boolean KeyboardSetHandler (map options) {

    string keyboard	= options["layout"]:"";

    if (keyboard == "" || !haskey (Keyboard::Selection(), keyboard))
    {
	// error message (%1 is given layout); do not translate 'list'
	CommandLine::Print (sformat (_("Keyboard layout '%1' is invalid. Use a 'list' command to see possible values."), keyboard));
    }
    Keyboard::Set (keyboard);

    return (keyboard_on_entry != Keyboard::current_kbd);
}


/* -- the command line description map -------------------------------------- */
map cmdline = $[
    "id"		: "keyboard",
    // translators: command line help text for Securoty module
    "help"		: _("Keyboard configuration."),
    "guihandler"	: KeyboardSequence,
    "initialize"	: KeyboardRead,
    "finish"		: KeyboardWrite,
    "actions"		: $[
	"summary" :$[
	    "handler"	: KeyboardSummaryHandler,
	    // command line help text for 'summary' action
	    "help"	: _("Keyboard configuration summary."),
	],
	"set" :$[
	    "handler"	: KeyboardSetHandler,
	    // command line help text for 'set' action
	    "help"	: _("Set new values for keyboard configuration."),
	],
	"list": $[
	    "handler"	: KeyboardListHandler,
	    // command line help text for 'list' action
	    "help"	: _("List all available keyboard layouts.")
	],
    ],
    "options"		: $[
	"layout"		: $[
	    // command line help text for 'set layout' option
	    "help"	: _("New keyboard layout"),
	    "type"	: "string"
	],
    ],
    "mappings"		: $[
	"summary"	: [],
	"set"		: [ "layout" ],
	"list"		: [],
    ]
];

CommandLine::Run (cmdline);
return true;

}
