50 #include <sbIAlbumArtListener.h>
51 #include <sbIMediaItem.h>
59 #include <nsIFileURL.h>
60 #include <nsIIOService.h>
62 #include <nsServiceManagerUtils.h>
63 #include <nsUnicharUtils.h>
64 #include <nsIMutableArray.h>
91 NS_ENSURE_ARG_POINTER(aMediaItems);
95 nsCOMPtr<nsIFile> albumArtFile;
96 nsCOMPtr<nsIURI> albumArtURI;
101 nsCOMPtr<sbIMediaItem> firstMediaItem;
102 rv = aMediaItems->QueryElementAt(0,
104 getter_AddRefs(firstMediaItem));
105 NS_ENSURE_SUCCESS(rv, rv);
109 rv = FindAlbumArtFile(firstMediaItem,
110 getter_AddRefs(albumArtFile));
111 NS_ENSURE_SUCCESS(rv, rv);
116 rv = mIOService->NewFileURI(albumArtFile, getter_AddRefs(albumArtURI));
117 NS_ENSURE_SUCCESS(rv, rv);
121 aListener->OnAlbumResult(albumArtURI, aMediaItems);
122 aListener->OnSearchComplete(aMediaItems);
129 sbFileAlbumArtFetcher::FetchAlbumArtForTrack(
sbIMediaItem* aMediaItem,
133 NS_ENSURE_ARG_POINTER(aMediaItem);
137 nsCOMPtr<nsIFile> albumArtFile;
138 nsCOMPtr<nsIURI> albumArtURI;
142 rv = FindAlbumArtFile(aMediaItem,
143 getter_AddRefs(albumArtFile));
144 NS_ENSURE_SUCCESS(rv, rv);
149 rv = mIOService->NewFileURI(albumArtFile, getter_AddRefs(albumArtURI));
150 NS_ENSURE_SUCCESS(rv, rv);
154 aListener->OnTrackResult(albumArtURI, aMediaItem);
157 nsCOMPtr<nsIMutableArray> items =
158 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
159 NS_ENSURE_SUCCESS(rv, rv);
160 rv = items->AppendElement(NS_ISUPPORTS_CAST(
sbIMediaItem*, aMediaItem),
162 NS_ENSURE_SUCCESS(rv, rv);
163 aListener->OnSearchComplete(items);
173 sbFileAlbumArtFetcher::Shutdown()
188 sbFileAlbumArtFetcher::GetShortName(nsAString& aShortName)
190 aShortName.AssignLiteral(
"file");
202 sbFileAlbumArtFetcher::GetName(nsAString&
aName)
205 aName.Assign(stringBundle.
Get(
"songbird.albumart.file.name"));
216 sbFileAlbumArtFetcher::GetDescription(nsAString& aDescription)
219 aDescription.Assign(stringBundle.
Get(
"songbird.albumart.file.description"));
229 sbFileAlbumArtFetcher::GetIsLocal(PRBool* aIsLocal)
231 NS_ENSURE_ARG_POINTER(aIsLocal);
241 sbFileAlbumArtFetcher::GetIsEnabled(PRBool* aIsEnabled)
243 NS_ENSURE_ARG_POINTER(aIsEnabled);
244 NS_ENSURE_STATE(mPrefService);
246 nsresult rv = mPrefService->GetBoolPref(
"songbird.albumart.file.enabled",
249 *aIsEnabled = PR_FALSE;
256 sbFileAlbumArtFetcher::SetIsEnabled(PRBool aIsEnabled)
258 NS_ENSURE_STATE(mPrefService);
259 return mPrefService->SetBoolPref(
"songbird.albumart.file.enabled",
268 sbFileAlbumArtFetcher::GetPriority(PRInt32* aPriority)
270 NS_ENSURE_ARG_POINTER(aPriority);
271 NS_ENSURE_STATE(mPrefService);
273 nsresult rv = mPrefService->GetIntPref(
"songbird.albumart.file.priority",
284 sbFileAlbumArtFetcher::SetPriority(PRInt32 aPriority)
286 NS_ENSURE_STATE(mPrefService);
287 return mPrefService->SetIntPref(
"songbird.albumart.file.priority",
296 sbFileAlbumArtFetcher::GetAlbumArtSourceList(nsIArray** aAlbumArtSourceList)
298 NS_ENSURE_ARG_POINTER(aAlbumArtSourceList);
299 NS_ADDREF(*aAlbumArtSourceList = mAlbumArtSourceList);
304 sbFileAlbumArtFetcher::SetAlbumArtSourceList(nsIArray* aAlbumArtSourceList)
306 mAlbumArtSourceList = aAlbumArtSourceList;
315 sbFileAlbumArtFetcher::GetIsFetching(PRBool* aIsFetching)
317 NS_ENSURE_ARG_POINTER(aIsFetching);
319 *aIsFetching = PR_FALSE;
358 mIOService = do_GetService(
"@mozilla.org/network/io-service;1", &rv);
359 NS_ENSURE_SUCCESS(rv, rv);
362 mPrefService = do_GetService(
"@mozilla.org/preferences-service;1", &rv);
363 NS_ENSURE_SUCCESS(rv, rv);
366 nsCString fileExtensions;
367 rv = mPrefService->GetCharPref(
"songbird.albumart.file.extensions",
368 getter_Copies(fileExtensions));
369 NS_ENSURE_SUCCESS(rv, rv);
371 NS_LITERAL_STRING(
","),
375 nsCString fileBaseNames;
376 rv = mPrefService->GetCharPref(
"songbird.albumart.file.base_names",
377 getter_Copies(fileBaseNames));
378 NS_ENSURE_SUCCESS(rv, rv);
380 NS_LITERAL_STRING(
","),
385 NS_ENSURE_SUCCESS(rv, rv);
411 sbFileAlbumArtFetcher::GetURLDirEntries
413 PRBool* aIsLocalFile,
417 NS_ENSURE_ARG_POINTER(aURL);
418 NS_ENSURE_ARG_POINTER(aIsLocalFile);
419 NS_ENSURE_ARG_POINTER(contentSrcDirEntries);
426 nsCOMPtr<nsIFileURL> contentSrcFileURL = do_QueryInterface(aURL,
429 *aIsLocalFile = PR_FALSE;
434 nsCOMPtr<nsIFile> contentSrcFile;
435 rv = contentSrcFileURL->GetFile(getter_AddRefs(contentSrcFile));
436 NS_ENSURE_SUCCESS(rv, rv);
437 nsCOMPtr<nsIFile> contentSrcDir;
438 rv = contentSrcFile->GetParent(getter_AddRefs(contentSrcDir));
439 NS_ENSURE_SUCCESS(rv, rv);
442 rv = contentSrcDir->GetDirectoryEntries(contentSrcDirEntries);
443 NS_ENSURE_SUCCESS(rv, rv);
446 *aIsLocalFile = PR_TRUE;
460 sbFileAlbumArtFetcher::FindAlbumArtFile(
sbIMediaItem* aMediaItem,
461 nsIFile** aAlbumArtFile)
464 NS_ENSURE_ARG_POINTER(aMediaItem);
465 NS_ENSURE_ARG_POINTER(aAlbumArtFile);
469 nsCOMPtr<nsISimpleEnumerator> contentSrcDirEntries;
470 nsCOMPtr<nsIMutableArray> entriesToBeCached = nsnull;
473 *aAlbumArtFile = nsnull;
477 rv = mPrefService->GetIntPref(
"songbird.albumart.maxsize", &maxFileSize);
478 NS_ENSURE_SUCCESS(rv, rv);
485 NS_ENSURE_SUCCESS(rv, rv);
488 NS_ENSURE_SUCCESS(rv, rv);
489 nsString artistAlbum(artistName);
490 artistAlbum.AppendLiteral(
" - ");
491 artistAlbum.Append(albumName);
495 nsString cacheKeyFile = NS_LITERAL_STRING(
"File:");
496 cacheKeyFile.Append(artistAlbum);
497 nsCOMPtr<nsISupports> cacheData = nsnull;
498 rv = mAlbumArtService->RetrieveTemporaryData(cacheKeyFile,
499 getter_AddRefs(cacheData));
502 if (NS_SUCCEEDED(rv)) {
503 nsCOMPtr<nsIFile>
file = do_QueryInterface(cacheData, &rv);
504 if (NS_SUCCEEDED(rv)) {
507 rv = file->GetFileSize(&fileSize);
508 NS_ENSURE_SUCCESS(rv, rv);
510 if (fileSize <= maxFileSize) {
512 file.forget(aAlbumArtFile);
519 nsCOMPtr<nsIURI> contentSrcURI;
520 rv = aMediaItem->GetContentSrc(getter_AddRefs(contentSrcURI));
521 NS_ENSURE_SUCCESS(rv, rv);
522 nsCOMPtr<nsIURL> contentSrcURL = do_QueryInterface(contentSrcURI, &rv);
523 NS_ENSURE_SUCCESS(rv, rv);
529 contentSrcURL->GetDirectory(directory);
530 nsString cacheKeyDir = NS_LITERAL_STRING(
"Directory:");
531 cacheKeyDir.Append(NS_ConvertUTF8toUTF16(directory));
532 rv = mAlbumArtService->RetrieveTemporaryData(cacheKeyDir,
533 getter_AddRefs(cacheData));
536 if (NS_SUCCEEDED(rv)) {
537 nsCOMPtr<nsIArray> cachedEntries = do_QueryInterface(cacheData, &rv);
538 NS_ENSURE_SUCCESS(rv, rv);
539 rv = cachedEntries->Enumerate(getter_AddRefs(contentSrcDirEntries));
540 NS_ENSURE_SUCCESS(rv, rv);
545 PRBool isLocalFile = PR_FALSE;
546 rv = GetURLDirEntries(contentSrcURL,
548 getter_AddRefs(contentSrcDirEntries));
549 NS_ENSURE_SUCCESS(rv, rv);
556 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
557 NS_ENSURE_SUCCESS(rv, rv);
562 while (!(*aAlbumArtFile) && entriesToBeCached) {
565 rv = contentSrcDirEntries->HasMoreElements(&hasMoreElements);
566 NS_ENSURE_SUCCESS(rv, rv);
567 if (!hasMoreElements)
569 nsCOMPtr<nsIFile>
file;
570 rv = contentSrcDirEntries->GetNext(getter_AddRefs(file));
571 NS_ENSURE_SUCCESS(rv, rv);
575 rv = file->IsFile(&isFile);
576 NS_ENSURE_SUCCESS(rv, rv);
582 rv = file->GetLeafName(leafName);
583 NS_ENSURE_SUCCESS(rv, rv);
584 ToLowerCase(leafName);
587 PRInt32 fileExtensionIndex = leafName.RFind(NS_LITERAL_STRING(
"."));
588 if (fileExtensionIndex < 0)
593 nsDependentSubstring fileExtension(leafName, fileExtensionIndex + 1);
594 PRBool fileExtensionMatched = PR_FALSE;
595 for (PRUint32
i = 0;
i < mFileExtensionList.Length();
i++) {
596 if (fileExtension.Equals(mFileExtensionList[
i])) {
597 fileExtensionMatched = PR_TRUE;
601 if (!fileExtensionMatched)
606 rv = file->GetFileSize(&fileSize);
607 NS_ENSURE_SUCCESS(rv, rv);
608 if (fileSize > maxFileSize)
613 if (entriesToBeCached) {
614 entriesToBeCached->AppendElement(file, PR_FALSE);
619 nsDependentSubstring fileBaseName(leafName, 0, fileExtensionIndex);
622 for (PRInt32
i = (mFileBaseNameList.Length() - 1);
i >= 0;
i--) {
623 if (fileBaseName.Equals(mFileBaseNameList[
i])) {
624 if (!(*aAlbumArtFile)) {
625 file.forget(aAlbumArtFile);
632 if (entriesToBeCached) {
633 rv = mAlbumArtService->CacheTemporaryData(cacheKeyDir, entriesToBeCached);
634 NS_ENSURE_SUCCESS(rv, rv);
638 if (*aAlbumArtFile) {
639 rv = mAlbumArtService->CacheTemporaryData(cacheKeyFile, *aAlbumArtFile);
#define SB_ALBUMARTSERVICE_CONTRACTID
nsString Get(const nsAString &aKey, const nsAString &aDefault=SBVoidString())
NS_DECL_ISUPPORTS NS_DECL_SBIALBUMARTFETCHER sbFileAlbumArtFetcher()
NS_IMPL_THREADSAFE_ISUPPORTS1(sbDeviceCapsCompatibility, sbIDeviceCapsCompatibility) sbDeviceCapsCompatibility
A component which is interested in the result of an album art fetch request.
void nsString_Split(const nsAString &aString, const nsAString &aDelimiter, nsTArray< nsString > &aSubStringArray)
#define SB_PROPERTY_ARTISTNAME
Songbird String Bundle Definitions.
SimpleArrayEnumerator prototype hasMoreElements
#define SB_PROPERTY_ALBUMNAME
virtual ~sbFileAlbumArtFetcher()
Interface for an album art fetcher. Instantiate as a component instance.
_getSelectedPageStyle s i
Songbird Local File Album Art Fetcher Definitions.