/**
 * File:
 *   select.ycp
 *
 * Module:
 *   Sound
 *
 * Summary:
 *   sound card selection dialog
 *
 * Authors:
 * Dan Meszaros <dmeszar@suse.cz>
 *
 *
 * $Id: select.ycp 15841 2004-03-09 18:47:30Z sh $
 *
 */

{

textdomain "sound";

import "Sound";
import "Summary";
import "Label";
import "Wizard";
import "Wizard_hw";

include "sound/ui.ycp"; // ReallyAbort

/**
 * creates hardware-summary-like list of configured cards
 * @return string richtext
 * @param save_info save info
 */
define string ConfCardsRT(list save_info) ``{
    list<string> retlist = [];
    list<string> conflist = [];
    foreach(map card, (list<map>) save_info, ``{
        conflist = add (conflist, card["model"]:"");
    });
    integer i = 0;
    foreach(string cardname, conflist, ``{
        retlist = add (retlist, Summary::Device (cardname,
	// to translators: '(sound card is) Configured as snd-card-1'
	sformat(_("Configured as snd-card-%1."), i)));
        i = i + 1;
    });
    return Summary::DevicesList(retlist);
}

/**
 * ui that displays two listbox with new and configured harwdare
 *
 * @param newHw list with new hardware
 * @param confHw list with already configured hardware
 * @return void
 */
define void selectHardware (list newHw, string confHw) ``{
    integer index = -1;
    newHw = maplist (string cardname, (list<string>) newHw, ``{
        index = index + 1;              // select first item
        return `item(`id(index), cardname, index == 0);
    });

    // the full meaning of message is: "Add other not detected soundcard"
    newHw = add (newHw, `item(`id(-1), _("Other (not detected)"),
        size(newHw) == 0));

    // label above list
    term widget = Wizard_hw::DetectedContent(_("&Sound cards to configure"),
        newHw, false,
        // label above list
        sformat(_("<P><B>Already configured sound cards:</B>%1</P>"),confHw)
    );

    string helptext = Sound::STRINGS["selectHardware"]:"";

    // dialog header
    Wizard::SetContents (_("Sound card configuration"), widget, helptext,
        true, true);
}

/**
 * dialog with list of unconfigured hardware
 * @param sound_cards unconfigured cards
 * @param save_info configured cards
 * @return map result
 */
define map sound_select (list sound_cards, list save_info) ``{

    // show the dialog
    string confhw = ConfCardsRT(save_info);

    selectHardware (sound_cards, confhw);

    Wizard::SetNextButton(`next, Label::FinishButton() );

    symbol ui = nil;
    do
    {
	ui = (symbol) UI::UserInput();
	if ((ui == `abort || ui == `cancel) && ReallyAbort())
        {
	    return $["ui": `abort];
	}
    } while (ui == nil ||
	!contains([`back, `next, `edit_button, `configure_button], ui));

    symbol type = nil;
    integer index = 0;

    if (ui == `configure_button)
    {
	type = `new;
	index = (integer)UI::QueryWidget(`id(`detected_selbox), `CurrentItem);
	ui = `configure;
    }

    if (ui == `configure && index < 0)
    {
	type = `manual;
	index = -1;
    }

    if (ui == `edit_button)
    {
	ui = `detail;
    }

    return $["ui" : ui, "type" : type, "id" : index];
}

}//EOF
