2011-09-28 09:16:26 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2009-10-25 18:53:10 -04:00
|
|
|
|
2011-06-11 00:21:35 -04:00
|
|
|
const Lang = imports.lang;
|
|
|
|
const Signals = imports.signals;
|
|
|
|
|
2009-10-25 18:53:10 -04:00
|
|
|
const GLib = imports.gi.GLib;
|
|
|
|
const Gio = imports.gi.Gio;
|
|
|
|
const St = imports.gi.St;
|
|
|
|
|
2012-01-18 19:55:20 -05:00
|
|
|
const ExtensionUtils = imports.misc.extensionUtils;
|
2011-09-12 14:16:03 -04:00
|
|
|
|
2009-10-25 18:53:10 -04:00
|
|
|
const ExtensionState = {
|
|
|
|
ENABLED: 1,
|
|
|
|
DISABLED: 2,
|
|
|
|
ERROR: 3,
|
2011-06-22 21:56:24 -04:00
|
|
|
OUT_OF_DATE: 4,
|
2011-06-27 22:27:56 -04:00
|
|
|
DOWNLOADING: 5,
|
2011-10-25 14:38:25 -04:00
|
|
|
INITIALIZED: 6,
|
2011-06-22 21:56:24 -04:00
|
|
|
|
|
|
|
// Used as an error state for operations on unknown extensions,
|
|
|
|
// should never be in a real extensionMeta object.
|
|
|
|
UNINSTALLED: 99
|
2009-10-25 18:53:10 -04:00
|
|
|
};
|
|
|
|
|
2011-05-25 15:34:33 -04:00
|
|
|
// Arrays of uuids
|
|
|
|
var enabledExtensions;
|
2012-01-30 20:58:29 -05:00
|
|
|
// Contains the order that extensions were enabled in.
|
|
|
|
const extensionOrder = [];
|
2009-10-25 18:53:10 -04:00
|
|
|
|
2011-06-11 00:21:35 -04:00
|
|
|
// We don't really have a class to add signals on. So, create
|
|
|
|
// a simple dummy object, add the signal methods, and export those
|
|
|
|
// publically.
|
|
|
|
var _signals = {};
|
|
|
|
Signals.addSignalMethods(_signals);
|
|
|
|
|
|
|
|
const connect = Lang.bind(_signals, _signals.connect);
|
|
|
|
const disconnect = Lang.bind(_signals, _signals.disconnect);
|
|
|
|
|
2011-08-18 07:56:22 -04:00
|
|
|
const ENABLED_EXTENSIONS_KEY = 'enabled-extensions';
|
|
|
|
|
2011-06-22 21:56:24 -04:00
|
|
|
function disableExtension(uuid) {
|
2012-01-30 20:58:29 -05:00
|
|
|
let extension = ExtensionUtils.extensions[uuid];
|
|
|
|
if (!extension)
|
2011-06-22 21:56:24 -04:00
|
|
|
return;
|
|
|
|
|
2012-01-30 20:58:29 -05:00
|
|
|
if (extension.state != ExtensionState.ENABLED)
|
2011-06-22 21:56:24 -04:00
|
|
|
return;
|
|
|
|
|
2011-10-14 18:06:03 -04:00
|
|
|
// "Rebase" the extension order by disabling and then enabling extensions
|
|
|
|
// in order to help prevent conflicts.
|
|
|
|
|
|
|
|
// Example:
|
|
|
|
// order = [A, B, C, D, E]
|
|
|
|
// user disables C
|
|
|
|
// this should: disable E, disable D, disable C, enable D, enable E
|
|
|
|
|
|
|
|
let orderIdx = extensionOrder.indexOf(uuid);
|
|
|
|
let order = extensionOrder.slice(orderIdx + 1);
|
|
|
|
let orderReversed = order.slice().reverse();
|
|
|
|
|
|
|
|
for (let i = 0; i < orderReversed.length; i++) {
|
|
|
|
let uuid = orderReversed[i];
|
|
|
|
try {
|
2012-02-08 12:48:05 -05:00
|
|
|
ExtensionUtils.extensions[uuid].stateObj.disable();
|
2011-10-14 18:06:03 -04:00
|
|
|
} catch(e) {
|
2012-06-28 22:00:45 -04:00
|
|
|
logExtensionError(uuid, e);
|
2011-10-14 18:06:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-04 18:42:42 -04:00
|
|
|
if (extension.stylesheet) {
|
|
|
|
let theme = St.ThemeContext.get_for_stage(global.stage).get_theme();
|
|
|
|
theme.unload_stylesheet(extension.stylesheet.get_path());
|
|
|
|
}
|
|
|
|
|
2012-06-28 22:00:45 -04:00
|
|
|
extension.stateObj.disable();
|
2011-06-22 21:56:24 -04:00
|
|
|
|
2011-10-14 18:06:03 -04:00
|
|
|
for (let i = 0; i < order.length; i++) {
|
|
|
|
let uuid = order[i];
|
|
|
|
try {
|
2012-02-08 12:48:05 -05:00
|
|
|
ExtensionUtils.extensions[uuid].stateObj.enable();
|
2011-10-14 18:06:03 -04:00
|
|
|
} catch(e) {
|
2012-06-28 22:00:45 -04:00
|
|
|
logExtensionError(uuid, e);
|
2011-10-14 18:06:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-25 14:43:51 -04:00
|
|
|
extensionOrder.splice(orderIdx, 1);
|
|
|
|
|
2012-01-30 20:58:29 -05:00
|
|
|
extension.state = ExtensionState.DISABLED;
|
|
|
|
_signals.emit('extension-state-changed', extension);
|
2011-06-22 21:56:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function enableExtension(uuid) {
|
2012-01-30 20:58:29 -05:00
|
|
|
let extension = ExtensionUtils.extensions[uuid];
|
|
|
|
if (!extension)
|
2011-10-25 14:38:25 -04:00
|
|
|
return;
|
|
|
|
|
2012-02-08 12:48:05 -05:00
|
|
|
if (extension.state == ExtensionState.INITIALIZED)
|
|
|
|
initExtension(uuid);
|
2011-06-22 21:56:24 -04:00
|
|
|
|
2012-01-30 20:58:29 -05:00
|
|
|
if (extension.state != ExtensionState.DISABLED)
|
2011-06-22 21:56:24 -04:00
|
|
|
return;
|
|
|
|
|
2011-10-14 18:06:03 -04:00
|
|
|
extensionOrder.push(uuid);
|
|
|
|
|
2012-06-28 22:00:45 -04:00
|
|
|
extension.stateObj.enable();
|
2011-06-22 21:56:24 -04:00
|
|
|
|
2012-06-04 18:42:42 -04:00
|
|
|
let stylesheetFile = extension.dir.get_child('stylesheet.css');
|
|
|
|
if (stylesheetFile.query_exists(null)) {
|
|
|
|
let theme = St.ThemeContext.get_for_stage(global.stage).get_theme();
|
2012-06-28 22:00:45 -04:00
|
|
|
theme.load_stylesheet(stylesheetFile.get_path());
|
|
|
|
extension.stylesheet = stylesheetFile;
|
2012-06-04 18:42:42 -04:00
|
|
|
}
|
|
|
|
|
2012-01-30 20:58:29 -05:00
|
|
|
extension.state = ExtensionState.ENABLED;
|
|
|
|
_signals.emit('extension-state-changed', extension);
|
2011-06-22 21:56:24 -04:00
|
|
|
}
|
|
|
|
|
2012-06-28 22:00:45 -04:00
|
|
|
function logExtensionError(uuid, error) {
|
2012-01-30 20:58:29 -05:00
|
|
|
let extension = ExtensionUtils.extensions[uuid];
|
|
|
|
if (!extension)
|
|
|
|
return;
|
|
|
|
|
2012-06-28 22:00:45 -04:00
|
|
|
let message = '' + error;
|
|
|
|
|
|
|
|
if (error.state)
|
|
|
|
extension.state = error.state;
|
|
|
|
else
|
|
|
|
extension.state = ExtensionState.ERROR;
|
|
|
|
|
2012-01-30 20:58:29 -05:00
|
|
|
if (!extension.errors)
|
|
|
|
extension.errors = [];
|
|
|
|
|
2012-05-09 21:37:42 -04:00
|
|
|
log('Extension "%s" had error: %s'.format(uuid, message));
|
2011-06-27 22:21:02 -04:00
|
|
|
_signals.emit('extension-state-changed', { uuid: uuid,
|
|
|
|
error: message,
|
2012-06-28 22:00:45 -04:00
|
|
|
state: extension.state });
|
2011-06-22 21:41:30 -04:00
|
|
|
}
|
|
|
|
|
2012-06-04 18:25:51 -04:00
|
|
|
function loadExtension(extension) {
|
2009-10-25 18:53:10 -04:00
|
|
|
// Default to error, we set success as the last step
|
2012-01-30 20:58:29 -05:00
|
|
|
extension.state = ExtensionState.ERROR;
|
2009-10-25 18:53:10 -04:00
|
|
|
|
2012-01-30 20:58:29 -05:00
|
|
|
if (ExtensionUtils.isOutOfDate(extension)) {
|
2012-06-28 22:00:45 -04:00
|
|
|
let error = new Error('extension is not compatible with current GNOME Shell and/or GJS version');
|
|
|
|
error.state = ExtensionState.OUT_OF_DATE;
|
|
|
|
throw error;
|
2011-07-05 14:40:48 -04:00
|
|
|
}
|
|
|
|
|
2012-06-04 18:25:51 -04:00
|
|
|
let enabled = enabledExtensions.indexOf(extension.uuid) != -1;
|
2012-02-08 12:48:05 -05:00
|
|
|
if (enabled) {
|
2012-06-04 18:25:51 -04:00
|
|
|
initExtension(extension.uuid);
|
2012-02-08 12:48:05 -05:00
|
|
|
if (extension.state == ExtensionState.DISABLED)
|
2012-06-04 18:25:51 -04:00
|
|
|
enableExtension(extension.uuid);
|
2012-02-08 12:48:05 -05:00
|
|
|
} else {
|
2012-01-30 20:58:29 -05:00
|
|
|
extension.state = ExtensionState.INITIALIZED;
|
2011-10-25 14:38:25 -04:00
|
|
|
}
|
|
|
|
|
2012-02-08 12:48:05 -05:00
|
|
|
_signals.emit('extension-state-changed', extension);
|
|
|
|
}
|
|
|
|
|
2012-06-05 13:03:42 -04:00
|
|
|
function unloadExtension(uuid) {
|
|
|
|
let extension = ExtensionUtils.extensions[uuid];
|
|
|
|
if (!extension)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Try to disable it -- if it's ERROR'd, we can't guarantee that,
|
|
|
|
// but it will be removed on next reboot, and hopefully nothing
|
|
|
|
// broke too much.
|
|
|
|
disableExtension(uuid);
|
|
|
|
|
|
|
|
extension.state = ExtensionState.UNINSTALLED;
|
|
|
|
_signals.emit('extension-state-changed', extension);
|
|
|
|
|
|
|
|
delete ExtensionUtils.extensions[uuid];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-08 12:48:05 -05:00
|
|
|
function initExtension(uuid) {
|
|
|
|
let extension = ExtensionUtils.extensions[uuid];
|
|
|
|
let dir = extension.dir;
|
|
|
|
|
|
|
|
if (!extension)
|
|
|
|
throw new Error("Extension was not properly created. Call loadExtension first");
|
|
|
|
|
2009-10-25 18:53:10 -04:00
|
|
|
let extensionJs = dir.get_child('extension.js');
|
2012-06-28 22:00:45 -04:00
|
|
|
if (!extensionJs.query_exists(null))
|
|
|
|
throw new Error('Missing extension.js');
|
2009-10-25 18:53:10 -04:00
|
|
|
|
|
|
|
let extensionModule;
|
2011-06-22 21:56:24 -04:00
|
|
|
let extensionState = null;
|
2012-06-28 22:00:45 -04:00
|
|
|
|
|
|
|
ExtensionUtils.installImporter(extension);
|
|
|
|
extensionModule = extension.imports.extension;
|
2011-06-22 21:56:24 -04:00
|
|
|
|
2012-06-04 18:43:52 -04:00
|
|
|
if (extensionModule.init) {
|
2012-06-28 22:00:45 -04:00
|
|
|
extensionState = extensionModule.init(extension);
|
2009-10-25 18:53:10 -04:00
|
|
|
}
|
2011-06-22 21:56:24 -04:00
|
|
|
|
|
|
|
if (!extensionState)
|
|
|
|
extensionState = extensionModule;
|
2012-01-30 20:58:29 -05:00
|
|
|
extension.stateObj = extensionState;
|
2011-06-22 21:56:24 -04:00
|
|
|
|
2012-01-30 20:58:29 -05:00
|
|
|
extension.state = ExtensionState.DISABLED;
|
|
|
|
_signals.emit('extension-loaded', uuid);
|
2009-10-25 18:53:10 -04:00
|
|
|
}
|
|
|
|
|
2011-06-22 21:56:24 -04:00
|
|
|
function onEnabledExtensionsChanged() {
|
|
|
|
let newEnabledExtensions = global.settings.get_strv(ENABLED_EXTENSIONS_KEY);
|
|
|
|
|
|
|
|
// Find and enable all the newly enabled extensions: UUIDs found in the
|
|
|
|
// new setting, but not in the old one.
|
|
|
|
newEnabledExtensions.filter(function(uuid) {
|
|
|
|
return enabledExtensions.indexOf(uuid) == -1;
|
|
|
|
}).forEach(function(uuid) {
|
2012-06-28 22:00:45 -04:00
|
|
|
try {
|
|
|
|
enableExtension(uuid);
|
|
|
|
} catch(e) {
|
2012-07-11 22:46:59 -04:00
|
|
|
logExtensionError(uuid, e);
|
2012-06-28 22:00:45 -04:00
|
|
|
}
|
2011-06-22 21:56:24 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// Find and disable all the newly disabled extensions: UUIDs found in the
|
|
|
|
// old setting, but not in the new one.
|
|
|
|
enabledExtensions.filter(function(item) {
|
|
|
|
return newEnabledExtensions.indexOf(item) == -1;
|
|
|
|
}).forEach(function(uuid) {
|
2012-06-28 22:00:45 -04:00
|
|
|
try {
|
|
|
|
disableExtension(uuid);
|
|
|
|
} catch(e) {
|
2012-07-11 22:46:59 -04:00
|
|
|
logExtensionError(uuid, e);
|
2012-06-28 22:00:45 -04:00
|
|
|
}
|
2011-06-22 21:56:24 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
enabledExtensions = newEnabledExtensions;
|
|
|
|
}
|
|
|
|
|
2012-06-04 18:46:27 -04:00
|
|
|
function loadExtensions() {
|
2011-06-22 21:56:24 -04:00
|
|
|
global.settings.connect('changed::' + ENABLED_EXTENSIONS_KEY, onEnabledExtensionsChanged);
|
2011-08-18 07:56:22 -04:00
|
|
|
enabledExtensions = global.settings.get_strv(ENABLED_EXTENSIONS_KEY);
|
2009-10-25 18:53:10 -04:00
|
|
|
|
2012-06-04 17:14:18 -04:00
|
|
|
let finder = new ExtensionUtils.ExtensionFinder();
|
2012-06-04 18:25:51 -04:00
|
|
|
finder.connect('extension-found', function(signals, extension) {
|
2012-06-28 22:00:45 -04:00
|
|
|
try {
|
|
|
|
loadExtension(extension);
|
|
|
|
} catch(e) {
|
|
|
|
logExtensionError(extension.uuid, e);
|
|
|
|
}
|
2012-01-18 19:55:20 -05:00
|
|
|
});
|
2012-06-04 17:14:18 -04:00
|
|
|
finder.scanExtensions();
|
2009-10-25 18:53:10 -04:00
|
|
|
}
|