ACC SHELL

Path : /usr/share/YaST2/include/packager/
File Upload :
Current File : //usr/share/YaST2/include/packager/key_manager_dialogs.ycp

/**
 * File:	key_manager.ycp
 *
 * Author:	Ladislav Slezak <lslezak@novell.com>
 *
 * Purpose:	Manages GPG keys in the package manager
 *
 * $Id: key_manager_dialogs.ycp 57028 2009-04-29 10:58:09Z lslezak $
 */

{

textdomain "packager";

import "Wizard";
import "WizardHW";
import "Label";
import "Popup";
import "KeyManager";
import "String";
import "FileUtils";
import "Directory";
import "Report";
import "Sequencer";

boolean gpg_mgr_standalone_mode = false;

/**
 * Creates a summary table with GPG key configuration
 * @return list table contents
 */
list<map<string,any> > createHWTable()
{
    list<map<string,any> > ret = [];

    foreach(map<string,any> key, KeyManager::GetKeys(),
	{
	    list<string> descr = [];

	    // escape <> characters in the key name
	    // translators: %1 is GPG key name (e.g. 'SuSE Package Signing Key <build@suse.de>')
	    descr = add(descr, sformat(_("Name: %1"), String::EscapeTags(key["name"]:"")));
	    // %1 is a GPG Key fingerprint (e.g. '79C179B2E1C820C1890F9994A84EDAE89C800ACA')
	    descr = add(descr, sformat(_("Finger Print: %1"), key["fingerprint"]:""));
	    // %1 is the date when the GPG key was generated (e.g. '9.10.2000')
	    descr = add(descr, sformat(_("Created: %1"), key["created"]:""));

	    integer expires = key["expires_raw"]:0;
	    string exp_str = (expires > 0 && time() > expires) ?
		// %1 is the date when the GPG key expired (e.g. '10.6.2005'), display the date in red
		sformat(_("Expires: <font color = \"red\">%1</font> (The key is expired.)"), key["expires"]:"")
		// summary string - the GPG key never expires
		: (expires == 0 ? _("The key never expires.")
		    // %1 is the date when the GPG key expires (e.g. '21.3.2015') or "Never"
		    : sformat(_("Expires: %1"), key["expires"]:"")
		);

	    descr = add(descr, exp_str);

	    string icon_tag = "<IMG SRC=\"" + Directory::icondir
		+ "/22x22/apps/yast-security.png\">&nbsp;&nbsp;&nbsp;";

	    map<string,any> r = $[
		"id" : key["id"]:"",
		"table_descr" : [ key["id"]:"", key["name"]:"" ],
		"rich_descr" : WizardHW::CreateRichTextDescription(icon_tag + sformat(_("Key: %1"), key["id"]:""), descr)
	    ];

	    ret = add(ret, r);
	}
    );

    y2debug("table content: %1", ret);

    return ret;
}

/**
 * Set/refresh the table content
 */
void SetItems(string selected_key)
{
    // create description for WizardHW
    list<map<string,any> > items = createHWTable();
    y2debug("items: %1", items);

    WizardHW::SetContents(items);

    if (selected_key != nil)
    {
	// set the previously selected key
	WizardHW::SetSelectedItem(selected_key);
    }

    // properties of a key cannot be changed, disable Edit button
    UI::ChangeWidget(`id(`edit), `Enabled, false);
}

// remember the details about the added key from AddGPGKey(),
// the details are displayed in NewKeySummary()
map<string,any> added_key = $[];

void refreshNewKeyDetails(string file)
{
    map<string,any> key = $[];

    // at first check whether the file exists at all
    if (FileUtils::Exists(file))
    {
	// check whether the file contains a valid GPG key
	key = Pkg::CheckGPGKeyFile(file);
	y2milestone("File content: %1", key);
    }

    UI::ChangeWidget(`id(`key_id), `Value, key["id"]:"");
    UI::ChangeWidget(`id(`key_name), `Value, key["name"]:"");
    UI::ChangeWidget(`id(`key_fp), `Value, key["fingerprint"]:"");
    UI::ChangeWidget(`id(`key_creadted), `Value, key["created"]:"");
    UI::ChangeWidget(`id(`key_expires), `Value, key["expires"]:"");
}

/**
 * Display a dialog for adding a GPG key
 */
symbol AddGPGKey()
{
    term contents = `VBox(
	`Frame(`id(`fr), _("Select a GPG Key"),
	    `MarginBox(1, 0.3,
		`VBox(
		    `HBox(
			`InputField(`id(`file), `opt(`notify, `hstretch), Label::FileName()),
			`VBox(
			    `Label(""),
			    `PushButton(`id(`browse), Label::BrowseButton())
			)
		    )
		)
	    )
	),
	`VSpacing(1),
	`Frame(_("Properties of the GPG Key"),
	    `MarginBox(1, 0.3,
		`HBox(
		    `HSquash(
			`VBox(
			    `Left(`Label(_("Key ID: "))),
			    `Left(`Label(_("Name: "))),
			    `Left(`Label(_("Finger Print: "))),
			    `Left(`Label(_("Created: "))),
			    `Left(`Label(_("Expires: ")))
			)
		    ),
		    `VBox(
			`Label(`id(`key_id), `opt(`hstretch), ""),
			`Label(`id(`key_name), `opt(`hstretch), ""),
			`Label(`id(`key_fp), `opt(`hstretch), ""),
			`Label(`id(`key_creadted), `opt(`hstretch), ""),
			`Label(`id(`key_expires), `opt(`hstretch), "")
		    )
		)
	    )
	)
    );

    // dialog caption
    string title = _("Adding a GPG Public Key");

    // help
    string help_text = _("<p>
In this dialog, manage known GPG public keys.</p>");

    help_text = help_text + _("<p>
<b>Adding a New GPG Key</b><br>
To add a new GPG key specify the path to the key file.
Check the <B>Trusted</B> check box if the key is trusted.
</p>");

    Wizard::SetNextButton(`next, Label::OKButton());
    Wizard::SetContents(title, contents, help_text, true, true);

    symbol ret = nil;

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

	y2debug("UserInput: %1", ret);

	if (ret == `browse)
	{
	    string currentfile = (string) UI::QueryWidget(`id(`file), `Value);
	    // header in file selection popup
	    string newfile = (string) UI::AskForExistingFile(currentfile, "*", _("Select a GPG Key To Import"));

	    if (newfile != nil)
	    {
		UI::ChangeWidget(`id(`file), `Value, newfile);
		refreshNewKeyDetails(newfile);
	    }
	}
	else if (ret == `file)
	{
	    string keyfile = (string) UI::QueryWidget(`id(`file), `Value);

	    y2debug("The file has changed: %1", keyfile);

	    // refresh the information
	    refreshNewKeyDetails(keyfile);
	}
	else if (ret == `next)
	{
	    // validate the entered file
	    string keyfile = (string) UI::QueryWidget(`id(`file), `Value);
	    y2milestone("Selected file: %1", keyfile);

	    if (keyfile == nil || keyfile == "")
	    {
		Report::Error(_("Enter a filename"));
		UI::SetFocus(`id(`file));
		ret = `_dummy;
		continue;
	    }

	    // always add as trusted
	    added_key = KeyManager::ImportFromFile(keyfile, true);

	    if (added_key == nil || size(added_key) == 0)
	    {
		ret = `_dummy;
	    }
	}

    }
    while (!contains([`back, `abort, `next], ret));

    Wizard::RestoreNextButton();

    return ret;
}

/**
 * Display the main dialog for GPG key management
 */
symbol KeySummary()
{
    y2milestone ("Running Summary dialog");

    // dialog caption
    string title = _("GPG Public Key Management");

    // help
    string help_text = _("<p>
In this dialog, manage known GPG public keys.</p>");

    help_text = help_text + _("<p>
<b>Adding a New GPG Key</b><br>
To add a new GPG key, use <b>Add</b> and specify the path to the key file.
</p>");

    // help, continued
    help_text = help_text + _("<p>
<b>Modifying a GPG Key Status</b>
To modify the trusted flag, use <b>Edit</b>. To remove a GPG key, use
<b>Delete</b>.
</p>
");

    // table header
    WizardHW::CreateHWDialog(title, help_text, [_("Key ID"), _("Name")], []);

    // set the navigation keys according to the current mode
    if (gpg_mgr_standalone_mode)
    {
	Wizard::DisableBackButton();
	Wizard::SetNextButton(`next, Label::FinishButton());
    }
    else
    {
	Wizard::EnableBackButton();
	Wizard::SetNextButton(`next, Label::OKButton());
    }

    symbol ret = nil;

    // set the table content
    SetItems(nil);

    do
    {
	map<string,any> ev = WizardHW::WaitForEvent();
	y2milestone("WaitForEvent: %1", ev);

	ret = (symbol)ev["event","ID"]:nil;

	// the selected key
	string key_id = ev["selected"]:"";

	y2milestone("Selected key: %1, action: %2", key_id, ret);

	// remove the key
	if (ret == `delete)
	{
	    map<string,any> key = KeyManager::SearchGPGKey(key_id);

	    if (Popup::YesNo(sformat(_("Really delete key '%1'
'%2'?"), key_id, key["name"]:"")))
	    {
		KeyManager::DeleteKey(key_id);
		// refresh the table
		SetItems(nil);

		// hack - refresh (clear) the rich text part of the dialog
		// TODO: fix a bug in WizardHW?
		if (size(KeyManager::GetKeys()) == 0)
		{
		    SetItems("");
		}
	    }
	}
    }
    while (!contains([`back, `abort, `next, `add], ret));

    return ret;
}

/**
 * Run the GPG key management workflow
 */
symbol RunGPGKeyMgmt(boolean standalone)
{
    gpg_mgr_standalone_mode = standalone;

    map<string,any> aliases = $[
	"summary" : ``(KeySummary()),
	"add" : [``(AddGPGKey()), true ]
    ];

    map sequence = $[
	"ws_start" : "summary",
	"summary" : $[
	    `abort : `abort,
	    `next : `next,
	    `add  : "add"
	],
	"add" : $[
	    `next : "summary",
	    `abort : `abort
	]
    ];

    y2milestone ("Starting the key management sequence (standalone: %1)", standalone);
    symbol ret = Sequencer::Run (aliases, sequence);

    return ret;
}

}

ACC SHELL 2018