home *** CD-ROM | disk | FTP | other *** search
/ Freelog 112 / FreelogNo112-NovembreDecembre2012.iso / Multimedia / Songbird / Songbird_2.0.0-2311_windows-i686-msvc8.exe / jsmodules / SBUtils.jsm < prev    next >
Text File  |  2012-06-08  |  9KB  |  279 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim: set sw=2 :miv */
  3. /*
  4. //
  5. // BEGIN SONGBIRD GPL
  6. //
  7. // This file is part of the Songbird web player.
  8. //
  9. // Copyright(c) 2005-2009 POTI, Inc.
  10. // http://songbirdnest.com
  11. //
  12. // This file may be licensed under the terms of of the
  13. // GNU General Public License Version 2 (the "GPL").
  14. //
  15. // Software distributed under the License is distributed
  16. // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
  17. // express or implied. See the GPL for the specific language
  18. // governing rights and limitations.
  19. //
  20. // You should have received a copy of the GPL along with this
  21. // program. If not, go to http://www.gnu.org/licenses/gpl.html
  22. // or write to the Free Software Foundation, Inc.,
  23. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  24. //
  25. // END SONGBIRD GPL
  26. //
  27. */
  28.  
  29. /**
  30.  * \file  SBUtils.jsm
  31.  * \brief Javascript source for the general Songbird utilities.
  32.  */
  33.  
  34. //------------------------------------------------------------------------------
  35. //
  36. // Songbird utilities configuration.
  37. //
  38. //------------------------------------------------------------------------------
  39.  
  40. EXPORTED_SYMBOLS = [ "SBUtils", "SBFilteredEnumerator" ];
  41.  
  42.  
  43. //------------------------------------------------------------------------------
  44. //
  45. // Songbird utilities defs.
  46. //
  47. //------------------------------------------------------------------------------
  48.  
  49. const Cc = Components.classes;
  50. const Ci = Components.interfaces;
  51. const Cr = Components.results;
  52. const Cu = Components.utils;
  53.  
  54.  
  55. //------------------------------------------------------------------------------
  56. //
  57. // Songbird utilities imports.
  58. //
  59. //------------------------------------------------------------------------------
  60.  
  61. // Mozilla imports.
  62. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  63.  
  64.  
  65. //------------------------------------------------------------------------------
  66. //
  67. // Songbird utility services.
  68. //
  69. //------------------------------------------------------------------------------
  70.  
  71. var SBUtils = {
  72.   /**
  73.    * Return true if the first-run has completed.
  74.    *
  75.    * \return                    True if first-run has completed.
  76.    */
  77.  
  78.   hasFirstRunCompleted: function SBUtils_hasFirstRunCompleted() {
  79.     // Read the first-run has completed preference.
  80.     var Application = Cc["@mozilla.org/fuel/application;1"]
  81.                         .getService(Ci.fuelIApplication);
  82.     var hasCompleted = Application.prefs.getValue("songbird.firstrun.check.0.3",
  83.                                                   false);
  84.  
  85.     return hasCompleted;
  86.   },
  87.  
  88.  
  89.   /**
  90.    *   Defer invocation of the function specified by aFunction by queueing an
  91.    * invocation event on the current thread event queue.
  92.    *   Use deferFunction instead of a zero-delay timer for deferring function
  93.    * invocation.
  94.    *
  95.    * \param aFunction           Function for which to defer invocation.
  96.    */
  97.  
  98.   deferFunction: function SBUtils_deferFunction(aFunction) {
  99.     // Get the current thread.
  100.     var threadManager = Cc["@mozilla.org/thread-manager;1"]
  101.                             .getService(Ci.nsIThreadManager);
  102.     var currentThread = threadManager.currentThread;
  103.  
  104.     // Queue an event to call the function.
  105.     var runnable = { run: aFunction };
  106.     currentThread.dispatch(runnable, Ci.nsIEventTarget.DISPATCH_NORMAL);
  107.   },
  108.  
  109.  
  110.   /**
  111.    * Set the text for the XUL description element specified by aDescriptionElem
  112.    * to the text specified by aText.
  113.    *
  114.    * \param aDescriptionElem    XUL description element for which to set text.
  115.    * \param aText               Text to set for XUL description element.
  116.    */
  117.  
  118.   setDescriptionText: function SBUtils_setDescriptionText(aDescriptionElem,
  119.                                                           aText) {
  120.     // Remove all description element children.
  121.     while (aDescriptionElem.firstChild)
  122.       aDescriptionElem.removeChild(aDescriptionElem.firstChild);
  123.  
  124.     // Add a text node with the text to the description element.
  125.     var textNode = aDescriptionElem.ownerDocument.createTextNode(aText);
  126.     aDescriptionElem.appendChild(textNode);
  127.   }
  128. };
  129.  
  130.  
  131. //------------------------------------------------------------------------------
  132. //
  133. // Filtered enumerator services.
  134. //
  135. //   These services provide support for adding a filter to an nsIEnumerator.
  136. //
  137. // Example:
  138. //
  139. // // Return an enumerator that contains all even elements of aEnumerator.
  140. // function GetEvenEnumerator(aEnumerator) {
  141. //   var elementCount = 0;
  142. //   var func = function(aElement) {
  143. //     var isEven = (elementCount % 2) == 0 ? true : false;
  144. //     elementCount++;
  145. //     return isEven;
  146. //   }
  147. //
  148. //   return new SBFilteredEnumerator(aEnumerator, func);
  149. // }
  150. //
  151. //------------------------------------------------------------------------------
  152.  
  153. /**
  154.  * Construct a filtered enumerator for the enumerator specified by aEnumerator
  155.  * using the filter function specified by aFilterFunc.  The filter function is
  156.  * provided an element from the base enumerator and should return true if the
  157.  * element should not be filtered out.
  158.  *
  159.  * \param aEnumerator           Base enumerator to filter.
  160.  * \param aFilterFunc           Filter function.
  161.  */
  162.  
  163. function SBFilteredEnumerator(aEnumerator, aFilterFunc)
  164. {
  165.   this._enumerator = aEnumerator;
  166.   this._filterFunc = aFilterFunc;
  167. }
  168.  
  169. // Define to class.
  170. SBFilteredEnumerator.prototype = {
  171.   // Set the constructor.
  172.   constructor: SBFilteredEnumerator,
  173.  
  174.   //
  175.   // Internal object fields.
  176.   //
  177.   //   _enumerator              Base enumerator.
  178.   //   _filterFunc              Filter function to apply to base enumerator.
  179.   //   _nextElement             Next available element.
  180.   //
  181.  
  182.   _enumerator: null,
  183.   _filterFunc: null,
  184.   _nextElement: null,
  185.  
  186.  
  187.   //----------------------------------------------------------------------------
  188.   //
  189.   // nsISimpleEnumerator services.
  190.   //
  191.   //----------------------------------------------------------------------------
  192.  
  193.   /**
  194.    * Called to determine whether or not the enumerator has
  195.    * any elements that can be returned via getNext(). This method
  196.    * is generally used to determine whether or not to initiate or
  197.    * continue iteration over the enumerator, though it can be
  198.    * called without subsequent getNext() calls. Does not affect
  199.    * internal state of enumerator.
  200.    *
  201.    * @see getNext()
  202.    * @return PR_TRUE if there are remaining elements in the enumerator.
  203.    *         PR_FALSE if there are no more elements in the enumerator.
  204.    */
  205.  
  206.   hasMoreElements: function SBFilteredEnumerator_hasMoreElements() {
  207.     if (this._getNext())
  208.       return true;
  209.   },
  210.  
  211.  
  212.   /**
  213.    * Called to retrieve the next element in the enumerator. The "next"
  214.    * element is the first element upon the first call. Must be
  215.    * pre-ceeded by a call to hasMoreElements() which returns PR_TRUE.
  216.    * This method is generally called within a loop to iterate over
  217.    * the elements in the enumerator.
  218.    *
  219.    * @see hasMoreElements()
  220.    * @return NS_OK if the call succeeded in returning a non-null
  221.    *               value through the out parameter.
  222.    *         NS_ERROR_FAILURE if there are no more elements
  223.    *                          to enumerate.
  224.    * @return the next element in the enumeration.
  225.    */
  226.  
  227.   getNext: function SBFilteredEnumerator_getNext() {
  228.     // Get the next element.  Throw an exception if none is available.
  229.     var nextElement = this._getNext();
  230.     if (!nextElement)
  231.       throw Components.results.NS_ERROR_FAILURE;
  232.  
  233.     // Consume the next element and return it.
  234.     this._nextElement = null;
  235.     return nextElement;
  236.   },
  237.  
  238.  
  239.   //----------------------------------------------------------------------------
  240.   //
  241.   // nsISupports services.
  242.   //
  243.   //----------------------------------------------------------------------------
  244.  
  245.   QueryInterface: XPCOMUtils.generateQI([ Ci.nsISimpleEnumerator ]),
  246.  
  247.  
  248.   //----------------------------------------------------------------------------
  249.   //
  250.   // Internal services.
  251.   //
  252.   //----------------------------------------------------------------------------
  253.  
  254.   /**
  255.    * Get and return the next element that passes the filter.  Store the element
  256.    * internally and continue returning it until it's consumed.
  257.    *
  258.    * \return                    Next available element.
  259.    */
  260.  
  261.   _getNext: function SBFilteredEnumerator__getNext() {
  262.     // If next element is already available, simply return it.
  263.     if (this._nextElement)
  264.       return this._nextElement;
  265.  
  266.     // Get the next element that passes the filter.
  267.     while (this._enumerator.hasMoreElements()) {
  268.       var nextElement = this._enumerator.getNext();
  269.       if (this._filterFunc(nextElement)) {
  270.         this._nextElement = nextElement;
  271.         break;
  272.       }
  273.     }
  274.  
  275.     return this._nextElement;
  276.   }
  277. }
  278.  
  279.