/**************
FILE          : x11_proposal.ycp
***************
PROJECT       : YaST2
              :
AUTHOR        : Marcus Schäfer <ms@suse.de>
              :
BELONGS TO    : YaST2
              : (X11 integration part using SaX2/ISaX)
              :
DESCRIPTION   : Proposal function dispatcher for
              : X11 configuration
              :
              :
STATUS        : Development
**************/
/**
 * File:        installation/x11_proposal.ycp
 * Package:     X11 Configuration
 * Summary:     Installation Proposal for X11 stuff
 * Authors:     Marcus Schaefer <ms@suse.de>
 *
 * $Id: x11_proposal.ycp 19992 2004-10-27 10:32:44Z arvin $
 */
{
textdomain "x11";

import "XProposal";
import "Report";
import "Installation";
import "Arch";
import "Confirm";

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

//===================================================
// Handle Arch flag
//---------------------------------------------------
if ( (! Arch::x11_setup_needed ()) || (! Installation::x11_setup_needed ()) ) {
	if ( func != "Description" ) {
		ret = $[
			"rich_text_title"       : "",
			"menu_title"            : "",
			"id"                    : "",
			"preformatted_proposal" : "<b> </b>",
			"success"               : true
		];
	}
	return ret;
}

//===================================================
// Make proposal for installation/configuration...
//---------------------------------------------------
if ( func == "MakeProposal" ) {
	//.../
	// create 3Ddiag flag file which is needed to
	// give a correct answer on the 3D question
	//----
	SCR::Execute (
		.target.bash, "/usr/bin/touch /var/lib/YaST2/omit_nvidia_check"
	);
	string  suggestFile  = "/var/cache/sax/files/suggest.ycp";
	boolean forceReset   = param["force_reset"]:false;
	boolean suggestExist = (SCR::Read(.target.size, suggestFile)!=-1);
        boolean success = true;
	if ( ! suggestExist ) {
		forceReset = true;
	}
	if ( forceReset ) {
		y2milestone ("initial configuration: create...");
		if ( Confirm::Detection (_("Graphics Cards")) != true ) {
			y2milestone ("Abort X11 configuration...");
			string proposal = "<ul><li>" + _("Not detected.") + "</li></ul>";
			ret = $[
				"rich_text_title"       : "",
				"menu_title"            : "",
				"id"                    : "",
				"preformatted_proposal" : proposal,
				"success"               : success
			];
			return ret;
		}
		initResult = XProposal::getSuggestion ( true );
	} else {
		y2milestone ("configuration changed: resetting...");
		initResult = XProposal::resetSuggestion ();
	}
	string proposal = "<ul>";
	map allCards = initResult[0]:$[];
	map allViews = initResult[1]:$[];
	boolean done = false;
	integer cnr  = 0;
	repeat {
		map currentCard = allCards[cnr]:$[];
		map currentView = allViews[cnr]:$[];
		cnr = cnr + 1;
		if (currentCard == $[]) {
			string key = _("3D Acceleration:") + " ";
			string env = _("Display Environment:") + " ";
			if (cnr > 2) {
				proposal = proposal + "<li>" + env + _("Multihead") + "</li>";
			} else {
				proposal = proposal + "<li>" + env + _("Single Head") + "</li>";
			}
			if (XProposal::D3Active) {
				proposal = proposal + "<li>" + key + _("Enabled") + "</li>";
			} else {
			if (cnr > 2) {
				proposal = proposal + "<li>" + key + _("Unavailable in multihead environment") + "</li>";
			} else if ( SCR::Execute (
				.target.bash, "/usr/bin/3Ddiag --num-3dboards"
			) == 0) {
				proposal = proposal + "<li>" + key + _("Unavailable") + "</li>";
			} else {
				proposal = proposal + "<li>" + key + _("Disabled") + "</li>";
			}
			}
			proposal = proposal + "</ul>";
			done = true;
		} else {
			map card = currentCard["Card"]:$[];
			map view = currentView["Desktop"]:$[];
			string cardVendor = card["Vendor"]:"nil";
			string cardModel  = card["Name"]:"nil";
			string cardDriver = card["Driver"]:"nil";
			string moniVendor = view["VendorName"]:"nil";
			string moniModel  = view["ModelName"]:"nil";
			string colorDepth = view["ColorDepth"]:"nil";
			string modes = "Modes:" + colorDepth;
			string resolution = view[modes]:"nil";
			list resList = splitstring (resolution,",");
			resolution = resList[0]:"nil";
			string moniName = moniVendor + " " + moniModel;
			if (moniVendor == moniModel) {
				moniName = _("Unknown Monitor");
			}
			proposal = proposal
			+ "<li>"
				+ cardVendor + " " + cardModel + " ( " + cardDriver + " )"
				+ " " + _("Connected to Monitor:") + "<br>"
				+ moniName + " [ " + resolution + "@" + colorDepth
				+ " " + _("bit") + " ]"
			+ "</li>";
		}
	} until ( done == true );
	y2milestone (
		"x11_proposal data string: <%1>",
		proposal
	);
	ret = $[
		"preformatted_proposal" : proposal
	];
}
//===================================================
// Run SaX2 to change the configuration...
//---------------------------------------------------
else if ( func == "AskUser" ) {
	y2milestone ("Calling SaX2 for configuration changes...");
	XProposal::changeConfiguration();
}
//===================================================
// Return human readable titles for the proposal
//---------------------------------------------------
else if ( func == "Description" ) {
	ret = $[
		"rich_text_title" : _("Graphics Cards"),
		"menu_title"      : _("&Graphics Cards"),
		"id"              : "x11_conf"
	];
}
//===================================================
// Return "success" cause SaX2 has written the config
//---------------------------------------------------
else if ( func == "Write" ) {
	boolean success = true;
	XProposal::concludeConfiguration(true);
	ret = $[
		"success" : success
	];
}
// Do Return...
return ret;
}
