SBUtils.jsm
Go to the documentation of this file.
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 
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 = {
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 
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 
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 
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.
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 
206  hasMoreElements: function SBFilteredEnumerator_hasMoreElements() {
207  if (this._getNext())
208  return true;
209  },
210 
211 
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 
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 
GeneratorThread currentThread
const Ci
Definition: SBUtils.jsm:50
function SBFilteredEnumerator(aEnumerator, aFilterFunc)
Definition: SBUtils.jsm:163
var Application
Definition: sbAboutDRM.js:37
const Cc
Definition: SBUtils.jsm:49
const Cu
Definition: SBUtils.jsm:52
sbOSDControlService prototype QueryInterface
var SBUtils
Definition: SBUtils.jsm:71
DataRemote prototype constructor
return null
Definition: FeedWriter.js:1143
const Cr
Definition: SBUtils.jsm:51
SimpleArrayEnumerator prototype hasMoreElements
EXPORTED_SYMBOLS
Definition: SBUtils.jsm:40