ACC SHELL

Path : /usr/share/YaST2/modules/
File Upload :
Current File : //usr/share/YaST2/modules/DontShowAgain.ycp

/**
 * File:    modules/DontShowAgain.ycp
 * Authors: Lukas Ocilka <locilka@suse.cz>
 * Summary: Handling "Don Not Show This Dialog Again"
 *
 * $Id: DontShowAgain.ycp 11111 2006-05-29 12:27:15Z locilka $
 */
{
    module "DontShowAgain";
    textdomain "base";

    import "Directory";
    import "FileUtils";

    /**
     * Module for that stores and returns the information for
     * "Don't Show This Dialog/Question Again"
     */
    
    /* File with the current configuration */
    string conf_file = Directory::vardir + "/dont_show_again.conf";

    /**
     * Current configuration map
     *
     * @struct $[
     *     // question type
     *     "inst-source" : $[
     *         // question identification (MD5sum of the question in the future?)
     *         "-question-ident-" : $[
     *             // url of the file or directory
     *             "ftp://abc.xyz/rtf" : $[
     *                 // show the dialog again
     *                 "show_again" : false,
     *                 // additional question return
     *                 "return" : true,
     *             ]
     *         ]
     *     ]
     * ]
     */
    map <string, map <string, map <string, any> > > current_configuration = $[];

    /* Configuration has already been read */
    global boolean already_read = false;

    /**
     * Function that reads the current configuration if it hasn't been
     * read already. It must be called before every Get or Set command.
     */
    void LazyLoadCurrentConf () {
	if (! already_read) {
	    if (FileUtils::Exists(conf_file) && FileUtils::IsFile(conf_file)) {
		y2milestone("Reading %1 file", conf_file);
		// Read and evaluate the current configuration
		map <string, map <string, map <string, any> > >  read_conf = (map <string, map <string, map <string, any> > >)
		    SCR::Read(.target.ycp, conf_file);
		if (read_conf != nil) {
		    current_configuration = read_conf;
		}
	    } else {
		y2milestone("Configuration file %1 doesn't exist, there's no current configuration.",
		    conf_file
		);
	    }

	    // Configuration mustn't be read again
	    already_read = true;
	}
    }

    /**
     * Saves  the current configuration into the configuration file
     */
    boolean SaveCurrentConfiguration () {
	LazyLoadCurrentConf();

	// Removing nil entries from the configuration
	map <string, map <string, map <string, any> > > new_configuration = $[];

	foreach (string dont_show_type, map <string, map <string, any> > records, current_configuration, {
	    // Defined and known type
	    if (dont_show_type == "inst-source") {
		// Every popup type
		foreach (string popup_type, map <string, any> one_record, records, {
		    // Every URL
		    foreach (string url, any record_options, one_record, {
			// Record mustn't be nil or empty to be reused
			if (record_options != nil && record_options != $[]) {
			    // Creating map from the base
			    if (new_configuration[dont_show_type]:nil == nil)
				new_configuration[dont_show_type] = $[];
			    if (new_configuration[dont_show_type, popup_type]:nil == nil)
				new_configuration[dont_show_type, popup_type] = $[];

			    new_configuration[dont_show_type, popup_type, url] = record_options;
			}
		    });
		});
	    // Unknown type
	    } else {
		new_configuration[dont_show_type] = records;
	    }
	});

	current_configuration = new_configuration;

	return SCR::Write(.target.ycp, conf_file, current_configuration);
    }

    /**
     * Returns whether the question should be shown again
     *
     * @param map <string, string> of params
     * @see current_configuration
     * @return boolean it should be shown
     */
    global boolean GetShowQuestionAgain (map <string, string> params) {
	LazyLoadCurrentConf();
	string q_type = params["q_type"]:nil;

	// <--- repositories --->
	/*
	 * Parameters, $[
	 *     "q_type"  : "inst-source",             // mandatory
	 *     "q_ident" : "Question Identification", // mandatory
	 *     "q_url" : "URL"                        // optional
	 * ];
	 */
	if (q_type == "inst-source") {
	    string q_ident = params["q_ident"]:nil;
	    string q_url   = params["q_url"]:nil;

	    if (q_ident == nil) {
		y2error("'q_ident' is a mandatory parameter");
		return nil;
	    }
	    
	    if (current_configuration[q_type]:nil == nil
		|| current_configuration[q_type, q_ident]:nil == nil
		|| current_configuration[q_type, q_ident, q_url]:nil == nil
		|| current_configuration[q_type, q_ident, q_url, "show_again"]:nil == nil) {
		return nil;
	    }
	    
	    return (boolean) current_configuration[q_type, q_ident, q_url, "show_again"]:nil;
	// <--- repositories --->

	// Add another types here...
	} else {
	    y2error("'%1' is an unknown type", q_type);
	    return nil;
	}
    }

    /**
     * Sets and stores whether the question should be shown again.
     * If it should be, the result is not stored since the 'show again'
     * is the default value.
     *
     * @param map <string, string> of params
     * @see current_configuration
     * @param boolean show again
     * @return boolean if success
     */
    global boolean SetShowQuestionAgain (map <string, string> params, boolean new_value) {
	LazyLoadCurrentConf();
	string q_type = params["q_type"]:nil;
	// Always set to 'true' if the configuration is changed
	boolean conf_changed = false;

	// <--- repositories --->
	/*
	 * Parameters, $[
	 *     "q_type"  : "inst-source",             // mandatory
	 *     "q_ident" : "Question Identification", // mandatory
	 *     "q_url" : "URL"                        // optional
	 * ];
	 */
	if (q_type == "inst-source") {
	    string q_ident = params["q_ident"]:nil;
	    string q_url   = params["q_url"]:nil;

	    if (q_ident == nil) {
		y2error("'q_ident' is a mandatory parameter");
		return nil;
	    }
	    
	    // building the configuration map
	    if (current_configuration[q_type]:nil == nil) {
		current_configuration[q_type] = $[];
	    }
	    if (current_configuration[q_type, q_ident]:nil == nil) {
		current_configuration[q_type, q_ident] = $[];
	    }
	    if (current_configuration[q_type, q_ident, q_url]:nil == nil) {
		current_configuration[q_type, q_ident, q_url] = $[];
	    }
	    
	    // save the new value into the configuration
	    conf_changed = true;
	    current_configuration[q_type, q_ident, q_url, "show_again"] = new_value;
	// <--- repositories --->

	// Add another types here...
	} else {
	    y2error("'%1' is an unknown type", q_type);
	    return nil;
	}

	if (conf_changed) return SaveCurrentConfiguration();
	else return nil;
    }

    /**
     * Return the default return value for question that should not
     * be shown again
     *
     * @param map <string, string> of params
     * @see current_configuration
     * @return any default return value
     */
    global any GetDefaultReturn (map <string, string> params) {
	LazyLoadCurrentConf();
	string q_type = params["q_type"]:nil;
	
	// <--- repositories --->
	/*
	 * Parameters, $[
	 *     "q_type"  : "inst-source",             // mandatory
	 *     "q_ident" : "Question Identification", // mandatory
	 *     "q_url" : "URL"                        // optional
	 * ];
	 */
	// <--- repositories --->
	if (q_type == "inst-source") {
	    string q_ident = params["q_ident"]:nil;
	    string q_url   = params["q_url"]:nil;

	    if (current_configuration[q_type]:nil == nil
		|| current_configuration[q_type, q_ident]:nil == nil
		|| current_configuration[q_type, q_ident, q_url]:nil == nil
		|| current_configuration[q_type, q_ident, q_url, "return"]:nil == nil) {
		return nil;
	    }
	    
	    return current_configuration[q_type, q_ident, q_url, "return"]:nil;

	// Add another types here...
	} else {
	    y2error("'%1' is an unknown type", q_type);
	    return nil;
	}
	// <--- repositories --->
    }

    /**
     * Sets the default return value for the question that should not be shown
     *
     * @param map <string, string> of params
     * @param any default return
     * @see current_configuration
     * @return boolean if success
     */
    global boolean SetDefaultReturn (map <string, string> params, any default_return) {
	LazyLoadCurrentConf();
	string q_type = params["q_type"]:nil;
	// Always set to 'true' if the configuration is changed
	boolean conf_changed = false;

	// <--- repositories --->
	/*
	 * Parameters, $[
	 *     "q_type"  : "inst-source",             // mandatory
	 *     "q_ident" : "Question Identification", // mandatory
	 *     "q_url" : "URL"                        // optional
	 * ];
	 */
	if (q_type == "inst-source") {
	    string q_ident = params["q_ident"]:nil;
	    string q_url   = params["q_url"]:nil;

	    if (q_ident == nil) {
		y2error("'q_ident' is a mandatory parameter");
		return nil;
	    }
	    
	    // building the configuration map
	    if (current_configuration[q_type]:nil == nil) {
		current_configuration[q_type] = $[];
	    }
	    if (current_configuration[q_type, q_ident]:nil == nil) {
		current_configuration[q_type, q_ident] = $[];
	    }
	    if (current_configuration[q_type, q_ident, q_url]:nil == nil) {
		current_configuration[q_type, q_ident, q_url] = $[];
	    }
	    
	    // save the new value into the configuration
	    conf_changed = true;
	    current_configuration[q_type, q_ident, q_url, "return"] = default_return;
	// <--- repositories --->

	// Add another types here...
	} else {
	    y2error("'%1' is an unknown type", q_type);
	    return nil;
	}

	if (conf_changed) return SaveCurrentConfiguration();
	else return nil;
    }

    /**
     * Returns the current configuration map
     *
     * @return map <string, map <string, map <string, any> > > with the current configuration
     * @see current_configuration
     */
    global map <string, map <string, map <string, any> > > GetCurrentConfigurationMap () {
	LazyLoadCurrentConf();
	return current_configuration;
    }

    /**
     * Removes one entry defined with map params
     *
     * @param map <string, string> of params
     * @see current_configuration
     * @return boolean if success
     */
    global boolean RemoveShowQuestionAgain (map <string, string> params) {
	LazyLoadCurrentConf();
	string q_type = params["q_type"]:nil;

	if (q_type == "inst-source") {
	    string q_ident = params["q_ident"]:nil;
	    string q_url   = params["q_url"]:nil;

	    if (current_configuration[q_type]:nil != nil
		&& current_configuration[q_type, q_ident]:nil != nil
		&& current_configuration[q_type, q_ident, q_url]:nil != nil) {
		
		current_configuration[q_type, q_ident, q_url] = nil;
		SaveCurrentConfiguration();
	    }
	    
	    return (current_configuration[q_type]:nil != nil
		&& current_configuration[q_type, q_ident]:nil != nil
		&& current_configuration[q_type, q_ident, q_url]:nil != nil);
	} else {
	    y2error("'%1' is an unknown type", q_type);
	    return false;
	}
    }
}

ACC SHELL 2018