disable the remove button and the stop daemon button when no host is selected

This commit is contained in:
Damien Churchill 2009-09-23 19:10:44 +00:00
commit 68b4af839f

View file

@ -1,6 +1,6 @@
/* /*
Script: deluge-connections.js Script: deluge-connections.js
Contains all objects and functions related to the connection manager. Contains all objects and functions related to the connection manager.
Copyright: Copyright:
(C) Damien Churchill 2009 <damoxc@gmail.com> (C) Damien Churchill 2009 <damoxc@gmail.com>
@ -20,15 +20,15 @@ Copyright:
51 Franklin Street, Fifth Floor 51 Franklin Street, Fifth Floor
Boston, MA 02110-1301, USA. Boston, MA 02110-1301, USA.
In addition, as a special exception, the copyright holders give In addition, as a special exception, the copyright holders give
permission to link the code of portions of this program with the OpenSSL permission to link the code of portions of this program with the OpenSSL
library. library.
You must obey the GNU General Public License in all respects for all of You must obey the GNU General Public License in all respects for all of
the code used other than OpenSSL. If you modify file(s) with this the code used other than OpenSSL. If you modify file(s) with this
exception, you may extend this exception to your version of the file(s), exception, you may extend this exception to your version of the file(s),
but you are not obligated to do so. If you do not wish to do so, delete but you are not obligated to do so. If you do not wish to do so, delete
this exception statement from your version. If you delete this exception this exception statement from your version. If you delete this exception
statement from all source files in the program, then also delete it here. statement from all source files in the program, then also delete it here.
*/ */
@ -36,9 +36,9 @@ Copyright:
var hostRenderer = function(value, p, r) { var hostRenderer = function(value, p, r) {
return value + ':' + r.data['port'] return value + ':' + r.data['port']
} }
Ext.deluge.AddConnectionWindow = Ext.extend(Ext.Window, { Ext.deluge.AddConnectionWindow = Ext.extend(Ext.Window, {
constructor: function(config) { constructor: function(config) {
config = Ext.apply({ config = Ext.apply({
layout: 'fit', layout: 'fit',
@ -54,17 +54,17 @@ Copyright:
}, config); }, config);
Ext.deluge.AddConnectionWindow.superclass.constructor.call(this, config); Ext.deluge.AddConnectionWindow.superclass.constructor.call(this, config);
}, },
initComponent: function() { initComponent: function() {
Ext.deluge.AddConnectionWindow.superclass.initComponent.call(this); Ext.deluge.AddConnectionWindow.superclass.initComponent.call(this);
this.addEvents('hostadded'); this.addEvents('hostadded');
this.addButton(_('Close'), this.hide, this); this.addButton(_('Close'), this.hide, this);
this.addButton(_('Add'), this.onAdd, this); this.addButton(_('Add'), this.onAdd, this);
this.on('hide', this.onHide, this); this.on('hide', this.onHide, this);
this.form = this.add({ this.form = this.add({
xtype: 'form', xtype: 'form',
defaultType: 'textfield', defaultType: 'textfield',
@ -72,7 +72,7 @@ Copyright:
baseCls: 'x-plain', baseCls: 'x-plain',
labelWidth: 55 labelWidth: 55
}); });
this.hostField = this.form.add({ this.hostField = this.form.add({
fieldLabel: _('Host'), fieldLabel: _('Host'),
id: 'host', id: 'host',
@ -80,7 +80,7 @@ Copyright:
anchor: '100%', anchor: '100%',
value: '' value: ''
}); });
this.portField = this.form.add({ this.portField = this.form.add({
fieldLabel: _('Port'), fieldLabel: _('Port'),
id: 'port', id: 'port',
@ -91,7 +91,7 @@ Copyright:
value: '58846', value: '58846',
anchor: '50%' anchor: '50%'
}); });
this.usernameField = this.form.add({ this.usernameField = this.form.add({
fieldLabel: _('Username'), fieldLabel: _('Username'),
id: 'username', id: 'username',
@ -99,7 +99,7 @@ Copyright:
anchor: '100%', anchor: '100%',
value: '' value: ''
}); });
this.passwordField = this.form.add({ this.passwordField = this.form.add({
fieldLabel: _('Password'), fieldLabel: _('Password'),
anchor: '100%', anchor: '100%',
@ -109,13 +109,13 @@ Copyright:
value: '' value: ''
}); });
}, },
onAdd: function() { onAdd: function() {
var host = this.hostField.getValue(); var host = this.hostField.getValue();
var port = this.portField.getValue(); var port = this.portField.getValue();
var username = this.usernameField.getValue(); var username = this.usernameField.getValue();
var password = this.passwordField.getValue(); var password = this.passwordField.getValue();
Deluge.Client.web.add_host(host, port, username, password, { Deluge.Client.web.add_host(host, port, username, password, {
success: function(result) { success: function(result) {
if (!result[0]) { if (!result[0]) {
@ -135,14 +135,14 @@ Copyright:
scope: this scope: this
}); });
}, },
onHide: function() { onHide: function() {
this.form.getForm().reset(); this.form.getForm().reset();
} }
}); });
Ext.deluge.ConnectionManager = Ext.extend(Ext.Window, { Ext.deluge.ConnectionManager = Ext.extend(Ext.Window, {
layout: 'fit', layout: 'fit',
width: 300, width: 300,
height: 220, height: 220,
@ -153,7 +153,7 @@ Copyright:
plain: true, plain: true,
title: _('Connection Manager'), title: _('Connection Manager'),
iconCls: 'x-deluge-connect-window-icon', iconCls: 'x-deluge-connect-window-icon',
initComponent: function() { initComponent: function() {
Ext.deluge.ConnectionManager.superclass.initComponent.call(this); Ext.deluge.ConnectionManager.superclass.initComponent.call(this);
this.on({ this.on({
@ -162,10 +162,10 @@ Copyright:
}); });
Deluge.Events.on('login', this.onLogin, this); Deluge.Events.on('login', this.onLogin, this);
Deluge.Events.on('logout', this.onLogout, this); Deluge.Events.on('logout', this.onLogout, this);
this.addButton(_('Close'), this.onClose, this); this.addButton(_('Close'), this.onClose, this);
this.addButton(_('Connect'), this.onConnect, this); this.addButton(_('Connect'), this.onConnect, this);
this.grid = this.add({ this.grid = this.add({
xtype: 'grid', xtype: 'grid',
store: new Ext.data.SimpleStore({ store: new Ext.data.SimpleStore({
@ -201,7 +201,8 @@ Copyright:
selModel: new Ext.grid.RowSelectionModel({ selModel: new Ext.grid.RowSelectionModel({
singleSelect: true, singleSelect: true,
listeners: { listeners: {
'rowselect': {fn: this.onSelect, scope: this} 'rowselect': {fn: this.onSelect, scope: this},
'selectionchange': {fn: this.onSelectionChanged, scope: this}
} }
}), }),
autoExpandColumn: 'host', autoExpandColumn: 'host',
@ -209,45 +210,47 @@ Copyright:
autoScroll:true, autoScroll:true,
margins: '0 0 0 0', margins: '0 0 0 0',
bbar: new Ext.Toolbar({ bbar: new Ext.Toolbar({
items: [ buttons: [
{ {
id: 'add', id: 'cm-add',
cls: 'x-btn-text-icon', cls: 'x-btn-text-icon',
text: _('Add'), text: _('Add'),
icon: '/icons/add.png', icon: '/icons/add.png',
handler: this.onAdd, handler: this.onAdd,
scope: this scope: this
}, { }, {
id: 'remove', id: 'cm-remove',
cls: 'x-btn-text-icon', cls: 'x-btn-text-icon',
text: _('Remove'), text: _('Remove'),
icon: '/icons/remove.png', icon: '/icons/remove.png',
handler: this.onRemove, handler: this.onRemove,
disabled: true,
scope: this scope: this
}, '->', { }, '->', {
id: 'stop', id: 'cm-stop',
cls: 'x-btn-text-icon', cls: 'x-btn-text-icon',
text: _('Stop Daemon'), text: _('Stop Daemon'),
icon: '/icons/error.png', icon: '/icons/error.png',
handler: this.onStop, handler: this.onStop,
disabled: true,
scope: this scope: this
} }
] ]
}) })
}); });
}, },
disconnect: function() { disconnect: function() {
Deluge.Events.fire('disconnect'); Deluge.Events.fire('disconnect');
}, },
loadHosts: function() { loadHosts: function() {
Deluge.Client.web.get_hosts({ Deluge.Client.web.get_hosts({
success: this.onGetHosts, success: this.onGetHosts,
scope: this scope: this
}); });
}, },
update: function(self) { update: function(self) {
self.grid.getStore().each(function(r) { self.grid.getStore().each(function(r) {
Deluge.Client.web.get_host_status(r.id, { Deluge.Client.web.get_host_status(r.id, {
@ -256,7 +259,7 @@ Copyright:
}); });
}, this); }, this);
}, },
onAdd: function(button, e) { onAdd: function(button, e) {
if (!this.addWindow) { if (!this.addWindow) {
this.addWindow = new Ext.deluge.AddConnectionWindow(); this.addWindow = new Ext.deluge.AddConnectionWindow();
@ -264,20 +267,20 @@ Copyright:
} }
this.addWindow.show(); this.addWindow.show();
}, },
onHostAdded: function() { onHostAdded: function() {
this.update(this); this.update(this);
}, },
onClose: function(e) { onClose: function(e) {
if (this.running) window.clearInterval(this.running); if (this.running) window.clearInterval(this.running);
this.hide(); this.hide();
}, },
onConnect: function(e) { onConnect: function(e) {
var selected = this.grid.getSelectionModel().getSelected(); var selected = this.grid.getSelectionModel().getSelected();
if (!selected) return; if (!selected) return;
if (selected.get('status') == _('Connected')) { if (selected.get('status') == _('Connected')) {
Deluge.Client.web.disconnect({ Deluge.Client.web.disconnect({
success: function(result) { success: function(result) {
@ -300,7 +303,7 @@ Copyright:
this.hide(); this.hide();
} }
}, },
onGetHosts: function(hosts) { onGetHosts: function(hosts) {
this.grid.getStore().loadData(hosts); this.grid.getStore().loadData(hosts);
Ext.each(hosts, function(host) { Ext.each(hosts, function(host) {
@ -310,14 +313,14 @@ Copyright:
}); });
}, this); }, this);
}, },
onGetHostStatus: function(host) { onGetHostStatus: function(host) {
var record = this.grid.getStore().getById(host[0]); var record = this.grid.getStore().getById(host[0]);
record.set('status', host[3]) record.set('status', host[3])
record.set('version', host[4]) record.set('version', host[4])
record.commit(); record.commit();
}, },
onLogin: function() { onLogin: function() {
Deluge.Client.web.connected({ Deluge.Client.web.connected({
success: function(connected) { success: function(connected) {
@ -330,16 +333,18 @@ Copyright:
scope: this scope: this
}); });
}, },
onLogout: function() { onLogout: function() {
this.disconnect(); this.disconnect();
if (!this.hidden && this.rendered) { if (!this.hidden && this.rendered) {
this.hide(); this.hide();
} }
}, },
onRemove: function(button) { onRemove: function(button) {
var connection = this.grid.getSelectionModel().getSelected(); var connection = this.grid.getSelectionModel().getSelected();
if (!connection) return;
Deluge.Client.web.remove_host(connection.id, { Deluge.Client.web.remove_host(connection.id, {
success: function(result) { success: function(result) {
if (!result) { if (!result) {
@ -358,7 +363,7 @@ Copyright:
scope: this scope: this
}); });
}, },
onSelect: function(selModel, rowIndex, record) { onSelect: function(selModel, rowIndex, record) {
this.selectedRow = rowIndex; this.selectedRow = rowIndex;
var button = this.buttons[1]; var button = this.buttons[1];
@ -368,14 +373,32 @@ Copyright:
button.setText(_('Connect')); button.setText(_('Connect'));
} }
}, },
onSelectionChanged: function(selModel) {
if (!this.addHostButton) {
var bbar = this.grid.getBottomToolbar();
this.addHostButton = bbar.items.get('cm-add');
this.removeHostButton = bbar.items.get('cm-remove');
this.stopHostButton = bbar.items.get('cm-stop');
}
if (selModel.hasSelection()) {
this.removeHostButton.enable();
this.stopHostButton.enable();
} else {
this.removeHostButton.disable();
this.stopHostButton.disable();
}
},
onShow: function() { onShow: function() {
this.loadHosts(); this.loadHosts();
this.running = window.setInterval(this.update, 2000, this); this.running = window.setInterval(this.update, 2000, this);
}, },
onStop: function(button, e) { onStop: function(button, e) {
var connection = this.grid.getSelectionModel().getSelected(); var connection = this.grid.getSelectionModel().getSelected();
if (!connection) return;
Deluge.Client.web.stop_daemon(connection.id, { Deluge.Client.web.stop_daemon(connection.id, {
success: function(result) { success: function(result) {
if (!result[0]) { if (!result[0]) {
@ -392,5 +415,5 @@ Copyright:
}); });
} }
}); });
Deluge.ConnectionManager = new Ext.deluge.ConnectionManager(); Deluge.ConnectionManager = new Ext.deluge.ConnectionManager();
})(); })();