50 #include <sbIAlbumArtListener.h>
51 #include <sbIMediaItem.h>
57 #include <nsComponentManagerUtils.h>
58 #include <nsArrayUtils.h>
59 #include <nsIFileURL.h>
60 #include <nsISimpleEnumerator.h>
61 #include <nsServiceManagerUtils.h>
62 #include <nsStringGlue.h>
74 static PRLogModuleInfo* gMetadataAlbumArtFetcherLog = nsnull;
75 #define TRACE(args) PR_LOG(gMetadataAlbumArtFetcherLog, PR_LOG_DEBUG, args)
76 #define LOG(args) PR_LOG(gMetadataAlbumArtFetcherLog, PR_LOG_WARN, args)
78 #define __FUNCTION__ __PRETTY_FUNCTION__
109 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
111 NS_ENSURE_ARG_POINTER(aMediaItems);
114 nsCOMPtr<nsISimpleEnumerator> listEnum;
115 rv = aMediaItems->Enumerate(getter_AddRefs(listEnum));
116 NS_ENSURE_SUCCESS(rv, rv);
119 rv = listEnum->HasMoreElements(&hasMore);
120 NS_ENSURE_SUCCESS(rv, rv);
122 return NS_ERROR_NOT_AVAILABLE;
126 nsCOMPtr<sbIMetadataManager> metadataManager =
127 do_GetService(
"@songbirdnest.com/Songbird/MetadataManager;1", &rv);
128 NS_ENSURE_SUCCESS(rv, rv);
133 nsCOMPtr<nsIArray> sourceList = mAlbumArtSourceList;
135 while (NS_SUCCEEDED(listEnum->HasMoreElements(&hasMore)) && hasMore) {
136 nsCOMPtr<nsISupports>
next;
137 if (NS_SUCCEEDED(listEnum->GetNext(getter_AddRefs(next))) && next) {
138 nsCOMPtr<sbIMediaItem> mediaItem(do_QueryInterface(next, &rv));
139 if (NS_SUCCEEDED(rv) && mediaItem) {
140 rv = GetImageForItem(mediaItem, sourceList,
141 metadataManager, aListener);
142 if (NS_FAILED(rv) && aListener) {
143 aListener->OnTrackResult(nsnull, mediaItem);
154 aListener->OnSearchComplete(aMediaItems);
160 sbMetadataAlbumArtFetcher::FetchAlbumArtForTrack(
sbIMediaItem* aMediaItem,
163 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
165 NS_ENSURE_ARG_POINTER(aMediaItem);
171 nsCOMPtr<nsIMutableArray> items =
172 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
173 NS_ENSURE_SUCCESS(rv, rv);
174 rv = items->AppendElement(NS_ISUPPORTS_CAST(
sbIMediaItem*, aMediaItem),
176 NS_ENSURE_SUCCESS(rv, rv);
178 return FetchAlbumArtForAlbum(items, aListener);
185 sbMetadataAlbumArtFetcher::Shutdown()
187 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
188 mAlbumArtService = nsnull;
202 sbMetadataAlbumArtFetcher::GetShortName(nsAString& aShortName)
204 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
205 aShortName.AssignLiteral(
"metadata");
217 sbMetadataAlbumArtFetcher::GetName(nsAString&
aName)
219 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
221 aName.Assign(stringBundle.
Get(
"songbird.albumart.metadata.name"));
232 sbMetadataAlbumArtFetcher::GetDescription(nsAString& aDescription)
234 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
236 aDescription.Assign(stringBundle.
Get(
"songbird.albumart.metadata.description"));
246 sbMetadataAlbumArtFetcher::GetIsLocal(PRBool* aIsLocal)
248 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
249 NS_ENSURE_ARG_POINTER(aIsLocal);
259 sbMetadataAlbumArtFetcher::GetIsEnabled(PRBool* aIsEnabled)
261 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
262 NS_ENSURE_ARG_POINTER(aIsEnabled);
263 NS_ENSURE_STATE(mPrefService);
265 nsresult rv = mPrefService->GetBoolPref(
"songbird.albumart.metadata.enabled",
268 *aIsEnabled = PR_FALSE;
275 sbMetadataAlbumArtFetcher::SetIsEnabled(PRBool aIsEnabled)
277 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
278 NS_ENSURE_STATE(mPrefService);
279 return mPrefService->SetBoolPref(
"songbird.albumart.metadata.enabled",
288 sbMetadataAlbumArtFetcher::GetPriority(PRInt32* aPriority)
290 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
291 NS_ENSURE_ARG_POINTER(aPriority);
292 NS_ENSURE_STATE(mPrefService);
294 nsresult rv = mPrefService->GetIntPref(
"songbird.albumart.metadata.priority",
304 sbMetadataAlbumArtFetcher::SetPriority(PRInt32 aPriority)
306 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
307 NS_ENSURE_STATE(mPrefService);
308 return mPrefService->SetIntPref(
"songbird.albumart.metadata.priority",
318 sbMetadataAlbumArtFetcher::GetAlbumArtSourceList(nsIArray** aAlbumArtSourceList)
320 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
321 NS_ENSURE_ARG_POINTER(aAlbumArtSourceList);
322 NS_ADDREF(*aAlbumArtSourceList = mAlbumArtSourceList);
327 sbMetadataAlbumArtFetcher::SetAlbumArtSourceList(nsIArray* aAlbumArtSourceList)
329 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
330 mAlbumArtSourceList = aAlbumArtSourceList;
340 sbMetadataAlbumArtFetcher::GetIsFetching(PRBool* aIsFetching)
342 NS_ENSURE_ARG_POINTER(aIsFetching);
344 *aIsFetching = PR_FALSE;
362 if (!gMetadataAlbumArtFetcherLog) {
363 gMetadataAlbumArtFetcherLog = PR_NewLogModule(
"sbMetadataAlbumArtFetcher");
366 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
376 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
387 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
392 NS_ENSURE_SUCCESS(rv, rv);
395 mPrefService = do_GetService(
"@mozilla.org/preferences-service;1", &rv);
396 NS_ENSURE_SUCCESS(rv, rv);
421 sbMetadataAlbumArtFetcher::GetMetadataHandler
422 (nsIURI* aContentSrcURI,
423 nsIArray* aSourceList,
427 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
429 NS_ASSERTION(aContentSrcURI,
"aContentSrcURI is null");
430 NS_ASSERTION(aMetadataHandler,
"aMetadataHandler is null");
433 nsCOMPtr<sbIMetadataHandler> metadataHandler;
438 PRUint32 albumArtSourceListCount;
439 rv = aSourceList->GetLength(&albumArtSourceListCount);
440 NS_ENSURE_SUCCESS(rv, rv);
441 for (PRUint32
i = 0;
i < albumArtSourceListCount;
i++) {
442 metadataHandler = do_QueryElementAt(aSourceList,
i, &rv);
443 if (NS_SUCCEEDED(rv))
449 if (!metadataHandler) {
450 nsCAutoString contentSrcURISpec;
451 rv = aContentSrcURI->GetSpec(contentSrcURISpec);
452 NS_ENSURE_SUCCESS(rv, rv);
453 NS_ENSURE_TRUE(aManager, NS_ERROR_NOT_INITIALIZED);
454 rv = aManager->GetHandlerForMediaURL
455 (NS_ConvertUTF8toUTF16(contentSrcURISpec),
456 getter_AddRefs(metadataHandler));
458 metadataHandler = nsnull;
462 if (!metadataHandler)
463 return NS_ERROR_NOT_AVAILABLE;
466 metadataHandler.forget(aMetadataHandler);
472 sbMetadataAlbumArtFetcher::GetImageForItem(
sbIMediaItem* aMediaItem,
473 nsIArray* aSourceList,
477 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
479 NS_ENSURE_ARG_POINTER(aMediaItem);
485 nsCOMPtr<nsIURI> contentSrcURI;
486 nsCOMPtr<nsIFileURL> contentSrcFileURL;
487 rv = aMediaItem->GetContentSrc(getter_AddRefs(contentSrcURI));
488 NS_ENSURE_SUCCESS(rv, rv);
489 contentSrcFileURL = do_QueryInterface(contentSrcURI, &rv);
490 NS_ENSURE_SUCCESS(rv, rv);
494 nsCOMPtr<sbIMetadataHandler> metadataHandler;
495 rv = GetMetadataHandler(contentSrcURI, aSourceList, aManager,
496 getter_AddRefs(metadataHandler));
497 NS_ENSURE_SUCCESS(rv, rv);
500 nsCAutoString mimeType;
501 PRUint32 dataLength = 0;
502 PRUint8*
data = nsnull;
503 rv = metadataHandler->GetImageData
512 if (dataLength == 0) {
513 rv = metadataHandler->GetImageData
523 if (dataLength == 0) {
524 return NS_ERROR_FAILURE;
531 nsCOMPtr<nsIURI> cacheURI;
532 NS_ENSURE_TRUE(mAlbumArtService, NS_ERROR_NOT_INITIALIZED);
533 rv = mAlbumArtService->CacheImage(mimeType,
536 getter_AddRefs(cacheURI));
537 NS_ENSURE_SUCCESS(rv, rv);
541 aListener->OnTrackResult(cacheURI, aMediaItem);
#define SB_ALBUMARTSERVICE_CONTRACTID
nsString Get(const nsAString &aKey, const nsAString &aDefault=SBVoidString())
NS_IMPL_THREADSAFE_ISUPPORTS1(sbDeviceCapsCompatibility, sbIDeviceCapsCompatibility) sbDeviceCapsCompatibility
A component which is interested in the result of an album art fetch request.
Songbird String Bundle Definitions.
StringArrayEnumerator prototype hasMore
Interface for an album art fetcher. Instantiate as a component instance.
_getSelectedPageStyle s i