/**
 *  File:
 *    OSRFloppy.ycp
 *
 *  Module:
 *     Read/Write files from floppy with dialog support.
 *
 *  Summary:
 *      How to use OSRFloppy module:
 *	1. OSRFloppy::Open( with ui support, count of files to read/wirte );
 *
 *      2. optional: OSRFloppy::NextStep(_(Copy the file ..  ));
 *		WFM::Execute(.local.bash, "/bin/cp ... "); or
 *		OSRFloppy::ReadMap( .. ) or
 *		OSRFloppy::SaveMap( .. )
 *
 *	3. OSRFloppy::Close();
 *
 *  Author:
 *    Johannes Buchhold <jbuch@suse.de>
 *
 * $Id: OSRFloppy.ycp 20502 2004-12-06 14:24:06Z jsuchome $
 */
{
  module "OSRFloppy";

  textdomain "repair";

  import "StorageDevices";
  import "Partitions";
  import "Report";
  import "Label";

  import "OSRModuleLoading";

  /**
   * Default mount point
   */
  global string mount_point            = "/media/floppy";

  /**
   * Default floppy device
   */
  global string floppy_device	       = StorageDevices::FloppyDevice;

  /**
   * Supported file systems
   */
  list<string> needed_fs_kernel_modules        = ["fat", "vfat" ];

  /**
   * Only for internal use.
   */
  boolean dialog_open	        = false;
  boolean use_ui		= true;
  integer step			= 0;
  global boolean opened		= false;


  /**
   * Reset the internal values.
   */
  define void Reset(boolean local_use_ui)``{
      use_ui = local_use_ui;
      step  = 0;
      mount_point = "/media/floppy";
  }

  /**
   * Check if a floppy device exists.
   */
  define boolean CheckPresent()``{

      if ( ! StorageDevices::FloppyPresent )
      {
	  // error message
	  string message = _("
No floppy device was detected. Loading a
file from a removable device is not possible.
");

	  if ( use_ui ) {
	      Report::Error(message);
	  }
	  else {
	      y2error(message);
	  }
	  return false;
      }
      return true;
  }

  /**
   * Load needed fs modules.
   */
  define boolean LoadFsModules()``{
      boolean error = false;

      foreach (string module_name, needed_fs_kernel_modules, ``{

	  if ( !error ) {

	      if ( !OSRModuleLoading::Load (module_name,"","","",false, true ))
	      {
		  // error popup, %1 is name of kernel module (driver)
		  string message = sformat(_("
An error occurred while loading the %1 kernel
module. Mounting the floppy device is not possible.
System information cannot be read.

"), "");
		  if ( use_ui )
		      Report::Error(message);
		  else
		      y2error(message );
		  error = true;
	      }
	  }
      });
      return !error;
  }

  /**
   * Check the mount point! If the mount point does not exist
   * CheckMountPoint create it.
   */
  define boolean CheckMountPoint()``{

      string command  =  sformat("/usr/bin/test -d %1", mount_point );

      if (  WFM::Execute(.local.bash, command) != 0 )  {

	  // error popup (%1 is mount point, e.g. /media/floppy)
	  string message = sformat (_("
The default floppy mount point \"%1\"
does not exist and cannot be created.
"), mount_point);

	  if ((integer) WFM::Execute (.local.bash, sformat("if /usr/bin/test -d %1; then /bin/mkdir %1; fi", "/media" )) == 0)
	  {
	      if ((integer) WFM::Execute (.local.bash, sformat("if /usr/bin/test -d %1; then /bin/mkdir %1; fi", "/media/floppy" )) == 0)
	      {
		  return true;
	      }
	  }
	  if ( use_ui )
	      Report::Error(message);
	  else
	      y2error(message);
	  return false;
      }
      return true;
  }


  /**
   * Mount default floppy device
   */
  define boolean Mount()``{

      //WFM::Execute(.local.bash, "/bin/umount " + mount_point );
      string mpoint = Partitions::MountedOn( floppy_device, Partitions::CurMounted() );
      if ( mpoint != "") {
	  y2milestone("Floppy is already mounted on %1", mpoint );
	  mount_point = mpoint;
	  return true;
      }


      if ( WFM::Execute(.local.bash, "/bin/mount " + floppy_device + " "+mount_point+ " -t auto" ) != 0 )
      {
	// error popup
	string message = _("\nAn error occurred while mounting the floppy.");
	if ( use_ui )
	    Report::Error(message);
	else
	    y2error(message );
	return  false;
      }
      return true;
  }


  /**
   * Unmount floppy device
   */
  define boolean Umount()``{
      if( Partitions::MountedOn (floppy_device, Partitions::CurMounted()) == "")
	  return true;
      if ((integer)WFM::Execute(.local.bash, "/bin/umount "+ mount_point) != 0)
      {
	// error popup
	string message = _("An error occurred while unmounting the floppy.");

	if ( use_ui )
	    Report::Error(message);
	else
	    y2error(message );
	return  false;
      }
      return true;
  }

  /**
   * Open the floppy dialog
   */
  define boolean OpenFloppyDialog(string label, integer steps)``{

    UI::OpenDialog (
	`VBox(
	    `ProgressBar (`id(`progress), label, steps, 0),
	    `VSpacing(0.4),
	    `PushButton(`id(`cancel ) , Label::CancelButton() )
        )
    );
    UI::SetFocus(`id(`cancel ));
    dialog_open = true;
    return true;
  }

  /**
   * Change the text in the floppy dialog and increase the progress bar.
   * If not dialog is opened, write the test to the YaST2 log file (y2log).
   */
  global define void NextStep( string label ) ``{
      y2milestone( label );
      if ( dialog_open && use_ui ) {
	  step = step + 1;
	  UI::ChangeWidget(`id(`progress), `Value, step);
	  UI::ChangeWidget(`id(`progress), `Label, label );
      }
      else {
	  y2milestone( label );
      }
  }

  /**
   * Close the floppy dialog if the dialog is opened.
   */
 define boolean CloseFloppyDialog()``{
      if ( dialog_open ) {
	  if ( UI::CloseDialog() ) {
	      dialog_open = false;
	      return true;
	  }
	  y2error("Floppy Dialog could not be closed");
	  return false;
      }
      return true;
  }

 /**
  * Open floppy for IO.
  * Afterwards you must call OSRFloppy::Close().
  */
  global define boolean Open(boolean use_ui, integer count_io_files )``{

      if( opened ) {
	  y2error("Floppy is already opened. Please use
CloseFloppy(...) to close floppy device.");
	  return false;
      }
      opened = true;

      Reset( use_ui );

      if  ( use_ui ) OpenFloppyDialog( _("Checking floppy device..."), count_io_files + 4 );
      if ( ! CheckPresent()) return false;

      // label text
      NextStep(_("Loading file system modules...") );
      if ( ! LoadFsModules()) return false;

      // label text
      NextStep(_("Checking mount point..."));
      if ( ! CheckMountPoint()) return false;

      // label text
      NextStep(_("Mounting floppy..."));
      if ( ! Mount()) return false;

      return true;
  }

  /**
   * Close Floppy Dialog and unmount floppy device.
   * You must call OSRFloppy::Open(.. ) before.
   */
  global define boolean Close()``{

      if( ! opened ) {
	  y2error("Floppy is not opend. Could not close floppy device.");
	  return false;
      }
      boolean error = false;
      // label text
      NextStep(_("Unmounting floppy..."));
      if ( !Umount())	error = true;

      if ( use_ui )	CloseFloppyDialog();

      opened = false;
      return ! error;
  }


}//EOF
