/**
 * File:	include/irda/ui.ycp
 * Package:	Configuration of irda
 * Summary:	Dialogs definitions
 * Authors:	Jiri Suchomel <jsuchome@suse.cz>
 *
 * $Id: ui.ycp 22091 2005-03-02 08:07:47Z jsuchome $
 */

{

textdomain "irda";

import "Irda";
import "Label";
import "Message";
import "Package";
import "Popup";
import "Service";
import "Wizard";


/**
 * Popup for testing IrDA
 * @param port the serial port use for IrDA
 * (to see if it was changed and service needs to be restarted)
 */
define boolean TestPopup (string port) {

    boolean modified	= port != Irda::port;
    // if service was originaly started
    boolean orig_start	= false;

    /**
     * temporary start the service
     * return error output
     */
    define string irda_tmp_start () {

	map out = $[];
	orig_start =  (Service::Status ("irda") == 0);

	if (modified)
	{
	    // 1. save new configuration
	    SCR::Write (.sysconfig.irda.IRDA_PORT, port);
	    SCR::Write (.sysconfig.irda, nil);
	}

        // 2. start/restart the service
	// when module cannot be loaded, Runlevel returns 0 -> use target.bash
	if (!orig_start)
	    out = (map) SCR::Execute (.target.bash_output, "rcirda start");
	else if (modified)
	    out = (map) SCR::Execute (.target.bash_output, "rcirda restart");

	return out["stderr"]:"";
    }

    /**
     * internal function
     * return IrDA configuration to original state after testing
     */
    define void irda_tmp_stop () {

	if (modified)
	{
	    SCR::Write (.sysconfig.irda.IRDA_PORT, Irda::port);
	    SCR::Write (.sysconfig.irda, nil);
	}

	if (!orig_start)
	    Service::RunInitScript ("irda", "stop");
	else if (modified)
	    Service::RunInitScript ("irda", "restart");
    }

    UI::OpenDialog(`opt(`decorated), `HBox(`HSpacing(1.5),
    `VBox(
        `VSpacing(1),
        // Wait text label
        `Label(_("Initializing -- please wait...")),
        `VSpacing(1),
        `PushButton(`id(`done), `opt(`default), Label::AbortButton()),
        `VSpacing(1)),
    `HSpacing(1.5)
    ));

    string start = irda_tmp_start ();
    UI::CloseDialog();
    if (start != "")
    {
	Popup::Error (sformat ("%1
%2", Message::CannotStartService ("irda"), start));
	irda_tmp_stop ();
	return false;
    }

    // run test application
    SCR::Execute (.background.run_output, "/usr/bin/irdadump");

    // construct the dialog
    UI::OpenDialog(`opt(`decorated), `HBox(`HSpacing(1.5),
    `VSpacing(18),
    `VBox(
        `HSpacing(80),
        `VSpacing(0.5),
	// Popup label (heading)
        `Label(_("IrDA Test")),
        `VSpacing(0.5),
        `LogView (`id (`log), "", 8, 0),
        `VSpacing(0.5),
        `LogView (`id (`discovery), "", 5, 0),
        `VSpacing(0.5),
	`HBox (
	    `PushButton (`id(`done), `opt(`key_F9), Label::CloseButton ()),
	    `PushButton (`id(`stop), `opt(`key_F5), Label::StopButton ()),
	    `PushButton (`id(`cont), `opt(`key_F6), Label::ContinueButton ())
	),
        `VSpacing(0.5)),
    `HSpacing(1.5)
    ));

    // read the output of test application
    string test_output	= "";
    string discovery	= "";
    UI::ChangeWidget (`id (`cont), `Enabled, false);

    symbol ret = nil;
    do
    {
	ret = (symbol) UI::PollInput();
        if ((boolean) SCR::Read(.background.output_open) &&
	    (integer) SCR::Read(.background.newlines)>0)
        {
            // read the output line from irw:
	    list out	= (list)SCR::Read(.background.newout);
	    test_output = out[0]:"";
	    if (test_output != "")
		UI::ChangeWidget (`id(`log), `LastLine, test_output + "\n");
        }
	else if (!(boolean)SCR::Read(.background.output_open))
	{
	    // error text
	    Popup::Error(_("The testing application is not responding."));
	    ret = `ok;
	}
	string disc = (string)
	    SCR::Read (.target.string, "/proc/net/irda/discovery");
	if (disc != discovery)
	{
	    discovery	= disc;
	    UI::ChangeWidget (`id(`discovery), `Value, discovery);
	}
	if (ret == `stop)
	{
	    SCR::Execute(.background.kill);
	    UI::ChangeWidget (`id (`cont), `Enabled, true);
	    UI::ChangeWidget (`id (`stop), `Enabled, false);
	    ret = (symbol) UI::UserInput ();
	}
	if (ret == `cont)
	{
	    SCR::Execute (.background.run_output, "/usr/bin/irdadump");
	    UI::ChangeWidget (`id (`stop), `Enabled, true);
	    UI::ChangeWidget (`id (`cont), `Enabled, false);
	    ret = nil;
	}
	sleep (10);
    }
    while (ret == nil);

    SCR::Execute(.background.kill);
    irda_tmp_stop ();
    UI::CloseDialog();

    return true;
}


/**
 * Dialog for seting up IrDA
 */
define symbol IrDADialog () {

    // For translators: Caption of the dialog
    string caption = _("IrDA Configuration");

    // help text for IrDA 1/4
    string help = _("<p>Here, configure an infrared interface (<b>IrDA</b>) for your computer.</p>") +

    // help text for IrDA 2/4
    _("<p>Choose the correct serial port for <b>Port</b>. Refer to your BIOS setup to find out which is correct.</p>") +

    // help text for IrDA 3/4
    _("<p>To test if it works, put your IrDA device (phone, PDA, etc.) in range of your infrared port and push <b>Test</b>.</p>") +

    // help text for IrDA 4/4
    _("<p>For some mobile phones, the speed of the infrared connection must be limited. Try setting <b>Maximum Baud Rate</b> if you encounter problems.</p>");

    boolean start	= Irda::start;
    string port		= Irda::port;
    string baud_rate	= Irda::max_baud_rate;
    boolean brate_limited	= (baud_rate != "0");

    list ports	= [ "/dev/ttyS0", "/dev/ttyS1", "/dev/ttyS2", "/dev/ttyS3" ];
    list rates	= [ "9600", "19200", "38400", "57600", "115200" ];

    term con = `HBox (`HSpacing (3), `VBox (
        `VSpacing (2),
        `RadioButtonGroup(`id(`rd),
	    `Left(`HVSquash(`VBox (
                `Left (`RadioButton(`id(`no), `opt (`notify),
		    // radio button label
		    _("Do No&t Start IrDA"), !start)),
                `Left(`RadioButton(`id(`yes), `opt (`notify),
		    // radio button label
		    _("&Start IrDA"), start)))))
	),
        `VSpacing (),
	// frame label
	`Frame (_("Basic IrDA Settings"), `HBox (`HSpacing (),
	    `VBox (
		`VSpacing (),
		`ComboBox (`id(`ports), `opt(`notify, `hstretch, `editable),
		    // combobox label
		    _("&Port"), ports),
		`VSpacing (0.5),
		`Right(
		    // button label
		    `PushButton (`id(`test), `opt(`key_F6), _("&Test"))),
		`VSpacing (0.5)
	    ), `HSpacing ()
	)),
	`VSpacing (0.5),
	// frame label
	`Frame (_("Baud Rate Limit"), `HBox (`HSpacing (),
	    `VBox (
		`VSpacing (),
		`Left (`CheckBox (`id(`limited), `opt(`notify),
		    // checkbox label
		    _("&Limit Baud Rate"), brate_limited)),
		`VSpacing (0.5),
		`ComboBox (`id(`brate), `opt(`notify, `hstretch),
		    // combobox label
		    _("&Maximum Baud Rate"), rates),
		`VSpacing (0.5)
	    ), `HSpacing ()
	)),
	`VStretch ()
    ), `HSpacing(3));


    Wizard::SetContentsButtons (caption, con, help,
	Label::BackButton (), Label::FinishButton());

    UI::ChangeWidget (`id(`ports), `Value, port);

    foreach (symbol widget, [`ports, `test, `limited],{
	UI::ChangeWidget (`id (widget), `Enabled, start);
    });

    UI::ChangeWidget (`id (`brate), `Enabled, start && brate_limited);
    if (brate_limited && contains (rates, baud_rate))
    {
	UI::ChangeWidget (`id(`brate), `Value, baud_rate);
    }

    symbol ret = nil;
    do
    {
	ret		= (symbol) UI::UserInput ();
	port		= (string) UI::QueryWidget (`id(`ports), `Value);

	if (brate_limited)
	{
	    baud_rate	= (string) UI::QueryWidget (`id(`brate), `Value);
	}
        if (ret == `yes || ret == `no)
	{
            start = (ret == `yes);
	    if (start && !Package::InstalledAll (["irda"]))
	    {
		if (Package::InstallAll (["irda"]))
		{
		    Irda::ReadSysconfig ();
		    port = Irda::port;
		    UI::ChangeWidget (`id(`ports), `Value, port);
		}
		else
		{
		    start = false;
		    UI::ChangeWidget (`id(`rd), `CurrentButton, `no);
		}
	    }
	    foreach (symbol widget, [`ports, `test, `limited], {
		UI::ChangeWidget (`id (widget), `Enabled, start);
	    });
	    UI::ChangeWidget (`id (`brate), `Enabled, start && brate_limited);
	}
	if (ret == `limited)
	{
	    brate_limited = (boolean) UI::QueryWidget (`id(`limited), `Value);
	    UI::ChangeWidget (`id (`brate), `Enabled, start && brate_limited);
	    if (!brate_limited)
		baud_rate	= "0";
	}
	if (ret == `test)
	{
	    TestPopup (port);
	}

    } while (!contains ([`back, `abort, `cancel, `next, `ok], ret));

    if (ret == `next &&
	(start != Irda::start || port != Irda::port ||
	baud_rate != Irda::max_baud_rate))
    {
	Irda::modified		= true;
	Irda::start		= start;
	Irda::port		= port;
	Irda::max_baud_rate	= baud_rate;

	if (start &&
	    SCR::Read (.target.size, "/dev/pilot") == -1 &&
	    Package::Installed ("pilot-link") &&
	    // yes/no popup
	    Popup::YesNo (_("If you have a Palm device using an IrDA port,
it is useful to have a symbolic link between the IR device and the pilot device.
Create it now?")))
	{
	    Irda::link_pilot	= true;
	}
    }
    return ret;
}

/**
 * Thew whole sequence
 */
define symbol IrDASequence () {

    Wizard::CreateDialog();
    Wizard::SetDesktopIcon("irda");

    Irda::Read ();

    symbol ret = IrDADialog ();
    if (ret == `next || ret == `finish)
    {
	Irda::Write ();
    }

    UI::CloseDialog ();
    return ret;
}

/* EOF */
}
