50 #include <nsArrayUtils.h>
51 #include <nsComponentManagerUtils.h>
52 #include <nsIFileURL.h>
53 #include <nsIMutableArray.h>
55 #include <nsIVariant.h>
56 #include <nsServiceManagerUtils.h>
57 #include <nsStringGlue.h>
71 static PRLogModuleInfo* gAlbumArtFetcherSetLog = nsnull;
72 #define TRACE(args) PR_LOG(gAlbumArtFetcherSetLog, PR_LOG_DEBUG, args)
73 #define LOG(args) PR_LOG(gAlbumArtFetcherSetLog, PR_LOG_WARN, args)
75 #define __FUNCTION__ __PRETTY_FUNCTION__
111 TRACE((
"%s[%.8x] = %d", __FUNCTION__,
this, mType));
112 NS_ENSURE_ARG_POINTER(aType);
118 sbAlbumArtFetcherSet::SetFetcherType(PRUint32 aType)
120 TRACE((
"%s[%.8x] = %d", __FUNCTION__,
this, aType));
124 if (aType != mType) {
126 TRACE((
"%s - Reloading fetcher list.", __FUNCTION__));
127 rv = mAlbumArtService->GetFetcherList(mType,
129 getter_AddRefs(mFetcherList));
130 NS_ENSURE_SUCCESS(rv, rv);
150 sbAlbumArtFetcherSet::FetchAlbumArtForAlbum(nsIArray* aMediaItems,
153 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
155 "sbAlbumArtFetcherSet::FetchAlbumArtForAlbum is main thread only!");
157 NS_ENSURE_ARG_POINTER(aMediaItems);
158 NS_ENSURE_ARG_POINTER(aListener);
161 PRUint32 fetcherListCount;
162 rv = mFetcherList->GetLength(&fetcherListCount);
163 NS_ENSURE_SUCCESS(rv, rv);
165 if (fetcherListCount <= 0) {
167 aListener->OnSearchComplete(aMediaItems);
172 mListener = aListener;
173 mMediaItems = aMediaItems;
177 mShutdown = PR_FALSE;
179 mFoundAllArtwork = PR_TRUE;
182 mIsFetching = PR_TRUE;
184 return TryNextFetcher();
193 sbAlbumArtFetcherSet::FetchAlbumArtForTrack(
sbIMediaItem* aMediaItem,
196 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
198 "sbAlbumArtFetcherSet::FetchAlbumArtForTrack is main thread only!");
200 NS_ENSURE_ARG_POINTER(aMediaItem);
201 NS_ENSURE_ARG_POINTER(aListener);
205 mListener = aListener;
206 nsCOMPtr<nsIMutableArray> itemArray =
207 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
208 NS_ENSURE_SUCCESS(rv, rv);
209 rv = itemArray->AppendElement(NS_ISUPPORTS_CAST(
sbIMediaItem*, aMediaItem),
211 NS_ENSURE_SUCCESS(rv, rv);
212 mMediaItems = do_QueryInterface(itemArray, &rv);
213 NS_ENSURE_SUCCESS(rv, rv);
218 mShutdown = PR_FALSE;
220 mFoundAllArtwork = PR_TRUE;
223 mIsFetching = PR_TRUE;
225 return TryNextFetcher();
232 sbAlbumArtFetcherSet::Shutdown()
234 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
236 "sbAlbumArtFetcherSet::Shutdown is main thread only!");
239 mFetcher->Shutdown();
242 mIsFetching = PR_FALSE;
245 mMediaItems = nsnull;
259 sbAlbumArtFetcherSet::GetShortName(nsAString& aShortName)
261 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
262 aShortName.AssignLiteral(
"set");
273 sbAlbumArtFetcherSet::GetName(nsAString&
aName)
275 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
276 aName.AssignLiteral(
"set");
286 sbAlbumArtFetcherSet::GetDescription(nsAString& aDescription)
288 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
289 aDescription.AssignLiteral(
"set");
300 sbAlbumArtFetcherSet::GetIsLocal(PRBool* aIsLocal)
302 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
303 NS_ENSURE_ARG_POINTER(aIsLocal);
314 sbAlbumArtFetcherSet::GetIsEnabled(PRBool* aIsEnabled)
316 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
317 NS_ENSURE_ARG_POINTER(aIsEnabled);
318 *aIsEnabled = PR_TRUE;
323 sbAlbumArtFetcherSet::SetIsEnabled(PRBool aIsEnabled)
325 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
335 sbAlbumArtFetcherSet::GetPriority(PRInt32* aPriority)
337 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
338 NS_ENSURE_ARG_POINTER(aPriority);
344 sbAlbumArtFetcherSet::SetPriority(PRInt32 aPriority)
346 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
355 sbAlbumArtFetcherSet::GetAlbumArtSourceList(nsIArray** aAlbumArtSourceList)
357 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
358 NS_ENSURE_ARG_POINTER(aAlbumArtSourceList);
359 NS_ADDREF(*aAlbumArtSourceList = mAlbumArtSourceList);
364 sbAlbumArtFetcherSet::SetAlbumArtSourceList(nsIArray* aAlbumArtSourceList)
366 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
367 mAlbumArtSourceList = aAlbumArtSourceList;
376 sbAlbumArtFetcherSet::GetIsFetching(PRBool* aIsFetching)
378 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
379 NS_ENSURE_ARG_POINTER(aIsFetching);
380 *aIsFetching = mIsFetching;
398 mIsFetching(PR_FALSE),
400 mFetcherList(nsnull),
405 mFoundAllArtwork(PR_FALSE)
408 if (!gAlbumArtFetcherSetLog) {
409 gAlbumArtFetcherSetLog = PR_NewLogModule(
"sbAlbumArtFetcherSet");
412 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
423 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
438 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
442 mTimeoutTimer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
443 NS_ENSURE_SUCCESS(rv, rv);
447 mThreadManager = do_GetService(
"@mozilla.org/thread-manager;1", &rv);
448 NS_ENSURE_SUCCESS(rv, rv);
452 NS_ENSURE_SUCCESS(rv, rv);
455 rv = mAlbumArtService->GetFetcherList(mType,
457 getter_AddRefs(mFetcherList));
458 NS_ENSURE_SUCCESS(rv, rv);
463 NS_ENSURE_SUCCESS(rv, rv);
469 mConsoleService = do_GetService(
"@mozilla.org/consoleservice;1", &rv);
470 NS_ENSURE_SUCCESS(rv, rv);
483 sbAlbumArtFetcherSet::Notify(nsITimer* aTimer)
485 NS_ENSURE_ARG_POINTER(aTimer);
487 if (aTimer == mTimeoutTimer) {
489 TRACE((
"%s[%.8x] - Timeout exceeded so moving to next fetcher",
490 __FUNCTION__,
this));
491 mTimeoutTimer->Cancel();
492 return TryNextFetcher();
507 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
513 sbAlbumArtFetcherSet::OnTrackResult(nsIURI* aImageLocation,
516 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
518 NS_ENSURE_ARG_POINTER(aMediaItem);
521 if (!aImageLocation) {
523 TRACE((
"%s[%.8x] - No image found, marking mFoundAllArtwork as FALSE",
524 __FUNCTION__,
this));
525 mFoundAllArtwork = PR_FALSE;
528 rv = CheckLocalImage(aImageLocation);
529 NS_ENSURE_SUCCESS(rv, rv);
533 mListener->OnTrackResult(aImageLocation, aMediaItem);
541 sbAlbumArtFetcherSet::OnAlbumResult(nsIURI* aImageLocation,
542 nsIArray* aMediaItems)
544 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
546 NS_ENSURE_ARG_POINTER(aMediaItems);
549 if (!aImageLocation) {
551 mFoundAllArtwork = PR_FALSE;
554 rv = CheckLocalImage(aImageLocation);
555 NS_ENSURE_SUCCESS(rv, rv);
559 mListener->OnAlbumResult(aImageLocation, aMediaItems);
567 sbAlbumArtFetcherSet::OnSearchComplete(nsIArray* aMediaItems)
569 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
573 mTimeoutTimer->Cancel();
575 if (mFoundAllArtwork) {
577 TRACE((
"Found all artwork, finishing"));
578 mIsFetching = PR_FALSE;
583 mListener->OnSearchComplete(aMediaItems);
590 rv = mFetcher->Shutdown();
591 NS_ENSURE_SUCCESS(rv, rv);
596 TRACE((
"Images missing, using next fetcher."));
597 return TryNextFetcher();
610 sbAlbumArtFetcherSet::CheckLocalImage(nsIURI* aImageLocation)
612 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
613 NS_ENSURE_ARG_POINTER(aImageLocation);
615 nsCOMPtr<nsIFileURL> localFile = do_QueryInterface(aImageLocation, &rv);
618 nsString
message(NS_LITERAL_STRING(
"Fetcher returned non-local file for image"));
621 rv = aImageLocation->GetSpec(uriSpec);
622 if (NS_SUCCEEDED(rv)) {
624 message.AppendLiteral(uriSpec.get());
626 mConsoleService->LogStringMessage(
message.get());
633 sbAlbumArtFetcherSet::TryNextFetcher()
635 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
639 PRUint32 fetcherListCount;
640 rv = mFetcherList->GetLength(&fetcherListCount);
641 NS_ENSURE_SUCCESS(rv, rv);
646 while ( (mFetcherIndex <= fetcherListCount) &&
649 if (NS_SUCCEEDED(rv)) {
659 sbAlbumArtFetcherSet::NextFetcher()
661 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
663 "sbAlbumArtFetcherSet::NextFetcher is main thread only!");
665 PRUint32 currentFetcherIndex = mFetcherIndex;
669 mFetcher->Shutdown();
675 rv = mFetcher->GetIsFetching(&isFetching);
678 "Could not determine whether fetcher is still fetching.\n");
679 if (NS_SUCCEEDED(rv) && isFetching)
694 PRUint32 fetcherListCount;
695 rv = mFetcherList->GetLength(&fetcherListCount);
696 NS_ENSURE_SUCCESS(rv, rv);
699 PRUint32 itemListLength;
700 rv = mMediaItems->GetLength(&itemListLength);
701 NS_ENSURE_SUCCESS(rv, rv);
703 if (currentFetcherIndex >= fetcherListCount) {
704 TRACE((
"%s[%.8x] - No more fetchers", __FUNCTION__,
this));
705 mIsFetching = PR_FALSE;
707 mListener->OnSearchComplete(mMediaItems);
715 TRACE((
"%s[%.8x] - Querying fetcher at index %d of %d",
716 __FUNCTION__,
this, (currentFetcherIndex + 1), fetcherListCount));
719 nsCAutoString fetcherContractID;
721 fetcherContractIDVariant = do_QueryElementAt(mFetcherList,
724 NS_ENSURE_SUCCESS(rv, rv);
726 rv = fetcherContractIDVariant->GetAsACString(fetcherContractID);
727 NS_ENSURE_SUCCESS(rv, rv);
729 TRACE((
"%s[%.8x] - Trying fetcher %s",
730 __FUNCTION__,
this, fetcherContractID.get()));
733 mFetcher = do_CreateInstance(fetcherContractID.get(), &rv);
734 NS_ENSURE_SUCCESS(rv, rv);
736 mListener->OnChangeFetcher(mFetcher);
740 rv = mFetcher->SetAlbumArtSourceList(mAlbumArtSourceList);
741 NS_ENSURE_SUCCESS(rv, rv);
744 rv = mTimeoutTimer->InitWithCallback(
this,
746 nsITimer::TYPE_ONE_SHOT);
747 NS_ENSURE_SUCCESS(rv, rv);
753 if (itemListLength == 1) {
755 nsCOMPtr<sbIMediaItem> firstMediaItem;
756 rv = mMediaItems->QueryElementAt(0,
758 getter_AddRefs(firstMediaItem));
759 NS_ENSURE_SUCCESS(rv, rv);
760 mFoundAllArtwork = PR_TRUE;
761 rv = mFetcher->FetchAlbumArtForTrack(firstMediaItem,
this);
763 mFoundAllArtwork = PR_TRUE;
764 rv = mFetcher->FetchAlbumArtForAlbum(mMediaItems,
this);
769 mTimeoutTimer->Cancel();
Songbird Album Art Fetcher Set Definitions.
#define SB_ALBUMARTSERVICE_CONTRACTID
NS_DECL_ISUPPORTS NS_DECL_SBIALBUMARTFETCHER NS_DECL_SBIALBUMARTFETCHERSET NS_DECL_NSITIMERCALLBACK NS_DECL_SBIALBUMARTLISTENER sbAlbumArtFetcherSet()
#define ALBUMART_SCANNER_TIMEOUT
#define PREF_ALBUMART_SCANNER_TIMEOUT
const unsigned long TYPE_LOCAL
PRBool SB_IsMainThread(nsIThreadManager *aThreadManager)
A component which is interested in the result of an album art fetch request.
readonly attribute boolean isFetching
Flag to indicate if this fetcher is currently fetching.
virtual ~sbAlbumArtFetcherSet()
Songbird Thread Utilities Definitions.
Interface for an album art fetcher set. Instantiate as a component instance.
#define PREF_ALBUMART_SCANNER_BRANCH
Interface for an album art fetcher. Instantiate as a component instance.
NS_IMPL_THREADSAFE_ISUPPORTS3(sbAlbumArtFetcherSet, sbIAlbumArtFetcherSet, sbIAlbumArtFetcher, sbIAlbumArtListener) NS_IMETHODIMP sbAlbumArtFetcherSet
If true, only attempt to fetch album art from local sources.
PRInt32 GetIntPref(const char *aKey, const PRInt32 aDefault)