home *** CD-ROM | disk | FTP | other *** search
/ Freelog 112 / FreelogNo112-NovembreDecembre2012.iso / Multimedia / Songbird / Songbird_2.0.0-2311_windows-i686-msvc8.exe / components / sbAutoDownloader.js < prev    next >
Text File  |  2012-06-08  |  8KB  |  278 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. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  28. Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
  29.  
  30. var Ci = Components.interfaces;
  31. var Cc = Components.classes;
  32. var Cr = Components.results;
  33.  
  34. // big-ass debug function
  35. function DEBUG(msg) {
  36.   return;
  37.  
  38.   function repr(x) {
  39.     if (x == undefined) {
  40.       return 'undefined';
  41.     } else if (x == null) {
  42.       return 'null';
  43.     } else if (typeof x == 'function') {
  44.       return x.name+'(...)';
  45.     } else if (typeof x == 'string') {
  46.       return x.toSource().match(/^\(new String\((.*)\)\)$/)[1];
  47.     } else if (typeof x == 'number') {
  48.       return x.toSource().match(/^\(new Number\((.*)\)\)$/)[1];
  49.     } else if (typeof x == 'object' && x instanceof Array) {
  50.       var value = '';
  51.       for (var i=0; i<x.length; i++) {
  52.         if (i) value = value + ', ';
  53.         value = value + repr(x[i]);
  54.       }
  55.       return '['+value+']';
  56.     } else if (x instanceof Ci.nsISupports) {
  57.       return x.toString();
  58.     } else {
  59.       return x.toSource();
  60.     }
  61.   }
  62.   dump('DEBUG '+DEBUG.caller.name);
  63.   if (msg == undefined) {
  64.     // when nothing is passed in, print the arguments
  65.     dump('(');
  66.     for (var i=0; i<DEBUG.caller.length; i++) {
  67.       if (i) dump(', ');
  68.       dump(repr(DEBUG.caller.arguments[i]));
  69.     }
  70.     dump(')');
  71.   } else {
  72.     dump(': ');
  73.     if (typeof msg != 'object' || msg instanceof Array) {
  74.       dump(repr(msg));
  75.     } else {
  76.       dump(msg.toSource());
  77.     }
  78.   }
  79.   dump('\n');
  80. }
  81.  
  82.  
  83.   ////////////////////
  84.  // Initialization //
  85. ////////////////////
  86. function sbAutoDownloader() {
  87.   DEBUG();
  88.  
  89.   var obs = Cc["@mozilla.org/observer-service;1"]
  90.               .getService(Ci.nsIObserverService);
  91.   obs.addObserver(this, 'songbird-library-manager-ready', false);
  92. }
  93. sbAutoDownloader.prototype._libraryManager = null;
  94. sbAutoDownloader.prototype._library = null;
  95. sbAutoDownloader.prototype._helper = null;
  96. sbAutoDownloader.prototype._queue = [];
  97. sbAutoDownloader.prototype._timer = null;
  98.  
  99.   ///////////
  100.  // XPCOM //
  101. ///////////
  102. sbAutoDownloader.prototype.classDescription =
  103.     'Songbird Auto Downloader Service';
  104. sbAutoDownloader.prototype.classID =
  105.     Components.ID("{a3d7426b-0b22-4f07-b72c-e44bab0759f7}");
  106. sbAutoDownloader.prototype.contractID = '@songbirdnest.com/autodownloader;1';
  107. sbAutoDownloader.prototype.flags = Ci.nsIClassInfo.SINGLETON;
  108. sbAutoDownloader.prototype.interfaces =
  109.     [Ci.nsISupports, Ci.nsIClassInfo, Ci.nsIObserver, Ci.sbIMediaListListener];
  110. sbAutoDownloader.prototype.getHelperForLanguage = function(x) { return null; }
  111. sbAutoDownloader.prototype.getInterfaces =
  112. function sbAutoDownloader_getInterfaces(count, array) {
  113.   array.value = this.interfaces;
  114.   count.value = array.value.length;
  115. }
  116. sbAutoDownloader.prototype.QueryInterface =
  117.     XPCOMUtils.generateQI(sbAutoDownloader.prototype.interfaces);
  118.  
  119.  
  120.   /////////////////
  121.  // nsIObserver //
  122. /////////////////
  123. sbAutoDownloader.prototype.observe =
  124. function sbAutoDownloader_observe(subject, topic, data) {
  125.   DEBUG();
  126.  
  127.   var obs = Cc["@mozilla.org/observer-service;1"]
  128.               .getService(Ci.nsIObserverService);
  129.  
  130.   if (topic == "songbird-library-manager-ready") {
  131.     obs.removeObserver(this, "songbird-library-manager-ready");
  132.     obs.addObserver(this, "songbird-library-manager-before-shutdown", false);
  133.  
  134.     // get the library manager
  135.     this._libraryManager = Cc['@songbirdnest.com/Songbird/library/Manager;1']
  136.                              .getService(Ci.sbILibraryManager);
  137.  
  138.     // get the main library
  139.     this._library = this._libraryManager.mainLibrary;
  140.  
  141.     // watch for added items
  142.     this._library.addListener(this, false,
  143.         Ci.sbIMediaList.LISTENER_FLAGS_ITEMADDED);
  144.  
  145.     this._helper = Cc["@songbirdnest.com/Songbird/DownloadDeviceHelper;1"]
  146.                      .getService(Ci.sbIDownloadDeviceHelper);
  147.  
  148.   } else if (topic == "songbird-library-manager-before-shutdown") {
  149.     obs.removeObserver(this, "songbird-library-manager-before-shutdown");
  150.  
  151.     if (this._library) {
  152.       this._library.removeListener(this);
  153.     }
  154.  
  155.     if (this._timer) {
  156.       this._clearTimer();
  157.     }
  158.   } else if (topic == 'timer-callback') {
  159.     while (this._queue.length) {
  160.       var item = this._queue.shift();
  161.       var playlist = this._helper.getDownloadMediaList();
  162.       if (!this._library.contains(item)) {
  163.         // it's been removed from the library
  164.         continue;
  165.       }
  166.       if (playlist.contains(item)) {
  167.         // it's already in the download playlist
  168.         continue;
  169.       }
  170.       if (item.getProperty(SBProperties.destination)) {
  171.         // it's already been processed by the download device
  172.         continue;
  173.       }
  174.       if (item.getProperty(SBProperties.disableDownload) == '1') {
  175.         // don't auto-download items who've had download disabled...
  176.         continue;
  177.       }
  178.       this._helper.downloadItem(item);
  179.     }
  180.     this._clearTimer();
  181.   }
  182. }
  183.  
  184.  
  185.   //////////////////////////
  186.  // sbIMediaListListener //
  187. //////////////////////////
  188. sbAutoDownloader.prototype.onItemAdded =
  189. function sbAutoDownloader_onItemAdded(aMediaList, aMediaItem, aIndex) {
  190.   DEBUG();
  191.   if ((aMediaItem.getProperty(SBProperties.enableAutoDownload) == "1") &&
  192.       aMediaItem.contentSrc.scheme.match(/^http/)) {
  193.     // Don't download items already in the download medialist.
  194.     if (!this._helper.getDownloadMediaList().contains(aMediaItem)) {
  195.       this._queue.push(aMediaItem);
  196.       if (!this._timer) {
  197.         this._setUpTimer();
  198.       }
  199.     }
  200.   }
  201. }
  202. sbAutoDownloader.prototype.onBeforeItemRemoved =
  203. function sbAutoDownloader_onBeforeItemRemoved(aMediaList, aMediaItem, aIndex) {
  204.   DEBUG();
  205.   return true;
  206. }
  207. sbAutoDownloader.prototype.onAfterItemRemoved =
  208. function sbAutoDownloader_onAfterItemRemoved(aMediaList, aMediaItem, aIndex) {
  209.   DEBUG();
  210.   return true;
  211. }
  212. sbAutoDownloader.prototype.onItemUpdated =
  213. function sbAutoDownloader_onItemUpdated(aMediaList, aMediaItem, aProperties) {
  214.   DEBUG();
  215.   return true;
  216. }
  217. sbAutoDownloader.prototype.onItemMoved =
  218. function sbAutoDownloader_onItemMoved(aMediaList, aFromIndex, aToIndex) {
  219.   DEBUG();
  220.   return true;
  221. }
  222. sbAutoDownloader.prototype.onBeforeListCleared =
  223. function sbAutoDownloader_onBeforeListCleared(aMediaList, aExcludeLists) {
  224.   DEBUG();
  225.   return true;
  226. }
  227. sbAutoDownloader.prototype.onListCleared =
  228. function sbAutoDownloader_onListCleared(aMediaList, aExcludeLists) {
  229.   DEBUG();
  230.   return true;
  231. }
  232. sbAutoDownloader.prototype.onBatchBegin =
  233. function sbAutoDownloader_onBatchBegin(aMediaList) {
  234.   DEBUG();
  235. }
  236. sbAutoDownloader.prototype.onBatchEnd =
  237. function sbAutoDownloader_onBatchEnd(aMediaList) {
  238.   DEBUG();
  239. }
  240.  
  241.  
  242.   /////////////////////
  243.  // Private Methods //
  244. /////////////////////
  245. sbAutoDownloader.prototype._setUpTimer =
  246. function sbAutoDownloader__setUpTimer() {
  247.   DEBUG();
  248.   if (!this._timer) {
  249.     // set up the timer
  250.     this._timer = Cc['@mozilla.org/timer;1'].createInstance(Ci.nsITimer);
  251.     this._timer.init(this, 500, Ci.nsITimer.TYPE_REPEATING_SLACK);
  252.   }
  253. }
  254. sbAutoDownloader.prototype._clearTimer =
  255. function sbAutoDownloader__clearTimer() {
  256.   DEBUG();
  257.   if (this._timer) {
  258.     this._timer.cancel();
  259.     this._timer = null;
  260.   }
  261. }
  262.  
  263.  
  264.  
  265. var NSGetModule = XPCOMUtils.generateNSGetModule(
  266.   [
  267.     sbAutoDownloader,
  268.   ],
  269.   function(aCompMgr, aFileSpec, aLocation) {
  270.     XPCOMUtils.categoryManager.addCategoryEntry(
  271.       "app-startup",
  272.       sbAutoDownloader.prototype.classDescription,
  273.       "service," + sbAutoDownloader.prototype.contractID,
  274.       true,
  275.       true);
  276.   }
  277. );
  278.