ACC SHELL

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

/**
 * File:	modules/ProductFetures.ycp
 * Package:	installation
 * Summary:	Product features
 * Authors:	Anas Nashif <nashif@suse.de>
 *              Jiri Srain <jsrain@suse.cz>
 *
 * $Id: ProductFeatures.ycp 60109 2009-12-15 10:50:54Z aschnell $
 */

{
module "ProductFeatures";
textdomain "base";

import "Misc";
import "Mode";
import "Stage";

/**
 * Map of all features
 * See defaults map below for sample contents
 */
map<string,map<string,any> > features = nil;

/**
 * Default values for features
 * two-level map, section_name -> [ feature -> value ]
 */
map<string,map<string,any> > defaults = $[
    "globals" : $[
	"incomplete_translation_treshold" : "95",
	"ui_mode" : "expert",
	"enable_autologin" : true,
	"language" : "",
	"skip_language_dialog" : false,
	"keyboard" : "",
	"runlevel" : "",
	"timezone" : "",
	"fam_local_only" : "never",
	"enable_firewall" : true,
	"firewall_enable_ssh" : false,
	"additional_kernel_parameters" : "",
	"flags" : [],
	"run_you" : true,
	"relnotesurl" : "",
	"vendor_url" : "",
	"enable_clone" : false,
	// FATE #304865
	"base_product_license_directory" : "/etc/YaST2/licenses/base/",
    ],
    "partitioning" : $[
	"use_flexible_partitioning" : false,
	"flexible_partitioning" : $[],
	"vm_keep_unpartitioned_region" : false,
    ],
    "software" : $[
	"software_proposal" : "selection",
	"selection_type" : `auto,
	"delete_old_packages" : true,
	"only_update_installed" : false,
	"packages_transmogrify" : "",
	"base_selection" : "",
	"packages" : [],
	"kernel_packages" : [],
	"addon_selections" : [],
	"inform_about_suboptimal_distribution" : false,
    ],
    "network" : $[
	"force_static_ip" : false,
    ],
];

// function pre-declarations

global string GetStringFeature (string section, string feature);


/**
 * Initialize default values of features
 * @param force boolean drop all settings which were set before
 */
void InitFeatures (boolean force) {
    if (! (force || features == nil))
	return;
    features = defaults;
}

/**
 * Set a feature section
 * Default values will be used where value not defined
 * @stable
 * @param section name string the name of the section
 * @param section_map a map containing data of the section
 */
global void SetSection(string section_name, map<string,any> section_map) {
    InitFeatures (false);
    y2debug("Setting section: %1", section_name);
    section_map = (map<string,any>)
	union (defaults[section_name]:$[], section_map);
    features[section_name] = section_map;
}

/**
 * Get a complete section for evaluation
 * @stable
 * @param section_name string name of the section
 * @return a map key->value, options in the section
 */
global map<string,any> GetSection(string section_name) {
    InitFeatures (false);
    return features[section_name]:$[];
}

/**
 * Save product features
 * @stable
 */
global void Save() {
    InitFeatures (false);
    if (Mode::update()) // in case of update old file has different format
	SCR::Execute (.target.bash, "test -f /etc/YaST2/ProductFeatures && /bin/rm /etc/YaST2/ProductFeatures");
    foreach (string group, map<string,any> options, features, {
	foreach (string key, any value, options, {
	    if (is (value, map) || is (value, list) || is (value, symbol))
		y2debug ("Skipping option %1", key);
	    else
	    {
		string strval = GetStringFeature (group, key);
		SCR::Write (.product.features.value + group + key, strval);
	    }
	});
    });
    SCR::Write (.product.features, nil );     // flush
}

/**
 * Restore product features in running system
 * @stable
 */
global void Restore() {
    InitFeatures (true);
    list<string> groups = (list<string>)SCR::Dir (.product.features.section);
    foreach (string group, groups, {
	features[group] = features[group]:$[];
	list<string> values = (list<string>)SCR::Dir (.product.features.value + group);
	foreach (string v, values, {
	    features[group, v] = SCR::Read (.product.features.value + group + v);
	});
    });
}

/**
 * Initialize the features structure if needed
 * @stable
 * Either read from /etc/YaST2/ProductFeatures or set default values
 */
global void InitIfNeeded () {
    if (features != nil)
	return;
    if (Stage::normal () || Stage::firstboot())
	Restore ();
    else
	InitFeatures (false);
}

/**
 * Get value of a feature
 * @stable
 * @param section string section of the feature
 * @param features string feature name
 * @return any the feature value
 */
global any GetFeature (string section, string feature) {
    InitIfNeeded ();
    any ret = features[section, feature]:nil;
    if (ret == nil)
	ret = "";
    return ret;
}

/**
 * Get value of a feature
 * @stable
 * @param section string section of the feature
 * @param features string feature name
 * @return string the feature value
 */
global string GetStringFeature (string section, string feature) {
    any value = GetFeature (section, feature);
    if (value == nil)
	return nil;
    else if (is (value, string))
	return (string)value;
    else if (is (value, boolean))
	return (boolean)value ? "yes" : "no";
    else
	return sformat ("%1", value);
}

/**
 * Get value of a feature
 * @stable
 * @param section string section of the feature
 * @param features string feature name
 * @return boolean the feature value
 */
global boolean GetBooleanFeature (string section, string feature) {
    any value = GetFeature (section, feature);
    if (value == nil)
	return nil;
    else if (is (value, boolean))
	return (boolean)value;
    else if (is (value, string) && tolower ((string)value) == "yes")
	return true;
    else
	return false;
}

/**
 * Get value of a feature
 * @stable
 * @param section string section of the feature
 * @param features string feature name
 * @return integer the feature value
 */
global integer GetIntegerFeature (string section, string feature) {
    any value = GetFeature (section, feature);
    if (value == nil)
	return nil; 
    else if (is (value, integer))
	return (integer) value;
    else if (is (value, string))
	return tointeger((string) value);
    else
	return nil;
}

/**
 * Set value of a feature
 * @stable
 * @param section string section of the feature
 * @param features string feature name
 * @param value any the feature value
 */
global void SetFeature (string section, string feature, any value) {
    InitIfNeeded ();
    if (! haskey (features, section))
	features[section] = $[];
    features[section, feature] = value;
}

/**
 * Set value of a feature
 * @stable
 * @param section string section of the feature
 * @param features string feature name
 * @param value string the feature value
 */
global void SetStringFeature (string section, string feature, string value) {
    SetFeature (section, feature, value);
}

/**
 * Set value of a feature
 * @stable
 * @param section string section of the feature
 * @param features string feature name
 * @param value boolean the feature value
 */
global void SetBooleanFeature (string section, string feature, boolean value) {
    SetFeature (section, feature, value);
}

/**
 * Set value of a feature
 * @stable
 * @param section string section of the feature
 * @param features string feature name
 * @param value integer the feature value
 */
global void SetIntegerFeature (string section, string feature, integer value) {
    SetFeature (section, feature, value);
}

/**
 * Exports the current set of ProductFeatures
 *
 * @return map <string, map <string, any> > features
 */
global map <string, map <string, any> > Export () {
    return features;
}

/**
 * Imports product features
 *
 * @param map <string, map <string, any> > features
 */
global void Import (map <string, map <string, any> > import_settings) {
    features = import_settings;
}

/* EOF */
}

ACC SHELL 2018