| 1 |
/* |
| 2 |
* Copyright (C) 2017 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 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 9 |
import { Progress, ProgressMeasureLocation } from "@patternfly/react-core/dist/esm/components/Progress/index.js"; |
| 10 |
import { Split, SplitItem } from "@patternfly/react-core/dist/esm/layouts/Split/index.js"; |
| 11 |
import { Spinner } from "@patternfly/react-core/dist/esm/components/Spinner/index.js"; |
| 12 |
import { show_modal_dialog } from "cockpit-components-dialog.jsx"; |
| 13 |
|
| 14 |
const _ = cockpit.gettext; |
| 15 |
|
| 16 |
export function debug(...args: unknown[]) { |
| 17 |
if (window.debugging == "all" || window.debugging?.includes("apps")) { |
| 18 |
console.debug("apps:", ...args); |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
export function icon_url(path_or_url: string): string { |
| 23 |
if (!path_or_url) |
| 24 |
return "default.png"; |
| 25 |
|
| 26 |
if (path_or_url[0] != '/') |
| 27 |
return path_or_url; |
| 28 |
|
| 29 |
interface QueryObj { |
| 30 |
payload: string, |
| 31 |
binary: "raw", |
| 32 |
path: string, |
| 33 |
external?: { [key: string]: string } |
| 34 |
} |
| 35 |
|
| 36 |
const queryobj: QueryObj = { |
| 37 |
payload: "fsread1", |
| 38 |
binary: "raw", |
| 39 |
path: path_or_url, |
| 40 |
}; |
| 41 |
|
| 42 |
if (path_or_url.endsWith(".svg")) { |
| 43 |
queryobj.external = { "content-type": "image/svg+xml" }; |
| 44 |
} |
| 45 |
|
| 46 |
const prefix = (new URL(cockpit.transport.uri("channel/" + cockpit.transport.csrf_token))).pathname; |
| 47 |
const query = window.btoa(JSON.stringify(queryobj)); |
| 48 |
return prefix + '?' + query; |
| 49 |
} |
| 50 |
|
| 51 |
export interface JobProgress { |
| 52 |
percentage: number, |
| 53 |
waiting: boolean, |
| 54 |
cancel: React.MouseEventHandler, |
| 55 |
} |
| 56 |
|
| 57 |
export const ProgressBar = ({ size, data, ariaLabelledBy }: { |
| 58 |
size?: "lg" | "md" | "sm", |
| 59 |
data: JobProgress, |
| 60 |
ariaLabelledBy?: string, |
| 61 |
}) => { |
| 62 |
if (data.waiting) { |
| 63 |
return (<Split> |
| 64 |
<SplitItem className="progress-title" isFilled> |
| 65 |
{_("Waiting for other programs to finish using the package manager...")} |
| 66 |
</SplitItem> |
| 67 |
<SplitItem> |
| 68 |
<Spinner size="md" /> |
| 69 |
</SplitItem> |
| 70 |
</Split>); |
| 71 |
} else { |
| 72 |
return <Progress className="progress-bar" value={data.percentage} size={size} measureLocation={ProgressMeasureLocation.inside} aria-labelledby={ariaLabelledBy} />; |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
export const CancelButton = ({ data }: { data: JobProgress }) => ( |
| 77 |
<Button variant="secondary" isDisabled={!data.cancel} onClick={data.cancel}> |
| 78 |
{_("Cancel")} |
| 79 |
</Button>); |
| 80 |
|
| 81 |
type ProgressCallback = (data: JobProgress) => void |
| 82 |
|
| 83 |
export class ProgressReporter { |
| 84 |
base: number; |
| 85 |
range: number; |
| 86 |
percentage: number; |
| 87 |
callback: ProgressCallback; |
| 88 |
|
| 89 |
constructor(base: number, range: number, callback: ProgressCallback) { |
| 90 |
this.base = base; |
| 91 |
this.range = range; |
| 92 |
this.callback = callback; |
| 93 |
this.percentage = 0; |
| 94 |
this.progress_reporter = this.progress_reporter.bind(this); |
| 95 |
} |
| 96 |
|
| 97 |
progress_reporter(data: JobProgress) { |
| 98 |
if (data.percentage >= 0) { |
| 99 |
const newPercentage = this.base + data.percentage / 100 * this.range; |
| 100 |
// PackageKit with Apt backend reports wrong percentages https://github.com/PackageKit/PackageKit/issues/516 |
| 101 |
// Double check here that we have an increasing only progress value |
| 102 |
if (this.percentage == undefined || newPercentage >= this.percentage) |
| 103 |
this.percentage = newPercentage; |
| 104 |
} |
| 105 |
this.callback({ ...data, percentage: this.percentage }); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// ex is a PackageKit error; requires typing pkg/lib/packagekit.js first |
| 110 |
// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 111 |
export const show_error = (ex: any) => { |
| 112 |
if (ex.code == "cancelled") |
| 113 |
return; |
| 114 |
|
| 115 |
if (ex.code == "not-found") |
| 116 |
ex.detail = _("No installation package found for this application."); |
| 117 |
|
| 118 |
show_modal_dialog( |
| 119 |
{ |
| 120 |
title: _("Error"), |
| 121 |
body: ( |
| 122 |
<p>{typeof ex == 'string' ? ex : (ex.detail || ex.message)}</p> |
| 123 |
) |
| 124 |
}, |
| 125 |
{ |
| 126 |
cancel_button: { text: _("Close"), variant: "secondary" }, |
| 127 |
actions: [] |
| 128 |
}); |
| 129 |
}; |
| 130 |
|
| 131 |
export interface Launchable { |
| 132 |
type: string, |
| 133 |
name: string, |
| 134 |
} |
| 135 |
|
| 136 |
// see pkg/apps/watch-appstream.py |
| 137 |
export interface Component { |
| 138 |
id: string, |
| 139 |
pkgname: string, |
| 140 |
name: string, |
| 141 |
summary: string, |
| 142 |
description: string, |
| 143 |
icon: string, // this is a path |
| 144 |
screenshots: { full: string }[], |
| 145 |
launchables: Launchable[], |
| 146 |
urls: { type: string, link: string }[], |
| 147 |
installed?: boolean, |
| 148 |
} |
| 149 |
|
| 150 |
export const launch = (comp: Component) => { |
| 151 |
for (let i = 0; i < comp.launchables.length; i++) { |
| 152 |
if (comp.launchables[i].type == "cockpit-manifest") { |
| 153 |
debug("launching", comp.launchables[i].name, "in component", JSON.stringify(comp)); |
| 154 |
cockpit.jump([comp.launchables[i].name]); |
| 155 |
return; |
| 156 |
} |
| 157 |
} |
| 158 |
}; |
| 159 |
|
| 160 |
export function reload_bridge_packages() { |
| 161 |
return cockpit.dbus(null, { bus: "internal" }).call("/packages", "cockpit.Packages", "Reload", []); |
| 162 |
} |