/**
 * File:
 *      include/bootloader/ppc/widgets.ycp
 *
 * Module:
 *      Bootloader installation and configuration
 *
 * Summary:
 *      Widgets specific for GRUB bootloader
 *
 * Authors:
 *      Jiri Srain <jsrain@suse.cz>
 *
 * $Id: widgets.ycp 19973 2004-10-27 07:50:12Z jsrain $
 *
 */

{

    textdomain "bootloader";

    import "BootCommon";
    import "Label";
    import "Mode";
    import "TablePopup";

    include "bootloader/grub/helps.ycp";
    include "bootloader/routines/popups.ycp";
    include "bootloader/routines/routines.ycp";



    /**
      * Create a list of options for the table
      * @param descr map description of the table
      * @return list of option ids as it should be displayed
      */
    global define list getTableContents (map descr) ``{
	if (BootCommon::getLoaderType (false) == "none")
	    return [$["key":"__loader_type"]];
	list prependlist = [];
	list forbidden_keys = [];
	any type = BootCommon::getSectionType (BootCommon::current_section);
	if (type == `global)
	{
	    prependlist = ["__loader_type"];
	    if (Mode::config ())
	    {
		prependlist = add (prependlist, "__board_type");
	    }
	    prependlist = add (prependlist, "__sep2");
	    prependlist = merge (prependlist, table_items);
	    prependlist = merge (prependlist, [
		"default", "__sections", "__save_method", "__sep1"]
	    );
	    forbidden_keys = ["boot", "default", "activate"];
	}
	else
	{
	    prependlist = ["label", "__section_type", "__sep1"];
	}
	return BootCommon::createSectionItemsList (
	    prependlist, forbidden_keys, true);
    }

    /**
      * Get map of widget
      * @return map of widget
      */
    global define map<string,any> getGlobalSectionWidget () ``{
	map<string,any> ret = BootCommon::getGlobalSectionWidget ();
	ret["ids"] = BootPPC::getTableContents;
	return ret;
    }

    /**
      * Init function of a popup
      * @param opt_id any option id
      * @param opt_key any option key
      */
    global define void STMFInit (any opt_id, string opt_key) ``{
	UI::ChangeWidget (`id (opt_key), `Value, iseries_streamfile);
    }

    /**
      * Store function of a popup
      * @param opt_id any option id
      * @param opt_key any option key
      */
    global define void STMFStore (any opt_id, string opt_key) ``{
	iseries_streamfile = (string)UI::QueryWidget (`id (opt_key), `Value);
    }

    /**
      * Summary function of a table entry
      * @param opt_id any option id
      * @param opt_key any option key
      * @return string summary to the table
      */
    global define string STMFSummary (any opt_id, string opt_key) ``{
	return iseries_streamfile;
    }

    /**
      * Get map of widget
      * @return map of widget
      */
    global define map<string,any> getSTMFWidget () ``{
	return $[
	    "table" : $[
		"summary" : BootPPC::STMFSummary,
		// label / table entry
		"label" : _("Stream File for *STMF Booting"),
		"unique_key" : true,
	    ],
	    "popup" : $[
		"widget" : `textentry,
		"init" : BootPPC::STMFInit,
		"store" : BootPPC::STMFStore,
	    ],
	];
    }

    /**
      * Init function of a popup
      * @param opt_id any option id
      * @param opt_key any option key
      */
    global define void setDefaultOFInit (any opt_id, string opt_key) ``{
	UI::ChangeWidget (`id (opt_key), `Value, of_defaultdevice);
    }

    /**
      * Store function of a popup
      * @param opt_id any option id
      * @param opt_key any option key
      */
    global define void setDefaultOFStore (any opt_id, string opt_key) ``{
	of_defaultdevice = (boolean)UI::QueryWidget (`id (opt_key), `Value);
	BootCommon::markGeneralOptionChanged ((string)opt_key);
	BootCommon::location_changed = true;
    }

    /**
      * Summary function of a table entry
      * @param opt_id any option id
      * @param opt_key any option key
      * @return string summary to the table
      */
    global define string setDefaultOFSummary (any opt_id, string opt_key) ``{
	return of_defaultdevice
	    ? _("Yes")
	    : _("No");
    }

    /**
      * Get map of widget
      * @return map of widget
      */
    global define map<string,any> getSetDeafultOFWidget () ``{
	return $[
	    "table" : $[
		"summary" : BootPPC::setDefaultOFSummary,
		// label / table entry
		"label" : _("Set Default OpenFirmware Boot Device"),
		"unique_key" : true,
	    ],
	    "popup" : $[
		"widget" : `checkbox,
		"init" : BootPPC::setDefaultOFInit,
		"store" : BootPPC::setDefaultOFStore,
	    ],
	];
    }

    /**
      * Get widget's term
      * @return term describing widget
      */
    global define term getPRePPartsCombo () ``{
	return `ComboBox (`id ("__prep_boot_part"), `opt (`editable),
	    // combo box
	    _("41 &PReP Boot Partition"), prep_boot_partitions);
    }

    /**
      * Init function of a popup
      * @param opt_id any option id
      * @param opt_key any option key
      */
    global define void PRePPartsInit (any opt_id, string opt_key) ``{
	UI::ChangeWidget (`id (opt_key), `Value, prep_boot_partition);
    }

    /**
      * Store function of a popup
      * @param opt_id any option id
      * @param opt_key any option key
      */
    global define void PRePPartsStore (any opt_id, string opt_key) ``{
	prep_boot_partition = (string)UI::QueryWidget (`id (opt_key), `Value);
	BootCommon::markGeneralOptionChanged ((string)opt_id);
    }

    /**
      * Summary function of a table entry
      * @param opt_id any option id
      * @param opt_key any option key
      * @return string summary to the table
      */
    global define string PRePPartsSummary (any opt_id, string opt_key) ``{
	if (prep_boot_partition == "" || prep_boot_partition == "/dev/null")
	{
	    // table item
	    return _("None");
	}
	return prep_boot_partition;
    }

    /**
      * Get map of widget
      * @return map of widget
      */
    global define map<string,any> getPRePPartsWidget () ``{
	return $[
	    "table" : $[
		"summary" : BootPPC::PRePPartsSummary,
		// label / table entry
		"label" : _("41 PReP Boot Partition"),
		"unique_key" : true,
	    ],
	    "popup" : $[
		"widget" : `func,
		"widget_func" : BootPPC::getPRePPartsCombo,
		"init" : BootPPC::PRePPartsInit,
		"store" : BootPPC::PRePPartsStore,
	    ],
	];
    }

    /**
      * Get widget's term
      * @return term describing widget
      */
    global define term iSeriesTargetWidgetLayout () ``{
	return `VBox (
	    `Left (`CheckBox (`id (`iseries_streamfile), `opt (`notify),
		// checkbox
		_("Generate a Boot File for *&STMF Booting")
	    )),
	    `Left (`HBox (`HSpacing (2),
		`TextEntry (`id (`iseries_streamfile_filename),
		    // text entry
		    _("Stream File for *&STMF Booting")))),
	    `Left (`CheckBox (`id (`iseries_prepboot), `opt (`notify),
		// checkbox
		_("Write the Kernel to 41 &PReP Boot Partition (for *NWSSTG)")
	    )),
	    `Left (`HBox (`HSpacing (2),
		`ComboBox (`id (`iseries_prepboot_part),
		    `opt (`editable),
		    // combo box
		   _("41 P&ReP Boot Partition"), prep_boot_partitions))),
	    `Left (`CheckBox (`id (`iseries_slot_a),
		// checkbox
		_("Write Rescue Kernel to Slot &A")
	    )),
	    `Left (`CheckBox (`id (`iseries_slot_b),
		// radiobutton
		_("Write the Kernel to Slot &B")
	    ))
	);
    }

    /**
      * Init function of a popup
      * @param opt_id any option id
      * @param opt_key any option key
      */
    global define void iSeriesTargetInit (any opt_id, string opt_key) ``{
	UI::ChangeWidget (`id (`iseries_streamfile), `Value,
	    iseries_write_streamfile);
	UI::ChangeWidget (`id (`iseries_streamfile_filename), `Value,
	    iseries_streamfile);
	UI::ChangeWidget (`id (`iseries_streamfile_filename), `Enabled,
	    iseries_write_streamfile);
	UI::ChangeWidget (`id (`iseries_prepboot), `Value,
	    iseries_write_prepboot);
	UI::ChangeWidget (`id (`iseries_prepboot_part), `Value,
	    prep_boot_partition);
	UI::ChangeWidget (`id (`iseries_prepboot_part), `Enabled,
	    iseries_write_prepboot);
	UI::ChangeWidget (`id (`iseries_slot_a), `Value,
	    iseries_write_slot_a);
	UI::ChangeWidget (`id (`iseries_slot_b), `Value,
	    iseries_write_slot_b);
    }

    /**
      * Handle function of widget
      * @param opt_id any option id
      * @param opt_key any option key
      * @param event map event that occured
      */
    global define void iSeriesTargetHandle (any opt_id, string opt_key, map event)
    ``{
        UI::ChangeWidget (`id (`iseries_streamfile_filename), `Enabled,
	    UI::QueryWidget (`id (`iseries_streamfile), `Value));
        UI::ChangeWidget (`id (`iseries_prepboot_part), `Enabled,
	    UI::QueryWidget (`id (`iseries_prepboot), `Value));

    }

    /**
      * Store function of a popup
      * @param opt_id any option id
      * @param opt_key any option key
      */
    global define void iSeriesTargetStore (any opt_id, string opt_key) ``{
	iseries_write_streamfile = (boolean)
	    UI::QueryWidget (`id (`iseries_streamfile), `Value);
	iseries_streamfile = (string)
	    UI::QueryWidget (`id (`iseries_streamfile_filename), `Value);
	iseries_write_prepboot = (boolean)
	    UI::QueryWidget (`id (`iseries_prepboot), `Value);
	prep_boot_partition = (string)
	    UI::QueryWidget ( `id (`iseries_prepboot_part), `Value);
	iseries_write_slot_a = (boolean)
	    UI::QueryWidget (`id (`iseries_slot_a), `Value);
	iseries_write_slot_b = (boolean)
	    UI::QueryWidget (`id (`iseries_slot_b), `Value);

	iSeriesAssignDevice ();
	BootCommon::markGeneralOptionChanged ((string)opt_id);
	BootCommon::location_changed = true;
    }

    /**
      * Summary function of a table entry
      * @param opt_id any option id
      * @param opt_key any option key
      * @return string summary to the table
      */
    global define string iSeriesTargetSummary (any opt_id, string opt_key) ``{
	list targets = [];
	if (iseries_write_slot_b)
	{
	    // table cell
	    targets = add (targets, _("Slot B"));
	}
	if (iseries_write_slot_a)
	{
	    // table cell
	    targets = add (targets, _("Slot A"));
	}
	if (iseries_write_prepboot)
	{
	    // table cell, %1 is device name (eg. /dev/hda1)
	    targets = add (targets, sformat (_("PReP (%1)"),
		prep_boot_partition));
	}
	if (iseries_write_streamfile)
	{
	    // table cell, %1 is file name
	    targets = add (targets, sformat (_("*STMF (%1)"),
		iseries_streamfile));
	}
	if (size (targets) == 0)
	{
	    // part of summatu
	    return _("Do Not Install Boot Loader");
	}
	else
	{
	    return mergestring ((list<string>)targets, ", ");
	}
    }

    /**
      * Get map of widget
      * @return map of widget
      */
    global define map<string,any> iSeriesTargetWidget () ``{
	return $[
	    "table" : $[
		// table cell / label
		"label" : _("Boot Loader Location"),
		"optional" : false,
		"ordering" : false,
		"unique_key" : true,
		"summary" : BootPPC::iSeriesTargetSummary,
	    ],
	    "popup" : $[
		"init" : BootPPC::iSeriesTargetInit,
		"handle" : BootPPC::iSeriesTargetHandle,
		"store" : BootPPC::iSeriesTargetStore,
		"widget" : `func,
		"widget_func" : BootPPC::iSeriesTargetWidgetLayout,
	    ],
	];
    }

    /**
      * Get widget's term
      * @return term describing widget
      */
    global define term PRePTargetWidgetLayout () ``{
	return `VBox (
	    `Left (`CheckBox (`id (`install_prepboot), `opt (`notify),
		// checkbox
		_("Write the Kernel to 41 &PReP Boot Partition")
	    )),
	    `Left (`HBox (`HSpacing (2),
		`ComboBox (`id (`prepboot_part),
		    `opt (`editable),
		    // combo box
		   _("41 &PReP Boot Partition"), prep_boot_partitions)))
	);
    }

    /**
      * Init function of a popup
      * @param opt_id any option id
      * @param opt_key any option key
      */
    global define void PRePTargetInit (any opt_id, string opt_key) ``{
	UI::ChangeWidget (`id (`install_prepboot), `Value,
	    BootCommon::activate);
	UI::ChangeWidget (`id (`prepboot_part), `Value,
	    prep_boot_partition);
	UI::ChangeWidget (`id (`prepboot_part), `Enabled,
	    BootCommon::activate);
    }

    /**
      * Handle function of widget
      * @param opt_id any option id
      * @param opt_key any option key
      * @param event map event that occured
      */
    global define void PRePTargetHandle (any opt_id, string opt_key, map event)
    ``{
        UI::ChangeWidget (`id (`prepboot_part), `Enabled,
	    UI::QueryWidget (`id (`install_prepboot), `Value));

    }

    /**
      * Store function of a popup
      * @param opt_id any option id
      * @param opt_key any option key
      */
    global define void PRePTargetStore (any opt_id, string opt_key) ``{
	BootCommon::activate = (boolean)
	    UI::QueryWidget (`id (`install_prepboot), `Value);
	prep_boot_partition = (string)
	    UI::QueryWidget (`id (`prepboot_part), `Value);

	BootCommon::markGeneralOptionChanged ((string)opt_id);
	BootCommon::location_changed = true;
    }

    /**
      * Summary function of a table entry
      * @param opt_id any option id
      * @param opt_key any option key
      * @return string summary to the table
      */
    global define string PRePTargetSummary (any opt_id, string opt_key) ``{
	list targets = [];
	if (BootCommon::activate)
	{
	    // table cell, %1 is device name (eg. /dev/hda1)
	    return sformat (_("PReP Boot Partition %1"), prep_boot_partition);
	}
	else
	{
	    // table cell
	    return _("Do Not Install Boot Loader");
	}
    }

    /**
      * Get map of widget
      * @return map of widget
      */
    global define map<string,any> PRePTargetWidget () ``{
	return $[
	    "table" : $[
		// table cell / label
		"label" : _("Boot Loader Location"),
		"optional" : false,
		"ordering" : false,
		"unique_key" : true,
		"summary" : BootPPC::PRePTargetSummary,
	    ],
	    "popup" : $[
		"init" : BootPPC::PRePTargetInit,
		"handle" : BootPPC::PRePTargetHandle,
		"store" : BootPPC::PRePTargetStore,
		"widget" : `func,
		"widget_func" : BootPPC::PRePTargetWidgetLayout,
	    ],
	];
    }

    /**
      * Init function of a popup
      * @param opt_id any option id
      * @param opt_key any option key
      */
    global define void BoardTypeInit (any opt_id, string opt_key) ``{
	UI::ChangeWidget (`id (opt_key), `Value, getBoardType ());
    }
    /**
      * Store function of a popup
      * @param opt_id any option id
      * @param opt_key any option key
      */
    global define void BoardTypeStore (any opt_id, string opt_key) ``{
	BootCommon::redraw_table = true;
	string bt = (string)UI::QueryWidget (`id (opt_key), `Value);
	y2milestone ("Setting board type to %1", bt);
	setBoardType (bt);
    }

    /**
      * Summary function of a table entry
      * @param opt_id any option id
      * @param opt_key any option key
      * @return string summary to the table
      */
    global define string BoardTypeSummary (any opt_id, string opt_key) ``{
	return board_type_names[getBoardType ()]:getBoardType ();
    }

    /**
      * Get map of widget
      * @return map of widget
      */
    global define map<string,any> getBoardTypeWidget () {
	return $[
	    "table" : $[
		// table cell / label
		"label" : _("PPC Board Type"),
		"optional" : false,
		"ordering" : false,
		"unique_key" : true,
		"summary" : BootPPC::BoardTypeSummary,
	    ],
	    "popup" : $[
		"init" : BootPPC::BoardTypeInit,
		"store" : BootPPC::BoardTypeStore,
		"widget" : `combobox,
		"items" : maplist (string k, string v, board_type_names, ``(
		    [ k, v ] )),
	    ],
	];
    }

    /**
      * Get map of bootloader options
      * @return map of options
      */
    global define map<string,map<string,any> > GetOptionMaps () ``{
	return $[
	    "__iseries_location" : iSeriesTargetWidget (),
	    "__prep_location" : PRePTargetWidget (),
	    "__chrp_location" : PRePTargetWidget (), // at the moment it is
					// the same as PReP
	    "__stmf_image" : getSTMFWidget (),
	    "__set_default_of" : getSetDeafultOFWidget (),
	    "__prep_boot_part" : getPRePPartsWidget (),
	    "__board_type" : getBoardTypeWidget (),
	];
    }

    /**
      * Get map of dialogs and widgets
      * @return map of dialogs and widgets
      */
    global define map<string,map<string,any> > GetWidgetMaps () ``{
        return $[
	    "globalsection" : getGlobalSectionWidget (),
        ];
    }

}
