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,
|
|
|
|
getSettings, initTranslations, isOutOfDate, installImporter,
|
|
|
|
serializeExtension, deserializeExtension */
|
2012-01-19 00:55:20 +00:00
|
|
|
|
|
|
|
// Common utils for the extension system and the extension
|
|
|
|
// preferences tool
|
|
|
|
|
2018-11-01 12:55:17 +00:00
|
|
|
const { Gio, GLib } = imports.gi;
|
|
|
|
|
2018-07-14 18:16:13 +00:00
|
|
|
const Gettext = imports.gettext;
|
2018-11-01 12:55:17 +00:00
|
|
|
const Lang = imports.lang;
|
2012-06-04 21:14:18 +00:00
|
|
|
|
2012-01-19 00:55:20 +00:00
|
|
|
const Config = imports.misc.config;
|
|
|
|
|
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,
|
|
|
|
|
|
|
|
// 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
|
|
|
};
|
|
|
|
|
2018-12-04 08:31:27 +00:00
|
|
|
const SERIALIZED_PROPERTIES = ['type', 'state', 'path', 'error', 'hasPrefs', 'canChange'];
|
2018-11-01 12:55:17 +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() {
|
2016-10-03 23:08:18 +00:00
|
|
|
let stack = (new Error()).stack.split('\n');
|
|
|
|
let extensionStackLine;
|
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
|
|
|
|
for (let i = 1; i < stack.length; i++) {
|
2018-07-14 20:56:22 +00:00
|
|
|
if (stack[i].includes('/gnome-shell/extensions/')) {
|
2016-10-03 23:08:18 +00:00
|
|
|
extensionStackLine = stack[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-01-31 01:58:29 +00:00
|
|
|
if (!extensionStackLine)
|
2016-10-03 23:08:18 +00:00
|
|
|
return null;
|
2012-01-31 01:58:29 +00:00
|
|
|
|
|
|
|
// The stack line is like:
|
|
|
|
// init([object Object])@/home/user/data/gnome-shell/extensions/u@u.id/prefs.js:8
|
|
|
|
//
|
|
|
|
// In the case that we're importing from
|
|
|
|
// module scope, the first field is blank:
|
|
|
|
// @/home/user/data/gnome-shell/extensions/u@u.id/prefs.js:8
|
|
|
|
let match = new RegExp('@(.+):\\d+').exec(extensionStackLine);
|
|
|
|
if (!match)
|
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
|
|
|
|
// as well (not this function though)
|
|
|
|
let extensionManager = imports.ui.main.extensionManager;
|
|
|
|
|
2012-01-31 01:58:29 +00:00
|
|
|
let path = match[1];
|
2012-05-28 21:21:02 +00:00
|
|
|
let file = Gio.File.new_for_path(path);
|
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.
|
|
|
|
while (file != null) {
|
2019-07-07 22:01:11 +00:00
|
|
|
let extension = extensionManager.lookup(file.get_basename());
|
2012-05-28 21:21:02 +00:00
|
|
|
if (extension !== undefined)
|
|
|
|
return extension;
|
|
|
|
file = file.get_parent();
|
|
|
|
}
|
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
|
|
|
/**
|
|
|
|
* initTranslations:
|
2019-10-17 16:41:52 +00:00
|
|
|
* @param {string=} domain - the gettext domain to use
|
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']
|
|
|
|
*/
|
|
|
|
function initTranslations(domain) {
|
|
|
|
let extension = getCurrentExtension();
|
|
|
|
|
|
|
|
if (!extension)
|
|
|
|
throw new Error('initTranslations() can only be called from extensions');
|
|
|
|
|
|
|
|
domain = domain || extension.metadata['gettext-domain'];
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getSettings:
|
2019-10-17 16:41:52 +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
|
|
|
*
|
|
|
|
* 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'].
|
|
|
|
*/
|
|
|
|
function getSettings(schema) {
|
|
|
|
let extension = getCurrentExtension();
|
|
|
|
|
|
|
|
if (!extension)
|
|
|
|
throw new Error('getSettings() can only be called from extensions');
|
|
|
|
|
|
|
|
schema = schema || extension.metadata['settings-schema'];
|
|
|
|
|
|
|
|
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)) {
|
2018-07-14 18:16:13 +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`);
|
|
|
|
|
|
|
|
return new Gio.Settings({ settings_schema: schemaObj });
|
|
|
|
}
|
|
|
|
|
2012-01-19 00:55:20 +00:00
|
|
|
/**
|
|
|
|
* versionCheck:
|
2019-10-17 16:41:52 +00:00
|
|
|
* @param {string[]} required - an array of versions we're compatible with
|
|
|
|
* @param {string} current - the version we have
|
|
|
|
* @returns {bool} - true if @current is compatible with @required
|
2012-01-19 00:55:20 +00:00
|
|
|
*
|
|
|
|
* Check if a component is compatible for an extension.
|
|
|
|
* @required is an array, and at least one version must match.
|
|
|
|
* @current must be in the format <major>.<minor>.<point>.<micro>
|
|
|
|
* <micro> is always ignored
|
|
|
|
* <point> is ignored if <minor> is even (so you can target the
|
|
|
|
* whole stable release)
|
|
|
|
* <minor> and <major> must match
|
|
|
|
* Each target version must be at least <major> and <minor>
|
|
|
|
*/
|
|
|
|
function versionCheck(required, current) {
|
|
|
|
let currentArray = current.split('.');
|
|
|
|
let major = currentArray[0];
|
|
|
|
let minor = currentArray[1];
|
|
|
|
let point = currentArray[2];
|
|
|
|
for (let i = 0; i < required.length; i++) {
|
|
|
|
let requiredArray = required[i].split('.');
|
|
|
|
if (requiredArray[0] == major &&
|
|
|
|
requiredArray[1] == minor &&
|
2018-11-19 11:26:16 +00:00
|
|
|
((requiredArray[2] === undefined && parseInt(minor) % 2 == 0) ||
|
|
|
|
requiredArray[2] == point))
|
2012-01-19 00:55:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-31 01:58:29 +00:00
|
|
|
function isOutOfDate(extension) {
|
|
|
|
if (!versionCheck(extension.metadata['shell-version'], Config.PACKAGE_VERSION))
|
2012-01-19 00:55:20 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-01 12:55:17 +00:00
|
|
|
function serializeExtension(extension) {
|
|
|
|
let obj = {};
|
|
|
|
Lang.copyProperties(extension.metadata, obj);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
function deserializeExtension(variant) {
|
|
|
|
let res = { metadata: {} };
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-01-31 01:58:29 +00:00
|
|
|
function installImporter(extension) {
|
2016-10-05 05:27:18 +00:00
|
|
|
let oldSearchPath = imports.searchPath.slice(); // make a copy
|
2016-10-17 21:05:05 +00:00
|
|
|
imports.searchPath = [extension.dir.get_parent().get_path()];
|
2016-10-05 05:27:18 +00:00
|
|
|
// importing a "subdir" creates a new importer object that doesn't affect
|
|
|
|
// the global one
|
2016-10-17 21:05:05 +00:00
|
|
|
extension.imports = imports[extension.uuid];
|
2016-10-05 05:27:18 +00:00
|
|
|
imports.searchPath = oldSearchPath;
|
2012-01-19 00:55:20 +00:00
|
|
|
}
|