27 if (typeof(
Ci) ==
"undefined")
28 var
Ci = Components.interfaces;
29 if (typeof(
Cc) ==
"undefined")
30 var
Cc = Components.classes;
31 if (typeof(
Cr) ==
"undefined")
32 var
Cr = Components.results;
33 if (typeof(
Cu) ==
"undefined")
34 var
Cu = Components.utils;
36 Cu.import(
"resource://app/jsmodules/ArrayConverter.jsm");
37 Cu.import(
"resource://app/jsmodules/sbCoverHelper.jsm");
38 Cu.import(
"resource://app/jsmodules/SBJobUtils.jsm");
39 Cu.import(
"resource://app/jsmodules/sbLibraryUtils.jsm");
40 Cu.import(
"resource://app/jsmodules/sbProperties.jsm");
41 Cu.import(
"resource://app/jsmodules/StringUtils.jsm");
42 Cu.import(
"resource://gre/modules/XPCOMUtils.jsm");
56 TrackEditorInputWidget.call(
this,
element);
58 this._replaceLabel =
SBString(
"trackeditor.artwork.replace");
59 this._addLabel =
SBString(
"trackeditor.artwork.add");
61 this._createDragOverlay();
62 this._createContextMenu();
65 this._elementStack.addEventListener(
"click",
66 function(evt) {
self.onClick(evt); },
false);
67 this._elementStack.addEventListener(
"keypress",
68 function(evt) {
self.onKeyPress(evt); },
false);
69 this._elementStack.addEventListener(
"contextmenu",
70 function(evt) {
self.onContextMenu(evt); },
false);
74 this._elementStack.addEventListener(
"dragover",
75 function(evt) { nsDragAndDrop.dragOver(evt,
self); },
false);
76 this._elementStack.addEventListener(
"dragdrop",
77 function(evt) { nsDragAndDrop.drop(evt,
self); },
false);
78 this._elementStack.addEventListener(
"draggesture",
79 function(evt) { nsDragAndDrop.startDrag(evt,
self); },
false);
82 __proto__: TrackEditorInputWidget.prototype,
99 _imageSrcChange:
function TrackEditorArtwork__imageSrcChange(newValue) {
100 var oldValue =
TrackEditor.state.getPropertyValue(this.property);
102 if (newValue != oldValue) {
105 TrackEditor.state.setPropertyValue(this.property, newValue);
108 if (!
TrackEditor.state.isPropertyEnabled(
this.property)) {
109 TrackEditor.state.setPropertyEnabled(this.property,
true);
118 _createButton:
function TrackEditorArtwork__createButton() {
119 this._button = document.createElement(
"button");
120 var vbox = document.createElement(
"vbox");
121 this._element.parentNode.replaceChild(vbox, this._element);
125 if (this._element.hasAttribute(
"tabindex")) {
126 var
value = parseInt(this._element.getAttribute(
"tabindex")) + 1;
127 this._button.setAttribute(
"tabindex", value);
131 this._button.addEventListener(
"command",
132 function() {
self.onButtonCommand(); },
false);
134 vbox.appendChild(this._element);
135 vbox.appendChild(this._button);
142 _createDragOverlay:
function TrackEditorArtwork__createDragOverlay() {
144 this._elementStack = document.createElement(
"stack");
145 this._elementStack.setAttribute(
"class",
"art");
146 if (this._element.hasAttribute(
"tabindex")) {
147 this._elementStack.setAttribute(
"tabindex",
148 this._element.getAttribute(
"tabindex"));
149 this._element.removeAttribute(
"tabindex");
151 this._element.parentNode.replaceChild(this._elementStack, this._element);
154 var dragLabel = document.createElement(
"label");
155 dragLabel.setAttribute(
"class",
"drop-message");
159 dragLabelValue = document.createTextNode(
SBString(
"trackeditor.artwork.drag"));
160 dragLabel.appendChild(dragLabelValue);
163 var imageVBox = document.createElement(
"vbox");
164 imageVBox.setAttribute(
"class",
"artWrapperBox");
165 imageVBox.appendChild(this._element);
168 this._dragoverlay = document.createElement(
"vbox");
169 this._dragoverlay.setAttribute(
"class",
"artWrapperBox");
170 this._dragoverlay.appendChild(dragLabel);
173 this._elementStack.appendChild(imageVBox);
174 this._elementStack.appendChild(this._dragoverlay)
181 _createContextMenu:
function TrackEditorArtwork__createContextMenu() {
182 this._menuPopup = document.createElement(
"menupopup");
183 this._menuCut = document.createElement(
"menuitem");
184 this._menuCopy = document.createElement(
"menuitem");
185 this._menuPaste = document.createElement(
"menuitem");
186 this._menuClear = document.createElement(
"menuitem");
187 var menuSeparatorPaste = document.createElement(
"menuseparator");
190 this._menuCut.setAttribute(
"label",
SBString(
"trackeditor.artwork.menu.cut"));
191 this._menuCut.addEventListener(
"command",
192 function() {
self.onCut();},
false);
194 this._menuCopy.setAttribute(
"label",
SBString(
"trackeditor.artwork.menu.copy"));
195 this._menuCopy.addEventListener(
"command",
196 function() {
self.onCopy();},
false);
198 this._menuPaste.setAttribute(
"label",
SBString(
"trackeditor.artwork.menu.paste"));
199 this._menuPaste.addEventListener(
"command",
200 function() {
self.onPaste();},
false);
202 this._menuClear.setAttribute(
"label",
SBString(
"trackeditor.artwork.menu.clear"));
203 this._menuClear.addEventListener(
"command",
204 function() {
self.onClear();},
false);
205 this._menuPopup.appendChild(this._menuCut);
206 this._menuPopup.appendChild(this._menuCopy);
207 this._menuPopup.appendChild(this._menuPaste);
208 this._menuPopup.appendChild(menuSeparatorPaste);
209 this._menuPopup.appendChild(this._menuClear);
211 this._menuPopup.addEventListener(
"popupshowing",
212 function(evt) {
self.onPopupShowing(evt); },
false);
214 this._element.parentNode.appendChild(this._menuPopup);
220 onContextMenu:
function TrackEditorArtwork_onContextMenu(aEvent) {
222 this._elementStack.focus();
227 var anchor =
"after_start";
228 var anchor_element = this._element;
231 if (aEvent.button == 2) {
233 anchor_element =
null;
235 xPos = aEvent.clientX;
236 yPos = aEvent.clientY;
239 this._menuPopup.openPopup(anchor_element,
252 onPopupShowing:
function TrackEditorArtwork_onPopupShowing(aEvent) {
253 var curImageUrl =
TrackEditor.state.getPropertyValue(this.property);
256 var sbClipboard = Cc[
"@songbirdnest.com/moz/clipboard/helper;1"]
257 .createInstance(Ci.sbIClipboardHelper);
259 var imageData = sbClipboard.copyImageFromClipboard(mimeType, {});
260 mimeType = mimeType.value;
263 var isValidAlbumArt =
false;
264 if (imageData && (imageData.length > 0)) {
265 var artService = Cc[
"@songbirdnest.com/Songbird/album-art-service;1"]
266 .getService(Ci.sbIAlbumArtService);
267 isValidAlbumArt = artService.imageIsValidAlbumArt(mimeType,
273 this._menuCut.setAttribute(
"disabled",
true);
274 this._menuCopy.setAttribute(
"disabled",
true);
275 this._menuClear.setAttribute(
"disabled",
true);
277 this._menuCut.removeAttribute(
"disabled");
278 this._menuCopy.removeAttribute(
"disabled");
279 this._menuClear.removeAttribute(
"disabled");
282 if (!isValidAlbumArt) {
283 this._menuPaste.setAttribute(
"disabled",
true);
285 this._menuPaste.removeAttribute(
"disabled");
290 this._menuCut.setAttribute(
"disabled",
true);
291 this._menuClear.setAttribute(
"disabled",
true);
292 this._menuPaste.setAttribute(
"disabled",
true);
299 onClick:
function TrackEditorArtwork_onClick(aEvent) {
301 this._elementStack.focus();
308 onKeyPress:
function TrackEditorArtwork_onKeyPress(aEvent) {
309 var validMetaKeys =
false;
311 if (aEvent.keyCode == 46 || aEvent.keyCode == 8) {
317 validMetaKeys = (aEvent.metaKey && !aEvent.ctrlKey && !aEvent.altKey);
320 validMetaKeys = (!aEvent.metaKey && aEvent.ctrlKey && !aEvent.altKey);
324 switch (aEvent.charCode) {
345 onPaste:
function TrackEditorArtwork_onPaste() {
346 var sbClipboard = Cc[
"@songbirdnest.com/moz/clipboard/helper;1"]
347 .createInstance(Ci.sbIClipboardHelper);
349 var imageData = sbClipboard.copyImageFromClipboard(mimeType, {});
350 if (sbCoverHelper.isImageSizeValid(
null, imageData.length)) {
353 Cc[
"@songbirdnest.com/Songbird/album-art-service;1"]
354 .getService(Ci.sbIAlbumArtService);
356 var
newURI = artService.cacheImage(mimeType.value,
360 this._imageSrcChange(newURI.spec);
369 onCopy:
function TrackEditorArtwork_onCopy() {
370 var sbClipboard = Cc[
"@songbirdnest.com/moz/clipboard/helper;1"]
371 .createInstance(Ci.sbIClipboardHelper);
374 var imageFilePath =
TrackEditor.state.getPropertyValue(this.property);
375 var
ioService = Cc[
"@mozilla.org/network/io-service;1"]
376 .getService(Ci.nsIIOService);
381 imageURI = ioService.newURI(imageFilePath,
null,
null);
383 Cu.reportError(
"trackEditor: Unable to convert to URI: [" +
384 imageFilePath +
"] - " + err);
389 if (!(imageURI instanceof Ci.nsIFileURL)) {
390 Cu.reportError(
"trackEditor: Not a local file [" +
391 imageFilePath +
"]");
395 imageFile = imageURI.file;
398 [imageData, mimeType] = sbCoverHelper.readImageData(imageFile);
401 sbClipboard.pasteImageToClipboard(mimeType,
405 Cu.reportError(
"trackEditor: Unable to copy from clipboard - " + err);
416 onCut:
function TrackEditorArtwork_onCut() {
425 onClear:
function TrackEditorArtwork_onClear() {
426 this._imageSrcChange(
"");
432 getSupportedFlavours :
function TrackEditorArtwork_getSupportedFlavours() {
433 var flavours =
new FlavourSet();
434 return sbCoverHelper.getFlavours(flavours);
442 onDrop:
function TrackEditorArtwork_onDrop(aEvent, aDropData, aSession) {
447 sbCoverHelper.handleDrop(
function (newFile) {
449 self._imageSrcChange(newFile);
454 onDragStart:
function TrackEditorArtwork_onDragStart(aEvent,
457 var imageURL =
TrackEditor.state.getPropertyValue(this.property);
458 aTransferData.data =
new TransferData();
459 sbCoverHelper.setupDragTransferData(aTransferData, imageURL);
467 onButtonCommand:
function TrackEditorArtwork_onButtonCommand() {
468 this._elementStack.focus();
471 var filePicker = Cc[
"@mozilla.org/filepicker;1"]
472 .createInstance(Ci.nsIFilePicker);
473 var windowTitle =
SBString(
"trackeditor.filepicker.title");
474 filePicker.init(
window, windowTitle, Ci.nsIFilePicker.modeOpen);
475 filePicker.appendFilters(Ci.nsIFilePicker.filterImages);
476 var fileResult = filePicker.show();
477 if (fileResult == Ci.nsIFilePicker.returnOK) {
478 var ioService = Cc[
"@mozilla.org/network/io-service;1"]
479 .getService(Ci.nsIIOService);
480 var fileURL = ioService.newFileURI(filePicker.file).spec;
481 if (sbCoverHelper.isImageSizeValid(fileURL)) {
482 this._imageSrcChange(fileURL);
487 onTrackEditorPropertyChange:
function TrackEditorArtwork_onTrackEditorPropertyChange() {
488 var value =
TrackEditor.state.getPropertyValue(this.property);
490 var XLINK_NS =
"http://www.w3.org/1999/xlink";
491 var SVG_NS =
"http://www.w3.org/2000/svg";
495 var imageElement = this._element.getElementsByTagNameNS(SVG_NS,
"image")[0];
496 if (value && value == imageElement.getAttributeNS(XLINK_NS,
"href")) {
504 allMatch = !
TrackEditor.state.hasMultipleValuesForProperty(this.property);
508 var canEdit = (
TrackEditor.state.writableItemCount ==
512 if (value && value !=
"") {
514 this._dragoverlay.hidden =
true;
516 this._dragoverlay.hidden =
false;
520 if( (!value || value ==
"") && allMatch ) {
525 this._button.label = (value ==
ARTWORK_NO_COVER ? this._addLabel : this._replaceLabel);
528 imageElement.setAttributeNS(XLINK_NS,
"href", (allMatch ? value :
""));
531 if (
TrackEditor.state.isPropertyEdited(
this.property)) {
532 this._elementStack.setAttribute(
"edited",
true);
534 this._elementStack.removeAttribute(
"edited");
538 this._checkbox.checked =
TrackEditor.state.isPropertyEdited(this.property);
542 this._button.setAttribute(
"disabled",
"true");
545 this._button.removeAttribute(
"disabled");
function getPlatformString()
Get the name of the platform we are running on.
dndDefaultHandler_module onDragOver
function SBString(aKey, aDefault, aStringBundle)
function newURI(aURLString)