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