/**
 * File:
 *   lvm_ui_lib.ycp
 *
 * Module: 
 *    configuration of lvm: lib for the user interface
 *
 * Summary:
 *
 * Authors:
 *   mike <mike@suse.de>
 *
 *
 * $Id: lvm_ui_lib.ycp 22496 2005-03-14 11:43:10Z arvin $
 *
 */


{

textdomain "storage";

#include "partitioning/signatures.ycp";

////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
//                             Helptext  defines                  //
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////
// helptext for dialog createEditLogicaVolume
//////////////////////////////////////////////////////////////////////
 
define string getCreateEditLogicaVolumeHelptext()
    ``{
    string help_text = "cstein";
    return( help_text );
    };

//////////////////////////////////////////////////////////////////////
// helptext for logical volume
//////////////////////////////////////////////////////////////////////

define string getLvHelptext()
    ``{
    string help_text = "";

    // helptext LVM partitioning. 
    help_text =  _("<p>Here, create the logical 
volumes used to store your data.</p>
");

    // helptext LVM partitioning. 
    help_text = help_text + _("<p>
Logical volumes are
usable almost everywhere normal <b>disk partitions</b> can be used.
You can create file systems on logical volumes and use them, for example, as swap 
or as raw partitions for databases.</p>
");

    // helptext LVM partitioning. 
   help_text = help_text + _("<p>
If there is still unallocated
physical storage in a volume group and you use <b>reiserfs</b> as your file system,
extend a logical volume and the underlying file system while it
is <b>mounted</b> and in <b>use</b>.</p>
");

    // helptext LVM partitioning. 
    help_text = help_text + _("<p>
The logical volumes need to be large enough
to hold all the files to install now, but you do not necessarily need to
allocate all your physical storage now. The file systems can be increased 
later while your system is in use.
</p>
");

    return( help_text );
    };



//////////////////////////////////////////////////////////////////////
// helptext for physical volume
//////////////////////////////////////////////////////////////////////

define string getPvHelptext()
    ``{
    string help_text = "";

 // helptext LVM partitioning. 
    help_text =  _("<p><b>Add partitions</b> (called physical volumes) to your volume group.</p>");

    // helptext LVM partitioning. 
    help_text = help_text + _("<p>
The volume group forms the <b>storage pool</b> from which your logical volumes,
like virtual partitions, are allocated.</p>
");

    // helptext LVM partitioning. 
    help_text = help_text + _("<p>
Under normal circumstances, there is no need to have more than one volume 
group. If you need more than one volume group for special reasons,
create them here. Each volume group must have at least one partition 
that belongs to that volume group.</p>
");

    // helptext LVM partitioning. 
    help_text = help_text + _("<p>
Each physical volume belongs <b>to exactly one</b> volume group. Assign all
partitions to use with Linux LVM to volume groups.</p>
");

    // helptext LVM partitioning. 
    help_text = help_text + _("<p>
Additionally, it is possible to remove volume groups 
when they do not contain any logical volumes.</p>
");

    return( help_text );
    };
    


//////////////////////////////////////////////////////////////////////
// helptext for lvm module in the installed system
//////////////////////////////////////////////////////////////////////

define string getLvmHelptext()
    ``{
     string help_text = "";

     // helptext LVM partitioning. 
     help_text =  _("<p>With this tool, manage or create logical volumes.
</p>
");

     // helptext LVM partitioning. 
     help_text = help_text + _("<ol><li>
First, create a volume group, if you have not done this already.
</li>
");

     // helptext LVM partitioning. 
     help_text = help_text + _("<li>
In the next step, add <b>physical volumes</b> (see paragraph below).
</li>
");

     // helptext LVM partitioning. 
     help_text = help_text + _("<li>
In the last step, create <b>logical volumes</b> (see paragraph below).
</li></ol><br>
");

     // helptext LVM partitioning. 
     help_text = help_text + _("<p>
<b>Physical Volumes:</b></p>");
     help_text = help_text + getPvHelptext();
     // helptext LVM partitioning. 
     help_text = help_text + _("<p>
<b>Logical Volumes:</b></p>");
     help_text = help_text + getLvHelptext();

     return( help_text );
     };

    

/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
//                                  DIALOG  defines                //
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////

//----------------------------------------------------
// give bar widget if possible
    
define term get_bargraph_or_compatible( string avail_vg_size )
    ``{
    if( UI::HasSpecialWidget( `BarGraph ))
	{
	return( `HBox(
		// label text
		`Label(_("Available size:")),
		`HSpacing(0.5),
		`Left(`BarGraph( `id(`vg_size), `opt(`hstretch),
				   [ 0, 10 ], [ "", ""]))
		// `Label( `opt( `outputField), avail_vg_size )
		));
	}
    else
	{
	return( `HBox(
		    // label text
		    `Bottom(`Left(`Label(_("Available Size:")))),
		    `Bottom(`Left(`Label( `id(`vg_size),
					  `opt( `hstretch, `outputField), avail_vg_size ))),
		    `HWeight(1,`HStretch())
		    // `Label( `opt( `outputField), avail_vg_size )
		     ));
	}
    };
		
//----------------------------------------------------
// give table for step 2: create volume group, add phys. volumes (pv)
    
define term get_pv_table_content( list table_list )
    ``{
    return( `VBox(
		`Table(`id(`pv_table), `opt(`notify),
		       // header text
		       `header(_("  Device  "), `Right(_("  Size  ")), `Center(_("  Type  ")), `Center(_(" Volume Group "))),
			table_list
			),
			// button text
		       `HBox( `PushButton(`id(`pv_add), _("&Add Volume")),
			      // button text
			      `PushButton(`id(`pv_delete), _("&Remove Volume"))
			     ),
		       `VSpacing(0.5)
		       )
		  );
    };

	   
//----------------------------------------------------
// give table for step 3: add/delete logical volumes (lv)
   
define term get_lv_table_content( list table_list, boolean view_all_mnts )
    ``{
    return( `VBox(
		  `Table(`id(`lv_table), `opt(`notify),
			 // table column headings
			 `header(`Left(_("Device  ")), `Center(_(" Mount   ")),
				  // table column heading Vol.Grp = Volume Group
				 `Center(_(" Vol. Grp.")),
				 // table column headings
				 `Right(_("Size")), `Center(_(" Type ")) ),
			 table_list
			 ),
		  `Left(`CheckBox( `id(`viewmnt),
				   `opt(`notify),
				   // button text
				   _("&View all mount points, not just the current volume group"),
				   view_all_mnts )),
		  `VSpacing(0.2),
			 // button text
		  `HBox( `PushButton(`id(`lv_add),   _("A&dd")),
			 // button text
			 `PushButton(`id(`lv_edit),  _("&Edit")),
			 // button text
			 `PushButton(`id(`lv_delete), _("&Remove"))
			 ),
		  `VSpacing(0.5)
		  )
	  );
    };


    
