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 file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: var FeedHandler = { michael@0: PREF_CONTENTHANDLERS_BRANCH: "browser.contentHandlers.types.", michael@0: TYPE_MAYBE_FEED: "application/vnd.mozilla.maybe.feed", michael@0: michael@0: _contentTypes: null, michael@0: michael@0: getContentHandlers: function fh_getContentHandlers(contentType) { michael@0: if (!this._contentTypes) michael@0: this.loadContentHandlers(); michael@0: michael@0: if (!(contentType in this._contentTypes)) michael@0: return []; michael@0: michael@0: return this._contentTypes[contentType]; michael@0: }, michael@0: michael@0: loadContentHandlers: function fh_loadContentHandlers() { michael@0: this._contentTypes = {}; michael@0: michael@0: let kids = Services.prefs.getBranch(this.PREF_CONTENTHANDLERS_BRANCH).getChildList(""); michael@0: michael@0: // First get the numbers of the providers by getting all ###.uri prefs michael@0: let nums = []; michael@0: for (let i = 0; i < kids.length; i++) { michael@0: let match = /^(\d+)\.uri$/.exec(kids[i]); michael@0: if (!match) michael@0: continue; michael@0: else michael@0: nums.push(match[1]); michael@0: } michael@0: michael@0: // Sort them, to get them back in order michael@0: nums.sort(function(a, b) { return a - b; }); michael@0: michael@0: // Now register them michael@0: for (let i = 0; i < nums.length; i++) { michael@0: let branch = Services.prefs.getBranch(this.PREF_CONTENTHANDLERS_BRANCH + nums[i] + "."); michael@0: let vals = branch.getChildList(""); michael@0: if (vals.length == 0) michael@0: return; michael@0: michael@0: try { michael@0: let type = branch.getCharPref("type"); michael@0: let uri = branch.getComplexValue("uri", Ci.nsIPrefLocalizedString).data; michael@0: let title = branch.getComplexValue("title", Ci.nsIPrefLocalizedString).data; michael@0: michael@0: if (!(type in this._contentTypes)) michael@0: this._contentTypes[type] = []; michael@0: this._contentTypes[type].push({ contentType: type, uri: uri, name: title }); michael@0: } michael@0: catch(ex) {} michael@0: } michael@0: }, michael@0: michael@0: observe: function fh_observe(aSubject, aTopic, aData) { michael@0: if (aTopic === "Feeds:Subscribe") { michael@0: let args = JSON.parse(aData); michael@0: let tab = BrowserApp.getTabForId(args.tabId); michael@0: if (!tab) michael@0: return; michael@0: michael@0: let browser = tab.browser; michael@0: let feeds = browser.feeds; michael@0: if (feeds == null) michael@0: return; michael@0: michael@0: // First, let's decide on which feed to subscribe michael@0: let feedIndex = -1; michael@0: if (feeds.length > 1) { michael@0: let p = new Prompt({ michael@0: window: browser.contentWindow, michael@0: title: Strings.browser.GetStringFromName("feedHandler.chooseFeed") michael@0: }).setSingleChoiceItems(feeds.map(function(feed) { michael@0: return { label: feed.title || feed.href } michael@0: })).show((function(data) { michael@0: feedIndex = data.button; michael@0: if (feedIndex == -1) michael@0: return; michael@0: michael@0: this.loadFeed(feeds[feedIndex], browser); michael@0: }).bind(this)); michael@0: return; michael@0: } michael@0: michael@0: this.loadFeed(feeds[0], browser); michael@0: } michael@0: }, michael@0: michael@0: loadFeed: function fh_loadFeed(aFeed, aBrowser) { michael@0: let feedURL = aFeed.href; michael@0: michael@0: // Next, we decide on which service to send the feed michael@0: let handlers = this.getContentHandlers(this.TYPE_MAYBE_FEED); michael@0: if (handlers.length == 0) michael@0: return; michael@0: michael@0: // JSON for Prompt michael@0: let p = new Prompt({ michael@0: window: aBrowser.contentWindow, michael@0: title: Strings.browser.GetStringFromName("feedHandler.subscribeWith") michael@0: }).setSingleChoiceItems(handlers.map(function(handler) { michael@0: return { label: handler.name }; michael@0: })).show(function(data) { michael@0: if (data.button == -1) michael@0: return; michael@0: michael@0: // Merge the handler URL and the feed URL michael@0: let readerURL = handlers[data.button].uri; michael@0: readerURL = readerURL.replace(/%s/gi, encodeURIComponent(feedURL)); michael@0: michael@0: // Open the resultant URL in a new tab michael@0: BrowserApp.addTab(readerURL, { parentId: BrowserApp.selectedTab.id }); michael@0: }); michael@0: } michael@0: };