47 Components.utils.import(
"resource://app/jsmodules/DOMUtils.jsm");
48 Components.utils.import(
"resource://app/jsmodules/StringUtils.jsm");
49 Components.utils.import(
"resource://app/jsmodules/sbProperties.jsm");
50 Components.utils.import(
"resource://app/jsmodules/WindowUtils.jsm");
52 Components.utils.import(
"resource://app/jsmodules/sbLibraryUtils.jsm");
53 Components.utils.import(
"resource://app/jsmodules/ArrayConverter.jsm");
63 if (typeof(
Cc) ==
"undefined")
64 var
Cc = Components.classes;
65 if (typeof(
Ci) ==
"undefined")
66 var
Ci = Components.interfaces;
67 if (typeof(
Cr) ==
"undefined")
68 var
Cr = Components.results;
69 if (typeof(
Cu) ==
"undefined")
70 var
Cu = Components.utils;
87 this._widget = aWidget;
111 _deviceLibrary:
null,
113 _domEventListenerSet:
null,
114 _deviceListenerAdded:
false,
115 _currentState: Ci.sbIDevice.STATE_IDLE,
116 _currentReadOnly:
false,
117 _currentImageMgmtType: Ci.sbIDeviceLibraryMediaSyncSettings.SYNC_MGMT_NONE,
130 bindDevice:
function deviceControlWidget_bindDevice() {
133 let devLibGUID = this._widget.getAttribute(
"dev-lib-guid");
139 if (devLibGUID == this._widget.devLibGUID)
143 let libMgr = Cc[
"@songbirdnest.com/Songbird/library/Manager;1"]
144 .getService(Ci.sbILibraryManager);
145 devLib = libMgr.getLibrary(devLibGUID);
149 device = devLib.device;
151 deviceID = device.id;
156 deviceID = this._widget.getAttribute(
"device-id");
157 if (!deviceID || (deviceID == this._widget.deviceID))
161 let deviceManager = Cc[
"@songbirdnest.com/Songbird/DeviceManager;2"]
162 .getService(Ci.sbIDeviceManager2);
163 device = deviceManager.getDevice(Components.ID(deviceID));
167 devLib = device.defaultLibrary;
169 devLibGUID = devLib.guid;
173 let deviceBound = device != this._widget.device;
174 let devLibBound = devLib != this._widget.devLib;
177 this._widget.device = device;
178 this._widget.deviceID = deviceID;
179 this._widget.devLib = devLib;
180 this._widget.devLibGUID = devLibGUID;
188 let
event = document.createEvent(
"Events");
189 event.initEvent(
"deviceBound",
false,
false);
190 this._widget.dispatchEvent(
event);
196 let
event = document.createEvent(
"Events");
197 event.initEvent(
"devLibBound",
false,
false);
198 this._widget.dispatchEvent(
event);
207 initialize:
function deviceControlWidget_initialize() {
212 this._device = this._widget.device;
213 this._deviceLibrary = this._widget.devLib;
216 this._getBoundElem();
222 func =
function(aEvent) { _this.onAction(aEvent); };
223 this._domEventListenerSet.add(this._boundElem,
"command", func,
false);
226 if (this._widget.getAttribute(
"listentodevice") ==
"true") {
227 var deviceEventTarget = this._device.QueryInterface
228 (Ci.sbIDeviceEventTarget);
229 deviceEventTarget.addEventListener(
this);
230 this._deviceListenerAdded =
true;
234 if (this._deviceLibrary) {
236 var
prefs = Cc[
"@mozilla.org/preferences-service;1"]
237 .getService(Ci.nsIPrefBranch);
239 var syncPlaylistPrefKey =
"songbird.device." + this._widget.deviceID +
240 ".preferences.library." + this._deviceLibrary.guid +
243 var syncPlaylistPrefs =
null;
244 if (prefs.prefHasUserValue(syncPlaylistPrefKey))
245 syncPlaylistPrefs = prefs.getCharPref(syncPlaylistPrefKey);
248 if (syncPlaylistPrefs) {
250 var guidList = syncPlaylistPrefs.split(
",");
251 var syncAudioPlaylists =
"";
252 var syncVideoPlaylists =
"";
254 for (var
i = 0;
i < guidList.length; ++
i) {
256 mediaList =
LibraryUtils.mainLibrary.getMediaItem(guidList[
i])
257 var contentType = mediaList.getListContentType();
258 if (contentType & Ci.sbIMediaList.CONTENTTYPE_AUDIO)
259 syncAudioPlaylists += guidList[
i] +
",";
260 if (contentType & Ci.sbIMediaList.CONTENTTYPE_VIDEO)
261 syncVideoPlaylists += guidList[
i] +
",";
267 prefs.setCharPref(syncPlaylistPrefKey +
".audio", syncAudioPlaylists);
268 prefs.setCharPref(syncPlaylistPrefKey +
".video", syncVideoPlaylists);
271 prefs.setCharPref(syncPlaylistPrefKey,
"");
285 finalize:
function deviceControlWidget_finalize(aReset) {
287 if (this._domEventListenerSet) {
288 this._domEventListenerSet.removeAll();
290 this._domEventListenerSet =
null;
293 if (this._deviceListenerAdded) {
294 var deviceEventTarget = this._device.QueryInterface
295 (Ci.sbIDeviceEventTarget);
296 deviceEventTarget.removeEventListener(
this);
298 this._deviceListenerAdded =
false;
302 this._deviceLibrary =
null;
303 this._boundElem =
null;
321 onAction:
function deviceControlWidget_onAction(aEvent) {
327 switch (aEvent.target.getAttribute(
"action")) {
329 this._device.cancelRequests();
333 this._device.eject();
337 this._device.format();
341 this._ignoreDevice();
345 this._getDeviceInfo();
348 case "new_playlist" :
349 this._createPlaylist();
353 this._renameDevice();
358 if (this._deviceLibrary) {
359 this._deviceLibrary.sync();
362 this._device.syncLibraries();
383 _createPlaylist:
function deviceControlWidget__createPlaylist() {
384 var libSPS = Cc[
"@songbirdnest.com/servicepane/library;1"]
385 .getService(Ci.sbILibraryServicePaneService);
388 var mediaList = this._deviceLibrary.createMediaList(
"simple");
389 mediaList.name = libSPS.suggestNameForNewPlaylist(this._deviceLibrary);
393 var libDSPS = Cc[
"@songbirdnest.com/servicepane/device;1"]
394 .getService(Ci.sbIDeviceServicePaneService);
395 var
node = libSPS.getNodeForLibraryResource(mediaList);
396 var deviceNode = libDSPS.getNodeForDevice(this._device);
397 deviceNode.appendChild(node);
406 _renameDevice:
function deviceControlWidget__renameDevice() {
408 var servicePaneNode = this._getServicePaneNode();
412 if (servicePaneNode && (typeof(
gServicePane) !=
"undefined")) {
416 var friendlyName =
null;
417 try { friendlyName = this._device.properties.friendlyName; }
catch(ex) {}
420 var vendorModelName =
"";
421 var vendorName =
null;
422 var modelNumber =
null;
423 try { vendorName = this._device.properties.vendorName; }
catch(ex) {}
424 try { modelNumber = this._device.properties.modelNumber; }
catch(ex) {}
426 modelNumber =
SBString(
"device.default.model.number");
428 vendorModelName += vendorName +
" ";
429 vendorModelName += modelNumber;
432 var
promptService = Cc[
"@mozilla.org/embedcomp/prompt-service;1"]
433 .getService(Ci.nsIPromptService);
434 var newName = {
value: friendlyName };
435 var ok = promptService.prompt
437 SBString(
"device.dialog.rename.title"),
447 this._device.properties.friendlyName = newName.value;
449 Cu.reportError(
"Cannot write device name.\n");
460 _ignoreDevice:
function deviceControlWidget__ignoreDevice() {
461 var
promptService = Cc[
"@mozilla.org/embedcomp/prompt-service;1"]
462 .getService(Ci.nsIPromptService);
465 var buttonPressed = promptService.confirmEx(
window,
466 SBString(
'device.dialog.setup.ignoreDeviceTitle'),
467 SBString(
'device.dialog.setup.ignoreDevice'),
468 promptService.STD_YES_NO_BUTTONS,
472 acceptedBlacklist = (buttonPressed == 0);
473 if (acceptedBlacklist) {
475 .getValue(
"songbird.device.ignorelist",
"");
477 if (ignoreList !=
"") {
480 ignoreList += this._device.id;
481 Application.prefs.setValue(
"songbird.device.ignorelist", ignoreList);
484 var manager = Cc[
"@songbirdnest.com/Songbird/DeviceManager;2"]
485 .getService(Ci.sbIDeviceManager2);
486 var controller = manager.getController(this._device.controllerId);
487 controller.releaseDevice(this._device);
489 return acceptedBlacklist;
496 _getDeviceInfo:
function deviceControlWidget__getDeviceInfo() {
498 WindowUtils.openModalDialog
500 "chrome://songbird/content/xul/device/deviceInfoDialog.xul",
502 "chrome,centerscreen",
520 onDeviceEvent:
function DeviceSyncWidget_onDeviceEvent(aEvent) {
524 case Ci.sbIDeviceEvent.EVENT_DEVICE_PREFS_CHANGED :
528 case Ci.sbIDeviceEvent.EVENT_DEVICE_STATE_CHANGED :
550 _getProductName:
function deviceControlWidget__getProductName() {
552 var productName =
null;
553 try { productName = this._device.productName; }
catch(err) {}
554 if (productName ==
null)
555 productName =
SBString(
"device.info.unknown");
567 _getDeviceModelCap:
function deviceControlWidget__getDeviceModelCap() {
569 var productName =
null;
570 try { productName = this._device.productName; }
catch(err) {}
571 if (productName ==
null)
572 productName =
SBString(
"device.info.unknown");
576 if (this._deviceLibrary) {
579 Cc[
"@songbirdnest.com/Songbird/Properties/UnitConverter/Storage;1"]
580 .createInstance(Ci.sbIPropertyUnitConverter);
581 capacity = this._deviceLibrary.getProperty
582 (
"http://songbirdnest.com/device/1.0#capacity");
583 capacity = storageConverter.autoFormat(capacity, -1, 1);
588 [ productName, capacity ]);
598 _isReadOnly:
function deviceControlWidget__isReadOnly() {
602 var accessCompatibility;
603 var deviceProperties = this._device.properties.properties;
605 accessCompatibility =
606 deviceProperties.getPropertyAsAString(
"http://songbirdnest.com/" +
607 "device/1.0#accessCompatibility");
609 if (accessCompatibility ==
"ro")
613 if (!this._deviceLibrary)
617 if (this._device.state == Ci.sbIDevice.STATE_FORMATTING)
624 if (this._deviceLibrary.getProperty(SBProperties.isReadOnly) ==
"1")
651 _update:
function deviceControlWidget__update(aForce) {
653 var state = this._device.state;
654 var readOnly = this._isReadOnly();
655 var supportsReformat = this._device.supportsReformat;
656 var supportsPlaylist = this._supportsPlaylist();
657 var msc = (this._device.parameters.getProperty(
"DeviceType") ==
"MSCUSB");
659 var imagesMgmtType = Ci.sbIDeviceLibraryMediaSyncSettings.SYNC_MGMT_NONE;
660 if (this._deviceLibrary) {
661 let currSettings = this._deviceLibrary.syncSettings;
663 currSettings.getMediaSettings(this._deviceLibrary.MEDIATYPE_IMAGE)
669 (this._currentImageMgmtType == imagesMgmtType) &&
670 (this._currentState == state) &&
671 (this._currentReadOnly == readOnly) &&
672 (this._currentSupportsReformat == supportsReformat) &&
673 (this._currentSupportsPlaylist == supportsPlaylist) &&
674 (this._currentMsc == msc)) {
679 this._currentImageMgmtType = imagesMgmtType;
680 this._currentState = state;
681 this._currentReadOnly = readOnly;
682 this._currentSupportsReformat = supportsReformat;
683 this._currentSupportsPlaylist = supportsPlaylist;
684 this._currentMsc = msc;
687 var updateAttributeList = [];
688 this._updateAttribute(
"accesskey", updateAttributeList);
689 this._updateAttribute(
"action", updateAttributeList);
690 this._updateAttribute(
"disabled", updateAttributeList);
691 this._updateAttribute(
"label", updateAttributeList);
692 this._updateAttribute(
"hidden", updateAttributeList);
693 this._updateAttribute(
"checked", updateAttributeList);
696 if (this._boundElem != this._widget) {
697 DOMUtils.copyAttributes(this._widget,
709 _supportsPlaylist:
function deviceControlWidget__supportsPlaylist() {
710 var capabilities = this._device.capabilities;
711 var sbIDC = Ci.sbIDeviceCapabilities;
713 if (capabilities.supportsContent(sbIDC.FUNCTION_DEVICE,
714 sbIDC.CONTENT_PLAYLIST) ||
715 capabilities.supportsContent(sbIDC.FUNCTION_AUDIO_PLAYBACK,
716 sbIDC.CONTENT_PLAYLIST)) {
735 _updateAttribute:
function deviceControlWidget__updateAttribute(aAttrName,
739 if (this._currentReadOnly &&
740 this._getStateAttribute(attrVal, aAttrName,
"readonly")) {}
741 else if (!this._currentReadOnly &&
742 this._getStateAttribute(attrVal, aAttrName,
"readwrite")) {}
743 else if ((this._currentState == Ci.sbIDevice.STATE_CANCEL) &&
744 this._getStateAttribute(attrVal, aAttrName,
"cancel")) {}
745 else if ((this._currentState == Ci.sbIDevice.STATE_COPYING) &&
746 this._getStateAttribute(attrVal, aAttrName,
"copy")) {}
747 else if ((this._currentState == Ci.sbIDevice.STATE_DELETING) &&
748 this._getStateAttribute(attrVal, aAttrName,
"delete")) {}
749 else if ((this._currentState == Ci.sbIDevice.STATE_MOUNTING) &&
750 this._getStateAttribute(attrVal, aAttrName,
"mount")) {}
751 else if ((this._currentState == Ci.sbIDevice.STATE_SYNCING) &&
752 this._getStateAttribute(attrVal, aAttrName,
"sync")) {}
753 else if ((this._currentState == Ci.sbIDevice.STATE_UPDATING) &&
754 this._getStateAttribute(attrVal, aAttrName,
"update")) {}
755 else if ((this._currentState == Ci.sbIDevice.STATE_TRANSCODE) &&
756 this._getStateAttribute(attrVal, aAttrName,
"transcode")) {}
757 else if ((this._currentState == Ci.sbIDevice.STATE_FORMATTING) &&
758 this._getStateAttribute(attrVal, aAttrName,
"format")) {}
759 else if ((this._currentState != Ci.sbIDevice.STATE_IDLE) &&
760 this._getStateAttribute(attrVal, aAttrName,
"busy")) {}
761 else if ((this._currentState == Ci.sbIDevice.STATE_IDLE) &&
762 this._getStateAttribute(attrVal, aAttrName,
"idle")) {}
763 else if (this._deviceLibrary && this._canTriggerSync() &&
764 this._getStateAttribute(attrVal, aAttrName,
"can_trigger_sync")) {}
765 else if (this._currentSupportsReformat &&
766 this._getStateAttribute(attrVal,
768 "supports_reformat")) {}
769 else if (this._currentMsc &&
770 this._getStateAttribute(attrVal, aAttrName,
"msc")) {}
771 else if (this._currentSupportsPlaylist &&
772 this._getStateAttribute(attrVal,
774 "supports_playlist")) {}
775 else if (!this._currentSupportsPlaylist &&
776 this._getStateAttribute(attrVal,
778 "not_supports_playlist")) {}
779 else if (this._getStateAttribute(attrVal, aAttrName,
"default")) {}
780 else this._getStateAttribute(attrVal, aAttrName,
null);
783 if ((typeof(attrVal.value) ==
"undefined") || (attrVal.value ==
null))
785 attrVal = attrVal.value;
789 var removeAttr =
false;
790 if (attrVal ==
"remove_attribute")
795 if (attrVal.match(/product_name/)) {
796 var productName = this._getProductName();
797 attrVal = attrVal.replace(/product_name/g, productName);
802 if (attrVal.match(/device_model_cap/)) {
803 var deviceModelCap = this._getDeviceModelCap();
804 attrVal = attrVal.replace(/device_model_cap/g, deviceModelCap);
809 this._widget.setAttribute(aAttrName, attrVal);
811 this._widget.removeAttribute(aAttrName);
812 aAttrList.push(aAttrName);
831 _getStateAttribute:
function deviceControlWidget__getStateAttribute
836 var stateAttrName = aAttrName;
838 stateAttrName +=
"_" + aAttrState;
841 if (!this._widget.hasAttribute(stateAttrName))
845 aAttrVal.value = this._widget.getAttribute(stateAttrName);
855 _getBoundElem:
function deviceControlWidget__getBoundElem() {
859 boundElem = this._getElementFromSpec(this._widget,
"bindforward");
860 boundElem = this._getElementFromSpec(boundElem,
"bindelem");
863 this._boundElem = boundElem;
864 this._widget.boundElem = boundElem;
878 _getElementFromSpec:
function deviceControlWidget__getElement(aElementStart,
882 var elementSpec = this._widget.getAttribute(aElementSpec).split(
"=");
883 switch (elementSpec[0]) {
885 element = aElementStart.previousSibling;
886 while (element && (element.nodeType != Node.ELEMENT_NODE)) {
887 element = element.previousSibling;
892 element = aElementStart.nextSibling;
893 while (element && (element.nodeType != Node.ELEMENT_NODE)) {
894 element = element.nextSibling;
899 element = aElementStart.parentNode;
903 element = aElementStart;
917 _getServicePaneNode:
function deviceControlWidget__getServicePaneNode() {
919 var servicePaneNodeID = this._widget.getAttribute(
"service_pane_node_id");
920 if (!servicePaneNodeID)
924 var servicePaneService = Cc[
"@songbirdnest.com/servicepane/service;1"]
925 .getService(Ci.sbIServicePaneService);
926 servicePaneNode = servicePaneService.getNode(servicePaneNodeID);
928 return servicePaneNode;
931 _canTriggerSync:
function deviceControlWidget__canTriggerSync() {
933 if (!this._deviceLibrary)
937 let syncSettings = this._deviceLibrary.syncSettings;
943 let mediaSettings = syncSettings.getMediaSettings(
944 this._deviceLibrary.MEDIATYPE_IMAGE);
949 return (mediaSettings.mgmtType !=
950 Ci.sbIDeviceLibraryMediaSyncSettings.SYNC_MGMT_NONE);
function SBFormattedString(aKey, aParams, aDefault, aStringBundle)
function DOMEventListenerSet()
function SBString(aKey, aDefault, aStringBundle)
function deviceControlWidget(aWidget)
Javascript wrappers for common library tasks.
_getSelectedPageStyle s i