/**
 *
 * File:
 *   slaves.ycp
 *
 * Module:
 *   Network/YPServer
 *
 * Summary:
 *   YPServer module.
 *
 * Authors:
 *   Dan Vesely <dan@suse.cz>
 *   Martin Vidner <mvidner@suse.cz>
 *
 * $Id: slaves.ycp 18066 2004-07-12 11:40:21Z mvidner $
 *
 * YPServer module part.
 *
 */


{

    textdomain "nis_server";

    import "Address";
    import "Label";
    import "NisServer";
    import "Popup";
    import "Wizard";

    include "nis_server/routines.ycp";

    list<string> hosts = nil;

    /**
     * Give me one name from the list of hosts
     * @param hosts list of hosts
     * @return hostname or nil
     */
    define string ChooseHostName(list<string> hosts) ``{
	string hname = nil;

	UI::OpenDialog(
	       `VBox(`HSpacing(40),
		     `HBox(`VSpacing(10),
			   // To translators: selection box label
			   `SelectionBox(`id(`hosts), _("&Remote hosts"), hosts)),
		     `HBox(`PushButton(`id(`ok), `opt (`default, `key_F10),
				       Label::OKButton()),
			   `PushButton(`id(`cancel), `opt (`key_F9),
				       Label::CancelButton()))));
	any ret = nil;
	do {
	    ret = UI::UserInput();
	} while (ret != `cancel && ret != `ok);

	if (ret == `ok)
	    hname = (string) UI::QueryWidget(`id(`hosts), `CurrentItem);

	UI::CloseDialog();

	return hname;
    };


    /**
     * Popup for editing a slaver server hostname
     * @param slave	hostname
     * @return	hostname or nil if canceled
     */
    define string YPSlavePopup (string slave) ``{

	term hbox = `HBox ();
				// To translators: textentry label
	hbox = add (hbox, `TextEntry (`id(`slave), _("&Slave's host name"), slave));
				// To translators: pushbutton label
	hbox = add (hbox, `VBox (`VSpacing (), `PushButton (`id (`browse), `opt (`key_F6), _("&Browse"))));

	term contents = `VBox ();
	contents = add (contents, `VSpacing (0.3));
			       // To translators: popup dialog heading
	contents = add (contents, `Heading (_("Edit slave")));
	contents = add (contents, `VSpacing (0.5));
	contents = add (contents, hbox);
	contents = add (contents, `VSpacing (0.5));

	hbox = `HBox ();
	hbox = add (hbox, `PushButton (`id (`ok), `opt (`default, `key_F10), Label::OKButton()));
	hbox = add (hbox, `PushButton (`id (`cancel), `opt (`key_F9), Label::CancelButton()));

	contents = add (contents, hbox);
	contents = add (contents, `VSpacing (0.3));
	contents = `HBox (`HSpacing (), contents, `HSpacing ());

	UI::OpenDialog (contents);
	UI::SetFocus (`id (`slave));

	symbol ui = nil;

	repeat {
	    ui = (symbol) UI::UserInput ();

	    if (ui == `ok)
	    {
		slave = tolower ((string) UI::QueryWidget (`id (`slave), `Value));

		if (!Address::Check4(slave))
		{
		    UI::SetFocus (`id (`slave));
		    Popup::Error(Address::Valid4());
		    ui = `again;
		}
	    }
	    else if (ui == `browse)
	    {
		if (hosts == nil) {
				// To translators: label message
		    UI::OpenDialog(`Label(_("Scanning for hosts on this LAN...")));
		    hosts = sort((list<string>) SCR::Read(.net.hostnames));
		    UI::CloseDialog();
		    if (hosts == nil) hosts = [];
		}
		string host = ChooseHostName (hosts);
		if (host != nil)
		{
		    slave = host;
		    UI::ChangeWidget(`id(`slave), `Value, host);
		}
	    }

	} until (ui == `ok || ui == `cancel);

	UI::CloseDialog ();

	return ui == `ok ? slave : nil;

    }

    /**
     * Slaves dialog
     * @return `back, `abort or `next
     */
    define symbol MastersSlavesDialog () ``{

	// help text 1/1
	string helptext = _("<p>Here, enter the names of hosts to configure as NIS server slaves. Use <i>Add</i> to add a new one, <i>Edit</i>  to change an existing entry, and <i>Delete</i> to remove an entry.</p>");

	list<string> slaves = NisServer::ypservers;

	term buttons = `HBox ();
				// To translators: pushbutton label
	buttons = add (buttons, `PushButton (`id (`add), `opt (`key_F3), _("A&dd")));
				// To translators: pushbutton label
	buttons = add (buttons, `PushButton (`id (`edit), `opt (`key_F4), _("&Edit")));
				// To translators: pushbutton label
	buttons = add (buttons, `PushButton (`id (`delete), `opt (`key_F5), _("De&lete")));

	term contents = `VBox ();
	contents = add (contents, `ReplacePoint (`id (`replace),
				// To translators: selection box label
						 `SelectionBox (`id (`slaves), `opt (`notify), _("&Slaves"), sort(slaves))));
	contents = add (contents, buttons);

				// To translators: dialog label
	Wizard::SetContents (_("NIS Master Server Slaves Setup"), contents, helptext, true, true);

	symbol ui = nil;

	repeat
	{
	    boolean anyslaves = UI::QueryWidget (`id (`slaves), `CurrentItem) != nil;
	    UI::ChangeWidget (`id (`edit),   `Enabled, anyslaves);
	    UI::ChangeWidget (`id (`delete), `Enabled, anyslaves);

	    ui = (symbol) UI::UserInput ();
	    if (ui == `cancel)
	    {
		ui = `abort;
	    }

	    if (ui == `edit)
	    {
		string selected = (string) UI::QueryWidget (`id (`slaves), `CurrentItem);
		if (selected == nil) continue;
		string edited = YPSlavePopup (selected);
		if (edited != nil)
		{
		    slaves = filter (string e, slaves, ``(e != selected));
		    slaves = add (slaves, edited);
				// To translators: selectionbox label
		    UI::ReplaceWidget (`id (`replace), `SelectionBox (`id (`slaves), `opt (`notify), _("&Slaves"), sort (slaves)));
		}
	    }

	    else if (ui == `delete)
	    {
		string selected = (string) UI::QueryWidget (`id (`slaves), `CurrentItem);
		if (selected == nil) continue;
		slaves = filter (string e, slaves, ``(e != selected));
				// To translators: selectionbox label
		UI::ReplaceWidget (`id (`replace), `SelectionBox (`id (`slaves), `opt (`notify), _("&Slaves"), sort (slaves)));
	    }

	    else if (ui == `add)
	    {
		string edited = YPSlavePopup ("");
		if (edited != nil && !contains (slaves, edited))
		{
		    slaves = add (slaves, edited);
				// To translators: selectionbox label
		    UI::ReplaceWidget (`id (`replace), `SelectionBox (`id (`slaves), `opt (`notify), _("&Slaves"), sort (slaves)));
		}
	    }

	    if (ui == `abort && !Popup::ReallyAbort (true)) ui = `again;
	}
	until (contains ([`back, `next, `abort], ui));

	NisServer::ypservers = slaves;

	return ui;
    }
}
