[Sage] patch - Unread items count

Graham King graham at darkcoding.net
Sun Mar 19 23:17:10 EST 2006


	The thing I missed most when I moved from using Thunderbird's feed 
reader to Sage is knowing how many unread items there are on each feed. 
So, in the spirit of open source...

  Attached is a patch to apply to the latest version of Sage from CVS. 
The modified files are:
  - commonfunc.js
  - sage.js
  - updatechecker.js

  If like me you really want this feature now, you can download an 
experimental .xpi from here:
  http://www.darkcoding.net/software/sage-unread-posts-count-patch/

  Could this be considered for inclusion in the official Sage ?

  I'd welcome any feedback to improve it. For example I am storing the 
unread count as a preference, as I wasn't sure of another way to store 
persistent data. Also the two new methods in commonfunc.js should really 
go in sageFeed.js, but I could not figure out a way of getting an object 
past the .idl.

  Best regards,
  Graham.
-------------- next part --------------
? unread-count.patch
Index: commonfunc.js
===================================================================
RCS file: /cvs/sage/src/sage/content/commonfunc.js,v
retrieving revision 1.21
diff -u -5 -p -r1.21 commonfunc.js
--- commonfunc.js	19 Dec 2005 08:44:37 -0000	1.21
+++ commonfunc.js	19 Mar 2006 22:10:59 -0000
@@ -49,11 +49,12 @@ var CommonFunc = {
 	AUTO_FEED_TITLE: "sage.auto_feed_title",
 	RENDER_FEEDS: "sage.render_feeds",
 	TWELVE_HOUR_CLOCK: "sage.twelve_hour_clock",
 	FEED_ITEM_ORDER: "sage.feed_item_order",
 	FEED_DISCOVERY_MODE: "sage.feed_discovery_mode",
-
+	
+	UNREAD_COUNT_PREFIX:	"sage.unread_counts.",
 
 	RESULT_OK:	 			0,
 	RESULT_PARSE_ERROR:		1,
 	RESULT_NOT_RSS:			2,
 	RESULT_NOT_FOUND:		3,
@@ -69,11 +70,11 @@ var CommonFunc = {
 	BM_LAST_MODIFIED:	"http://home.netscape.com/WEB-rdf#LastModifiedDate",
 	BM_DESCRIPTION:		"http://home.netscape.com/NC-rdf#Description",
 	BM_NAME:				"http://home.netscape.com/NC-rdf#Name",
 	BM_URL:				"http://home.netscape.com/NC-rdf#URL",
 	BM_FEEDURL:			"http://home.netscape.com/NC-rdf#FeedURL",
-
+	
 	RDF_NS:				"http://www.w3.org/1999/02/22-rdf-syntax-ns#",
 	RDF_TYPE:			"http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
 	
 	XUL_NS:				"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
 
@@ -362,8 +363,39 @@ var CommonFunc = {
 			formatted += version[2] != 0 ? "." + version[2] : ""
 		} else {
 			formatted = version[0].toString() + '.' + version[1].toString() + '.' + version[2].toString();
 		}
 		return formatted;
+	},
+	
+	getUnreadItemCount: function(feed) {
+	
+		return CommonFunc.getPrefValue( 
+					CommonFunc.UNREAD_COUNT_PREFIX + feed.getSignature(), 
+					"int", 
+					0);	
+	},
+	
+	updateUnreadItemCount: function(feed, resource) {
+
+		var newItemCount = 0;
+		for ( var i=0; i< feed.getItemCount(); i++) {
+			var feedItem = feed.getItem(i);
+			if ( ! linkVisitor.getVisited(feedItem.getLink()) ) {
+				newItemCount++;
+			}
+		}
+
+		CommonFunc.setPrefValue( 
+			CommonFunc.UNREAD_COUNT_PREFIX + feed.getSignature(),
+			"int",
+			newItemCount);
+			
+		var fullTitle = feed.getTitle();
+		if ( newItemCount > 0 ) {
+			fullTitle += " ("+ newItemCount +")";
+		}
+		
+		CommonFunc.setBMDSProperty(resource, CommonFunc.BM_NAME, fullTitle);
 	}
 
 }
