1 /*
  2 Script: Deluge.Details.Peers.js
  3     The peers tab displayed in the details panel.
  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 	function flagRenderer(value) {
 37 		return String.format('<img src="/flag/{0}" />', value);
 38 	}
 39 	function peerAddressRenderer(value, p, record) {
 40 		var seed = (record.data['seed'] == 1024) ? 'x-deluge-seed' : 'x-deluge-peer'
 41 		return String.format('<div class="{0}">{1}</div>', seed, value);
 42 	}
 43 	function peerProgressRenderer(value) {
 44 		var progress = (value * 100).toFixed(0);
 45 		var width = new Number(this.style.match(/\w+:\s*(\d+)\w+/)[1]).toFixed(0) - 8;
 46 		return Deluge.progressBar(progress, width, progress + '%');
 47 	}
 48 	function sort_address(value) {
 49 		var m = value.match(/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\:(\d+)/);
 50 		var address = 0;
 51 		var parts = [m[1], m[2], m[3], m[4]];
 52 		Ext.each(parts, function(part, index) {
 53 			part = parseInt(part);
 54 			address = address | part << ((3 - index) * 8);
 55 			//alert("Total: " + address + "\nPart: " + part + "\nIndex: " + index + "\nCalc: " + (part << ((3 - index) * 8)));
 56 		});
 57 		return address;
 58 	}
 59 
 60 	Ext.deluge.details.PeersTab = Ext.extend(Ext.grid.GridPanel, {
 61 		
 62 		constructor: function(config) {
 63 			config = Ext.apply({
 64 				title: _('Peers'),
 65 				cls: 'x-deluge-peers',
 66 				store: new Ext.data.SimpleStore({
 67 					fields: [
 68 						{name: 'country'},
 69 						{name: 'address', sortType: sort_address},
 70 						{name: 'client'},
 71 						{name: 'progress', type: 'float'},
 72 						{name: 'downspeed', type: 'int'},
 73 						{name: 'upspeed', type: 'int'},
 74 						{name: 'seed', type: 'int'}
 75 					],
 76 					id: 0
 77 				}),
 78 				columns: [{
 79 					header: ' ',
 80 					width: 30,
 81 					sortable: true,
 82 					renderer: flagRenderer,
 83 					dataIndex: 'country'
 84 				}, {
 85 					header: 'Address',
 86 					width: 125,
 87 					sortable: true,
 88 					renderer: peerAddressRenderer,
 89 					dataIndex: 'address'
 90 				}, {
 91 					header: 'Client',
 92 					width: 125,
 93 					sortable: true,
 94 					renderer: fplain,
 95 					dataIndex: 'client'
 96 				}, {
 97 					header: 'Progress',
 98 					width: 150,
 99 					sortable: true,
100 					renderer: peerProgressRenderer,
101 					dataIndex: 'progress'
102 				}, {
103 					header: 'Down Speed',
104 					width: 100,
105 					sortable: true,
106 					renderer: fspeed,
107 					dataIndex: 'downspeed'
108 				}, {
109 					header: 'Up Speed',
110 					width: 100,
111 					sortable: true,
112 					renderer: fspeed,
113 					dataIndex: 'upspeed'
114 				}],	
115 				stripeRows: true,
116 				deferredRender:false,
117 				autoScroll:true
118 			}, config);
119 			Ext.deluge.details.PeersTab.superclass.constructor.call(this, config);
120 		},
121 		
122 		onRender: function(ct, position) {
123 			Ext.deluge.details.PeersTab.superclass.onRender.call(this, ct, position);
124 		},
125 		
126 		clear: function() {
127 			this.getStore().loadData([]);
128 		},
129 		
130 		update: function(torrentId) {
131 			Deluge.Client.core.get_torrent_status(torrentId, Deluge.Keys.Peers, {
132 				success: this.onRequestComplete,
133 				scope: this
134 			});
135 		},
136 		
137 		onRequestComplete: function(torrent, options) {
138 			if (!torrent) return;
139 			var peers = new Array();
140 			Ext.each(torrent.peers, function(peer) {
141 				peers.push([peer.country, peer.ip, peer.client, peer.progress, peer.down_speed, peer.up_speed, peer.seed]);
142 			}, this);
143 			this.getStore().loadData(peers);
144 		}
145 	});
146 	Deluge.Details.add(new Ext.deluge.details.PeersTab());
147 })();