Symbian.org/RssReader.js
author ivanl
Tue, 16 Jun 2009 10:23:34 +0100
changeset 5 3a3bac500145
parent 2 99bc8e56b756
child 10 07ac2f6a36a9
permissions -rw-r--r--
Added public domain license notification NOTE - not all files in the package are public domain. Refer to in source comments for details.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
     1
///////////////////////////////////////////////////////////////////////////////
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
     2
// RSS Reader from WRTKit examples, modified for Symbian.org
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
     3
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
     4
// RSS reader class
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
     5
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
     6
function RssReader(aFeedName, aFeedUrl, aFeedPresenter, aParent, aResponseParser){
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
     7
	this.responseParser = aResponseParser;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
     8
	this.feedUpdateBroker = null;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
     9
	this.feedUpdateBrokerActive = false;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    10
	this.feedName = aFeedName;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    11
	this.feedURL = aFeedUrl;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    12
	this.feedUpdateForced = false;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    13
	this.feedItemControls = [];
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    14
	this.feedLastModified = 0;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    15
	this.ignoreContent = false;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    16
	this.hasData = false;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    17
	this.startFromItem = 0;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    18
	this.maxItems = 0;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    19
	
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    20
	var caption = createCaption(aFeedName);
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    21
	
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    22
	ListView.prototype.init.call(this, null, caption);
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    23
	this.previousView = aParent;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    24
	if (aFeedPresenter != null) {
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    25
		this.feedPresenter = aFeedPresenter;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    26
		this.feedPresenter.init(this);
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    27
	} else {
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    28
		this.feedPresenter = new HtmlFeedPresenter(this);
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    29
	}
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    30
	
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    31
	// add pre-amble items (latest posts, new thread, reply etc)
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    32
	this.feedPresenter.addPreambleItems();
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    33
}	
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    34
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    35
RssReader.prototype = new ListView(null, null);
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    36
	
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    37
// Callback function that gets called when a feed update has completed.
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    38
RssReader.prototype.feedUpdateCompleted = function(event){
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    39
	// remove cancel button
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    40
	this.setupSoftKeys();
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    41
	if (event.status == "ok") {
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    42
		// if there aren't any feed items yet, we'll hide the progress dialog
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    43
		
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    44
		// check if the feed has updated
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    45
		if (event.lastModified != this.feedLastModified) {
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    46
			// remember the last modified timestamp
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    47
			this.feedLastModified = event.lastModified;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    48
			
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    49
			// feed fetched and parsed successfully
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    50
			this.setFeedItems(event.items);
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    51
			
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    52
			// focus the first feed item control
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    53
			// (but only if we are in the main view)
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    54
			if (this.feedItemControls.length > 0 ) {
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    55
				this.feedItemControls[0].setFocused(true);
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    56
			}
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    57
		}
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    58
		uiManager.hideNotification();
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    59
	}
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    60
	else if (event.status != "cancelled") {
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    61
		// show error message
2
99bc8e56b756 Font size fix
ivanl
parents: 0
diff changeset
    62
		uiManager.showNotification(3000, "warning", "Error while updating feed!");
0
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    63
	}
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    64
	
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    65
	// reset the broker 
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    66
	this.feedUpdateBroker = null;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    67
	this.feedUpdateBrokerActive = false;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    68
	
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    69
	// reset commanded feed update flag
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    70
	this.feedUpdateForced = false;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    71
}
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    72
	
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    73
// Removes feed items.
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    74
RssReader.prototype.removeFeedItems = function (){
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    75
	// remove all current feed items from the main view
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    76
	for (var i = 0; i < this.feedItemControls.length; i++) {
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    77
		this.removeControl(this.feedItemControls[i]);
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    78
	}
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    79
	
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    80
	// reset feed item control array
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    81
	this.feedItemControls = [];
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    82
}
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    83
	
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    84
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    85
// Sets feed items.
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    86
RssReader.prototype.setFeedItems = function (items){
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    87
	// start by removing all current feed items
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    88
	this.removeFeedItems();
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    89
	
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    90
	if (items.length == 0) {
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    91
		this.feedPresenter.showNoItems();
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    92
	}
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    93
	else {
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    94
		// create new feed items and add them to the main view
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    95
		// use feed item pool to recycle controls
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    96
		for (var i = 0; i < items.length; i++) {
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    97
			var item = items[i];
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    98
			this.feedPresenter.show(item);
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
    99
		}
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   100
		
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   101
	}
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   102
	this.hasData = true;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   103
}
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   104
	
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   105
	
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   106
// Update feed
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   107
RssReader.prototype.update = function(forceFeedUpdate){
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   108
	if ( this.hasData && !forceFeedUpdate ) {
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   109
		return;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   110
	}		
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   111
	this.feedUpdateForced = forceFeedUpdate;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   112
	
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   113
	// check if a feed update has been scheduled, if it's time to update now,
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   114
	// and if there's no update currently in progress and if we're in the main view
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   115
	if ((this.feedURL != null) && (!this.feedUpdateBrokerActive) ) { //&& (uiManager.getView() == this) ) {
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   116
		// fetch the feed from the specified URL
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   117
		this.feedUpdateBrokerActive = true;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   118
		this.feedUpdateBroker = new FeedUpdateBroker();
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   119
		this.feedUpdateBroker.startFromItem = this.startFromItem;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   120
		this.feedUpdateBroker.maxItems = this.maxItems;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   121
		
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   122
		if ( this.responseParser != null ) {
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   123
			this.feedUpdateBroker.responseParser = this.responseParser;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   124
		}
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   125
		this.feedUpdateBroker.ignoreContent = this.ignoreContent;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   126
		this.feedUpdateBroker.fetchFeed(this.feedURL, this);
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   127
		// allow cancelling
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   128
	    if (window.widget) {
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   129
			var self = this;
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   130
			menu.setRightSoftkeyLabel("Cancel", function(){
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   131
				self.feedUpdateBroker.cancel(); self.setupSoftKeys();
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   132
				uiManager.hideNotification();
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   133
				});
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   134
	    }
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   135
		uiManager.showNotification(-1, "wait", "Loading feed...", -1);
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   136
	}
54498df70f5d Adding Symbian.org Widget 1.0rc3 source
ivanl
parents:
diff changeset
   137
}