 /**
 * Module:		proposal_partitions.ycp
 *
 * $Id: partitions_proposal.ycp 22053 2005-03-01 10:36:31Z fehr $
 *
 * Author:		Klaus Kaempf <kkaempf@suse.de>
 *
 * Purpose:		Proposal function dispatcher - partitions.
 *
 *			See also file proposal-API.txt for details.
 */
{
    textdomain "storage";

    import "Arch";

    include "wizard/sequencer.ycp";
    include "partitioning/partition_defines.ycp";
    include "partitioning/do_proposal.ycp";

    string func  = (string) WFM::Args(0);
    map    param = (map) WFM::Args(1);
    map    ret   = $[];

    define void HandlePackages()
	``{
	Pkg::DoRemove( ["lvm2"] );
	list<string> pack = Storage::AddPackageList();
	if( size(pack)>0 )
	    {
	    Pkg::DoProvide( pack );
	    }
	}

    y2milestone( "func:%1 param:%2", func, param );

    if( func == "MakeProposal" && Mode::autoinst ())
	{
	ret["preformatted_proposal"] =
	    TargetChangesToRichText(Storage::GetTargetMap(),
	                            Storage::GetModifyTargets() );
	}
    else if( func == "MakeProposal" )
	{
	boolean force_reset      = param["force_reset"     ]:false;
	boolean language_changed = param["language_changed"]:false;

	y2milestone( "force_reset:%1 lang_changed:%2",
	             force_reset, language_changed );
	if( force_reset || Storage::GetPartProposalFirst() )
	    {
	    if( !Storage::GetPartProposalFirst() )
		{
		Storage::ResetOndiskTarget();
		map<string,map> tg = Storage::GetTargetMap();
		tg = Storage::AddMountPointsForWin(tg);
		Storage::SetTargetMap( tg );
		}
	    map prop = do_proposal();
	    y2milestone( "prop ok:%1", prop["ok"]:false );
	    if( prop["ok"]:false )
		{
		Storage::SetTargetMap( prop["target"]:$[] );
		Storage::SetPartProposalMode( "accept" );
		}
	    else
		{
		Storage::SetPartProposalMode( "impossible" );
		}
	    Storage::SetPartProposalFirst( false );
	    Storage::SetPartProposalActive( true );
	    y2milestone( "prop=%1", prop );
	    }

	if( Storage::GetPartProposalMode() != "impossible" ||
	    !Storage::GetPartProposalActive() )
	    {
	    ret["preformatted_proposal"] =
		TargetChangesToRichText(Storage::GetTargetMap(),
		                        Storage::GetModifyTargets() );
	    }
	else
	    {
	    ret["raw_proposal"] = [];
	    // popup text
	    ret["warning"] = _("No automatic proposal possible.
Specify mount points manually in the 'Partitioner' dialog.");
	    ret["warning_level"] = `blocker;
	    }
	HandlePackages();
	}
    else if ( func == "AskUser" )
	{
	boolean has_next = param["has_next"]:false;

	// call some function that displays a user dialog
	// or a sequence of dialogs here:
	//
	// sequence = DummyMod::AskUser( has_next );

	map aliases =
	    $[ "disk" :
		    ``(WFM::CallFunction("inst_disk", [true, true])),
	       "target_sel" :
		    ``(WFM::CallFunction("inst_target_selection", [true, true])),
	       "target_part" :
		    ``(WFM::CallFunction("inst_target_part", [true, true])),
	       "resize_ui" :
		    ``(WFM::CallFunction("inst_resize_ui", [true, true])),
	       "part_proposal" :
		    ``(WFM::CallFunction("inst_part_proposal", [true, true]))
	     ];

	map seq = $[];

	if( Storage::GetPartProposalMode() != "impossible" )
	    {
	    Storage::SetPartProposalMode( "accept" );
	    }
	else
	    {
	    Storage::SetPartMode( "USE_DISK" );
	    }

	seq["disk"] = $[ `abort : `abort, `cancel: `cancel, `next: `next ];

	if( Arch::s390 ())
	    {
	    Storage::SetPartMode("PROP_MODIFY");
	    seq["ws_start"] = "disk";
	    }
	else if (Mode::autoinst ())
	    {
	    Storage::SetPartMode("PROP_MODIFY");
	    seq["ws_start"] = "disk";
	    }
	else
	    {
	    y2milestone( "ProposalActive %1 ProposalMode %2 PartMode %3", 
	                 Storage::GetPartProposalActive(),
	                 Storage::GetPartProposalMode(),
			 Storage::GetPartMode() );
	    seq["target_sel"] = $[ `abort : `abort, `cancel: `cancel,
	                           `next : "target_part" ];
	    seq["target_part"] = $[ `abort : `abort, `cancel: `cancel,
	                           `next : "disk" ];
	    if( Arch::i386 () )
		{
		seq["resize_ui"] = $[ `abort : `abort, `cancel: `cancel,
		                      `next : "disk" ];
		seq["target_part",`next] = "resize_ui";
		}
	    seq["disk"] = $[ `abort : `abort, `cancel: `cancel, `next : `next ];
	    if( Storage::GetPartProposalMode() != "impossible" &&
	        Storage::GetPartProposalActive() )
		{
		seq["part_proposal"] = $[ `abort : `abort, `cancel: `cancel,
		                          `next : "target_sel" ];
		seq["ws_start"] = "part_proposal";
		}
	    else if( Storage::GetPartMode()=="USE_DISK" )
		{
		seq["ws_start"] = "target_sel";
		}
	    else
		{
		seq["ws_start"] = "disk";
		}
	    }
	y2milestone( "propsal sequence %1", seq );

	Wizard::OpenNextBackDialog();
	symbol result = WizardSequencer(aliases, seq);
	Wizard::CloseDialog();

	y2milestone( "AskUser Mode:%1 ProActive:%2", Storage::GetPartMode(),
	             Storage::GetPartProposalActive() );

	// Fill return map
	HandlePackages();
	ret = $[ "workflow_sequence" : result ];
	}
    else if ( func == "Description" )
	{
	// Fill return map.
	//
	// Static values do just nicely here, no need to call a function.

	ret =
	    $[
	      // label text
	      "rich_text_title"	:	_("Partitioning"),
	      // label text
	      "menu_title"	:	_("&Partitioning"),
	      "id"		:	"partitions_stuff"
	    ];
    }

    return ret;
}
