1 (function() {	
  2 	Ext.deluge.Statusbar = Ext.extend(Ext.StatusBar, {
  3 		constructor: function(config) {
  4 			config = Ext.apply({
  5 				id: 'deluge-statusbar',
  6 				defaultIconCls: 'x-not-connected',
  7 				defaultText: _('Not Connected')
  8 			}, config);
  9 			Ext.deluge.Statusbar.superclass.constructor.call(this, config);
 10 		},
 11 		
 12 		initComponent: function() {
 13 			Ext.deluge.Statusbar.superclass.initComponent.call(this);
 14 			
 15 			Deluge.Events.on('connect', this.onConnect, this);
 16 			Deluge.Events.on('disconnect', this.onDisconnect, this);
 17 		},
 18 		
 19 		createButtons: function() {
 20 			this.add({
 21 				id: 'statusbar-connections',
 22 				text: ' ',
 23 				cls: 'x-btn-text-icon',
 24 				iconCls: 'x-deluge-connections',
 25 				menu: Deluge.Menus.Connections
 26 			}, '-', {
 27 				id: 'statusbar-downspeed',
 28 				text: ' ',
 29 				cls: 'x-btn-text-icon',
 30 				iconCls: 'x-deluge-downloading',
 31 				menu: Deluge.Menus.Download
 32 			}, '-', {
 33 				id: 'statusbar-upspeed',
 34 				text: ' ',
 35 				cls: 'x-btn-text-icon',
 36 				iconCls: 'x-deluge-seeding',
 37 				menu: Deluge.Menus.Upload
 38 			}, '-', {
 39 				id: 'statusbar-traffic',
 40 				text: ' ',
 41 				cls: 'x-btn-text-icon',
 42 				iconCls: 'x-deluge-traffic'
 43 			}, '-', {
 44 				id: 'statusbar-dht',
 45 				text: ' ',
 46 				cls: 'x-btn-text-icon',
 47 				iconCls: 'x-deluge-dht'
 48 			});
 49 			this.created = true;
 50 		},
 51 		
 52 		onConnect: function() {
 53 			this.setStatus({
 54 				iconCls: 'x-connected',
 55 				text: ''
 56 			});
 57 			if (!this.created) this.createButtons();
 58 			else {
 59 				this.items.each(function(item) {
 60 					item.show();
 61 					item.enable();
 62 				});
 63 			}
 64 		},
 65 	
 66 		onDisconnect: function() {
 67 			this.clearStatus({useDefaults:true});
 68 			this.items.each(function(item) {
 69 				item.hide();
 70 				item.disable();
 71 			});
 72 		},
 73 		
 74 		update: function(stats) {
 75 			function addSpeed(val) {return val + ' KiB/s'}
 76 			
 77 			var updateStat = function(name, config) {
 78 				var item = this.items.get('statusbar-' + name);
 79 				if (config.limit.value == -1) {
 80 					var str = (config.value.formatter) ? config.value.formatter(config.value.value) : config.value.value;
 81 				} else {
 82 					var value = (config.value.formatter) ? config.value.formatter(config.value.value) : config.value.value;
 83 					var limit = (config.limit.formatter) ? config.limit.formatter(config.limit.value) : config.limit.value;
 84 					var str = String.format(config.format, value, limit);
 85 				}
 86 				item.setText(str);
 87 			}.bind(this);
 88 			
 89 			updateStat('connections', {
 90 				value: {value: stats.num_connections},
 91 				limit: {value: stats.max_num_connections},
 92 				format: '{0} ({1})'
 93 			});
 94 	
 95 			updateStat('downspeed', {
 96 				value: {
 97 					value: stats.download_rate,
 98 					formatter: Deluge.Formatters.speed
 99 				},
100 				limit: {
101 					value: stats.max_download,
102 					formatter: addSpeed
103 				},
104 				format: '{0} ({1})'
105 			});
106 	
107 			updateStat('upspeed', {
108 				value: {
109 					value: stats.upload_rate,
110 					formatter: Deluge.Formatters.speed
111 				},
112 				limit: {
113 					value: stats.max_upload,
114 					formatter: addSpeed
115 				},
116 				format: '{0} ({1})'
117 			});
118 			
119 			updateStat('traffic', {
120 				value: {
121 					value: stats.payload_download_rate,
122 					formatter: Deluge.Formatters.speed
123 				},
124 				limit: {
125 					value: stats.payload_upload_rate,
126 					formatter: Deluge.Formatters.speed
127 				},
128 				format: '{0}/{1}'
129 			});
130 	
131 			this.items.get('statusbar-dht').setText(stats.dht_nodes);
132 	
133 			function updateMenu(menu, stat) {
134 				var item = menu.items.get(stat)
135 				if (!item) {
136 					item = menu.items.get('other')
137 				}
138 				item.setChecked(true);
139 			}
140 			
141 			updateMenu(Deluge.Menus.Connections, stats.max_num_connections);
142 			updateMenu(Deluge.Menus.Download, stats.max_download);
143 			updateMenu(Deluge.Menus.Upload, stats.max_upload);
144 		}
145 	});
146 	Deluge.Statusbar = new Ext.deluge.Statusbar();
147 })();