From e4283f6d48b98e764b988b43bbc86b9d52e6ec94 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:54:43 +0200 Subject: Adding upstream version 43.9. Signed-off-by: Daniel Baumann --- js/misc/dbusUtils.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 js/misc/dbusUtils.js (limited to 'js/misc/dbusUtils.js') diff --git a/js/misc/dbusUtils.js b/js/misc/dbusUtils.js new file mode 100644 index 0000000..ac26894 --- /dev/null +++ b/js/misc/dbusUtils.js @@ -0,0 +1,68 @@ +// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*- +/* exported loadInterfaceXML, loadSubInterfaceXML */ + +const Config = imports.misc.config; +const { Gio, GLib } = imports.gi; + +let _ifaceResource = null; + +/** + * @private + */ +function _ensureIfaceResource() { + if (_ifaceResource) + return; + + // don't use global.datadir so the method is usable from tests/tools + let dir = GLib.getenv('GNOME_SHELL_DATADIR') || Config.PKGDATADIR; + let path = `${dir}/gnome-shell-dbus-interfaces.gresource`; + _ifaceResource = Gio.Resource.load(path); + _ifaceResource._register(); +} + +/** + * @param {string} iface the interface name + * @returns {string | null} the XML string or null if it is not found + */ +function loadInterfaceXML(iface) { + _ensureIfaceResource(); + + let uri = `resource:///org/gnome/shell/dbus-interfaces/${iface}.xml`; + let f = Gio.File.new_for_uri(uri); + + try { + let [ok_, bytes] = f.load_contents(null); + return new TextDecoder().decode(bytes); + } catch (e) { + log(`Failed to load D-Bus interface ${iface}`); + } + + return null; +} + +/** + * @param {string} iface the interface name + * @param {string} ifaceFile the interface filename + * @returns {string | null} the XML string or null if it is not found + */ +function loadSubInterfaceXML(iface, ifaceFile) { + let xml = loadInterfaceXML(ifaceFile); + if (!xml) + return null; + + let ifaceStartTag = ``; + let ifaceStopTag = ''; + let ifaceStartIndex = xml.indexOf(ifaceStartTag); + let ifaceEndIndex = xml.indexOf(ifaceStopTag, ifaceStartIndex + 1) + ifaceStopTag.length; + + let xmlHeader = '\n' + + '\n'; + let xmlFooter = ''; + + return ( + xmlHeader + + xml.substr(ifaceStartIndex, ifaceEndIndex - ifaceStartIndex) + + xmlFooter); +} -- cgit v1.2.3