| 1 |
/* |
| 2 |
* Copyright (C) 2018 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit from "cockpit"; |
| 7 |
import React from "react"; |
| 8 |
|
| 9 |
import { Flex } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js"; |
| 10 |
import { Spinner } from "@patternfly/react-core/dist/esm/components/Spinner/index.js"; |
| 11 |
import { WarningTriangleIcon } from "@patternfly/react-icons"; |
| 12 |
|
| 13 |
import { show_modal_dialog } from "cockpit-components-dialog.jsx"; |
| 14 |
import { getPackageManager, InstallProgressType } from "packagemanager"; |
| 15 |
|
| 16 |
import "cockpit-components-install-dialog.css"; |
| 17 |
import { fmt_to_fragments } from "utils"; |
| 18 |
|
| 19 |
const _ = cockpit.gettext; |
| 20 |
|
| 21 |
/* Calling install_dialog will open a dialog that lets the user |
| 22 |
* install the given package. |
| 23 |
* |
| 24 |
* The install_dialog function returns a promise that is fulfilled when the dialog closes after |
| 25 |
* a successful installation. The promise is rejected when the user cancels the dialog. |
| 26 |
* |
| 27 |
* If the package is already installed before the dialog opens, we still go |
| 28 |
* through all the motions and the dialog closes successfully without doing |
| 29 |
* anything when the use hits "Install". |
| 30 |
* |
| 31 |
* You shouldn't call install_dialog unless you know that PackageKit is available. |
| 32 |
* (If you do anyway, the resulting D-Bus errors will be shown to the user.) |
| 33 |
*/ |
| 34 |
|
| 35 |
export async function install_dialog(pkg, options) { |
| 36 |
let data = null; |
| 37 |
let error_message = null; |
| 38 |
let progress_message = null; |
| 39 |
let cancel = null; |
| 40 |
let done = null; |
| 41 |
|
| 42 |
if (!Array.isArray(pkg)) |
| 43 |
pkg = [pkg]; |
| 44 |
|
| 45 |
options = options || { }; |
| 46 |
|
| 47 |
const package_manager = await getPackageManager(); |
| 48 |
const prom = new Promise((resolve, reject) => { done = f => { if (f) resolve(); else reject(); } }); |
| 49 |
|
| 50 |
let dialog = null; |
| 51 |
function update() { |
| 52 |
let extra_details = null; |
| 53 |
let remove_details = null; |
| 54 |
let footer_message = null; |
| 55 |
|
| 56 |
const missing_name = <strong>{pkg.join(", ")}</strong>; |
| 57 |
|
| 58 |
if (data && data.extra_names.length > 0) |
| 59 |
extra_details = ( |
| 60 |
<div className="scale-up-ct"> |
| 61 |
{_("Additional packages:")} |
| 62 |
<ul className="package-list-ct">{data.extra_names.map(id => <li key={id}>{id}</li>)}</ul> |
| 63 |
</div> |
| 64 |
); |
| 65 |
|
| 66 |
if (data && data.remove_names.length > 0) |
| 67 |
remove_details = ( |
| 68 |
<div className="scale-up-ct"> |
| 69 |
<WarningTriangleIcon /> {_("Removals:")} |
| 70 |
<ul className="package-list">{data.remove_names.map(id => <li key={id}>{id}</li>)}</ul> |
| 71 |
</div> |
| 72 |
); |
| 73 |
|
| 74 |
if (progress_message) |
| 75 |
footer_message = ( |
| 76 |
<Flex spaceItems={{ default: 'spaceItemsSm' }} alignItems={{ default: 'alignItemsCenter' }}> |
| 77 |
<span>{ progress_message }</span> |
| 78 |
<Spinner size="sm" /> |
| 79 |
</Flex> |
| 80 |
); |
| 81 |
else if (data?.download_size) { |
| 82 |
footer_message = ( |
| 83 |
<div> |
| 84 |
{ fmt_to_fragments(_("Total size: $0"), <strong>{cockpit.format_bytes(data.download_size)}</strong>) } |
| 85 |
</div> |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
const body = { |
| 90 |
id: "dialog", |
| 91 |
title: options.title || _("Install software"), |
| 92 |
body: ( |
| 93 |
<div className="scroll"> |
| 94 |
<p>{ fmt_to_fragments(options.text || _("$0 will be installed."), missing_name) }</p> |
| 95 |
{ remove_details } |
| 96 |
{ extra_details } |
| 97 |
</div> |
| 98 |
), |
| 99 |
static_error: error_message, |
| 100 |
}; |
| 101 |
|
| 102 |
const footer = { |
| 103 |
actions: [ |
| 104 |
{ |
| 105 |
caption: _("Install"), |
| 106 |
style: "primary", |
| 107 |
clicked: install_missing, |
| 108 |
disabled: data == null |
| 109 |
} |
| 110 |
], |
| 111 |
idle_message: footer_message, |
| 112 |
dialog_done: f => { if (!f && cancel) cancel(); done(f) } |
| 113 |
}; |
| 114 |
|
| 115 |
if (dialog) { |
| 116 |
dialog.setProps(body); |
| 117 |
dialog.setFooterProps(footer); |
| 118 |
} else { |
| 119 |
dialog = show_modal_dialog(body, footer); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
function check_missing() { |
| 124 |
package_manager.check_missing_packages(pkg, |
| 125 |
p => { |
| 126 |
cancel = p.cancel; |
| 127 |
let pm = null; |
| 128 |
if (p.waiting) |
| 129 |
pm = _("Waiting for other software management operations to finish"); |
| 130 |
else |
| 131 |
pm = _("Checking installed software"); |
| 132 |
if (pm != progress_message) { |
| 133 |
progress_message = pm; |
| 134 |
update(); |
| 135 |
} |
| 136 |
}) |
| 137 |
.then(d => { |
| 138 |
if (d.unavailable_names.length > 0) |
| 139 |
error_message = cockpit.format(_("$0 is not available from any repository."), |
| 140 |
d.unavailable_names[0]); |
| 141 |
else |
| 142 |
data = d; |
| 143 |
progress_message = null; |
| 144 |
cancel = null; |
| 145 |
update(); |
| 146 |
}) |
| 147 |
.catch(e => { |
| 148 |
progress_message = null; |
| 149 |
cancel = null; |
| 150 |
error_message = e.toString(); |
| 151 |
update(); |
| 152 |
}); |
| 153 |
} |
| 154 |
|
| 155 |
function install_missing(progress_cb) { |
| 156 |
return package_manager.install_missing_packages(data, |
| 157 |
p => { |
| 158 |
let text = null; |
| 159 |
if (p.waiting) { |
| 160 |
text = _("Waiting for other software management operations to finish"); |
| 161 |
} else if (p.package) { |
| 162 |
let fmt; |
| 163 |
if (p.info == InstallProgressType.DOWNLOADING) |
| 164 |
fmt = _("Downloading $0"); |
| 165 |
else if (p.info == InstallProgressType.REMOVING) |
| 166 |
fmt = _("Removing $0"); |
| 167 |
else |
| 168 |
fmt = _("Installing $0"); |
| 169 |
text = fmt_to_fragments(fmt, <strong>{p.package}</strong>); |
| 170 |
} |
| 171 |
progress_cb(text, p.cancel); |
| 172 |
}); |
| 173 |
} |
| 174 |
|
| 175 |
update(); |
| 176 |
check_missing(); |
| 177 |
return prom; |
| 178 |
} |