ACC SHELL

Path : /usr/share/YaST2/clients/
File Upload :
Current File : //usr/share/YaST2/clients/inst_addon_update_sources.ycp

/**
 * File:	clients/inst_addon_update_sources.ycp
 * Package:	yast2-installation
 * Summary:	Add installation sources for online update, #163192
 * Authors:	Martin Vidner <mvidner@suse.cz>
 *
 * Assumptions:
 * - the sources will be saved afterwards
 * (this means that running this client alone will not work)
 */

{
textdomain "installation";

import "GetInstArgs";
import "PackageCallbacks";
import "Popup";
import "SourceManager";
import "Report";
import "Installation";

include "packager/inst_source_dialogs.ycp";

if ( GetInstArgs::going_back())     // going backwards?
    return `auto;                   // don't execute this once more


/**
 * @return the urls of known installation sources
 */
list<string> KnownUrls () {
    list<integer> src_ids = Pkg::SourceGetCurrent (true /*enabled only?*/);
    list<string> urls = maplist (integer src_id, src_ids, {
	map gendata = Pkg::SourceGeneralData (src_id);
	return gendata["url"]:"";
    });
    return urls;
}

/**
 * @return the installation sources to be added
 */
map <string, string> UpdateUrls () {
    map <string, string> urls = $[];
    list <map <string,any> > products = Pkg::ResolvableProperties ("", `product, "");

    foreach (map<string, any> p, products, {
	foreach (string u,  p["update_urls"]:[], {
	    // bnc #542792
	    // Repository name must be generated from product details
	    urls[u] = sformat (
		_("Updates for %1 %2"),
		p["display_name"]:p["name"]:p["summary"]:_("Unknown Product"),
		p["version"]:""
	    );
	});
    });

    return urls;
}


// feedback heading
string heading = _("Add-On Product Installation");
// feedback message
string message = _("Reading packages available in the repositories...");

// bugzilla #270899#c29
Pkg::TargetInitialize (Installation::destdir);
Pkg::TargetLoad();
Pkg::SourceStartManager (true);

list<string> knownUrls = KnownUrls ();
y2milestone ("sources known: %1", knownUrls);
map<string, boolean> is_known = listmap (string u, knownUrls, ``($[u: true]));

map <string, string> updateUrls = UpdateUrls ();
y2milestone ("sources for updates: %1", updateUrls);

map <string, string> addUrls = filter (string url, string name, updateUrls, ``( ! is_known[url]:false ));
y2milestone ("sources to add: %1", addUrls);

if (addUrls != $[])
{
    Popup::ShowFeedback (heading, message);

    list<integer> added_ids = [];
    foreach (string url, string name, addUrls, {
	boolean again = true;

	while (again) {
	    // BNC #557723: Repositories migh be created without access to network
	    // Libzypp must not probe the repo
	    map <string,any> repo_prop = $[
		"enabled"	: true,
		"autorefresh"	: true,
		"name"		: (size(name) > 0 ? name : url),
		"alias"		: (size(name) > 0 ? name : url),
		"base_urls"	: [url],
		"prod_dir"	: "/",
		
	    ];

	    integer srcid = Pkg::RepositoryAdd (repo_prop);
	    y2milestone ("got %1 from creating %2/%3", srcid, url, name);

	    // wrong srcid, must have failed
	    if (srcid == -1) {
		// popup error message
		// %1 represents the the error message details
		if (Popup::YesNo ( sformat(
		    _("An error occurred while connecting to the server.
Details: %1

Try again?"),
		    Pkg::LastError()
		))) {
		    // try again
		    url = editUrl (url);
		} else {
		    // abort
		    again = false;
		}

	    // everything is ok
	    } else {
		added_ids = add (added_ids, srcid);
		again = false;
	    }
	}
    });

    y2milestone ("syncing to zmd");
    boolean synced = SourceManager::SyncAddedAndDeleted (added_ids, []);
    y2milestone ("sync status: %1", synced);

    Popup::ClearFeedback ();
}

return `auto;

/* EOF */
}

ACC SHELL 2018