      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 Deluge.UI = {
     37
     38 	cookies: new Ext.state.CookieProvider(),
     39
     40 	errorCount: 0,
     41
     42 	initialize: function() {
     43 		Ext.state.Manager.setProvider(this.cookies);
     44 		this.MainPanel = new Ext.Panel({
     45 			id: 'mainPanel',
     46 			iconCls: 'x-deluge-main-panel',
     47 			title: 'Deluge',
     48 			layout: 'border',
     49 			tbar: Deluge.Toolbar,
     50 			items: [
     51 				Deluge.Sidebar,
     52 				Deluge.Details,
     53 				Deluge.Torrents
     54 			],
     55 			bbar: Deluge.Statusbar
     56 		});
     57
     58 		this.Viewport = new Ext.Viewport({
     59 			layout: 'fit',
     60 			items: [this.MainPanel]
     61 		});
     62
     63 		Deluge.Events.on("connect", this.onConnect, this);
     64 		Deluge.Events.on("disconnect", this.onDisconnect, this);
     65 		Deluge.Client = new Ext.ux.util.RpcClient({
     66 			url: '/json'
     67 		});
     68 		Deluge.Client.on('connected', function(e) {
     69 			Deluge.Login.show();
     70 		});
     71 		this.update = this.update.bind(this);
     72 	},
     73
     74 	update: function() {
     75 		var filters = Deluge.Sidebar.getFilters();
     76 		Deluge.Client.web.update_ui(Deluge.Keys.Grid, filters, {
     77 			success: this.onUpdate,
     78 			failure: this.onUpdateError,
     79 			scope: this
     80 		});
     81 		Deluge.Details.update();
     82 		Deluge.Client.web.connected({
     83 			success: this.onConnectedCheck,
     84 			scope: this
     85 		});
     86 	},
     87
     88 	onConnectedCheck: function(connected) {
     89 		if (!connected) {
     90 			Deluge.Events.fire('disconnect');
     91 		}
     92 	},
     93
     94 	onUpdateError: function(error) {
     95 		if (this.errorCount == 2) {
     96 			Ext.MessageBox.show({
     97 				title: 'Lost Connection',
     98 				msg: 'The connection to the webserver has been lost!',
     99 				buttons: Ext.MessageBox.OK,
    100 				icon: Ext.MessageBox.ERROR
    101 			});
    102 		}
    103 		this.errorCount++;
    104 	},
    105
    106 	onUpdate: function(data) {
    107 		Deluge.Torrents.update(data['torrents']);
    108 		Deluge.Statusbar.update(data['stats']);
    109 		Deluge.Sidebar.update(data['filters']);
    110 		this.errorCount = 0;
    111 	},
    112
    113 	/*
    114     Property: run
    115         Start the Deluge UI polling the server to get the updated torrent
    116         information.
    117
    118     Example:
    119         Deluge.UI.onConnect();
    120     */
    121 	onConnect: function() {
    122 		if (!this.running) {
    123 			this.running = setInterval(this.update, 2000);
    124 			this.update();
    125 		}
    126 	},
    127
    128 	onDisconnect: function() {
    129 		this.stop();
    130 	},
    131
    132 	/*
    133     Property: stop
    134         Stop the Deluge UI polling the server to get the updated torrent
    135         information.
    136
    137     Example:
    138         Deluge.UI.stop();
    139     */
    140 	stop: function() {
    141 		if (this.running) {
    142             clearInterval(this.running);
    143             this.running = false;
    144 			Deluge.Torrents.getStore().loadData([]);
    145         }
    146 	}
    147 }
    148
    149 Ext.onReady(function(e) {
    150 	Deluge.UI.initialize();
    151 });