//////////////////////////////////////////////////////////////////////
// give main page for configuration - X11 version 
//////////////////////////////////////////////////////////////////////

define term get_config_content( list pv_table_list, list lv_table_list,
			        list vg_list, string phys_vg_size,
			        string avail_vg_size, boolean view_all_mnts )
    ``{
    return(  `HBox( `HSpacing(0.5),
		    `VBox(
			  `VSquash(`HBox( 
					 `Left(`ReplacePoint( `id(`rvg ),
							      `ComboBox(`id(`vg),
									`opt(`notify),
									// label text
									_("V&olume Group Name"),
									vg_list ))),
					 `HSpacing(2),
					 // button text
					 `Left(`Bottom(`PushButton( `id(`vg_add_vg), _("A&dd group")))),
					 `HSpacing(0.5),
					 // button text
					 `Left(`Bottom(`PushButton( `id(`vg_remove_vg), _("Remove &group")))),
					 `HWeight(1,`HStretch())
					 )),
			  `VSpacing(1),
			  // label text
			  `Frame(_("Edit the current volume group:"),
				 `HBox( 
				    `HWeight( 50,
				       `VBox(
					     `VSpacing(0.7),
					      // label text
					     `Heading(_("Physical volumes")),
					     `VSpacing(0.7),
					     `HBox(
						   // label text
						   `Label(_("Physical volume size:")),
						   `Label( `id(`pv_size), `opt(`outputField), phys_vg_size )
						   ),
					     `VSpacing(0.5),
					     get_pv_table_content( pv_table_list )
					     )),
				    `HWeight( 50,
				       `VBox(
					     `VSpacing(0.5),
					     // label text
					     `Heading(_("Logical volumes")),
					     `VSpacing(0.5),
					     get_bargraph_or_compatible( avail_vg_size ),
					     `VSpacing(0.2),
					     get_lv_table_content( lv_table_list, view_all_mnts )
					     ))
				       )
				  )
			  ),
		    `HSpacing(0.5)
		    )
	      );
    };


    
    //////////////////////////////////////////////////////////////////////
    // give main page for step 2: create volume group, add phys. volumes (pv)
    //////////////////////////////////////////////////////////////////////

define term get_pv_content( list table_list, list vg_list, string vg_size_str )
    ``{
    // button text
    string lvm_mp_button = _("LVM Multipath detection");
    return(  `HBox(`HSpacing(2.5),
		   `VBox(
		       `VSpacing(0.1),
		       `VWeight(20, `HBox( 
			   `Left(`ReplacePoint(`id(`rvg), `ComboBox(`id(`vg),
				               `opt(`notify), 
					       // button text
					       _("Volume &Group:"), vg_list ))),
			   `HSpacing(0.5),
			   // label text
			   `Left(`Label(_("Size:"))),
			   `Left(`Label( `id(`pv_size), 
			                 `opt( `hstretch, `outputField), 
					 vg_size_str )),
			   `HWeight(1,`HStretch()),
			   `Bottom(`Right(`VBox(
			       `Top(`PushButton( `id(`vg_remove_vg), 
			                         `opt(`hstretch), 
						 // button text
						 _("Remove &group"))),
			       `Bottom(`PushButton( `id(`vg_add_vg), 
			                            `opt(`hstretch), 
						    // button text
						    _("A&dd group"))))))
				)),
			   `VSpacing(0.2),
			   `VWeight(50, get_pv_table_content( table_list ))
			),
		    `HSpacing(2)
		  )
	  );
    };

    					       
    
    //////////////////////////////////////////////////////////////////////
    // give main page for step 3: add/delete logical volumes (lv)
    //////////////////////////////////////////////////////////////////////
    
define term get_lv_content( list table_list, list vg_list, string avail_size,
                            boolean view_all_mnts )
    ``{
    if ( UI::HasSpecialWidget( `BarGraph ))
	{
	return( `HBox(`HSpacing(2.5),
		      `VBox(`VSpacing(0.5),
			    `VSquash(`HBox(
					   `ReplacePoint( `id(`rvg ),
							  `ComboBox(`id(`vg),
								    `opt(`notify),
								    // Label of Combobox    
								   _("V&olume Group Name"),
								   vg_list )),

					   `HSpacing(0.5),
					   `Left(`BarGraph( `id(`vg_size), `opt(`hstretch),
							    [ 0, 10 ], [ "", ""]))
					   )),
			    `VSpacing(0.5),
			    get_lv_table_content( table_list, view_all_mnts )
			    ),
		      `HSpacing(2)
		     )
	      );
	}
    else
	{
	return( `HBox(`HSpacing(2.5),
		      `VBox(`VSpacing(0.5),
			    `VSquash(`HBox(
					   `Left(`ReplacePoint( `id(`rvg ),
							  `ComboBox(`id(`vg),
								    `opt(`notify),
								    // Label of Combobox    
								   _("V&olume Group Name"),
								   vg_list ))),

					   `HSpacing(0.5),
					    // label text
					   `Bottom(`Left(`Label(_("Available Size:")))),
					   `Bottom(`Left(`Label( `id(`vg_size),
								 `opt( `hstretch, `outputField), avail_size ))),
					   `HWeight(1,`HStretch())
					   
					   )),
			    `VSpacing(0.5),
			    get_lv_table_content( table_list, view_all_mnts )
			    ),
		      `HSpacing(2)
		      )
	      );
	}
    };


		    
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
//                                     Help defines                //
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////

    
    
    
//////////////////////////////////////////////////////////////////////
// checks if pesize is valid   8K to 521M in power of 2
//
// 8 is 8k
// 16K == 16k == 16KB == 16kb

// --------------------------------------------------------------------
// Check if the string is a number 
// Returns true or false
// ---------------------------------------------------------------------
//

define boolean icheck_pesize( string input )
    ``{
    return( pesize_str_to_byte( input ) > 0 );
    };


//////////////////////////////////////////////////////////////////////
// checks if vgname is a valid device name : todo: do it better 

define boolean icheck_devname( string name )
    ``{
    if ( size( name ) == 0 ) return( false );
   
    return( ( findfirstof( name, " ;/`'´!,\"%#" ) == nil ) );
    }


//////////////////////////////////////////////////////////////////////
// checks if vgname is a valid device name : todo: do it better
define boolean icheck_volume_name( string vgname )
    ``{
    return( icheck_devname( vgname ));
    }

//////////////////////////////////////////////////////////////////////
// checks if vgname is a valid device name : equivalent to validate_name
// in lvm2 source
    
define boolean icheck_vgname( string vgname )
    ``{

    if (size (vgname) == 0 || size (vgname) > 128)
	return false;

    if (substring (vgname, 0, 1) == "-")
	return false;

    string allowed_chars = "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
	"abcdefghijklmnopqrstuvwxyz" + "._-+";

    if (findfirstnotof (vgname, allowed_chars) != nil)
	return false;

    return true;
    }

 
//////////////////////////////////////////////////////////////////////
// show a new list of volume groups
//////////////////////////////////////////////////////////////////////

define void new_vg_list( list vg_list )
    ``{
    UI::ReplaceWidget(`id(`rvg), `ComboBox(`id(`vg), `opt(`notify),
		      // label text
		      _("V&olume Group"), vg_list ) );
    };

}
