/*
 *
 * Module:             timezone.ycp
 *
 * Author:             Klaus Kaempf (kkaempf@suse.de)
 *
 * Submodules:
 *
 *
 * Purpose:	configure timezone in running system
 *
 * Modify:
 *
 *
 * $Id: timezone.ycp 21295 2005-02-07 13:31:00Z jsuchome $
 */

{
textdomain "country";

import "CommandLine";
import "Timezone";
import "Wizard";


string timezone_on_entry	= "";
string hwclock_on_entry		= "";

/**
 * read timezone settings
 */
define boolean TimezoneRead () {

    // Memorize the current timezone.
    //
    timezone_on_entry	= Timezone::timezone;
    hwclock_on_entry	= Timezone::hwclock;
    return true;
}

/**
 * write timezone settings
 */
define boolean TimezoneWrite () {

    if (timezone_on_entry != Timezone::timezone ||
	hwclock_on_entry != Timezone::hwclock )
    {
	y2milestone ("User selected new timezone/clock setting: <%1> <%2>",
	    Timezone::timezone, Timezone::hwclock);

	Timezone::Save ();
	WFM::CallFunction( "inst_suseconfig", [ false, false ] );
    }
    else
    {
	y2milestone( "Timezone not changed --> doing nothing" );
    }
    return true;
}


/**
 * the timezone configuration sequence
 */
define any TimezoneSequence () {

    TimezoneRead ();

    // create the wizard dialog
    Wizard::CreateDialog();

    //------------------------------------------------------------

    any result = nil;

    result = WFM::CallFunction( "inst_timezone", [ true, true ] );

    if ( result == `next )
    {
	TimezoneWrite ();
    }
    else	// `cancel or `back
    {
	y2milestone( "User cancelled --> no change" );
    }
    UI::CloseDialog();
    return result;
}

/**
 * Handler for timezone summary
 */
define boolean TimezoneSummaryHandler (map options) {

    // summary label
    CommandLine::Print(sformat (_("Current Time Zone:\t%1"),Timezone::timezone));

    if (!Timezone::utc_only())
    {
	// summary label
	CommandLine::Print (sformat (_("Hardware Clock Set To:\t%1"),
	    Timezone::hwclock == "-u" ? _("UTC") : _("local time")));

    }
    // summary label
    CommandLine::Print (sformat (_("Current Time and Date:\t%1"),
	Timezone::GetDateTime(true, true)));
    return false;
}

/**
 * Handler for listing timezone layouts
 */
define boolean TimezoneListHandler (map options) {

    foreach (map<string,any> zone, Timezone::get_zonemap (), {
	CommandLine::Print ("");
	// summary label
	CommandLine::Print (sformat ("Region: %1", zone["name"]:""));
	foreach (string code, string name, zone["entries"]:$[], {
	    CommandLine::Print (sformat ("%1 (%2)", code, name));
	});
    });
    return false;
}


/**
 * Handler for changing timezone settings
 */
define boolean TimezoneSetHandler (map options) {

    string timezone	= options["timezone"]:"";
    string hwclock	= options["hwclock"]:"";

    if (timezone != "")
    {
	Timezone::Set (timezone, true);
    }
    if (hwclock != "" && !Timezone::utc_only ())
    {
	Timezone::hwclock	= hwclock == "utc" ? "-u" : "--localtime";
    }
    return (timezone_on_entry != Timezone::timezone ||
	    hwclock_on_entry != Timezone::hwclock);
}


/* -- the command line description map -------------------------------------- */
map cmdline = $[
    "id"		: "timezone",
    // translators: command line help text for Securoty module
    "help"		: _("Time zone configuration."),
    "guihandler"	: TimezoneSequence,
    "initialize"	: TimezoneRead,
    "finish"		: TimezoneWrite,
    "actions"		: $[
	"summary" :$[
	    "handler"	: TimezoneSummaryHandler,
	    // command line help text for 'summary' action
	    "help"	: _("Time zone configuration summary."),
	],
	"set" :$[
	    "handler"	: TimezoneSetHandler,
	    // command line help text for 'set' action
	    "help"	: _("Set new values for time zone configuration."),
	],
	"list": $[
	    "handler"	: TimezoneListHandler,
	    // command line help text for 'list' action
	    "help"	: _("List all available time zones.")
	],
    ],
    "options"		: $[
	"timezone"		: $[
	    // command line help text for 'set timezone' option
	    "help"	: _("New time zone"),
	    "type"	: "string"
	],
	"hwclock"		: $[
	    // command line help text for 'set hwclock' option
	    "help"	: _("New value for hardware clock"),
	    "type"	: "enum",
	    "typespec"	: [ "local", "utc" ],
	],
    ],
    "mappings"		: $[
	"summary"	: [],
	"set"		: [ "timezone", "hwclock" ],
	"list"		: [],
    ]
];

CommandLine::Run (cmdline);
return true;


}
