/**
 * Module: 		PackageSelectionIO.ycp
 *
 * Authors:		Stefan Schubert (schubi@suse.de)
 *
 * Purpose: 		Saving and loading user package selections
 *
 * $Id: PackageSelectionIO.ycp 21866 2005-02-23 13:50:15Z jsrain $
 */

{

    module "PackageSelectionIO";

    textdomain "packager";

    import "Installation";
    import "Label";
    import "Stage";
    import "Popup";
    import "Report";
    import "StorageDevices";

    string lastHarddiskPath = "/var/lib/YaST2/packages.usr";
    string lastFloppyPath = "packages.usr";
    boolean floppySelected = false;
    boolean savedLaterSelected = false;

    //
    // Saving package selections
    //
    global define boolean savePackageSelection( boolean	saveLater ) ``{
	boolean success	= true;
	if ( floppySelected && saveLater)
	{
	    string floppy_path = "/media/floppy/";
	    WFM::Execute (.local.umount, floppy_path); // Just for safety

	    // Yes/No MsgBox; default: Yes
	    // Ask for a formatted floppy to save the package selection
	    while (Popup::YesNo(_("The current package selection will be saved
on a floppy disk.  Insert a formatted floppy.

Write data to the floppy disk now?
")))
	    {

		// mount floppy_path by WFM, so use Execute() and Write()

		if ( WFM::Execute (.local.mount, [StorageDevices::FloppyDevice, floppy_path]) == true)
		{
		    success = SCR::Write (.package.packageSelections, floppy_path+lastFloppyPath );

		    WFM::Execute (.local.umount, floppy_path);

		    if (success)
		    {
			// msgbox
			// data saved to floppy disk
			Report::Message(_("Your settings have been saved to the floppy disk."));
			break;
		    }
		    else
		    {
			// text of a error popup: error occured while saving settings to the floppy
			Report::Error(_("Error while saving settings to the floppy disk.
Verify that the floppy is formatted
and is not write protected.
"));
		    }
		}
		else
		{
		    // Popup with a 'Retry', a 'Cancel' button and a 'Format floppy' button.
		    // Check inserted floppy.
		    // headline
		    any ret = Popup::AnyQuestion3(_("Could not find a formatted floppy disk."),
// popup contents
						    _("Are you sure you inserted a floppy disk
into the correct drive?
If so, is the disk formatted and verified?
"), Label::RetryButton(), Label::CancelButton(),
// push button
_("&Format floppy"), `focus_yes);

		    if (ret == `no)		// cancel
		    {
			break;
		    }
		    else if (ret == `retry)	// format floppy
		    {
			UI::OpenDialog( `opt( `decorated ),
					`HBox(`HSpacing(1), `Label(Label::PleaseWaitMsg()), `HSpacing(1)));
			SCR::Execute (.target.bash, "/sbin/mkdosfs " + StorageDevices::FloppyDevice);
			UI::CloseDialog();
		    }
		    // default: retry loop
		}
	    }
	}
	else if ( !floppySelected )
	{
	    // Harddisk
	    if ( !Stage::initial () ||
		 ( !saveLater
		   && savedLaterSelected ))
	    {
		// harddisk is present
		success = SCR::Write (.package.packageSelections,
				      Installation::destdir + lastHarddiskPath );
		if ( success )
		{
		    // text of a message popup: package selection is written correctly
		    Report::Message(_("Your package selections have been written to the hard disk."));
		}
		else
		{
		    // text of en error popup: error while writing data
		    Report::Message(_("Error while saving package selections to the hard disk."));
		}
	    }
	    else
	    {
		savedLaterSelected = true;
	    }
	}
	return success;
    };

    //
    // Loading package selections
    //
    global define boolean loadPackageSelection( ) ``{
	boolean success = true;
	// text of a popup with 'Yes' and 'No' button
	if ( Popup::YesNo(_("Do you really want to reset your settings?")))
	{
	    if ( floppySelected )
	    {
		string floppy_path = "/media/floppy/";
		WFM::Execute (.local.umount, floppy_path); // Just for safety

		// Yes/No MsgBox; default: Yes
		// Ask for the floppy with the package selection.
		while (Popup::YesNo(_("The current package selection will be loaded
from a floppy disk.  Insert the floppy.

Read data from the floppy disk now?
")))
		{
		    // mount floppy_path by WFM, so use Execute() and Read()

		    if ( WFM::Execute (.local.mount, [StorageDevices::FloppyDevice, floppy_path]) == true)
		    {
			success = (boolean) SCR::Read (.package.packageSelections, floppy_path+lastFloppyPath );

			WFM::Execute (.local.umount, floppy_path);

			if (success)
			{
			    break;
			}
			else
			{
			    // text of an error popup: can't read settings from floppy
			    Report::Error(_("Error while loading settings from the floppy disk.
Verify that the correct floppy has been inserted
and that the path name has been written correctly.
"));
			}
		    }
		}
	    }
	    else
	    {
		// Harddisk
		success = (boolean) SCR::Read (.package.packageSelections,
				     Installation::destdir + lastHarddiskPath );
		if ( !success )
		{
		    // text of an error poup: can't read settings from hard disk
		    Report::Error(_("Error while loading settings from the hard disk.
Verify that the path name has been written correctly.
"));
		}
	    }
	}
	else
	{
	    success = false;
	}

	return success;
    };

    //
    // Saving and Loading package user selections
    //
    global define void SaveLoadSelection( ) ``{

	savedLaterSelected = false;

	term dia_opt = `opt ( `decorated );

	UI::OpenDialog( dia_opt,
		    `HBox( `VSpacing(10),
			   `VBox (`HSpacing(50),
				  // Headline of a popup with a 'Cancel', a 'Load' and a 'Save' button.
				  // A user defined selection of packages can be loaded or saved.
				  `Left(`Heading( _("Load or save package selection") )),
				  `VSpacing(0.2),
				  `RadioButtonGroup(`id(`selections),`opt(`notify),
						    `VBox(
							  `HBox(
								`Left(`RadioButton(`id(`harddisk),
										   `opt(`notify),
										   // Radio button for saving/loading from harddisk
										   _("&Harddisk"),
										   !floppySelected)),
								`Left(`RadioButton(`id(`floppy),
										   `opt(`notify),
										   // Radio button for saving/loading from floppy
										   _("&Floppy"),
										   floppySelected))
								),
							  `VSpacing(0.8),
							  `TextEntry(`id(`path),
								     // Input field label for the filename
								     _("&Path name"),
								     ( floppySelected? lastFloppyPath:lastHarddiskPath ))
							  )
						    ),
				  `VSpacing(0.2),
				  `HBox(
					// button labels selection popup
					`Left(`PushButton( `id(`cancel),`opt(`default), Label::CancelButton())),
					// push button
					`Right(`PushButton( `id(`load), _("&Load"))),
					// push button
					`Right(`PushButton( `id(`save),  _("&Save")))
					)
				  )
			   )
		    );

	while ( true )
	{
	    boolean oldDiskSelected = (boolean) UI::QueryWidget(`id(`harddisk), `Value);

	    any ret = UI::UserInput();

	    if ( ret == `cancel )
	    {
		break;
	    }

	    if ( UI::QueryWidget(`id(`harddisk), `Value)  == true)
	    {
		floppySelected = false;
		if ( !oldDiskSelected )
		{
		    // changed from floppy to HD
		    UI::ChangeWidget( `id(`path), `Value, lastHarddiskPath );
		}
		else
		{
		    lastHarddiskPath = (string) UI::QueryWidget(`id(`path), `Value);
		}
	    }
	    else
	    {
		floppySelected = true;
		if ( oldDiskSelected )
		{
		    // changed from HD to floppy
		    UI::ChangeWidget( `id(`path), `Value, lastFloppyPath );
		}
		else
		{
		    lastFloppyPath = (string) UI::QueryWidget(`id(`path), `Value);
		}
	    }

	    if ( ret == `load )
	    {
		if ( loadPackageSelection() )
		{
		    break;
		}
	    }

	    if ( ret == `save )
	    {
		if ( savePackageSelection( true ) )
		{
		    break;
		}
	    }
	}

	UI::CloseDialog();
    };




}
