/**
 * File:	include/network/modem/dialogs.ycp
 * Package:	Network configuration
 * Summary:	Modem configuration dialogs
 * Authors:	Michal Svec <msvec@suse.cz>
 *
 * $Id: dialogs.ycp 21341 2005-02-07 16:15:59Z mvidner $
 */

{

textdomain "network";

import "CWM";
import "Label";
import "Modem";
import "Popup";
import "Wizard";

include "network/routines.ycp";
include "network/widgets.ycp";

/**
 * Modem dialog
 * @param detected true if the type is detected
 * @return dialog result
 */
define symbol ModemDialog(boolean detected) {

    ScreenName("modem-dialog");

    /* PREPARE VARIABLES */

    /* FIXME make the Connection dialog optional in the provider dialog */

    list devices = maplist(string e, toset([ Modem::Device, "/dev/modem",
		"/dev/ttyS0", "/dev/ttyS1", "/dev/ttyS2", "/dev/ttyS3",
		"/dev/ttyACM0", "/dev/ttyACM1", "/dev/ttyACM2", "/dev/ttyACM3" ]), {
	return `item(`id(sformat("%1", e)), sformat("%1", e), e == Modem::Device);
    });

    /* DIALOG TEXTS */

    /* Modem dialog caption */
    string caption = _("Modem Parameters");

    /* Modem dialog help 1/5 */
    string helptext = _("<p>Enter all modem configuration values.</p>") +

    /* Modem dialog help 2/5 */
_("<p><b>Modem Device</b> specifies to which port your modem is connected. ttyS0,
ttyS1, etc., refer to serial ports and usually correspond to COM1, COM2, etc.,
in DOS/Windows. ttyACM0 and ttyACM1 refer to USB ports.</p>") +

    /* Modem dialog help 3/5 */
_("<p>If you are on a PBX, you probably need to enter a <b>Dial Prefix</b>.
Often, this is <i>9</i> or <i>0</i>.</p>
") +

    /* Modem dialog help 4/5 */
_("<p>Choose <b>Dial Mode</b> according to your phone link. Most telephone
companies use <i>Tone Dial</i> as the <b>Dial Mode</b>. Check the additional
check boxes to turn on your modem speaker (<i>Speaker On</i>) or for your
modem to wait until it detects a dial tone (<i>Detect Dial Tone</i>).</p>
") +

    /* Modem dialog help 5/5 */
_("<p>Press <b>Details</b> to configure the baud rate and the modem 
initialization strings.</p>");

    /* DIALOG CONTENTS */

    term DeviceTerm = nil;

    if(detected == true)
	DeviceTerm = `Left(`HBox(
        /* Label text */
	    `Label(_("Modem Device:")),
	    `HSpacing(0.5),
	    `Label(`opt(`outputField), Modem::Device)
	));
    else
    /* ComboBox label */
	DeviceTerm = `ComboBox(`id(`Device), `opt(`hstretch, `editable), _("Modem De&vice"), devices);

    term contents = `HBox(
	`HSpacing(6),
	`VBox(
	    `VSpacing(0.2),
	    DeviceTerm,
	    `VSpacing(1),
	    `HBox(
		//`TextEntry(`id(`ModemName), _("&Modem name"), name),
        /* TextEntry label */
		`TextEntry(`id(`DialPrefix), _("Dial Prefi&x (if needed)"), Modem::DialPrefix)
	    ),
	    `VSpacing(0.8),
	    `HBox(
        /* Frame label */
		`Frame(_("Dial Mode"),
		    `VBox(
			`VSpacing(0.3),
			`HBox(
			    `HSpacing(0.3),
			    `RadioButtonGroup(`id(`DialMode),
				`VBox(
                    /* RadioButton label */
				    `Left(`RadioButton(`id(`Tone), _("&Tone Dialing"), !Modem::PulseDial)),
                    /* RadioButton label */
				    `Left(`RadioButton(`id(`Pulse), _("&Pulse Dialing"), Modem::PulseDial))
				)
			    ),
			    `HSpacing(0.3)
			),
			`VSpacing(0.3)
		    )
		),
		`HSpacing(1),
        /* Frame label */
		`Frame(_("Special Settings"),
		    `HBox(
			`HSpacing(0.3),
			`VBox(
			    `VSpacing(0.3),
                /* Checkbox label */
			    `Left(`CheckBox(`id(`Speaker), _("&Speaker On"), Modem::Speaker)),
                /* Checkbox label */
			    `Left(`CheckBox(`id(`CarrierDetect), _("D&etect Dial Tone"), Modem::Carrier)),
			    `VSpacing(0.3)
			),
			`HSpacing(0.3)
		    )
		)
	    ),
	    `VSpacing(1),
        /* Button label */
	    `PushButton(`id(`Details), _("&Details")),
	    `VSpacing(0.2)
	),
	`HSpacing(6)
    );

    /* DIALOG PREPARE */

    Wizard::SetContentsButtons(caption, contents, helptext,
	    Label::BackButton(), Label::NextButton());

    /* MAIN CYCLE */

    symbol ret = nil;
    while(true) {

	ret = (symbol) UI::UserInput();

	/* abort? */
	if(ret == `abort || ret == `cancel) {
	    if(ReallyAbort()) break;
	    else continue;
	}
	/* back */
	else if(ret == `back) {
	    break;
	}
	/* next */
	else if(ret == `next || ret == `Details) {
	    /* check_* */
	    break;
	}
	else {
	    y2error("unexpected retcode: %1", ret);
	    continue;
	}

    }

    /* UPDATE VARIABLES */

    if(ret == `next || ret == `Details) {
	Modem::DialPrefix = (string) UI::QueryWidget(`id(`DialPrefix), `Value);

	if(detected != true)
	    Modem::Device = (string) UI::QueryWidget(`id(`Device), `Value);

	Modem::PulseDial = (boolean) UI::QueryWidget(`id(`Pulse), `Value);
	Modem::Speaker = (boolean) UI::QueryWidget(`id(`Speaker), `Value);
	Modem::Carrier = (boolean) UI::QueryWidget(`id(`CarrierDetect), `Value);
    }

    return ret;
}

