deviceMediaManagement.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 media management widget.
34 //
35 //------------------------------------------------------------------------------
36 //------------------------------------------------------------------------------
37 
38 //------------------------------------------------------------------------------
39 //
40 // Device media management 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 var DeviceMediaManagementServices = {
58  //
59  // Device media management object fields.
60  //
61  // _widget Device media management widget.
62  // _device Device we are working with.
63  //
64 
65  _widget: null,
66  _device: null,
67 
75  initialize: function DeviceMediaManagementServices__initialize(aWidget) {
76  if (!aWidget.device)
77  return;
78 
79  // Get the media management widget.
80  this._widget = aWidget;
81 
82  // Initialize object fields.
83  this._device = this._widget.device;
84 
85  // Listen for device events.
86  var deviceEventTarget =
87  this._device.QueryInterface(Ci.sbIDeviceEventTarget);
88  deviceEventTarget.addEventListener(this);
89 
90  // Initialize, read, and apply the music preferences.
91  this.musicManagementPrefsInitialize();
92  this._widget.reset();
93  },
94 
99  finalize: function DeviceMediaManagementServices_finalize() {
100  this.musicManagementPrefsFinalize();
101 
102  // Stop listening for device events.
103  if (this._device) {
104  var deviceEventTarget =
105  this._device.QueryInterface(Ci.sbIDeviceEventTarget);
106  deviceEventTarget.removeEventListener(this);
107  }
108 
109  // Clear object fields.
110  this._widget = null;
111  this._device = null;
112  },
113 
114  savePreferences: function DeviceMediaManagementServices_savePreferences() {
115  if (!this._device)
116  return;
117 
118  // Extract preferences from UI and write to storage
119  this._transcodeModeManual =
120  this._getElement("transcoding-mode-manual").selected;
121  this.musicManagementPrefsWrite();
122 
123  // Now re-read from the newly-stored preference to ensure we don't get out
124  // of sync
125  this.resetPreferences();
126  },
127 
128  resetPreferences: function DeviceMediaManagementServices_resetPreferences() {
129  if (!this._device)
130  return;
131 
132  // Re-read from preferences storage and apply
133  this.musicManagementPrefsRead();
134  this.musicManagementPrefsApply();
135  },
136 
137  _updateNotchDisabledState:
138  function DeviceMediaManagementServices__updateNotchDisabledState() {
139  var notchHbox = this._getElement("notch-hbox");
140  var spaceLimitScale = this._getElement("space_limit");
141  var zeroPercentDescription = this._getElement("notch-0percent-description");
142  var hundredPercentDescription =
143  this._getElement("notch-100percent-description");
144 
145  // Enable/disable the notch boxes.
146  for (var i = 0; i < notchHbox.childNodes.length; i++) {
147  var curChildNode = notchHbox.childNodes.item(i);
148  if (spaceLimitScale.disabled) {
149  curChildNode.setAttribute("disabled", "true");
150  }
151  else {
152  curChildNode.removeAttribute("disabled");
153  }
154  }
155 
156  // Enable/disable the percentage labels
157  if (spaceLimitScale.disabled) {
158  zeroPercentDescription.setAttribute("disabled", "true");
159  hundredPercentDescription.setAttribute("disabled", "true");
160  }
161  else {
162  zeroPercentDescription.removeAttribute("disabled");
163  hundredPercentDescription.removeAttribute("disabled");
164  }
165  },
166 
170  _updateBusyState: function DeviceMediaManagementServices__updateBusyState() {
171  var isBusy = this._device && this._device.isBusy;
172 
173  if (this._transcodeModeManual && !isBusy) {
174  this._transcodeSettings.disabled = false;
175  }
176  else {
177  this._transcodeSettings.disabled = true;
178  }
179 
180  var spaceLimitToggle = this._getElement("space_limit_enable");
181  var spaceLimitScale = this._getElement("space_limit");
182  if (!isBusy && spaceLimitToggle.checked) {
183  spaceLimitScale.removeAttribute("disabled");
184  }
185  else {
186  spaceLimitScale.setAttribute("disabled", "true");
187  }
188 
189  this._updateNotchDisabledState();
190  },
191 
192  onUIPrefChange: function DeviceMediaManagementServices_onUIPrefChange() {
193  /* Extract preferences from UI and apply */
194  this._transcodeModeManual =
195  this._getElement("transcoding-mode-manual").selected;
196  this.musicManagementPrefsApply();
197 
198  this.dispatchSettingsChangeEvent();
199  },
200 
205  dispatchSettingsChangeEvent:
206  function DeviceMediaManagementServices_dispatchSettingsChangeEvent() {
207  let event = document.createEvent("UIEvents");
208  event.initUIEvent("settings-changed", false, false, window, null);
209  document.dispatchEvent(event);
210  },
211 
212  //----------------------------------------------------------------------------
213  //
214  // Device sync sbIDeviceEventListener services.
215  //
216  //----------------------------------------------------------------------------
217 
224  onDeviceEvent:
225  function DeviceMediaManagementServices_onDeviceEvent(aEvent) {
226  // Dispatch processing of the event.
227  switch(aEvent.type)
228  {
229  case Ci.sbIDeviceEvent.EVENT_DEVICE_STATE_CHANGED:
230  this._updateBusyState();
231  break;
232 
233  default :
234  break;
235  }
236  },
237 
238  //----------------------------------------------------------------------------
239  //
240  // Device media management XUL services.
241  //
242  //----------------------------------------------------------------------------
243 
253  _getElement: function DeviceMediaManagementServices__getElement(aElementID) {
254  return document.getAnonymousElementByAttribute(this._widget,
255  "sbid",
256  aElementID);
257  },
258 
259  /*
260  * \brief selects the radio element specified by aRadioID.
261  *
262  * \param aRadioID ID of radio to select.
263  *
264  */
265 
266  _selectRadio : function DeviceMediaManagementServices__selectRadio(aRadioID)
267  {
268  var radioElem;
269 
270  /* Get the radio element. */
271  radioElem = this._getElement(aRadioID);
272 
273  /* Select the radio. */
274  radioElem.radioGroup.selectedItem = radioElem;
275  },
276 
277  /* *****************************************************************************
278  *
279  * device media management preference services.
280  *
281  ******************************************************************************/
282 
283  /*
284  * _transcodeSettings Transcode settings for the device.
285  *
286  */
287 
288  _transcodeSettings : null,
289 
290  /*
291  * musicManagementPrefsInitialize
292  *
293  * \brief This function initializes the music preference services.
294  */
295 
296  musicManagementPrefsInitialize:
297  function DeviceMediaManagementServices_musicManagementPrefsInitialize()
298  {
299  /* Initialize the working preference set. */
300  this.musicManagementPrefsInitPrefs();
301 
302  /* Initialize the preference UI. */
303  this.musicManagementPrefsInitUI();
304  },
305 
306 
307  /*
308  * musicManagementPrefsFinalize
309  *
310  * \brief This function finalizes the music preference services.
311  */
312 
313  musicManagementPrefsFinalize:
314  function DeviceMediaManagementServices_musicManagementPrefsFinalize()
315  {
316  /* Clear object fields. */
317  this._transcodeSettings = null;
318  },
319 
320 
321  /*
322  * musicManagementPrefsInitPrefs
323  *
324  * \brief This function initializes the working music management preference
325  * set. It sets up the working music management preference object with all
326  * of the preference fields and sets default values. It does not read the
327  * preferences from the preference storage.
328  */
329 
330  musicManagementPrefsInitPrefs:
331  function DeviceMediaManagementServices_musicManagementPrefsInitPrefs()
332  {
333  this._transcodeModeManual = false;
334 
335  this._transcodeSettings = this._getElement("transcode-settings");
336 
337  var transcodeManager =
338  Cc["@songbirdnest.com/Songbird/Mediacore/TranscodeManager;1"]
339  .getService(Ci.sbITranscodeManager);
340 
341  var supportedProfiles = [];
342  var profiles = transcodeManager.getTranscodeProfiles(
343  Ci.sbITranscodeProfile.TRANSCODE_TYPE_AUDIO);
344  var deviceCaps = this._device.capabilities;
345  var formatMimeTypes = deviceCaps.getSupportedMimeTypes(
346  Ci.sbIDeviceCapabilities.CONTENT_AUDIO, {});
347 
348  for (formatMimeTypeIndex in formatMimeTypes) {
349  var formats = deviceCaps.
350  getFormatTypes(Ci.sbIDeviceCapabilities.CONTENT_AUDIO,
351  formatMimeTypes[formatMimeTypeIndex], {});
352  for (formatIndex in formats) {
353  var format = formats[formatIndex];
354  var container = format.containerFormat;
355  var audioCodec = format.audioCodec;
356 
357  for (var i = 0; i < profiles.length; i++) {
358  var profile = profiles.queryElementAt(i, Ci.sbITranscodeProfile);
359 
360  if (profile.type == Ci.sbITranscodeProfile.TRANSCODE_TYPE_AUDIO &&
361  profile.containerFormat == container &&
362  profile.audioCodec == audioCodec)
363  {
364  supportedProfiles.push(profile);
365  }
366  }
367  }
368  }
369 
370  this._transcodeSettings.profiles = supportedProfiles;
371  },
372 
373 
374  /*
375  * musicManagementPrefsInitUI
376  *
377  * \brief This function initializes the music management preference UI
378  * elements to match the set of working music preference fields.
379  * It does not apply the preference values to the UI.
380  */
381 
382  musicManagementPrefsInitUI:
383  function DeviceMediaManagementServices_musicManagementPrefsInitUI() {
384  // Get the available transcoding profiles.
385  var profiles = this._transcodeSettings.profiles;
386 
387  // Show the transcoding preferences UI if any transcoding profiles are
388  // available. Otherwise, show the no encoders available description.
389  var transcodingSettingsDeckElem =
390  this._getElement("transcoding_settings_deck");
391  if (profiles.length > 0) {
392  transcodingSettingsDeckElem.selectedPanel =
393  this._getElement("transcoding_preferences");
394  }
395  else {
396  transcodingSettingsDeckElem.selectedPanel =
397  this._getElement("no_encoders_available_description");
398  }
399  },
400 
401 
402  /*
403  * musicManagementPrefsRead
404  *
405  * \brief This function reads the media Management preferences from the
406  * preference storage into the working media management preferences
407  * object. It also updates the stored media Management prefs.
408  */
409 
410  musicManagementPrefsRead:
411  function DeviceMediaManagementServices_musicManagementPrefsRead(aPrefs)
412  {
413  var profileId = this._device.getPreference("transcode_profile.profile_id");
414 
415  // Only store the profile id if transcode is in manual mode.
416  if (profileId) {
417  this._transcodeModeManual = true;
418  this._transcodeSettings.transcodeBitrate =
419  this._device.getPreference(
420  "transcode_profile.audio_properties.bitrate");
421  this._transcodeSettings.transcodeProfileId = profileId;
422  }
423  // Otherwise, get the profile id from global one or pick the one
424  // with highest priority.
425  else {
426  this._transcodeModeManual = false;
427 
428  // Get the global default profile if there's one of those.
429  var gProfileId = Application.prefs.getValue(
430  "songbird.device.transcode_profile.profile_id", null);
431  if (gProfileId) {
432  this._transcodeSettings.transcodeBitrate =
433  Application.prefs.getValue(
434  "songbird.device.transcode_profile.audio_properties.bitrate",
435  null);
436  this._transcodeSettings.transcodeProfileId = gProfileId;
437  } else {
438  this._transcodeSettings.transcodeProfile = null;
439  this._transcodeSettings.transcodeBitrate = null;
440  }
441  }
442  },
443 
444 
445  /*
446  * musicManagementPrefsWrite
447  *
448  * \brief This function writes the working music preferences to the preference
449  * storage.
450  */
451 
452  musicManagementPrefsWrite:
453  function DeviceMediaManagementServices_musicManagementPrefsWrite()
454  {
455  if (this._transcodeModeManual) {
456  this._device.setPreference("transcode_profile.profile_id",
457  this._transcodeSettings.transcodeProfileId);
458  this._device.setPreference("transcode_profile.audio_properties.bitrate",
459  this._transcodeSettings.transcodeBitrate);
460  } else {
461  this._device.setPreference("transcode_profile.profile_id", "");
462  // Resets transcode settings to default
463  this._transcodeSettings.transcodeProfileId = "";
464  }
465  },
466 
467 
468  /*
469  * musicManagementPrefsApply
470  *
471  * \brief This function applies the working preference set to the preference UI.
472  */
473 
474  musicManagementPrefsApply:
475  function DeviceMediaManagementServices_musicManagementPrefsApply()
476  {
477  if (this._transcodeModeManual)
478  this._selectRadio("transcoding-mode-manual");
479  else {
480  this._selectRadio("transcoding-mode-auto");
481  }
482 
483  return this._updateBusyState();
484  }
485 }
const Cu
function Fx prototype initialize
const Cc
var Application
Definition: sbAboutDRM.js:37
const XUL_NS
Definition: FeedWriter.js:83
var event
let window
return null
Definition: FeedWriter.js:1143
const Cr
const Ci
_getSelectedPageStyle s i