/**
 * File:
 *   inst_evms.ycp
 *
 * Module:
 *   evms
 *
 * Summary:
 *
 * Authors:
 *   Thomas Fehr <fehr@suse.de>
 *
 *
 *
 * $Id: inst_evms.ycp 18114 2004-07-15 09:59:47Z fehr $
 *
 */

{

    textdomain "storage"; 

    import "Installation";
    import "Mode";
    import "Storage";
    import "Wizard";
    import "Label";
    
    include "partitioning/partition_defines.ycp";
    include "partitioning/custom_part_dialogs.ycp";
    include "partitioning/custom_part_lib.ycp";
    include "partitioning/custom_part_check_generated.ycp";
    include "partitioning/evms_lib.ycp";

    map<string,map> targetMap    = Storage::GetTargetMap();
    string no_container = "-none-";
    
    list evms_cont = get_containers( targetMap, no_container );
    list table = [];

    string  current_co   = evms_cont[0]:"";

    term dlg = 
	`HBox( 
	    `HSpacing(2.5),
	    `VBox(
		`VSpacing(0.1),
		`VWeight( 20, 
		    `HBox(
			`VBox( 
			    get_bargraph_if_possible(),
			    `HBox(
				`Left(`ReplacePoint(
					  `id(`rcont), 
					  `ComboBox(`id(`cont), `opt(`notify),
						    // button text
						    _("EVMS &Container"), 
						    [] ))),
				`HSpacing(2),
				       // label text
				`VBox( `Left(`Label(_("Size:"))),
					// label text
				       `Left(`Label(_("Available:")))),
				`VBox( `Left(`Label( `id(`co_size),
						     `opt(`hstretch, 
						          `outputField), 
						     "" )),
				       `Left(`Label( `id(`co_free),
						     `opt(`hstretch, 
						          `outputField), 
						     "" )))
				)
			    ),
			`HWeight(1,`HStretch()),
			`Top(`Right(
			    `VBox(
				`PushButton( `id(`add_container),
					     `opt(`hstretch),
					     // button text
					     _("C&reate Container")),
				`PushButton( `id(`edit_container),
					     `opt(`hstretch),
					     // button text
					     _("&Edit Container")),
				`PushButton( `id(`remove_container),
					     `opt(`hstretch),
					     // button text
					     _("&Remove Container"))
				 )))
			)
		    ),
		`VSpacing(1),
		`VWeight( 50, 
		    `VBox(
			`Table( `id(`evms_table), `opt(`notify),
				// header text
				`header(_("Device"), `Right(_(" Size")), 
				        `Center(_("Format")), _(" Mount"), 
					`Center(_("Type"))), [] ),
			`VSpacing(0.2),
			// button text
			`HBox( `PushButton(`id(`evms_add), _("A&dd")),
			       // button text
			       `PushButton(`id(`evms_edit), _("&Edit")),
			       // button text
			       `PushButton(`id(`evms_delete), _("&Remove"))
			       // button text
			       //`PushButton(`id(`evms_convert), _("C&onvert"))
			     ),
			`VSpacing(0.5)
			)
		    )
		),
	    `HSpacing(2)
	    );

    // heading text
    Wizard::SetContents( _("EVMS Configurator"), dlg, "", true, true);
    Wizard::SetHelpText( getEvmsHelptext() );

    // Display current container:
    new_co_list( evms_cont );
    UI::ChangeWidget( `id(`cont), `Value, current_co );
    map co = get_co_map( current_co, targetMap );
    new_co_size( co );
    
    
    //////////////////////////////////////////////////////////////////////
    // mainloop
    //////////////////////////////////////////////////////////////////////

    string id  =  "";
    symbol ret = `next;
    boolean changed = false;

    repeat
	{
	/////////////////////////////////////////////////////////////////
	// Show the current state:
	table 	 = get_evms_volumes( targetMap );
	UI::ChangeWidget( `id(`evms_table), `Items, table);

        UI::SetFocus(`id(`evms_table));

	if( id!=nil && id != "" )
	    {
	    UI::ChangeWidget( `id(`evms_table), `CurrentItem, id);
	    }

        /////////////////////////////////////////////////////////////////
        // Wait for User input

	ret = (symbol)Wizard::UserInput();

	////////////////////////////////////////
	// user has changed the current volume group
	////////////////////////////////////////
	
	if( ret == `cont )
	    {
	    current_co = (string)UI::QueryWidget( `id(`cont), `Value);
	    y2milestone("TTT %1", current_co );
	    co = get_co_map( current_co, targetMap );
	    new_co_size( co );
	    }

        ////////////////////////////////////////
        // Add a new container:
        ////////////////////////////////////////

        if( ret == `add_container )
	    {
            map ad = add_evms_container( targetMap, 
	                                 evms_cont[0]:""==no_container );

            // has the user cancelled the dialog?
            if( ad["ok"]:false )
		{
		changed = true;
                targetMap = ad["targets"]:$[];
		evms_cont = get_containers( targetMap, no_container );
		new_co_list( evms_cont );
		current_co = ad["name"]:"";
		UI::ChangeWidget( `id(`cont), `Value, current_co );
		co = get_co_map( current_co, targetMap );
		new_co_size( co );
		}
	    }
	else if( ret == `edit_container )
	    {
            map ad = edit_evms_container( targetMap, current_co );

            // has the user cancelled the dialog?
            if( ad["ok"]:false )
		{
		changed = true;
                targetMap = ad["targets"]:$[];
		co = get_co_map( current_co, targetMap );
		new_co_size( co );
		}
	    }

	////////////////////////////////////////
	// Remove volume group:
	////////////////////////////////////////
	
	else if( ret == `remove_container && current_co!=no_container )
	    {
	    map ad = remove_evms_container( targetMap, current_co );
	    if( ad["ok"]:false )
		{
		changed = true;
                targetMap = ad["targets"]:$[];
		evms_cont = get_containers( targetMap, no_container );
		new_co_list( evms_cont );
		current_co = evms_cont[0]:"";
		UI::ChangeWidget( `id(`cont), `Value, current_co );
		co = get_co_map( current_co, targetMap );
		new_co_size( co );
		}
	    }
	else if( ret == `evms_add && current_co != no_container )
	    {
            map ad = add_evms_volume( current_co, targetMap );

            // has the user cancelled the dialog?
            if( ad["ok"]:false )
		{
                targetMap = ad["targets"]:$[];
		co = get_co_map( current_co, targetMap );
		changed = true;
		new_co_size( co );
		}
	    }
	else if( ret == `evms_edit )
	    {
	    id = (string)UI::QueryWidget(`id(`evms_table), `CurrentItem);

            map ad = edit_evms_volume( id, targetMap );

            // has the user cancelled the dialog?
            if( ad["ok"]:false )
		{
                targetMap = ad["targets"]:$[];
		co = get_co_map( current_co, targetMap );
		changed = true;
		new_co_size( co );
		}
	    }
	else if( ret == `evms_delete )
	    {
	    id = (string)UI::QueryWidget(`id(`evms_table), `CurrentItem);

            map ad = delete_evms_volume( id, targetMap );

            // has the user cancelled the dialog?
            if( ad["ok"]:false )
		{
                targetMap = ad["targets"]:$[];
		co = get_co_map( current_co, targetMap );
		changed = true;
		new_co_size( co );
		}
	    }

	////////////////////////////////////////
	// Finish this step
	////////////////////////////////////////
	
	if( ret == `next )
	    {
	    Storage::SetTargetMap( targetMap );
	    }

	if( ret == `back || ret == `cancel || ret == `abort )
	    {
	    if( changed )
		{
		// popup text 
		string txt = _("You have changed the EVMS configuration.
These changes will be lost if you exit the dialog now.
Continue?
");
		if( !Popup::ContinueCancel( txt ))
		    {
		    ret = `again;
		    }
		}
	    }

    } until (ret == `next || ret == `back || ret == `cancel || ret == `abort );
    
    return ret;
}

// end
