michael@0: # -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cr = Components.results; michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: const FEEDWRITER_CID = Components.ID("{49bb6593-3aff-4eb3-a068-2712c28bd58e}"); michael@0: const FEEDWRITER_CONTRACTID = "@mozilla.org/browser/feeds/result-writer;1"; michael@0: michael@0: function LOG(str) { michael@0: var prefB = Cc["@mozilla.org/preferences-service;1"]. michael@0: getService(Ci.nsIPrefBranch); michael@0: michael@0: var shouldLog = false; michael@0: try { michael@0: shouldLog = prefB.getBoolPref("feeds.log"); michael@0: } michael@0: catch (ex) { michael@0: } michael@0: michael@0: if (shouldLog) michael@0: dump("*** Feeds: " + str + "\n"); michael@0: } michael@0: michael@0: /** michael@0: * Wrapper function for nsIIOService::newURI. michael@0: * @param aURLSpec michael@0: * The URL string from which to create an nsIURI. michael@0: * @returns an nsIURI object, or null if the creation of the URI failed. michael@0: */ michael@0: function makeURI(aURLSpec, aCharset) { michael@0: var ios = Cc["@mozilla.org/network/io-service;1"]. michael@0: getService(Ci.nsIIOService); michael@0: try { michael@0: return ios.newURI(aURLSpec, aCharset, null); michael@0: } catch (ex) { } michael@0: michael@0: return null; michael@0: } michael@0: michael@0: const XML_NS = "http://www.w3.org/XML/1998/namespace"; michael@0: const HTML_NS = "http://www.w3.org/1999/xhtml"; michael@0: const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; michael@0: const TYPE_MAYBE_FEED = "application/vnd.mozilla.maybe.feed"; michael@0: const TYPE_MAYBE_AUDIO_FEED = "application/vnd.mozilla.maybe.audio.feed"; michael@0: const TYPE_MAYBE_VIDEO_FEED = "application/vnd.mozilla.maybe.video.feed"; michael@0: const URI_BUNDLE = "chrome://browser/locale/feeds/subscribe.properties"; michael@0: michael@0: const PREF_SELECTED_APP = "browser.feeds.handlers.application"; michael@0: const PREF_SELECTED_WEB = "browser.feeds.handlers.webservice"; michael@0: const PREF_SELECTED_ACTION = "browser.feeds.handler"; michael@0: const PREF_SELECTED_READER = "browser.feeds.handler.default"; michael@0: michael@0: const PREF_VIDEO_SELECTED_APP = "browser.videoFeeds.handlers.application"; michael@0: const PREF_VIDEO_SELECTED_WEB = "browser.videoFeeds.handlers.webservice"; michael@0: const PREF_VIDEO_SELECTED_ACTION = "browser.videoFeeds.handler"; michael@0: const PREF_VIDEO_SELECTED_READER = "browser.videoFeeds.handler.default"; michael@0: michael@0: const PREF_AUDIO_SELECTED_APP = "browser.audioFeeds.handlers.application"; michael@0: const PREF_AUDIO_SELECTED_WEB = "browser.audioFeeds.handlers.webservice"; michael@0: const PREF_AUDIO_SELECTED_ACTION = "browser.audioFeeds.handler"; michael@0: const PREF_AUDIO_SELECTED_READER = "browser.audioFeeds.handler.default"; michael@0: michael@0: const PREF_SHOW_FIRST_RUN_UI = "browser.feeds.showFirstRunUI"; michael@0: michael@0: const TITLE_ID = "feedTitleText"; michael@0: const SUBTITLE_ID = "feedSubtitleText"; michael@0: michael@0: function getPrefAppForType(t) { michael@0: switch (t) { michael@0: case Ci.nsIFeed.TYPE_VIDEO: michael@0: return PREF_VIDEO_SELECTED_APP; michael@0: michael@0: case Ci.nsIFeed.TYPE_AUDIO: michael@0: return PREF_AUDIO_SELECTED_APP; michael@0: michael@0: default: michael@0: return PREF_SELECTED_APP; michael@0: } michael@0: } michael@0: michael@0: function getPrefWebForType(t) { michael@0: switch (t) { michael@0: case Ci.nsIFeed.TYPE_VIDEO: michael@0: return PREF_VIDEO_SELECTED_WEB; michael@0: michael@0: case Ci.nsIFeed.TYPE_AUDIO: michael@0: return PREF_AUDIO_SELECTED_WEB; michael@0: michael@0: default: michael@0: return PREF_SELECTED_WEB; michael@0: } michael@0: } michael@0: michael@0: function getPrefActionForType(t) { michael@0: switch (t) { michael@0: case Ci.nsIFeed.TYPE_VIDEO: michael@0: return PREF_VIDEO_SELECTED_ACTION; michael@0: michael@0: case Ci.nsIFeed.TYPE_AUDIO: michael@0: return PREF_AUDIO_SELECTED_ACTION; michael@0: michael@0: default: michael@0: return PREF_SELECTED_ACTION; michael@0: } michael@0: } michael@0: michael@0: function getPrefReaderForType(t) { michael@0: switch (t) { michael@0: case Ci.nsIFeed.TYPE_VIDEO: michael@0: return PREF_VIDEO_SELECTED_READER; michael@0: michael@0: case Ci.nsIFeed.TYPE_AUDIO: michael@0: return PREF_AUDIO_SELECTED_READER; michael@0: michael@0: default: michael@0: return PREF_SELECTED_READER; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Converts a number of bytes to the appropriate unit that results in a michael@0: * number that needs fewer than 4 digits michael@0: * michael@0: * @return a pair: [new value with 3 sig. figs., its unit] michael@0: */ michael@0: function convertByteUnits(aBytes) { michael@0: var units = ["bytes", "kilobyte", "megabyte", "gigabyte"]; michael@0: let unitIndex = 0; michael@0: michael@0: // convert to next unit if it needs 4 digits (after rounding), but only if michael@0: // we know the name of the next unit michael@0: while ((aBytes >= 999.5) && (unitIndex < units.length - 1)) { michael@0: aBytes /= 1024; michael@0: unitIndex++; michael@0: } michael@0: michael@0: // Get rid of insignificant bits by truncating to 1 or 0 decimal points michael@0: // 0 -> 0; 1.2 -> 1.2; 12.3 -> 12.3; 123.4 -> 123; 234.5 -> 235 michael@0: aBytes = aBytes.toFixed((aBytes > 0) && (aBytes < 100) ? 1 : 0); michael@0: michael@0: return [aBytes, units[unitIndex]]; michael@0: } michael@0: michael@0: function FeedWriter() {} michael@0: FeedWriter.prototype = { michael@0: _mimeSvc : Cc["@mozilla.org/mime;1"]. michael@0: getService(Ci.nsIMIMEService), michael@0: michael@0: _getPropertyAsBag: function FW__getPropertyAsBag(container, property) { michael@0: return container.fields.getProperty(property). michael@0: QueryInterface(Ci.nsIPropertyBag2); michael@0: }, michael@0: michael@0: _getPropertyAsString: function FW__getPropertyAsString(container, property) { michael@0: try { michael@0: return container.fields.getPropertyAsAString(property); michael@0: } michael@0: catch (e) { michael@0: } michael@0: return ""; michael@0: }, michael@0: michael@0: _setContentText: function FW__setContentText(id, text) { michael@0: this._contentSandbox.element = this._document.getElementById(id); michael@0: this._contentSandbox.textNode = text.createDocumentFragment(this._contentSandbox.element); michael@0: var codeStr = michael@0: "while (element.hasChildNodes()) " + michael@0: " element.removeChild(element.firstChild);" + michael@0: "element.appendChild(textNode);"; michael@0: if (text.base) { michael@0: this._contentSandbox.spec = text.base.spec; michael@0: codeStr += "element.setAttributeNS('" + XML_NS + "', 'base', spec);"; michael@0: } michael@0: Cu.evalInSandbox(codeStr, this._contentSandbox); michael@0: this._contentSandbox.element = null; michael@0: this._contentSandbox.textNode = null; michael@0: }, michael@0: michael@0: /** michael@0: * Safely sets the href attribute on an anchor tag, providing the URI michael@0: * specified can be loaded according to rules. michael@0: * @param element michael@0: * The element to set a URI attribute on michael@0: * @param attribute michael@0: * The attribute of the element to set the URI to, e.g. href or src michael@0: * @param uri michael@0: * The URI spec to set as the href michael@0: */ michael@0: _safeSetURIAttribute: michael@0: function FW__safeSetURIAttribute(element, attribute, uri) { michael@0: var secman = Cc["@mozilla.org/scriptsecuritymanager;1"]. michael@0: getService(Ci.nsIScriptSecurityManager); michael@0: const flags = Ci.nsIScriptSecurityManager.DISALLOW_INHERIT_PRINCIPAL; michael@0: try { michael@0: secman.checkLoadURIStrWithPrincipal(this._feedPrincipal, uri, flags); michael@0: // checkLoadURIStrWithPrincipal will throw if the link URI should not be michael@0: // loaded, either because our feedURI isn't allowed to load it or per michael@0: // the rules specified in |flags|, so we'll never "linkify" the link... michael@0: } michael@0: catch (e) { michael@0: // Not allowed to load this link because secman.checkLoadURIStr threw michael@0: return; michael@0: } michael@0: michael@0: this._contentSandbox.element = element; michael@0: this._contentSandbox.uri = uri; michael@0: var codeStr = "element.setAttribute('" + attribute + "', uri);"; michael@0: Cu.evalInSandbox(codeStr, this._contentSandbox); michael@0: }, michael@0: michael@0: /** michael@0: * Use this sandbox to run any dom manipulation code on nodes which michael@0: * are already inserted into the content document. michael@0: */ michael@0: __contentSandbox: null, michael@0: get _contentSandbox() { michael@0: // This whole sandbox setup is totally archaic. It was introduced in bug michael@0: // 360529, presumably before the existence of a solid security membrane, michael@0: // since all of the manipulation of content here should be made safe by michael@0: // Xrays. And now that anonymous content is no longer content-accessible, michael@0: // manipulating the xml stylesheet content can't be done from content michael@0: // anymore. michael@0: // michael@0: // The right solution would be to rip out all of this sandbox junk and michael@0: // manipulate the DOM directly. But that's a big yak to shave, so for now, michael@0: // we just give the sandbox an nsExpandedPrincipal with []. This has the michael@0: // effect of giving it Xrays, and making it same-origin with the XBL scope, michael@0: // thereby letting it manipulate anonymous content. michael@0: if (!this.__contentSandbox) michael@0: this.__contentSandbox = new Cu.Sandbox([this._window], michael@0: {sandboxName: 'FeedWriter'}); michael@0: michael@0: return this.__contentSandbox; michael@0: }, michael@0: michael@0: /** michael@0: * Calls doCommand for a given XUL element within the context of the michael@0: * content document. michael@0: * michael@0: * @param aElement michael@0: * the XUL element to call doCommand() on. michael@0: */ michael@0: _safeDoCommand: function FW___safeDoCommand(aElement) { michael@0: this._contentSandbox.element = aElement; michael@0: Cu.evalInSandbox("element.doCommand();", this._contentSandbox); michael@0: this._contentSandbox.element = null; michael@0: }, michael@0: michael@0: __faviconService: null, michael@0: get _faviconService() { michael@0: if (!this.__faviconService) michael@0: this.__faviconService = Cc["@mozilla.org/browser/favicon-service;1"]. michael@0: getService(Ci.nsIFaviconService); michael@0: michael@0: return this.__faviconService; michael@0: }, michael@0: michael@0: __bundle: null, michael@0: get _bundle() { michael@0: if (!this.__bundle) { michael@0: this.__bundle = Cc["@mozilla.org/intl/stringbundle;1"]. michael@0: getService(Ci.nsIStringBundleService). michael@0: createBundle(URI_BUNDLE); michael@0: } michael@0: return this.__bundle; michael@0: }, michael@0: michael@0: _getFormattedString: function FW__getFormattedString(key, params) { michael@0: return this._bundle.formatStringFromName(key, params, params.length); michael@0: }, michael@0: michael@0: _getString: function FW__getString(key) { michael@0: return this._bundle.GetStringFromName(key); michael@0: }, michael@0: michael@0: /* Magic helper methods to be used instead of xbl properties */ michael@0: _getSelectedItemFromMenulist: function FW__getSelectedItemFromList(aList) { michael@0: var node = aList.firstChild.firstChild; michael@0: while (node) { michael@0: if (node.localName == "menuitem" && node.getAttribute("selected") == "true") michael@0: return node; michael@0: michael@0: node = node.nextSibling; michael@0: } michael@0: michael@0: return null; michael@0: }, michael@0: michael@0: _setCheckboxCheckedState: function FW__setCheckboxCheckedState(aCheckbox, aValue) { michael@0: // see checkbox.xml, xbl bindings are not applied within the sandbox! michael@0: this._contentSandbox.checkbox = aCheckbox; michael@0: var codeStr; michael@0: var change = (aValue != (aCheckbox.getAttribute('checked') == 'true')); michael@0: if (aValue) michael@0: codeStr = "checkbox.setAttribute('checked', 'true'); "; michael@0: else michael@0: codeStr = "checkbox.removeAttribute('checked'); "; michael@0: michael@0: if (change) { michael@0: this._contentSandbox.document = this._document; michael@0: codeStr += "var event = document.createEvent('Events'); " + michael@0: "event.initEvent('CheckboxStateChange', true, true);" + michael@0: "checkbox.dispatchEvent(event);" michael@0: } michael@0: michael@0: Cu.evalInSandbox(codeStr, this._contentSandbox); michael@0: }, michael@0: michael@0: /** michael@0: * Returns a date suitable for displaying in the feed preview. michael@0: * If the date cannot be parsed, the return value is "false". michael@0: * @param dateString michael@0: * A date as extracted from a feed entry. (entry.updated) michael@0: */ michael@0: _parseDate: function FW__parseDate(dateString) { michael@0: // Convert the date into the user's local time zone michael@0: dateObj = new Date(dateString); michael@0: michael@0: // Make sure the date we're given is valid. michael@0: if (!dateObj.getTime()) michael@0: return false; michael@0: michael@0: var dateService = Cc["@mozilla.org/intl/scriptabledateformat;1"]. michael@0: getService(Ci.nsIScriptableDateFormat); michael@0: return dateService.FormatDateTime("", dateService.dateFormatLong, dateService.timeFormatNoSeconds, michael@0: dateObj.getFullYear(), dateObj.getMonth()+1, dateObj.getDate(), michael@0: dateObj.getHours(), dateObj.getMinutes(), dateObj.getSeconds()); michael@0: }, michael@0: michael@0: /** michael@0: * Returns the feed type. michael@0: */ michael@0: __feedType: null, michael@0: _getFeedType: function FW__getFeedType() { michael@0: if (this.__feedType != null) michael@0: return this.__feedType; michael@0: michael@0: try { michael@0: // grab the feed because it's got the feed.type in it. michael@0: var container = this._getContainer(); michael@0: var feed = container.QueryInterface(Ci.nsIFeed); michael@0: this.__feedType = feed.type; michael@0: return feed.type; michael@0: } catch (ex) { } michael@0: michael@0: return Ci.nsIFeed.TYPE_FEED; michael@0: }, michael@0: michael@0: /** michael@0: * Maps a feed type to a maybe-feed mimetype. michael@0: */ michael@0: _getMimeTypeForFeedType: function FW__getMimeTypeForFeedType() { michael@0: switch (this._getFeedType()) { michael@0: case Ci.nsIFeed.TYPE_VIDEO: michael@0: return TYPE_MAYBE_VIDEO_FEED; michael@0: michael@0: case Ci.nsIFeed.TYPE_AUDIO: michael@0: return TYPE_MAYBE_AUDIO_FEED; michael@0: michael@0: default: michael@0: return TYPE_MAYBE_FEED; michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Writes the feed title into the preview document. michael@0: * @param container michael@0: * The feed container michael@0: */ michael@0: _setTitleText: function FW__setTitleText(container) { michael@0: if (container.title) { michael@0: var title = container.title.plainText(); michael@0: this._setContentText(TITLE_ID, container.title); michael@0: this._contentSandbox.document = this._document; michael@0: this._contentSandbox.title = title; michael@0: var codeStr = "document.title = title;" michael@0: Cu.evalInSandbox(codeStr, this._contentSandbox); michael@0: } michael@0: michael@0: var feed = container.QueryInterface(Ci.nsIFeed); michael@0: if (feed && feed.subtitle) michael@0: this._setContentText(SUBTITLE_ID, container.subtitle); michael@0: }, michael@0: michael@0: /** michael@0: * Writes the title image into the preview document if one is present. michael@0: * @param container michael@0: * The feed container michael@0: */ michael@0: _setTitleImage: function FW__setTitleImage(container) { michael@0: try { michael@0: var parts = container.image; michael@0: michael@0: // Set up the title image (supplied by the feed) michael@0: var feedTitleImage = this._document.getElementById("feedTitleImage"); michael@0: this._safeSetURIAttribute(feedTitleImage, "src", michael@0: parts.getPropertyAsAString("url")); michael@0: michael@0: // Set up the title image link michael@0: var feedTitleLink = this._document.getElementById("feedTitleLink"); michael@0: michael@0: var titleText = this._getFormattedString("linkTitleTextFormat", michael@0: [parts.getPropertyAsAString("title")]); michael@0: this._contentSandbox.feedTitleLink = feedTitleLink; michael@0: this._contentSandbox.titleText = titleText; michael@0: this._contentSandbox.feedTitleText = this._document.getElementById("feedTitleText"); michael@0: this._contentSandbox.titleImageWidth = parseInt(parts.getPropertyAsAString("width")) + 15; michael@0: michael@0: // Fix the margin on the main title, so that the image doesn't run over michael@0: // the underline michael@0: var codeStr = "feedTitleLink.setAttribute('title', titleText); " + michael@0: "feedTitleText.style.marginRight = titleImageWidth + 'px';"; michael@0: Cu.evalInSandbox(codeStr, this._contentSandbox); michael@0: this._contentSandbox.feedTitleLink = null; michael@0: this._contentSandbox.titleText = null; michael@0: this._contentSandbox.feedTitleText = null; michael@0: this._contentSandbox.titleImageWidth = null; michael@0: michael@0: this._safeSetURIAttribute(feedTitleLink, "href", michael@0: parts.getPropertyAsAString("link")); michael@0: } michael@0: catch (e) { michael@0: LOG("Failed to set Title Image (this is benign): " + e); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Writes all entries contained in the feed. michael@0: * @param container michael@0: * The container of entries in the feed michael@0: */ michael@0: _writeFeedContent: function FW__writeFeedContent(container) { michael@0: // Build the actual feed content michael@0: var feed = container.QueryInterface(Ci.nsIFeed); michael@0: if (feed.items.length == 0) michael@0: return; michael@0: michael@0: this._contentSandbox.feedContent = michael@0: this._document.getElementById("feedContent"); michael@0: michael@0: for (var i = 0; i < feed.items.length; ++i) { michael@0: var entry = feed.items.queryElementAt(i, Ci.nsIFeedEntry); michael@0: entry.QueryInterface(Ci.nsIFeedContainer); michael@0: michael@0: var entryContainer = this._document.createElementNS(HTML_NS, "div"); michael@0: entryContainer.className = "entry"; michael@0: michael@0: // If the entry has a title, make it a link michael@0: if (entry.title) { michael@0: var a = this._document.createElementNS(HTML_NS, "a"); michael@0: var span = this._document.createElementNS(HTML_NS, "span"); michael@0: a.appendChild(span); michael@0: if (entry.title.base) michael@0: span.setAttributeNS(XML_NS, "base", entry.title.base.spec); michael@0: span.appendChild(entry.title.createDocumentFragment(a)); michael@0: michael@0: // Entries are not required to have links, so entry.link can be null. michael@0: if (entry.link) michael@0: this._safeSetURIAttribute(a, "href", entry.link.spec); michael@0: michael@0: var title = this._document.createElementNS(HTML_NS, "h3"); michael@0: title.appendChild(a); michael@0: michael@0: var lastUpdated = this._parseDate(entry.updated); michael@0: if (lastUpdated) { michael@0: var dateDiv = this._document.createElementNS(HTML_NS, "div"); michael@0: dateDiv.className = "lastUpdated"; michael@0: dateDiv.textContent = lastUpdated; michael@0: title.appendChild(dateDiv); michael@0: } michael@0: michael@0: entryContainer.appendChild(title); michael@0: } michael@0: michael@0: var body = this._document.createElementNS(HTML_NS, "div"); michael@0: var summary = entry.summary || entry.content; michael@0: var docFragment = null; michael@0: if (summary) { michael@0: if (summary.base) michael@0: body.setAttributeNS(XML_NS, "base", summary.base.spec); michael@0: else michael@0: LOG("no base?"); michael@0: docFragment = summary.createDocumentFragment(body); michael@0: if (docFragment) michael@0: body.appendChild(docFragment); michael@0: michael@0: // If the entry doesn't have a title, append a # permalink michael@0: // See http://scripting.com/rss.xml for an example michael@0: if (!entry.title && entry.link) { michael@0: var a = this._document.createElementNS(HTML_NS, "a"); michael@0: a.appendChild(this._document.createTextNode("#")); michael@0: this._safeSetURIAttribute(a, "href", entry.link.spec); michael@0: body.appendChild(this._document.createTextNode(" ")); michael@0: body.appendChild(a); michael@0: } michael@0: michael@0: } michael@0: body.className = "feedEntryContent"; michael@0: entryContainer.appendChild(body); michael@0: michael@0: if (entry.enclosures && entry.enclosures.length > 0) { michael@0: var enclosuresDiv = this._buildEnclosureDiv(entry); michael@0: entryContainer.appendChild(enclosuresDiv); michael@0: } michael@0: michael@0: this._contentSandbox.entryContainer = entryContainer; michael@0: this._contentSandbox.clearDiv = michael@0: this._document.createElementNS(HTML_NS, "div"); michael@0: this._contentSandbox.clearDiv.style.clear = "both"; michael@0: michael@0: var codeStr = "feedContent.appendChild(entryContainer); " + michael@0: "feedContent.appendChild(clearDiv);" michael@0: Cu.evalInSandbox(codeStr, this._contentSandbox); michael@0: } michael@0: michael@0: this._contentSandbox.feedContent = null; michael@0: this._contentSandbox.entryContainer = null; michael@0: this._contentSandbox.clearDiv = null; michael@0: }, michael@0: michael@0: /** michael@0: * Takes a url to a media item and returns the best name it can come up with. michael@0: * Frequently this is the filename portion (e.g. passing in michael@0: * http://example.com/foo.mpeg would return "foo.mpeg"), but in more complex michael@0: * cases, this will return the entire url (e.g. passing in michael@0: * http://example.com/somedirectory/ would return michael@0: * http://example.com/somedirectory/). michael@0: * @param aURL michael@0: * The URL string from which to create a display name michael@0: * @returns a string michael@0: */ michael@0: _getURLDisplayName: function FW__getURLDisplayName(aURL) { michael@0: var url = makeURI(aURL); michael@0: url.QueryInterface(Ci.nsIURL); michael@0: if (url == null || url.fileName.length == 0) michael@0: return decodeURIComponent(aURL); michael@0: michael@0: return decodeURIComponent(url.fileName); michael@0: }, michael@0: michael@0: /** michael@0: * Takes a FeedEntry with enclosures, generates the HTML code to represent michael@0: * them, and returns that. michael@0: * @param entry michael@0: * FeedEntry with enclosures michael@0: * @returns element michael@0: */ michael@0: _buildEnclosureDiv: function FW__buildEnclosureDiv(entry) { michael@0: var enclosuresDiv = this._document.createElementNS(HTML_NS, "div"); michael@0: enclosuresDiv.className = "enclosures"; michael@0: michael@0: enclosuresDiv.appendChild(this._document.createTextNode(this._getString("mediaLabel"))); michael@0: michael@0: var roundme = function(n) { michael@0: return (Math.round(n * 100) / 100).toLocaleString(); michael@0: } michael@0: michael@0: for (var i_enc = 0; i_enc < entry.enclosures.length; ++i_enc) { michael@0: var enc = entry.enclosures.queryElementAt(i_enc, Ci.nsIWritablePropertyBag2); michael@0: michael@0: if (!(enc.hasKey("url"))) michael@0: continue; michael@0: michael@0: var enclosureDiv = this._document.createElementNS(HTML_NS, "div"); michael@0: enclosureDiv.setAttribute("class", "enclosure"); michael@0: michael@0: var mozicon = "moz-icon://.txt?size=16"; michael@0: var type_text = null; michael@0: var size_text = null; michael@0: michael@0: if (enc.hasKey("type")) { michael@0: type_text = enc.get("type"); michael@0: try { michael@0: var handlerInfoWrapper = this._mimeSvc.getFromTypeAndExtension(enc.get("type"), null); michael@0: michael@0: if (handlerInfoWrapper) michael@0: type_text = handlerInfoWrapper.description; michael@0: michael@0: if (type_text && type_text.length > 0) michael@0: mozicon = "moz-icon://goat?size=16&contentType=" + enc.get("type"); michael@0: michael@0: } catch (ex) { } michael@0: michael@0: } michael@0: michael@0: if (enc.hasKey("length") && /^[0-9]+$/.test(enc.get("length"))) { michael@0: var enc_size = convertByteUnits(parseInt(enc.get("length"))); michael@0: michael@0: var size_text = this._getFormattedString("enclosureSizeText", michael@0: [enc_size[0], this._getString(enc_size[1])]); michael@0: } michael@0: michael@0: var iconimg = this._document.createElementNS(HTML_NS, "img"); michael@0: iconimg.setAttribute("src", mozicon); michael@0: iconimg.setAttribute("class", "type-icon"); michael@0: enclosureDiv.appendChild(iconimg); michael@0: michael@0: enclosureDiv.appendChild(this._document.createTextNode( " " )); michael@0: michael@0: var enc_href = this._document.createElementNS(HTML_NS, "a"); michael@0: enc_href.appendChild(this._document.createTextNode(this._getURLDisplayName(enc.get("url")))); michael@0: this._safeSetURIAttribute(enc_href, "href", enc.get("url")); michael@0: enclosureDiv.appendChild(enc_href); michael@0: michael@0: if (type_text && size_text) michael@0: enclosureDiv.appendChild(this._document.createTextNode( " (" + type_text + ", " + size_text + ")")); michael@0: michael@0: else if (type_text) michael@0: enclosureDiv.appendChild(this._document.createTextNode( " (" + type_text + ")")) michael@0: michael@0: else if (size_text) michael@0: enclosureDiv.appendChild(this._document.createTextNode( " (" + size_text + ")")) michael@0: michael@0: enclosuresDiv.appendChild(enclosureDiv); michael@0: } michael@0: michael@0: return enclosuresDiv; michael@0: }, michael@0: michael@0: /** michael@0: * Gets a valid nsIFeedContainer object from the parsed nsIFeedResult. michael@0: * Displays error information if there was one. michael@0: * @param result michael@0: * The parsed feed result michael@0: * @returns A valid nsIFeedContainer object containing the contents of michael@0: * the feed. michael@0: */ michael@0: _getContainer: function FW__getContainer(result) { michael@0: var feedService = michael@0: Cc["@mozilla.org/browser/feeds/result-service;1"]. michael@0: getService(Ci.nsIFeedResultService); michael@0: michael@0: try { michael@0: var result = michael@0: feedService.getFeedResult(this._getOriginalURI(this._window)); michael@0: } michael@0: catch (e) { michael@0: LOG("Subscribe Preview: feed not available?!"); michael@0: } michael@0: michael@0: if (result.bozo) { michael@0: LOG("Subscribe Preview: feed result is bozo?!"); michael@0: } michael@0: michael@0: try { michael@0: var container = result.doc; michael@0: } michael@0: catch (e) { michael@0: LOG("Subscribe Preview: no result.doc? Why didn't the original reload?"); michael@0: return null; michael@0: } michael@0: return container; michael@0: }, michael@0: michael@0: /** michael@0: * Get the human-readable display name of a file. This could be the michael@0: * application name. michael@0: * @param file michael@0: * A nsIFile to look up the name of michael@0: * @returns The display name of the application represented by the file. michael@0: */ michael@0: _getFileDisplayName: function FW__getFileDisplayName(file) { michael@0: #ifdef XP_WIN michael@0: if (file instanceof Ci.nsILocalFileWin) { michael@0: try { michael@0: return file.getVersionInfoField("FileDescription"); michael@0: } catch (e) {} michael@0: } michael@0: #endif michael@0: #ifdef XP_MACOSX michael@0: if (file instanceof Ci.nsILocalFileMac) { michael@0: try { michael@0: return file.bundleDisplayName; michael@0: } catch (e) {} michael@0: } michael@0: #endif michael@0: return file.leafName; michael@0: }, michael@0: michael@0: /** michael@0: * Get moz-icon url for a file michael@0: * @param file michael@0: * A nsIFile object for which the moz-icon:// is returned michael@0: * @returns moz-icon url of the given file as a string michael@0: */ michael@0: _getFileIconURL: function FW__getFileIconURL(file) { michael@0: var ios = Cc["@mozilla.org/network/io-service;1"]. michael@0: getService(Ci.nsIIOService); michael@0: var fph = ios.getProtocolHandler("file") michael@0: .QueryInterface(Ci.nsIFileProtocolHandler); michael@0: var urlSpec = fph.getURLSpecFromFile(file); michael@0: return "moz-icon://" + urlSpec + "?size=16"; michael@0: }, michael@0: michael@0: /** michael@0: * Helper method to set the selected application and system default michael@0: * reader menuitems details from a file object michael@0: * @param aMenuItem michael@0: * The menuitem on which the attributes should be set michael@0: * @param aFile michael@0: * The menuitem's associated file michael@0: */ michael@0: _initMenuItemWithFile: function(aMenuItem, aFile) { michael@0: this._contentSandbox.menuitem = aMenuItem; michael@0: this._contentSandbox.label = this._getFileDisplayName(aFile); michael@0: this._contentSandbox.image = this._getFileIconURL(aFile); michael@0: var codeStr = "menuitem.setAttribute('label', label); " + michael@0: "menuitem.setAttribute('image', image);" michael@0: Cu.evalInSandbox(codeStr, this._contentSandbox); michael@0: }, michael@0: michael@0: /** michael@0: * Helper method to get an element in the XBL binding where the handler michael@0: * selection UI lives michael@0: */ michael@0: _getUIElement: function FW__getUIElement(id) { michael@0: return this._document.getAnonymousElementByAttribute( michael@0: this._document.getElementById("feedSubscribeLine"), "anonid", id); michael@0: }, michael@0: michael@0: /** michael@0: * Displays a prompt from which the user may choose a (client) feed reader. michael@0: * @param aCallback the callback method, passes in true if a feed reader was michael@0: * selected, false otherwise. michael@0: */ michael@0: _chooseClientApp: function FW__chooseClientApp(aCallback) { michael@0: try { michael@0: let fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker); michael@0: let fpCallback = function fpCallback_done(aResult) { michael@0: if (aResult == Ci.nsIFilePicker.returnOK) { michael@0: this._selectedApp = fp.file; michael@0: if (this._selectedApp) { michael@0: // XXXben - we need to compare this with the running instance michael@0: // executable just don't know how to do that via script michael@0: // XXXmano TBD: can probably add this to nsIShellService michael@0: #ifdef XP_WIN michael@0: #expand if (fp.file.leafName != "__MOZ_APP_NAME__.exe") { michael@0: #else michael@0: #ifdef XP_MACOSX michael@0: #expand if (fp.file.leafName != "__MOZ_MACBUNDLE_NAME__") { michael@0: #else michael@0: #expand if (fp.file.leafName != "__MOZ_APP_NAME__-bin") { michael@0: #endif michael@0: #endif michael@0: this._initMenuItemWithFile(this._contentSandbox.selectedAppMenuItem, michael@0: this._selectedApp); michael@0: michael@0: // Show and select the selected application menuitem michael@0: let codeStr = "selectedAppMenuItem.hidden = false;" + michael@0: "selectedAppMenuItem.doCommand();" michael@0: Cu.evalInSandbox(codeStr, this._contentSandbox); michael@0: if (aCallback) { michael@0: aCallback(true); michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: if (aCallback) { michael@0: aCallback(false); michael@0: } michael@0: }.bind(this); michael@0: michael@0: fp.init(this._window, this._getString("chooseApplicationDialogTitle"), michael@0: Ci.nsIFilePicker.modeOpen); michael@0: fp.appendFilters(Ci.nsIFilePicker.filterApps); michael@0: fp.open(fpCallback); michael@0: } catch(ex) { michael@0: } michael@0: }, michael@0: michael@0: _setAlwaysUseCheckedState: function FW__setAlwaysUseCheckedState(feedType) { michael@0: var checkbox = this._getUIElement("alwaysUse"); michael@0: if (checkbox) { michael@0: var alwaysUse = false; michael@0: try { michael@0: var prefs = Cc["@mozilla.org/preferences-service;1"]. michael@0: getService(Ci.nsIPrefBranch); michael@0: if (prefs.getCharPref(getPrefActionForType(feedType)) != "ask") michael@0: alwaysUse = true; michael@0: } michael@0: catch(ex) { } michael@0: this._setCheckboxCheckedState(checkbox, alwaysUse); michael@0: } michael@0: }, michael@0: michael@0: _setSubscribeUsingLabel: function FW__setSubscribeUsingLabel() { michael@0: var stringLabel = "subscribeFeedUsing"; michael@0: switch (this._getFeedType()) { michael@0: case Ci.nsIFeed.TYPE_VIDEO: michael@0: stringLabel = "subscribeVideoPodcastUsing"; michael@0: break; michael@0: michael@0: case Ci.nsIFeed.TYPE_AUDIO: michael@0: stringLabel = "subscribeAudioPodcastUsing"; michael@0: break; michael@0: } michael@0: michael@0: this._contentSandbox.subscribeUsing = michael@0: this._getUIElement("subscribeUsingDescription"); michael@0: this._contentSandbox.label = this._getString(stringLabel); michael@0: var codeStr = "subscribeUsing.setAttribute('value', label);" michael@0: Cu.evalInSandbox(codeStr, this._contentSandbox); michael@0: }, michael@0: michael@0: _setAlwaysUseLabel: function FW__setAlwaysUseLabel() { michael@0: var checkbox = this._getUIElement("alwaysUse"); michael@0: if (checkbox) { michael@0: if (this._handlersMenuList) { michael@0: var handlerName = this._getSelectedItemFromMenulist(this._handlersMenuList) michael@0: .getAttribute("label"); michael@0: var stringLabel = "alwaysUseForFeeds"; michael@0: switch (this._getFeedType()) { michael@0: case Ci.nsIFeed.TYPE_VIDEO: michael@0: stringLabel = "alwaysUseForVideoPodcasts"; michael@0: break; michael@0: michael@0: case Ci.nsIFeed.TYPE_AUDIO: michael@0: stringLabel = "alwaysUseForAudioPodcasts"; michael@0: break; michael@0: } michael@0: michael@0: this._contentSandbox.checkbox = checkbox; michael@0: this._contentSandbox.label = this._getFormattedString(stringLabel, [handlerName]); michael@0: michael@0: var codeStr = "checkbox.setAttribute('label', label);"; michael@0: Cu.evalInSandbox(codeStr, this._contentSandbox); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: // nsIDomEventListener michael@0: handleEvent: function(event) { michael@0: if (event.target.ownerDocument != this._document) { michael@0: LOG("FeedWriter.handleEvent: Someone passed the feed writer as a listener to the events of another document!"); michael@0: return; michael@0: } michael@0: michael@0: if (event.type == "command") { michael@0: switch (event.target.getAttribute("anonid")) { michael@0: case "subscribeButton": michael@0: this.subscribe(); michael@0: break; michael@0: case "chooseApplicationMenuItem": michael@0: /* Bug 351263: Make sure to not steal focus if the "Choose michael@0: * Application" item is being selected with the keyboard. We do this michael@0: * by ignoring command events while the dropdown is closed (user michael@0: * arrowing through the combobox), but handling them while the michael@0: * combobox dropdown is open (user pressed enter when an item was michael@0: * selected). If we don't show the filepicker here, it will be shown michael@0: * when clicking "Subscribe Now". michael@0: */ michael@0: var popupbox = this._handlersMenuList.firstChild.boxObject; michael@0: popupbox.QueryInterface(Components.interfaces.nsIPopupBoxObject); michael@0: if (popupbox.popupState == "hiding") { michael@0: this._chooseClientApp(function(aResult) { michael@0: if (!aResult) { michael@0: // Select the (per-prefs) selected handler if no application michael@0: // was selected michael@0: this._setSelectedHandler(this._getFeedType()); michael@0: } michael@0: }.bind(this)); michael@0: } michael@0: break; michael@0: default: michael@0: this._setAlwaysUseLabel(); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: _setSelectedHandler: function FW__setSelectedHandler(feedType) { michael@0: var prefs = michael@0: Cc["@mozilla.org/preferences-service;1"]. michael@0: getService(Ci.nsIPrefBranch); michael@0: michael@0: var handler = "bookmarks"; michael@0: try { michael@0: handler = prefs.getCharPref(getPrefReaderForType(feedType)); michael@0: } michael@0: catch (ex) { } michael@0: michael@0: switch (handler) { michael@0: case "web": { michael@0: if (this._handlersMenuList) { michael@0: var url = prefs.getComplexValue(getPrefWebForType(feedType), Ci.nsISupportsString).data; michael@0: var handlers = michael@0: this._handlersMenuList.getElementsByAttribute("webhandlerurl", url); michael@0: if (handlers.length == 0) { michael@0: LOG("FeedWriter._setSelectedHandler: selected web handler isn't in the menulist") michael@0: return; michael@0: } michael@0: michael@0: this._safeDoCommand(handlers[0]); michael@0: } michael@0: break; michael@0: } michael@0: case "client": { michael@0: try { michael@0: this._selectedApp = michael@0: prefs.getComplexValue(getPrefAppForType(feedType), Ci.nsILocalFile); michael@0: } michael@0: catch(ex) { michael@0: this._selectedApp = null; michael@0: } michael@0: michael@0: if (this._selectedApp) { michael@0: this._initMenuItemWithFile(this._contentSandbox.selectedAppMenuItem, michael@0: this._selectedApp); michael@0: var codeStr = "selectedAppMenuItem.hidden = false; " + michael@0: "selectedAppMenuItem.doCommand(); "; michael@0: michael@0: // Only show the default reader menuitem if the default reader michael@0: // isn't the selected application michael@0: if (this._defaultSystemReader) { michael@0: var shouldHide = michael@0: this._defaultSystemReader.path == this._selectedApp.path; michael@0: codeStr += "defaultHandlerMenuItem.hidden = " + shouldHide + ";" michael@0: } michael@0: Cu.evalInSandbox(codeStr, this._contentSandbox); michael@0: break; michael@0: } michael@0: } michael@0: case "bookmarks": michael@0: default: { michael@0: var liveBookmarksMenuItem = this._getUIElement("liveBookmarksMenuItem"); michael@0: if (liveBookmarksMenuItem) michael@0: this._safeDoCommand(liveBookmarksMenuItem); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: _initSubscriptionUI: function FW__initSubscriptionUI() { michael@0: var handlersMenuPopup = this._getUIElement("handlersMenuPopup"); michael@0: if (!handlersMenuPopup) michael@0: return; michael@0: michael@0: var feedType = this._getFeedType(); michael@0: var codeStr; michael@0: michael@0: // change the background michael@0: var header = this._document.getElementById("feedHeader"); michael@0: this._contentSandbox.header = header; michael@0: switch (feedType) { michael@0: case Ci.nsIFeed.TYPE_VIDEO: michael@0: codeStr = "header.className = 'videoPodcastBackground'; "; michael@0: break; michael@0: michael@0: case Ci.nsIFeed.TYPE_AUDIO: michael@0: codeStr = "header.className = 'audioPodcastBackground'; "; michael@0: break; michael@0: michael@0: default: michael@0: codeStr = "header.className = 'feedBackground'; "; michael@0: } michael@0: michael@0: var liveBookmarksMenuItem = this._getUIElement("liveBookmarksMenuItem"); michael@0: michael@0: // Last-selected application michael@0: var menuItem = liveBookmarksMenuItem.cloneNode(false); michael@0: menuItem.removeAttribute("selected"); michael@0: menuItem.setAttribute("anonid", "selectedAppMenuItem"); michael@0: menuItem.className = "menuitem-iconic selectedAppMenuItem"; michael@0: menuItem.setAttribute("handlerType", "client"); michael@0: try { michael@0: var prefs = Cc["@mozilla.org/preferences-service;1"]. michael@0: getService(Ci.nsIPrefBranch); michael@0: this._selectedApp = prefs.getComplexValue(getPrefAppForType(feedType), michael@0: Ci.nsILocalFile); michael@0: michael@0: if (this._selectedApp.exists()) michael@0: this._initMenuItemWithFile(menuItem, this._selectedApp); michael@0: else { michael@0: // Hide the menuitem if the last selected application doesn't exist michael@0: menuItem.setAttribute("hidden", true); michael@0: } michael@0: } michael@0: catch(ex) { michael@0: // Hide the menuitem until an application is selected michael@0: menuItem.setAttribute("hidden", true); michael@0: } michael@0: this._contentSandbox.handlersMenuPopup = handlersMenuPopup; michael@0: this._contentSandbox.selectedAppMenuItem = menuItem; michael@0: michael@0: codeStr += "handlersMenuPopup.appendChild(selectedAppMenuItem); "; michael@0: michael@0: // List the default feed reader michael@0: try { michael@0: this._defaultSystemReader = Cc["@mozilla.org/browser/shell-service;1"]. michael@0: getService(Ci.nsIShellService). michael@0: defaultFeedReader; michael@0: menuItem = liveBookmarksMenuItem.cloneNode(false); michael@0: menuItem.removeAttribute("selected"); michael@0: menuItem.setAttribute("anonid", "defaultHandlerMenuItem"); michael@0: menuItem.className = "menuitem-iconic defaultHandlerMenuItem"; michael@0: menuItem.setAttribute("handlerType", "client"); michael@0: michael@0: this._initMenuItemWithFile(menuItem, this._defaultSystemReader); michael@0: michael@0: // Hide the default reader item if it points to the same application michael@0: // as the last-selected application michael@0: if (this._selectedApp && michael@0: this._selectedApp.path == this._defaultSystemReader.path) michael@0: menuItem.hidden = true; michael@0: } michael@0: catch(ex) { menuItem = null; /* no default reader */ } michael@0: michael@0: if (menuItem) { michael@0: this._contentSandbox.defaultHandlerMenuItem = menuItem; michael@0: codeStr += "handlersMenuPopup.appendChild(defaultHandlerMenuItem); "; michael@0: } michael@0: michael@0: // "Choose Application..." menuitem michael@0: menuItem = liveBookmarksMenuItem.cloneNode(false); michael@0: menuItem.removeAttribute("selected"); michael@0: menuItem.setAttribute("anonid", "chooseApplicationMenuItem"); michael@0: menuItem.className = "menuitem-iconic chooseApplicationMenuItem"; michael@0: menuItem.setAttribute("label", this._getString("chooseApplicationMenuItem")); michael@0: michael@0: this._contentSandbox.chooseAppMenuItem = menuItem; michael@0: codeStr += "handlersMenuPopup.appendChild(chooseAppMenuItem); "; michael@0: michael@0: // separator michael@0: this._contentSandbox.chooseAppSep = michael@0: menuItem = liveBookmarksMenuItem.nextSibling.cloneNode(false); michael@0: codeStr += "handlersMenuPopup.appendChild(chooseAppSep); "; michael@0: michael@0: Cu.evalInSandbox(codeStr, this._contentSandbox); michael@0: michael@0: // List of web handlers michael@0: var wccr = Cc["@mozilla.org/embeddor.implemented/web-content-handler-registrar;1"]. michael@0: getService(Ci.nsIWebContentConverterService); michael@0: var handlers = wccr.getContentHandlers(this._getMimeTypeForFeedType(feedType)); michael@0: if (handlers.length != 0) { michael@0: for (var i = 0; i < handlers.length; ++i) { michael@0: menuItem = liveBookmarksMenuItem.cloneNode(false); michael@0: menuItem.removeAttribute("selected"); michael@0: menuItem.className = "menuitem-iconic"; michael@0: menuItem.setAttribute("label", handlers[i].name); michael@0: menuItem.setAttribute("handlerType", "web"); michael@0: menuItem.setAttribute("webhandlerurl", handlers[i].uri); michael@0: this._contentSandbox.menuItem = menuItem; michael@0: codeStr = "handlersMenuPopup.appendChild(menuItem);"; michael@0: Cu.evalInSandbox(codeStr, this._contentSandbox); michael@0: michael@0: this._setFaviconForWebReader(handlers[i].uri, menuItem); michael@0: } michael@0: this._contentSandbox.menuItem = null; michael@0: } michael@0: michael@0: this._setSelectedHandler(feedType); michael@0: michael@0: // "Subscribe using..." michael@0: this._setSubscribeUsingLabel(); michael@0: michael@0: // "Always use..." checkbox initial state michael@0: this._setAlwaysUseCheckedState(feedType); michael@0: this._setAlwaysUseLabel(); michael@0: michael@0: // We update the "Always use.." checkbox label whenever the selected item michael@0: // in the list is changed michael@0: handlersMenuPopup.addEventListener("command", this, false); michael@0: michael@0: // Set up the "Subscribe Now" button michael@0: this._getUIElement("subscribeButton") michael@0: .addEventListener("command", this, false); michael@0: michael@0: // first-run ui michael@0: var showFirstRunUI = true; michael@0: try { michael@0: showFirstRunUI = prefs.getBoolPref(PREF_SHOW_FIRST_RUN_UI); michael@0: } michael@0: catch (ex) { } michael@0: if (showFirstRunUI) { michael@0: var textfeedinfo1, textfeedinfo2; michael@0: switch (feedType) { michael@0: case Ci.nsIFeed.TYPE_VIDEO: michael@0: textfeedinfo1 = "feedSubscriptionVideoPodcast1"; michael@0: textfeedinfo2 = "feedSubscriptionVideoPodcast2"; michael@0: break; michael@0: case Ci.nsIFeed.TYPE_AUDIO: michael@0: textfeedinfo1 = "feedSubscriptionAudioPodcast1"; michael@0: textfeedinfo2 = "feedSubscriptionAudioPodcast2"; michael@0: break; michael@0: default: michael@0: textfeedinfo1 = "feedSubscriptionFeed1"; michael@0: textfeedinfo2 = "feedSubscriptionFeed2"; michael@0: } michael@0: michael@0: this._contentSandbox.feedinfo1 = michael@0: this._document.getElementById("feedSubscriptionInfo1"); michael@0: this._contentSandbox.feedinfo1Str = this._getString(textfeedinfo1); michael@0: this._contentSandbox.feedinfo2 = michael@0: this._document.getElementById("feedSubscriptionInfo2"); michael@0: this._contentSandbox.feedinfo2Str = this._getString(textfeedinfo2); michael@0: this._contentSandbox.header = header; michael@0: codeStr = "feedinfo1.textContent = feedinfo1Str; " + michael@0: "feedinfo2.textContent = feedinfo2Str; " + michael@0: "header.setAttribute('firstrun', 'true');" michael@0: Cu.evalInSandbox(codeStr, this._contentSandbox); michael@0: prefs.setBoolPref(PREF_SHOW_FIRST_RUN_UI, false); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Returns the original URI object of the feed and ensures that this michael@0: * component is only ever invoked from the preview document. michael@0: * @param aWindow michael@0: * The window of the document invoking the BrowserFeedWriter michael@0: */ michael@0: _getOriginalURI: function FW__getOriginalURI(aWindow) { michael@0: var chan = aWindow.QueryInterface(Ci.nsIInterfaceRequestor). michael@0: getInterface(Ci.nsIWebNavigation). michael@0: QueryInterface(Ci.nsIDocShell).currentDocumentChannel; michael@0: michael@0: var resolvedURI = Cc["@mozilla.org/network/io-service;1"]. michael@0: getService(Ci.nsIIOService). michael@0: newChannel("about:feeds", null, null).URI; michael@0: michael@0: if (resolvedURI.equals(chan.URI)) michael@0: return chan.originalURI; michael@0: michael@0: return null; michael@0: }, michael@0: michael@0: _window: null, michael@0: _document: null, michael@0: _feedURI: null, michael@0: _feedPrincipal: null, michael@0: _handlersMenuList: null, michael@0: michael@0: // BrowserFeedWriter WebIDL methods michael@0: init: function FW_init(aWindow) { michael@0: var window = aWindow; michael@0: this._feedURI = this._getOriginalURI(window); michael@0: if (!this._feedURI) michael@0: return; michael@0: michael@0: this._window = window; michael@0: this._document = window.document; michael@0: this._document.getElementById("feedSubscribeLine").offsetTop; michael@0: this._handlersMenuList = this._getUIElement("handlersMenuList"); michael@0: michael@0: var secman = Cc["@mozilla.org/scriptsecuritymanager;1"]. michael@0: getService(Ci.nsIScriptSecurityManager); michael@0: this._feedPrincipal = secman.getSimpleCodebasePrincipal(this._feedURI); michael@0: michael@0: LOG("Subscribe Preview: feed uri = " + this._window.location.href); michael@0: michael@0: // Set up the subscription UI michael@0: this._initSubscriptionUI(); michael@0: var prefs = Cc["@mozilla.org/preferences-service;1"]. michael@0: getService(Ci.nsIPrefBranch); michael@0: prefs.addObserver(PREF_SELECTED_ACTION, this, false); michael@0: prefs.addObserver(PREF_SELECTED_READER, this, false); michael@0: prefs.addObserver(PREF_SELECTED_WEB, this, false); michael@0: prefs.addObserver(PREF_SELECTED_APP, this, false); michael@0: prefs.addObserver(PREF_VIDEO_SELECTED_ACTION, this, false); michael@0: prefs.addObserver(PREF_VIDEO_SELECTED_READER, this, false); michael@0: prefs.addObserver(PREF_VIDEO_SELECTED_WEB, this, false); michael@0: prefs.addObserver(PREF_VIDEO_SELECTED_APP, this, false); michael@0: michael@0: prefs.addObserver(PREF_AUDIO_SELECTED_ACTION, this, false); michael@0: prefs.addObserver(PREF_AUDIO_SELECTED_READER, this, false); michael@0: prefs.addObserver(PREF_AUDIO_SELECTED_WEB, this, false); michael@0: prefs.addObserver(PREF_AUDIO_SELECTED_APP, this, false); michael@0: }, michael@0: michael@0: writeContent: function FW_writeContent() { michael@0: if (!this._window) michael@0: return; michael@0: michael@0: try { michael@0: // Set up the feed content michael@0: var container = this._getContainer(); michael@0: if (!container) michael@0: return; michael@0: michael@0: this._setTitleText(container); michael@0: this._setTitleImage(container); michael@0: this._writeFeedContent(container); michael@0: } michael@0: finally { michael@0: this._removeFeedFromCache(); michael@0: } michael@0: }, michael@0: michael@0: close: function FW_close() { michael@0: this._getUIElement("handlersMenuPopup") michael@0: .removeEventListener("command", this, false); michael@0: this._getUIElement("subscribeButton") michael@0: .removeEventListener("command", this, false); michael@0: this._document = null; michael@0: this._window = null; michael@0: var prefs = Cc["@mozilla.org/preferences-service;1"]. michael@0: getService(Ci.nsIPrefBranch); michael@0: prefs.removeObserver(PREF_SELECTED_ACTION, this); michael@0: prefs.removeObserver(PREF_SELECTED_READER, this); michael@0: prefs.removeObserver(PREF_SELECTED_WEB, this); michael@0: prefs.removeObserver(PREF_SELECTED_APP, this); michael@0: prefs.removeObserver(PREF_VIDEO_SELECTED_ACTION, this); michael@0: prefs.removeObserver(PREF_VIDEO_SELECTED_READER, this); michael@0: prefs.removeObserver(PREF_VIDEO_SELECTED_WEB, this); michael@0: prefs.removeObserver(PREF_VIDEO_SELECTED_APP, this); michael@0: michael@0: prefs.removeObserver(PREF_AUDIO_SELECTED_ACTION, this); michael@0: prefs.removeObserver(PREF_AUDIO_SELECTED_READER, this); michael@0: prefs.removeObserver(PREF_AUDIO_SELECTED_WEB, this); michael@0: prefs.removeObserver(PREF_AUDIO_SELECTED_APP, this); michael@0: michael@0: this._removeFeedFromCache(); michael@0: this.__faviconService = null; michael@0: this.__bundle = null; michael@0: this._feedURI = null; michael@0: this.__contentSandbox = null; michael@0: }, michael@0: michael@0: _removeFeedFromCache: function FW__removeFeedFromCache() { michael@0: if (this._feedURI) { michael@0: var feedService = Cc["@mozilla.org/browser/feeds/result-service;1"]. michael@0: getService(Ci.nsIFeedResultService); michael@0: feedService.removeFeedResult(this._feedURI); michael@0: this._feedURI = null; michael@0: } michael@0: }, michael@0: michael@0: subscribe: function FW_subscribe() { michael@0: var feedType = this._getFeedType(); michael@0: michael@0: // Subscribe to the feed using the selected handler and save prefs michael@0: var prefs = Cc["@mozilla.org/preferences-service;1"]. michael@0: getService(Ci.nsIPrefBranch); michael@0: var defaultHandler = "reader"; michael@0: var useAsDefault = this._getUIElement("alwaysUse").getAttribute("checked"); michael@0: michael@0: var selectedItem = this._getSelectedItemFromMenulist(this._handlersMenuList); michael@0: let subscribeCallback = function() { michael@0: if (selectedItem.hasAttribute("webhandlerurl")) { michael@0: var webURI = selectedItem.getAttribute("webhandlerurl"); michael@0: prefs.setCharPref(getPrefReaderForType(feedType), "web"); michael@0: michael@0: var supportsString = Cc["@mozilla.org/supports-string;1"]. michael@0: createInstance(Ci.nsISupportsString); michael@0: supportsString.data = webURI; michael@0: prefs.setComplexValue(getPrefWebForType(feedType), Ci.nsISupportsString, michael@0: supportsString); michael@0: michael@0: var wccr = Cc["@mozilla.org/embeddor.implemented/web-content-handler-registrar;1"]. michael@0: getService(Ci.nsIWebContentConverterService); michael@0: var handler = wccr.getWebContentHandlerByURI(this._getMimeTypeForFeedType(feedType), webURI); michael@0: if (handler) { michael@0: if (useAsDefault) { michael@0: wccr.setAutoHandler(this._getMimeTypeForFeedType(feedType), handler); michael@0: } michael@0: michael@0: this._window.location.href = handler.getHandlerURI(this._window.location.href); michael@0: } michael@0: } else { michael@0: switch (selectedItem.getAttribute("anonid")) { michael@0: case "selectedAppMenuItem": michael@0: prefs.setComplexValue(getPrefAppForType(feedType), Ci.nsILocalFile, michael@0: this._selectedApp); michael@0: prefs.setCharPref(getPrefReaderForType(feedType), "client"); michael@0: break; michael@0: case "defaultHandlerMenuItem": michael@0: prefs.setComplexValue(getPrefAppForType(feedType), Ci.nsILocalFile, michael@0: this._defaultSystemReader); michael@0: prefs.setCharPref(getPrefReaderForType(feedType), "client"); michael@0: break; michael@0: case "liveBookmarksMenuItem": michael@0: defaultHandler = "bookmarks"; michael@0: prefs.setCharPref(getPrefReaderForType(feedType), "bookmarks"); michael@0: break; michael@0: } michael@0: var feedService = Cc["@mozilla.org/browser/feeds/result-service;1"]. michael@0: getService(Ci.nsIFeedResultService); michael@0: michael@0: // Pull the title and subtitle out of the document michael@0: var feedTitle = this._document.getElementById(TITLE_ID).textContent; michael@0: var feedSubtitle = this._document.getElementById(SUBTITLE_ID).textContent; michael@0: feedService.addToClientReader(this._window.location.href, feedTitle, feedSubtitle, feedType); michael@0: } michael@0: michael@0: // If "Always use..." is checked, we should set PREF_*SELECTED_ACTION michael@0: // to either "reader" (If a web reader or if an application is selected), michael@0: // or to "bookmarks" (if the live bookmarks option is selected). michael@0: // Otherwise, we should set it to "ask" michael@0: if (useAsDefault) { michael@0: prefs.setCharPref(getPrefActionForType(feedType), defaultHandler); michael@0: } else { michael@0: prefs.setCharPref(getPrefActionForType(feedType), "ask"); michael@0: } michael@0: }.bind(this); michael@0: michael@0: // Show the file picker before subscribing if the michael@0: // choose application menuitem was chosen using the keyboard michael@0: if (selectedItem.getAttribute("anonid") == "chooseApplicationMenuItem") { michael@0: this._chooseClientApp(function(aResult) { michael@0: if (aResult) { michael@0: selectedItem = michael@0: this._getSelectedItemFromMenulist(this._handlersMenuList); michael@0: subscribeCallback(); michael@0: } michael@0: }.bind(this)); michael@0: } else { michael@0: subscribeCallback(); michael@0: } michael@0: }, michael@0: michael@0: // nsIObserver michael@0: observe: function FW_observe(subject, topic, data) { michael@0: if (!this._window) { michael@0: // this._window is null unless this.init was called with a trusted michael@0: // window object. michael@0: return; michael@0: } michael@0: michael@0: var feedType = this._getFeedType(); michael@0: michael@0: if (topic == "nsPref:changed") { michael@0: switch (data) { michael@0: case PREF_SELECTED_READER: michael@0: case PREF_SELECTED_WEB: michael@0: case PREF_SELECTED_APP: michael@0: case PREF_VIDEO_SELECTED_READER: michael@0: case PREF_VIDEO_SELECTED_WEB: michael@0: case PREF_VIDEO_SELECTED_APP: michael@0: case PREF_AUDIO_SELECTED_READER: michael@0: case PREF_AUDIO_SELECTED_WEB: michael@0: case PREF_AUDIO_SELECTED_APP: michael@0: this._setSelectedHandler(feedType); michael@0: break; michael@0: case PREF_SELECTED_ACTION: michael@0: case PREF_VIDEO_SELECTED_ACTION: michael@0: case PREF_AUDIO_SELECTED_ACTION: michael@0: this._setAlwaysUseCheckedState(feedType); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Sets the icon for the given web-reader item in the readers menu. michael@0: * The icon is fetched and stored through the favicon service. michael@0: * michael@0: * @param aReaderUrl michael@0: * the reader url. michael@0: * @param aMenuItem michael@0: * the reader item in the readers menulist. michael@0: * michael@0: * @note For privacy reasons we cannot set the image attribute directly michael@0: * to the icon url. See Bug 358878 for details. michael@0: */ michael@0: _setFaviconForWebReader: michael@0: function FW__setFaviconForWebReader(aReaderUrl, aMenuItem) { michael@0: var readerURI = makeURI(aReaderUrl); michael@0: if (!/^https?$/.test(readerURI.scheme)) { michael@0: // Don't try to get a favicon for non http(s) URIs. michael@0: return; michael@0: } michael@0: var faviconURI = makeURI(readerURI.prePath + "/favicon.ico"); michael@0: var self = this; michael@0: var usePrivateBrowsing = this._window.QueryInterface(Ci.nsIInterfaceRequestor) michael@0: .getInterface(Ci.nsIWebNavigation) michael@0: .QueryInterface(Ci.nsIDocShell) michael@0: .QueryInterface(Ci.nsILoadContext) michael@0: .usePrivateBrowsing; michael@0: this._faviconService.setAndFetchFaviconForPage(readerURI, faviconURI, false, michael@0: usePrivateBrowsing ? this._faviconService.FAVICON_LOAD_PRIVATE michael@0: : this._faviconService.FAVICON_LOAD_NON_PRIVATE, michael@0: function (aURI, aDataLen, aData, aMimeType) { michael@0: if (aDataLen > 0) { michael@0: var dataURL = "data:" + aMimeType + ";base64," + michael@0: btoa(String.fromCharCode.apply(null, aData)); michael@0: self._contentSandbox.menuItem = aMenuItem; michael@0: self._contentSandbox.dataURL = dataURL; michael@0: var codeStr = "menuItem.setAttribute('image', dataURL);"; michael@0: Cu.evalInSandbox(codeStr, self._contentSandbox); michael@0: self._contentSandbox.menuItem = null; michael@0: self._contentSandbox.dataURL = null; michael@0: } michael@0: }); michael@0: }, michael@0: michael@0: classID: FEEDWRITER_CID, michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMEventListener, Ci.nsIObserver, michael@0: Ci.nsINavHistoryObserver, michael@0: Ci.nsIDOMGlobalPropertyInitializer]) michael@0: }; michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([FeedWriter]);