Index: sage.js
===================================================================
RCS file: /cvs/sage/src/sage/content/sage.js,v
retrieving revision 1.55
diff -u -5 -p -r1.55 sage.js
--- sage.js	20 Dec 2005 09:18:48 -0000	1.55
+++ sage.js	19 Mar 2006 22:11:00 -0000
@@ -500,14 +500,17 @@ function onFeedLoaded(aFeed)
 {
 	currentFeed = aFeed;
 
 	if (lastResource && lastResource.res) {
 		if (CommonFunc.getPrefValue(CommonFunc.AUTO_FEED_TITLE, "bool", true)) {
-			var title = aFeed.getTitle()
+			var title = aFeed.getTitle();
+						
 			if (CommonFunc.getBMDSProperty(lastResource.res, CommonFunc.BM_NAME) != title) {
 				CommonFunc.setBMDSProperty(lastResource.res, CommonFunc.BM_NAME, title);
 			}
+			
+			CommonFunc.updateUnreadItemCount(aFeed, lastResource.res);
 		}
 
 		BMSVC.updateLastVisitedDate(lastResource.url, "UTF-8");
 		CommonFunc.setBMDSProperty(lastResource.res, CommonFunc.BM_DESCRIPTION, CommonFunc.STATUS_NO_UPDATE + " [" + currentFeed.getSignature() + "]");
 	}
@@ -733,18 +736,24 @@ var linkVisitor = {
 	addItem: function (aURI, aListItem) {
 		this._items[aURI] = aListItem;
 	},
 	
 	onURIChanged: function (aURI) {
+	
 		if (aURI in this._items) {
 			var listItem = this._items[aURI];
 			if (this.getVisited(aURI)) {
 				listItem.setAttribute("visited", "true");
 			} else {
 				listItem.removeAttribute("visited");
 			}
 		}
+		
+		if (currentFeed) {
+			CommonFunc.updateUnreadItemCount(currentFeed, lastResource.res);
+		}
+		
 	}
 };
 linkVisitor.init();
 
 
@@ -788,10 +797,12 @@ function markAllReadState(bRead) {
 			if (bRead)
 				listItem.setAttribute("visited", "true");
 			else
 				listItem.removeAttribute("visited");
 		}
+		
+		CommonFunc.updateUnreadItemCount(currentFeed, lastResource.res);
 	}
 }
 
 
 /**
@@ -810,10 +821,12 @@ function markReadState(bRead) {
 			listItem.setAttribute("visited", "true");
 		else
 			listItem.removeAttribute("visited");
 		linkVisitor.setVisited(uri, bRead);
 	}
+	
+	CommonFunc.updateUnreadItemCount(currentFeed, lastResource.res);
 }
 
 /**
  * This toggles the selected items read state. This works with multiple
  * selection as well if we want to enable that in the future.
@@ -836,10 +849,12 @@ function toggleMarkAsRead() {
 			listItem.setAttribute("visited", "true");
 		else
 			listItem.removeAttribute("visited");
 		linkVisitor.setVisited(uri, read);
 	}
+	
+	CommonFunc.updateUnreadItemCount(currentFeed, lastResource.res);
 }
 
 
 /**
  * This controller object takes care of the commands related to marking feed
@@ -908,10 +923,11 @@ var readStateController = {
 		}
 
 		return false;
 	},
 	doCommand : function(cmd) {
+				
 		switch (cmd) {
 			case "cmd_markasread":
 				markReadState(true);
 				break;
 
Index: updatechecker.js
===================================================================
RCS file: /cvs/sage/src/sage/content/updatechecker.js,v
retrieving revision 1.24
diff -u -5 -p -r1.24 updatechecker.js
--- updatechecker.js	19 Dec 2005 08:44:37 -0000	1.24
+++ updatechecker.js	19 Mar 2006 22:11:00 -0000
@@ -67,11 +67,11 @@ var UpdateChecker = {
 		// select feeds to be checked, exclude separators and updated feeds
 		for(var i = 0; i < resourceList.length; i++) {
 			var url = this.getURL(resourceList[i]);
 			var desc = CommonFunc.getBMDSProperty(resourceList[i], CommonFunc.BM_DESCRIPTION);
 			var status = desc.split(" ")[0];
-			if(url && !(status == CommonFunc.STATUS_UPDATE || status == CommonFunc.STATUS_NO_CHECK)) {
+			if(url && status != CommonFunc.STATUS_NO_CHECK ) {
 				this.checkList.push(resourceList[i]);
 			}
 		}
 
 		this.logger.info("found " + this.checkList.length + " feed(s) to check");
@@ -198,10 +198,12 @@ var UpdateChecker = {
 			}
 		} else {
 			status = CommonFunc.STATUS_ERROR;
 		}
 
+		CommonFunc.updateUnreadItemCount(feed, this.lastResource);
+		
 		CommonFunc.setBMDSProperty(this.lastResource, CommonFunc.BM_DESCRIPTION, status + " " + (CommonFunc.getBMDSProperty(this.lastResource, CommonFunc.BM_DESCRIPTION).match(/\[.*\]/) || ""));
 		this.setCheckingFlag(this.lastResource, false);
 		
 		if(this.checkList.length == 0) {
 			this.checking = false;


More information about the Sage mailing list