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