ACC SHELL

Path : /usr/share/YaST2/include/samba-server/
File Upload :
Current File : //usr/share/YaST2/include/samba-server/dialogs-items.ycp

/**
 * File:	modules/dialogs-items.ycp
 * Package:	Configuration of samba-server
 * Summary:	Widgets used by SAMBA server configuration
 * Authors:	Jiri Srain <jsrain@suse.cz>
 *		Stanislav Visnovsky <visnov@suse.cz>
 *		Martin Lazar <mlazar@suse.cz>
 *
 * $Id: dialogs-items.ycp 50826 2008-09-08 15:52:46Z locilka $
 */

{

    textdomain "samba-server";

    import "Label";
    import "Popup";
    import "LogView";
    import "TablePopup";
    import "FileUtils";
    import "Report";
    
    import "SambaServer";
    import "SambaConfig";
    import "SambaBackend";
    
    include "samba-server/helps.ycp";
    include "samba-server/complex.ycp";

    define list ShareEditContents (map descr);
    define void ShareEditPopupInit (any opt_id, string opt_key);
    define void ShareEditPopupStore (any opt_id, string opt_key);
    define string ShareEditSummary (any opt_id, string opt_key);
    define boolean ShareEditEntryDelete (any opt_id, string opt_key);

    define list GlobalSettingsContents (map descr);
    define void GlobalSettingsPopupInit (any opt_id, string opt_key);
    define void GlobalSettingsPopupStore (any opt_id, string opt_key);
    define string GlobalSettingsSummary (any opt_id, string opt_key);
    define boolean GlobalSettingsEntryDelete (any opt_id, string opt_key);

    define list PassdbEditContents (map descr);
    define string PassdbEditOptionSummary (any opt_id, string opt_key);
    define any PassdbOptionMove (any opt_id, string opt_key, symbol direction);
    define string PassdbId2Key (map desc, any id);
    define boolean PassdbEntryDelete (any opt_id, string opt_key);
    define void PassdbEditOptionInit (any opt_id, string opt_key);
    define boolean PassdbEditOptionValidate (string key, map event_descr);
    define void PassdbEditOptionStore (any opt_id, string opt_key);

define void ShareInitFun(string key);
define symbol ShareHandleFun(string key, map event_descr);

define void initPassdb(string key);
define symbol handlePassdb(string key, map event_descr);
define void storePassdb(string key, map event);
define void AddPassdbBackend ();
define boolean ValidateLDAPURL(any opt_id, string opt_key, map event_descr);
define boolean ValidateSharePath(any opt_id, string opt_key, map event_descr);

include "samba-server/samba-options-local.ycp";
include "samba-server/samba-options-global.ycp"; 

////////////////////////////////////
map shareswidget =  TablePopup::CreateTableDescr (
	$[
	      "add_delete_buttons" : true,
	      "up_down_buttons" : false,
	      "unique_keys" : true
	],
	$[
	    "init" : ShareInitFun,
	    "handle" : ShareHandleFun,
	    "options" : local_option_widgets,
	    "ids" : ShareEditContents,
	    "fallback" : $[
		"init" : ShareEditPopupInit,
		"store" : ShareEditPopupStore,
		"summary" : ShareEditSummary,
	    ],
	    "option_delete" : ShareEditEntryDelete,
	    "add_items" : maplist( string key, map values, local_option_widgets, 
		``( key ) ),
	    "help" : HELPS["share_edit"]:"",
	]);

map globalsettingswidget =  TablePopup::CreateTableDescr (
	    $["add_delete_buttons" : true, 
	      "up_down_buttons" : false, 
	      "unique_keys" : true]
	  , $[
	    "options" : global_option_widgets,
	    "ids" : GlobalSettingsContents,
	    "fallback" : $[
		"init" : GlobalSettingsPopupInit,
		"store" : GlobalSettingsPopupStore,
		"summary" : GlobalSettingsSummary,
	    ],
	    "option_delete" : GlobalSettingsEntryDelete,
	    "add_items" : maplist( string key, map values, global_option_widgets, 
		``( key ) ),
	    "help" : HELPS["global_settings"]:"",
	]);

map passdboptions = $[
    "smbpasswd" : $[
        "table" : $[
	    // table entry description for smbpasswd-based SAM
           "label" : _("smbpasswd file"),
        ],
        "popup" : $[
         "widget" : `textentry,
        ],
    ],
    "ldapsam" : $[
        "table" : $[
	    // table entry description for LDAP-based SAM
           "label" : _("LDAP"),
        ],
        "popup" : $[
            "widget" : `textentry,
	    "validate_type" : `function,
	    "validate_function" : ValidateLDAPURL
        ],
    ],
    "tdbsam" : $[
        "table" : $[
	    // table entry description for TDB-based SAM
           "label" : _("TDB database"),
        ],
        "popup" : $[
         "widget" : `textentry,
        ],
    ],
// MySQL will not be offered, unknown backend
//    "mysql" : $[
//        "table" : $[
//	    // table entry description for MySQL-based SAM
//           "label" : _("MySQL database"),
//        ],
//        "popup" : $[
//         "widget" : `textentry,
//        ],
//    ],
];

map passdbwidget =  TablePopup::CreateTableDescr (
	$[
	      "add_delete_buttons" : true, 
	      "up_down_buttons" : false,
	      "unique_keys" : false
	],
	$[
	    "init" : initPassdb,
	    "handle" : handlePassdb,
	    "store" : storePassdb,
	    "ids" : PassdbEditContents,
	    "help" : HELPS["passdb_edit"]:"",
	    "options": passdboptions,
	    "add_items" : [ "smbpasswd", "ldapsam", "tdbsam" ],
	    "add_unlisted" : false,
	    "id2key": PassdbId2Key,
	    "option_delete" : PassdbEntryDelete,
	    "validate_type" : `function,
	    "validate_function" : PassdbEditOptionValidate,

	    // only one passdb backend is possible
	    // "option_move" : PassdbOptionMove,

	    "fallback" : $[
		"init"	  : PassdbEditOptionInit,
		"store"	  : PassdbEditOptionStore,
		"summary" : PassdbEditOptionSummary,
	    ],
	]);


/**
 * Map of widgets for CWM
 */
map xx_widgets = $[
	"share_edit" : shareswidget,
	"passdb_edit" : passdbwidget,
	"globalsettings" : globalsettingswidget,
];


/**
 * A share name do be editted in the Edit share dialog
 */
string shareToEdit = nil;

boolean ValidateLDAPURL(any opt_id, string opt_key, map event_descr) {

    import "URL";
    
    string url = (string) UI::QueryWidget ( `id (opt_key), `Value );

    if (regexpmatch (url, "^[ \t]+")) {
	// TRANSLATORS: popup error
	Report::Error (_("Optional value must not begin with a space character."));
	return false;
    }

    boolean quotes_used = false;

    // ldapsam:"ldap://ldap1.example.com ldap://ldap2.example.com"
    if (regexpmatch (url, "^\".*\"[ \t]*$")) {
	quotes_used = true;
	url = regexpsub (url, "^\"(.*)\"[ \t]*$", "\\1");
    }

    list <string> urls = splitstring (url, " \t");
    urls = filter (string url, urls, { return url != ""; });

    if (size(urls) > 1 && !quotes_used) {
	// TRANSLATORS: popup error
	Report::Error (_("Multiple optional values for one back-end must be quoted."));
	return false;
    }

    boolean ret = true;
    foreach (string url, urls, {
	if (ret != true) return;

	if (! URL::Check (url)) {
	    y2error ("Invalid url '%1'", url);
	    // TRANSLATORS: popup error, %1 is replaced with some URL
	    Popup::Error (sformat(_("The entered URL '%1' is invalid"), url));
	    ret = false;
	}
    
	map u = URL::Parse (url);
	if (u["scheme"]:nil != "ldap" && u["scheme"]:nil != "ldaps") {
	    y2error ("Invalid url '%1'", url);
	    // TRANSLATORS: popup error, %1 is replaced with some URL
	    Popup::Error (sformat(_("The entered URL '%1' is invalid"), url));
	    ret = false;
	}
    });
    
    return ret;
}

boolean SharePathWarning (string p)
{
    string text = nil;

    if ( p == "/" ) {
	text = root_warning;
    } else {
	if (warnings[p]:nil != nil) text = warnings[p]:nil;
    }
    
    if (text != nil) {
        return Popup::ContinueCancel (text);
    }
    
    return true;
}

boolean ValidateSharePath(any opt_id, string opt_key, map event_descr) {

    string p = (string)UI::QueryWidget (`id(opt_key), `Value);

    if (!SharePathWarning (p)) return false;
    if (!FileUtils::CheckAndCreatePath (p)) return false;
    
    return true;
}


define void ShareInitFun(string key) ``{
    TablePopup::TableInit(xx_widgets["share_edit"]:$[], key);
}

define symbol ShareHandleFun(string key, map event_descr) ``{
    return TablePopup::TableHandle(xx_widgets["share_edit"]:$[], key, event_descr);
}

// ************************************ default host table ********************

    /**
      * Function for getting contents of the default host table
      * @param descr map description map of the table
      * @return list of items for the table
      */
    define list ShareEditContents (map descr) ``{
	return SambaConfig::ShareKeys(shareToEdit);
    }
    
    /**
      * Delete function of the table
      * @param opt_id any option id of selected option
      * @param opt_key any option key of selected option
      * @return boolean true if was really deleted
      */
    define boolean ShareEditEntryDelete (any opt_id, string opt_key) ``{
	// message popup
	if (! Popup::YesNo (_("Delete the selected entry?")))
	    return false;
	SambaConfig::ShareSetStr(shareToEdit, opt_key, nil);
	return true;
    }

    /**
      * Fallback initialization function of a table entry / popup
      * @param opt_id any option id
      * @param opt_key any option key
      */
    define void ShareEditPopupInit (any opt_id, string opt_key) ``{
	if( opt_id != nil ) {
	    // not adding a new option
	    any val = UI::QueryWidget(`id (opt_key), `Value);
	    if (is(val, boolean)) {
		UI::ChangeWidget(`id (opt_key), `Value, SambaConfig::ShareGetTruth(shareToEdit, opt_key, nil));
	    } else {
		UI::ChangeWidget(`id (opt_key), `Value, SambaConfig::ShareGetStr(shareToEdit, opt_key, nil));
	    }
	}
	UI::SetFocus (`id (opt_key));
    }

    /**
      * Fallback store function of a table entry / popup
      * @param opt_id any option id
      * @param opt_key any option key
      */
    define void ShareEditPopupStore (any opt_id, string opt_key) ``{
	any val = UI::QueryWidget(`id (opt_key), `Value);
	if (is(val, boolean)) {
	    SambaConfig::ShareSetTruth(shareToEdit, opt_key, (boolean)val);
	} else {
	    SambaConfig::ShareSetStr(shareToEdit, opt_key, (string)val);
	}
    }

    /**
      * Fallback summary function of a table entry / popup
      * @param opt_id any option id
      * @param opt_key any option key
      * @return	value of the option
      */
    define string ShareEditSummary (any opt_id, string opt_key) ``{
	return SambaConfig::ShareGetStr(shareToEdit, opt_key, "");
    }


/********************************************************************************************/

    /**
      * Function for getting contents of the default host table
      * @param descr map description map of the table
      * @return list of items for the table
      */
    define list GlobalSettingsContents (map descr) ``{
	return SambaConfig::GlobalKeys();
    }
    
    /**
      * Delete function of the table
      * @param opt_id any option id of selected option
      * @param opt_key any option key of selected option
      * @return boolean true if was really deleted
      */
    define boolean GlobalSettingsEntryDelete (any opt_id, string opt_key) ``{
	// message popup
	if (! Popup::YesNo (_("Delete the selected entry?")))
	    return false;
	SambaConfig::GlobalSetStr(opt_key, nil);
	return true;
    }

    /**
      * Fallback initialization function of a table entry / popup
      * @param opt_id any option id
      * @param opt_key any option key
      */
    define void GlobalSettingsPopupInit (any opt_id, string opt_key) ``{
	if( opt_id != nil ) {
	    any val = UI::QueryWidget(`id (opt_key), `Value);
	    if (is(val, boolean)) {
		UI::ChangeWidget(`id (opt_key), `Value, SambaConfig::GlobalGetTruth(opt_key, nil));
	    } else {
		UI::ChangeWidget(`id (opt_key), `Value, SambaConfig::GlobalGetStr(opt_key, nil));
	    }
	}
	UI::SetFocus (`id (opt_key));
    }

    /**
      * Fallback store function of a table entry / popup
      * @param opt_id any option id
      * @param opt_key any option key
      */
    define void GlobalSettingsPopupStore (any opt_id, string opt_key) ``{
	any val = UI::QueryWidget(`id (opt_key), `Value);
	if (is(val, boolean)) {
	    SambaConfig::GlobalSetTruth(opt_key, (boolean)val);
	} else {
	    SambaConfig::GlobalSetStr(opt_key, (string)val);
	}
    }

    /**
      * Fallback summary function of a table entry / popup
      * @param opt_id any option id
      * @param opt_key any option key
      * @return	value of the option
      */
    define string GlobalSettingsSummary (any opt_id, string opt_key) ``{
	return SambaConfig::GlobalGetStr(opt_key, "");
    }

/************************************* passdb list table ************************************/

    integer max_id = 0;
    map<string, string> passdb_backends = nil;
    list<string> passdb_order = [];

    boolean SomeBackendsAreInUse () {
	return size (passdb_backends) > 0;
    }

    void RedrawPassdbEditButtons () {
	if (UI::WidgetExists (`id (`_tp_add))) {
	    UI::ChangeWidget (`id (`_tp_add), `Enabled, (! SomeBackendsAreInUse()));
	}
    }

    define list PassdbEditContents (map descr) ``{
	if (passdb_backends != nil)
	{
	    // we are already initialized
	    return passdb_order;
	}
	else 
	{
	    list<string> l = SambaBackend::GetPassdbBackends();
	    list<string> ids = [];
	    max_id = -1;
	    passdb_backends = (map<string, string>)listmap ( string backend, l, ``{ 
		max_id = max_id + 1;
		string s = sformat( "%1", max_id );
		ids = add( ids, s );
		return $[ s: backend ];
	    });
	    max_id = max_id + 1;
	    passdb_order = ids;
	    return ids;
	}
    }

    define boolean PassdbEditOptionValidate (string key, map event_descr) {
	if (! SomeBackendsAreInUse()) {
	    Report::Error (_("At least one back-end must be specified."));

	    return false;
	}
	
	return true;
    }
    
    define void PassdbEditOptionInit (any opt_id, string opt_key) ``{
	if( opt_id != nil ) {
	    // not adding a new option
	    UI::ChangeWidget (`id (opt_key), `Value, SambaBackend::GetLocation(passdb_backends[(string)opt_id]:""));
	}
	UI::SetFocus (`id (opt_key));

	RedrawPassdbEditButtons();
    }

    define void PassdbEditOptionStore (any opt_id, string opt_key) ``{
	string details = (string) UI::QueryWidget (`id (opt_key), `Value);
	// TODO: handle empty details
	
	if (size(details) != 0 ) {
	    details = ":"+details;
	} else {
	    details = "";
	}
	
	if ( opt_id != nil )
	{
	    // update
	    // take the SAM type from the old data and append the new details
	    passdb_backends[ (string)opt_id ] = SambaBackend::GetName(passdb_backends[ (string)opt_id ]:nil) + details;
	}
	else
	{
	    // insert new
	    string id = sformat ("%1", max_id);
	    max_id = max_id+1;
	    passdb_backends = (map<string, string>)add ( passdb_backends, id, (string)opt_key+details );
	    passdb_order = (list<string>) add (passdb_order, id);
	}
    }

    define string PassdbEditOptionSummary (any opt_id, string opt_key) ``{
	string option = passdb_backends [(string)opt_id]:"";
	return SambaBackend::GetLocation(option);
    }
    
    define string PassdbId2Key (map desc, any id)
    {
	string s_id = passdb_backends [(string)id]:"";
	return SambaBackend::GetName(s_id);
    }

    define boolean PassdbEntryDelete (any opt_id, string opt_key) ``{
    
//	if (size (passdb_backends) == 1)
//	{
//	    // error message if user tries to delete the last passdb backend
//	    Report::Error (_("At least one back-end must be specified.
//
//The back-end cannot be deleted.
//"));
//	    return false;
//	}
	
	// message popup
	if (! Popup::YesNo (_("Delete the selected back-end?")))
	    return false;
	    
	string id = (string) opt_id;
	passdb_backends = (map<string, string>)filter (string k, string v, passdb_backends, ``(k != id));
	passdb_order = (list<string>)filter (string k, passdb_order, ``(k != id));

	RedrawPassdbEditButtons();

	return true;
    }
    
    define void AddPassdbBackend () {
	UI::OpenDialog (
	    `VBox (
		// translators: frame text when adding a passdb backend
		`Frame (_("Back-End Type"),
		`RadioButtonGroup ( `id (`types),  
		`VBox(
		    // translators: passdb backend radio button
		    `Left (`RadioButton ( `id("smbpasswd"), `opt (`notify),_("smbpasswd File") ) ),
		    // translators: passdb backend radio button
		    `Left (`RadioButton ( `id("ldapsam"), `opt (`notify),_("LDAP") ) ),
		    // translators: passdb backend radio button
		    `Left (`RadioButton ( `id("tdbsam"), `opt (`notify),_("TDB Database") ) )
// Unknown passdb backend
//		    // translators: passdb backend radio button
//		    `Left (`RadioButton ( `id("mysql"), `opt (`notify),_("MySQL Database") ) )
		))),
		// translators: textentry label to enter details for the selected passdb backend
		`TextEntry ( `id("details"), _("&Details") ),
		`HBox (
		    `PushButton (`id("add"), `opt(`default), Label::AddButton ()),
		    `PushButton (`id("cancel"), Label::CancelButton ())
		)
	    )
	);
	
	do {
	    any ret = UI::UserInput ();
	    if ( contains ( (list)[ "smbpasswd", "ldapsam", "tdbsam", "mysql" ], ret ) )
	    {
		// prefill a hint, if details empty
		string details = (string) UI::QueryWidget(`id("details"), `Value );
		if (size(details) == 0)
		{
		    if ( ret == "ldapsam" )
		    {
			import "Ldap";
			// ask LDAP client for the value
			string server = Ldap::server;
			
			// ensure to take only the first one
			list<string> servers = splitstring (server, " ");
			UI::ChangeWidget(`id("details"), `Value, "ldap://" + servers[0]:"localhost" );
		    }
		}

		continue;
	    } else
	
	    if ( ret == "add" )
	    {
		// check: all types except smbpasswd must get detailed info
		string type = (string) UI::QueryWidget(`id(`types), `CurrentButton);
		string details = (string) UI::QueryWidget(`id("details"), `Value);
		if (size(details) != 0 ) {
		    details = ":"+details;
		} else {
		    details = "";
		}
	
		if (type == "mysql" && size (details) == 0)
		{
		    // translators: error message, if the MySQL backend 
		    // is selected, but no details are entered
		    Popup::Error ( _("An identifier must be provided
in details 
for the MySQL passdb back-end.

Consult the Samba HOWTO collection for
further information.
") );
		    continue;
		}
		
		// validate LDAP
		else if (type == "ldapsam" && size(details) > 0)
		{
		    // fill in empty params to keep interpreter happy
		    if (!ValidateLDAPURL(nil, "details", nil)) {
			continue;
		    }
		}
		
		// add the value		
		string id = sformat ("%1", max_id);
		max_id = max_id+1;
		passdb_backends = (map<string, string>)add ( passdb_backends, id, type+details );
		passdb_order = (list<string>) add (passdb_order, id);
	    }
	    break;
	} while (true);
	
	UI::CloseDialog ();
    }
    
    define any PassdbOptionMove (any opt_id, string opt_key, symbol direction) {
	string id = (string)opt_id;

	passdb_order = sort (string left, string right, passdb_order, ``{ boolean res = 
	    direction == `down ?
	    (left == id ? false : true)
	    :
	    (right == id ? false : true);
	    return res;
	});
	initPassdb (opt_key);
	return opt_id;
    }

    define void initPassdb(string key) ``{
	TablePopup::TableInit(xx_widgets["passdb_edit"]:$[], key);
	RedrawPassdbEditButtons();
    }

    define symbol handlePassdb(string key, map event_descr) ``{
	if ( event_descr["ID"]:nil == `_tp_add )
	{
	    AddPassdbBackend ();
	    TablePopup::TableInit(xx_widgets["passdb_edit"]:$[], key);
	    RedrawPassdbEditButtons();
	    // continue
	    return nil;
	}

	if ( event_descr["ID"]:nil == `back )
	{
	    passdb_backends = nil;
	    return nil;
	}
    
	symbol ret = TablePopup::TableHandle(xx_widgets["passdb_edit"]:$[], key, event_descr);
	RedrawPassdbEditButtons();
	return ret;
    }

    define void storePassdb(string key, map event) ``{
	list<string> res = maplist (string id, passdb_order, ``{
	    return passdb_backends[id]:"";
	});
	SambaBackend::SetPassdbBackends(res);
	passdb_backends = nil; // require a new initialization
    }


/* EOF */
}


ACC SHELL 2018