      1 /*
      2 Script: deluge-connections.js
      3     Contains all objects and functions related to the connection manager.
      4
      5 Copyright:
      6 	(C) Damien Churchill 2009 <damoxc@gmail.com>
      7 	This program is free software; you can redistribute it and/or modify
      8 	it under the terms of the GNU General Public License as published by
      9 	the Free Software Foundation; either version 3, or (at your option)
     10 	any later version.
     11
     12 	This program is distributed in the hope that it will be useful,
     13 	but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 	GNU General Public License for more details.
     16
     17 	You should have received a copy of the GNU General Public License
     18 	along with this program.  If not, write to:
     19 		The Free Software Foundation, Inc.,
     20 		51 Franklin Street, Fifth Floor
     21 		Boston, MA  02110-1301, USA.
     22 #
     23 #    In addition, as a special exception, the copyright holders give
     24 #    permission to link the code of portions of this program with the OpenSSL
     25 #    library.
     26 #    You must obey the GNU General Public License in all respects for all of
     27 #    the code used other than OpenSSL. If you modify file(s) with this
     28 #    exception, you may extend this exception to your version of the file(s),
     29 #    but you are not obligated to do so. If you do not wish to do so, delete
     30 #    this exception statement from your version. If you delete this exception
     31 #    statement from all source files in the program, then also delete it here.
     32 #
     33 */
     34
     35 (function() {
     36 	var hostRenderer = function(value, p, r) {
     37 		return value + ':' + r.data['port']
     38 	}
     39
     40 	Ext.deluge.ConnectionManager = Ext.extend(Ext.Window, {
     41
     42 		layout: 'fit',
     43 		width: 300,
     44 		height: 220,
     45 		bodyStyle: 'padding: 10px 5px;',
     46 		buttonAlign: 'right',
     47 		closeAction: 'hide',
     48 		closable: true,
     49 		plain: true,
     50 		title: _('Connection Manager'),
     51 		iconCls: 'x-deluge-connect-window-icon',
     52
     53 		initComponent: function() {
     54 			Ext.deluge.ConnectionManager.superclass.initComponent.call(this);
     55 			this.on({
     56 				'hide': this.onHide,
     57 				'show': this.onShow
     58 			});
     59 			Deluge.Events.on('login', this.onLogin, this);
     60 			Deluge.Events.on('logout', this.onLogout, this);
     61
     62 			this.addButton(_('Close'), this.onClose, this);
     63 			this.addButton(_('Connect'), this.onConnect, this);
     64
     65 			this.grid = this.add({
     66 				xtype: 'grid',
     67 				store: new Ext.data.SimpleStore({
     68 					fields: [
     69 						{name: 'status', mapping: 3},
     70 						{name: 'host', mapping: 1},
     71 						{name: 'port', mapping: 2},
     72 						{name: 'version', mapping: 4}
     73 					],
     74 					id: 0
     75 				}),
     76 				columns: [{
     77 					header: _('Status'),
     78 					width: 65,
     79 					sortable: true,
     80 					renderer: fplain,
     81 					dataIndex: 'status'
     82 				}, {
     83 					id:'host',
     84 					header: _('Host'),
     85 					width: 150,
     86 					sortable: true,
     87 					renderer: hostRenderer,
     88 					dataIndex: 'host'
     89 				}, {
     90 					header: _('Version'),
     91 					width: 75,
     92 					sortable: true,
     93 					renderer: fplain,
     94 					dataIndex: 'version'
     95 				}],
     96 				stripeRows: true,
     97 				selModel: new Ext.grid.RowSelectionModel({
     98 					singleSelect: true,
     99 					listeners: {
    100 						'rowselect': {fn: this.onSelect, scope: this}
    101 					}
    102 				}),
    103 				autoExpandColumn: 'host',
    104 				deferredRender:false,
    105 				autoScroll:true,
    106 				margins: '0 0 0 0',
    107 				bbar: new Ext.Toolbar({
    108 					items: [
    109 						{
    110 							id: 'add',
    111 							cls: 'x-btn-text-icon',
    112 							text: _('Add'),
    113 							icon: '/icons/add.png',
    114 							handler: this.onAdd,
    115 							scope: this
    116 						}, {
    117 							id: 'remove',
    118 							cls: 'x-btn-text-icon',
    119 							text: _('Remove'),
    120 							icon: '/icons/remove.png',
    121 							handler: this.onRemove,
    122 							scope: this
    123 						}, '->', {
    124 							id: 'stop',
    125 							cls: 'x-btn-text-icon',
    126 							text: _('Stop Daemon'),
    127 							icon: '/icons/error.png',
    128 							handler: this.onStop,
    129 							scope: this
    130 						}
    131 					]
    132 				})
    133 			});
    134 		},
    135
    136 		disconnect: function() {
    137 			Deluge.Events.fire('disconnect');
    138 		},
    139
    140 		runCheck: function(callback, scope) {
    141 			callback = callback || this.onGetHosts;
    142 			scope = scope || this;
    143 			Deluge.Client.web.get_hosts({
    144 				success: callback,
    145 				scope: scope
    146 			});
    147 		},
    148
    149 		onAdd: function(button, e) {
    150 			//Deluge.Connections.Add.show();
    151 		},
    152
    153 		onAddHost: function() {
    154 			var form = Deluge.Connections.Add.items.first();
    155 			var host = form.items.get('host').getValue();
    156 			var port = form.items.get('port').getValue();
    157 			var username = form.items.get('username').getValue();
    158 			var password = form.items.get('_password').getValue();
    159
    160 			Deluge.Client.web.add_host(host, port, username, password, {
    161 				onSuccess: function(result) {
    162 					if (!result[0]) {
    163 						Ext.MessageBox.show({
    164 							title: _('Error'),
    165 							msg: "Unable to add host: " + result[1],
    166 							buttons: Ext.MessageBox.OK,
    167 							modal: false,
    168 							icon: Ext.MessageBox.ERROR,
    169 							iconCls: 'x-deluge-icon-error'
    170 						});
    171 					} else {
    172 						this.runCheck();
    173 					}
    174 					Deluge.Connections.Add.hide();
    175 				}
    176 			});
    177 		},
    178
    179 		onAddWindowHide: function() {
    180 			// Tidy up the form to ensure all the values are default.
    181 			var form = Deluge.Connections.Add.items.first();
    182 			form.items.get('host').reset();
    183 			form.items.get('port').reset();
    184 			form.items.get('username').reset();
    185 			form.items.get('_password').reset();
    186 		},
    187
    188 		onClose: function(e) {
    189 			if (this.running) window.clearInterval(this.running);
    190 			this.hide();
    191 		},
    192
    193 		onConnect: function(e) {
    194 			var selected = this.grid.getSelectionModel().getSelected();
    195 			if (!selected) return;
    196
    197 			if (selected.get('status') == _('Connected')) {
    198 				Deluge.Client.web.disconnect({
    199 					success: function(result) {
    200 						this.runCheck();
    201 						Deluge.Events.fire('disconnect');
    202 					},
    203 					scope: this
    204 				});
    205 			} else {
    206 				var id = selected.id;
    207 				Deluge.Client.web.connect(id, {
    208 					success: function(methods) {
    209 						Deluge.Client.reloadMethods();
    210 						Deluge.Client.on('connected', function(e) {
    211 							Deluge.Events.fire('connect');
    212 						}, this, {single: true});
    213 					}
    214 				});
    215 				if (this.running) window.clearInterval(this.running);
    216 				this.hide();
    217 			}
    218 		},
    219
    220 		onGetHosts: function(hosts) {
    221 			this.grid.getStore().loadData(hosts);
    222 			var selection = this.grid.getSelectionModel();
    223 			selection.selectRow(this.selectedRow);
    224 		},
    225
    226 		onLogin: function() {
    227 			Deluge.Client.web.connected({
    228 				success: function(connected) {
    229 					if (connected) {
    230 						Deluge.Events.fire('connect');
    231 					} else {
    232 						this.show();
    233 					}
    234 				},
    235 				scope: this
    236 			});
    237 		},
    238
    239 		onLogout: function() {
    240 			this.disconnect();
    241 			if (!this.hidden && this.rendered) {
    242 				this.hide();
    243 			}
    244 		},
    245
    246 		onRemove: function(button) {
    247 			var connection = Deluge.Connections.Grid.getSelectionModel().getSelected();
    248 			Deluge.Client.web.remove_host(connection.id, {
    249 				onSuccess: function(result) {
    250 					if (!result) {
    251 						Ext.MessageBox.show({
    252 							title: _('Error'),
    253 							msg: result[1],
    254 							buttons: Ext.MessageBox.OK,
    255 							modal: false,
    256 							icon: Ext.MessageBox.ERROR,
    257 							iconCls: 'x-deluge-icon-error'
    258 						});
    259 					} else {
    260 						Deluge.Connections.Grid.store.remove(connection);
    261 					}
    262 				}
    263 			});
    264 		},
    265
    266 		onSelect: function(selModel, rowIndex, record) {
    267 			this.selectedRow = rowIndex;
    268 			var button = this.buttons[1];
    269 			if (record.get('status') == _('Connected')) {
    270 				button.setText(_('Disconnect'));
    271 			} else {
    272 				button.setText(_('Connect'));
    273 			}
    274 		},
    275
    276 		onShow: function() {
    277 			this.runCheck();
    278 			this.running = window.setInterval(this.runCheck, 2000);
    279 		},
    280
    281 		onStop: function(button, e) {
    282 			var connection = this.grid.getSelectionModel().getSelected();
    283 			Deluge.Client.web.stop_daemon(connection.id, {
    284 				success: function(result) {
    285 					if (!result[0]) {
    286 						Ext.MessageBox.show({
    287 							title: _('Error'),
    288 							msg: result[1],
    289 							buttons: Ext.MessageBox.OK,
    290 							modal: false,
    291 							icon: Ext.MessageBox.ERROR,
    292 							iconCls: 'x-deluge-icon-error'
    293 						});
    294 					}
    295 				}
    296 			});
    297 		}
    298 	});
    299 	Deluge.ConnectionManager = new Ext.deluge.ConnectionManager();
    300 })();
    301
    302 /*Deluge.Connections = {
    303
    304 	loginShow: function() {
    305 	},
    306
    307 	,
    308
    309
    310 }
    311
    312 Deluge.Connections.Store ;
    313
    314
    315
    316 Deluge.Connections.Grid = new Ext.grid.GridPanel();
    317
    318 Deluge.Connections.Add = new Ext.Window({
    319 	layout: 'fit',
    320     width: 300,
    321     height: 195,
    322     bodyStyle: 'padding: 10px 5px;',
    323     buttonAlign: 'right',
    324     closeAction: 'hide',
    325     closable: true,
    326     plain: true,
    327     title: _('Add Connection'),
    328     iconCls: 'x-deluge-add-window-icon',
    329     items: [new Ext.form.FormPanel({
    330 		defaultType: 'textfield',
    331 		id: 'connectionAddForm',
    332 		baseCls: 'x-plain',
    333 		labelWidth: 55,
    334 		items: [{
    335 			fieldLabel: _('Host'),
    336 			id: 'host',
    337 			name: 'host',
    338 			anchor: '100%',
    339 			listeners: {}
    340 		},{
    341 			fieldLabel: _('Port'),
    342 			id: 'port',
    343 			xtype: 'uxspinner',
    344 			ctCls: 'x-form-uxspinner',
    345 			name: 'port',
    346 			strategy: Ext.ux.form.Spinner.NumberStrategy(),
    347 			value: '58846',
    348 			anchor: '50%',
    349 			listeners: {}
    350 		}, {
    351 			fieldLabel: _('Username'),
    352 			id: 'username',
    353 			name: 'username',
    354 			anchor: '100%',
    355 			listeners: {}
    356 		},{
    357 			fieldLabel: _('Password'),
    358 			anchor: '100%',
    359 			id: '_password',
    360 			name: '_password',
    361 			inputType: 'password'
    362 		}]
    363 	})],
    364     buttons: [{
    365         text: _('Close'),
    366 		handler: function() {
    367 			Deluge.Connections.Add.hide();
    368 		}
    369     },{
    370         text: _('Add'),
    371 		handler: Deluge.Connections.onAddHost
    372     }],
    373 	listeners: {
    374 		'hide': Deluge.Connections.onAddWindowHide
    375 	}
    376 });*/