home *** CD-ROM | disk | FTP | other *** search
/ Chip 2008 June / CHIP-2008-06.iso / software / Songbird / Songbird_0.5_windows-i686-msvc8.exe / scripts / sbPlaylistHandlerUtils.js < prev    next >
Encoding:
JavaScript  |  2008-03-10  |  6.0 KB  |  222 lines

  1. /**
  2. //
  3. // BEGIN SONGBIRD GPL
  4. //
  5. // This file is part of the Songbird web player.
  6. //
  7. // Copyright(c) 2005-2008 POTI, Inc.
  8. // http://songbirdnest.com
  9. //
  10. // This file may be licensed under the terms of of the
  11. // GNU General Public License Version 2 (the "GPL").
  12. //
  13. // Software distributed under the License is distributed
  14. // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
  15. // express or implied. See the GPL for the specific language
  16. // governing rights and limitations.
  17. //
  18. // You should have received a copy of the GPL along with this
  19. // program. If not, go to http://www.gnu.org/licenses/gpl.html
  20. // or write to the Free Software Foundation, Inc.,
  21. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. //
  23. // END SONGBIRD GPL
  24. //
  25.  */
  26.  
  27. /**
  28.  * \file sbPlaylistHandlerUtils.js
  29.  */
  30.  
  31. Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
  32.  
  33. const PR_RDONLY = -1;
  34. const PR_FLAGS_DEFAULT = -1;
  35.  
  36. function SB_ProcessFile(aFile, aCallback, aThis) {
  37.  
  38.   var istream = Cc["@mozilla.org/network/file-input-stream;1"]
  39.                   .createInstance(Ci.nsIFileInputStream);
  40.   istream.init(aFile, PR_RDONLY, PR_FLAGS_DEFAULT, 0);
  41.   istream.QueryInterface(Ci.nsILineInputStream);
  42.  
  43.   var line = {}, hasmore;
  44.   do {
  45.     hasmore = istream.readLine(line);
  46.     aCallback.apply(aThis, [line.value]);
  47.   } while(hasmore);
  48.  
  49.   istream.close();
  50. }
  51.  
  52. function SB_AddItems(aItems, aMediaList, aAddDistinctOnly) {
  53.  
  54.   if (aItems.length == 0)
  55.     return;
  56.  
  57.   function removeItemsByUri(items, uri) {
  58.     for (var i = items.length - 1; i >= 0; i--) {
  59.       if (items[i].uri.spec == uri)
  60.         items.splice(i, 1);
  61.     }
  62.   }
  63.  
  64.   // If aAddDistinctOnly is true, remove all items from the aItems array that
  65.   // are already in this list.  List membership is based on matching either
  66.   // the #contentURL or #originURL properties
  67.   if (aAddDistinctOnly) {
  68.  
  69.     // De-dup aItems by uri
  70.     for (var i = 0; i < aItems.length - 1; i++) {
  71.       var uri = aItems[i].uri;
  72.       for (var j = i + 1; j < aItems.length; j++)
  73.         if (aItems[j].uri.equals(uri))
  74.           aItems.splice(j, 1);
  75.     }
  76.  
  77.     // Remove all the items from aItems that have matching #contentURL
  78.     // property values
  79.     var propertyArray = SBProperties.createArray();
  80.     aItems.forEach(function(e) {
  81.       propertyArray.appendProperty(SBProperties.contentURL, e.uri.spec);
  82.     });
  83.  
  84.     var listener = {
  85.       item: null,
  86.       onEnumerationBegin: function() {
  87.       },
  88.       onEnumeratedItem: function(list, item) {
  89.         removeItemsByUri(aItems, item.contentSrc.spec);
  90.       },
  91.       onEnumerationEnd: function() {
  92.       }
  93.     };
  94.  
  95.     aMediaList.enumerateItemsByProperties(propertyArray,
  96.                                           listener );
  97.  
  98.     // Remove all the items from aItems that have matching originUrl
  99.     // property values
  100.     if (aItems.length > 0) {
  101.       propertyArray.clear();
  102.       aItems.forEach(function(e) {
  103.         propertyArray.appendProperty(SBProperties.originURL, e.uri.spec);
  104.       });
  105.  
  106.       listener = {
  107.         item: null,
  108.         onEnumerationBegin: function() {
  109.         },
  110.         onEnumeratedItem: function(list, item) {
  111.           removeItemsByUri(aItems, item.getProperty(SBProperties.originURL));
  112.         },
  113.         onEnumerationEnd: function() {
  114.         }
  115.       };
  116.  
  117.       aMediaList.enumerateItemsByProperties(propertyArray,
  118.                                             listener );
  119.  
  120.     }
  121.   }
  122.  
  123.   // If any items are to be added, add them in a batch
  124.   if (aItems.length > 0) {
  125.  
  126.     var uris = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray);
  127.     aItems.forEach(function(e) {
  128.       uris.appendElement(e.uri, false);
  129.     });
  130.  
  131.     var added = aMediaList.library.batchCreateMediaItems(uris, null, true);
  132.     for (var i = 0; i < added.length; i++) {
  133.       aItems[i].item = added.queryElementAt(i, Ci.sbIMediaItem);
  134.     }
  135.   }
  136.  
  137.   // Set the properties on all the items
  138.   aMediaList.runInBatchMode(function() {
  139.     aItems.forEach(function(e) {
  140.       for (var prop in e.properties) {
  141.         try {
  142.           e.item.setProperty(prop, e.properties[prop]);
  143.         }
  144.         catch(e) {
  145.           Components.utils.reportError(e);
  146.         }
  147.       }
  148.     });
  149.   });
  150.  
  151.   // We also need to add the new items to the media list.  If the media list
  152.   // is actually the library, this is essentially a no-op
  153.   var enumerator = {
  154.     _index: 0,
  155.     _array: aItems,
  156.     hasMoreElements: function() {
  157.       return this._index < this._array.length;
  158.     },
  159.     getNext: function() {
  160.       var item = this._array[this._index].item;
  161.       this._index++;
  162.       return item;
  163.     }
  164.   };
  165.  
  166.   aMediaList.addSome(enumerator);
  167. }
  168.  
  169. function SB_ResolveURI(aStringURL, aBaseURI)
  170. {
  171.   var isURI = false;
  172.  
  173.   var ios = Cc["@mozilla.org/network/io-service;1"]
  174.               .getService(Ci.nsIIOService);
  175.  
  176.   // If there is no base URI or the base URI is a file, try the string as a
  177.   // file
  178.   if (aBaseURI == null || aBaseURI.scheme == "file") {
  179.     try {
  180.       var file = Cc["@mozilla.org/file/local;1"]
  181.                    .createInstance(Ci.nsILocalFile);
  182.       file.initWithPath(aStringURL);
  183.  
  184.       var uri = ios.newFileURI(file);
  185.       return uri;
  186.     }
  187.     catch(e) {
  188.       // If the base URI is a local file, try to use it to resolve the local
  189.       // file path
  190.       // XXXsteve: this does not work since setRelativeDescriptor does not know
  191.       // if the leaf of the base path is a directory or a file.
  192. /*
  193.       if (aBaseURI && aBaseURI.scheme == "file") {
  194.         try {
  195.           var baseFile = aBaseURI.QueryInterface(Ci.nsIFileURL).file;
  196.           var file = Cc["@mozilla.org/file/local;1"]
  197.                        .createInstance(Ci.nsILocalFile);
  198.           file.setRelativeDescriptor(baseFile, aStringURL);
  199.           var uri = ios.newFileURI(file);
  200.           return uri;
  201.         }
  202.         catch(e) {
  203.           // fall through
  204.         }
  205.       }
  206. */
  207.     }
  208.   }
  209.  
  210.   // Ok, it is not a local file.  Try creating a new URI with the base URI
  211.   try {
  212.     var uri = ios.newURI(aStringURL, null, aBaseURI);
  213.     return uri;
  214.   }
  215.   catch(e) {
  216.     // fall through
  217.   }
  218.  
  219.   // Couldn't resolve it, return null for failure
  220.   return null;
  221. }
  222.