prefs.js
Go to the documentation of this file.
1 var mashTapePreferences = {
2  populateServices: function(providerType) {
3  var box = document.getElementById("services-" + providerType);
4  var enabledKey = "extensions.mashTape." + providerType + ".enabled";
5  var enabled = Application.prefs.getValue(enabledKey, true);
6 
7  for each (var contract in this.providers[providerType]) {
8  var classId = this.compMgr.contractIDToCID(contract);
9  var prov = Cc[contract].createInstance(Ci.sbIMashTapeProvider);
10 
11  var element = "checkbox";
12  var attribute = "checked";
13  switch(providerType) {
14  case "info":
15  prov = prov.QueryInterface(Ci.sbIMashTapeInfoProvider);
16  element = "radio";
17  attribute = "selected";
18  break;
19  case "review":
20  prov = prov.QueryInterface(Ci.sbIMashTapeReviewProvider);
21  break;
22  case "rss":
23  prov = prov.QueryInterface(Ci.sbIMashTapeRSSProvider);
24  break;
25  case "photo":
26  prov = prov.QueryInterface(Ci.sbIMashTapePhotoProvider);
27  element = "radio";
28  attribute = "selected";
29  break;
30  case "flash":
31  prov = prov.QueryInterface(Ci.sbIMashTapeFlashProvider);
32  break;
33  default:
34  break;
35  }
36 
37  var li = document.createElement("richlistitem");
38 
39  var providers = Application.prefs.getValue(
40  "extensions.mashTape." + providerType + ".providers", "")
41  .split(",");
42  var el = document.createElement(element);
43  el.setAttribute("command", "cmd-" + providerType);
44  el.setAttribute("value", classId);
45  for each (var provider in providers) {
46  if (classId == provider)
47  el.setAttribute(attribute, true);
48  }
49  li.appendChild(el);
50 
51  if (providerType == "info") {
52  var icons = new Object;
53  var favicon = document.createElement("image");
54  favicon.setAttribute("src", prov.providerIconBio);
55  li.appendChild(favicon);
56  icons[prov.providerIconBio] = 1;
57  if (typeof(icons[prov.providerIconTags]) == "undefined") {
58  favicon = document.createElement("image");
59  favicon.setAttribute("src", prov.providerIconTags);
60  li.appendChild(favicon);
61  icons[prov.providerIconTags] = 1;
62  }
63  if (typeof(icons[prov.providerIconDiscography]) == "undefined") {
64  favicon = document.createElement("image");
65  favicon.setAttribute("src", prov.providerIconDiscography);
66  li.appendChild(favicon);
67  icons[prov.providerIconDiscography] = 1;
68  }
69  if (typeof(icons[prov.providerIconMembers]) == "undefined") {
70  favicon = document.createElement("image");
71  favicon.setAttribute("src", prov.providerIconMembers);
72  li.appendChild(favicon);
73  icons[prov.providerIconMembers] = 1;
74  }
75  if (typeof(icons[prov.providerIconLinks]) == "undefined") {
76  favicon = document.createElement("image");
77  favicon.setAttribute("src", prov.providerIconLinks);
78  li.appendChild(favicon);
79  icons[prov.providerIconLinks] = 1;
80  }
81  } else {
82  var favicon = document.createElement("image");
83  favicon.setAttribute("src", prov.providerIcon);
84  li.appendChild(favicon);
85  }
86 
87  var serviceLabel = document.createElement("label");
88  serviceLabel.setAttribute("value", prov.providerName);
89  li.appendChild(serviceLabel);
90 
91  box.appendChild(li);
92  }
93 
94  // Set initial disabled or enabled state
95  this.toggleServices(box, providerType, !enabled);
96  },
97 
98 
99  enumerateServices: function() {
100  this.compMgr =
101  Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
102 
103  this.providers = new Array();
104  this.providers["info"] = new Array();
105  this.providers["review"] = new Array();
106  this.providers["rss"] = new Array();
107  this.providers["photo"] = new Array();
108  this.providers["flash"] = new Array();
109 
110  for (var contract in Cc) {
111  if (contract.indexOf("mashTape/provider") >= 0) {
112  var mtClass = contract.substring(
113  contract.indexOf("mashTape/provider") + 18,contract.length);
114  mtClass = mtClass.substring(0, mtClass.indexOf("/"));
115  this.providers[mtClass].push(contract);
116  }
117  }
118  this.providers["info"].sort();
119  this.providers["review"].sort();
120  this.providers["rss"].sort();
121  this.providers["photo"].sort();
122  this.providers["flash"].sort();
123  },
124 
125  savePrefs: function(providerType) {
126  var box = document.getElementById("services-" + providerType);
127  var prefKey = "extensions.mashTape." + providerType + ".providers";
128  var prefServices = new Array();
129  var attr;
130  switch (providerType) {
131  case "rss":
132  case "review":
133  case "flash":
134  attr = "checked";
135  break;
136  default:
137  attr = "selected";
138  break;
139  }
140 
141  var providerSelected = false;
142  for each (var rli in box.childNodes) {
143  if (rli.tagName != "richlistitem")
144  continue;
145  var el = rli.firstChild;
146  if (el.hasAttribute(attr)) {
147  prefServices.push(el.getAttribute("value"));
148  providerSelected = true;
149  }
150  };
151  if (!providerSelected) {
152  document.getElementById("checkbox-" + providerType).checked = false;
153  mashTapePreferences.toggle(providerType);
154  }
155 
156  Application.prefs.setValue(prefKey, prefServices.join(","));
157  },
158 
159  toggle: function(providerType) {
160  var checkbox = document.getElementById("checkbox-" + providerType);
161  var box = document.getElementById("services-" + providerType);
162 
163  // Save the pref first
164  var enabledKey = "extensions.mashTape." + providerType + ".enabled";
165  var enabled = checkbox.hasAttribute("checked");
166  Application.prefs.setValue(enabledKey, enabled);
167 
168  // Now see if we should disable the richlistbox
169  this.toggleServices(box, providerType, !enabled);
170  if (providerType == "photo") {
171  var hbox = document.getElementById("hbox-photo-speed");
172  for each (var node in hbox.childNodes)
173  node.disabled = !enabled;
174  }
175 
176  // ensure if we're turning it on that at least one provider is
177  // enabled
178  if (enabled) {
179  var box = document.getElementById("services-" + providerType);
180  var attr;
181  switch (providerType) {
182  case "rss":
183  case "review":
184  case "flash":
185  attr = "checked";
186  break;
187  default:
188  attr = "selected";
189  break;
190  }
191 
192  var providerSelected = false;
193  var firstProvider = null;
194  for each (var rli in box.childNodes) {
195  if (rli.tagName != "richlistitem")
196  continue;
197  var el = rli.firstChild;
198  if (!firstProvider)
199  firstProvider = el;
200  };
201  if (!providerSelected) {
202  // no provider is selected, enable the first one
203  firstProvider.setAttribute(attr, true);
204  mashTapePreferences.savePrefs(providerType);
205  }
206  }
207  },
208 
209  toggleServices: function(box, providerType, disabled) {
210  var helpText = document.getElementById("help-" + providerType);
211  helpText.disabled = disabled;
212  helpText.setAttribute("disabled", disabled);
213  box.setAttribute("disabled", disabled);
214  for each (var rli in box.childNodes) {
215  if (rli.tagName != "richlistitem")
216  continue;
217  var el = rli.firstChild;
218  el.setAttribute("disabled", disabled);
219  }
220  },
221 
222  observe: function(subject, topic, data) {
223  if (subject instanceof Components.interfaces.nsIPrefBranch) {
224  var pref = data.split(".");
225  if (pref.length == 2 && pref[1] == "enabled") {
226  var tabName = pref[0];
227  this.checkDefaultTab(tabName);
228  }
229  }
230  },
231 
232  getDefaultTabItem : function(providerType) {
233  for (var i=0; i<this.defaultTabSelect.itemCount; i++) {
234  var item = this.defaultTabSelect.getItemAtIndex(i);
235  if (item.value == providerType)
236  return item;
237  }
238  return null;
239  },
240 
241  checkDefaultTab: function(tabName) {
242  var prefKey = tabName + ".enabled";
243  var enabled = this._prefBranch.getBoolPref(prefKey);
244  var item = this.getDefaultTabItem(tabName);
245  if (!enabled) {
246  item.disabled = true;
247  if (item.selected) {
248  // select an enabled tab instead
249  for each (var tab in ["info", "review","rss", "photo", "flash"])
250  {
251  if (this._prefBranch.getBoolPref(tab + ".enabled") &&
252  this.providers[tab].length > 0) {
253  var sItem = this.getDefaultTabItem(tab);
254  this.defaultTabSelect.selectedItem = sItem;
255  this._prefBranch.setCharPref("defaultpane", tab);
256  break;
257  }
258  }
259  }
260  } else
261  item.disabled = false;
262  },
263 
264  init: function() {
265  this.defaultTabSelect = document.getElementById("general-default-tab");
266 
267  // Setup our preferences observer
268  this._prefBranch = Cc["@mozilla.org/preferences-service;1"]
269  .getService(Ci.nsIPrefService).getBranch("extensions.mashTape.")
270  .QueryInterface(Ci.nsIPrefBranch2);
271  this._prefBranch.addObserver("", this, false);
272 
273  for each (var tab in ["info", "review", "rss", "photo", "flash"])
274  this.checkDefaultTab(tab);
275 
276  if (!this._prefBranch.getBoolPref("photo.enabled")) {
277  var hbox = document.getElementById("hbox-photo-speed");
278  for each (var node in hbox.childNodes)
279  node.disabled = true;
280  }
281 
282  /* Mozilla bugs: https://bugzilla.mozilla.org/show_bug.cgi?id=521215,
283  * https://bugzilla.mozilla.org/show_bug.cgi?id=499105
284  *
285  * XUL scale widget cannot load preference, so set the value based on
286  * the preference, then set the preference attribute so that the widget
287  * writes changes.
288  */
289  var scale = document.getElementById("scale-photo-speed");
290  scale.value = this._prefBranch.getIntPref("photo.speed");
291  scale.setAttribute("preference", "photo-speed");
292 
293  this.enumerateServices();
294  this.populateServices("info");
295  this.populateServices("review");
296  this.populateServices("rss");
297  this.populateServices("photo");
298  this.populateServices("flash");
299  },
300 
301  unload: function() {
302  this._prefBranch.removeObserver("", this);
303  }
304 }
const Cc
var Application
Definition: sbAboutDRM.js:37
pref("extensions.7digital.configured", false)
var tab
systray _prefBranch
Definition: mainwin.js:150
_window init
Definition: FeedWriter.js:1144
ConcertTicketing unload
Definition: browse.js:54
return null
Definition: FeedWriter.js:1143
let node
function toggle(toShow, toHide, data, clickedActive, down)
observe topic
Definition: FeedWriter.js:1326
const Ci
observe data
Definition: FeedWriter.js:1329
_getSelectedPageStyle s i
sbDeviceFirmwareAutoCheckForUpdate prototype observe