From 9e9a4808e3bc18fd217e63defef35bf295b49dd0 Mon Sep 17 00:00:00 2001 From: Martin Kolman Date: Mon, 13 Mar 2023 16:31:42 +0100 Subject: [PATCH] Timezone module DBus API access for the Web UI Provide access for the necessary DBus calls from the Date and time screen on the Web UI. --- ui/webui/src/apis/timezone.js | 156 ++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 ui/webui/src/apis/timezone.js diff --git a/ui/webui/src/apis/timezone.js b/ui/webui/src/apis/timezone.js new file mode 100644 index 000000000000..82228c78a316 --- /dev/null +++ b/ui/webui/src/apis/timezone.js @@ -0,0 +1,156 @@ +/* + * Copyright (C) 2023 Red Hat, Inc. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with This program; If not, see . + */ + +import cockpit from "cockpit"; + +export class TimezoneClient { + constructor (address) { + if (TimezoneClient.instance) { + return TimezoneClient.instance; + } + TimezoneClient.instance = this; + + this.client = cockpit.dbus( + "org.fedoraproject.Anaconda.Modules.Timezone", + { superuser: "try", bus: "none", address } + ); + } + + init () { + this.client.addEventListener("close", () => console.error("Timezone client closed")); + } +} + +/** + * @returns {Promise} The current system Timezone + */ +export const getTimezone = () => { + return ( + new TimezoneClient().client.call( + "/org/fedoraproject/Anaconda/Modules/Timezone", + "org.freedesktop.DBus.Properties", + "Get", + [ + "org.fedoraproject.Anaconda.Modules.Timezone", + "Timezone" + ] + ) + .then(res => res[0].v) + ); +}; + +/** + * @param {string} timezone Timezone id + */ +export const setTimezone = ({ timezone }) => { + return new TimezoneClient().client.call( + "/org/fedoraproject/Anaconda/Modules/Timezone", + "org.freedesktop.DBus.Properties", + "Set", + [ + "org.fedoraproject.Anaconda.Modules.Timezone", + "Timezone", + cockpit.variant("s", timezone) + ] + ); +}; + +/** + * @returns {Promise} Resolves a list of timezones + */ +export const getTimezones = () => { + return new TimezoneClient().client.call( + "/org/fedoraproject/Anaconda/Modules/Timezone", + "org.fedoraproject.Anaconda.Modules.Timezone", + "GetTimezones", [] + ); +}; + +/** + * @returns {Promise} NTP enabled + */ +export const getNTPEnabled = () => { + return ( + new TimezoneClient().client.call( + "/org/fedoraproject/Anaconda/Modules/Timezone", + "org.freedesktop.DBus.Properties", + "Get", + ["org.fedoraproject.Anaconda.Modules.Timezone", "NTPEnabled"] + ) + .then(res => res[0].v) + ); +}; + +/** + * @param {bool} enabled - enable/disable NTP + * + * @returns {Promise} FIXME: what does it return in this case ?? + */ + +export const setNTPEnabled = ({ enabled }) => { + return ( + new TimezoneClient().client.call( + "/org/fedoraproject/Anaconda/Modules/Timezone", + "org.freedesktop.DBus.Properties", + "Set", + [ + "org.fedoraproject.Anaconda.Modules.Timezone", + "NTPEnabled", + cockpit.variant("b", enabled) + ] + ) + ); +}; + +/** + * @returns {Promise} NTP sync status + */ +export const getNTPStatus = () => { + return ( + new TimezoneClient().client.call( + "/org/fedoraproject/Anaconda/Modules/Timezone", + "org.freedesktop.DBus.Properties", + "Get", + ["org.fedoraproject.Anaconda.Modules.Timezone", "NTPStatus"] + ) + .then(res => res[0].v) + ); +}; + +/** + * @returns {Promise} Resolves the DBus path to the partitioning + */ +export const getSystemDateTime = () => { + return new TimezoneClient().client.call( + "/org/fedoraproject/Anaconda/Modules/Timezone", + "org.fedoraproject.Anaconda.Modules.Timezone", + "GetSystemDateTime", [] + ); +}; + +/** + * @param {string} datetimespec date time specification in ISO 8601 format + * + * @returns {Promise} FIXME: what does it return in this case ?? + */ +export const setSystemDateTime = ({ datetimespec }) => { + return new TimezoneClient().client.call( + "/org/fedoraproject/Anaconda/Modules/Timezone", + "org.fedoraproject.Anaconda.Modules.Timezone", + "SetSystemDateTime", [datetimespec] + ); +};