47 if (typeof(
Cc) ==
"undefined")
48 var
Cc = Components.classes;
49 if (typeof(
Ci) ==
"undefined")
50 var
Ci = Components.interfaces;
51 if (typeof(
Cr) ==
"undefined")
52 var
Cr = Components.results;
53 if (typeof(
Cu) ==
"undefined")
54 var
Cu = Components.utils;
57 Cu.import(
"resource://app/jsmodules/DOMUtils.jsm");
58 Cu.import(
"resource://app/jsmodules/SBTimer.jsm");
68 if (typeof(
XUL_NS) ==
"undefined")
69 var
XUL_NS =
"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
87 _pollPeriodTable: {
"battery": 60000,
"firmware_version": 60000 },
89 "chrome://songbird/content/xul/device/deviceContextMenu.xul",
105 _deviceProperties:
null,
106 _deviceLibrary:
null,
107 _contextMenuEnabled:
false,
108 _contextMenuPopup:
null,
118 initialize:
function DIW__initialize(aWidget) {
120 this._widget = aWidget;
123 this._device = this._widget.device;
124 this._deviceProperties = this._device.properties;
125 this._deviceLibrary = this._widget.devLib;
128 this._initializeContextMenu();
131 this._showElements();
137 this._deviceSpecInitialize();
143 this._device.QueryInterface(Ci.sbIDeviceEventTarget).addEventListener(
this);
146 onDeviceEvent :
function DIW_onDeviceEvent(anEvent) {
147 if (anEvent.type == Ci.sbIDeviceEvent.EVENT_DEVICE_INFO_CHANGED) {
155 finalize:
function DIW_finalize() {
157 this._pollingFinalize();
160 if (this._device !=
null) {
161 this._device.QueryInterface(Ci.sbIDeviceEventTarget)
162 .removeEventListener(
this);
166 this._deviceFinalize();
170 this._deviceLibrary =
null;
171 this._contextMenuPopup =
null;
190 _deviceSpecInitialize:
function DIW__deviceSpecInitialize() {
192 var deviceSpecs = this._widget.getAttribute(
"devicespecs");
193 var deviceSpecList = deviceSpecs.split(
",");
196 var deviceSpecRows = this._getElement(
"device_spec_rows");
197 var deviceSpecRowList = deviceSpecRows.getElementsByTagNameNS(XUL_NS,
201 for (var
i = 0;
i < deviceSpecRowList.length;
i++) {
202 deviceSpecRowList[
i].hidden =
true;
207 var pollPeriod = Number.POSITIVE_INFINITY;
208 var refDeviceSpecRow = deviceSpecRows.firstChild;
209 for (var
i = 0;
i < deviceSpecList.length;
i++) {
211 var deviceSpec = deviceSpecList[
i];
212 var deviceSpecRow = this._getDeviceSpecRow(deviceSpec);
215 if (!this._supportsDeviceSpec(deviceSpec))
219 if (deviceSpecRow != refDeviceSpecRow) {
220 deviceSpecRows.insertBefore(deviceSpecRow, refDeviceSpecRow);
222 refDeviceSpecRow = deviceSpecRow.nextSibling;
225 deviceSpecRow.hidden =
false;
228 var specPollPeriod = this._pollPeriodTable[deviceSpec];
229 if (specPollPeriod && (specPollPeriod < pollPeriod))
230 pollPeriod = specPollPeriod;
234 if (pollPeriod != Number.POSITIVE_INFINITY)
235 this._pollingInitialize(pollPeriod);
243 _deviceSpecUpdateAll:
function DIW__deviceSpecUpdateAll() {
245 var deviceSpecRows = this._getElement(
"device_spec_rows");
246 var deviceSpecRowList = deviceSpecRows.getElementsByTagNameNS
247 (
"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
249 for (var
i = 0;
i < deviceSpecRowList.length;
i++) {
250 var deviceSpecRow = deviceSpecRowList[
i];
251 if (!deviceSpecRow.hidden)
252 this._deviceSpecUpdate(deviceSpecRow);
263 _deviceSpecUpdate:
function DIW__deviceSpecUpdate(aRow) {
265 var deviceSpec = aRow.getAttribute(
"devicespec");
266 switch (deviceSpec) {
268 this._deviceSpecUpdateValue(aRow,
270 this._getDeviceModel());
274 this._deviceSpecUpdateValue(aRow,
275 "capacity_value_label",
276 this._getDeviceModelSize());
279 case "model_capacity" :
280 this._deviceSpecUpdateModelCapacity(aRow);
283 case "product_capacity" :
284 this._deviceSpecUpdateProductCapacity(aRow);
287 case "friendly_name" :
288 this._deviceSpecUpdateValue(aRow,
289 "friendly_name_value_label",
290 this._getDeviceFriendlyName());
293 case "serial_number" :
294 this._deviceSpecUpdateValue(aRow,
295 "serial_number_value_label",
296 this._getDeviceSerialNumber());
300 this._deviceSpecUpdateValue(aRow,
301 "vendor_value_label",
302 this._getDeviceVendor());
306 this._deviceSpecUpdateValue(aRow,
307 "access_value_label",
308 this._getDeviceAccessCompatibilityDesc());
311 case "playback_formats" :
312 this._deviceSpecUpdateDesc(aRow,
313 "playback_formats_value_label",
314 this._getDevicePlaybackFormats());
318 this._deviceSpecUpdateBattery(aRow);
321 case "firmware_version":
322 this._deviceSpecUpdateValue(aRow,
323 "firmware_version_value_label",
324 this._getDeviceFirmwareVersion());
343 _deviceSpecUpdateValue:
function DIW__deviceSpecUpdateValue(aRow,
347 if (
aValue && aRow.hasAttribute(
"novalue"))
348 aRow.removeAttribute(
"novalue");
349 else if (!
aValue && (aRow.getAttribute(
"novalue") !=
"true"))
350 aRow.setAttribute(
"novalue",
true);
353 var labelElem = this._getElement(aLabelID);
354 if (labelElem.getAttribute(
"value") !=
aValue)
355 labelElem.setAttribute(
"value",
aValue);
370 _deviceSpecUpdateDesc:
function DIW__deviceSpecUpdateDesc(aRow,
374 if (aText && aRow.hasAttribute(
"novalue"))
375 aRow.removeAttribute(
"novalue");
376 else if (!aText && (aRow.getAttribute(
"novalue") !=
"true"))
377 aRow.setAttribute(
"novalue",
true);
380 var descElem = this._getElement(aDescID);
381 if (descElem.firstChild.data != aText)
382 descElem.firstChild.data = aText;
392 _deviceSpecUpdateModelCapacity:
function
393 DIW__deviceSpecUpdateModelCapacity(aRow) {
395 var devModelValue = this._getDeviceModel();
396 var devCapValue = this._getDeviceModelSize();
397 var devModelCapValue = devModelValue +
" (" + devCapValue +
")";
400 this._deviceSpecUpdateValue(aRow,
401 "model_capacity_value_label",
412 _deviceSpecUpdateProductCapacity:
function
413 DIW__deviceSpecUpdateProductCapacity(aRow) {
415 var devProductValue = this._getDeviceProduct();
419 if (this._deviceLibrary) {
422 Cc[
"@songbirdnest.com/Songbird/Properties/UnitConverter/Storage;1"]
423 .createInstance(Ci.sbIPropertyUnitConverter);
424 capacity = this._deviceLibrary.getProperty
425 (
"http://songbirdnest.com/device/1.0#capacity");
426 capacity = storageConverter.autoFormat(capacity, -1, 1);
431 [ devProductValue, capacity ]);
434 this._deviceSpecUpdateValue(aRow,
435 "product_capacity_value_label",
446 _deviceSpecUpdateBattery:
function DIW__deviceSpecUpdateBattery(aRow) {
448 var batteryLevel = {};
449 var onBatteryPower = {};
450 this._getDeviceBatteryStatus(batteryLevel, onBatteryPower);
451 batteryLevel = batteryLevel.value;
452 onBatteryPower = onBatteryPower.value;
455 batteryStatus =
"battery-powered";
456 else if (batteryLevel == 100)
457 batteryStatus =
"charged";
459 batteryStatus =
"charging";
462 var batteryElem = this._getElement(
"battery_status");
463 if (batteryLevel == -1) {
464 batteryElem.hidden =
true;
467 batteryElem.hidden =
false;
468 if ((batteryElem.getAttribute(
"status") != batteryStatus) ||
469 (batteryElem.getAttribute(
"level") != batteryLevel)) {
470 batteryElem.setAttribute(
"status", batteryStatus);
471 batteryElem.setAttribute(
"level", batteryLevel);
487 _initializeContextMenu:
function DIW__initializeContextMenu() {
489 if (this._widget.getAttribute(
"enable_context_menu") ==
"true") {
490 this._contextMenuEnabled =
true;
492 this._contextMenuEnabled =
false;
497 this._contextMenuPopup = this._getElement(
"context_menu_popup");
498 while (this._contextMenuPopup.hasChildNodes()) {
499 this._contextMenuPopup.removeChild(this._contextMenuPopup.firstChild);
503 var deviceContextMenuDoc = DOMUtils.loadDocument(this._contextMenuDocURL);
504 DOMUtils.importChildElements(this._contextMenuPopup,
505 deviceContextMenuDoc,
506 "device_context_menu_items",
507 {
"device-id": this._device.id });
517 onContextMenu:
function DIW_onContextMenu(aEvent) {
519 if (!this._contextMenuEnabled)
523 this._contextMenuPopup.openPopup(
null,
547 _getElement:
function DIW__getElement(aElementID) {
548 return document.getAnonymousElementByAttribute(this._widget,
563 _getDeviceSpecRow:
function DIW__getDeviceSpecRow(aDeviceSpec) {
564 return document.getAnonymousElementByAttribute(this._widget,
578 _getShowElement:
function DIW__getShowElement(aShowID) {
579 return document.getAnonymousElementByAttribute(this._widget,
607 _pollingInitialize:
function DIW__pollingInitialize(aPollPeriod) {
610 var func =
function() { _this._update(); }
611 this._pollingTimer =
new SBTimer(func,
613 Ci.nsITimer.TYPE_REPEATING_SLACK);
621 _pollingFinalize:
function DIW__pollingFinalize() {
622 if (this._pollingTimer) {
623 this._pollingTimer.cancel();
624 this._pollingTimer =
null;
639 _hideLabels:
function DIW__hideLabels() {
640 if (this._widget.getAttribute(
"hidelabels")) {
641 this._getElement(
"device_specs_label_column").hidden =
true;
644 var gridRows = this._getElement(
"device_spec_rows");
648 var rowElements = gridRows.getElementsByTagNameNS(XUL_NS,
"row");
649 for each (var row
in rowElements) {
651 row.firstChild.hidden =
true;
660 _showElements:
function DIW__showElements() {
662 var showListAttr = this._widget.getAttribute(
"showlist");
665 var showList = this._widget.getAttribute(
"showlist").split(
",");
668 for (var
i = 0;
i < showList.length;
i++) {
669 var
element = this._getShowElement(showList[
i]);
670 element.hidden =
false;
671 if (element.parentNode.hidden)
672 element.parentNode.hidden =
false;
682 _update:
function DIW__update() {
684 var devNameLabel = this._getElement(
"device_name_label");
685 var devName = this._getDeviceName();
686 if (devNameLabel.value != devName)
687 devNameLabel.value = devName;
690 var devImage = this._getElement(
"device_image");
691 var devIconURL = this._getDeviceIcon();
692 if (devIconURL && (devImage.src != devIconURL))
693 devImage.src = devIconURL;
697 if (!devImage.hidden) {
698 var lockImage = this._getElement(
"device_image_ro");
699 var accessCompatibility = this._getDeviceAccessCompatibility();
700 if (accessCompatibility ==
"ro")
701 lockImage.hidden =
false;
705 this._deviceSpecUpdateAll();
727 _lastPropertyValue : {},
733 _deviceFinalize:
function DIW__deviceFinalize() {
749 _getDeviceProperty:
function DIW__getDeviceProperty(aPropertyName, aDefault) {
751 if (this._deviceProperties.properties.hasKey(aPropertyName)) {
752 let
value = this._deviceProperties.properties.getPropertyAsAString(
754 this._lastPropertyValue[aPropertyName] =
value;
758 if (this._lastPropertyValue[aPropertyName]) {
759 return this._lastPropertyValue[aPropertyName];
771 _getDeviceModel:
function DIW__getDeviceModel() {
772 var modelNumber =
null;
774 modelNumber = this._deviceProperties.modelNumber;
775 this._lastPropertyValue.modelNumber = modelNumber;
778 if (this._lastPropertyValue.modelNumber) {
779 modelNumber = this._lastPropertyValues.modelNumber;
782 if (modelNumber ==
null)
783 modelNumber =
SBString(
"device.info.unknown");
794 _getDeviceProduct:
function DIW__getDeviceProduct() {
795 var productName =
null;
797 productName = this._device.productName;
798 this._lastPropertyValue.productName = productName;
800 if (this._lastPropertyValue.productName) {
801 productName = this._lastPropertyValue.productName;
804 if (productName ==
null)
805 productName =
SBString(
"device.info.unknown");
816 _getDeviceModelSize:
function DIW__getDeviceModelSize() {
817 if (this._deviceLibrary) {
820 Cc[
"@songbirdnest.com/Songbird/Properties/UnitConverter/Storage;1"]
821 .createInstance(Ci.sbIPropertyUnitConverter);
822 var modelSize = this._deviceLibrary.getProperty
823 (
"http://songbirdnest.com/device/1.0#capacity");
824 return storageConverter.autoFormat(modelSize, -1, 1);
828 return SBString(
"device.info.unknown");
838 _getDeviceName:
function DIW__getDeviceName() {
840 try { name = this._device.name; }
catch(err) {}
842 name =
SBString(
"device.info.unknown");
854 _getDeviceFriendlyName:
function DIW__getDeviceFriendlyName() {
855 var friendlyName =
null;
857 friendlyName = this._deviceProperties.friendlyName;
858 this._lastPropertyValue.friendlyName = friendlyNname;
860 if (this._lastPropertyValue.friendlyName) {
861 friendlyName = this._lastPropertyValue.friendlyName;
864 if (friendlyName ==
null)
865 friendlyName =
SBString(
"device.info.unknown");
877 _getDeviceSerialNumber:
function DIW__getDeviceSerialNumber() {
878 var serialNumber =
null;
880 serialNumber = this._deviceProperties.serialNumber;
881 this._lastPropertyValue.serialNumber = serialNumber;
883 if (this._lastPropertyValue.serialNumber) {
884 serialNumber = this._lastPropertyValue.serialNumber;
887 if (serialNumber ==
null)
888 serialNumber =
SBString(
"device.info.unknown");
900 _getDeviceVendor:
function DIW__getDeviceVendor() {
901 var vendorName =
null;
903 vendorName = this._deviceProperties.vendorName;
904 this._lastPropertyValue.vendorName = this._lastPropertyValue.vendorName;
906 if (this._lastPropertyValue.vendorName) {
907 vendorName = this._lastPropertyValue.vendorName;
910 if (vendorName ==
null)
911 vendorName =
SBString(
"device.info.unknown");
916 _getDeviceFirmwareVersion:
function DIW__getDeviceFirmwareVersion() {
917 var firmwareVersion =
null;
919 var deviceFirmwareUpdater =
920 Cc[
"@songbirdnest.com/Songbird/Device/Firmware/Updater;1"]
921 .getService(Ci.sbIDeviceFirmwareUpdater);
927 if(deviceFirmwareUpdater.hasHandler(
this._device, 0, 0)) {
928 var
handler = deviceFirmwareUpdater.getHandler(this._device, 0, 0);
929 handler.bind(this._device,
null);
932 firmwareVersion = handler.currentFirmwareReadableVersion;
938 if (firmwareVersion ==
null) {
939 firmwareVersion =
SBString(
"device.info.unknown");
943 firmwareVersion =
"";
946 return firmwareVersion;
955 _getDeviceAccessCompatibility:
function DIW__getDeviceAccessCompatibility() {
956 var accessCompatibility =
957 this._getDeviceProperty
958 (
"http://songbirdnest.com/device/1.0#accessCompatibility",
960 return accessCompatibility;
969 _getDeviceAccessCompatibilityDesc:
function DIW__getDeviceAccessCompatibilityDesc() {
970 var accessCompatibility = this._getDeviceAccessCompatibility();
971 if (accessCompatibility ==
"ro")
972 accessCompatibility =
SBString(
"device.info.read_only");
973 else if (accessCompatibility ==
"rw")
974 accessCompatibility =
SBString(
"device.info.read_write");
976 return accessCompatibility;
985 _getExtsForMimeType :
function(aMimeType, aContentType) {
991 Cc[
"@songbirdnest.com/Songbird/Device/DeviceCapabilitiesUtils;1"]
992 .getService(Ci.sbIDeviceCapabilitiesUtils);
993 var extEnum = devCapsUtils.mapContentTypeToFileExtensions(aMimeType,
995 while (extEnum.hasMore()) {
996 exts[idx++] = extEnum.getNext();
1003 var mimeService = Cc[
"@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
1007 mimeExtension = mimeService.getFromTypeAndExtension(aMimeType,
null)
1011 if (mimeExtension) {
1012 exts[idx] =
SBString(
"device.info.mimetype." + mimeExtension, mimeExtension);
1018 exts[idx] = aMimeType;
1028 _getDevicePlaybackFormats:
function DIW__getDevicePlaybackFormats() {
1029 var retFormats = [];
1030 var extensions = [];
1032 var deviceCapabilities = this._device.capabilities;
1033 var functionArray = deviceCapabilities.getSupportedFunctionTypes({});
1034 for (var functionCounter = 0;
1035 functionCounter < functionArray.length;
1038 var contentArray = deviceCapabilities.getSupportedContentTypes(
1039 functionArray[functionCounter], {});
1040 for (var contentCounter = 0;
1041 contentCounter < contentArray.length;
1044 var mimeTypeArray = deviceCapabilities.getSupportedMimeTypes(
1045 contentArray[contentCounter], {});
1046 if (mimeTypeArray.length > 0) {
1047 for each (var mimetype
in mimeTypeArray) {
1048 var exts = this._getExtsForMimeType(mimetype,
1049 contentArray[contentCounter]);
1050 for (var idx = 0; idx < exts.length; idx++) {
1051 let ext = exts[idx];
1052 if (extensions.indexOf(ext) == -1)
1053 extensions.push(ext);
1063 return extensions.join(
", ") ||
SBString(
"device.info.unknown");
1075 _getDeviceBatteryStatus:
function DIW__getDeviceBatteryStatus
1078 aBatteryLevel.value = this._getDeviceProperty
1079 (
"http://songbirdnest.com/device/1.0#batteryLevel",
1081 var powerSource = this._getDeviceProperty
1082 (
"http://songbirdnest.com/device/1.0#powerSource", 0);
1083 aOnBatteryPower.value = (parseInt(powerSource) ?
true :
false);
1093 _supportsDeviceSpec:
function DIW__supportsDeviceSpec(aDeviceSpec) {
1095 switch (aDeviceSpec) {
1098 case "model_capacity" :
1099 case "product_capacity" :
1100 case "friendly_name" :
1101 case "serial_number" :
1104 case "playback_formats" :
1106 case "firmware_version" :
1123 _getDeviceIcon:
function DIW__getDeviceIcon() {
1125 var
uri = this._deviceProperties.iconUri;
1126 this._lastPropertyValue.uri =
uri;
1127 var spec = uri.spec;
1128 if (uri.schemeIs(
"file") && /\.ico$/(spec)) {
1130 spec =
"moz-icon://" + spec +
"?size=64";
1134 if (this._lastPropertyValue.uri) {
1135 return this._lastPropertyValue.uri;
function SBFormattedString(aKey, aParams, aDefault, aStringBundle)
function SBString(aKey, aDefault, aStringBundle)
_getSelectedPageStyle s i