author | victorp@symbian.org |
Mon, 10 May 2010 14:32:47 +0100 | |
changeset 51 | abeee2a2e01b |
parent 44 | 48bcd0bbc1ab |
permissions | -rwxr-xr-x |
42 | 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; |
|
44
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
15 |
var logincompleted=null; |
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
16 |
|
42 | 17 |
|
18 |
var isHideNotifications = true; |
|
19 |
function login(callback){ |
|
44
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
20 |
debuglog.debug("Starting login"); |
42 | 21 |
if ( forumUsername == null || forumPassword == null ) { |
22 |
loginInitiated = true; |
|
23 |
loginInitiatedCallback = callback; |
|
24 |
promptForPassword(); |
|
25 |
return; |
|
26 |
} |
|
27 |
loginCallback = callback; |
|
28 |
loginInitiated = false; |
|
29 |
loginInitiatedCallback = null; |
|
44
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
30 |
//sflogout(); |
42 | 31 |
uiManager.showNotification(-1, "wait", "Please wait...", -1); |
32 |
||
33 |
var parameters = symbianOrgLoginUsernameField + "=" + forumUsername |
|
34 |
+ "&" + symbianOrgLoginPasswordField + "=" + forumPassword |
|
35 |
+ "&submit=Login&image_submit.x=0&image_submit.y=0&image_submit=submit" |
|
36 |
+ "&referrer="+symbianOrgBaseUrl; |
|
37 |
loginUrlHttpReq = new Ajax(); |
|
38 |
loginUrlHttpReq.onreadystatechange = loginComplete; |
|
44
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
39 |
|
42 | 40 |
|
41 |
// initiate the request |
|
42 |
loginUrlHttpReq.open('POST', symbianOrgLoginUrl +"?referer="+symbianOrgBaseUrl, true); |
|
43 |
loginUrlHttpReq.setRequestHeader("Referer", symbianOrgBaseUrl); |
|
44 |
loginUrlHttpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); |
|
45 |
loginUrlHttpReq.setRequestHeader("Content-length", parameters.length); |
|
46 |
loginUrlHttpReq.setRequestHeader("Connection", "close"); |
|
47 |
loginUrlHttpReq.send(parameters); |
|
48 |
} |
|
49 |
||
44
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
50 |
function loginfailed(myerror){ |
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
51 |
debuglog.debug("login failed with error " + myerror); |
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
52 |
uiManager.showNotification(2000, "warning", "Login failed "+ myerror); |
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
53 |
isHideNotifications=false; |
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
54 |
//promptForPassword(); |
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
55 |
|
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
56 |
} |
42 | 57 |
function loginComplete(){ |
44
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
58 |
debuglog.debug("login completed called"); |
42 | 59 |
if ( loginUrlHttpReq == null ) { |
60 |
return; |
|
61 |
} |
|
62 |
// complete request? |
|
63 |
var readyState = loginUrlHttpReq.readyState; |
|
64 |
// attempt to get response status |
|
65 |
var responseStatus = null; |
|
66 |
try { |
|
67 |
responseStatus = loginUrlHttpReq.status; |
|
68 |
} catch (noStatusException) {} |
|
69 |
if (readyState == 4) { |
|
44
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
70 |
debuglog.debug("ready state found"); |
42 | 71 |
if (responseStatus < 300) { |
72 |
||
73 |
var content = loginUrlHttpReq.responseText; |
|
44
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
74 |
if (content.indexOf("<title>Symbian developer community - log in</title>")!=-1) { |
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
75 |
loginfailed("- Bad Credentials"); |
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
76 |
return; |
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
77 |
} |
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
78 |
|
42 | 79 |
if (content.indexOf("LoginWelcome") == -1) { |
44
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
80 |
loginfailed(null); |
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
81 |
return; |
42 | 82 |
} |
83 |
else { |
|
84 |
if (loginCallback != null) { |
|
85 |
loginCallback.call(); |
|
86 |
} |
|
87 |
// ensure we have all the cookies we need |
|
88 |
var vbCookieGet = new Ajax(); |
|
89 |
var vburl = symbianOrgBaseUrl + "/forum/"; |
|
90 |
vbCookieGet.onreadystatechange = forumCookieHarvestComplete; |
|
91 |
vbCookieGet.open('GET', vburl, true); |
|
92 |
vbCookieGet.send(null); |
|
44
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
93 |
} |
42 | 94 |
} else if (responseStatus < 400) { |
95 |
// do nothing, this must be a redirect |
|
96 |
} else { |
|
44
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
97 |
loginfailed("- HTTP error " + responseStatus); |
42 | 98 |
} |
44
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
99 |
} |
42 | 100 |
} |
101 |
||
102 |
function forumCookieHarvestComplete () { |
|
103 |
if (isHideNotifications) { |
|
104 |
uiManager.hideNotification(); |
|
105 |
} |
|
106 |
isHideNotifications = true; |
|
44
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
107 |
logincompleted=true; |
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
108 |
debuglog.debug("cookie harvested"); |
42 | 109 |
} |
44
48bcd0bbc1ab
My package wiki v0.3 - http://developer.symbian.org/wiki/index.php/My_Package_Widget#Current_version
victorp@symbian.org
parents:
42
diff
changeset
|
110 |