/**
 * File:	modules/AutoinstX11.ycp
 * Module:	Auto-Installation
 * Summary:	X11
 * Authors:	Anas Nashif <nashif@suse.de>
 *
 * $Id: AutoinstX11.ycp 21524 2005-02-11 04:48:30Z nashif $
 */
{
    textdomain "x11";
    module "AutoinstX11";
    import "Summary";
    import "Directory";
    import "XProposal";


    global map x11 = $[];

    global map monitor = $[];
    global map display = $[];

    global string current_vendor = "";
    global string current_model  = "";

    // The lists to hold vendors and models when all_monitors is taken apart.
    //
    global list vendors = [];
    global list models  = [];


    string static_monitors  = Directory::datadir + "/monitors.ycp";

    // The monitor data base
    //
    global list<map> all_monitors = [];



    /* default value of settings modified */
    global boolean modified = false;


    /**
     * Function sets internal variable, which indicates, that any
     * settings were modified, to "true"
     */
    global define void SetModified ()
    {
        modified = true;
    }

    /**
     * Functions which returns if the settings were modified
     * @return boolean  settings were modified
     */
    global define boolean GetModified ()
    {
        return modified;
    }

    /**
     * Import settings
     */
    global define boolean Import (map settings)
    {
	x11 = settings;
	monitor = x11["monitor"]:$[];
	display =  monitor["display"]:$[];
	current_vendor = monitor["monitor_vendor"]:"" ;
	current_model  = monitor["monitor_device"]:"" ;

	XProposal::X11 = x11;
	return true;
    }

    /**
     * Export
     */
    global define map Export ()
    {

	monitor["display"] =  display;
	monitor["monitor_device"] =  current_model;
	monitor["monitor_vendor"] = current_vendor;
	x11["monitor"] = monitor;
	return x11;
    }

    /**
     * Summary
     */
    global define string Summary()
    {
	string summary = "";
	map m = x11["monitor"]:$[];
	string my_current_vendor = m["monitor_vendor"]:"";
	string my_current_model  = m["monitor_device"]:"";

	map   d   = m["display"]:$[];


	string my_monitor =  Summary::NotConfigured();

	if (size(my_current_vendor) > 0)
	{
	    my_monitor =  sformat("%1   %2", my_current_vendor, my_current_model);
	}

	string horizontal = sformat("%1 - %2",  d["min_hsync"]:0 , d["max_hsync"]:0);
	string vertical = sformat("%1 - %2",  d["min_vsync"]:0 , d["max_vsync"]:0);
	string nc = Summary::NotConfigured ();
	string depth = sformat ("%1", (x11["color_depth"]:-1 != -1) ? x11["color_depth"]:-1 : nc );

	summary = Summary::AddHeader(summary, _("Configure X11"));
	summary = Summary::AddLine(summary,  (x11["configure_x11"]:false) ? _("Yes") : nc);

	summary = Summary::AddHeader(summary, _("Enable 3D Support If Possible"));
	summary = Summary::AddLine(summary,  (x11["enable_3d"]:false) ? _("Yes") : nc);

	summary = Summary::AddHeader(summary, _("Color Depth"));
	summary = Summary::AddLine(summary, depth );


	summary = Summary::AddHeader(summary, _("Resolution"));
	summary = Summary::AddLine(summary,  (x11["resolution"]:"" != "") ? x11["resolution"]:"" : nc);

	summary = Summary::AddHeader(summary, _("Display Manager"));
	summary = Summary::AddLine(summary,  (x11["display_manager"]:"" != "") ? x11["display_manager"]:"" : nc);

	summary = Summary::AddHeader(summary, _("Window Manager"));
	summary = Summary::AddLine(summary,  (x11["window_manager"]:"" != "") ? x11["window_manager"]:"" : nc);


	summary = Summary::AddHeader(summary, _("Monitor"));
	summary = Summary::AddLine(summary, my_monitor);
	summary = Summary::AddHeader(summary, _("Horizontal frequency"));
	summary = Summary::AddLine(summary, horizontal);
	summary = Summary::AddHeader(summary, _("Vertical frequency"));
	summary = Summary::AddLine(summary, vertical);

	return summary;
    }


    /**
     *	Get a list of all monitor vendors from the monitor database.
     *  Add a special monitor list entry for users who don't want to install X11 at all.
     *	@return list
     */
    define void readVendors()
    {
	vendors = toset( maplist( map monitor, all_monitors, ``( monitor["vendor"]:"" )));
	return;
    }



    global define void readMonitorDB()
    {
	all_monitors = (list<map>)SCR::Read( .target.ycp, [ static_monitors, [] ]);
	readVendors();
	return;
    }


    /**
     * Read data from suggest file if available
     */
    global define boolean Read()
    {

        x11["window_manager"] = (string) eval (SCR::Read(
                    .sysconfig.displaymanager.DISPLAYMANAGER
                    ));

        x11["display_manager"] = (string) eval (SCR::Read(.sysconfig.windowmanager.DEFAULT_WM ));

        string suggestFile = "/var/cache/sax/files/suggest.ycp";
        if (SCR::Read(.target.size, suggestFile) == -1 )
            return false;

        // defaults
        x11["configure_x11"] = true;
        x11["enable_3d"] = false;

        list initResult = (list)SCR::Read ( .target.ycp,
                suggestFile
                );
        map allCards = initResult[0]:$[];
        map allViews = initResult[1]:$[];
        map currentView = allViews[0]:$[];
        map view = currentView["Desktop"]:$[];
        current_vendor = view["VendorName"]:"nil";
        current_model  = view["ModelName"]:"nil";
        x11["color_depth"] = view["ColorDepth"]:"nil";
        string moniHsync  = view["HorizSync"]:"nil";
        string moniVsync  = view["VertRefresh"]:"nil";
        string modes = "Modes:" + view["ColorDepth"]:"nil";
        if (moniHsync!=nil) {
            list h = splitstring(moniHsync, "-");
            display["min_hsync"] = tointeger(h[0]:"");
            display["max_hsync"] = tointeger(h[1]:"");
        }
        if (moniVsync!=nil) {
            list v = splitstring(moniVsync, "-");
            display["min_vsync"] = tointeger(v[0]:"");
            display["max_vsync"] = tointeger(v[1]:"");
        }
        x11["resolution"] = view[modes]:"nil";

    }

}


