1 /*
  2 Script: Deluge.Add.File.js
  3     Contains the Add Torrent by file window.
  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 Ext.deluge.add.FileWindow = Ext.extend(Ext.deluge.add.Window, {
 36 	constructor: function(config) {
 37 		config = Ext.apply({
 38 			layout: 'fit',
 39 			width: 350,
 40 			height: 115,
 41 			bodyStyle: 'padding: 10px 5px;',
 42 			buttonAlign: 'center',
 43 			closeAction: 'hide',
 44 			modal: true,
 45 			plain: true,
 46 			title: _('Add from File'),
 47 			iconCls: 'x-deluge-add-file',
 48 			buttons: [{
 49 				text: _('Add'),
 50 				handler: this.onAdd,
 51 				scope: this
 52 			}]
 53 		}, config);
 54 		Ext.deluge.add.UrlWindow.superclass.constructor.call(this, config);
 55 	},
 56 	
 57 	initComponent: function() {
 58 		Ext.deluge.add.UrlWindow.superclass.initComponent.call(this);
 59 		this.form = this.add(new Ext.form.FormPanel({
 60 			baseCls: 'x-plain',
 61 			labelWidth: 55,
 62 			autoHeight: true,
 63 			fileUpload: true,
 64 			items: [{
 65 				xtype: 'fileuploadfield',
 66 				id: 'torrentFile',
 67 				emptyText: _('Select a torrent'),
 68 				fieldLabel: _('File'),
 69 				name: 'file',
 70 				buttonCfg: {
 71 					text: _('Browse') + '...'
 72 				}
 73 			}]
 74 		}));
 75 	},
 76 	
 77 	onAdd: function(field, e) {
 78 		if (this.form.getForm().isValid()) {
 79 			this.torrentId = this.createTorrentId();
 80 			this.form.getForm().submit({
 81 				url: '/upload',
 82 				waitMsg: _('Uploading your torrent...'),
 83 				success: this.onUploadSuccess,
 84 				scope: this
 85 			});
 86 			var name = this.form.getForm().findField('torrentFile').value;
 87 			this.fireEvent('beforeadd', this.torrentId, name);
 88 		}
 89 	},
 90 	
 91 	onGotInfo: function(info, obj, response, request) {
 92 		info['filename'] = request.options.filename;
 93 		this.fireEvent('add', this.torrentId, info);
 94 	},
 95 	
 96 	onUploadSuccess: function(fp, upload) {
 97 		this.hide();
 98 		var filename = upload.result.toString();
 99 		this.form.getForm().findField('torrentFile').setValue('');
100 		Deluge.Client.web.get_torrent_info(filename, {
101 			success: this.onGotInfo,
102 			scope: this,
103 			filename: filename
104 		});
105 	}
106 });