sbDefaultBaseDeviceInfoRegistrar.cpp
Go to the documentation of this file.
1 /* -*- Mode: C++; 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 
28 
29 // Mozilla includes
30 #include <nsArrayUtils.h>
31 #include <nsISupportsPrimitives.h>
32 #include <nsIVariant.h>
33 #include <nsIWritablePropertyBag2.h>
34 #include <nsMemory.h>
35 #include <prlog.h>
36 
37 // Songbird includes
39 #include <sbIDevice.h>
40 #include <sbIDeviceCapabilities.h>
41 #include <sbIDeviceEvent.h>
42 #include <sbIDeviceEventTarget.h>
43 #include <sbIDeviceManager.h>
44 #include <sbIMediaItem.h>
45 #include <sbITranscodeManager.h>
47 #include <sbStandardProperties.h>
48 #include <sbStringUtils.h>
49 #include <sbVariantUtils.h>
50 
55 #ifdef PR_LOGGING
56 static PRLogModuleInfo* gDefaultBaseDeviceInfoRegistrarLog = nsnull;
57 #define TRACE(args) PR_LOG(gDefaultBaseDeviceInfoRegistrarLog , PR_LOG_DEBUG, args)
58 #define LOG(args) PR_LOG(gDefaultBaseDeviceInfoRegistrarLog , PR_LOG_WARN, args)
59 #ifdef __GNUC__
60 #define __FUNCTION__ __PRETTY_FUNCTION__
61 #endif /* __GNUC__ */
62 #else
63 #define TRACE(args) /* nothing */
64 #define LOG(args) /* nothing */
65 #endif
66 
68  mDevice(nsnull),
69  mDeviceXMLInfoPresent(PR_FALSE)
70 {
71  #ifdef PR_LOGGING
72  if (!gDefaultBaseDeviceInfoRegistrarLog)
73  gDefaultBaseDeviceInfoRegistrarLog =
74  PR_NewLogModule("sbDefaultBaseDeviceInfoRegistrar");
75  #endif
76 }
77 
79 {
80 }
81 
82 /* readonly attribute PRUint32 type; */
83 NS_IMETHODIMP
84 sbDefaultBaseDeviceInfoRegistrar::GetType(PRUint32 *aType)
85 {
86  TRACE(("%s: default", __FUNCTION__));
87  NS_ENSURE_ARG_POINTER(aType);
88 
90  return NS_OK;
91 }
92 
93 NS_IMETHODIMP
94 sbDefaultBaseDeviceInfoRegistrar::
95  AddCapabilities(sbIDevice *aDevice,
96  sbIDeviceCapabilities *aCapabilities) {
97  TRACE(("%s", __FUNCTION__));
98  NS_ENSURE_ARG_POINTER(aDevice);
99  NS_ENSURE_ARG_POINTER(aCapabilities);
100 
101  nsresult rv;
102 
103  // Look for capabilities settings in the device preferences
104  nsCOMPtr<nsIVariant> capabilitiesVariant;
105  rv = aDevice->GetPreference(NS_LITERAL_STRING("capabilities"),
106  getter_AddRefs(capabilitiesVariant));
107  if (NS_SUCCEEDED(rv)) {
108  PRUint16 dataType;
109  rv = capabilitiesVariant->GetDataType(&dataType);
110  NS_ENSURE_SUCCESS(rv, rv);
111 
112  if ((dataType == nsIDataType::VTYPE_INTERFACE) ||
113  (dataType == nsIDataType::VTYPE_INTERFACE_IS)) {
114  nsCOMPtr<nsISupports> capabilitiesISupports;
115  nsCOMPtr<sbIDeviceCapabilities> capabilities;
116  rv = capabilitiesVariant->GetAsISupports
117  (getter_AddRefs(capabilitiesISupports));
118  NS_ENSURE_SUCCESS(rv, rv);
119  capabilities = do_QueryInterface(capabilitiesISupports, &rv);
120  NS_ENSURE_SUCCESS(rv, rv);
121  rv = aCapabilities->AddCapabilities(capabilities);
122  NS_ENSURE_SUCCESS(rv, rv);
123  return NS_OK;
124  }
125  }
126 
127  // Add capabilities from the default device info document.
128  sbDeviceXMLInfo* deviceXMLInfo;
129  rv = GetDeviceXMLInfo(aDevice, &deviceXMLInfo);
130  NS_ENSURE_SUCCESS(rv, rv);
131  if (deviceXMLInfo) {
132  // Get the device info element.
133  nsCOMPtr<nsIDOMElement> deviceInfoElement;
134  rv = deviceXMLInfo->GetDeviceInfoElement(getter_AddRefs(deviceInfoElement));
135  NS_ENSURE_SUCCESS(rv, rv);
136 
137  // Add capabilities from the device info element.
138  if (deviceInfoElement) {
139  PRBool addedCapabilities;
140  rv = sbDeviceXMLCapabilities::AddCapabilities(aCapabilities,
141  deviceInfoElement,
142  &addedCapabilities,
143  aDevice);
144  NS_ENSURE_SUCCESS(rv, rv);
145  }
146  }
147 
148  return NS_OK;
149 }
150 
151 NS_IMETHODIMP
152 sbDefaultBaseDeviceInfoRegistrar::GetDefaultName(sbIDevice* aDevice,
153  nsAString& retval)
154 {
155  TRACE(("%s", __FUNCTION__));
156 
157  nsresult rv;
158 
159  // Default to no name.
160  retval.Truncate();
161 
162  // Get the device XML info. Just return if none available.
163  sbDeviceXMLInfo* deviceXMLInfo;
164  rv = GetDeviceXMLInfo(aDevice, &deviceXMLInfo);
165  NS_ENSURE_SUCCESS(rv, rv);
166  if (!deviceXMLInfo)
167  return NS_OK;
168 
169  // Get the device name.
170  rv = deviceXMLInfo->GetDefaultName(retval);
171  NS_ENSURE_SUCCESS(rv, rv);
172 
173  return NS_OK;
174 }
175 
176 NS_IMETHODIMP
177 sbDefaultBaseDeviceInfoRegistrar::GetDeviceFolder(sbIDevice* aDevice,
178  PRUint32 aContentType,
179  nsAString& retval)
180 {
181  TRACE(("%s", __FUNCTION__));
182 
183  nsresult rv;
184 
185  // Default to no folder.
186  retval.Truncate();
187 
188  // Get the device XML info. Just return if none available.
189  sbDeviceXMLInfo* deviceXMLInfo;
190  rv = GetDeviceXMLInfo(aDevice, &deviceXMLInfo);
191  NS_ENSURE_SUCCESS(rv, rv);
192  if (!deviceXMLInfo)
193  return NS_OK;
194 
195  // Get the device folder.
196  rv = deviceXMLInfo->GetDeviceFolder(aContentType, retval);
197  NS_ENSURE_SUCCESS(rv, rv);
198 
199  return NS_OK;
200 }
201 
202 NS_IMETHODIMP
203 sbDefaultBaseDeviceInfoRegistrar::GetImportRules(
204  sbIDevice * aDevice,
205  nsIArray ** _retval NS_OUTPARAM)
206 {
207  TRACE(("%s", __FUNCTION__));
208 
209  NS_ENSURE_ARG_POINTER(_retval);
210 
211  nsresult rv;
212 
213  // Get the device XML info. Just return if none available.
214  sbDeviceXMLInfo* deviceXMLInfo;
215  rv = GetDeviceXMLInfo(aDevice, &deviceXMLInfo);
216  NS_ENSURE_SUCCESS(rv, rv);
217  if (!deviceXMLInfo)
218  return NS_OK;
219 
220  // Get the rules.
221  rv = deviceXMLInfo->GetImportRules(_retval);
222  NS_ENSURE_SUCCESS(rv, rv);
223 
224  return NS_OK;
225 }
226 
227 NS_IMETHODIMP
228 sbDefaultBaseDeviceInfoRegistrar::GetMountTimeout(sbIDevice* aDevice,
229  PRUint32* retval)
230 {
231  TRACE(("%s", __FUNCTION__));
232 
233  NS_ENSURE_ARG_POINTER(aDevice);
234  NS_ENSURE_ARG_POINTER(retval);
235 
236  nsresult rv;
237 
238  // Get the device XML info and check if it's available.
239  sbDeviceXMLInfo* deviceXMLInfo;
240  rv = GetDeviceXMLInfo(aDevice, &deviceXMLInfo);
241  NS_ENSURE_SUCCESS(rv, rv);
242  if (!deviceXMLInfo)
243  return NS_ERROR_NOT_AVAILABLE;
244 
245  // Get the mount timeout value.
246  rv = deviceXMLInfo->GetMountTimeout(retval);
247  if (rv == NS_ERROR_NOT_AVAILABLE)
248  return NS_ERROR_NOT_AVAILABLE;
249  NS_ENSURE_SUCCESS(rv, rv);
250 
251  return NS_OK;
252 }
253 
254 NS_IMETHODIMP
255 sbDefaultBaseDeviceInfoRegistrar::GetExcludedFolders(sbIDevice * aDevice,
256  nsAString & aFolders)
257 {
258  TRACE(("%s", __FUNCTION__));
259 
260  nsresult rv;
261 
262  aFolders.Truncate();
263  // Get the device XML info and check if it's available.
264  sbDeviceXMLInfo* deviceXMLInfo;
265  rv = GetDeviceXMLInfo(aDevice, &deviceXMLInfo);
266  NS_ENSURE_SUCCESS(rv, rv);
267  if (!deviceXMLInfo)
268  return NS_OK;
269 
270  rv = deviceXMLInfo->GetExcludedFolders(aFolders);
271  NS_ENSURE_SUCCESS(rv, rv);
272 
273  return NS_OK;
274 }
275 
276 NS_IMETHODIMP
277 sbDefaultBaseDeviceInfoRegistrar::GetDoesDeviceSupportReformat(
278  sbIDevice *aDevice,
279  PRBool *aOutSupportsReformat)
280 {
281  NS_ENSURE_ARG_POINTER(aDevice);
282  NS_ENSURE_ARG_POINTER(aOutSupportsReformat);
283 
284  // Get the device XML info and check if it's available.
285  sbDeviceXMLInfo* deviceXMLInfo;
286  nsresult rv = GetDeviceXMLInfo(aDevice, &deviceXMLInfo);
287  NS_ENSURE_SUCCESS(rv, rv);
288  if (!deviceXMLInfo)
289  return NS_OK;
290 
291  rv = deviceXMLInfo->GetDoesDeviceSupportReformat(aOutSupportsReformat);
292  NS_ENSURE_SUCCESS(rv, rv);
293 
294  return NS_OK;
295 }
296 
297 NS_IMETHODIMP
298 sbDefaultBaseDeviceInfoRegistrar::GetOnlyMountMediaFolders
299  (sbIDevice *aDevice,
300  PRBool *aOnlyMountMediaFolders)
301 {
302  TRACE(("%s", __FUNCTION__));
303 
304  NS_ENSURE_ARG_POINTER(aDevice);
305  NS_ENSURE_ARG_POINTER(aOnlyMountMediaFolders);
306 
307  // Get the device XML info and check if it's available.
308  sbDeviceXMLInfo* deviceXMLInfo;
309  nsresult rv = GetDeviceXMLInfo(aDevice, &deviceXMLInfo);
310  NS_ENSURE_SUCCESS(rv, rv);
311  if (!deviceXMLInfo)
312  return NS_OK;
313 
314  // Get the only mount media folders value.
315  rv = deviceXMLInfo->GetOnlyMountMediaFolders(aOnlyMountMediaFolders);
316  NS_ENSURE_SUCCESS(rv, rv);
317 
318  return NS_OK;
319 }
320 
321 NS_IMETHODIMP
322 sbDefaultBaseDeviceInfoRegistrar::GetStorageDeviceInfoList
323  (sbIDevice* aDevice,
324  nsIArray** retval)
325 {
326  TRACE(("%s", __FUNCTION__));
327 
328  NS_ENSURE_ARG_POINTER(aDevice);
329  NS_ENSURE_ARG_POINTER(retval);
330 
331  nsresult rv;
332 
333  // Get the device XML info and check if it's available.
334  sbDeviceXMLInfo* deviceXMLInfo;
335  rv = GetDeviceXMLInfo(aDevice, &deviceXMLInfo);
336  NS_ENSURE_SUCCESS(rv, rv);
337  if (!deviceXMLInfo)
338  return NS_ERROR_NOT_AVAILABLE;
339 
340  // Get the storage device info.
341  rv = deviceXMLInfo->GetStorageDeviceInfoList(retval);
342  if (rv == NS_ERROR_NOT_AVAILABLE)
343  return NS_ERROR_NOT_AVAILABLE;
344  NS_ENSURE_SUCCESS(rv, rv);
345 
346  return NS_OK;
347 }
348 
349 NS_IMETHODIMP
350 sbDefaultBaseDeviceInfoRegistrar::GetDeviceIcon(sbIDevice* aDevice,
351  nsAString& retval)
352 {
353  TRACE(("%s", __FUNCTION__));
354 
355  // Validate arguments.
356  NS_ENSURE_ARG_POINTER(aDevice);
357 
358  // function variables.
359  nsresult rv;
360 
361  // Default to no device icon.
362  retval.Truncate();
363 
364  // Get the device XML info. Just return if none available.
365  sbDeviceXMLInfo* deviceXMLInfo;
366  rv = GetDeviceXMLInfo(aDevice, &deviceXMLInfo);
367  NS_ENSURE_SUCCESS(rv, rv);
368  if (!deviceXMLInfo)
369  return NS_OK;
370 
371  // Get the device icon.
372  rv = deviceXMLInfo->GetDeviceIcon(retval);
373  NS_ENSURE_SUCCESS(rv, rv);
374 
375  return NS_OK;
376 }
377 
378 NS_IMETHODIMP
379 sbDefaultBaseDeviceInfoRegistrar::InterestedInDevice(sbIDevice *aDevice,
380  PRBool *retval)
381 {
382  NS_ENSURE_ARG_POINTER(aDevice);
383  NS_ENSURE_ARG_POINTER(retval);
384 
385  nsresult rv;
386  *retval = PR_FALSE;
387 
388  // Peek in the device XML file to see if the caps are there. Just return if
389  // no device XML file has been specified.
390  nsCString xmlInfoSpec;
391  rv = GetDeviceXMLInfoSpec(xmlInfoSpec);
392  NS_ENSURE_SUCCESS(rv, rv);
393  if (xmlInfoSpec.IsEmpty())
394  return NS_OK;
395 
396  nsAutoPtr<sbDeviceXMLInfo> xmlInfo(new sbDeviceXMLInfo(aDevice));
397  NS_ENSURE_TRUE(xmlInfo, NS_ERROR_OUT_OF_MEMORY);
398 
399  nsCString xmlInfoExtensions;
400  rv = GetDeviceXMLInfoExtensions(xmlInfoExtensions);
401  NS_ENSURE_SUCCESS(rv, rv);
402  rv = xmlInfo->Read(xmlInfoSpec.get(), xmlInfoExtensions.get());
403  NS_ENSURE_SUCCESS(rv, rv);
404 
405  // Check if the device XML info is present.
406  PRBool infoPresent = PR_FALSE;
407  rv = xmlInfo->GetDeviceInfoPresent(&infoPresent);
408  NS_ENSURE_SUCCESS(rv, rv);
409 
410  if (infoPresent) {
411  *retval = PR_TRUE;
412  }
413 
414  return NS_OK;
415 }
416 
417 nsresult
419  (sbIDevice* aDevice,
420  sbDeviceXMLInfo** aDeviceXMLInfo)
421 {
422  NS_ENSURE_ARG_POINTER(aDevice);
423  NS_ENSURE_ARG_POINTER(aDeviceXMLInfo);
424 
425  nsresult rv;
426 
427  // Default to no device XML info.
428  *aDeviceXMLInfo = nsnull;
429 
430  // Check if device info was already read.
431  if (mDeviceXMLInfo && (aDevice == mDevice)) {
432  if (mDeviceXMLInfoPresent)
433  *aDeviceXMLInfo = mDeviceXMLInfo;
434  return NS_OK;
435  }
436 
437  // Remember the device.
438  mDevice = aDevice;
439 
440  // Get the device XML info document spec.
441  nsCAutoString deviceXMLInfoSpec;
442  rv = GetDeviceXMLInfoSpec(deviceXMLInfoSpec);
443  NS_ENSURE_SUCCESS(rv, rv);
444 
445  // Read the device XML info document.
446  if (!deviceXMLInfoSpec.IsEmpty()) {
447  rv = GetDeviceXMLInfo(deviceXMLInfoSpec, aDevice);
448  NS_ENSURE_SUCCESS(rv, rv);
449  }
450 
451  // If no device XML info was present, read from the default device XML info
452  // document.
453  if (!mDeviceXMLInfoPresent) {
454  rv = GetDefaultDeviceXMLInfoSpec(deviceXMLInfoSpec);
455  NS_ENSURE_SUCCESS(rv, rv);
456  rv = GetDeviceXMLInfo(deviceXMLInfoSpec, aDevice);
457  NS_ENSURE_SUCCESS(rv, rv);
458  }
459 
460  // Return device XML info if it's present.
461  if (mDeviceXMLInfoPresent)
462  *aDeviceXMLInfo = mDeviceXMLInfo;
463 
464  return NS_OK;
465 }
466 
467 nsresult
469  (const nsACString& aDeviceXMLInfoSpec,
470  sbIDevice* aDevice)
471 {
472  NS_ENSURE_ARG_POINTER(aDevice);
473 
474  nsresult rv;
475 
476  // Read the device XML info.
477  mDeviceXMLInfo = new sbDeviceXMLInfo(aDevice);
478  NS_ENSURE_TRUE(mDeviceXMLInfo, NS_ERROR_OUT_OF_MEMORY);
479  nsCString xmlInfoExtensions;
480  rv = GetDeviceXMLInfoExtensions(xmlInfoExtensions);
481  NS_ENSURE_SUCCESS(rv, rv);
482  rv = mDeviceXMLInfo->Read(aDeviceXMLInfoSpec.BeginReading(),
483  xmlInfoExtensions.get());
484  NS_ENSURE_SUCCESS(rv, rv);
485 
486  // Check if device XML info is present.
487  rv = mDeviceXMLInfo->GetDeviceInfoPresent(&mDeviceXMLInfoPresent);
488  NS_ENSURE_SUCCESS(rv, rv);
489 
490  return NS_OK;
491 }
492 
493 nsresult
495  (nsACString& aDeviceXMLInfoSpec)
496 {
497  aDeviceXMLInfoSpec.Truncate();
498  return NS_OK;
499 }
500 
501 nsresult
503  (nsACString& aDeviceXMLInfoExtensions)
504 {
505  aDeviceXMLInfoExtensions.Truncate();
506  return NS_OK;
507 }
508 
509 nsresult
511  (nsACString& aDeviceXMLInfoSpec)
512 {
513  aDeviceXMLInfoSpec.Assign
514  ("chrome://songbird/content/devices/sbDefaultDeviceInfo.xml");
515  return NS_OK;
516 }
517 
nsresult GetDeviceXMLInfo(sbIDevice *aDevice, sbDeviceXMLInfo **aDeviceXMLInfo)
#define TRACE(args)
virtual nsresult GetDefaultDeviceXMLInfoSpec(nsACString &aDeviceXMLInfoSpec)
return NS_OK
[UNIMPLEMENTED UNTIL AFTER 0.3]
nsresult GetStorageDeviceInfoList(nsIArray **aStorageDeviceInfoList)
Songbird Variant Utility Definitions.
nsresult GetDeviceInfoElement(nsIDOMElement **aDeviceInfoElement)
nsresult GetDeviceIcon(nsAString &aDeviceIconURL)
nsresult GetDeviceFolder(const nsAString &aFolderType, nsAString &aFolderURL)
nsresult GetDefaultName(nsAString &aDefaultName)
nsresult GetOnlyMountMediaFolders(PRBool *aOnlyMountMediaFolders)
static nsresult AddCapabilities(sbIDeviceCapabilities *aCapabilities, const char *aXMLCapabilitiesSpec, PRBool *aAddedCapabilities=nsnull, sbIDevice *aDevice=nsnull)
virtual nsresult GetDeviceXMLInfoSpec(nsACString &aDeviceXMLInfoSpec)
nsresult GetDoesDeviceSupportReformat(PRBool *aOutSupportsReformat)
NS_DECL_SBIDEVICEINFOREGISTRAR sbDefaultBaseDeviceInfoRegistrar()
virtual nsresult GetDeviceXMLInfoExtensions(nsACString &aDeviceXMLInfoExtensions)
nsresult GetMountTimeout(PRUint32 *aMountTimeout)
nsresult GetImportRules(nsIArray **aImportRules)
nsresult GetExcludedFolders(nsAString &aExcludedFolders)