deviceCapacity.js
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  *=BEGIN SONGBIRD GPL
5  *
6  * This file is part of the Songbird web player.
7  *
8  * Copyright(c) 2005-2010 POTI, Inc.
9  * http://www.songbirdnest.com
10  *
11  * This file may be licensed under the terms of of the
12  * GNU General Public License Version 2 (the ``GPL'').
13  *
14  * Software distributed under the License is distributed
15  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied. See the GPL for the specific language
17  * governing rights and limitations.
18  *
19  * You should have received a copy of the GPL along with this
20  * program. If not, go to http://www.gnu.org/licenses/gpl.html
21  * or write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  *=END SONGBIRD GPL
25  */
26 
32 //------------------------------------------------------------------------------
33 //------------------------------------------------------------------------------
34 //
35 // Device capacity widget.
36 //
37 //------------------------------------------------------------------------------
38 //------------------------------------------------------------------------------
39 
40 //------------------------------------------------------------------------------
41 //
42 // Device capacity imported services.
43 //
44 //------------------------------------------------------------------------------
45 
46 // Component manager defs.
47 if (typeof(Cc) == "undefined")
48  var Cc = Components.classes;
49 if (typeof(Ci) == "undefined")
50  var Ci = Components.interfaces;
51 if (typeof(Cr) == "undefined")
52  var Cr = Components.results;
53 if (typeof(Cu) == "undefined")
54  var Cu = Components.utils;
55 
56 
57 //------------------------------------------------------------------------------
58 //
59 // Device capacity configuration.
60 //
61 //------------------------------------------------------------------------------
62 
63 /*
64  * updatePeriod Time period in milliseconds of UI updates.
65  */
66 
67 var DCWCfg = {
68  updatePeriod: 500
69 };
70 
71 
72 //------------------------------------------------------------------------------
73 //
74 // Device capacity services.
75 //
76 //------------------------------------------------------------------------------
77 
78 var DCW = {
79  //
80  // Device capacity object.
81  //
82  // _cfg Configuration.
83  // _widget Device capacity widget.
84  // _deviceLibrary Device library we are working with.
85  // _updateInterval Timing interval used for updating UI.
86  // _capTable Table of capacity values.
87  //
88 
89  _cfg: DCWCfg,
90  _widget: null,
91  _deviceLibrary: null,
92  _updateInterval: null,
93  _capTable: null,
94  _panel: null,
95 
96  _panelElements: [ "device-cap-bar-music-box",
97  "device-cap-bar-video-box",
98  "device-cap-bar-image-box",
99  "device-capacity-legend-music",
100  "device-capacity-legend-video",
101  "device-capacity-legend-image"
102  ],
103 
111  initialize: function DCW__initialize(aWidget) {
112  // Get the device capacity widget.
113  this._widget = aWidget;
114 
115  // Initialize object fields.
116  this._deviceLibrary = this._widget.devLib;
117  this._capTable = {};
118 
119  // Shortcuts to elements
120  this._capacityBar = this._getElement("device-capacity-bar");
121  this._capacityLegend = this._getElement("device-cap-legend-box");
122 
123  // Get the list of elements to hide.
124  var hideListAttr = this._widget.getAttribute("hidelist");
125  if (hideListAttr) {
126  var hideList = this._widget.getAttribute("hidelist").split(",");
127 
128  // Hide the specified elements.
129  for (var i = 0; i < hideList.length; i++) {
130  var element = this._getHideElement(hideList[i]);
131  element.hidden = true;
132  }
133  }
134 
135  // Hook up the panel to the right boxes
136  var panelId = this._widget.getAttribute("panel");
137  this._panel = document.getElementById(this._widget.getAttribute("panel"));
138  for each (var elId in this._panelElements) {
139  var el = this._getElement(elId);
140  if (el)
141  el.addEventListener("click", this._openCapacityPanel, false);
142  }
143 
144  // Update the UI.
145  this._update();
146 
147  // Start the update timing interval.
148  var _this = this;
149  var func = function() { _this._update(); }
150  this._updateInterval = window.setInterval(func, this._cfg.updatePeriod);
151  },
152 
153 
158  finalize: function DCW_finalize() {
159  // Clear the update timing interval.
160  if (this._updateInterval) {
161  window.clearInterval(this._updateInterval);
162  this._updateInterval = null;
163  }
164 
165  try {
166  for each (var elId in this._panelElements) {
167  var el = this._getElement(elId);
168  if (el)
169  el.removeEventListener("click", this._openCapacityPanel, false);
170  }
171  } catch (e) {
172  dump("Failed to remove event listener: " + e + "\n");
173  }
174 
175  // Clear object fields.
176  this._widget = null;
177  this._deviceLibrary = null;
178  },
179 
185  _openCapacityPanel: function DIW__openCapacityPanel(aEvent) {
186  DCW._panel.openPopup(aEvent.target);
187  },
188 
197  _getHideElement: function DIW__getHideElement(aHideID) {
198  return document.getAnonymousElementByAttribute(this._widget,
199  "hideid",
200  aHideID);
201  },
202 
203  //----------------------------------------------------------------------------
204  //
205  // Device capacity XUL services.
206  //
207  //----------------------------------------------------------------------------
208 
218  _getElement: function DCW__getElement(aElementID) {
219  return document.getAnonymousElementByAttribute(this._widget,
220  "sbid",
221  aElementID);
222  },
223 
224 
225  //----------------------------------------------------------------------------
226  //
227  // Internal device capacity services.
228  //
229  //----------------------------------------------------------------------------
230 
236  _update: function DCW__update() {
237  // Get the capacity table.
238  var capTable = this._getDeviceCapacity();
239 
240  // Check if update is needed. Do nothing more if not.
241  var needsUpdate = false;
242 
243  for (var cap in capTable) {
244  if (capTable[cap] != this._capTable[cap]) {
245  needsUpdate = true;
246  break;
247  }
248  }
249  if (!needsUpdate)
250  return;
251 
252  if (!this._deviceLibrary)
253  this._capacityBar.setAttribute("disabled", "true");
254  else
255  this._capacityBar.removeAttribute("disabled");
256 
257  // Update the capacity.
258  this._capTable = capTable;
259  if (!this._capacityBar.hidden)
260  this._updateCapBars();
261  if (!this._capacityLegend.hidden)
262  this._updateCapLegends();
263  },
264 
265 
271  _updateCapBars: function DCW__updateCapBars() {
272  // Get the total capacity.
273  var capTotal = this._capTable["total"];
274 
275  // Update each capacity bar.
276  var barBox = this._getElement("device-cap-bar-box");
277  var childList = barBox.childNodes;
278  for (var i = 0; i < childList.length; i++) {
279  // Get the next capacity bar.
280  var child = childList[i];
281  var barType = child.getAttribute("type");
282  if (!barType)
283  continue;
284 
285  // Get the capacity bar value.
286  var value = this._capTable[barType];
287  if (!value)
288  value = 0;
289  value = Math.round((value / capTotal) * 10000);
290 
291  // Update the capacity bar.
292  child.setAttribute("flex", value);
293  }
294  },
295 
296 
302  _updateCapLegends: function DCW__updateCapLegends() {
303  // Update each capacity legend.
304  var legendBox = this._getElement("device-cap-legend-box");
305  var childList = legendBox.childNodes;
306  for (var i = 0; i < childList.length; i++) {
307  // Get the next capacity legend.
308  var child = childList[i];
309  var legendType = child.getAttribute("type");
310  if (!legendType)
311  continue;
312 
313  // Get the capacity legend value.
314  var value = this._capTable[legendType];
315  if (!value)
316  value = 0;
317 
318  // Update the capacity legend.
319  if (value) {
320  let storageConverter =
321  Cc["@songbirdnest.com/Songbird/Properties/UnitConverter/Storage;1"]
322  .createInstance(Ci.sbIPropertyUnitConverter);
323  child.setAttribute("value", storageConverter.autoFormat(value, -1, 1));
324  child.hidden = false;
325  }
326  else {
327  child.hidden = true;
328  }
329  }
330  },
331 
332 
333  //----------------------------------------------------------------------------
334  //
335  // Device capacity device services.
336  //
337  //----------------------------------------------------------------------------
338 
350  _getDevLibProperty: function DIW__getDevLibProperty(aPropertyName, aDefault) {
351  try {
352  if (this._deviceLibrary)
353  return this._deviceLibrary.getProperty(aPropertyName);
354  } catch (ex) { }
355  return aDefault;
356  },
357 
369  _getDeviceCapacity: function DCW__getDeviceCapacity() {
370  // Get the storage statistics from the device.
371  var freeSpace = parseInt(this._getDevLibProperty
372  ("http://songbirdnest.com/device/1.0#freeSpace", 0));
373  var musicSpace = parseInt(this._getDevLibProperty
374  ("http://songbirdnest.com/device/1.0#musicUsedSpace", 0));
375  var videoSpace = parseInt(this._getDevLibProperty
376  ("http://songbirdnest.com/device/1.0#videoUsedSpace", 0));
377  var imageSpace = parseInt(this._getDevLibProperty
378  ("http://songbirdnest.com/device/1.0#imageUsedSpace", 0));
379  var usedSpace = parseInt(this._getDevLibProperty
380  ("http://songbirdnest.com/device/1.0#totalUsedSpace", 0));
381  var totalSpace = usedSpace + freeSpace;
382  var otherSpace = usedSpace - musicSpace - videoSpace - imageSpace;
383 
384  // Set up the device capacity table.
385  var capTable = {};
386  capTable.total = Math.max(0, totalSpace);
387  capTable.free = Math.max(0, freeSpace);
388  capTable.music = Math.max(0, musicSpace);
389  capTable.video = Math.max(0, videoSpace);
390  capTable.image = Math.max(0, imageSpace);
391  capTable.other = Math.max(0, otherSpace);
392  return capTable;
393  }
394 };
395 
const Cu
function Fx prototype initialize
const Cc
var DCWCfg
let window
var _this
var DCW
return null
Definition: FeedWriter.js:1143
countRef value
Definition: FeedWriter.js:1423
const Cr
const Ci
_getSelectedPageStyle s i