create a plugin registration system to allow for plugin loading on the fly

remove the plugin event handlers from the event manager
fix enabling/disabling plugins when the preferences page hasn't been rendered yet
This commit is contained in:
Damien Churchill 2010-04-24 01:40:34 +01:00
commit 7e12222d33
4 changed files with 48 additions and 11 deletions

View file

@ -88,6 +88,9 @@ Ext.USE_NATIVE_JSON = true;
// Create the Deluge namespace // Create the Deluge namespace
Ext.apply(Deluge, { Ext.apply(Deluge, {
// private
pluginStore: {},
// private // private
progressTpl: '<div class="x-progress-wrap x-progress-renderered">' + progressTpl: '<div class="x-progress-wrap x-progress-renderered">' +
'<div class="x-progress-inner">' + '<div class="x-progress-inner">' +
@ -117,6 +120,31 @@ Ext.apply(Deluge, {
var barWidth = progressWidth - 1; var barWidth = progressWidth - 1;
var textWidth = ((progressWidth - modifier) > 0 ? progressWidth - modifier : 0); var textWidth = ((progressWidth - modifier) > 0 ? progressWidth - modifier : 0);
return String.format(Deluge.progressTpl, text, width, barWidth, textWidth); return String.format(Deluge.progressTpl, text, width, barWidth, textWidth);
},
/**
* Constructs a new instance of the specified plugin.
* @param {String} name The plugin name to create
*/
createPlugin: function(name) {
return new Deluge.pluginStore[name]();
},
/**
* Check to see if a plugin has been registered.
* @param {String} name The plugin name to check
*/
hasPlugin: function(name) {
return (Deluge.pluginStore[name]) ? true : false;
},
/**
* Register a plugin with the Deluge interface.
* @param {String} name The plugin name to register
* @param {Plugin} plugin The plugin to register
*/
registerPlugin: function(name, plugin) {
Deluge.pluginStore[name] = plugin;
} }
}); });

View file

@ -88,8 +88,6 @@ Deluge.EventsManager = Ext.extend(Ext.util.Observable, {
// private // private
onLogin: function() { onLogin: function() {
this.start(); this.start();
this.on('PluginEnabledEvent', this.onPluginEnabled, this);
this.on('PluginDisabledEvent', this.onPluginDisabled, this);
}, },
onGetEventsSuccess: function(events) { onGetEventsSuccess: function(events) {

View file

@ -81,8 +81,9 @@ deluge.ui = {
url: deluge.config.base + 'json' url: deluge.config.base + 'json'
}); });
for (var plugin in Deluge.plugins) { // enable all the already active plugins
plugin = new Deluge.plugins[plugin](); for (var plugin in Deluge.pluginStore) {
plugin = Deluge.createPlugin(plugin);
plugin.enable(); plugin.enable();
deluge.plugins[plugin.name] = plugin; deluge.plugins[plugin.name] = plugin;
} }
@ -161,14 +162,19 @@ deluge.ui = {
}, },
onPluginEnabled: function(pluginName) { onPluginEnabled: function(pluginName) {
deluge.client.web.get_plugin_resources(pluginName, { alert('enabled ' + pluginName);
success: this.onGotPluginResources, if (deluge.plugins[pluginName]) {
scope: this deluge.plugins[pluginName].enable();
}) } else {
deluge.client.web.get_plugin_resources(pluginName, {
success: this.onGotPluginResources,
scope: this
});
}
}, },
onGotPluginResources: function(resources) { onGotPluginResources: function(resources) {
var scripts = (deluge.debug) ? resources.debug_scripts : resources.scripts; var scripts = (Deluge.debug) ? resources.debug_scripts : resources.scripts;
Ext.each(scripts, function(script) { Ext.each(scripts, function(script) {
Ext.ux.JSLoader({ Ext.ux.JSLoader({
url: script, url: script,
@ -179,15 +185,18 @@ deluge.ui = {
}, },
onPluginDisabled: function(pluginName) { onPluginDisabled: function(pluginName) {
alert('disabled ' + pluginName);
deluge.plugins[pluginName].disable(); deluge.plugins[pluginName].disable();
}, },
onPluginLoaded: function(options) { onPluginLoaded: function(options) {
// This could happen if the plugin has multiple scripts // This could happen if the plugin has multiple scripts
if (!deluge.plugins[options.pluginName]) return; if (!Deluge.hasPlugin(options.pluginName)) return;
// Enable the plugin // Enable the plugin
deluge.plugins[options.pluginName].enable(); plugin = Deluge.createPlugin(options.pluginName);
plugin.enable();
deluge.plugins[plugin.name] = plugin;
}, },
/** /**

View file

@ -310,6 +310,7 @@ Deluge.preferences.Plugins = Ext.extend(Ext.Panel, {
onPluginEnabled: function(pluginName) { onPluginEnabled: function(pluginName) {
var index = this.grid.getStore().find('plugin', pluginName); var index = this.grid.getStore().find('plugin', pluginName);
if (index == -1) return;
var plugin = this.grid.getStore().getAt(index); var plugin = this.grid.getStore().getAt(index);
plugin.set('enabled', true); plugin.set('enabled', true);
plugin.commit(); plugin.commit();
@ -317,6 +318,7 @@ Deluge.preferences.Plugins = Ext.extend(Ext.Panel, {
onPluginDisabled: function(pluginName) { onPluginDisabled: function(pluginName) {
var index = this.grid.getStore().find('plugin', pluginName); var index = this.grid.getStore().find('plugin', pluginName);
if (index == -1) return;
var plugin = this.grid.getStore().getAt(index); var plugin = this.grid.getStore().getAt(index);
plugin.set('enabled', false); plugin.set('enabled', false);
plugin.commit(); plugin.commit();