/**
 * File:	include/network/provider/provider.ycp
 * Package:	Network configuration
 * Summary:	Provider dialogs
 * Authors:	Michal Svec <msvec@suse.cz>
 *
 * $Id: provider.ycp 21672 2005-02-17 16:49:17Z mvidner $
 */

{

textdomain "network";

import "ISDN";
import "Provider";
import "Wizard";

include "network/routines.ycp";
include "network/provider/helps.ycp";
include "network/provider/texts.ycp";

import "Popup";
import "Label";

/**
 * Build provider info text.
 * @return provider info text
 */
define string ProviderInfoText() ``{

    string info = "";
    string type = Provider::Type;

    string hp = Provider::Current["HOMEPAGE"]:"";
    string hl = Provider::Current["HOTLINE"]:"";

    if(hp != "")
	/* Provider info (%1 is URL) */
	info = info + sformat(_("<p>Home Page: %1</p>"), hp);

    if(hl != "")
	/* Provider info (%1 is phone number) */
	info = info + sformat(_("<p>Hot Line: %1</p>"), hl);


    if(Provider::Current["DIALMESSAGE1"]:"" != "" || Provider::Current["DIALMESSAGE2"]:"" != "") {
	info = info + sformat("<p>%1%2</p>",
		Provider::Current["DIALMESSAGE1"]:"",
		Provider::Current["DIALMESSAGE2"]:"");
    }
    else if(Provider::Current["PHONE"]:"" == "" && type != "dsl") {
	if(hp != "" && hl != "") {
	    /* Provider info text */
	    string it = _("<p>To register for <b>%1</b> and find the best
dialing number, connect to the home page <b>%2</b> or call the hot line
<b>%3</b>.</p>
");

	    info = info + sformat(it, Provider::Current["PROVIDER"]:"", hp, hl);
	}

	if(hp != "" && hl == "") {
	    /* Provider info text */
	    string it = _("<p>To register for <b>%1</b> and find the best
dialing number, connect to the home page <b>%2</b>.</p>");

	    info = info + sformat(it, Provider::Current["PROVIDER"]:"", hp);
	}

	if(hp == "" && hl != "") {
	    /* Provider info text */
	    string it = _("<p>To register for <b>%1</b> and find the best
dialing number, call the hot line <b>%2</b>.</p>
");

	    info = info + sformat(it, Provider::Current["PROVIDER"]:"", hl);
	}
    }

    return info;
}

/**
 * Provider dialog
 * @return dialog result
 */
define any ProviderDialog() ``{

    ScreenName("provider-dialog");

    string type = Provider::Type;

    string name = Provider::Current["PROVIDER"]:"";
    string phone = Provider::Current["PHONE"]:"";
    string encap = Provider::Current["ENCAP"]:"";
    string infotext = ProviderInfoText();

    string username = Provider::Current["USERNAME"]:"";
    string password = Provider::Current["PASSWORD"]:"";
    boolean ask_pass = Provider::Current["ASKPASSWORD"]:"no" == "yes";

    string uimode = Provider::Current["UIMODE"]:"";

    string lineid = "";
    string t_onlineid = "";
    string usercode = "";

/**
 * Create T-Online username from LineID, T-OnlineNo and UserCode
 *
 * It's concatenation of LineID (12 digits), T-OnlineNo (12 digits,
 * if less, then with appended '#', the user code (4 digits) and
 * finally of the string "@t-online.de" if the type is "dsl".
 *
 * T-Online Business DSL is created as
 * t-online-com/<12 character (real) username>@t-online-com.de
 */
define void tonline2username() ``{
    if(uimode == "T-Online DSL Business")
	username = "t-online-com/" + username + "@t-online-com.de";
    else {
	username = lineid + t_onlineid + (size(t_onlineid) < 12? "#":"" ) +
	    usercode + (uimode == "T-Online DSL" ? "@t-online.de" : "");
    }

    y2debug("T-Online: [%1,%2,%3] -> %4", lineid, t_onlineid, usercode, username);
}

/**
 * Split username to LineID, T-OnlineNo and UserCode
 * @see tonline2username
 */
define void username2tonline() ``{
    string user = username;
    usercode = "0001";
    lineid = "";
    t_onlineid = "";

    y2debug("user=%1",user);
    if(user != "") {

	if(uimode == "T-Online DSL Business") {
	    if(regexpmatch(user, "@t-online-com\.de$"))
		user = regexpsub(user, "^(.*)@t-online-com\.de$", "\\1");
	    y2debug("user=%1",user);

	    if(regexpmatch(user, "^t-online-com/"))
		user = regexpsub(user, "^t-online-com/(.*)$", "\\1");
	    y2debug("user=%1",user);
	    username = user;
	}
	else {
	    if(regexpmatch(user, "@t-online\.de$"))
		user = regexpsub(user, "^(.*)@t-online\.de$", "\\1");
	    y2debug("user=%1",user);

	    if(issubstring(user, "#")) {
		usercode = regexpsub(user, "^.*#(.*)$", "\\1");
		user = regexpsub(user, "^(.*)#.*$", "\\1");
	    }
	    else {
		if(size(user) > 3) {
		    usercode = regexpsub(user, "^.*(....)$", "\\1");
		    user = regexpsub(user, "^(.*)....$", "\\1");
		}
	    }
	    if(usercode == "") usercode = "0001";

	    y2debug("user=%1",user);
	    if(size(user) > 12) {
		lineid = regexpsub(user, "^(.{12}).*$", "\\1");
		t_onlineid = regexpsub(user, "^.{12}(.*)$", "\\1");
	    }
	    else
		lineid = user;
	}

    }
    y2debug("T-Online: %1 -> [%2,%3,%4]", username, lineid, t_onlineid, usercode);
}

    if(issubstring(uimode, "T-Online"))
	username2tonline();

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

    /* Provider dialog help 1/5 */
    string helptext = _("<p>Configure access to your Internet provider. If you have
selected your provider from the  list, these values are provided.</p>
") +

	(type == "dsl" ?
    /* Provider dialog help 1.5/5: DSL, thus no phone number */
    _("<p>Enter a <b>Provider Name</b> for the provider.</p>")
	 :
    /* Provider dialog help 1.5/5 */
    _("<p>Enter a <b>Provider Name</b> for the provider and a <b>Phone Number</b>
to access your provider.</p>")
	    );

    if(type == "isdn") /* FIXME: ISDN ??? */
	/* Provider dialog help 2/5 */
	helptext = helptext + _("<p>Select the type of packet encapsulation.
<b>RawIP</b> means that MAC headers are stripped. <b>SyncPPP</b> stands for
Synchronous PPP.</p>");

    if(uimode == "T-Online" || uimode == "T-Online DSL")

	/* Provider dialog help 4/5 (T-Online) */
	helptext = helptext + _("<p>Enter the <b>Line ID</b>
(e.g., 00056780362), the <b>T-Online Number</b> (e.g., 870008594732),
the <b>User Code</b> (typically 0001), and the <b>Password</b>
to use as the login (ask your provider if unsure).</p>");

    else

	/* Provider dialog help 4/5 (general) */
	helptext = helptext + _("<p>Enter the <b>User Name</b> and the
<b>Password</b> to use as the login (ask your provider if unsure).</p>");

    if(uimode == "T-Online DSL Business")

	/* Provider dialog help 4.5/5 (T-Online Business) */
	helptext = helptext + _("<p>The <b>User Name</b> will be extended
with the <i>t-online-com/</i> at the start and with <i>@t-online-com.de</i>
at the end.</p>");

    /* Provider dialog help 5/5 */
    helptext = helptext + _("<p>Check <b>Always Ask for Password</b> to be asked for the password every time.</p>");

    /* Frame label */
    term auth = `Frame(`id(`auth), _("Authorization"), `HBox(
	`HSpacing(0.5),
	`VBox(
	    /* TextEntry label */
	    `TextEntry(`id(`username), _("&User Name"), username),
	    `Label(""),
	    `VSpacing(0.2)
	),
	`HSpacing(0.5),
	`VBox(
	    `Password(`id(`password), Label::Password (), password),
	    /* CheckBox label */
	    `Left(`CheckBox(`id(`askpass), `opt(`notify), _("&Always Ask for Password"), ask_pass)),
	    `VSpacing(0.2)
	),
	`HSpacing(0.5)
    ));

    term encapbox = `Empty();
    if(type == "isdn") { /* FIXME: ISDN ??? */
	/* ComboBox label */
	encapbox = `Left(`ComboBox(`id(`encap), `opt(`notify), _("Packet &Encapsulation"), [
	    /* ComboBox item */
	    `item(`id("syncppp"), _("Synchronous PPP"), encap == "syncppp"),
	    /* ComboBox item */
	    `item(`id("rawip"), _("Raw IP"), encap == "rawip"),
	]));
	/*
	if (encap == "" && ISDN::operation == `addif)
		encap = ISDN::interface["PROTOCOL"]:"syncppp";
	*/
    }

    /* TextEntry label */
    term namebox = `TextEntry(`id(`name), _("Pr&ovider Name"), name);

    /* TextEntry label */
    term phonebox = `TextEntry(`id(`phone), _("P&hone Number"), phone);

    if(type == "dsl") {
	phonebox = namebox;
	namebox = `VSpacing(0);
    }

    phonebox = `HBox(
	phonebox,
	`HSpacing(0.5),
	`VBox(
	    `Label(""),
	    /* PushButton label */
	    `PushButton(`id(`info), `opt(`disabled), _("&Info"))
	)
    );

    /* Provider specific UI mode */
    // if(issubstring(uimode, "T-Online"))
    if(uimode == "T-Online" || uimode == "T-Online DSL") {
	y2debug("Using uimode=%1", uimode);

	auth = `Frame(`id(`auth), _("Authorization"), `VBox(
	    `HBox(
		`HSpacing(0.5),
		/* TextEntry label */
		`TextEntry(`id(`lineid), _("&Line ID"), lineid),
		`HSpacing(0.5),
		/* TextEntry label */
		`TextEntry(`id(`t_onlineid), _("&T-Online Number"), t_onlineid),
		`HSpacing(0.5)
	    ),
	    `VSpacing( 0.5 ),
	    `HBox(
		`HSpacing(0.5),
		`VBox(
		    /* TextEntry label */
		    `TextEntry(`id(`usercode), _("&User Code"), usercode),
		    `Label(""),
		    `VSpacing(0.2)
		),
		`HSpacing(0.5),
		`VBox(
		    `Password(`id(`password), Label::Password (), password),
		    /* CheckBox label */
		    `Left(`CheckBox(`id(`askpass), `opt(`notify), _("&Always Ask for Password"), ask_pass)),
		    `VSpacing(0.2)
		),
		`HSpacing(0.5)
	    )
	));

    }

    /* Provider dialog contents */
    term contents = `VBox(`VSpacing(2), `HBox(
	`HSpacing(4),
	`VBox(
	    `Left(`HBox(
		/* Label text */
		`Label(_("Name for Dialing:")),
		`HSpacing(0.5),
		`Label(`opt(`outputField), Provider::Name)
	    )),
	    `VSpacing(1),
	    namebox,
	    `VSpacing(0.5),
	    phonebox,
	    `VSpacing(0.5),
	    encapbox,
	    type == "isdn"?`VSpacing(1):`Empty(),
	    auth
	    //`VSpacing(0.5),
	    //ip
	),
	`HSpacing(4)
    ),`VSpacing(2));

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

    /* '*' and '#' are for GPRS connections (#16021) */
    /* ',' is for pausing dialing (#23678) */
    // `phone: in rare cases other characters are allowed (#43723)
    string phonevalidchars = "0123456789*#,";
    ChangeWidgetIfExists(`id(`usercode), `ValidChars, "0123456789#");
    ChangeWidgetIfExists(`id(`lineid), `ValidChars, "0123456789");
    ChangeWidgetIfExists(`id(`t_onlineid), `ValidChars, "0123456789");

    ChangeWidgetIfExists(`id(`password), `Enabled, !ask_pass);
    ChangeWidgetIfExists(`id(`info), `Enabled, infotext != nil && infotext != "");

    if(type == "isdn" && encap == "rawip") { /* FIXME: ISDN ??? */
	// seems that disabling via frame is not possible
	// UI::ChangeWidget(`id(`auth), `Enabled, false);
	ChangeWidgetIfExists(`id(`username), `Enabled, false);
	ChangeWidgetIfExists(`id(`usercode), `Enabled, false);
	ChangeWidgetIfExists(`id(`lineid), `Enabled, false);
	ChangeWidgetIfExists(`id(`t_onlineid), `Enabled, false);
	UI::ChangeWidget(`id(`password), `Enabled, false);
	UI::ChangeWidget(`id(`askpass), `Enabled, false);
    }

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

	ret = UI::UserInput();

	/* abort? */
	if(ret == `abort || ret == `cancel) {
	    if(ReallyAbort()) break;
	    else continue;
	}
	/* back */
	else if(ret == `back) {
	    break;
	}
	else if(ret == `encap) {
	    encap = (string) UI::QueryWidget(`id(`encap), `Value);
	    boolean e = encap == "syncppp";
	    // seems that disabling via frame is not possible
	    // UI::ChangeWidget(`id(`auth), `Enabled, encap == "syncppp");
	    ChangeWidgetIfExists(`id(`username), `Enabled, e);
	    ChangeWidgetIfExists(`id(`usercode), `Enabled, e);
	    ChangeWidgetIfExists(`id(`lineid), `Enabled, e);
	    ChangeWidgetIfExists(`id(`t_onlineid), `Enabled, e);
	    UI::ChangeWidget(`id(`password), `Enabled, e && !(boolean)UI::QueryWidget(`id(`askpass), `Value));
	    UI::ChangeWidget(`id(`askpass), `Enabled, e);
	    continue;
	}
	else if(ret == `info) {
	    /* Popup text header*/
	    Popup::LongText(_("Provider Information"), `RichText(infotext), 43, 13);
	    continue;
	}
	else if(ret == `askpass) {
	    UI::ChangeWidget(`id(`password), `Enabled, !(boolean)UI::QueryWidget(`id(`askpass), `Value));
	    continue;
	}
	/* next */
	else if(ret == `next) {

	    /* check_* */
	    name = (string) UI::QueryWidget(`id(`name), `Value);
	    username = (string) QueryWidgetIfExists(`id(`username), `Value, username);
	    password = (string) UI::QueryWidget(`id(`password), `Value);
	    ask_pass = (boolean) UI::QueryWidget(`id(`askpass), `Value);

	    phone = (string) QueryWidgetIfExists(`id(`phone), `Value, phone);

	    boolean auth_chk = true;
	    if(type == "isdn") { /* FIXME: ISDN ??? */
		encap = (string) UI::QueryWidget(`id(`encap), `Value);
		if (encap == "rawip")
			auth_chk = false;
	    }

	    // if(issubstring(uimode, "T-Online"))
	    if(uimode == "T-Online" || uimode == "T-Online DSL") {

		lineid = (string) UI::QueryWidget(`id(`lineid), `Value);
		t_onlineid = (string) UI::QueryWidget(`id(`t_onlineid), `Value);
		usercode = (string) UI::QueryWidget(`id(`usercode), `Value);

		if(lineid == "") {
		    /* Popup::Message text */
		    Popup::Message(_("Enter the line ID."));
		    UI::SetFocus(`id(`lineid));
		    continue;
		}
		if(t_onlineid == "") {
		    /* Popup::Message text */
		    Popup::Message(_("Enter the T-Online number."));
		    UI::SetFocus(`id(`t_onlineid));
		    continue;
		}
		if(usercode == "") {
		    /* Popup::Message text */
		    Popup::Message(_("Enter the user code."));
		    UI::SetFocus(`id(`usercode));
		    continue;
		}

		tonline2username();
	    }
	    else if(uimode == "T-Online DSL Business")
		tonline2username();

	    if(name != Provider::Current["PROVIDER"]:"" && !Provider::IsUnique(name)) {
		y2debug("n(%1), p(%2)",name,Provider::Current["PROVIDER"]:"");
		/* Popup::Message text */
		Popup::Message(sformat(_("Provider name %1 already exists."), name));
		UI::SetFocus(`id(`name));
		continue;
	    }
	    else if(name == "") {
		/* Popup::Message text */
		Popup::Message(_("Enter the provider name."));
		UI::SetFocus(`id(`name));
		continue;
	    }
	    else if(UI::WidgetExists(`id(`phone)) && phone == "") {
		/* Popup::Message text */
		Popup::Message(_("Enter the phone number."));
		UI::SetFocus(`id(`phone));
		continue;
	    }
	    else if(auth_chk && username == "") {
		/* Popup::Message text */
		Popup::Message(_("Enter the user name."));
		UI::SetFocus(`id(`username));
		continue;
	    }
	    /* password could be empty #16021
	    else if(!ask_pass && auth_chk && "" == pass)
	    {
		Popup::Message(_("Enter the password."));
		UI::SetFocus(`id(`passwd));
	    } */
	    else if(filterchars(phone, phonevalidchars) != phone &&
		    /* Popup::YesNo text */
		    !Popup::YesNo(_("You have entered some characters that are not numbers in the phone field.\n\nContinue?")))
	    {
		UI::SetFocus(`id(`phone));
		continue;
	    }
	    else
		break;
	}
	else {
	    y2error("unexpected retcode: %1", ret);
	    continue;
	}
    }

    if(ret == `next) {
	Provider::Current = union(Provider::Current, $[
	    "PROVIDER"		: name,
	    "USERNAME"		: username,
	    "PASSWORD"		: ask_pass? "" : password,
	    "PHONE"		: phone,
	    "ASKPASSWORD"	: ask_pass ? "yes" : "no",
	    "MODEMSUPPORTED"	: (type == "modem" ? "yes" : "no"),
	    "ISDNSUPPORTED"	: (type == "isdn" ? "yes" : "no"),
	    "DSLSUPPORTED"	: (type == "dsl" ? "yes" : "no"),
	]);
	if(type == "isdn") { /* FIXME: ISDN ??? */
	    Provider::Current = union(Provider::Current, $[
		"ENCAP"		: encap,
	    ]);
	    if(ISDN::operation == `addif)
		ISDN::provider_file = Provider::Name;
	}
    }

    return ret;
}

/* EOF */
}
