mobile/android/chrome/content/FeedHandler.js

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 "use strict";
michael@0 5
michael@0 6 var FeedHandler = {
michael@0 7 PREF_CONTENTHANDLERS_BRANCH: "browser.contentHandlers.types.",
michael@0 8 TYPE_MAYBE_FEED: "application/vnd.mozilla.maybe.feed",
michael@0 9
michael@0 10 _contentTypes: null,
michael@0 11
michael@0 12 getContentHandlers: function fh_getContentHandlers(contentType) {
michael@0 13 if (!this._contentTypes)
michael@0 14 this.loadContentHandlers();
michael@0 15
michael@0 16 if (!(contentType in this._contentTypes))
michael@0 17 return [];
michael@0 18
michael@0 19 return this._contentTypes[contentType];
michael@0 20 },
michael@0 21
michael@0 22 loadContentHandlers: function fh_loadContentHandlers() {
michael@0 23 this._contentTypes = {};
michael@0 24
michael@0 25 let kids = Services.prefs.getBranch(this.PREF_CONTENTHANDLERS_BRANCH).getChildList("");
michael@0 26
michael@0 27 // First get the numbers of the providers by getting all ###.uri prefs
michael@0 28 let nums = [];
michael@0 29 for (let i = 0; i < kids.length; i++) {
michael@0 30 let match = /^(\d+)\.uri$/.exec(kids[i]);
michael@0 31 if (!match)
michael@0 32 continue;
michael@0 33 else
michael@0 34 nums.push(match[1]);
michael@0 35 }
michael@0 36
michael@0 37 // Sort them, to get them back in order
michael@0 38 nums.sort(function(a, b) { return a - b; });
michael@0 39
michael@0 40 // Now register them
michael@0 41 for (let i = 0; i < nums.length; i++) {
michael@0 42 let branch = Services.prefs.getBranch(this.PREF_CONTENTHANDLERS_BRANCH + nums[i] + ".");
michael@0 43 let vals = branch.getChildList("");
michael@0 44 if (vals.length == 0)
michael@0 45 return;
michael@0 46
michael@0 47 try {
michael@0 48 let type = branch.getCharPref("type");
michael@0 49 let uri = branch.getComplexValue("uri", Ci.nsIPrefLocalizedString).data;
michael@0 50 let title = branch.getComplexValue("title", Ci.nsIPrefLocalizedString).data;
michael@0 51
michael@0 52 if (!(type in this._contentTypes))
michael@0 53 this._contentTypes[type] = [];
michael@0 54 this._contentTypes[type].push({ contentType: type, uri: uri, name: title });
michael@0 55 }
michael@0 56 catch(ex) {}
michael@0 57 }
michael@0 58 },
michael@0 59
michael@0 60 observe: function fh_observe(aSubject, aTopic, aData) {
michael@0 61 if (aTopic === "Feeds:Subscribe") {
michael@0 62 let args = JSON.parse(aData);
michael@0 63 let tab = BrowserApp.getTabForId(args.tabId);
michael@0 64 if (!tab)
michael@0 65 return;
michael@0 66
michael@0 67 let browser = tab.browser;
michael@0 68 let feeds = browser.feeds;
michael@0 69 if (feeds == null)
michael@0 70 return;
michael@0 71
michael@0 72 // First, let's decide on which feed to subscribe
michael@0 73 let feedIndex = -1;
michael@0 74 if (feeds.length > 1) {
michael@0 75 let p = new Prompt({
michael@0 76 window: browser.contentWindow,
michael@0 77 title: Strings.browser.GetStringFromName("feedHandler.chooseFeed")
michael@0 78 }).setSingleChoiceItems(feeds.map(function(feed) {
michael@0 79 return { label: feed.title || feed.href }
michael@0 80 })).show((function(data) {
michael@0 81 feedIndex = data.button;
michael@0 82 if (feedIndex == -1)
michael@0 83 return;
michael@0 84
michael@0 85 this.loadFeed(feeds[feedIndex], browser);
michael@0 86 }).bind(this));
michael@0 87 return;
michael@0 88 }
michael@0 89
michael@0 90 this.loadFeed(feeds[0], browser);
michael@0 91 }
michael@0 92 },
michael@0 93
michael@0 94 loadFeed: function fh_loadFeed(aFeed, aBrowser) {
michael@0 95 let feedURL = aFeed.href;
michael@0 96
michael@0 97 // Next, we decide on which service to send the feed
michael@0 98 let handlers = this.getContentHandlers(this.TYPE_MAYBE_FEED);
michael@0 99 if (handlers.length == 0)
michael@0 100 return;
michael@0 101
michael@0 102 // JSON for Prompt
michael@0 103 let p = new Prompt({
michael@0 104 window: aBrowser.contentWindow,
michael@0 105 title: Strings.browser.GetStringFromName("feedHandler.subscribeWith")
michael@0 106 }).setSingleChoiceItems(handlers.map(function(handler) {
michael@0 107 return { label: handler.name };
michael@0 108 })).show(function(data) {
michael@0 109 if (data.button == -1)
michael@0 110 return;
michael@0 111
michael@0 112 // Merge the handler URL and the feed URL
michael@0 113 let readerURL = handlers[data.button].uri;
michael@0 114 readerURL = readerURL.replace(/%s/gi, encodeURIComponent(feedURL));
michael@0 115
michael@0 116 // Open the resultant URL in a new tab
michael@0 117 BrowserApp.addTab(readerURL, { parentId: BrowserApp.selectedTab.id });
michael@0 118 });
michael@0 119 }
michael@0 120 };

mercurial