1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/chrome/content/FeedHandler.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,120 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 +"use strict"; 1.8 + 1.9 +var FeedHandler = { 1.10 + PREF_CONTENTHANDLERS_BRANCH: "browser.contentHandlers.types.", 1.11 + TYPE_MAYBE_FEED: "application/vnd.mozilla.maybe.feed", 1.12 + 1.13 + _contentTypes: null, 1.14 + 1.15 + getContentHandlers: function fh_getContentHandlers(contentType) { 1.16 + if (!this._contentTypes) 1.17 + this.loadContentHandlers(); 1.18 + 1.19 + if (!(contentType in this._contentTypes)) 1.20 + return []; 1.21 + 1.22 + return this._contentTypes[contentType]; 1.23 + }, 1.24 + 1.25 + loadContentHandlers: function fh_loadContentHandlers() { 1.26 + this._contentTypes = {}; 1.27 + 1.28 + let kids = Services.prefs.getBranch(this.PREF_CONTENTHANDLERS_BRANCH).getChildList(""); 1.29 + 1.30 + // First get the numbers of the providers by getting all ###.uri prefs 1.31 + let nums = []; 1.32 + for (let i = 0; i < kids.length; i++) { 1.33 + let match = /^(\d+)\.uri$/.exec(kids[i]); 1.34 + if (!match) 1.35 + continue; 1.36 + else 1.37 + nums.push(match[1]); 1.38 + } 1.39 + 1.40 + // Sort them, to get them back in order 1.41 + nums.sort(function(a, b) { return a - b; }); 1.42 + 1.43 + // Now register them 1.44 + for (let i = 0; i < nums.length; i++) { 1.45 + let branch = Services.prefs.getBranch(this.PREF_CONTENTHANDLERS_BRANCH + nums[i] + "."); 1.46 + let vals = branch.getChildList(""); 1.47 + if (vals.length == 0) 1.48 + return; 1.49 + 1.50 + try { 1.51 + let type = branch.getCharPref("type"); 1.52 + let uri = branch.getComplexValue("uri", Ci.nsIPrefLocalizedString).data; 1.53 + let title = branch.getComplexValue("title", Ci.nsIPrefLocalizedString).data; 1.54 + 1.55 + if (!(type in this._contentTypes)) 1.56 + this._contentTypes[type] = []; 1.57 + this._contentTypes[type].push({ contentType: type, uri: uri, name: title }); 1.58 + } 1.59 + catch(ex) {} 1.60 + } 1.61 + }, 1.62 + 1.63 + observe: function fh_observe(aSubject, aTopic, aData) { 1.64 + if (aTopic === "Feeds:Subscribe") { 1.65 + let args = JSON.parse(aData); 1.66 + let tab = BrowserApp.getTabForId(args.tabId); 1.67 + if (!tab) 1.68 + return; 1.69 + 1.70 + let browser = tab.browser; 1.71 + let feeds = browser.feeds; 1.72 + if (feeds == null) 1.73 + return; 1.74 + 1.75 + // First, let's decide on which feed to subscribe 1.76 + let feedIndex = -1; 1.77 + if (feeds.length > 1) { 1.78 + let p = new Prompt({ 1.79 + window: browser.contentWindow, 1.80 + title: Strings.browser.GetStringFromName("feedHandler.chooseFeed") 1.81 + }).setSingleChoiceItems(feeds.map(function(feed) { 1.82 + return { label: feed.title || feed.href } 1.83 + })).show((function(data) { 1.84 + feedIndex = data.button; 1.85 + if (feedIndex == -1) 1.86 + return; 1.87 + 1.88 + this.loadFeed(feeds[feedIndex], browser); 1.89 + }).bind(this)); 1.90 + return; 1.91 + } 1.92 + 1.93 + this.loadFeed(feeds[0], browser); 1.94 + } 1.95 + }, 1.96 + 1.97 + loadFeed: function fh_loadFeed(aFeed, aBrowser) { 1.98 + let feedURL = aFeed.href; 1.99 + 1.100 + // Next, we decide on which service to send the feed 1.101 + let handlers = this.getContentHandlers(this.TYPE_MAYBE_FEED); 1.102 + if (handlers.length == 0) 1.103 + return; 1.104 + 1.105 + // JSON for Prompt 1.106 + let p = new Prompt({ 1.107 + window: aBrowser.contentWindow, 1.108 + title: Strings.browser.GetStringFromName("feedHandler.subscribeWith") 1.109 + }).setSingleChoiceItems(handlers.map(function(handler) { 1.110 + return { label: handler.name }; 1.111 + })).show(function(data) { 1.112 + if (data.button == -1) 1.113 + return; 1.114 + 1.115 + // Merge the handler URL and the feed URL 1.116 + let readerURL = handlers[data.button].uri; 1.117 + readerURL = readerURL.replace(/%s/gi, encodeURIComponent(feedURL)); 1.118 + 1.119 + // Open the resultant URL in a new tab 1.120 + BrowserApp.addTab(readerURL, { parentId: BrowserApp.selectedTab.id }); 1.121 + }); 1.122 + } 1.123 +};