See09/twitter.js
changeset 19 f3521a11d878
equal deleted inserted replaced
18:b73e6caf0031 19:f3521a11d878
       
     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 
       
    11 var twitterurl = "http://twitter.com/statuses/user_timeline/21138778.rss";
       
    12 
       
    13 function Twitter(parentView) {
       
    14 	this.parentView = parentView;
       
    15 	this.buttons = null;	
       
    16 	this.numToShow = 5;
       
    17 	this.http = null;
       
    18 }
       
    19 
       
    20 Twitter.prototype.Update = function(numToShow){
       
    21 	this.numToShow = numToShow;
       
    22 	if ( this.buttons == null ) {
       
    23 		// add the separator
       
    24 		var separator = new NavigationButton(null, "tweetz-icon.png", "<img src='tweetz.png' border=0>");
       
    25 		separator.setEnabled(false);
       
    26 		this.parentView.addControl(separator);
       
    27 		// create buttons
       
    28 		this.buttons = new Array();
       
    29 		for( var i = 0 ; i < this.numToShow; i++ ) {
       
    30 			var button = new NavigationButton("twitter_button_"+i, null , "");
       
    31 			this.parentView.addControl(button);
       
    32 			this.buttons.push(button);
       
    33 		}
       
    34 		this.buttons[0].setText("Loading twitter feed...");
       
    35 	}
       
    36 	var self = this;
       
    37 	// get the rss 
       
    38 	// Prepare synchronous download
       
    39 	this.http = new Ajax();
       
    40     this.http.open("GET", twitterurl , true); // false means synchronous
       
    41     this.http.onreadystatechange = function() { self.DoUpdate(); };
       
    42     this.http.send(null);
       
    43 }
       
    44 
       
    45 Twitter.prototype.DoUpdate = function(){
       
    46     if (this.http.readyState == 4) {
       
    47 	
       
    48 		try {
       
    49 			// Get parsed Doc
       
    50 			var xmlDoc = this.http.responseXML;
       
    51 			if (xmlDoc == null) {
       
    52 				// if the content type is not set correctly, we get the response as text
       
    53 				var xmlparser = new DOMParser();
       
    54 				xmlDoc = xmlparser.parseFromString(this.http.responseText, "text/xml");
       
    55 			}
       
    56 			var itemElements = xmlDoc.getElementsByTagName("item");
       
    57 			var loopEnd = Math.min(this.numToShow, itemElements.length);
       
    58 			// traverse elements and create buttons
       
    59 			for (var i = 0; i < loopEnd; i++) {
       
    60 				// iterate through child nodes of this item and gather tweets
       
    61 				var title = null;
       
    62 				var date = null;
       
    63 				
       
    64 				node = itemElements[i].firstChild;
       
    65 				while (node != null) {
       
    66 					if (node.nodeType == Node.ELEMENT_NODE) {
       
    67 						if (node.nodeName == "title") {
       
    68 							// item title
       
    69 							title = getTextOfNode(node);
       
    70 						}
       
    71 						else 
       
    72 							if (node.nodeName == "pubDate" || node.nodeName == "dc:date") {
       
    73 								// item publishing date
       
    74 								date = getTextOfNode(node);
       
    75 							}
       
    76 					}
       
    77 					node = node.nextSibling;
       
    78 				}
       
    79 				
       
    80 				this.buttons[i].setText("<font size=0.6em><i>" + date + "</i> " + title + "</font>");
       
    81 				this.buttons[i].setImage("tweet.png");
       
    82 			}
       
    83 		} 
       
    84 		catch (x) {
       
    85 			this.buttons[0].setText("<font size=0.6em>Uh-Oh! Tweetz not tweeting right now.</font>");
       
    86 			for (var i = 0; i < this.numToShow; i++) {
       
    87 				this.buttons[i].setText("");
       
    88 				this.buttons[i].setImage(null);
       
    89 			}
       
    90 		}
       
    91 	}
       
    92 }
       
    93 
       
    94 // Returns the text of a node.
       
    95 function getTextOfNode(node) {
       
    96     var buf = "";
       
    97 	// iterate through all child elements and collect all text to the buffer
       
    98 	var child = node.firstChild;
       
    99 	while (child != null) {
       
   100 		if (child.nodeType == Node.TEXT_NODE || child.nodeType == Node.CDATA_SECTION_NODE) {
       
   101 			// append text to buffer
       
   102 			if (buf != "") {
       
   103 				buf += " ";
       
   104 			}
       
   105 			buf += child.nodeValue;
       
   106 		}
       
   107 		child = child.nextSibling;
       
   108 	}
       
   109 	// make link if there is a url
       
   110 	var ind = buf.indexOf("http://");
       
   111 	var endind = buf.indexOf(" ", ind);
       
   112 	if ( ind != -1 ) {
       
   113 		var tmp = buf.substring(0,ind);
       
   114 		var url = "";
       
   115 		if ( endind == -1 ) {
       
   116 			url = buf.substring(ind);
       
   117 		} else {
       
   118 			url = buf.substring(ind, endind);
       
   119 		}
       
   120 		tmp += "<div class=\"FeedItemLink\">";
       
   121         tmp += "<a href=\"JavaScript:void(0)\" onclick=\"openURL('" + url + "'); return false;\">";
       
   122 		tmp += url + "</a></div>";
       
   123 			
       
   124 		if ( endind != -1 ) {
       
   125 			tmp += buf.substring(endind);
       
   126 		}
       
   127 		buf = tmp;
       
   128 	}
       
   129 	return buf;
       
   130 }