/**
 * Modem details dialog
 * @return dialog result
 */
define symbol ModemDetailsDialog() {

    ScreenName("modem-details");

    /* PREPARE VARIABLES */
    integer BaudRate = Modem::BaudRate;
    string Init1 = Modem::Init1;
    string Init2 = Modem::Init2;
    string Init3 = Modem::Init3;
    boolean usercontrol = Modem::usercontrol;
    string DialPrefixRx = Modem::DialPrefixRx;


    list< map<string, any> > widgets = CWM::CreateWidgets (
	["USERCONTROL", "DIALPREFIXREGEX", ],
	widget_descr);

    /* DIALOG TEXTS */

    /* Modem datails dialog caption */
    string caption = _("Modem Parameter Details");

    /* Modem datails dialog help 1/2 */
    string helptext = _("<p><b>Baud Rate</b> is a transmission speed that tells
how many bits per second your computer communicates with your modem.</p>
") +

    /* Modem datails dialog help 2/2 */
_("<p>All the relevant information about <b>Init Strings</b>
should be in your modem manual.</p>
") +

	CWM::MergeHelps (widgets);


    /* DIALOG CONTENTS */

    list BaudRates = maplist(integer e, toset([1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, BaudRate]), {
	return `item(`id(sformat("%1", e)), sformat("%1", e), e == BaudRate);
    });

    term contents = `HBox(
	`HSpacing(6),
	`VBox(
	    /* Combo box label */
	    `ComboBox(`id(`Baud), `opt(`hstretch, `editable), _("B&aud Rate"), BaudRates),
	    `VSpacing(0.5),
		/* Frame label */
		`Frame(_("Modem Initialization Strings"),
		`HBox(
		    `HSpacing(0.2),
		    `VBox(
			/* Text entry label */
			`TextEntry(`id(`Init1), _("Init &1"), Init1),
			`VSpacing(0.5),
			/* Text entry label */
			`TextEntry(`id(`Init2), _("Init &2"), Init2),
			`VSpacing(0.5),
			/* Text entry label */
			`TextEntry(`id(`Init3), _("Init &3"), Init3),
			`VSpacing(0.4)
		    ),
		    `HSpacing(0.2)
		)
	    ),
	    `VSpacing (1),
	    // 0 is index to CreateWidgets... ugly
	    `Left (widgets[0, "widget"]:`Empty ()),
	    `VSpacing (0.5),
	    `Left (widgets[1, "widget"]:`Empty ()),
	    `VSpacing (1)
	),
	`HSpacing(6)
    );


    /* DIALOG PREPARE */

    Wizard::SetContentsButtons(caption, contents, helptext,
	    Label::BackButton(), Label::OKButton());
    UI::ChangeWidget (`id ("USERCONTROL"), `Value, usercontrol);
    UI::ChangeWidget (`id ("DIALPREFIXREGEX"), `Value, DialPrefixRx);

    /* MAIN CYCLE */

    any ret = nil;
    while(true) {
	usercontrol = (boolean) UI::QueryWidget (`id ("USERCONTROL"), `Value);
	UI::ChangeWidget (`id ("DIALPREFIXREGEX"), `Enabled, usercontrol);

	ret = UI::UserInput();

	/* abort? */
	if(ret == `abort || ret == `cancel) {
	    if(ReallyAbort()) break;
	    else continue;
	}
	else if(ret == `back) {
	    break;
	}
	else if(ret == `next) {
	    /* check_* */
	    break;
	}
	else if(ret != "USERCONTROL")
	{
	    y2error("unexpected retcode: %1", ret);
	    continue;
	}

    }

    /* UPDATE VARIABLES */

    if(ret == `next) {
	Modem::BaudRate = tointeger(UI::QueryWidget(`id(`Baud), `Value));
	Modem::Init1 = (string) UI::QueryWidget(`id(`Init1), `Value);
	Modem::Init2 = (string) UI::QueryWidget(`id(`Init2), `Value);
	Modem::Init3 = (string) UI::QueryWidget(`id(`Init3), `Value);
	Modem::usercontrol = usercontrol;
	if (usercontrol)
	{
	    Modem::DialPrefixRx = (string) UI::QueryWidget (`id ("DIALPREFIXREGEX"), `Value);
	}
    }

    return (symbol) ret;
}

/* EOF */
}
