1 /*
  2 Script: deluge-ui.js
  3     The core ui module that builds up the ui layout and controls the polling
  4 	of the server.
  5 
  6 Copyright:
  7 	(C) Damien Churchill 2009 <damoxc@gmail.com>
  8 	This program is free software; you can redistribute it and/or modify
  9 	it under the terms of the GNU General Public License as published by
 10 	the Free Software Foundation; either version 3, or (at your option)
 11 	any later version.
 12 
 13 	This program is distributed in the hope that it will be useful,
 14 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 	GNU General Public License for more details.
 17 
 18 	You should have received a copy of the GNU General Public License
 19 	along with this program.  If not, write to:
 20 		The Free Software Foundation, Inc.,
 21 		51 Franklin Street, Fifth Floor
 22 		Boston, MA  02110-1301, USA.
 23 
 24     In addition, as a special exception, the copyright holders give
 25     permission to link the code of portions of this program with the OpenSSL
 26     library.
 27     You must obey the GNU General Public License in all respects for all of
 28     the code used other than OpenSSL. If you modify file(s) with this
 29     exception, you may extend this exception to your version of the file(s),
 30     but you are not obligated to do so. If you do not wish to do so, delete
 31     this exception statement from your version. If you delete this exception
 32     statement from all source files in the program, then also delete it here.
 33 */
 34 
 35 /**
 36  * @namespace Deluge
 37  * @static
 38  * @class Deluge.UI
 39  * The controller for the whole interface, that ties all the components
 40  * together and handles the 2 second poll.
 41  */
 42 Deluge.UI = {
 43 
 44 	cookies: new Ext.state.CookieProvider(),
 45 	
 46 	errorCount: 0,
 47 	
 48 	/**
 49 	 * @description Create all the interface components, the json-rpc client
 50 	 * and set up various events that the UI will utilise.
 51 	 */
 52 	initialize: function() {
 53 		Ext.state.Manager.setProvider(this.cookies);		
 54 		this.MainPanel = new Ext.Panel({
 55 			id: 'mainPanel',
 56 			iconCls: 'x-deluge-main-panel',
 57 			title: 'Deluge',
 58 			layout: 'border',
 59 			tbar: Deluge.Toolbar,
 60 			items: [
 61 				Deluge.Sidebar,
 62 				Deluge.Details,
 63 				Deluge.Torrents
 64 			],
 65 			bbar: Deluge.Statusbar
 66 		});
 67 
 68 		this.Viewport = new Ext.Viewport({
 69 			layout: 'fit',
 70 			items: [this.MainPanel]
 71 		});
 72 		
 73 		Deluge.Events.on("connect", this.onConnect, this);
 74 		Deluge.Events.on("disconnect", this.onDisconnect, this);
 75 		Deluge.Client = new Ext.ux.util.RpcClient({
 76 			url: '/json'
 77 		});
 78 		Deluge.Client.on('connected', function(e) {
 79 			Deluge.Login.show();
 80 		});
 81 		this.update = this.update.bind(this);
 82 	},
 83 	
 84 	update: function() {
 85 		var filters = Deluge.Sidebar.getFilters();
 86 		Deluge.Client.web.update_ui(Deluge.Keys.Grid, filters, {
 87 			success: this.onUpdate,
 88 			failure: this.onUpdateError,
 89 			scope: this
 90 		});
 91 		Deluge.Details.update();
 92 		Deluge.Client.web.connected({
 93 			success: this.onConnectedCheck,
 94 			scope: this
 95 		});
 96 	},
 97 	
 98 	onConnectedCheck: function(connected) {
 99 		if (!connected) {
100 			Deluge.Events.fire('disconnect');
101 		}
102 	},
103 	
104 	onUpdateError: function(error) {
105 		if (this.errorCount == 2) {
106 			Ext.MessageBox.show({
107 				title: 'Lost Connection',
108 				msg: 'The connection to the webserver has been lost!',
109 				buttons: Ext.MessageBox.OK,
110 				icon: Ext.MessageBox.ERROR
111 			});
112 		}
113 		this.errorCount++;
114 	},
115 	
116 	/**
117 	 * @static
118 	 * @private
119 	 * Updates the various components in the interface.
120 	 */
121 	onUpdate: function(data) {
122 		Deluge.Torrents.update(data['torrents']);
123 		Deluge.Statusbar.update(data['stats']);
124 		Deluge.Sidebar.update(data['filters']);
125 		this.errorCount = 0;
126 	},
127 	
128 	/**
129 	 * @static
130 	 * @private
131 	 * Start the Deluge UI polling the server and update the interface.
132 	 */
133 	onConnect: function() {
134 		if (!this.running) {
135 			this.running = setInterval(this.update, 2000);
136 			this.update();
137 		}
138 	},
139 	
140 	/**
141 	 * @static
142 	 * @private
143 	 */
144 	onDisconnect: function() {
145 		this.stop();
146 	},
147 	
148 	/**
149 	 * @static
150 	 * Stop the Deluge UI polling the server and clear the interface.
151 	 */
152 	stop: function() {
153 		if (this.running) {
154             clearInterval(this.running);
155             this.running = false;
156 			Deluge.Torrents.getStore().loadData([]);
157         }
158 	}
159 }
160 
161 Ext.onReady(function(e) {
162 	Deluge.UI.initialize();
163 });