27 const Cc = Components.classes;
28 const Ci = Components.interfaces;
29 const Cr = Components.results;
30 const Ce = Components.Exception;
31 const Cu = Components.utils;
33 Cu.import(
"resource://gre/modules/XPCOMUtils.jsm");
34 Cu.import(
"resource://app/jsmodules/StringUtils.jsm");
35 Cu.import(
"resource://app/jsmodules/sbProperties.jsm");
36 Cu.import(
"resource://app/jsmodules/ArrayConverter.jsm");
37 Cu.import(
"resource://app/jsmodules/sbLibraryUtils.jsm");
38 Cu.import(
"resource://app/jsmodules/SBJobUtils.jsm");
39 Cu.import(
"resource://app/jsmodules/SBUtils.jsm");
61 isImageSizeValid:
function (aImageURL, aImageSize) {
63 .getService(
Ci.fuelIApplication);
64 var maxFileSize = Application.prefs.getValue(
"songbird.albumart.maxsize",
67 if ( (aImageURL ==
'undefined') &&
68 (aImageSize ==
'undefined') ) {
74 var checkFileSize = aImageSize;
77 var
ioService =
Cc[
"@mozilla.org/network/io-service;1"]
78 .getService(
Ci.nsIIOService);
81 uri = ioService.newURI(aImageURL,
null,
null);
83 Cu.reportError(
"sbCoverHelper: Unable to convert to URI: [" +
84 aImageURL +
"] " + err);
88 if (uri instanceof
Ci.nsIFileURL) {
89 var imageFile = uri.file;
90 checkFileSize = imageFile.fileSize;
94 if (checkFileSize > maxFileSize) {
97 .getService(
Ci.nsIPromptService);
100 Cc[
"@songbirdnest.com/Songbird/Properties/UnitConverter/Storage;1"]
101 .createInstance(
Ci.sbIPropertyUnitConverter);
102 var strTitle =
SBString(
"albumart.maxsize.title",
null);
104 (
"albumart.maxsize.message",
105 [ storageConverter.autoFormat(maxFileSize, -1, 1),
106 storageConverter.autoFormat(checkFileSize, -1, 1) ]);
108 promptService.alert(
null, strTitle, strMsg);
119 readImageData:
function (aInputFile) {
120 if(!aInputFile.exists()) {
126 var newMimeType =
Cc[
"@mozilla.org/mime;1"]
127 .getService(
Ci.nsIMIMEService)
128 .getTypeFromFile(aInputFile);
131 var inputStream =
Cc[
"@mozilla.org/network/file-input-stream;1"]
132 .createInstance(
Ci.nsIFileInputStream);
136 var binaryStream =
Cc[
"@mozilla.org/binaryinputstream;1"]
137 .createInstance(
Ci.nsIBinaryInputStream);
138 binaryStream.setInputStream(inputStream);
139 var size = binaryStream.available();
140 var newImageData = binaryStream.readByteArray(size);
141 binaryStream.close();
144 if (newImageData.length != size) {
148 return [newImageData, newMimeType];
150 Cu.reportError(
"sbCoverHelper: Unable to read file image data: " + err);
163 saveFileToArtworkFolder:
function (aFromFile) {
164 if ( !(aFromFile instanceof
Ci.nsIFile)) {
169 if (!this.isFileImage(aFromFile)) {
174 if (!this.isImageSizeValid(
null, aFromFile.fileSize)) {
181 [imageData, mimeType] = this.readImageData(aFromFile);
184 var artService =
Cc[
"@songbirdnest.com/Songbird/album-art-service;1"]
185 .getService(
Ci.sbIAlbumArtService);
186 var newUri = artService.cacheImage(mimeType, imageData, imageData.length);
191 Cu.reportError(
"sbCoverHelper: Unable to save file image data: " + err);
203 isFileImage:
function(aFile) {
206 if (aFile instanceof
Ci.nsIFile) {
207 var
prefs =
Cc[
"@mozilla.org/preferences-service;1"]
208 .getService(
Ci.nsIPrefBranch);
209 var prefStr = prefs.getCharPref(
"songbird.albumart.file.extensions");
210 var supportedFileExtensions = prefStr.split(
",");
212 var length = supportedFileExtensions.length;
213 var filename = aFile.QueryInterface(
Ci.nsIFile).leafName;
214 var result = filename.match(/.*\.(\w*)$/);
215 var extension = (result ? result[1] :
null);
217 isSafe = (supportedFileExtensions.indexOf(extension) != -1);
232 var tempFile =
Cc[
"@mozilla.org/file/directory_service;1"]
233 .getService(
Ci.nsIProperties)
234 .get(
"TmpD",
Ci.nsIFile);
237 var webProgressListener = {
242 onLocationChange :
function (a, b, c) { },
243 onProgressChange :
function (a, b, c,
d, e, f) { },
244 onSecurityChange :
function (a, b, c) { },
245 onStatusChange :
function (a, b, c,
d) { },
246 onStateChange :
function (aWebProgress, aRequest, aStateFlags, aStatus) {
248 if (aStateFlags &
Ci.nsIWebProgressListener.STATE_STOP) {
250 var fileName =
self.saveFileToArtworkFolder(tempFile);
258 var extension =
null;
263 extension = aWebURL.match(/\.[a-zA-Z0-9]+$/)[0];
266 var uuidGenerator =
Cc[
"@mozilla.org/uuid-generator;1"]
267 .getService(
Ci.nsIUUIDGenerator);
268 var
uuid = uuidGenerator.generateUUID();
269 var uuidFileName = uuid.toString();
271 uuidFileName = uuidFileName + extension;
272 tempFile.append(uuidFileName);
273 tempFile.createUnique(
Ci.nsIFile.NORMAL_FILE_TYPE, 0644);
276 var registerFileForDelete =
Cc[
"@mozilla.org/uriloader/external-helper-app-service;1"]
277 .getService(
Ci.nsPIExternalAppLauncher);
278 registerFileForDelete.deleteTemporaryFileOnExit(tempFile);
281 var
ioService =
Cc[
"@mozilla.org/network/io-service;1"]
282 .getService(
Ci.nsIIOService);
283 var webDownloader =
Cc[
'@mozilla.org/embedding/browser/nsWebBrowserPersist;1']
284 .createInstance(
Ci.nsIWebBrowserPersist);
285 webDownloader.persistFlags =
Ci.nsIWebBrowserPersist.PERSIST_FLAGS_NONE;
286 webDownloader.progressListener = webProgressListener;
287 webDownloader.saveURI(ioService.newURI(aWebURL,
null,
null),
298 getFlavours:
function(flavours) {
299 flavours.appendFlavour(
"application/x-moz-file",
"nsIFile");
300 flavours.appendFlavour(
"text/x-moz-url");
301 flavours.appendFlavour(
"text/uri-list");
302 flavours.appendFlavour(
"text/html");
303 flavours.appendFlavour(
"text/unicode");
304 flavours.appendFlavour(
"text/plain");
315 handleDrop:
function(aCallback, aDropData) {
316 switch(aDropData.flavour.contentType) {
318 case "text/x-moz-url":
319 case "text/uri-list":
324 if (aDropData.data instanceof
Ci.nsISupportsString) {
325 url = aDropData.data.toString();
327 url = aDropData.data;
330 if (aDropData.flavour.contentType ==
"text/html") {
333 var imgRegExpr = /\<img.+src=\
"(.+?)\"/i;
334 var matches = url.match(imgRegExpr);
335 if (matches && matches.length > 1) {
339 // Only take the first url if there are more than one.
340 url = url.split("\n
")[0];
343 // Now convert it into an URI so we can determine what to do with it
344 var ioService = Cc["@mozilla.org/network/io-
service;1
"]
345 .getService(Ci.nsIIOService);
348 uri = ioService.newURI(url, null, null);
350 Cu.reportError("sbCoverHelper: Unable to convert to URI: [
" + url +
355 switch (uri.scheme) {
357 if (uri instanceof Ci.nsIFileURL) {
358 var fileName = this.saveFileToArtworkFolder(uri.file);
366 this.downloadFile(url, aCallback);
370 Cu.reportError("sbCoverHelper: Unable to
handle:
" + uri.scheme);
375 case "application/x-moz-
file":
376 // Files are super easy :) just save it to our artwork folder
377 if (aDropData.data instanceof Ci.nsILocalFile) {
378 var fileName = this.saveFileToArtworkFolder(aDropData.data);
381 Cu.reportError("sbCoverHelper: Not a local
file.
");
394 setupDragTransferData: function(aTransferData, aImageURL) {
395 var ioService = Cc["@mozilla.org/network/io-
service;1
"]
396 .getService(Ci.nsIIOService);
399 // First try to convert the URL spec to an URI
401 imageURI = ioService.newURI(aImageURL, null, null);
402 // We need to convert resource:// urls to file://
403 if (imageURI.schemeIs("resource
")) {
404 var protoHandler = ioService.getProtocolHandler("resource
")
405 .QueryInterface(Ci.nsIResProtocolHandler);
406 var newImageURL = protoHandler.resolveURI(imageURI);
408 aImageURL = newImageURL;
409 imageURI = ioService.newURI(aImageURL, null, null);
411 Cu.reportError(aImageURL + " did not properly convert from resource
" +
417 Cu.reportError("sbCoverHelper: Unable to convert to URI: [
" + aImageURL +
422 // If we have a local file then put it as a proper image mime type
423 // and as a x-moz-file
424 if (imageURI instanceof Ci.nsIFileURL) {
426 // Read the mime type for the flavour
427 var mimetype = Cc["@mozilla.org/mime;1
"]
428 .getService(Ci.nsIMIMEService)
429 .getTypeFromFile(imageURI.file);
431 // Create an input stream for mime type flavour if we can
432 var inputStream = Cc["@mozilla.org/network/
file-input-stream;1
"]
433 .createInstance(Ci.nsIFileInputStream);
434 inputStream.init(imageURI.file, FLAGS_DEFAULT, FLAGS_DEFAULT, 0);
436 aTransferData.data.addDataForFlavour(mimetype,
439 Ci.nsIFileInputStream);
441 Cu.reportError("sbCoverHelper: Unable to
add image from
file: [
" +
442 aImageURL + "]
" + err);
445 // Add a file flavour
446 aTransferData.data.addDataForFlavour("application/x-moz-
file",
453 aTransferData.data.addDataForFlavour("text/x-moz-url
",
454 aImageURL + "\n
" + imageURI.file.path);
456 // Add a uri-list flavour
457 aTransferData.data.addDataForFlavour("text/
uri-list
", aImageURL);
459 // Add simple Unicode flavour
460 aTransferData.data.addDataForFlavour("text/unicode
", aImageURL);
463 aTransferData.data.addDataForFlavour("text/
html",
469 aTransferData.data.addDataForFlavour(
"text/plain", aImageURL);
481 getArtworkForItems:
function(aItemList,
484 aSuppressProgressDialog) {
485 var library = aLibrary;
489 var mediaItems = aItemList;
490 if (aItemList instanceof
Ci.nsIArray) {
491 mediaItems = aItemList.enumerate();
492 }
else if (!(aItemList instanceof
Ci.nsISimpleEnumerator)) {
493 Cu.reportError(
"getArtworkForItems: Item list is not a valid" +
494 " nsIArray or nsISimpleEnumerator.");
498 if (!mediaItems.hasMoreElements()) {
499 Cu.reportError(
"getArtworkForItems: No items to get artwork for.");
505 Cc[
"@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1"]
506 .createInstance(
Ci.sbIPropertyArray);
507 listProperties.appendProperty(SBProperties.hidden,
"1");
508 listProperties.appendProperty(SBProperties.mediaListName,
"Get Artwork");
509 var getArtworkMediaList = library.createMediaList(
"simple",
511 var isAudioItem =
function(aItem) {
512 return aItem.getProperty(SBProperties.contentType) ==
"audio";
516 getArtworkMediaList.addSome(audioItems);
519 var artworkScanner =
Cc[
"@songbirdnest.com/Songbird/album-art/scanner;1"]
520 .createInstance(
Ci.sbIAlbumArtScanner);
523 var jobProgressListener = {
524 onJobProgress:
function(aJobProgress) {
525 if (aJobProgress.status !=
Ci.sbIJobProgress.STATUS_RUNNING) {
526 library.remove(getArtworkMediaList);
528 artworkScanner.removeJobProgressListener(jobProgressListener);
535 artworkScanner.addJobProgressListener(jobProgressListener);
536 artworkScanner.scanListForArtwork(getArtworkMediaList);
537 if (!aSuppressProgressDialog)
538 SBJobUtils.showProgressDialog(artworkScanner, aWindow);
nsString encodeURIComponent(const nsString &c)
function SBFilteredEnumerator(aEnumerator, aFilterFunc)
const MAX_FILE_SIZE_BYTES
function SBBrandedFormattedString(aKey, aParams, aDefault, aStringBundle)
sbOSDControlService prototype QueryInterface
ui plugin add("draggable","cursor",{start:function(e, ui){var t=$('body');if(t.css("cursor")) ui.options._cursor=t.css("cursor");t.css("cursor", ui.options.cursor);}, stop:function(e, ui){if(ui.options._cursor)$('body').css("cursor", ui.options._cursor);}})
function handle(request, response)
function SBString(aKey, aDefault, aStringBundle)
function downloadFile(url)
Download an XPI file from a URL. Downloads an XPI file from a URL to a temporary file on disk...
Javascript wrappers for common library tasks.
ContinuingWebProgressListener prototype onStateChange