home *** CD-ROM | disk | FTP | other *** search
/ Freelog 112 / FreelogNo112-NovembreDecembre2012.iso / Multimedia / Songbird / Songbird_2.0.0-2311_windows-i686-msvc8.exe / components / sbPlaylistWriterManager.js < prev    next >
Text File  |  2012-06-08  |  5KB  |  172 lines

  1. /*
  2.  *=BEGIN SONGBIRD GPL
  3.  *
  4.  * This file is part of the Songbird web player.
  5.  *
  6.  * Copyright(c) 2005-2009 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.  
  25. //
  26. // sbIPlaylistWriterManager Object
  27. //
  28. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  29. Components.utils.import("resource://app/jsmodules/ArrayConverter.jsm");
  30.  
  31. const Cc = Components.classes;
  32. const Ci = Components.interfaces;
  33. const Cr = Components.results;
  34.  
  35. function sbPlaylistWriterManager()
  36. {
  37.   if (this._writers) {
  38.     // The class-stored _writers list is already initialized.
  39.     return;
  40.   }
  41.  
  42.   // At creation, initialize a list of playlist writers
  43.   var catMan = Cc["@mozilla.org/categorymanager;1"]
  44.                  .getService(Ci.nsICategoryManager);
  45.  
  46.   var category = catMan.enumerateCategory("playlist-writer");
  47.  
  48.   this._writers = [];
  49.   for (var entry in ArrayConverter.JSEnum(category)) {
  50.     entry = entry.QueryInterface(Ci.nsISupportsCString);
  51.     var writer = Cc[catMan.getCategoryEntry("playlist-writer", entry.data)]
  52.                    .createInstance(Ci.sbIPlaylistWriter);
  53.     this._writers.push(writer);
  54.   }
  55.  
  56.   // Cache the supported strings.
  57.   this._extensions = [];
  58.   this._MIMETypes = [];
  59.   for(var i in this._writers) {
  60.     this._extensions = this._extensions.concat(
  61.       this._writers[i].supportedFileExtensions({}));
  62.     this._MIMETypes = this._MIMETypes.concat(
  63.       this._writers[i].supportedMIMETypes({}));
  64.   }
  65. };
  66.  
  67. sbPlaylistWriterManager.prototype.constructor = sbPlaylistWriterManager;
  68. sbPlaylistWriterManager.prototype = {
  69.   classDescription: "Songbird Playlist Writer Manager Interface",
  70.   classID:          Components.ID("{9d66cb86-951e-49f4-bef8-26a578891275}"),
  71.   contractID:       "@songbirdnest.com/Songbird/PlaylistWriterManager;1",
  72.   QueryInterface:   XPCOMUtils.generateQI([Ci.sbIPlaylistWriterManager]),
  73.  
  74.   _writers: null,
  75.   _extensions: null,
  76.   _MIMETypes: null,
  77.  
  78.   //sbIPlaylistWriterManager
  79.   writePlaylist: function(aURI, aMediaList, aContentType, aPlaylistWriterListener)
  80.   {
  81.     if (aURI instanceof Ci.nsIFileURL) {
  82.       var file = aURI.QueryInterface(Ci.nsIFileURL).file;
  83.  
  84.       try {
  85.         this.write(file, aMediaList, aContentType, aAddDistinctOnly);
  86.         if (aPlaylistWriterListener && aPlaylistWriterListener.observer) {
  87.           aPlaylistWriterListener.observer.observe(aMediaList, "success", "");
  88.         }
  89.         return 1;
  90.       }
  91.       catch(e if e.result == Cr.NS_ERROR_NOT_AVAILABLE) {
  92.         return -1;
  93.       }
  94.     }
  95.  
  96.     return 1;
  97.   },
  98.  
  99.   write: function(aFile, aMediaList, aContentType, aPlaylistFormatType)
  100.   {
  101.     var theExtension = this._getFileExtension(aFile);
  102.     if (!this._writers) {
  103.       this._cacheWriters();
  104.     }
  105.  
  106.     for (var r in this._writers) {
  107.       var aWriter = this._writers[r];
  108.       var theExtensions = aWriter.supportedFileExtensions({});
  109.       var theMIMETypes = aWriter.supportedMIMETypes({});
  110.  
  111.       // Only consider file extensions if we have no content-type specified.
  112.       if(theMIMETypes.some(function (a) { return a == aContentType } ) ||
  113.         (!aContentType && theExtensions.some(
  114.             function(a) { return a == theExtension }))) {
  115.         aWriter.write(aFile, aMediaList, aContentType, aPlaylistFormatType);
  116.         return;
  117.       }
  118.     }
  119.  
  120.     // Couldn't handle it so throw
  121.     throw Cr.NS_ERROR_NOT_AVAILABLE;
  122.   },
  123.  
  124.   supportedFileExtensions: function(nExtCount)
  125.   {
  126.     if (!this._writers) {
  127.       this._cacheWriters();
  128.     }
  129.     nExtCount.value = this._extensions.length;
  130.     return this._extensions;
  131.   },
  132.  
  133.   supportedMIMETypes: function(nMIMECount)
  134.   {
  135.     if (!this._writers) {
  136.       this._cacheWriters();
  137.     }
  138.     nMIMECount.value = this._MIMETypes.length;
  139.     return this._MIMETypes;
  140.   },
  141.  
  142.   /**
  143.    * Extract the file extension from an {nsIUR{I,L}, nsIFile}
  144.    */
  145.   _getFileExtension: function(aThing)
  146.   {
  147.     var name = "";
  148.     if (aThing instanceof Ci.nsIURL) {
  149.       if ("" != aThing.fileExtension) {
  150.         return aThing.fileExtension;
  151.       }
  152.     }
  153.     if (aThing instanceof Ci.nsIURI) {
  154.       name = aThing.path;
  155.     }
  156.     if (aThing instanceof Ci.nsIFile) {
  157.       name = aThing.leafName;
  158.     }
  159.     // find the file extension
  160.     var m = /\.([^\.\/]+)$/(name);
  161.     if (m) {
  162.       return m[1];
  163.     } else {
  164.       return null;
  165.     }
  166.   },
  167. };
  168.  
  169. function NSGetModule(compMgr, fileSpec) {
  170.   return XPCOMUtils.generateModule([sbPlaylistWriterManager]);
  171. }
  172.