/**
 * File:	modules/Autologin.ycp
 * Package:	yast2
 * Summary:	Autologin read/write routines
 * Author:	Jiri Suchomel <jsuchome@suse.cz>
 *
 * $Id: Autologin.ycp 20828 2005-01-21 13:51:01Z jsuchome $
 */

{

module "Autologin";
textdomain "pam";

import "Package";
import "Popup";
import "ProductFeatures";

/**
 * User to log in automaticaly
 */
global string user = "";

/**
 * Login without passwords?
 */
global boolean pw_less = false;

/**
 * Is autologin feature available?
 */
global boolean available = true;

/**
 * Is autogion used? Usualy true when user is not empty, but for the first
 * time (during installation), this can be true by default although user is ""
 * (depends on the control file)
 */
global boolean used = ProductFeatures::enable_autologin;

/**
 * Autologin settings modified?
 */
global boolean modified = false;

/**
 * Read autologin settings
 * @return used?
 */
global define boolean Read () ``{

    if (SCR::Read (.target.size, "/etc/sysconfig/displaymanager") == -1 ||
	SCR::Read (.sysconfig.displaymanager.DISPLAYMANAGER) != "kdm" ||
	!Package::Installed ("kdebase3-kdm"))
    {
	available = false;
	user = "";
    }
    else
    {
	user = (string) SCR::Read (.sysconfig.displaymanager.DISPLAYMANAGER_AUTOLOGIN);
	pw_less = (string) SCR::Read (.sysconfig.displaymanager.DISPLAYMANAGER_PASSWORD_LESS_LOGIN) == "yes";

	if (user == nil || user == "")
	    user = "";
    }
    used = (user != "");
    return used;
}


/**
 * Write autologin settings
 * @param write_only when true, suseconfig script will not be run
 * @return written anything?
 */
global define boolean Write (boolean write_only) ``{

    if (!available || !modified)
	return false;

    SCR::Write (.sysconfig.displaymanager.DISPLAYMANAGER_AUTOLOGIN, user);
    SCR::Write (.sysconfig.displaymanager.DISPLAYMANAGER_PASSWORD_LESS_LOGIN,
	pw_less ? "yes" : "no" );
    SCR::Write(.sysconfig.displaymanager, nil);

    if (!write_only)
	SCR::Execute (.target.bash, "/sbin/SuSEconfig --module kdm3");

    modified = false;
    return true;
}

/**
 * Disable autologin
 */
global define void Disable () ``{

    user	= "";
    pw_less	= false;
    used	= false;
    modified	= true;
}

/**
 * Wrapper for setting the 'used' variable
 */
global define void Use (boolean use) ``{
    if (used != use)
    {
	used = use;
	modified = true;
    }
}

/**
 * Disable autologin and write it (used probably for automatic
 * disabling without asking)
 * @param write_only when true, suseconfig script will not be run
 * @return written anything?
 */
global define boolean DisableAndWrite (boolean write_only) ``{

    Disable ();
    return Write (write_only);
}

/**
 * Ask if autologin should be disabled (and disable it in such case)
 * @param new The reason for disabling autologin (e.g. new set of users)
 * @return Is autologin used?
 */
global define boolean AskForDisabling (string new) ``{

    // popup text (%1 is user name, %2 is additional info,
    // like "Now LDAP was enabled")
    string question = sformat (_("The automatic login feature is enabled for user %1.
%2
Disable automatic login?"), user, new);

    if (used && Popup::YesNo (question))
	Disable ();
    return used;
}

/* EOF */
}
