/**
 * File:
 *   include/network/isdn/routines.ycp
 *
 * Package:
 *   Configuration of network
 *
 * Summary:
 *   helper functions for ISDN configuration
 *
 * Authors:
 *   Karsten Keil <kkeil@suse.de>
 *
 * $Id: routines.ycp 21341 2005-02-07 16:15:59Z mvidner $
 *
 *
 *
 */

{

  textdomain "network";

  import "Label";
  import "Popup";

/**
 * Return true if the given driver match i4ltyp and i4l subtype
 * @param drv       - the driver info map
 * @param i4ltyp    - the I4L TYPE
 * @param i4lsubtyp - the I4L SUBTYPE
 * @return boolean  - true if match false if not
 */
define boolean driver_has_type(map drv, integer i4ltyp, integer i4lsubtyp) ``{
    boolean ret = false;

    if (i4ltyp == drv["type"]:-2) {
	if (i4lsubtyp == drv["subtype"]:-2)
	    ret = true;
    }
    return(ret);
}

/**
 * Return true if the given card match i4ltyp and i4l subtype
 * @param card      - the card info map
 * @param i4ltyp    - the I4L TYPE
 * @param i4lsubtyp - the I4L SUBTYPE
 * @return boolean  - true if match false if not
 */
define boolean card_has_type(map card, integer i4ltyp, integer i4lsubtyp) ``{
    boolean ret = false;

    maplist(map d, card["driver"]:[],
    ``{
	if (driver_has_type(d, i4ltyp, i4lsubtyp))
	   ret = true;
    });
    return(ret);
}

/**
 * Return the matching driver for i4ltyp and i4l subtype
 * @param cdb       - cdb ISDN db
 * @param i4ltyp    - the I4L TYPE
 * @param i4lsubtyp - the I4L SUBTYPE
 * @return map of matching driver info
 */
define map get_isdndriver_by_type(map cdb, integer i4ltyp, integer i4lsubtyp) ``{
    map ret =  $[];

    maplist(any i, map c, cdb["Cards"]:$[],
    ``{
	maplist(map d, c["driver"]:[],
	``{
	   if (driver_has_type(d, i4ltyp, i4lsubtyp))
		ret = d;
	});
    });
    return(ret);
}

/**
 * Return the matching card for i4ltyp and i4l subtype
 * @param cdb       - cdb ISDN db
 * @param i4ltyp    - the I4L TYPE
 * @param i4lsubtyp - the I4L SUBTYPE
 * @return map of matching card info
 */
define map get_isdncard_by_type(map cdb, integer i4ltyp, integer i4lsubtyp) ``{
    map ret =  $[];

    maplist(any i, map c, cdb["Cards"]:$[],
    ``{
	if (card_has_type(c, i4ltyp, i4lsubtyp))
	    ret = c;
    });
    return(ret);
}

/**
 * Return the I4L SUBTYPE from card info
 * @param card      - map of card info
 * @return I4L SUBTYPE
 */
define integer get_i4lsubtype(map card) ``{
    integer ret = -1;
    list d = card["driver"]:[];

    if (d == [])
	d = card["drivers"]:[];
    ret = d[card["sel_drv"]:0, "subtype"]:-1;
    return(ret);
}

/**
 * Return the I4L TYPE from card info
 * @param card      - map of card info
 * @return I4L TYPE
 */
define integer get_i4ltype(map card) ``{
    integer ret = -1;
    list d = card["driver"]:[];

    if (d == [])
	d = card["drivers"]:[];
    ret = d[card["sel_drv"]:0, "type"]:-1;
    return(ret);
}

/**
 * Creates a popup with test OK/ not OK and displays details on request
 * @param result   - return code of the test 0 is OK
 * @param details  - string of collected infos during the test 
 * @return allways true
 */
define boolean display_testresult(integer result, string details) ``{
    any ret = nil;
    string msg = "";

    if (result == 0) {
	// ISDN HW test result (positiv)
	msg = sformat(_("The test was successful."));
    } else {
	// ISDN HW test result (negativ)
	msg = sformat(_("The test was not successful.
 ReturnValue: %1
"), result);
    }
    UI::OpenDialog(
	`opt(`decorated),
	`HBox(
	    `HSpacing(1),
	    `VBox(
		`VSpacing(1),
		`Label(msg),
		`VSpacing(1),
		`HBox(
		    `PushButton(`id(`ok), `opt(`default), Label::OKButton()),
		    // Button label for details about the HW test
		    `PushButton(`id(`detail), _("&Details"))
		),
		`VSpacing(1)
	    ),
	    `HSpacing(1)
	)
    );
    UI::SetFocus(`id(`ok));
    ret = UI::UserInput();
    if (ret == `detail)
	Popup::Message (details);
    UI::CloseDialog();
    return true;
}

/**
 * Creates a popup with a selection list
 * @param title   - return code of the test 0 is OK
 * @param lst  - list of items
 * @return name of the selected item
 */
define string select_fromlist_popup(string title, list lst) ``{
    any ret = nil;

    UI::OpenDialog(
	`opt(`decorated),
	`HBox(
	    `HSpacing(1),
	    `VBox(
		`VSpacing(1),
		`ComboBox(`id(`sel), `opt(`hstretch,`notify), title, lst),
		`VSpacing(1),
		`PushButton(`id(`ok), `opt(`default), Label::OKButton()),
		`VSpacing(1)
	    ),
	    `HSpacing(1)
	)
    );
    UI::SetFocus(`id(`ok));
    while(true) {
	ret = UI::UserInput();
	if (ret == `ok)
	    break;
    }
    string sel = (string) UI::QueryWidget(`id(`sel), `Value);
    UI::CloseDialog();
    return sel;
}

}
