/**
 * File:	include/network/installation/dialogs.ycp
 * Package:	Network configuration
 * Summary:	Configuration dialogs for installation
 * Authors:	Michal Svec <msvec@suse.cz>
 *		Arvin Schnell <arvin@suse.de>
 *
 * $Id: dialogs.ycp 20164 2004-11-04 09:49:59Z mvidner $
 */

{

textdomain "network";

import "Mode";
import "Internet";
import "Label";
import "Popup";
import "Product";
import "String";
import "Wizard";

include "network/routines.ycp";

/**
 * Ask for password if required
 * @return true on success
 */
define boolean AskForPassword() {

    if (Internet::askpassword == nil)
	return true;

    if (Internet::askpassword == false)
	return true;

    UI::OpenDialog(`VBox(
	/* Heading text */
	`Heading(_("Enter Provider Password")),
	`Password(`id(`password), Label::Password ()),
	`HBox(
	    `PushButton(`id(`ok), `opt(`default), Label::OKButton()),
	    `PushButton(`id(`cancel), Label::CancelButton()))
    ));

    UI::SetFocus(`id(`password));

    any ret = UI::UserInput();

    if(ret == `ok)
	Internet::password = (string) UI::QueryWidget(`id(`password), `Value);

    UI::CloseDialog();

    return ret == `ok;
}

/**
 * Connection steps dialog
 * @return dialog result
 */
define symbol TestStepsDialog() {

    ScreenName("installation-steps-dialog");

    /* Steps dialog caption */
    string caption = _("Test Internet Connection");

    /* Steps dialog caption 1/2 */
    string help = _("<p>Here, validate the Internet connection just
configured. The test is entirely optional.</p>");

    if(Product::run_you) {
	/* Steps dialog caption 2/2 */
	help = help + _("<p>A successful result enables you to run
the YaST Online Update.</p>");
    }

    /* Label text (keep lines max. 65 chars long) */
    string label = _("To validate your Internet access,
activate the test procedure.");

    map labels = $[
	/* Label text (keep lines max. 65 chars long) */
	"dsl" : _("To validate your DSL Internet access,
activate the test procedure."),
	/* Label text (keep lines max. 65 chars long) */
	"isdn" : _("To validate your ISDN Internet access,
activate the test procedure."),
	/* Label text (keep lines max. 65 chars long) */
	"modem" : _("To validate your modem Internet access,
activate the test procedure."),
    ];

    if(haskey(labels, Internet::type))
	label = labels[Internet::type]:"";

    boolean already_up = false;
    if(!Mode::test ()) already_up = Internet::Status();

    /* Radiobuttons */
    term buttons = `VBox(
	`VSpacing(1),
	/* RadioButton label */
	`Left(`RadioButton(`id(`yes), _("&Yes, Test Connection to the Internet"), Internet::do_test)),
	/* RadioButton label */
	`Left(`RadioButton(`id(`no), _("N&o, Skip This Test"), !Internet::do_test)),
	`VSpacing(1)
    );

    /* the steps */
    term steps = `VBox();
    if(!already_up) {
	/* label text - one step of during network test */
	steps = add(steps, `Left(`Label(_("- Connect to the Internet"))));
    }
    if(true) {
	/* label text - one step of during network test */
	steps = add(steps, `Left(`Label(_("- Download latest release notes"))));
    }
    if(Product::run_you) {
	/* label text - one step of during network test */
	steps = add(steps, `Left(`Label(_("- Check for latest updates"))));
    }
    if(!already_up) {
	/* label text - one step of during network test */
	steps = add(steps, `Left(`Label(_("- Close connection"))));
    }

    /* Steps dialog contents */
    term contents = `HBox(
	`HSpacing(5),
	`VBox(
	    `Left(`Label(label)),
	    `VSpacing(1),
	    /* Heading text */
	    `Left(`Heading(_("The following steps will be performed:"))),
	    `VSpacing(1),
	    steps,
	    `VSpacing(2),
	    /* Frame label */
	    `Frame(_("Select:"), `RadioButtonGroup(`id(`rb), `HBox(
		`HSpacing(2),
		buttons,
		`HSpacing(2)
	    ))),
	    `VSpacing(1)
	),
	`HSpacing(5)
    );

    Wizard::SetContents(caption, contents, help, true, true);

    any ret = nil;

    while(true)
    {
	ret = UI::UserInput();

	if (ret == `abort || ret == `cancel)
	{
	    if (Popup::ConfirmAbort (`incomplete))
		break;
	    continue;
	}
	else if (ret == `next || ret == `back)
	{
	    break;
	}
	y2error("Unexpected return code: %1", ret);
    }

    Internet::do_test = UI::QueryWidget(`id(`rb), `CurrentButton) == `yes;
    y2debug("Internet::do_test=%1", Internet::do_test);

    return (symbol) ret;
}

/**
 * Connection test dialog
 * @return dialog result
 */
define void AskYOUDialog() {

    ScreenName("installation-you-dialog");

    /* Radiobuttons */
    term buttons = `VBox(
	`VSpacing(1),
	/* RadioButton label */
	`Left(`RadioButton(`id(`yes), _("&Yes, Run Online Update Now"), true)),
	/* RadioButton label */
	`Left(`RadioButton(`id(`no), _("N&o, Skip Update"), false)),
	`VSpacing(1)
    );

    /* Dialog Content */
    term content = `HBox(
	`HSpacing(1),
	`VBox(
	    `VSpacing(1),
	    /* Heading text */
	    `Left(`Heading(_("Online Updates Available"))),
	    `VSpacing(1),
	    /* Label text */
	    `Label(_("Download and install them via the YaST Online Update?")),
	    `RadioButtonGroup(`HBox(
		`HSpacing(2),
		buttons,
		`HSpacing(2)
	    )),
	    `HBox(`PushButton(`opt(`default), Label::OKButton())),
	    `VSpacing(0.5)
	),
	`HSpacing(1)
    );

    UI::OpenDialog(content);
    UI::UserInput();

    Internet::do_you = (boolean) UI::QueryWidget(`id(`yes), `Value);
    y2debug("Internet::do_you=%1", Internet::do_you);

    UI::CloseDialog ();
}


/**
 * Show several log files.
 * @param logs log files
 * @param logdir log files directory
 */
define void ShowLogs(list<map> logs, string logdir) {

    ScreenName("installation-show-logs");

    logs = sort(map x, map y, logs, {
	return x[`prio]:0 > y[`prio]:0;
    });

    list menunames = [];
    maplist(map v, logs, {
	menunames = add(menunames, v[`menuname]:"none");
    });

    term content = `VBox(
	/* Heading */
	`Left(`Heading(_("Internet Connection Test Logs:"))),
	`HSpacing(70),
	`HBox(
	    `HSpacing(1.0),
	    /* ComboBox label */
	    `ComboBox(`id(`selector), `opt(`notify, `hstretch),
		_("&Select Log:"), menunames),
	    `HStretch()
	),
	`HBox(
	    `VSpacing(18),
	    `HSpacing(0.5),
	    `RichText(`id(`log), ""),
	    `HSpacing(0.5)
	),
	`PushButton(`id(`ok), `opt(`default), Label::OKButton())
    );

    UI::OpenDialog(content);

    map tmp1 = logs[0]:$[];
    string filename = tmp1[`filename]:"none";

    while(true)
    {
	/* Read file and fill logview */
	string tmp2 = (string) SCR::Read(.target.string, logdir + "/" + filename);
	if(tmp2 == nil) tmp2 = "file not found";
	UI::ChangeWidget(`id(`log), `Value, "<pre>" +
			 String::EscapeTags (tmp2) + "</pre>");

	any ret = UI::UserInput();

	if(ret == `ok)
	{
	    break;
	}
	else if(ret == `selector)
	{
	    string menuname = (string) UI::QueryWidget(`id(`selector), `Value);

	    maplist(map v, logs, {
		if(v[`menuname]:"none" == menuname)
		    filename = v[`filename]:"none";
	    });
	}
    }

    UI::CloseDialog();
}

/* EOF */
}
