/**
 * File:	include/network/lan/dialogs.ycp
 * Package:	Network configuration
 * Summary:	Summary, overview and IO dialogs for network cards config
 * Authors:	Michal Svec <msvec@suse.cz>
 *
 * $Id: complex.ycp 22129 2005-03-02 10:04:23Z locilka $
 */

{

textdomain "network";

import "Lan";
import "DNS";
import "NetworkConfig";
import "NetworkModules";
import "Routing";

import "Wizard";
import "Wizard_hw";

import "Popup";
import "Label";

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

/**
 * Return a modification status
 * @return true if data was modified
 */
define boolean Modified() ``{
    return Lan::Modified() || DNS::modified || Routing::Modified() ||
	NetworkConfig::Modified();
}

/**
 * Commit changes to internal structures
 * @return always `next
 */
define symbol Commit() ``{
    Lan::Commit();
    return `next;
}

/**
 * Display finished popup
 * @return dialog result
 */
define symbol FinishDialog() ``{
    return FinishPopup(Modified(), "lan", "", "mail", ["permanent"]);
}

/**
 * Read settings dialog
 * @return `abort if aborted and `next otherwise
 */
define symbol ReadDialog() ``{

    ScreenName("lan-read");

    /* Network cards read dialog help 1/2 */
    string help = _("<p><b><big>Initializing Network Card
Configuration</big></b><br>Please wait...<br></p>
") +

    /* Network cards read dialog help 2/2 */
_("<p><b><big>Aborting the Initialization:</big></b><br>
Safely abort the configuration utility by pressing <B>Abort</B> now.</p>
");

    Wizard::RestoreHelp(help);
    Lan::AbortFunction = ``{return PollAbort();};
    boolean ret = Lan::Read();
    return ret ? `next : `abort;
}

/**
 * Write settings dialog
 * @return `abort if aborted and `next otherwise
 */
define symbol WriteDialog() ``{
    ScreenName("lan-write");

    if(!Modified()) return `next;

    /* Network cards write dialog help 1/2 */
    string help = _("<p><b><big>Saving Network Card
Configuration</big></b><br>Please wait...<br></p>
") +

    /* Network cards write dialog help 2/2 */
_("<p><b><big>Aborting Saving:</big></b><br>
Abort saving by pressing <b>Abort</b>.</p>
");

    Wizard::RestoreHelp(help);
    Lan::AbortFunction = ``{return PollAbort() && ReallyAbort();};
    boolean ret = Lan::Write();
    return ret ? `next : `abort;
}

/**
 * Main dialog
 * @return dialog result
 */
define any SummaryDialog() ``{

    ScreenName("lan-summary");

    /* Network cards summary dialog caption */
    string caption = _("Network Card Configuration");

    /* Network cards summary dialog help 1/3 */
    string help = _("<p><b><big>Network Card Setup</big></b><br>
Configure your network card here.<br></p>
") +

    /* Network cards summary dialog help 2/3 */
_("<p><b><big>Adding a Network Card:</big></b><br>
Choose a network card from the list of detected network cards.
If your network card was not autodetected, select <b>Other (not detected)</b>
then press <b>Configure</b>.</p>
") +

    /* Network cards summary dialog help 3/3 */
_("<p><b><big>Editing or Deleting:</big></b><br>
If you press <b>Change</b>, an additional dialog in which
to change the configuration opens.</p>");

    list summary = Lan::Summary("split");
    list unconfigured = summary[1]:[];
    string configured = summary[0]:"";

    y2debug("configured=%1", configured);
    y2debug("unconfigured=%1", unconfigured);

    /* Frame label */
    term contents = Wizard_hw::DetectedContent(_("Network Cards to Configure"),
	    unconfigured, false, configured);

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

    any ret = nil;
    while(true) {

	ret = UI::UserInput();

	/* abort? */
	if(ret == `abort || ret == `cancel || ret == `back) {
	    if(ReallyAbort()) break;
	    else continue;
	}
	/* overview dialog */
	else if(ret == `edit_button) {
	    ret = `overview;
	    break;
	}
	/* configure the selected card */
	else if(ret == `configure_button || ret == `detected_selbox) {
	    Lan::Add();
	    any selected = UI::QueryWidget(`id(`detected_selbox), `CurrentItem);
	    if(selected == `other) {
		ret = `other;
	    }
	    else if(is(selected, integer)) {
		Lan::SelectHW((integer) selected);
		// warn if selecting a card without a driver, #29296
		// TODO also for DSL...
		if (NetworkModules::Alias == "")
		{
		    if (!Popup::ContinueCancel (_("The device does not have a driver module.
The driver may be built in the kernel.")))
		    {
			continue; // means cancel
		    }
		}
		// this is one of 3 places to install packages :-(
		// - kernel modules (InstallKernel): before loaded
		// - smpppd & kinternet: before net start
		// - wlan firmware: here, just because it is copied from modems
		//   #45960
		if(Lan::Requires != [] && Lan::Requires != nil) {
		    if(PackagesInstall(Lan::Requires) != `next)
			continue;
		}
		ret = `configure;
	    }
	    else {
		y2error("Unexpected selection code: %1", selected);
		continue;
	    }
	    break;
	}
	else if(ret == `next) {
	    break;
	}
	else {
	    y2error("Unexpected retcode: %1", ret);
	    continue;
	}
    }

    return ret;
}

/**
 * Overview dialog
 * @return dialog result
 */
define any OverviewDialog() ``{

    ScreenName("lan-overview");

    /* Network cards overview dialog caption */
    string caption = _("Network Card Configuration Overview");

    /* Network cards overview dialog help 1/3 */
    string help = _("<p><b><big>Network Card Overview</big></b><br>
Obtain an overview of installed network cards. Additionally,
edit their configuration.<br></p>
") +

    /* Network cards overview dialog help 2/3 */
_("<p><b><big>Adding a Network Card:</big></b><br>
Press <b>Add</b> to configure a new network card manually.</p>
") +

    /* Network cards overview dialog help 3/3 */
_("<p><b><big>Editing or Deleting:</big></b><br>
Choose a network card to change or remove.
Then press <b>Edit</b> or <b>Delete</b> as desired.</p>
");

    list overview = Lan::Overview();
    y2debug("overview=%1",overview);

    term contents = OverviewTable(
	/* Table header */
	`header(/*_("No."), */ _("Name"), _("Device"), /* _("Type"), */ _("IP Address")),
	overview
    );

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

    if(size(overview) < 1) {
	UI::ChangeWidget(`id(`edit), `Enabled, false);
	UI::ChangeWidget(`id(`delete), `Enabled, false);
    }
    else
	UI::SetFocus(`id(`table));

    any ret = nil;
    while(true) {

	ret = UI::UserInput();

	/* abort? */
	if(ret == `abort || ret == `cancel) {
	    if(ReallyAbort()) break;
	    else continue;
	}
	/* add */
	else if(ret == `add) {
	    Lan::Add();
	    break;
	}
	/* edit */
	else if(ret == `edit || ret == `table) {
	    string dev = (string) UI::QueryWidget(`id(`table), `CurrentItem);
	    if(dev == nil) {
		y2error("Empty device during Edit");
		continue;
	    }
	    Lan::Edit(dev);
	    ret = `edit;
	    break;
	}
	/* delete */
	else if(ret == `delete) {
	    string dev = (string) UI::QueryWidget(`id(`table), `CurrentItem);

	    /* Popup text */
	    string pop = sformat(_("All additional addresses belonging to the interface %1
will be deleted as well.

Really continue?
"), dev);
	    if(Lan::HasAliases(dev) && Popup::YesNoHeadline(Label::WarningMsg(), pop) != true)
		continue;

	    Lan::Delete(dev);
	    Lan::Commit();
	    overview = Lan::Overview();
	    UI::ChangeWidget(`id(`table), `Items, overview);
	    if(size(overview) < 1) {
		UI::ChangeWidget(`id(`edit), `Enabled, false);
		UI::ChangeWidget(`id(`delete), `Enabled, false);
	    }
	    y2debug("overview=%1",overview);
	    continue;
	}
	else if(ret == `next || ret == `back) {
	    break;
	}
	else {
	    y2error("unexpected retcode: %1", ret);
	    continue;
	}
    }

    return ret;
}

/* EOF */
}
