mypackage_wrt/ForumPostForm.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 var allowRetry = true;
       
    11 function ForumPostForm(aParentView, forumid) {
       
    12 	ListView.prototype.init.call(this, null, null);
       
    13 	this.previousView = aParentView;
       
    14 	this.forumid = forumid;
       
    15 	
       
    16 	// add the banner / 'title bar' - avoids the caption bug
       
    17 	var titleBar = new NavigationButton(null, "titlebar.png", "New thread in " + aParentView.feedName);
       
    18 	titleBar.setEnabled(false);
       
    19 	this.addControl(titleBar);
       
    20 	
       
    21 	// add topic name textfield
       
    22 	this.topicNameTf = new TextField('threadPostTopic', "Topic title", "");
       
    23 	this.addControl(this.topicNameTf);
       
    24 	
       
    25 	// add content textarea
       
    26 	this.contentTa = new TextArea('threadPostContent', "Message", "", 6);
       
    27 	this.addControl(this.contentTa);
       
    28 	
       
    29 	var self = this;
       
    30 
       
    31     // post button
       
    32     this.postButton = new FormButton(null, "Submit");
       
    33     this.postButton.addEventListener("ActionPerformed", function(){
       
    34 		isHideNotifications = false;
       
    35 		login( function(){
       
    36 			submitNewTopic(
       
    37 				self.topicNameTf.getText(), // title
       
    38 				self.contentTa.getText(), // message
       
    39 				self.forumid, // forumid
       
    40 				function() { self.goBack();uiManager.currentView.update(true);}
       
    41 				);
       
    42 			});
       
    43 		});
       
    44     this.addControl(this.postButton);
       
    45     
       
    46     // cancel settings button
       
    47     this.cancelButton = new FormButton(null, "Cancel");
       
    48     this.cancelButton.addEventListener("ActionPerformed", function(){self.goBack();});
       
    49     this.addControl(this.cancelButton);
       
    50 	
       
    51 }
       
    52 
       
    53 ForumPostForm.prototype = new ListView(null, null);
       
    54 
       
    55 
       
    56 function ForumReplyForm(aParentView, threadid, postid, parentTitle) {
       
    57 	ListView.prototype.init.call(this, null, null);
       
    58 	this.previousView = aParentView;
       
    59 	this.threadid = threadid;
       
    60 	this.postid = postid;
       
    61 	this.parentTitle = parentTitle;
       
    62 	
       
    63 	// add the banner / 'title bar' - avoids the caption bug
       
    64 	var titleBar = new NavigationButton(null, "titlebar.png", "Reply to " + parentTitle);
       
    65 	titleBar.setEnabled(false);
       
    66 	this.addControl(titleBar);
       
    67 	
       
    68 	// add topic name textfield
       
    69 	this.topicNameTf = new TextField('threadPostTopic', "Title", "");
       
    70 	this.addControl(this.topicNameTf);
       
    71 	
       
    72 	// add content textarea
       
    73 	this.contentTa = new TextArea('threadPostContent', "Message", "", 6);
       
    74 	this.addControl(this.contentTa);
       
    75 	
       
    76 	var self = this;
       
    77 
       
    78     // post button
       
    79     this.postButton = new FormButton(null, "Submit");
       
    80     this.postButton.addEventListener("ActionPerformed", function(){
       
    81 		isHideNotifications = false;
       
    82 		login(
       
    83 		function(){
       
    84 			submitNewReply(self.topicNameTf.getText(), // title
       
    85 			 self.contentTa.getText(), // message
       
    86 			 self.threadid, // threadid
       
    87 			 self.postid, // threadid
       
    88 			 function(){
       
    89 				self.goBack();
       
    90 				uiManager.currentView.update(true);
       
    91 			});
       
    92 		});
       
    93 	});
       
    94     this.addControl(this.postButton);
       
    95     
       
    96     // cancel settings button
       
    97     this.cancelButton = new FormButton(null, "Cancel");
       
    98     this.cancelButton.addEventListener("ActionPerformed", function(){self.goBack();});
       
    99     this.addControl(this.cancelButton);
       
   100 	
       
   101 }
       
   102 
       
   103 ForumReplyForm.prototype = new ListView(null, null);
       
   104 
       
   105 
       
   106 // Submitting a new to vBulletin is somewhat complex. There appears to be
       
   107 // no XML based interface so we have to go through the usual web posting
       
   108 // procedure. So, first we must be logged in. Then, we must request forums
       
   109 // home page to get bbsessionhash cookie. Next, we request the form,
       
   110 // to collect required security information (securitytoken etc) from the form.
       
   111 // If all goes well, we can now post a message.
       
   112 
       
   113 var submitUrlContent = null;
       
   114 var submitUrlHttpReq = null;
       
   115 var submitCallback = null;
       
   116 var submitTitle = null;
       
   117 var submitContent = null;
       
   118 var submitForumId = null;
       
   119 var submitThreadId = null;
       
   120 var submitPostId = null;
       
   121 var submitCallback = null;
       
   122 var reply = false;
       
   123 
       
   124 // Initiates the submission process by requesting the form
       
   125 function submitNewTopic(title, content, forumid, callback){
       
   126 	uiManager.showNotification(-1, "wait", "Submitting...", -1);
       
   127 	isHideNotifications = false;
       
   128 
       
   129 	// Dealing with vBulletin nastiness...
       
   130 	
       
   131 	// ensure we have all the cookies we need
       
   132 	var vbCookieGet = new Ajax();
       
   133 	var vburl = symbianOrgBaseUrl + "/forum/";
       
   134 	vbCookieGet.open('GET', vburl, false);
       
   135 	vbCookieGet.send(null);
       
   136 
       
   137 	// Now we have to harvest some info from the post form. 	
       
   138 	submitUrlHttpReq = new Ajax();
       
   139 	var self = this;
       
   140 	submitTitle = title;
       
   141 	submitContent = content;
       
   142 	submitForumId = forumid;
       
   143 	submitThreadId = null;
       
   144 	submitPostId = null;
       
   145 	submitCallback = callback;
       
   146     submitUrlHttpReq.onreadystatechange = submitFormReady;
       
   147 	reply = false;
       
   148 	
       
   149 	var url = symbianOrgBaseUrl + "/forum/newthread.php?do=newthread&f=" + forumid;
       
   150 	submitUrlHttpReq.open('GET', url, true);
       
   151 	submitUrlHttpReq.send(null);
       
   152 }
       
   153 
       
   154 // Initiates the submission process by requesting the form
       
   155 function submitNewReply(title, content, threadid, postid, callback){
       
   156 	uiManager.showNotification(-1, "wait", "Submitting...", -1);
       
   157 	isHideNotifications = false;
       
   158 	
       
   159 	// Dealing with vBulletin nastiness...
       
   160 	
       
   161 	// ensure we have all the cookies we need
       
   162 	var vbCookieGet = new Ajax();
       
   163 	var vburl = symbianOrgBaseUrl + "/forum/";
       
   164 	vbCookieGet.open('GET', vburl, false);
       
   165 	vbCookieGet.send(null);
       
   166 
       
   167 	// Now we have to harvest some info from the post form. 	
       
   168 	submitUrlHttpReq = new Ajax();
       
   169 	var self = this;
       
   170 	submitTitle = title;
       
   171 	submitContent = content;
       
   172 	submitForumId = null;
       
   173 	submitThreadId = threadid;
       
   174 	submitPostId = postid;
       
   175 	submitCallback = callback;
       
   176     submitUrlHttpReq.onreadystatechange = submitFormReady;
       
   177 	reply = true;
       
   178 	
       
   179 	var url = symbianOrgBaseUrl + "/forum/newreply.php?do=newreply&noquote=1&p=" + postid;
       
   180 	submitUrlHttpReq.open('GET', url, true);
       
   181 	submitUrlHttpReq.send(null);
       
   182 }
       
   183 
       
   184 
       
   185 var forumPostHarvestString_loggedinuser = "name=\"loggedinuser\" value=\"";
       
   186 var forumPostHarvestString_poststarttime = "name=\"poststarttime\" value=\"";		
       
   187 var forumPostHarvestString_posthash = "name=\"posthash\" value=\"";		
       
   188 var forumPostHarvestString_securitytoken = "name=\"securitytoken\" value=\"";		
       
   189 		
       
   190 // Form has been received, extract important info
       
   191 function submitFormReady(){
       
   192 	uiManager.showNotification(-1, "wait", "Submitting...", -1);
       
   193 	isHideNotifications = false;
       
   194     if (submitUrlHttpReq.readyState == 4) {
       
   195         // attempt to get response status
       
   196         var responseStatus = null;
       
   197         try {
       
   198             responseStatus = submitUrlHttpReq.status;
       
   199         } catch (noStatusException) {}
       
   200         
       
   201 		
       
   202 		var content = submitUrlHttpReq.responseText;
       
   203 		checkForSecurityToken("submitFormReady", content);
       
   204 
       
   205 		// this is what we need to hardvest
       
   206 		var forumPostSecurityToken, forumPostHash, forumPostStartTime, forumPostLoggedInUser;
       
   207 		
       
   208 		if ( content.indexOf(forumPostHarvestString_loggedinuser) == -1 ) {
       
   209 			uiManager.showNotification(5000, "warning", "Submit failed.");	
       
   210 		} else {
       
   211 			forumPostLoggedInUser = extractFormField(content, forumPostHarvestString_loggedinuser);
       
   212 			forumPostStartTime = extractFormField(content, forumPostHarvestString_poststarttime);
       
   213 			forumPostHash = extractFormField(content, forumPostHarvestString_posthash);
       
   214 			forumPostSecurityToken = extractFormField(content, forumPostHarvestString_securitytoken);
       
   215 			
       
   216 			if (forumPostSecurityToken == null || forumPostSecurityToken.length < 5) {
       
   217 				if (!allowRetry) {
       
   218 					uiManager.showNotification(3000, "warning", "Failed, please try again...");
       
   219 				}
       
   220 				else {
       
   221 				    // workaround for a vBulletin bug, restart the process...
       
   222 					isHideNotifications = true;
       
   223 					login( function(){
       
   224 						if (reply) {
       
   225 							submitNewReply(submitTitle, // title
       
   226 							 submitContent, // message
       
   227 							 submitThreadId, // threadid
       
   228 							 submitPostId, // threadid
       
   229 							 submitCallback);
       
   230 						}
       
   231 						else {
       
   232 							submitNewTopic(submitTitle, // title
       
   233 								 submitContent, // message
       
   234 								 submitForumId, // forumid
       
   235 								 submitCallback);
       
   236 						}
       
   237 					});
       
   238 					// avoid loop
       
   239 					allowRetry = false;
       
   240 				}
       
   241 			} else {
       
   242 				doSubmitPost(submitTitle, submitContent, submitForumId, submitCallback, forumPostSecurityToken, forumPostHash, forumPostStartTime, forumPostLoggedInUser);
       
   243 			}
       
   244 		}
       
   245     }
       
   246 }
       
   247 
       
   248 // Send a POST request with our post information
       
   249 function doSubmitPost(title, message, forumid, callback, 
       
   250 			forumPostSecurityToken, forumPostHash, forumPostStartTime, forumPostLoggedInUser){
       
   251 	uiManager.showNotification(-1, "wait", "Submitting...", -1);
       
   252 	isHideNotifications = false;
       
   253 	var url = null;
       
   254 	var parameters = null;
       
   255 	
       
   256 	if (reply) {
       
   257 		// posting a reply to an article
       
   258 		url = symbianOrgNewReplyUrl + "do=postreply&t=" + submitThreadId;
       
   259 		parameters = "title=" + title + "&message=" + message +
       
   260 		"&wysiwyg=0&iconid=0&s=&securitytoken=" + forumPostSecurityToken +
       
   261 		"&do=postreply" +
       
   262 		"&t=" + submitThreadId + "&p=" + submitPostId + 
       
   263 		"&specifiedpost=0" +
       
   264 		"&posthash" + forumPostHash +
       
   265 		"&poststarttime=" + forumPostStartTime +
       
   266 		"&loggedinuser=" + forumPostLoggedInUser +
       
   267 		"&multiquoteempty=&sbutton=Submit+Reply&parseurl=1&emailupdate=9999&rating=0";
       
   268 	} else {
       
   269 		// posting a new thread
       
   270 		url = symbianOrgNewThreadUrl + "do=postthread&f=" + forumid;
       
   271 		parameters = "do=postthread&f=" + forumid + "&subject=" + title + "&message=" + message +
       
   272 		"&wysiwyg=0&taglist=&iconid=0&s=&securitytoken=" + forumPostSecurityToken +
       
   273 		"&posthash" + forumPostHash +
       
   274 		"&poststarttime=" + forumPostStartTime +
       
   275 		"&loggedinuser=" + forumPostLoggedInUser +
       
   276 		"&sbutton=Submit+New+Thread&parseurl=1&emailupdate=9999&polloptions=4";
       
   277 	}
       
   278 	
       
   279 	submitUrlHttpReq = new Ajax();
       
   280     submitUrlHttpReq.onreadystatechange = submitComplete;
       
   281     // initiate the request
       
   282 	submitUrlHttpReq.open('POST', url, true);
       
   283 	submitUrlHttpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
       
   284 	submitUrlHttpReq.setRequestHeader("Content-length", parameters.length);
       
   285 	submitUrlHttpReq.setRequestHeader("Connection", "close");
       
   286 	submitUrlHttpReq.send(parameters);
       
   287 }
       
   288 
       
   289 // Response to our POST has been received, analyse the result
       
   290 function submitComplete(){
       
   291     if (submitUrlHttpReq.readyState == 4) {
       
   292 		// attempt to get response status
       
   293 		var responseStatus = null;
       
   294 		try {
       
   295 			responseStatus = submitUrlHttpReq.status;
       
   296 		} 
       
   297 		catch (noStatusException) {
       
   298 		}
       
   299 		var content = submitUrlHttpReq.responseText;
       
   300 		if ( content.indexOf(submitTitle) == -1 ) {
       
   301 			uiManager.showNotification(3000, "warning", "Posting failed.");	
       
   302 		} else {
       
   303 			uiManager.showNotification(3000, "warning", "Please wait...");	
       
   304 			if ( submitCallback != null ) {
       
   305 				submitCallback.call();
       
   306 			}
       
   307 		}
       
   308 	}	
       
   309 }
       
   310 
       
   311 // Test weather page HTML contains a login form. This is useful in
       
   312 // being able to tell weather a login has been successfull, or if
       
   313 // we received login prompt instead of XML at any point.
       
   314 function isLoginPrompt (text) {
       
   315 	return text.indexOf("<title>Sign in</title>") != -1;
       
   316 }
       
   317 
       
   318 // Stores the current view, then shows the settings dialog
       
   319 // so that once settings dialog is closed, we go back to current screen
       
   320 function promptForPassword() {
       
   321 		if (uiManager.currentView == settings) {
       
   322 			settings.previousView = home;
       
   323 		}
       
   324 		else {
       
   325 			settings.previousView = uiManager.currentView;
       
   326 		}
       
   327 		uiManager.hideNotification();
       
   328 		settings.show();
       
   329 }
       
   330 
       
   331 function extractFormField(content, harvestString){
       
   332 	var startind = content.indexOf(harvestString);
       
   333 	if ( startind == -1 ) {
       
   334 		return null;
       
   335 	}
       
   336 	startind += harvestString.length;
       
   337 	var endind = content.indexOf("\"", startind);
       
   338 	return content.substring(startind, endind);
       
   339 }
       
   340 
       
   341 function checkForSecurityToken(where, content) {
       
   342 //	var stpos = content.indexOf("securitytoken");
       
   343 //	if ( stpos == -1 ) {
       
   344 //		var test = content.substring(stpos , stpos + 100);
       
   345 //		alert("securityToken not found in " + where + " : "+ test);
       
   346 //	}
       
   347 }