/**
 * File:	include/security/complex.ycp
 * Package:	Security configuration
 * Summary:	Complex dialogs definitions
 * Authors:	Michal Svec <msvec@suse.cz>
 *
 * $Id: complex.ycp 18688 2004-08-18 17:09:11Z jsuchome $
 */

{

textdomain "security";

import "Label";
import "Security";
import "Wizard";

include "security/helps.ycp";
include "security/levels.ycp";
include "security/routines.ycp";

/**
 * Read settings dialog
 * @return `next if success, else `abort
 */
define symbol ReadDialog() ``{

    Wizard::RestoreHelp(HELPS["read"]:"");
//    Security::AbortFunction = ``{return true;};
    Security::AbortFunction = ``{return Security::PollAbort();};
    boolean ret = Security::Read();
    return ret ? `next : `abort;
}

/**
 * Write settings dialog
 * @return `next if success, else `abort
 */
define symbol WriteDialog() ``{

    Wizard::RestoreHelp(HELPS["write"]:"");
    Security::AbortFunction = ``{return Security::PollAbort();};
    boolean ret = Security::Write();
    return ret ? `next : `abort;
}

/**
 * Main dialog
 * @return dialog result
 */
define symbol MainDialog() ``{

    /* Main dialog caption */
    string caption = _("Local Security Configuration");
    string help = HELPS["main"]:"";

    /* Determine current settings */
    any current = `custom;
    maplist(string key, map level, Levels, ``{
	y2debug("%1=%2", key, level);
	if(level == Security::Settings)
	    current = key;
    });
    y2debug("%1=%2", "curren", Security::Settings);

    /* Create RB group from the list of settings */
    term RB = `VBox();
    RB = add(RB, `VSpacing(0.5));
    mapmap(string key, string name, LevelsLabels, ``{
	RB = add(RB, `Left(`RadioButton(`id(key), `opt(`notify), name, key == current)));
	RB = add(RB, `VSpacing(0.03));
	return $[0:0];
    });
    RB = add(RB, `VSpacing(0.6));
    /* RadioButton label */
    RB = add(RB, `Left(`RadioButton(`id(`custom),`opt(`notify),_("&Custom Settings"), current == `custom)));
    RB = add(RB, `VSpacing(0.5));
    y2debug("RB=%1",RB);

    /* Main dialog contents */
    term contents = `HVCenter(
	`VBox(
	    `HVSquash(
		/* Frame caption */
		`Frame( _("Current Security Settings"),
		    `HBox(`HSpacing(0.8),
		    `RadioButtonGroup(`id(`rb), RB),
		    `HSpacing(0.8))
		)
	    ),
	    `VSpacing(0.6),
	    /* PushButton label */
	    `HVCenter(`PushButton(`id(`details), _("&Details..."))),
	    `VSpacing(0.6)
	)
    );

    contents = `HVCenter(`HVSquash(`HBox(`HSpacing(5),`VBox(`VSpacing(2),`ReplacePoint(`id(`rp_main),contents),`VSpacing(2)),`HSpacing(5))));
    Wizard::SetContentsButtons(caption, contents, help,
            Label::BackButton(), current == `custom ? Label::NextButton() : Label::FinishButton());

    if(current == `custom)
	UI::ChangeWidget(`id(`details), `Enabled, false);
    else
	UI::ChangeWidget(`id(`details), `Enabled, true);

    any ret = nil;
    while(true) {
	any cur = UI::QueryWidget(`id(`rb), `CurrentButton);
	ret = UI::UserInput();

	/* abort? */
	if(ret == `abort || ret == `cancel) {
	    if(ReallyAbort()) break;
	    else continue;
	}
	else if(ret == `back) {
	    break;
	}
	else if(ret == `next || ret == `details) {
	    /* check_* */
	    break;
	}
	else if(ret == `custom) {
	    if(cur != `custom) {
		UI::ChangeWidget(`id(`details), `Enabled, false);
		Wizard::RestoreNextButton();
		Wizard::SetFocusToNextButton();
	    }
	    continue;
	}
	else if(is(ret, string)) {
	    if(!haskey(Levels, (string) ret)) {
		y2error("Unexpected return code (key missing): %1", ret);
		continue;
	    }
	    if(cur == `custom) {
		UI::ChangeWidget(`id(`details), `Enabled, true);
		Wizard::SetNextButton(`next, Label::FinishButton() );
		Wizard::SetFocusToNextButton();
	    }
	    continue;
	}
	else {
	    y2error("Unexpected return code: %1", ret);
	    continue;
	}
    }

    if(ret == `next || ret == `details) {
	any cur = UI::QueryWidget(`id(`rb), `CurrentButton);

	y2debug("current=%1", current);
	y2debug("cur=%1", cur);

	if(cur != `custom) {
	    if(current != cur) {
		y2debug("Level modified (%1)", cur);
		Security::Settings = Levels[(string) cur]:$[];
		Security::modified = true;
	    }
	    if(ret == `next) ret = `finish;
	    if(ret == `details) ret = `next;
	}
    }

    return (symbol) ret;
}

/* EOF */
}
