change indentation to 4 spaces

This commit is contained in:
Damien Churchill 2009-10-01 10:12:56 +00:00
commit eae452dbe4

View file

@ -1,458 +1,458 @@
/* /*
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>
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option) the Free Software Foundation; either version 3, or (at your option)
any later version. any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, write to: along with this program. If not, write to:
The Free Software Foundation, Inc., The Free Software Foundation, Inc.,
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.
*/ */
(function() { (function() {
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',
width: 300, width: 300,
height: 195, height: 195,
bodyStyle: 'padding: 10px 5px;', bodyStyle: 'padding: 10px 5px;',
buttonAlign: 'right', buttonAlign: 'right',
closeAction: 'hide', closeAction: 'hide',
closable: true, closable: true,
plain: true, plain: true,
title: _('Add Connection'), title: _('Add Connection'),
iconCls: 'x-deluge-add-window-icon' iconCls: 'x-deluge-add-window-icon'
}, 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',
id: 'connectionAddForm', id: 'connectionAddForm',
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',
name: 'host', name: 'host',
anchor: '100%', anchor: '100%',
value: '' value: ''
}); });
this.portField = this.form.add({ this.portField = this.form.add({
fieldLabel: _('Port'), fieldLabel: _('Port'),
id: 'port', id: 'port',
xtype: 'uxspinner', xtype: 'uxspinner',
ctCls: 'x-form-uxspinner', ctCls: 'x-form-uxspinner',
name: 'port', name: 'port',
strategy: { strategy: {
xtype: 'number', xtype: 'number',
decimalPrecision: 0, decimalPrecision: 0,
minValue: -1, minValue: -1,
maxValue: 65535 maxValue: 65535
}, },
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',
name: 'username', name: 'username',
anchor: '100%', anchor: '100%',
value: '' value: ''
}); });
this.passwordField = this.form.add({ this.passwordField = this.form.add({
fieldLabel: _('Password'), fieldLabel: _('Password'),
anchor: '100%', anchor: '100%',
id: '_password', id: '_password',
name: '_password', name: '_password',
inputType: 'password', inputType: 'password',
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]) {
Ext.MessageBox.show({ Ext.MessageBox.show({
title: _('Error'), title: _('Error'),
msg: "Unable to add host: " + result[1], msg: "Unable to add host: " + result[1],
buttons: Ext.MessageBox.OK, buttons: Ext.MessageBox.OK,
modal: false, modal: false,
icon: Ext.MessageBox.ERROR, icon: Ext.MessageBox.ERROR,
iconCls: 'x-deluge-icon-error' iconCls: 'x-deluge-icon-error'
}); });
} else { } else {
this.fireEvent('hostadded'); this.fireEvent('hostadded');
} }
this.hide(); this.hide();
}, },
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,
bodyStyle: 'padding: 10px 5px;', bodyStyle: 'padding: 10px 5px;',
buttonAlign: 'right', buttonAlign: 'right',
closeAction: 'hide', closeAction: 'hide',
closable: true, closable: true,
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({
'hide': this.onHide, 'hide': this.onHide,
'show': this.onShow 'show': this.onShow
}); });
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({
fields: [ fields: [
{name: 'status', mapping: 3}, {name: 'status', mapping: 3},
{name: 'host', mapping: 1}, {name: 'host', mapping: 1},
{name: 'port', mapping: 2}, {name: 'port', mapping: 2},
{name: 'version', mapping: 4} {name: 'version', mapping: 4}
], ],
id: 0 id: 0
}), }),
columns: [{ columns: [{
header: _('Status'), header: _('Status'),
width: 65, width: 65,
sortable: true, sortable: true,
renderer: fplain, renderer: fplain,
dataIndex: 'status' dataIndex: 'status'
}, { }, {
id:'host', id:'host',
header: _('Host'), header: _('Host'),
width: 150, width: 150,
sortable: true, sortable: true,
renderer: hostRenderer, renderer: hostRenderer,
dataIndex: 'host' dataIndex: 'host'
}, { }, {
header: _('Version'), header: _('Version'),
width: 75, width: 75,
sortable: true, sortable: true,
renderer: fplain, renderer: fplain,
dataIndex: 'version' dataIndex: 'version'
}], }],
stripeRows: true, stripeRows: true,
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} 'selectionchange': {fn: this.onSelectionChanged, scope: this}
} }
}), }),
autoExpandColumn: 'host', autoExpandColumn: 'host',
deferredRender:false, deferredRender:false,
autoScroll:true, autoScroll:true,
margins: '0 0 0 0', margins: '0 0 0 0',
bbar: new Ext.Toolbar({ bbar: new Ext.Toolbar({
buttons: [ buttons: [
{ {
id: 'cm-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: 'cm-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, disabled: true,
scope: this scope: this
}, '->', { }, '->', {
id: 'cm-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, 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, {
success: self.onGetHostStatus, success: self.onGetHostStatus,
scope: self scope: self
}); });
}, 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();
this.addWindow.on('hostadded', this.onHostAdded, this); this.addWindow.on('hostadded', this.onHostAdded, this);
} }
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) {
this.update(this); this.update(this);
Deluge.Events.fire('disconnect'); Deluge.Events.fire('disconnect');
}, },
scope: this scope: this
}); });
} else { } else {
var id = selected.id; var id = selected.id;
Deluge.Client.web.connect(id, { Deluge.Client.web.connect(id, {
success: function(methods) { success: function(methods) {
Deluge.Client.reloadMethods(); Deluge.Client.reloadMethods();
Deluge.Client.on('connected', function(e) { Deluge.Client.on('connected', function(e) {
Deluge.Events.fire('connect'); Deluge.Events.fire('connect');
}, this, {single: true}); }, this, {single: true});
} }
}); });
if (this.running) window.clearInterval(this.running); if (this.running) window.clearInterval(this.running);
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) {
Deluge.Client.web.get_host_status(host[0], { Deluge.Client.web.get_host_status(host[0], {
success: this.onGetHostStatus, success: this.onGetHostStatus,
scope: this scope: this
}); });
}, 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();
var button = this.buttons[1], status = record.get('status'); var button = this.buttons[1], status = record.get('status');
if (this.grid.getSelectionModel().getSelected() == record) { if (this.grid.getSelectionModel().getSelected() == record) {
var status = record.get('status'); var status = record.get('status');
if (status == _('Offline')) { if (status == _('Offline')) {
if (record.get('host') == '127.0.0.1' || record.get('host') == 'localhost') { if (record.get('host') == '127.0.0.1' || record.get('host') == 'localhost') {
this.stopHostButton.enable(); this.stopHostButton.enable();
this.stopHostButton.setText(_('Start Daemon')); this.stopHostButton.setText(_('Start Daemon'));
} else { } else {
this.stopHostButton.disable(); this.stopHostButton.disable();
} }
} else { } else {
this.stopHostButton.enable(); this.stopHostButton.enable();
this.stopHostButton.setText(_('Stop Daemon')); this.stopHostButton.setText(_('Stop Daemon'));
} }
} }
}, },
onLogin: function() { onLogin: function() {
Deluge.Client.web.connected({ Deluge.Client.web.connected({
success: function(connected) { success: function(connected) {
if (connected) { if (connected) {
Deluge.Events.fire('connect'); Deluge.Events.fire('connect');
} else { } else {
this.show(); this.show();
} }
}, },
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; 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) {
Ext.MessageBox.show({ Ext.MessageBox.show({
title: _('Error'), title: _('Error'),
msg: result[1], msg: result[1],
buttons: Ext.MessageBox.OK, buttons: Ext.MessageBox.OK,
modal: false, modal: false,
icon: Ext.MessageBox.ERROR, icon: Ext.MessageBox.ERROR,
iconCls: 'x-deluge-icon-error' iconCls: 'x-deluge-icon-error'
}); });
} else { } else {
this.grid.getStore().remove(connection); this.grid.getStore().remove(connection);
} }
}, },
scope: this scope: this
}); });
}, },
onSelect: function(selModel, rowIndex, record) { onSelect: function(selModel, rowIndex, record) {
this.selectedRow = rowIndex; this.selectedRow = rowIndex;
}, },
onSelectionChanged: function(selModel) { onSelectionChanged: function(selModel) {
var record = selModel.getSelected(); var record = selModel.getSelected();
if (selModel.hasSelection()) { if (selModel.hasSelection()) {
this.removeHostButton.enable(); this.removeHostButton.enable();
this.stopHostButton.enable(); this.stopHostButton.enable();
this.stopHostButton.setText(_('Stop Daemon')); this.stopHostButton.setText(_('Stop Daemon'));
} else { } else {
this.removeHostButton.disable(); this.removeHostButton.disable();
this.stopHostButton.disable(); this.stopHostButton.disable();
} }
var button = this.buttons[1], status = record.get('status'); var button = this.buttons[1], status = record.get('status');
if (status == _('Connected')) { if (status == _('Connected')) {
button.enable(); button.enable();
button.setText(_('Disconnect')); button.setText(_('Disconnect'));
} else if (status == _('Offline')) { } else if (status == _('Offline')) {
button.disable(); button.disable();
if (record.get('host') == '127.0.0.1' || record.get('host') == 'localhost') { if (record.get('host') == '127.0.0.1' || record.get('host') == 'localhost') {
this.stopHostButton.setText(_('Start Daemon')); this.stopHostButton.setText(_('Start Daemon'));
} else { } else {
this.stopHostButton.disable(); this.stopHostButton.disable();
} }
} else { } else {
button.enable(); button.enable();
button.setText(_('Connect')); button.setText(_('Connect'));
} }
}, },
onShow: function() { onShow: function() {
if (!this.addHostButton) { if (!this.addHostButton) {
var bbar = this.grid.getBottomToolbar(); var bbar = this.grid.getBottomToolbar();
this.addHostButton = bbar.items.get('cm-add'); this.addHostButton = bbar.items.get('cm-add');
this.removeHostButton = bbar.items.get('cm-remove'); this.removeHostButton = bbar.items.get('cm-remove');
this.stopHostButton = bbar.items.get('cm-stop'); this.stopHostButton = bbar.items.get('cm-stop');
} }
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; if (!connection) return;
if (connection.get('status') == 'Offline') { if (connection.get('status') == 'Offline') {
// This means we need to start the daemon // This means we need to start the daemon
Deluge.Client.web.start_daemon(connection.get('port')); Deluge.Client.web.start_daemon(connection.get('port'));
} else { } else {
// This means we need to stop the daemon // This means we need to stop the daemon
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]) {
Ext.MessageBox.show({ Ext.MessageBox.show({
title: _('Error'), title: _('Error'),
msg: result[1], msg: result[1],
buttons: Ext.MessageBox.OK, buttons: Ext.MessageBox.OK,
modal: false, modal: false,
icon: Ext.MessageBox.ERROR, icon: Ext.MessageBox.ERROR,
iconCls: 'x-deluge-icon-error' iconCls: 'x-deluge-icon-error'
}); });
} }
} }
}); });
} }
} }
}); });
Deluge.ConnectionManager = new Ext.deluge.ConnectionManager(); Deluge.ConnectionManager = new Ext.deluge.ConnectionManager();
})(); })();