mypackage_wrt/Login.js
changeset 42 20be4dd42b12
child 44 48bcd0bbc1ab
equal deleted inserted replaced
41:315255cd1aef 42:20be4dd42b12
       
     1 // ////////////////////////////////////////////////////////////////////////////
       
     2 // Symbian Foundation Example Code
       
     3 //
       
     4 // This software is in the public domain. No copyright is claimed, and you 
       
     5 // may use it for any purpose without license from the Symbian Foundation.
       
     6 // No warranty for any purpose is expressed or implied by the authors or
       
     7 // the Symbian Foundation. 
       
     8 // ////////////////////////////////////////////////////////////////////////////
       
     9 
       
    10 // Login to the developer site
       
    11 
       
    12 var loginUrlContent = null;
       
    13 var loginUrlHttpReq = null;
       
    14 var loginCallback = null;
       
    15 
       
    16 var isHideNotifications = true;
       
    17 function login(callback){
       
    18 	if ( forumUsername == null || forumPassword == null ) {
       
    19 		loginInitiated = true;
       
    20 		loginInitiatedCallback = callback;
       
    21 		promptForPassword();
       
    22 		return;
       
    23 	}
       
    24 	loginCallback = callback;
       
    25 	loginInitiated = false;
       
    26 	loginInitiatedCallback = null;
       
    27 	uiManager.showNotification(-1, "wait", "Please wait...", -1);	
       
    28 	
       
    29 	var parameters =  symbianOrgLoginUsernameField + "=" + forumUsername
       
    30 	          + "&" + symbianOrgLoginPasswordField + "=" + forumPassword
       
    31 			  + "&submit=Login&image_submit.x=0&image_submit.y=0&image_submit=submit"
       
    32 			  + "&referrer="+symbianOrgBaseUrl;
       
    33 	loginUrlHttpReq = new Ajax();
       
    34     loginUrlHttpReq.onreadystatechange = loginComplete;
       
    35 	
       
    36     // initiate the request
       
    37 	loginUrlHttpReq.open('POST', symbianOrgLoginUrl +"?referer="+symbianOrgBaseUrl, true);
       
    38 	loginUrlHttpReq.setRequestHeader("Referer", symbianOrgBaseUrl);
       
    39 	loginUrlHttpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
       
    40 	loginUrlHttpReq.setRequestHeader("Content-length", parameters.length);
       
    41 	loginUrlHttpReq.setRequestHeader("Connection", "close");
       
    42 	loginUrlHttpReq.send(parameters);
       
    43 }
       
    44 
       
    45 function loginComplete(){
       
    46 	if ( loginUrlHttpReq == null ) {
       
    47 		return;
       
    48 	}
       
    49     // complete request?
       
    50 	var readyState = loginUrlHttpReq.readyState;
       
    51     // attempt to get response status
       
    52     var responseStatus = null;
       
    53     try {
       
    54         responseStatus = loginUrlHttpReq.status;
       
    55     } catch (noStatusException) {}
       
    56     if (readyState == 4) {
       
    57 
       
    58 		if (responseStatus < 300) {
       
    59 			
       
    60 			var content = loginUrlHttpReq.responseText;
       
    61 			if (content.indexOf("LoginWelcome") == -1) {
       
    62 				uiManager.showNotification(3000, "warning", "Login failed.");
       
    63 				promptForPassword();
       
    64 			}
       
    65 			else {
       
    66 				if (loginCallback != null) {
       
    67 					loginCallback.call();
       
    68 				}
       
    69 				// ensure we have all the cookies we need
       
    70 				var vbCookieGet = new Ajax();
       
    71 				var vburl = symbianOrgBaseUrl + "/forum/";
       
    72 			    vbCookieGet.onreadystatechange = forumCookieHarvestComplete;
       
    73 				vbCookieGet.open('GET', vburl, true);
       
    74 				vbCookieGet.send(null);
       
    75 			}
       
    76 		} else if (responseStatus < 400) {
       
    77 			// do nothing, this must be a redirect
       
    78 		} else {
       
    79 			uiManager.hideNotification();
       
    80 			uiManager.showNotification(3000, "warning", "Login failed.");
       
    81 			promptForPassword();
       
    82 		}
       
    83     }
       
    84 }
       
    85 
       
    86 function forumCookieHarvestComplete () {
       
    87 	if (isHideNotifications) {
       
    88 		uiManager.hideNotification();
       
    89 	}
       
    90 	isHideNotifications = true;
       
    91 }