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