ACC SHELL

Path : /var/lib/ntp/proc/self/root/usr/share/doc/licenses/md5/
File Upload :
Current File : //var/lib/ntp/proc/self/root/usr/share/doc/licenses/md5/8ee7e84f65adfb4274727d7c0fd71dd2

/**
 * File:	ProductLicense.ycp
 *
 * Module:	ProductLicense
 *
 * Summary:	Provide access / dialog for product license
 *
 * Author:	Jiri Srain <jsrain@suse.cz>
 *
 */
{

module "ProductLicense";

import "Directory";
import "InstShowInfo";
import "Language";
import "Popup";
import "Report";
import "Stage";
import "Wizard";
import "Mode";
import "FileUtils";
import "ProductFeatures";

textdomain "installation";

/**
 * Displays License with Help and ( ) Yes / ( ) No radio buttons
 * @param string file with the license
 */
void DisplayLicenseDialog (list<string> languages, boolean back) {
    string license_text = "";
    map display = UI::GetDisplayInfo();
    integer space = display["TextMode"]:true ? 1 : 3;
    term rt = `RichText(`id( `welcome_text), "");

    // dialog caption
    string caption = _("License Agreement");

    // bug #204791, no more "languages.ycp" client
    map<string,list> lang_names_orig = Language::GetLanguagesMap (false);
    if (lang_names_orig == nil) {
	y2error("Wrong definition of languages");
	lang_names_orig = $[];
    }

    // $[ "en" : "English", "de" : "Deutsch" ]
    map<string,string> lang_names = mapmap (string code, list descr,
	lang_names_orig,
    {
	return $[ code : descr[4]:"" ];
    });

    /* for the default fallback */

    // language name
    lang_names[""] = _("English");
    // language name
    lang_names["en"] = _("English");

    list<list<string> > lang_pairs = maplist (string l, languages, {
	string name = lang_names[l]:"";
	if (name == "")
	{
	    string l_short = substring (l, 0, 2);
	    foreach (string k, string v, lang_names, {
		if (substring (k, 0, 2) == l_short)
		{
		    name = v;
		    return true;
		}
		return false;
	    });
	}
	return [ l, name ];
    });

    // filter-out languages that don't have any name
    lang_pairs = filter (list<string> lang_pair, lang_pairs, {
	if (lang_pair[1]:"" == "") {
	    y2warning("Unknown license language '%1', filtering out...", lang_pair);
	    return false;
	} else {
	    return true;
	}
    });

    lang_pairs = sort (list<string> l1, list<string> l2, lang_pairs, {
	return l1[1]:"" < l2[1]:"";
    });
    list langs = maplist (list<string> descr, lang_pairs, {
	return `item (`id (descr[0]:""), descr[1]:"");
    });

    term contents = `VBox (
	`VSpacing (space),
	`HBox (
	    `HSpacing (2*space),
	    `VBox (
		// combo box
		`Left (`ComboBox (`id (`lang), `opt (`notify), _("&Language"),
		    langs)),
		`ReplacePoint (`id (`license_rp), rt)
	    ),
	    `HSpacing (2*space)
	),
	`VSpacing (2),
	`RadioButtonGroup(`id(`eula),
	    `HBox(
		`HSpacing (2*space),
		`VBox(
		    `Left(`RadioButton(`id(`yes),
			// radio button
			_("&Yes, I Agree to the License Agreement")
		    )),
		    `Left(`RadioButton(`id(`no),
			// radio button
			_("N&o, I Do Not Agree")
		    ))
		),
		`HSpacing (2*space)
	    )
	),
	`VSpacing(2)
    );

    // help text
    string help = _("<p>Read the license agreement carefully and select
one of the available options. If you do not agree to the license agreement,
the configuration will be aborted.</p>
");

    Wizard::SetContents(caption, contents, help,
	back, true);
    // bugzilla #203543
    if (size(langs) <= 1) {
	UI::ChangeWidget (`id(`lang), `Enabled, false);
    }

    Wizard::SetTitleIcon ("yast-license");
    Wizard::SetFocusToNextButton();
}



/**
 * Removes the temporary directory for licenses
 * @param string temporary directory path
 */
void CleanUpLicense (string tmpdir) {
    if (tmpdir != nil)
	SCR::Execute (.target.bash_output, sformat("rm -rf '%1'", tmpdir));
}

/**
 * Get all files with license existing in specified directory
 * @param dir string directory to look into
 * @param patterns a list of patterns for the files, regular expressions
 *   with %1 for the language
 * @return a map $[ lang_code : filename ]
 */
map<string,string> LicenseFiles (string dir, list<string> patterns) {
    map<string,string> ret = $[];
    list<string> files = (list<string>)SCR::Read (.target.dir, dir);
    y2milestone ("All files in license directory: %1", files);

    foreach (string p, patterns, {
	if (! issubstring (p, "%"))
	{
	    foreach (string file, files, {
		if (file == p)
		{
		    ret[""] = dir + "/" + file;
		}
	    });
	}
	else
	{
	    string regpat = sformat (p, "(.+)");
	    foreach (string file, files, {
		if (regexpmatch (file, regpat))
		{
		    string key = regexpsub (file, regpat, "\\1");
		    ret[key] = dir + "/" + file;
		}
	    });
	}
    });
    y2milestone ("Files containing license: %1", ret);
    return ret;
}

/**
 * Ask user to confirm license agreement
 * @param src_id integer source to get the license from
 * @param dir string directory to look for the license in if src_id is nil
 *   and not 1st stage installation
 * @param patterns a list of patterns for the files, regular expressions
 *   with %1 for the language
 * @param boolean enable_back sets the back_button status
 * @param boolean base_product defines whether it is a base or add-on product
 *   true means base product, false add-on product
 */
global symbol AskLicenseAgreement (integer src_id, string dir,
    list<string> patterns, string action, boolean enable_back,
    boolean base_product)
{
    string tmpdir = nil;
    string info_file = nil;
    string lic_lang = "";
    map<string,string> licenses = $[];
    list<string> available_langs = [];
    string license_dir = nil;

    if (src_id == nil)
    {
	if (Stage::initial ())
	{
	    license_dir = Directory::vardir + "/license";
	}
	else
	{
	    license_dir = dir;
	}
	if (FileUtils::Exists ("/info.txt"))
	    info_file = "/info.txt";
    }
    else
    {
	y2milestone ("Getting license info from source %1", src_id);
	tmpdir = (string) SCR::Read(.target.tmpdir) + "/product-license/";
	license_dir = tmpdir;
	string license_file = Pkg::SourceProvideOptionalFile (src_id, 1,
	    "/media.1/license.zip");

	// license is part of the add-on product
	if (license_file != nil) {
	    y2milestone("Product has a license");
	    map out = (map)SCR::Execute (.target.bash_output, sformat ("
rm -rf '%1' && \
mkdir -p '%1' && \
cd '%1' && \
unzip -qqo '%2'
", tmpdir, license_file));

	    // Extracting license failed, cannot accept the license
	    if (out["exit"]:0 != 0)
	    {
		y2error("Cannot unzip license -> %1", out);
		// popup error
		Report::Error (_("An error occurred while preparing the installation system."));
		CleanUpLicense(tmpdir);
		return nil; // FIXME
	    }
	}
	info_file = Pkg::SourceProvideOptionalFile (src_id, 1, "/media.1/info.txt");
    }
    licenses = LicenseFiles (license_dir,
	["license.txt", "license\\.%1\\.txt"]);
    if (info_file == nil && size (licenses) == 0)
	return `auto;

    // Preferencies how the client selects from available languages
    list<string> langs = [
	Language::language,
	substring (Language::language, 0, 2), // "it_IT" -> "it"
	"en_US",
	"en_EN",
	"en",
	"" // license.txt fallback
    ];
    available_langs = maplist (string lang, string fn,
	licenses,
    {
	return lang;
    });

    // "en" is the same as "", we don't need to have them both
    if (contains(available_langs, "en") && contains(available_langs, "")) {
	y2milestone("Removing license fallback '' as we already have 'en'...");
	available_langs = filter (string one_lang, available_langs, {
	    return one_lang != "en";
	});
    }

    y2milestone ("Preffered lang: %1", Language::language);
    if (size (available_langs) == 0)
	return `auto; // no license available
    lic_lang = find (string l, langs, {
	return haskey (licenses, l);
    });
    if (lic_lang == nil)
	lic_lang = available_langs[0]:"";

    y2milestone ("Preselected language: '%1'", lic_lang);
    DisplayLicenseDialog (available_langs, enable_back);
    // Display info as a popup if exists
    if (info_file != nil)
	InstShowInfo::show_info_txt(info_file);

    if (lic_lang == nil)
    {
	CleanUpLicense(tmpdir);
	return `auto;
    }

    // initial loop
    UI::ChangeWidget(`id(`lang), `Value, lic_lang);
    symbol ret = `first;

    // set timeout for autoinstallation
    // bugzilla #206706
    boolean timeout = false;
    if (Mode::autoinst()) {
	timeout = true;
    }

    while (true)
    {
	if (ret == `lang || ret == `first)
	{
	    // read the selected language if not in the initial loop
	    if (ret != `first) lic_lang = (string) UI::QueryWidget (`id (`lang), `Value);
	    string license_file = licenses[lic_lang]:"";
	    y2milestone ("Using license file: %1", license_file);
	    string license_text = (string)
		SCR::Read(.target.string, license_file);
	    if (license_text == nil)
	    {
		license_text = "";
// TODO error message
	    }
	    term rt = `Empty();
	    if (regexpmatch(license_text, "</.*>"))
		rt = `RichText(`id( `welcome_text), license_text);
	    else
		rt = `RichText(`id( `welcome_text), `opt(`plainText), license_text);
	    UI::ReplaceWidget (`id (`license_rp), rt);
	}

	// bugzilla #206706
	if (timeout) {
	    sleep (2000);
	    y2milestone("AutoYaST: License has been accepted after timeout...");
	    return `accepted;
	}

	ret = (symbol) UI::UserInput();

	symbol accept = (symbol) UI::QueryWidget(`id(`eula), `CurrentButton);
	// Aborting the license dialog
	if (ret == `abort) {
	    // bugzilla #218677
	    if (base_product) {
		if (Popup::ConfirmAbort (`painless)) {
		    y2milestone("Aborting...");
		    return `abort;
		}
	    } else {
		// popup question
		if (Popup::ContinueCancel(_("Really abort add-on product installation?"))) {
		    y2milestone("Aborting...");
		    return `abort;
		}
	    }
	}
	else if (ret == `next)
	{
	    // License declined
	    if (accept == `no)
	    {
		if (Popup::YesNo(_("Reread or reconsider the license agreement?")))
		{
		    continue;
		}
		else
		{
		    y2milestone("License has been declined.");
		    if (action == "abort")
			return `abort;
		    else if (action == "continue")
			return `accepted;
		    else if (action == "halt")
		    {
			return `halt;
			// timed ok/cancel popup
			if (!Popup::TimedOKCancel(_("System is shutting down..."), 10))
			{
			    continue;
			}
			else
			{
			    return `halt;
			}
		    }
		    else
		    {
			y2error ("Unknown action %1", action);
			return `abort;
		    }
		}
	    }
	    // License accepted
	    else if (accept == `yes)
	    {
		y2milestone("License has been accepted.");
		return `accepted;
	    }
	    else
	    {
		// message popup
		Popup::Message(_("Accept or decline the license agreement."));
		continue;
	    }
        }
	else if (ret == `back)
	{
	    return `back;
	}
    }

    CleanUpLicense(tmpdir);

}

global symbol AskAddOnLicenseAgreement (integer src_id) {
    return AskLicenseAgreement (src_id, "",
	[ "license\\.txt", "license\\.%1\\.txt" ],
	"abort",
	// back button is disabled
	false, false);
}

global symbol AskFirstStageLicenseAgreement (string action) {
    // bug #223258
    // disabling back button when the select-language dialog is skipped
    //
    // FIXME: hotfix for 10.2, solve better for 10.3
    //
    boolean enable_back = true;
    if (Stage::initial () &&
	ProductFeatures::GetBooleanFeature ("globals", "skip_language_dialog") &&
	Language::preselected != "en_US"
    ) enable_back = false;

    return AskLicenseAgreement (nil, "",
	[ "license\\.txt", "license\\.%1\\.txt" ],
	action,
	// back button is enabled
	enable_back, true);
}

global symbol AskInstalledLicenseAgreement (string directory,
    list<string> patterns, string action)
{
    return AskLicenseAgreement (nil, directory, patterns, action, false, true);

}


} // EOF

ACC SHELL 2018