deviceMgmtTabs.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 
31 //------------------------------------------------------------------------------
32 //------------------------------------------------------------------------------
33 //
34 // Device sync tabs widget.
35 //
36 //------------------------------------------------------------------------------
37 //------------------------------------------------------------------------------
38 
39 //------------------------------------------------------------------------------
40 //
41 // Device sync tabs defs.
42 //
43 //------------------------------------------------------------------------------
44 
45 // Component manager defs.
46 if (typeof(Cc) == "undefined")
47  var Cc = Components.classes;
48 if (typeof(Ci) == "undefined")
49  var Ci = Components.interfaces;
50 if (typeof(Cr) == "undefined")
51  var Cr = Components.results;
52 if (typeof(Cu) == "undefined")
53  var Cu = Components.utils;
54 
55 Cu.import("resource://app/jsmodules/sbLibraryUtils.jsm");
56 Cu.import("resource://app/jsmodules/sbProperties.jsm");
57 Cu.import("resource://app/jsmodules/StringUtils.jsm");
58 Cu.import("resource://app/jsmodules/SBUtils.jsm");
59 
60 if (typeof(XUL_NS) == "undefined")
61  var XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
62 
64  //
65  // Device Sync Tabs object fields.
66  //
67  // _widget Device sync tabs widget.
68  // _deviceID Device ID.
69  // _device sbIDevice object.
70  //
71 
72  _widget: null,
73  _deviceID: null,
74  _device: null,
75 
83  initialize: function DeviceMgmtTabs_initialize(aWidget) {
84  // Get the device widget.
85  this._widget = aWidget;
86 
87  // Initialize object fields.
88  this._deviceID = this._widget.deviceID;
89  this._device = this._widget.device;
90 
91  var toolsBox = this._getElement("device_tools");
92  var settingsBox = this._getElement("device_summary_settings");
93  var tabBox = this._getElement("device_management_tabbox");
94  if (toolsBox)
95  toolsBox.addEventListener("DOMAttrModified", this.tabListener, false);
96  if (settingsBox)
97  settingsBox.addEventListener("DOMAttrModified", this.tabListener, false);
98  if (tabBox)
99  tabBox.addEventListener("DOMAttrModified", this.tabListener, false);
100 
101  this._update();
102 
103  // Add device listener
104  if (this._device) {
105  var deviceEventTarget = this._device;
106  deviceEventTarget.QueryInterface(Ci.sbIDeviceEventTarget);
107  deviceEventTarget.addEventListener(this);
108  }
109  },
110 
111  tabListener: function DeviceMgmtTabs_tabDisableListener(event) {
112  switch (event.attrName) {
113  case "disabledTab":
114  var target = event.target;
115  var id = target.getAttribute("sbid");
116  if (event.newValue == "true") {
117  if (id == "device_tools") {
118  DeviceMgmtTabs._widget.setAttribute("hideToolsTab", "true");
119  } else if (id == "device_summary_settings") {
120  DeviceMgmtTabs._widget.setAttribute("hideSettingsTab", "true");
121  }
122  } else {
123  if (id == "device_tools") {
124  DeviceMgmtTabs._widget.removeAttribute("hideToolsTab");
125  } else if (id == "device_summary_settings") {
126  DeviceMgmtTabs._widget.removeAttribute("hideSettingsTab");
127  }
128  }
129  break;
130 
131  case "selectedIndex":
132  if (event.newValue == "2") {
133  DeviceMgmtTabs._dispatchImageTabEvent();
134  }
135  break;
136  }
137  },
138 
139  _update: function DeviceMgmtTabs_update() {
140  // Check what content this device supports then hide tabs for unsupported
141  // content types.
142  var sbIDC = Ci.sbIDeviceCapabilities;
143  // Check Audio
144  if (!this._deviceSupportsContent(sbIDC.CONTENT_AUDIO,
145  sbIDC.FUNCTION_AUDIO_PLAYBACK)) {
146  this._toggleTab("music", true);
147  }
148 
149  // Check Video
150  if (!this._deviceSupportsContent(sbIDC.CONTENT_VIDEO,
151  sbIDC.FUNCTION_VIDEO_PLAYBACK)) {
152  this._toggleTab("video", true);
153  }
154 
155  // Check Images
156  if (!this._deviceSupportsContent(sbIDC.CONTENT_IMAGE,
157  sbIDC.FUNCTION_IMAGE_DISPLAY)) {
158  this._toggleTab("image", true);
159  }
160  },
161 
166  finalize: function DeviceMgmtTabs_finalize() {
167  if (this._device) {
168  var deviceEventTarget = this._device;
169  deviceEventTarget.QueryInterface(Ci.sbIDeviceEventTarget);
170  deviceEventTarget.removeEventListener(this);
171  }
172 
173  var toolsBox = this._getElement("device_tools");
174  var settingsBox = this._getElement("device_summary_settings");
175  if (toolsBox)
176  toolsBox.removeEventListener("DOMAttrModified", this.tabListener, false);
177  if (settingsBox)
178  settingsBox.removeEventListener("DOMAttrModified",
179  this.tabListener,
180  false);
181 
182  this._device = null;
183  this._deviceID = null;
184  this._widget = null;
185  },
186 
187  onDeviceEvent: function DeviceMgmtTabs_onDeviceEvent(aEvent) {
188  switch (aEvent.type) {
189  case Ci.sbIDeviceEvent.EVENT_DEVICE_MEDIA_INSERTED:
190  case Ci.sbIDeviceEvent.EVENT_DEVICE_MEDIA_REMOVED:
191  case Ci.sbIDeviceEvent.EVENT_DEVICE_MOUNTING_END:
192  this._update();
193  break;
194  }
195  },
196 
201  _dispatchImageTabEvent: function DeviceMgmtTabs__dispatchImageTabEvent() {
202  let event = document.createEvent("UIEvents");
203  event.initUIEvent("image-tab-selected", false, false, window, null);
204  document.dispatchEvent(event);
205  },
206 
214  _checkFunctionSupport:
215  function DeviceMgmtTabs__checkFunctionSupport(aFunctionType) {
216  try {
217  var functionTypes = this._device
218  .capabilities
219  .getSupportedFunctionTypes({});
220  for (var i in functionTypes) {
221  if (functionTypes[i] == aFunctionType) {
222  return true;
223  }
224  }
225  }
226  catch (err) {
227  var strFunctionType;
228  switch (aFunctionType) {
229  case Ci.sbIDeviceCapabilities.FUNCTION_DEVICE:
230  strFunctionType = "Device";
231  break;
232  case Ci.sbIDeviceCapabilities.FUNCTION_AUDIO_PLAYBACK:
233  strFunctionType = "Audio Playback";
234  break;
235  case Ci.sbIDeviceCapabilities.FUNCTION_AUDIO_CAPTURE:
236  strFunctionType = "Audio Capture";
237  break;
238  case Ci.sbIDeviceCapabilities.FUNCTION_IMAGE_DISPLAY:
239  strFunctionType = "Image Display";
240  break;
241  case Ci.sbIDeviceCapabilities.FUNCTION_IMAGE_CAPTURE:
242  strFunctionType = "Image Capture";
243  break;
244  case Ci.sbIDeviceCapabilities.FUNCTION_VIDEO_PLAYBACK:
245  strFunctionType = "Video Playback";
246  break;
247  case Ci.sbIDeviceCapabilities.FUNCTION_VIDEO_CAPTURE:
248  strFunctionType = "Video Capture";
249  break;
250  default:
251  strFunctionType = "Unknown";
252  break;
253  }
254  Cu.reportError("Unable to determine if device supports function type: "+
255  strFunctionType + " [" + err + "]");
256  }
257 
258  return false;
259  },
260 
270  _deviceSupportsContent:
271  function DeviceMgmtTabs__deviceSupportsContent(aContentType,
272  aFunctionType) {
273  // First check if the device supports the function
274  if (!this._checkFunctionSupport(aFunctionType)) {
275  return false;
276  }
277 
278  // lone> the next statement is not good, instead all relevant device
279  // interfaces should report image support as needed: some devices currently
280  // do not report image support even though they do support images, and some
281  // devices will never report support because the api (ie, MSC) doesn't allow
282  // us to ask; any MSC device has potential support for anything whatsoever,
283  // since it appears as a USB drive. maybe it only means than MSC should
284  // always support everything, but why not MTP then ? In fact, it's an open
285  // question as to whether we should use this function to determine tab
286  // visibility at all because even if a device does not support images (or
287  // videos), you could still want to use it as syncable portable storage for
288  // the files. The same argument can be made of tabs which are always visible
289  // (ie, music): think of a USB key that plugs into a car stereo, it'll never
290  // be reported as supporting music, yet it's just the portable storage used
291  // by the user to play on a device that does support playback, so it's a
292  // good thing we show the tab even though the physical device reports no
293  // audio support.
294  var sbIDC = Ci.sbIDeviceCapabilities;
295  if (aContentType == sbIDC.CONTENT_IMAGE)
296  return true;
297 
298  // Now check if the device supports the content type in that function.
299  if (aFunctionType >= 0) {
300  try {
301  var contentTypes = this._device
302  .capabilities
303  .getSupportedContentTypes(aFunctionType, {});
304  for (var i in contentTypes) {
305  if (contentTypes[i] == aContentType) {
306  return true;
307  }
308  }
309  }
310  catch (err) {
311  var strContentType;
312  switch (aContentType) {
313  case sbIDC.CONTENT_AUDIO:
314  strContentType = "Audio";
315  break;
316  case sbIDC.CONTENT_VIDEO:
317  strContentType = "Video";
318  break;
319  case sbIDC.CONTENT_IMAGE:
320  strContentType = "Image";
321  break;
322  default:
323  strContentType = "Unknown";
324  break;
325  }
326  Cu.reportError("Unable to determine if device supports content: "+
327  strContentType + " [" + err + "]");
328  }
329  }
330 
331  return false;
332  },
333 
343  _toggleTab: function DeviceMgmtTabs__toggleTab(aTabType, shouldHide) {
344  var tab = this._getElement("device_management_" + aTabType + "_sync_tab");
345 
346  if (shouldHide) {
347  tab.setAttribute("hidden", true);
348  }
349  else {
350  tab.removeAttribute("hidden");
351  }
352  },
353 
354  //----------------------------------------------------------------------------
355  //
356  // Device sync tabs XUL services.
357  //
358  //----------------------------------------------------------------------------
359 
369  _getElement: function DeviceMgmtTabs__getElement(aElementID) {
370  return document.getAnonymousElementByAttribute(this._widget,
371  "sbid",
372  aElementID);
373  }
374 };
const Cu
function Fx prototype initialize
const Cc
const XUL_NS
Definition: FeedWriter.js:83
var event
var tab
let window
return null
Definition: FeedWriter.js:1143
var DeviceMgmtTabs
const Cr
const Ci
_getSelectedPageStyle s i