home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / audio-video / songbird / Songbird_0.3_windows-i686.exe / components / sbDownloadDeviceHelper.js < prev    next >
Text File  |  2007-10-27  |  8KB  |  248 lines

  1. /*
  2. //
  3. // BEGIN SONGBIRD GPL
  4. //
  5. // This file is part of the Songbird web player.
  6. //
  7. // Copyright(c) 2005-2007 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. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  28. Components.utils.import("resource://app/components/sbProperties.jsm");
  29. Components.utils.import("resource://app/components/ArrayConverter.jsm");
  30.  
  31. const Ci = Components.interfaces;
  32. const Cc = Components.classes;
  33. const Cr = Components.results;
  34.  
  35. const DOWNLOAD_PREF = "songbird.library.download";
  36.  
  37. function sbDownloadDeviceHelper()
  38. {
  39.   this._mainLibrary = Cc["@songbirdnest.com/Songbird/library/Manager;1"]
  40.                         .getService(Ci.sbILibraryManager).mainLibrary;
  41. }
  42.  
  43. sbDownloadDeviceHelper.prototype =
  44. {
  45.   classDescription: "Songbird Download Device Helper",
  46.   classID:          Components.ID("{576b6833-15d8-483a-84d6-2fbd329c82e1}"),
  47.   contractID:       "@songbirdnest.com/Songbird/DownloadDeviceHelper;1",
  48.   QueryInterface:   XPCOMUtils.generateQI([Ci.sbIDownloadDeviceHelper])
  49. }
  50.  
  51. sbDownloadDeviceHelper.prototype.downloadItem =
  52. function sbDownloadDeviceHelper_downloadItem(aMediaItem)
  53. {
  54.   if (!this._ensureDestination()) {
  55.     return;
  56.   }
  57.   if (!this.downloadMediaList) {
  58.     throw Cr.NS_ERROR_FAILURE;
  59.   }
  60.  
  61.   let uriArray           = Cc["@mozilla.org/array;1"]
  62.                              .createInstance(Ci.nsIMutableArray);
  63.   let propertyArrayArray = Cc["@mozilla.org/array;1"]
  64.                              .createInstance(Ci.nsIMutableArray);
  65.  
  66.   let item;
  67.   if (aMediaItem.library.equals(this._mainLibrary)) {
  68.     item = aMediaItem;
  69.   }
  70.   else {
  71.     this._addItemToArrays(aMediaItem, uriArray, propertyArrayArray);
  72.     let items = this._mainLibrary.batchCreateMediaItems(uriArray,
  73.                                                         propertyArrayArray,
  74.                                                         true);
  75.     item = items.queryElementAt(0, Ci.sbIMediaItem);
  76.   }
  77.  
  78.   this.downloadMediaList.add(item);
  79. }
  80.  
  81. sbDownloadDeviceHelper.prototype.downloadSome =
  82. function sbDownloadDeviceHelper_downloadSome(aMediaItems)
  83. {
  84.   if (!this._ensureDestination()) {
  85.     return;
  86.   }
  87.   if (!this.downloadMediaList) {
  88.     throw Cr.NS_ERROR_FAILURE;
  89.   }
  90.  
  91.   let uriArray           = Cc["@mozilla.org/array;1"]
  92.                              .createInstance(Ci.nsIMutableArray);
  93.   let propertyArrayArray = Cc["@mozilla.org/array;1"]
  94.                              .createInstance(Ci.nsIMutableArray);
  95.  
  96.   var items = [];
  97.   while (aMediaItems.hasMoreElements()) {
  98.     let item = aMediaItems.getNext();
  99.     if (item.library.equals(this._mainLibrary)) {
  100.       items.push(item);
  101.     }
  102.     else {
  103.       this._addItemToArrays(item, uriArray, propertyArrayArray);
  104.     }
  105.   }
  106.  
  107.   if (uriArray.length > 0) {
  108.     let addedItems = this._mainLibrary.batchCreateMediaItems(uriArray,
  109.                                                              propertyArrayArray,
  110.                                                              true);
  111.     for (let i = 0; i < addedItems.length; i++) {
  112.       items.push(addedItems.queryElementAt(i, Ci.sbIMediaItem));
  113.     }
  114.   }
  115.  
  116.   this.downloadMediaList.addSome(ArrayConverter.enumerator(items));
  117. }
  118.  
  119. sbDownloadDeviceHelper.prototype.downloadAll =
  120. function sbDownloadDeviceHelper_downloadAll(aMediaList)
  121. {
  122.   if (!this._ensureDestination()) {
  123.     return;
  124.   }
  125.   if (!this.downloadMediaList) {
  126.     throw Cr.NS_ERROR_FAILURE;
  127.   }
  128.  
  129.   let uriArray           = Cc["@mozilla.org/array;1"]
  130.                              .createInstance(Ci.nsIMutableArray);
  131.   let propertyArrayArray = Cc["@mozilla.org/array;1"]
  132.                              .createInstance(Ci.nsIMutableArray);
  133.  
  134.   var items = [];
  135.   var isForeign = aMediaList.library.equals(this._mainLibrary);
  136.   for (let i = 0; i < aMediaList.length; i++) {
  137.     var item = aMediaList.getItemByIndex(i);
  138.     if (isForeign) {
  139.       this._addItemToArrays(item, uriArray, propertyArrayArray);
  140.     }
  141.     else {
  142.       items.push(item);
  143.     }
  144.   }
  145.  
  146.   if (uriArray.length > 0) {
  147.     let addedItems = this._mainLibrary.batchCreateMediaItems(uriArray,
  148.                                                              propertyArrayArray,
  149.                                                              true);
  150.     for (let i = 0; i < addedItems.length; i++) {
  151.       items.push(addedItems.queryElementAt(i, Ci.sbIMediaItem));
  152.     }
  153.   }
  154.  
  155.   this.downloadMediaList.addSome(ArrayConverter.enumerator(items));
  156. }
  157.  
  158. sbDownloadDeviceHelper.prototype.__defineGetter__("downloadMediaList",
  159. function sbDownloadDeviceHelper_getDownloadMediaList()
  160. {
  161.   if (!this._downloadDevice) {
  162.     var devMgr = Cc["@songbirdnest.com/Songbird/DeviceManager;1"]
  163.                    .getService(Ci.sbIDeviceManager);
  164.     if (devMgr) {
  165.       var downloadCat = "Songbird Download Device";
  166.       if (devMgr.hasDeviceForCategory(downloadCat)) {
  167.         this._downloadDevice = devMgr.getDeviceByCategory(downloadCat)
  168.                                      .QueryInterface(Ci.sbIDownloadDevice);
  169.       }
  170.     }
  171.   }
  172.   return this._downloadDevice ? this._downloadDevice.downloadMediaList : null ;
  173. });
  174.  
  175. sbDownloadDeviceHelper.prototype._addItemToArrays =
  176. function sbDownloadDeviceHelper__addItemToArrays(aMediaItem,
  177.                                                  aURIArray,
  178.                                                  aPropertyArrayArray)
  179. {
  180.   aURIArray.appendElement(aMediaItem.contentSrc, false);
  181.  
  182.   var dest   = SBProperties.createArray();
  183.   var source = aMediaItem.getProperties();
  184.   for (let i = 0; i < source.length; i++) {
  185.     var prop = source.getPropertyAt(i);
  186.     if (prop.name != SBProperties.contentSrc) {
  187.       dest.appendElement(prop, false);
  188.     }
  189.   }
  190.  
  191.   var target = aMediaItem.library.guid + "," + aMediaItem.guid;
  192.   dest.appendProperty(SBProperties.downloadStatusTarget, target);
  193.  
  194.   aPropertyArrayArray.appendElement(dest, false);
  195. }
  196.  
  197. sbDownloadDeviceHelper.prototype._ensureDestination =
  198. function sbDownloadDeviceHelper__ensureDestination()
  199. {
  200.   var prefs = Cc["@mozilla.org/preferences-service;1"]
  201.                   .getService(Ci.nsIPrefBranch2);
  202.   try {
  203.     var always = prefs.getCharPref("songbird.download.always") == "1";
  204.     if (always) {
  205.       return true;
  206.     }
  207.   }
  208.   catch(e) {
  209.     // Don't care if it fails
  210.   }
  211.   
  212.   var chromeFeatures = "modal,chrome,centerscreen,dialog=yes,resizable=no";
  213.   var useTitlebar = false;
  214.   try {
  215.     useTitlebar = prefs.getCharPref("songbird.accessibility.enabled") == "1";
  216.   }
  217.   catch(e) {
  218.     // Don't care if it fails
  219.   }
  220.  
  221.   // bonus stuff to shut the mac up.
  222.   var chromeFeatures = ",modal=yes,resizable=no";
  223.   if (useTitlebar) chromeFeatures += ",titlebar=yes";
  224.   else chromeFeatures += ",titlebar=no";
  225.  
  226.   var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  227.                       .getService(Components.interfaces.nsIWindowMediator);
  228.   var mainWin = wm.getMostRecentWindow("Songbird:Main");
  229.  
  230.   var paramBlock = Cc["@mozilla.org/embedcomp/dialogparam;1"]
  231.                      .createInstance(Ci.nsIDialogParamBlock);
  232.  
  233.   var watcher = Cc["@mozilla.org/embedcomp/window-watcher;1"]
  234.                   .getService(Ci.nsIWindowWatcher);
  235.   watcher.openWindow(mainWin,
  236.                      "chrome://songbird/content/xul/download.xul",
  237.                      "_blank",
  238.                      chromeFeatures,
  239.                      paramBlock);
  240.  
  241.   return paramBlock.GetInt(0) == 1;
  242. }
  243.  
  244. function NSGetModule(compMgr, fileSpec) {
  245.   return XPCOMUtils.generateModule([sbDownloadDeviceHelper]);
  246. }
  247.  
  248.