/**
 * Module: 		auto_part_ui.ycp
 *
 * Authors: 		Andreas Schwab (schwab@suse.de)
 *			Klaus Kämpf (kkaempf@suse.de)
 *
 * Purpose: 		This module contains the user interface
 *			definitions for the automatic partitioner
 *
 *
 *
 * $Id: auto_part_ui.ycp 22133 2005-03-02 10:16:36Z fehr $
 *
 * used globals:
 *
 * defined functions
    global define display_error_box (string reason) ``{
    global define warn_not_bootable () ``{
    global define partition_text (integer nr, map pentry) ``{
    global define ask_free_space ()
    global define construct_partition_dialog ()
    global define open_auto_dialog (string targetname, term targetbox) ``{
    global define create_resize_dialog (list partitions) ``{
 */
{
    textdomain "storage";

    import "Wizard";
    import "Partitions";
	import "Report";

    include "partitioning/partition_defines.ycp";
    import "Popup";
    import "Product";

    // --------------------------------------------------------------
    // warning and pop-ups

define void display_error_box (string reason) ``{
	// There is a consistency check for the selection. Next is the message, that
	// is displayed. The reason is determined within this consistency check and
	// then the message is passed through this interface transparently
	string text = sformat(_("The current selection is invalid:\n%1"), reason);
	Popup::Message(text);
    };

define void warn_not_bootable () ``{
	  // This is a warning message that pops up when the selection does not
	  // include a bootable disk region (< 1024 cylinders).
	  Report::Warning(sformat(_("With the current partitioning, your %2 installation
cannot be booted directly, because your /boot partition 
is above cylinder %1.
"), Partitions::BootCyl(), Product::name) );
    };


    // Return a text that describes a partition

define string partition_text (integer nr, map pentry, integer bps) 
    ``{
    string size_str = ByteToHumanString(size_of_region( pentry["region"]:[], bps));
    if( pentry["type"]:`unknown == `free)
        // list of partition checkboxes: show partition as unassigned
	// e.g. "1:    2 GB, unassigned"
        return sformat( _("&%1:    %2, unassigned"), nr, size_str );
    else
	// list of partition checkboxes: show partition as assigned
	return sformat( "&%1:    %2, %3 (%4)", nr, size_str,
			pentry["fstype"]:"", pentry["device"]:"" );
    };

define boolean ask_free_space () ``{
	// we found an unassigned partition which is large enough for a minimal installation
	return Popup::YesNo(sformat(_("\
There is free space on the disk.
Should YaST2 use this for %1?"),Product::name));
    };

define void inform_about_foreign () ``{
// This disk has an unsupported partition scheme and must be
// partitioned with the custom partitioner (i.e. Apple Mac)
	Report::Warning(_("\
Warning: This disk has foreign partitions and
must be handled by the custom partitioner."));
    };

define map construct_partition_dialog( list<map> partitions, string ptype, 
				       integer bps ) 
    ``{
    term vbox_contents = 
	`VBox(  // Message between the full name of the hard disk to use
	        // and the "Use entire hard disk" button
		// - please avoid excessively long lines - rather, include a newline
		`Left( `Label (sformat(_("Choose partitions that can be erased\nto make room for %1:"),Product::name))),
		`VSpacing(0.3)
	     );

    // Add option to select the entire disk at once
    vbox_contents = 
	add( vbox_contents,
	     // pushbutton to choose the entire disk, erasing all data on 
	     // the disk this is an easy way to select all partitions on 
	     // the target disk
	     `Left(`PushButton (`id (`full), _("Use &entire hard disk"))));

    vbox_contents = add (vbox_contents, `VSpacing(0.5) );

    integer i = 0;
    integer ui_id = 0;
    foreach( map pentry, partitions, 
	``{
	symbol ptype = pentry["type"]:`unknown;
	if (ptype != `extended) 
	    {
	    list<string> hit = filter(string v, Partitions::not_shown_partitions,
			      ``(issubstring(pentry["fstype"]:"", v)) );
	    // skip #3 on AlphaBSD and SparcBSD
	    if (size(hit)==0 &&
		((ptype!=`bsd && ptype!=`sun) || pentry["nr"]:0 != 3) &&
		!pentry["create"]:false) 
		{
		ui_id = pentry["ui_id"]:0;
		i = i + 1;
		vbox_contents = add (vbox_contents,
				 `Left(`CheckBox (`id (ui_id),
						  partition_text(i, pentry, bps),
						  pentry["delete"]:false)));
		}
	    }
	});

    return $[ "term": vbox_contents, "high_id": ui_id ];
    };

define term create_whole_disk_dialog () ``{
	// There were no prior partitions on this disk.
	// No partitions to choose from will be displayed.
	return (`Left(`Label( sformat(_("There are no partitions on this disk yet.
The entire disk will be used for %1."),Product::name))));
    };

define term create_resize_dialog (list<map> partitions, integer bps) ``{
        // popup text
	string explanation = _("This disk appears to be used by Windows.
There is not enough space to install Linux.");
	
	map pentry = partitions[0]:$[];
	string part_name = sformat ( "%1, %2 (%3)",
				     ByteToHumanString(size_of_region(pentry["region"]:[], bps )),
				     pentry["fstype"]:"",
				     pentry["device"]:"" );
	
	return (`HVSquash (
	           `RadioButtonGroup (
		      `HBox(
			    `HSpacing(1.5),
			    `VBox (
				   `Left(`Label( explanation )),
				   `VSpacing(0.5),
				   `Left (`RadioButton (`id (`part_id),
							// Radio button for using an entire (Windows) partition for Linux
							_("&Delete Windows completely") ) ),
				   `VSpacing(0.5),
				   `Left (`RadioButton (`id (`resize),
							// Radio button for resizing a (Windows) partition
							_("&Shrink Windows partition"), true ) )
				   )
			    )
		      )
		   )
		);
    };

    // --------------------------------------------------------------


    // normal case
    //
define void open_auto_dialog (string targetname, term targetbox) ``{

	// helptext for semi-automatic partitioning
	// part 1 of 5
	string helptext = _("<p>
Select where on your hard disk to install &product;.
</p>
");
	// helptext, part 2 of 5
	helptext = helptext + _("<p>
You can either use the <b>entire hard disk</b> or one or more of the
partitions or free regions shown. The selected space must be contiguous.
</p>");
	// helptext, part 3 of 5
	helptext = helptext + _("<p>
You must start your selection with the highest numbered entry in 
the list. You cannot select entries 'in the middle'.
</p>\n");
	// helptext, part 4 of 5
	helptext = helptext + _("<p>
Notice: If you select a region that is not shown as <i>free</i>, you
might loose existing data on your hard disk. This could also affect
other operating systems.
</p>");
	// helptext, part 4 of 5
	helptext = helptext + _("<p>
<b><i>The marked regions will be deleted. All data there will be
lost. </i></b> There will be no way to recover this data.
</p>
");

	// Information what to do, background information
	Wizard::SetContents( _("Preparing Hard Disk -- Step 2"),
			   `HCenter(
				    `HSquash(
					     `Frame(
						    // Frame title for installation target hard disk / partition(s)
						    _("Installing on:"),
						    `HBox(
							  `HSpacing(),
							  `VBox(
								`VSpacing(0.5),
								`HBox(
								      `HSpacing(2),
								      `Left( `Label(`opt(`outputField), targetname) )
								      ),
								`VSpacing(0.5),

								// All partitions are listed that are found on the target (hard disk).
								`Left(`HVSquash( targetbox ) ),
								`VSpacing(0.5)
								),
							  `HSpacing()
							  )
						    )
					     )
				    ), helptext, (boolean)WFM::Args (0), (boolean)WFM::Args (1));
    };


    // resize case
    //
define void open_auto_dialog_resize (string targetname, term targetbox) 
    ``{
    // helptext for semi-automatic partitioning
    // part 1 of 2
    string helptext = _("<p>
The selected hard disk is probably used by Windows. There is not enough
space for &product;. You can either <b>delete Windows completely</b> or
<b>shrink</b> it to get enough free space.
</p>");
    // helptext, part 2 of 2
    helptext = helptext + _("<p>
If you delete Windows, all data on this partition will be <b>irreversibly
lost</b> in the installation. When shrinking Windows, we <b>strongly
recommend a data backup</b>, because the data must be reorganized.
This may fail under rare circumstances.
</p>
");

    // Information what to do, background information
    Wizard::SetContents( _("Preparing Hard Disk -- Step 2"),
		       `HCenter(
				`HSquash(
					 `Frame(
						// Frame title for installation target hard disk / partition(s)
						_("Installing on:"),
						`HBox(
						      `HSpacing(),
						      `VBox(
							    `VSpacing(0.5),
							    `HBox(
								  `HSpacing(2),
								  `Left( `Label(`opt(`outputField), targetname) )
								  ),
							    `VSpacing(0.5),

							    // All partitions are listed that are found on the target (hard disk).
							    `Left(`HVSquash( targetbox ) ),
							    `VSpacing(0.5)
							    ),
						      `HSpacing()
						      )
						)
					 )
				), helptext, (boolean)WFM::Args(0), (boolean)WFM::Args (1));
    };

}
