2012-01-19 00:55:20 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2019-01-31 14:07:06 +00:00
|
|
|
/* exported ExtensionState, ExtensionType, getCurrentExtension,
|
2020-04-12 00:45:55 +00:00
|
|
|
getSettings, initTranslations, gettext, ngettext, pgettext,
|
2023-07-14 12:39:45 +00:00
|
|
|
openPrefs, serializeExtension, deserializeExtension, setExtensionManager */
|
2012-01-19 00:55:20 +00:00
|
|
|
|
|
|
|
// Common utils for the extension system and the extension
|
|
|
|
// preferences tool
|
|
|
|
|
2023-06-08 04:52:46 +00:00
|
|
|
const Gio = imports.gi.Gio;
|
|
|
|
const GLib = imports.gi.GLib;
|
2018-11-01 12:55:17 +00:00
|
|
|
|
2018-07-14 18:16:13 +00:00
|
|
|
const Gettext = imports.gettext;
|
2012-06-04 21:14:18 +00:00
|
|
|
|
2012-01-19 00:55:20 +00:00
|
|
|
const Config = imports.misc.config;
|
|
|
|
|
2023-07-09 10:47:05 +00:00
|
|
|
let _extensionManager = null;
|
2022-07-05 16:09:26 +00:00
|
|
|
|
2017-07-18 17:47:27 +00:00
|
|
|
var ExtensionType = {
|
2012-01-19 00:55:20 +00:00
|
|
|
SYSTEM: 1,
|
2019-08-20 21:43:54 +00:00
|
|
|
PER_USER: 2,
|
2012-01-19 00:55:20 +00:00
|
|
|
};
|
|
|
|
|
2019-07-06 13:31:57 +00:00
|
|
|
var ExtensionState = {
|
|
|
|
ENABLED: 1,
|
|
|
|
DISABLED: 2,
|
|
|
|
ERROR: 3,
|
|
|
|
OUT_OF_DATE: 4,
|
|
|
|
DOWNLOADING: 5,
|
|
|
|
INITIALIZED: 6,
|
2022-07-25 02:25:23 +00:00
|
|
|
DISABLING: 7,
|
|
|
|
ENABLING: 8,
|
2019-07-06 13:31:57 +00:00
|
|
|
|
|
|
|
// Used as an error state for operations on unknown extensions,
|
|
|
|
// should never be in a real extensionMeta object.
|
2019-08-20 21:43:54 +00:00
|
|
|
UNINSTALLED: 99,
|
2019-07-06 13:31:57 +00:00
|
|
|
};
|
|
|
|
|
2020-01-22 13:45:15 +00:00
|
|
|
const SERIALIZED_PROPERTIES = [
|
|
|
|
'type',
|
|
|
|
'state',
|
|
|
|
'path',
|
|
|
|
'error',
|
|
|
|
'hasPrefs',
|
|
|
|
'hasUpdate',
|
|
|
|
'canChange',
|
|
|
|
];
|
2018-11-01 12:55:17 +00:00
|
|
|
|
2022-07-05 16:09:26 +00:00
|
|
|
/**
|
2023-07-09 10:47:05 +00:00
|
|
|
* @param {object} extensionManager to use in utilities like `initTranslations()`
|
2022-07-05 16:09:26 +00:00
|
|
|
*/
|
2023-07-09 10:47:05 +00:00
|
|
|
function setExtensionManager(extensionManager) {
|
|
|
|
if (_extensionManager)
|
|
|
|
throw new Error('Trying to override existing extension manager');
|
2022-07-05 16:09:26 +00:00
|
|
|
|
2023-07-09 10:47:05 +00:00
|
|
|
_extensionManager = extensionManager;
|
2022-07-05 16:09:26 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 23:08:18 +00:00
|
|
|
/**
|
|
|
|
* getCurrentExtension:
|
|
|
|
*
|
2019-10-17 16:41:52 +00:00
|
|
|
* @returns {?object} - The current extension, or null if not called from
|
|
|
|
* an extension.
|
2016-10-03 23:08:18 +00:00
|
|
|
*/
|
2012-01-31 01:58:29 +00:00
|
|
|
function getCurrentExtension() {
|
2023-07-09 23:20:53 +00:00
|
|
|
const basePath = '/gnome-shell/extensions/';
|
2012-01-31 01:58:29 +00:00
|
|
|
|
2016-10-03 23:08:18 +00:00
|
|
|
// Search for an occurrence of an extension stack frame
|
|
|
|
// Start at 1 because 0 is the stack frame of this function
|
2023-07-09 23:20:53 +00:00
|
|
|
const [, ...stack] = new Error().stack.split('\n');
|
|
|
|
const extensionLine = stack.find(
|
|
|
|
line => line.includes(basePath));
|
2012-01-31 01:58:29 +00:00
|
|
|
|
2023-07-09 23:20:53 +00:00
|
|
|
if (!extensionLine)
|
2016-10-03 23:08:18 +00:00
|
|
|
return null;
|
2012-01-31 01:58:29 +00:00
|
|
|
|
2019-07-07 21:38:27 +00:00
|
|
|
// local import, as the module is used from outside the gnome-shell process
|
2023-07-09 10:47:05 +00:00
|
|
|
// as well
|
|
|
|
if (!_extensionManager)
|
|
|
|
setExtensionManager(imports.ui.main.extensionManager);
|
2019-07-07 21:38:27 +00:00
|
|
|
|
2023-07-09 23:20:53 +00:00
|
|
|
// The exact stack line differs depending on where the function
|
|
|
|
// was called (function or module scope), and whether it's called
|
|
|
|
// from a module or legacy import (file:// URI vs. plain path).
|
|
|
|
//
|
|
|
|
// We don't have to care about the exact composition, all we need
|
|
|
|
// is a string that can be traversed as path and contains the UUID
|
2023-07-09 23:20:53 +00:00
|
|
|
let path = extensionLine.slice(extensionLine.indexOf(basePath));
|
2012-01-31 01:58:29 +00:00
|
|
|
|
2014-11-29 18:05:11 +00:00
|
|
|
// Walk up the directory tree, looking for an extension with
|
2012-05-28 21:21:02 +00:00
|
|
|
// the same UUID as a directory name.
|
2023-07-09 23:20:53 +00:00
|
|
|
do {
|
|
|
|
path = GLib.path_get_dirname(path);
|
|
|
|
|
|
|
|
const dirName = GLib.path_get_basename(path);
|
2023-07-09 10:47:05 +00:00
|
|
|
const extension = _extensionManager.lookup(dirName);
|
2012-05-28 21:21:02 +00:00
|
|
|
if (extension !== undefined)
|
|
|
|
return extension;
|
2023-07-09 23:20:53 +00:00
|
|
|
} while (path !== '/');
|
2012-01-31 01:58:29 +00:00
|
|
|
|
2016-10-03 23:08:18 +00:00
|
|
|
return null;
|
2012-01-31 01:58:29 +00:00
|
|
|
}
|
|
|
|
|
2018-07-14 18:16:13 +00:00
|
|
|
/**
|
|
|
|
* Initialize Gettext to load translations from extensionsdir/locale.
|
|
|
|
* If @domain is not provided, it will be taken from metadata['gettext-domain']
|
2023-07-10 02:43:14 +00:00
|
|
|
*
|
|
|
|
* @param {string=} domain - the gettext domain to use
|
2018-07-14 18:16:13 +00:00
|
|
|
*/
|
|
|
|
function initTranslations(domain) {
|
|
|
|
let extension = getCurrentExtension();
|
|
|
|
|
|
|
|
if (!extension)
|
|
|
|
throw new Error('initTranslations() can only be called from extensions');
|
|
|
|
|
2022-01-18 20:02:04 +00:00
|
|
|
domain ||= extension.metadata['gettext-domain'];
|
2018-07-14 18:16:13 +00:00
|
|
|
|
|
|
|
// Expect USER extensions to have a locale/ subfolder, otherwise assume a
|
|
|
|
// SYSTEM extension that has been installed in the same prefix as the shell
|
|
|
|
let localeDir = extension.dir.get_child('locale');
|
|
|
|
if (localeDir.query_exists(null))
|
|
|
|
Gettext.bindtextdomain(domain, localeDir.get_path());
|
|
|
|
else
|
|
|
|
Gettext.bindtextdomain(domain, Config.LOCALEDIR);
|
2020-04-12 00:45:55 +00:00
|
|
|
|
|
|
|
Object.assign(extension, Gettext.domain(domain));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translate @str using the extension's gettext domain
|
|
|
|
*
|
2023-07-10 02:43:14 +00:00
|
|
|
* @param {string} str - the string to translate
|
2020-04-12 00:45:55 +00:00
|
|
|
*
|
2023-07-10 02:43:14 +00:00
|
|
|
* @returns {string} - the translated string
|
2020-04-12 00:45:55 +00:00
|
|
|
*/
|
|
|
|
function gettext(str) {
|
|
|
|
return callExtensionGettextFunc('gettext', str);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-07-10 02:43:14 +00:00
|
|
|
* Translate @str and choose plural form using the extension's
|
|
|
|
* gettext domain
|
|
|
|
*
|
2020-04-12 00:45:55 +00:00
|
|
|
* @param {string} str - the string to translate
|
|
|
|
* @param {string} strPlural - the plural form of the string
|
|
|
|
* @param {number} n - the quantity for which translation is needed
|
|
|
|
*
|
|
|
|
* @returns {string} - the translated string
|
|
|
|
*/
|
|
|
|
function ngettext(str, strPlural, n) {
|
|
|
|
return callExtensionGettextFunc('ngettext', str, strPlural, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translate @str in the context of @context using the extension's
|
|
|
|
* gettext domain
|
|
|
|
*
|
2023-07-10 02:43:14 +00:00
|
|
|
* @param {string} context - context to disambiguate @str
|
|
|
|
* @param {string} str - the string to translate
|
2020-04-12 00:45:55 +00:00
|
|
|
*
|
2023-07-10 02:43:14 +00:00
|
|
|
* @returns {string} - the translated string
|
2020-04-12 00:45:55 +00:00
|
|
|
*/
|
|
|
|
function pgettext(context, str) {
|
|
|
|
return callExtensionGettextFunc('pgettext', context, str);
|
|
|
|
}
|
|
|
|
|
2023-07-10 02:43:14 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @param {string} func - function name
|
|
|
|
* @param {*[]} args - function arguments
|
|
|
|
*/
|
2020-04-12 00:45:55 +00:00
|
|
|
function callExtensionGettextFunc(func, ...args) {
|
|
|
|
const extension = getCurrentExtension();
|
|
|
|
|
|
|
|
if (!extension)
|
|
|
|
throw new Error(`${func}() can only be called from extensions`);
|
|
|
|
|
|
|
|
if (!extension[func])
|
|
|
|
throw new Error(`${func}() is used without calling initTranslations() first`);
|
|
|
|
|
|
|
|
return extension[func](...args);
|
2018-07-14 18:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds and returns a GSettings schema for @schema, using schema files
|
|
|
|
* in extensionsdir/schemas. If @schema is omitted, it is taken from
|
|
|
|
* metadata['settings-schema'].
|
2023-07-10 02:43:14 +00:00
|
|
|
*
|
|
|
|
* @param {string=} schema - the GSettings schema id
|
|
|
|
* @returns {Gio.Settings} - a new settings object for @schema
|
2018-07-14 18:16:13 +00:00
|
|
|
*/
|
|
|
|
function getSettings(schema) {
|
|
|
|
let extension = getCurrentExtension();
|
|
|
|
|
|
|
|
if (!extension)
|
|
|
|
throw new Error('getSettings() can only be called from extensions');
|
|
|
|
|
2022-01-18 20:02:04 +00:00
|
|
|
schema ||= extension.metadata['settings-schema'];
|
2018-07-14 18:16:13 +00:00
|
|
|
|
|
|
|
const GioSSS = Gio.SettingsSchemaSource;
|
|
|
|
|
|
|
|
// Expect USER extensions to have a schemas/ subfolder, otherwise assume a
|
|
|
|
// SYSTEM extension that has been installed in the same prefix as the shell
|
|
|
|
let schemaDir = extension.dir.get_child('schemas');
|
|
|
|
let schemaSource;
|
2019-08-20 00:51:42 +00:00
|
|
|
if (schemaDir.query_exists(null)) {
|
2023-07-10 02:43:14 +00:00
|
|
|
schemaSource = GioSSS.new_from_directory(
|
|
|
|
schemaDir.get_path(), GioSSS.get_default(), false);
|
2019-08-20 00:51:42 +00:00
|
|
|
} else {
|
2018-07-14 18:16:13 +00:00
|
|
|
schemaSource = GioSSS.get_default();
|
2019-08-20 00:51:42 +00:00
|
|
|
}
|
2018-07-14 18:16:13 +00:00
|
|
|
|
|
|
|
let schemaObj = schemaSource.lookup(schema, true);
|
|
|
|
if (!schemaObj)
|
|
|
|
throw new Error(`Schema ${schema} could not be found for extension ${extension.metadata.uuid}. Please check your installation`);
|
|
|
|
|
2023-07-10 02:43:14 +00:00
|
|
|
return new Gio.Settings({settings_schema: schemaObj});
|
2018-07-14 18:16:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 19:08:38 +00:00
|
|
|
/**
|
|
|
|
* Open the preference dialog of the current extension
|
|
|
|
*/
|
|
|
|
function openPrefs() {
|
|
|
|
const extension = getCurrentExtension();
|
|
|
|
|
|
|
|
if (!extension)
|
|
|
|
throw new Error('openPrefs() can only be called from extensions');
|
|
|
|
|
|
|
|
try {
|
|
|
|
const extensionManager = imports.ui.main.extensionManager;
|
|
|
|
extensionManager.openExtensionPrefs(extension.uuid, '', {});
|
|
|
|
} catch (e) {
|
|
|
|
if (e.name === 'ImportError')
|
|
|
|
throw new Error('openPrefs() cannot be called from preferences');
|
|
|
|
logError(e, 'Failed to open extension preferences');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-10 02:43:14 +00:00
|
|
|
/**
|
|
|
|
* Serialize extension into an object that can be used
|
|
|
|
* in a vardict {GLib.Variant}
|
|
|
|
*
|
|
|
|
* @param {object} extension - an extension object
|
|
|
|
* @returns {object}
|
|
|
|
*/
|
2018-11-01 12:55:17 +00:00
|
|
|
function serializeExtension(extension) {
|
2023-07-10 02:43:14 +00:00
|
|
|
let obj = {...extension.metadata};
|
2018-11-01 12:55:17 +00:00
|
|
|
|
|
|
|
SERIALIZED_PROPERTIES.forEach(prop => {
|
|
|
|
obj[prop] = extension[prop];
|
|
|
|
});
|
|
|
|
|
|
|
|
let res = {};
|
|
|
|
for (let key in obj) {
|
|
|
|
let val = obj[key];
|
|
|
|
let type;
|
|
|
|
switch (typeof val) {
|
|
|
|
case 'string':
|
|
|
|
type = 's';
|
|
|
|
break;
|
|
|
|
case 'number':
|
|
|
|
type = 'd';
|
|
|
|
break;
|
|
|
|
case 'boolean':
|
|
|
|
type = 'b';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
res[key] = GLib.Variant.new(type, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2023-07-10 02:43:14 +00:00
|
|
|
/**
|
|
|
|
* Deserialize an unpacked variant into an extension object
|
|
|
|
*
|
|
|
|
* @param {object} variant - an unpacked {GLib.Variant}
|
|
|
|
* @returns {object}
|
|
|
|
*/
|
2018-11-01 12:55:17 +00:00
|
|
|
function deserializeExtension(variant) {
|
2023-07-10 02:43:14 +00:00
|
|
|
let res = {metadata: {}};
|
2018-11-01 12:55:17 +00:00
|
|
|
for (let prop in variant) {
|
|
|
|
let val = variant[prop].unpack();
|
|
|
|
if (SERIALIZED_PROPERTIES.includes(prop))
|
|
|
|
res[prop] = val;
|
|
|
|
else
|
|
|
|
res.metadata[prop] = val;
|
|
|
|
}
|
|
|
|
// add the 2 additional properties to create a valid extension object, as createExtensionObject()
|
|
|
|
res.uuid = res.metadata.uuid;
|
|
|
|
res.dir = Gio.File.new_for_path(res.path);
|
|
|
|
return res;
|
|
|
|
}
|