home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 120 / cdrom120.iso / internet / sage / sage.xpi / chrome / sage.jar / content / discover_feeds.js < prev    next >
Encoding:
Text File  |  2005-05-12  |  8.5 KB  |  231 lines

  1.  
  2. var strRes;
  3. var feedTree;
  4. var dataSource;
  5. var rdf;
  6. var ds;
  7. var rdfService;
  8. var schema;
  9. var document_host;
  10. var bookmarksTree;
  11. var progressMeter;
  12. var fetch_total;
  13. var fetch_done;
  14. var statusDeck;
  15. var statusMessage;
  16. var feeds_found_local;
  17. var feeds_found_external;
  18. var possibleFeeds;
  19.  
  20. function init() {
  21.     var discoveryMode = CommonFunc.getPrefValue(CommonFunc.FEED_DISCOVERY_MODE, "str", "exhaustive");
  22.  
  23.     initServices();
  24.     initBMService();
  25.  
  26.     strRes = document.getElementById("strRes");
  27.     statusDeck = document.getElementById("statusDeck");
  28.     statusMessage = document.getElementById("statusMessage");
  29.     progressMeter = document.getElementById("progress");
  30.     feedTree = document.getElementById("feedTree");
  31.  
  32.     dataSource = Components.classes["@mozilla.org/rdf/datasource;1?name=in-memory-datasource"];
  33.     rdf = Components.classes["@mozilla.org/rdf/rdf-service;1"];
  34.  
  35.     rdfService = rdf.getService(Components.interfaces.nsIRDFService);
  36.  
  37.     ds = dataSource.createInstance(Components.interfaces.nsIRDFInMemoryDataSource);
  38.     feedTree.database.AddDataSource(ds);
  39.  
  40.     schema = "http://sage.mozdev.org/FeedData#";
  41.  
  42.     ds = feedTree.database.GetDataSources();
  43.     ds = ( ds.getNext(), ds.getNext() );
  44.     ds = ds.QueryInterface(Components.interfaces.nsIRDFDataSource);
  45.  
  46.     var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService(Components.interfaces.nsIWindowMediator);
  47.     var browserWindow = windowManager.getMostRecentWindow("navigator:browser").document.getElementById("content");
  48.  
  49.     bookmarksTree = window.arguments[0];
  50.  
  51.     var current_document = browserWindow.contentDocument;
  52.  
  53.     document_host = current_document.location.host;
  54.     if(document_host.match(/^www\./i)) {
  55.         document_host = document_host.substring(4, document_host.length);
  56.     }
  57.  
  58.     possibleFeeds = new Array();
  59.  
  60.     var links, c;
  61.     // Allowing file: might not seem good but the XMLHttpRequest will prevent
  62.     // access to the file system if needed.
  63.     var uriSchemeRe = /^(http|https|ftp|file):$/;
  64.  
  65.     links = current_document.getElementsByTagName("a");
  66.  
  67.     if(discoveryMode == "exhaustive") {
  68.         for(c = 0; c < links.length; c++) {
  69.             if(uriSchemeRe.test(links[c].protocol) && links[c].href.match(/xml$|rss|rdf|atom|feed|syndicate/i)) {
  70.                 possibleFeeds[links[c].href] = Array(links[c].href, "implicit");
  71.             }
  72.         }
  73.     } else {
  74.         for(c = 0; c < links.length; c++) {
  75.             if(uriSchemeRe.test(links[c].protocol) &&
  76.                     links[c].href.match(/xml$|rss|rdf|atom|feed|syndicate/i) &&
  77.                     links[c].host.match(new RegExp(document_host, "i"))) {
  78.                 possibleFeeds[links[c].href] = Array(links[c].href, "implicit");
  79.             }
  80.         }
  81.     }
  82.  
  83.     links = current_document.getElementsByTagName("link");
  84.     for(c = 0; c < links.length; c++) {
  85.         if(links[c].rel == "alternate" && (links[c].type == "text/xml" || links[c].type == "application/atom+xml" || links[c].type == "application/rss+xml")) {
  86.             possibleFeeds[links[c].href] = Array(links[c].href, "explicit");
  87.         }
  88.     }
  89.  
  90.     fetch_total = 0;
  91.     fetch_done = 0;
  92.     feeds_found_local = 0;
  93.     feeds_found_external = 0;
  94.  
  95.     for(entry in possibleFeeds) {
  96.         fetch_total++;
  97.     }
  98.  
  99.     if(fetch_total == 0) {
  100.         progressUpdate();
  101.     }
  102.  
  103.     logMessage("found " + fetch_total + " potential feed URI(s) in " + current_document.location);
  104.  
  105.     var httpReq;
  106.     for(entry in possibleFeeds) {
  107.         httpReq = new XMLHttpRequest();
  108.         httpReq.onload = httpLoaded;
  109.         httpReq.onerror = httpError;
  110.         try {
  111.             httpReq.open("GET", possibleFeeds[entry][0], true);
  112.             httpReq.setRequestHeader("User-Agent", CommonFunc.USER_AGENT);
  113.             httpReq.overrideMimeType("application/xml");
  114.             httpReq.send(null);
  115.         } catch(e) {
  116.             httpReq.abort();
  117.             progressUpdate();
  118.         }
  119.     }
  120. }
  121.  
  122. function progressUpdate() {
  123.     fetch_done++;
  124.     progressMeter.value = Math.round((fetch_done/fetch_total) * 100);
  125.     if(fetch_done >= fetch_total) {
  126.         if((feeds_found_local + feeds_found_external) == 0) {
  127.             statusMessage.value = strRes.getString("discovery_status_none_found") + ".";
  128.         } else {
  129.             var message = "";
  130.             if(feeds_found_local > 1) message += feeds_found_local + " " + strRes.getString("discovery_status_site_feeds");
  131.             if(feeds_found_local == 1) message += feeds_found_local + " " + strRes.getString("discovery_status_site_feed");
  132.             if(feeds_found_local > 0 && feeds_found_external > 0) message += " " + strRes.getString("discovery_status_and") + " ";
  133.             if(feeds_found_external > 1) message += feeds_found_external + " " + strRes.getString("discovery_status_external_feeds");
  134.             if(feeds_found_external == 1) message += feeds_found_external + " " + strRes.getString("discovery_status_external_feed");
  135.             statusMessage.value = strRes.getString("discovery_status_discovered") + " " + message + ":";
  136.         }
  137.         statusDeck.selectedIndex = 1;
  138.     }
  139. }
  140.  
  141. function doAddFeed() {
  142.     var index = feedTree.view.selection.currentIndex;
  143.     if(index != -1) {
  144.         var url = feedTree.view.getCellText(index, "url");
  145.         var title = feedTree.view.getCellText(index, "title");
  146.         if(url) {
  147.             if(title == "") {
  148.                 title = "No Title";
  149.             }
  150.             var sage_folder = rdfService.GetResource(CommonFunc.getPrefValue(CommonFunc.FEED_FOLDER_ID, "str", "NC:BookmarksRoot"));
  151.             if(BMSVC.createBookmarkInContainer.length == 7) { // firefox 0.8 and lower
  152.                 BMSVC.createBookmarkInContainer(title, url, null, "updated", null, sage_folder, null);
  153.             } else {
  154.                 BMSVC.createBookmarkInContainer(title, url, null, "updated", null, null, sage_folder, null);
  155.             }
  156.             logMessage("added feed: '" + title + "' " + url);
  157.  
  158.             // select new feed in sibebar
  159.             var bm_index = bookmarksTree.treeBoxObject.view.rowCount - 1;
  160.             bookmarksTree.treeBoxObject.ensureRowIsVisible(bm_index);
  161.             bookmarksTree.treeBoxObject.selection.select(bm_index);
  162.         }
  163.     }
  164.   return true;
  165. }
  166.  
  167. function doClose() {
  168.   return true;
  169. }
  170.  
  171. function httpError() {
  172.     progressUpdate();
  173. }
  174.  
  175. function httpLoaded(e) {
  176.     var httpReq = e.target;
  177.     var uri = httpReq.channel.originalURI;
  178.     try {
  179.         var feed = new Feed(httpReq.responseXML);
  180.         addDiscoveredFeed(uri, feed);
  181.     } catch(e) { }
  182.     progressUpdate();
  183. }
  184.  
  185. function addDiscoveredFeed(uri, feed) {
  186.     var feedClass, lastPubDate, itemCount;
  187.     if(uri.host.match(new RegExp(document_host, "i"))) {  // feed is local
  188.         if(feeds_found_local == 0) {
  189.             //ds.Assert(rdfService.GetResource(schema + "Feeds"), rdfService.GetResource(schema + "child"), rdfService.GetResource(schema + "LocalFeeds"), true);
  190.             //ds.Assert(rdfService.GetResource(schema + "LocalFeeds"), rdfService.GetResource(schema + "Title"), rdfService.GetLiteral("Site Feeds"), true);
  191.             //ds.Assert(rdfService.GetResource(schema + "LocalFeeds"), rdfService.GetResource(schema + "Valuation"), rdfService.GetIntLiteral(1), true);
  192.         }
  193.         feedClass = "Feeds";
  194.         feeds_found_local++;
  195.     } else {  // feed is external
  196.         if(feeds_found_external == 0) {
  197.             ds.Assert(rdfService.GetResource(schema + "Feeds"), rdfService.GetResource(schema + "child"), rdfService.GetResource(schema + "ExternalFeeds"), true);
  198.             ds.Assert(rdfService.GetResource(schema + "ExternalFeeds"), rdfService.GetResource(schema + "Title"), rdfService.GetLiteral(strRes.getString("discovery_external_feeds_category")), true);
  199.             ds.Assert(rdfService.GetResource(schema + "ExternalFeeds"), rdfService.GetResource(schema + "Valuation"), rdfService.GetIntLiteral(0), true);
  200.         }
  201.         feedClass = "ExternalFeeds";
  202.         feeds_found_external++;
  203.     }
  204.  
  205.     var twelveHourClock = CommonFunc.getPrefValue(CommonFunc.TWELVE_HOUR_CLOCK, "bool", false);
  206.     lastPubDate = "N/A";
  207.     if(feed.hasLastPubDate()) {
  208.         lastPubDate = dateFormat(feed.getLastPubDate(), twelveHourClock, 1);
  209.     }
  210.     itemCount = feed.getItemCount();
  211.  
  212.     // feed valuation
  213.     var valuation = 0;
  214.     if(possibleFeeds[uri.spec][1] == "explicit") valuation += 100;
  215.     if(feedClass == "Feeds") valuation += 10;
  216.     if(feed.hasLastPubDate()) valuation += 1;
  217.  
  218.     ds.Assert(rdfService.GetResource(schema + feedClass), rdfService.GetResource(schema + "child"), rdfService.GetResource(schema + uri.spec), true);
  219.  
  220.     ds.Assert(rdfService.GetResource(schema + uri.spec), rdfService.GetResource(schema + "Title"), rdfService.GetLiteral(feed.getTitle()), true);
  221.     ds.Assert(rdfService.GetResource(schema + uri.spec), rdfService.GetResource(schema + "Format"), rdfService.GetLiteral(feed.getFormat()), true);
  222.     ds.Assert(rdfService.GetResource(schema + uri.spec), rdfService.GetResource(schema + "URL"), rdfService.GetLiteral(uri.spec), true);
  223.     ds.Assert(rdfService.GetResource(schema + uri.spec), rdfService.GetResource(schema + "LastPubDate"), rdfService.GetLiteral(lastPubDate), true);
  224.     ds.Assert(rdfService.GetResource(schema + uri.spec), rdfService.GetResource(schema + "ItemCount"), rdfService.GetLiteral(itemCount), true);
  225.     ds.Assert(rdfService.GetResource(schema + uri.spec), rdfService.GetResource(schema + "Valuation"), rdfService.GetIntLiteral(valuation), true);
  226.  
  227.     feedTree.builder.rebuild();
  228.  
  229.     logMessage("discovered feed: " + uri.spec);
  230. }
  231.