/**
 * File:
 *   include/network/isdn/interface.ycp
 *
 * Package:
 *   Configuration of network
 *
 * Summary:
 *   ISDN network interface configuration dialog
 *
 * Authors:
 *   Karsten Keil <kkeil@suse.de>
 *
 * $Id: interface.ycp 21341 2005-02-07 16:15:59Z mvidner $
 *
 */

{

textdomain "network";

import "Call";
import "CWM";
import "ISDN";
import "Label";
import "Popup";
import "Provider";
import "SuSEFirewall4Network";
import "Wizard";

include "network/dsl/dialogs.ycp";
include "network/widgets.ycp";

/**
 * Dialog for ISDN interface selection
 * @param symbol op operation
 *		`hw  called after HW config
 *		`add call from overview
 * @return any user input
 */

define any isdn_if_sel(symbol op) ``{

    /* DIALOG TEXTS */

    // title of ISDN service selection dialog
    string caption = _("ISDN Service Selection");
    string helptext = "";

    if (ISDN::have_dsl)
	helptext = helptext +
    // conditional help text 1/4
_("<p>If you have a combined ISDN and DSL CAPI controller, configure your DSL
connection via <b>Add DSL CAPI Interface</b>. You can also do this later
in the DSL configuration dialog.</p>
");

    helptext = helptext +
    // help text 1/4
_("<p>For networking over ISDN, there are two types of interfaces:
<b>RawIP</b> and <b>SyncPPP</b>. In most cases, use SyncPPP. It is
the default for all common Internet providers.</p>
");

    helptext = helptext +
    // helptext text 2/4
_("<p>To switch between various Internet providers, an
interface for each provider is not required. Simply add multiple providers to the
same interface.</p>
");

    helptext = helptext +
    // helptext text 3/4
_("<p>To avoid adding an interface now, use
<b>Skip</b> not to enter the interface and provider dialogs.</p>");


    if (ISDN::only_dsl)
	helptext =
    // conditional help text 1/1
_("<p>You have a DSL CAPI controller.  Configure your DSL
connection via <b>Add DSL CAPI Interface</b>. You can also do this later
in the DSL configuration dialog.</p>");


    /* DIALOG CONTENTS */
    term network = `VBox();
    network = add(network, `VSpacing(0.5));
    if (ISDN::have_dsl)
	// PushButton label to select the next Dialog
	network = add(network, `PushButton(`id(`AddDSLPPP),`opt(`hstretch), _("Add &DSL CAPI Interface")));
    // PushButton label to select the next Dialog
    if (!ISDN::only_dsl) {
	network = add(network, `PushButton(`id(`AddSyncPPP),`opt(`hstretch), _("Add New &SyncPPP Network Interface")));
	// PushButton label to select the next Dialog
	network = add(network, `PushButton(`id(`AddRawIP), `opt(`hstretch), _("Add New Raw&IP Network Interface")));
	// PushButton label to select the next Dialog
	network = add(network, `PushButton(`id(`AddProvider),`opt(`hstretch), _("Add &Provider to Existing Interface")));
    }
    network = add(network, `VSpacing(0.5));

    term contents =
	`HVSquash(
	    // Frame title
	    `Frame(_("Network Services"),
		`HBox(
		    `HSpacing(0.5),
		    network,
		    `HSpacing(0.5)
		)
	    )
	);

    /* DIALOG PREPARE */

    // PushButton label to not enter any sub dialogs
    Wizard::SetNextButton (`next, _("S&kip") );
    Wizard::SetContents(caption, contents, helptext, true, true);

    if (ISDN::CountIF() == 0) {
	UI::ChangeWidget(`id(`AddProvider), `Enabled, false);
    }
    ISDN::skip = false;

    /* MAIN CYCLE */
    any ret = nil;
    while (true) {

	ret = UI::UserInput();

	/* abort? */
	if(ret == `abort || ret == `cancel) {
	    if (Popup::ReallyAbort(true))
		break;
	    else
		continue;
	}
	else if (ret == `back) {
	    break;
	}
	else if (ret == `next) {
	    break;
	}
	else if (ret == `AddDSLPPP) {
	    map dslpara = $[
		"type"      : "dsl",
		"name"	    : ISDN::hw_device["NAME"]:"unknown",
		"unique"    : ISDN::hw_device["UNIQUE"]: "",
		"pppmode"   : "capi-adsl",
		"startmode" : "manual"
	    ];
	    ret = Call::Function("dsl", [.capiadsl, dslpara]);
	}
	else if (ret == `AddSyncPPP) {
	    if (op == `hw && ISDN::type == "contr")
		ISDN::Commit();
	    ISDN::AddIf("syncppp");
            Provider::Add("isdn");
	    break;
	}
	else if (ret == `AddRawIP) {
	    if (op == `hw && ISDN::type == "contr")
		ISDN::Commit();
	    ISDN::AddIf("rawip");
            Provider::Add("isdn");
	    break;
	}
	else if (ret == `AddProvider) {
	    if (op == `hw && ISDN::type == "contr")
		ISDN::Commit();
	    ISDN::operation = `addprov;
            Provider::Add("isdn");
	    break;
	}
	else {
	    y2error("unexpected retcode: %1", ret);
	    continue;
	}

    }
    if (ret == `next) {
        y2debug("Provider::Current=%1", Provider::Current);
        y2debug("ISDN::interface=%1", ISDN::interface);
        ISDN::skip = true;
    }

    return ret;
}

/**
 * Dialog for ISDN interface settings
 * @return any user input
 */
define any interface_dialog() ``{

    /* PREPARE VARIABLES */
    string msn = ISDN::interface["MSN"]:"0";
    string startmode = ISDN::interface["STARTMODE"]:"manual";
    boolean usercontrol = ISDN::interface["USERCONTROL"]:"no" == "yes";
    boolean syncppp = ISDN::interface["PROTOCOL"]:"syncppp" == "syncppp";
    boolean chargeHUP = ISDN::interface["CHARGEHUP"]:"on" == "on";
    boolean multilink = ISDN::interface["MULTILINK"]:"no" == "yes";
    boolean firewall = ISDN::interface["FIREWALL"]:"yes" == "yes";
    // temp var while we're in the detailed dialog
    boolean efwi = (boolean) ISDN::interface["efwi"]:nil;
    boolean ExternalFwInterface = true;
    string devstr = sformat("%1%2", syncppp ? "ippp" : "isdn", ISDN::device);

    if (efwi == false)
	ExternalFwInterface = false;
    if (ISDN::operation  == `editif && efwi == nil) {
	ExternalFwInterface = SuSEFirewall4Network::IsProtectedByFirewall (devstr);
    }
    if (efwi != nil)
	ISDN::interface = remove(ISDN::interface, "efwi");
    y2debug("ExternalFwInterface=%1",ExternalFwInterface);

    /* DIALOG TEXTS */
    string fcaption = ISDN::operation == `editif ?
	// dialog caption, %1: SyncPPP or RawIP, %2: interface name, eg. ppp0
	_("Edit %1 Interface %2") :
	// dialog caption, %1: SyncPPP or RawIP, %2: interface name, eg. ppp0
	_("Add %1 Interface %2");
    string caption = sformat(fcaption, syncppp ? "SyncPPP" : "RawIP", devstr);

    widget_descr["STARTMODE"] = MakeStartmode (
	["auto", "hotplug", "manual", "off", ]);

    string helptext =
    // help text 1/5
_("<p>My phone number --  As your own telephone number (MSN), put in your 
telephone number (without area code) if your ISDN card is connected directly
to the phone company-provided socket. If it is connected to a PBX, put in the
MSN stored in the PBX (e.g., your phone extension or the last digit or digits
of your phone extension) . If this fails, try using 0, which normally means
the default MSN is actually used.</p>");

    helptext = helptext + widget_descr["STARTMODE", "help"]:"";

    helptext = helptext +
    // help text 3/5
_("<p>If you select manual, start and stop the service manually by
issuing the following commands (while logged in as 'root'):
<tt>
 <br> <b>start: </b>ifup ippp0
 <br> <b>stop : </b>ifdown ippp0
 <br>
</tt>
Note: ippp0 is an example</p>");

    helptext = helptext + widget_descr["USERCONTROL", "help"]:"";

    helptext = helptext +
    // help text 4/5
_("<p>Selecting <b>channel bundling</b> sets up a 128-kBit connection
also known as Multilink PPP. To activate or deactivate the second channel,
use the following commands:
<tt>
 <br> isdnctrl addlink ippp0
 <br> isdnctrl removelink ippp0
 <br>
</tt>
You can also install the package <b>xibod</b> to have this happen automatically. If
there is a demand for more bandwidth, it adds a channel. If the traffic goes down, it 
removes a channel.
</p>
");

    helptext = helptext +
    // help text 5/5
_("<p>Selecting
<b>External Firewall Interface</b> activates the firewall
and sets this interface as external.
<b>Restart Firewall</b> restarts the firewall if a connection is established.
</p>");


    /* DIALOG CONTENTS */
    list provlist = [];
    term prov = `VSpacing(0.0);
    float afterprov = 0.0;
    if(ISDN::operation  == `editif) {
	Provider::Add("isdn");
	provlist = Provider::GetProviders("isdn", "_custom", ISDN::provider_file);
	y2debug("provlist: %1", provlist);
	/* ComboBox label */
	prov = `Left(`ComboBox(`id(`prov), _("D&efault Provider"), provlist));
	afterprov = 0.5;
    }

    y2debug("device %1", devstr);

    term FirewallCheckbox = `HBox(
	`HWeight (1,
	    `Left(`CheckBox(`id(`FirewallExt), _("External Fire&wall Interface"), ExternalFwInterface))),
	`HSpacing (1),
	`HWeight (1,
		  /* CheckBox label */
	    `Left(`CheckBox(`id(`Firewall), _("Restart Fire&wall"), firewall)))
    );

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

    term contents =
    `Top(
	`VBox(
	    `VSpacing(1.5),
        // Frame title
        `Frame(_("Connection Settings"),
        `HBox(
        `HSpacing(1),
        `VBox(
	    `VSpacing(0.5),
	    // TextEntry label
	    `Left(`TextEntry(`id(`msn), _("My &Phone Number"), msn)),
	    `VSpacing(0.5),
	    `HBox(
		`VBox(
		    `HBox (
			`HWeight (1, `Top (
				      // STARTMODE
				      `Left (widgets[1, "widget"]:`Empty ())
				      )),
			`HSpacing (1),
			`HWeight (1, `Top (`VBox (
					       `Label (""),
					       `Left (widgets[0, "widget"]:`Empty ())
					       )))
			),
		    `VSpacing(0.5),
		    prov,
		    `VSpacing(afterprov),
			// CheckBox label
			`Left(`CheckBox(`id(`chargehup), _("Charge&HUP"), chargeHUP)),
		    `VSpacing(0.5),
			// CheckBox label
			`Left(`CheckBox(`id(`multilink), _("Ch&annel Bundling"), multilink)),
		    `VSpacing(0.5),
			// CheckBox label
			FirewallCheckbox,
		    `VSpacing(0.5)
		))
        ),
        `HSpacing(1)
        )),
	    `VSpacing(0.5),
	    // PushButton label
	    `PushButton(`id(`detail), _("&Details..."))
	));


    /* DIALOG PREPARE */
    Wizard::SetContentsButtons(caption, contents, helptext,
	    Label::BackButton(), Label::NextButton());

    UI::ChangeWidget (`id ("USERCONTROL"), `Value, usercontrol);
    UI::ChangeWidget (`id ("STARTMODE"), `Value, startmode);
    UI::SetFocus(`id(`msn));

    /* MAIN CYCLE */
    any ret = nil;
    while (true) {

    /* We need ":"  for NI1 SPID and "VB" in future (for pending DATA over VOICE patch)
     * it should not hurt if we enable this now here, since the chance that a
     * customer enter V or B by accident is very low
     * since VB is only allowed as first char, maybe we should make a post
     * check for it too
     */
    UI::ChangeWidget(`id(`msn), `ValidChars, "0123456789:BV" );

	ret = UI::UserInput();

	/* abort? */
	if(ret == `abort || ret == `cancel) {
	    if (Popup::ReallyAbort(true))
		break;
	    else
		continue;
	}
	else if (ret == `back) {
	    break;
	}
	else if (ret == `next || ret == `detail) {
	    msn = (string) UI::QueryWidget(`id(`msn), `Value);
	    startmode = (string) UI::QueryWidget(`id("STARTMODE"), `Value);
	    usercontrol = (boolean) UI::QueryWidget (`id ("USERCONTROL"), `Value);
	    multilink = (boolean) UI::QueryWidget(`id(`multilink), `Value);
	    chargeHUP = (boolean) UI::QueryWidget(`id(`chargehup), `Value);
	    /* check_* */
	    break;
	}
	else {
	    y2error("unexpected retcode: %1", ret);
	    continue;
	}

    }
    if (ret == `next || ret == `detail) {

        /* update firewall info */
	/* Better update allways, maybe somebody changed it */

	firewall = (boolean) UI::QueryWidget(`id(`Firewall), `Value);
	ExternalFwInterface = (boolean) UI::QueryWidget(`id(`FirewallExt), `Value);
	SuSEFirewall4Network::ProtectByFirewall (devstr, "EXT", ExternalFwInterface);

	/* UPDATE VARIABLES */
	ISDN::interface = union(ISDN::interface, $[
	    "MSN"		: msn,
	    "STARTMODE"		: startmode,
	    "USERCONTROL"	: usercontrol ? "yes" : "no",
	    "MULTILINK"		: multilink ? "yes" : "no",
	    "CHARGEHUP"		: chargeHUP ? "on" : "off",
	    "FIREWALL"		: firewall ? "yes" : "no",
	]);
	if (ret == `detail) //temporary
	    ISDN::interface = add(ISDN::interface, "efwi", ExternalFwInterface);
	if (ISDN::operation  == `editif)
	    ISDN::provider_file = (string) UI::QueryWidget(`id(`prov), `Value);

    }
    return ret;
}

}
