/**
 * File:	include/tv/misc.ycp
 * Package:	TV cards configuration
 * Summary:	Miscelanous functions for configuration of tv.
 * Authors:	Jan Holesovsky <kendy@suse.cz>
 *
 * $Id: misc.ycp 15126 2004-03-01 18:52:37Z jsuchome $
 *
 * Functions that are not UI related, do not belong to modules/tv.ycp
 * and are not common enough to be in a library.
 */

{

    import "Sound";
    import "Tv";
    import "Label";
    import "Popup";

    textdomain "tv";

    /**
     * Compares parameters of a card with the current_card. It is needed for
     * decision whether to select the card.
     * @param parameters The 'parameters' which change
     * @param parameters_to_compare The reference 'parameters' (the ones I am searching for)
     * @return boolean Should I select the card with parameters 'parameters'?
     */
    define boolean CmpParameters(map parameters, map parameters_to_compare) ``{
	boolean select_this = true;
	if (size(parameters) > 0)
	{
	    foreach(string param, any value, (map<string,any>) parameters, ``{
		if (value != parameters_to_compare[param]:nil)
		{
		    select_this = false;
		}
	    });
	}
	else
	{
	    if (size(parameters) == 0 && size(parameters_to_compare) == 0)
	    {
		select_this = true;
	    }
	    else
	    {
		select_this = false;
	    }
	}
	return select_this;
    }

    /**
     * List of the sound cards acceptable by the Table widget.
     * @return list List of sound cards
     */
    define list SoundCardsAsItems() ``{

	return maplist(map card, (list<map<string,any> >)Sound::GetSoundCardList (), ``{
	    integer sound_card_no = card["card_no"]:-1;
	    return `item(`id(sound_card_no),
			sformat("%1", sound_card_no), card["name"]:"");
	});
    }

/**
 * Enhanced version of UserInput which asks if really abort after
 * pressing [Abort].
 * @return any The value of the resulting UserInput.
 */
define any tvUserInput() ``{
    any ret = nil;

    do
    {
	ret = UI::UserInput();
    } while ((ret == `cancel || ret == `abort) &&
	     !Popup::ReallyAbort (Tv::IsDirty()));
//	     (Tv::IsDirty()? !Popup::ReallyAbort (true): false));

    return (ret == `cancel)? `abort: ret;
}

/**
 * Error popup with the possibility of showing additional information
 * about the problem
 * @param error basic error message
 * @param details more informations, e.g. stderr of some script
 */
define void ErrorWithDetails (string error, string details) ``{

    UI::OpenDialog (`HBox(`HSpacing (0.5),
	`VBox(
	    `VSpacing (0.5),
	    // label
	    `Left(`Heading (Label::ErrorMsg())),
	    `Label (error),
	    `ReplacePoint (`id(`rp), `Empty()),
	    `VSpacing (0.5),
	    `Left(`CheckBox (`id(`details), `opt (`notify),
	        // checkbox label
		_("&Show Details"), false)),
	    `PushButton (`id(`ok), `opt(`key_F10,`default), Label::OKButton())
	),
	`HSpacing(0.5))
    );
    symbol ret = nil;

    do
    {
	ret = (symbol) UI::UserInput();
	if (ret == `details)
	{
	    if ((boolean)UI::QueryWidget (`id(`details), `Value))
		UI::ReplaceWidget (`id(`rp), `VBox (`Label (details)));
	    else
		UI::ReplaceWidget (`id(`rp), `Empty());
	}
    }
    while (ret != `ok && ret != `cancel);
    UI::CloseDialog();
}


/* EOF */
}
