1 /*
  2 Script: deluge-login.js
  3     Contains all objects and functions related to the login system.
  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 	Ext.deluge.LoginWindow = Ext.extend(Ext.Window, {
 37 		
 38 		firstShow: true,
 39 		
 40 		constructor: function(config) {
 41 			config = Ext.apply({
 42 				layout: 'fit',
 43 				width: 300,
 44 				height: 120,
 45 				bodyStyle: 'padding: 10px 5px;',
 46 				buttonAlign: 'center',
 47 				closeAction: 'hide',
 48 				closable: false,
 49 				modal: true,
 50 				plain: true,
 51 				resizable: false,
 52 				title: _('Login'),
 53 				iconCls: 'x-deluge-login-window-icon'
 54 			}, config);
 55 			Ext.deluge.LoginWindow.superclass.constructor.call(this, config);
 56 		},
 57 		
 58 		initComponent: function() {
 59 			Ext.deluge.LoginWindow.superclass.initComponent.call(this);
 60 			Deluge.Events.on('logout', this.onLogout, this);
 61 			this.on('show', this.onShow, this);
 62 			this.on('beforeshow', this.onBeforeShow, this);
 63 			
 64 			this.addButton({
 65 				text: _('Login'),
 66 				handler: this.onLogin,
 67 				scope: this
 68 			});
 69 			
 70 			this.loginForm = this.add({
 71 				xtype: 'form',
 72 				defaultType: 'textfield',
 73 				id: 'loginForm',
 74 				baseCls: 'x-plain',
 75 				labelWidth: 55,
 76 				items: [{
 77 					fieldLabel: _('Password'),
 78 					id: 'password',
 79 					name: 'password',
 80 					inputType: 'password',
 81 					anchor: '100%',
 82 					listeners: {
 83 						'specialkey': {
 84 							fn: this.onKey,
 85 							scope: this
 86 						}
 87 					}
 88 				}]
 89 			})
 90 		},
 91 		
 92 		onKey: function(field, e) {
 93 			if (e.getKey() == 13) this.onLogin();
 94 		},
 95 		
 96 		onLogin: function() {
 97 			var passwordField = this.loginForm.items.get('password');
 98 			Deluge.Client.auth.login(passwordField.getValue(), {
 99 				success: function(result) {
100 					if (result) {
101 						Deluge.Events.fire('login');
102 						this.hide();
103 						passwordField.setRawValue('');
104 						Deluge.UI.cookies.set("session", result);
105 					} else {
106 						Ext.MessageBox.show({
107 							title: _('Login Failed'),
108 							msg: _('You entered an incorrect password'),
109 							buttons: Ext.MessageBox.OK,
110 							modal: false,
111 							fn: function() {
112 								passwordField.focus();
113 							},
114 							icon: Ext.MessageBox.WARNING,
115 							iconCls: 'x-deluge-icon-warning'
116 						});
117 					}
118 				},
119 				scope: this
120 			});
121 		},
122 		
123 		onLogout: function() {
124 			var session = Deluge.UI.cookies.get("session", false);
125 			if (session) {
126 				Deluge.Client.auth.delete_session(session, {
127 					success: function(result) {
128 						Deluge.UI.cookies.clear("session");
129 						this.show();
130 					},
131 					scope: this
132 				});
133 			}
134 		},
135 		
136 		onBeforeShow: function() {
137 			var session = Deluge.UI.cookies.get("session", false);
138 			if (session) {
139 				Deluge.Client.auth.check_session(session, {
140 					success: function(result) {
141 						if (result) {
142 							Deluge.Events.fire('login');
143 							this.loginForm.items.get('password').setRawValue('');
144 							this.hide();
145 						} else {
146 							Deluge.UI.cookies.clear("session");
147 							this.show();
148 						}
149 					},
150 					failure: function(result) {
151 						Deluge.UI.cookies.clear("session");
152 						this.show();
153 					},
154 					scope: this
155 				});
156 				return false;
157 			}
158 		},
159 		
160 		onShow: function() {
161 			var passwordField = this.loginForm.items.get('password');
162 			passwordField.focus(false, 150);
163 		}
164 	});
165 	
166 	Deluge.Login = new Ext.deluge.LoginWindow();
167 })();