/**
 * File:	include/security/routines.ycp
 * Module:	Security configuration
 * Summary:	Routines
 * Authors:	Michal Svec <msvec@suse.cz>
 *
 * $Id: routines.ycp 13445 2004-01-23 15:34:20Z msvec $
 *
 * These functions are used for the user interface creation
 * and interaction.
 * <pre>
 * Usage:
 *   include "security/ui.ycp";
 *   map WIDGETS = CallFunction(`your_widgets());
 * </pre>
 */

{

textdomain "security";

import "Popup";
import "Security";

include "security/widgets.ycp";

/**
 * Vertical separator
 * @return vertical separator
 */
define term VSeparator() ``{
    return `VSpacing(`opt(`vstretch), 0.1);
}

/**
 * Horizontal separator
 * @return horizontal separator
 */
define term HSeparator() ``{
    return `HSpacing(`opt(`hstretch), 0.1);
}

/**
 * Return a widget from the WIDGETS map created acording to the ID.
 * @param ID security setting identifier
 * @return created widget
 * @see <a href="widgets.html">widgets.ycp</a>
 */
define term settings2widget(string ID) ``{

    map m = WIDGETS[ID]:$[];
    string label = m["Label"]:"";
    string widget = m["Widget"]:"";
    string value = Security::Settings[ID]:"";

    /* "Widget" == "CheckBox" */
    if(widget == "CheckBox") {
        boolean enabled = false;
        if(value == "yes")
            enabled = true;
        return `VBox(
            `Left(`CheckBox(`id(ID), label, enabled)),
            VSeparator()
        );
    }

    /* "Widget" == "TextEntry" */
    if(widget == "TextEntry") {
        return `VBox(
            `Left(`TextEntry(`id(ID), label, value)),
            VSeparator()
        );
    }

    /* "Widget" == "IntField" */
    if(widget == "IntField") {
        return `VBox(
            `Left(`IntField(`id(ID), label, 0, 2147483647, tointeger(value))),
            VSeparator()
        );
    }

    /* "Widget" == "???" */
    if(widget != "ComboBox") {
        y2error("Unknown widget: %1", widget);
        return VSeparator();
    }

    /* "Widget" == "ComboBox" */
    list li = m["Options"]:[];
    list combo = [];
    integer i = 0;

    while(i < size(li)) {
	// string|list it
	y2debug("li=%1 (%2)", li, i);
	any it = li[i]:nil;
	if(it == nil) it = "";
	y2debug("it=%1", it);
	string id_t = "";
	string id_s = "";
	if(is(it, string)) {
	    id_t = (string) it;
	    id_s = (string) it;
	}
	else {
	    list<string> it_list = (list<string>) it;

	    id_t = it_list[0]:"";
	    id_s = it_list[1]:"";
	}
	if(value == id_t)
	    combo = add(combo,`item(`id(id_t), id_s, true));
	else
	    combo = add(combo,`item(`id(id_t), id_s));
	i = i + 1;
    }

    term combobox = nil;
    if(m["Notify"]:"no" == "yes")
	combobox = `ComboBox(`id(ID), `opt(`notify), label, combo);
    else
	combobox = `ComboBox(`id(ID), label, combo);

    return `VBox(
        `Left(combobox),
        VSeparator()
    );
}

/**
 * Query the widget with `id(ID) for its `Value
 * @param ID security setting identifier
 */
define void widget2settings(string ID) ``{
    any ret = UI::QueryWidget(`id(ID), `Value);
    string new = "";
    if(is(ret, boolean)) {
        if(ret == true)
            new = "yes";
        else
            new = "no";
    }
    else if(is(ret, integer))
	new = sformat("%1", ret);
    else if(is(ret, string))
	new = (string) ret;
    else {
	y2error("Unknown widget type: %1", ret);
	new = nil;
    }

    if(new != nil && Security::Settings[ID]:"" != new) {
	y2milestone("Setting modified (%1): %2 -> %3)", ID, Security::Settings[ID]:"", new);
	Security::Settings[ID] = new;
	Security::modified = true;
    }
}

/**
 * Frame with spacings
 * @param f1 horizontal spacing
 * @param f2 vertical spacing
 * @param S frame label
 * @param T frame contents
 * @return frame with contents
 */
define term XFrame(float f1, float f2, string S, term T) ``{
  return `Frame(S,`HBox(`HSpacing(f1),`VBox(`VSpacing(f2),T,`VSpacing(f2)),`HSpacing(f1)));
}

/**
 * Check if minimum is less than maximum in the widget
 * @param minID ID os the minimum widget
 * @param maxID ID os the maximum widget
 * @return true or false
 */
define boolean checkMinMax(string minID, string maxID) ``{
    any min = UI::QueryWidget(`id(minID), `Value);
    any max = UI::QueryWidget(`id(maxID), `Value);
    if(is(min, integer) || is(min, float))
	if(is(max, integer) || is(max, float))
	    if(min <= max)
		return true;
    return false;
}

/**
 * If modified, ask for confirmation
 * @return true if abort is confirmed
 */
define boolean ReallyAbort() ``{
    return !Security::Modified() || Popup::ReallyAbort(true);
}

/* EOF */
}
