deviceTranscodeSettings.js
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2010 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
30 //------------------------------------------------------------------------------
31 //------------------------------------------------------------------------------
32 //
33 // Device transcode settings widget.
34 //
35 //------------------------------------------------------------------------------
36 //------------------------------------------------------------------------------
37 
38 //------------------------------------------------------------------------------
39 //
40 // Device transcode settings defs.
41 //
42 //------------------------------------------------------------------------------
43 
44 // Component manager defs.
45 if (typeof(Cc) == "undefined")
46  var Cc = Components.classes;
47 if (typeof(Ci) == "undefined")
48  var Ci = Components.interfaces;
49 if (typeof(Cr) == "undefined")
50  var Cr = Components.results;
51 if (typeof(Cu) == "undefined")
52  var Cu = Components.utils;
53 
54 if (typeof(XUL_NS) == "undefined")
55  var XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
56 
57 Cu.import("resource://app/jsmodules/sbProperties.jsm");
58 Cu.import("resource://app/jsmodules/WindowUtils.jsm");
59 
60 //------------------------------------------------------------------------------
61 //
62 // Device transcode settings service.
63 //
64 //------------------------------------------------------------------------------
65 
66 var DTSW = {
67  //
68  // Device transcode settings object fields.
69  //
70  // _widget Device transcode settings widget.
71  // _disabled Disabled state.
72  // _showDesc Quality description display.
73  //
74 
75  _widget: null,
76  _disabled: false,
77  _showDesc: false,
78 
85  initialize: function DTSW__initialize(aWidget) {
86  // Get the device widget.
87  this._widget = aWidget;
88 
89  // Initialize object fields.
90  var disabled = this._widget.getAttribute("disabled");
91  if (disabled)
92  this._disabled = true;
93 
94  this._showDescription();
95  this._update();
96 
97  },
98 
103  finalize: function DTSW_finalize() {
104  this._widget = null;
105  this._disabled = null;
106  },
107 
108  //----------------------------------------------------------------------------
109  //
110  // Device transcode settings UI update services.
111  //
112  //----------------------------------------------------------------------------
113 
118  _update: function DTSW__update() {
119  var modeDetailsBox = this._getElement("transcoding-mode-details");
120  var children = modeDetailsBox.childNodes;
121  for (let i = 0; i < children.length; i++) {
122  let currChild = children.item(i);
123  if (this._disabled) {
124  currChild.setAttribute("disabled", true);
125  } else {
126  currChild.removeAttribute("disabled");
127  }
128  }
129  },
130 
135  _showDescription: function DTSW__showDescription() {
136  var showDesc = this._widget.getAttribute("showDesc");
137  var qualityLabel = this._getElement("transcoding-quality-desc");
138  if (showDesc) {
139  qualityLabel.removeAttribute("hidden");
140  } else {
141  qualityLabel.setAttribute("hidden", true);
142  }
143  },
144 
155  _addBitrate: function DTSW__addBitrate(aBitrate, aSelected) {
156  var bitrateEntry = this._getElement("transcoding-bitrate-kbps");
157  var bitrateList = this._getElement("transcoding-bitrate-list");
158  var bitrateMenuItem = document.createElementNS(XUL_NS, "menuitem");
159  bitrateMenuItem.setAttribute("label", aBitrate);
160  bitrateMenuItem.setAttribute("value", aBitrate);
161  bitrateList.appendChild(bitrateMenuItem);
162  // Select the bitrate.
163  if (aSelected == aBitrate) {
164  bitrateMenuItem.setAttribute("selected", "true");
165  bitrateEntry.selectedItem = bitrateMenuItem;
166  } else {
167  bitrateMenuItem.setAttribute("selected", "false");
168  }
169  return aBitrate;
170  },
171 
180  _rebuildProfiles: function DTSW__rebuildProfiles(aProfiles) {
181  this._profiles = aProfiles;
182 
183  // Get the transcoding format menu list.
184  var profilesMenu = this._getElement("encoding-format-menu");
185  var profilesMenuList = this._getElement("encoding-format-list");
186 
187  // Clear the menu list.
188  while (profilesMenuList.firstChild)
189  profilesMenuList.removeChild(profilesMenuList.firstChild);
190 
191  // Fill in the menu list with each available profile.
192  for (let i = 0; i < this._profiles.length; i++) {
193  var profile = this._profiles[i];
194  var readableName = profile.description;
195  var menuItem = document.createElementNS(XUL_NS, "menuitem");
196  menuItem.setAttribute("label", readableName);
197  menuItem.setAttribute("value", profile.id);
198  // Add the menu item to the list.
199  profilesMenuList.appendChild(menuItem);
200  }
201  },
202 
211  _activateProfileById: function DTSW__activateProfileById(aId) {
212  var profile = this._getProfileFromProfileId(aId);
213  this._activateProfile(profile);
214  },
215 
225  _activateProfile: function DTSW__activateProfile(aProfile) {
226  var defaulted = false;
227  // For invalid profiles, obtain the default profile before proceeding.
228  if (!aProfile) {
229  aProfile = this._getDefaultProfile();
230  defaulted = true;
231  }
232 
233  var formatList = this._getElement("encoding-format-list");
234  for (var i = 0; i < formatList.childNodes.length; i++) {
235  var formatMenuItem = formatList.childNodes.item(i);
236  if (formatMenuItem.value == aProfile.id) {
237  formatMenuItem.setAttribute("selected", "true");
238  this._getElement("encoding-format-menu").selectedItem = formatMenuItem;
239  this._selectedProfile = aProfile;
240  } else {
241  formatMenuItem.setAttribute("selected", "false");
242  }
243  }
244 
245  // Update the bitrate menu
246  this._updateBitrates(aProfile, defaulted);
247  },
248 
256  _updateBitrates: function DTSW__updateBitrates(aProfile, aDefaulted) {
257  // Does the active profile have a bitrate property?
258  var foundBitrate = false;
259  var propertiesArray = aProfile.audioProperties;
260  if (propertiesArray) {
261  for (var i = 0; i < propertiesArray.length; i++) {
262  var property = propertiesArray.queryElementAt(i,
263  Ci.sbITranscodeProfileProperty);
264  if (property.propertyName == "bitrate") {
265  foundBitrate = true;
266  var bitrateEntry = this._getElement("transcoding-bitrate-kbps");
267  var bitrateList = this._getElement("transcoding-bitrate-list");
268  var brIndex = bitrateEntry.selectedIndex;
269  var selected = bitrateEntry.value;
270  // Clear the bitrate menu items.
271  while (bitrateList.firstChild)
272  bitrateList.removeChild(bitrateList.firstChild);
273 
274  var defaultBitrate = parseInt(property.value) / 1000;
275  // Set the bitrate for transcode profile (either by pref or
276  // default).
277  if (this._selectedBitrate && !aDefaulted) {
278  selected = parseInt(this._selectedBitrate) / 1000;
279  } else {
280  selected = defaultBitrate;
281  }
282 
283  var bitrate = parseInt(property.valueMin) / 1000;
284  var maxBitrate = parseInt(property.valueMax) / 1000;
285  var increment = parseInt(bitrateEntry.getAttribute("increment"));
286  var bitrateArray = [];
287 
288  // Create bitrate menu items for given profile.
289  while (bitrate < maxBitrate) {
290  bitrateArray.push(this._addBitrate(bitrate, selected));
291  // Increment for creating the next bitrate.
292  bitrate += increment;
293  var mod = bitrate % increment;
294  if (mod != 0) {
295  bitrate += increment - mod;
296  }
297  }
298 
299  // Create the lastChild for the bitrate menulist.
300  bitrateArray.push(this._addBitrate(maxBitrate, selected));
301 
302  // In the case where a previous bitrate did not exist in the updated
303  // menulist, select the menuitem by previous index or default.
304  if (bitrateArray.indexOf(parseInt(bitrateEntry.value)) == -1) {
305  if (brIndex >= bitrateArray.length) {
306  bitrateEntry.selectedItem = bitrateList.lastChild;
307  } else if (brIndex >= 0) {
308  bitrateEntry.selectedIndex = brIndex;
309  } else {
310  bitrateEntry.selectedIndex = bitrateArray.indexOf(defaultBitrate);
311  }
312  }
313 
314  // Store the selected bitrate
315  this._selectedBitrate = parseInt(bitrateEntry.value) * 1000;
316  }
317  }
318  }
319 
320  var bitrateBox = this._getElement("transcoding-bitrate");
321  if (foundBitrate) {
322  bitrateBox.removeAttribute("hidden");
323  }
324  else {
325  bitrateBox.setAttribute("hidden", "true");
326  }
327  },
328 
337  _getProfileFromProfileId: function DTSW__getProfileFromProfileId(aId) {
338  for (var i = 0; i < this._profiles.length; i++) {
339  var profile = this._profiles[i];
340  if (profile.id == aId)
341  return profile;
342  }
343  return null;
344  },
345 
352  _getDefaultProfile: function DTSW__getDefaultProfile() {
353  return this._profiles.reduce(function(high, next) {
354  return high.priority > next.priority ? high : next;
355  });
356  },
357 
367  _getElement: function DTSW__getElement(aElementID) {
368  return document.getAnonymousElementByAttribute(this._widget,
369  "sbid",
370  aElementID);
371  }
372 };
const Cu
function Fx prototype initialize
const Cc
const XUL_NS
Definition: FeedWriter.js:83
var menuItem
Definition: FeedWriter.js:970
return null
Definition: FeedWriter.js:1143
const Cr
const Ci
_getSelectedPageStyle s i
function next()