/**
 * File:	modules/Encoding.ycp
 * Package:	yast2
 * Summary:	Provide the encoding stuff
 * Authors:	Klaus Kaempf <kkaempf@suse.de>
 *
 * $Id: Encoding.ycp 20677 2005-01-07 03:23:55Z nashif $
 */

{

module "Encoding";
textdomain "base";

import "Stage";

/**
 * Current (ISO) encoding
 */
global string console = "ISO-8859-1";
global string lang = "en_US";
global boolean utf8 = true;

/**
 * Restore data to system
 * @return console encoding
 */
global define string Restore() {

    console = (string) SCR::Read (.sysconfig.console.CONSOLE_ENCODING);
    if(console == nil) console = "";

    map m = (map) SCR::Execute( .target.bash_output, "locale -k charmap" );
    if(m == nil) m = $[];

    list<string> out = splitstring(m["stdout"]:"", "\n");
    y2milestone( "list %1", out );

    out = filter( string e, out, ``(find(e, "charmap=")==0) );
    y2milestone( "list %1", out );

    if(size(out[0]:"") > 0) {
	string enc = substring( out[0]:"", 8 );
	y2milestone( "enc %1", enc );
	enc = deletechars( enc, "\" " );
	y2milestone( "enc %1", enc );
	if(size(enc) > 0)
	    console = enc;
    }
    y2milestone( "encoding %1", console );
    return console;
}

/**
 * Set Encoding Language
 * @param new_lang New Language
 * @return void
 */
global void SetEncLang( string new_lang )
    ``{
    lang = new_lang;
    y2milestone( "SetEncLang %1", lang );
    }

/**
 * Get Encoding Language
 * @return Language
 */
global string GetEncLang() 
{
    string ret = lang;
    y2milestone( "GetEncLang ret %1", ret );
    return( ret );
}

/**
 * Set UTF8 Language
 * @param  new_utf8 New UTF8 Language
 * @return void
 */
global void SetUtf8Lang( boolean new_utf8 )
{
    utf8 = new_utf8;
    y2milestone( "SetUtf8Lang %1", utf8 );
}

/**
 * Get UTF8 Language
 * @return boolean
 */
global boolean GetUtf8Lang()
    ``{
    boolean ret = utf8;
    y2milestone( "GetUtf8Lang ret %1", ret );
    return( ret );
    }

map enc_map = $[ "euc-jp" : "932",
                 "sjis"   : "932",
		 "gb2312" : "936",
		 "iso8859-2" : "852",
		 "big5" : "950",
		 "euc-kr" : "949" ];

map lang_map = $[ "ja_JP" : "932",
                  "zh_CN" : "936",
                  "zh_TW" : "950",
                  "zh_HK" : "950",
                  "ko_KR" : "949" ];



/**
 * Get Code Page
 * @param enc Encoding
 * @return string
 */
global define string GetCodePage( string enc ) 
    ``{
    string code = enc_map[enc]:"";
    if( size(code)==0 && lang != nil)
	{
	string l = substring( lang, 0, 5 );
	code = lang_map[l]:"";
	}
    y2milestone( "GetCodePage enc %1 lang %2 ret %3", enc, lang, code );
    return code;
    }


/**
 * Constructor
 * does nothing in initial mode
 * restores console encoding from /etc/sysconfig
 * in normal mode
 */
global define void Encoding() {
    if(!Stage::initial ()) Restore();
    return;
}

/* EOF */
}
