/*
 *
 * Module:             language.ycp
 *
 * Author:             Klaus Kaempf (kkaempf@suse.de)
 *
 * Submodules:
 *
 *
 * Purpose:	configure language in running system
 *
 * Modify:
 *
 *
 * $Id: language.ycp 22087 2005-03-02 08:00:56Z jsuchome $
 */

{

textdomain "country";

import "Bootloader";
import "CommandLine";
import "Console";
import "Language";
import "Popup";
import "Wizard";

string language_on_entry = "";
string languages_on_entry = "";

/**
 * read language settings
 */
define boolean LanguageRead () {

    // Memozize the current language.
    language_on_entry	= Language::language;
    languages_on_entry	= Language::languages;

    y2milestone("language_on_entry: %1, languages: %2",
	language_on_entry, languages_on_entry);
    return true;
}

/**
 * write language settings
 */
define boolean LanguageWrite () {

    Language::Save ();
    Console::Save ();

    y2milestone ("Language changed --> saving");

    WFM::CallFunction ("inst_suseconfig", [ false, false ]);

    Bootloader::UpdateGfxMenu();

    return true;
}

/**
 * the language configuration sequence
 */
define any LanguageSequence () {

    LanguageRead ();

    Console::Init ();

    Wizard::CreateDialog();

    any result = `again;

    Wizard::OpenAcceptDialog();		// Reduce flicker (yes, it's a kludge)

    while (result == `again)
    {
	Wizard::OpenAcceptDialog();	// This dialog will actually be used

	// Params are:				`back `next  set_default
        map args = $[];
        args["enable_back"] = true;
        args["enable_next"] = true;
        result = WFM::CallFunction ("inst_language", [args]);

	Wizard::CloseDialog();
    }

    Wizard::CloseDialog();
    Wizard::RetranslateButtons();

    y2milestone ("result '%1'", result);

    if (result == `cancel || result == `back)
    {
	// Back to original values...
	//
	y2milestone ("`cancel or `back --> restoring: <%1>", language_on_entry);

	// Set it in the Language module.
	//
	Language::Set (language_on_entry );

	// Set Console font
	//
	Console::SelectFont (language_on_entry);

	// Set it in YaST2
	Language::WfmSetLanguage ();
    }
    else
    {
	if (Language::language != language_on_entry	||
	    Language::languages != languages_on_entry	||
	    Language::ExpertSettingsChanged)
	{
	    LanguageWrite ();
	}
	else
	{
	    y2milestone( "Language not changed --> doing nothing" );
	}
    }
    UI::CloseDialog();
    return result;
}

/**
 * Handler for language summary
 */
define boolean LanguageSummaryHandler (map options) {

    map<string,list> selection	= Language::Selection();
    // summary label
    CommandLine::Print (sformat (_("Current Language: %1 (%2)"),
	Language::language, selection[Language::language,1]:""));

    string languages	= Language::languages;
    if (languages != "")
    {
	list<string> langs	= filter (string lang_code,
	    splitstring (languages, ","), ``(lang_code != Language::language));
	if (size (langs) > 0)
	{
	    CommandLine::Print (sformat (_("Additional Languages: %1"),
		mergestring (langs,",")));
	}
    }
    return false;
}

/**
 * Handler for listing available languages
 */
define boolean LanguageListHandler (map options) {

    foreach (string lang_code, list lang_info, Language::Selection(), {
	CommandLine::Print (sformat ("%1 (%2)", lang_code, lang_info[1]:""));
    });
    return false;
}


/**
 * Handler for changing language settings
 */
define boolean LanguageSetHandler (map options) {

    string language	= options["lang"]:"";

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

    return (language_on_entry != Language::language);
}


/* -- the command line description map -------------------------------------- */
map cmdline = $[
    "id"		: "language",
    // translators: command line help text for language module
    "help"		: _("Language configuration."),
    "guihandler"	: LanguageSequence,
    "initialize"	: LanguageRead,
    "finish"		: LanguageWrite,
    "actions"		: $[
	"summary" :$[
	    "handler"	: LanguageSummaryHandler,
	    // command line help text for 'summary' action
	    "help"	: _("Language configuration summary."),
	],
	"set" :$[
	    "handler"	: LanguageSetHandler,
	    // command line help text for 'set' action
	    "help"	: _("Set new values for language."),
	],
	"list": $[
	    "handler"	: LanguageListHandler,
	    // command line help text for 'list' action
	    "help"	: _("List all available languages.")
	],
    ],
    "options"		: $[
	"lang"		: $[
	    // command line help text for 'set lang' option
	    "help"	: _("New language value"),
	    "type"	: "string"
	],
    ],
    "mappings"		: $[
	"summary"	: [],
	"set"		: [ "lang" ],
	"list"		: [],
    ]
];

CommandLine::Run (cmdline);
return true;

}
