27 #include <nsICategoryManager.h>
28 #include <nsIAppStartup.h>
30 #include <nsIGenericFactory.h>
31 #include <nsILocalFile.h>
32 #include <nsIObserverService.h>
33 #include <nsIIOService.h>
34 #include <nsIPrefBranch.h>
35 #include <nsIPrefService.h>
36 #include <nsIPromptService.h>
37 #include <nsIProperties.h>
38 #include <nsIPropertyBag2.h>
39 #include <nsIStringBundle.h>
40 #include <nsISupportsPrimitives.h>
43 #include <sbILibrary.h>
44 #include <sbIMediaList.h>
45 #ifdef METRICS_ENABLED
46 #include <sbIMetrics.h>
49 #include <nsAutoPtr.h>
50 #include <nsComponentManagerUtils.h>
51 #include <nsServiceManagerUtils.h>
53 #include <nsNetUtil.h>
54 #include <nsServiceManagerUtils.h>
55 #include <nsTHashtable.h>
56 #include <nsXPCOMCID.h>
57 #include <nsXPFEComponentsCID.h>
66 #include <sbIPropertyManager.h>
71 #define PROPERTY_KEY_DATABASEFILE "databaseFile"
78 #define NS_APPSTARTUP_CATEGORY "app-startup"
79 #define NS_FINAL_UI_STARTUP_CATEGORY "final-ui-startup"
81 #define PREFBRANCH_LOADER SB_PREFBRANCH_LIBRARY "loader."
84 #define PREF_DATABASE_GUID "databaseGUID"
85 #define PREF_DATABASE_LOCATION "databaseLocation"
86 #define PREF_LOAD_AT_STARTUP "loadAtStartup"
87 #define PREF_RESOURCE_GUID "resourceGUID"
90 #define PREF_SUPPORT_INACCESSIBLE_LIBRARY "songbird.url.support.inaccessiblelibrary"
92 #define MINIMUM_LIBRARY_COUNT 2
93 #define LOADERINFO_VALUE_COUNT 4
97 #define DBENGINE_GUID_MAIN_LIBRARY "main@library.songbirdnest.com"
98 #define DBENGINE_GUID_WEB_LIBRARY "web@library.songbirdnest.com"
99 #define DBENGINE_GUID_PLAYQUEUE_LIBRARY "playqueue@library.songbirdnest.com"
102 #define SB_NAMEKEY_MAIN_LIBRARY \
103 "&chrome://songbird/locale/songbird.properties#servicesource.library"
104 #define SB_NAMEKEY_WEB_LIBRARY \
105 "&chrome://songbird/locale/songbird.properties#device.weblibrary"
106 #define SB_NAMEKEY_PLAYQUEUE_LIBRARY \
107 "&chrome://songbird/locale/songbird.properties#playqueue.library"
109 #define SB_CUSTOMTYPE_MAIN_LIBRARY \
111 #define SB_CUSTOMTYPE_WEB_LIBRARY \
113 #define SB_CUSTOMTYPE_PLAYQUEUE_LIBRARY \
116 #define DEFAULT_COLUMNSPEC_WEB_LIBRARY \
117 NS_LL("http://songbirdnest.com/data/1.0#trackName 264 ") \
118 NS_LL("http://songbirdnest.com/data/1.0#duration 56 ") \
119 NS_LL("http://songbirdnest.com/data/1.0#artistName 209 ") \
120 NS_LL("http://songbirdnest.com/data/1.0#originPageImage 44 ") \
121 NS_LL("http://songbirdnest.com/data/1.0#created 119 d ") \
122 NS_LL("http://songbirdnest.com/data/1.0#downloadButton 83")
127 : m_DetectedCorruptLibrary(PR_FALSE)
128 , m_DeleteLibrariesAtShutdown(PR_FALSE)
132 TRACE(
"sbLocalDatabaseLibraryLoader[0x%x] - Created",
this);
135 sbLocalDatabaseLibraryLoader::~sbLocalDatabaseLibraryLoader()
137 TRACE(
"sbLocalDatabaseLibraryLoader[0x%x] - Destroyed",
this);
146 TRACE(
"sbLocalDatabaseLibraryLoader[0x%x] - Init",
this);
151 do_GetService(
"@mozilla.org/observer-service;1", &rv);
152 if (NS_SUCCEEDED(rv)) {
155 NS_ENSURE_SUCCESS(rv, rv);
156 rv = observerService->AddObserver(
this, NS_XPCOM_SHUTDOWN_OBSERVER_ID,
158 NS_ENSURE_SUCCESS(rv, rv);
161 nsCOMPtr<nsIPrefService> prefService =
163 NS_ENSURE_SUCCESS(rv, rv);
165 mRootBranch = do_QueryInterface(prefService, &rv);
166 NS_ENSURE_SUCCESS(rv, rv);
168 PRUint32 libraryKeysCount;
173 NS_ENSURE_SUCCESS(rv, rv);
180 NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
182 for (PRUint32 index = 0; index < libraryKeysCount; index++) {
184 nsCAutoString
pref(libraryKeys[index]);
190 PRInt32 firstDotIndex = pref.FindChar(
'.', branchLength);
191 NS_ASSERTION(firstDotIndex != -1,
"Bad pref string!");
193 PRUint32 keyLength = firstDotIndex - branchLength;
194 NS_ASSERTION(keyLength > 0,
"Bad pref string!");
197 nsCAutoString keyString(Substring(pref, branchLength, keyLength));
198 PRUint32 libraryKey = keyString.ToInteger(&rv);
199 NS_ENSURE_SUCCESS(rv, rv);
202 nsCAutoString branchString(Substring(pref, 0, branchLength + keyLength + 1));
203 NS_ASSERTION(StringEndsWith(branchString, NS_LITERAL_CSTRING(
".")),
206 if (!mLibraryInfoTable.Get(libraryKey, nsnull)) {
208 NS_ENSURE_TRUE(newLibraryInfo, NS_ERROR_OUT_OF_MEMORY);
210 rv = newLibraryInfo->Init(branchString);
211 NS_ENSURE_SUCCESS(rv, rv);
213 success = mLibraryInfoTable.Put(libraryKey, newLibraryInfo);
214 NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
216 newLibraryInfo.forget();
220 mLibraryInfoTable.Enumerate(VerifyEntriesCallback, nsnull);
226 sbLocalDatabaseLibraryLoader::EnsureDefaultLibraries()
228 PRBool databasesOkay = PR_TRUE;
229 nsresult retval =
NS_OK;
238 databasesOkay = PR_FALSE;
248 databasesOkay = PR_FALSE;
258 databasesOkay = PR_FALSE;
262 if (! databasesOkay) {
267 m_DetectedCorruptLibrary = PR_TRUE;
269 #ifdef METRICS_ENABLED
271 nsCOMPtr<sbIMetrics> metrics =
272 do_CreateInstance(
"@songbirdnest.com/Songbird/Metrics;1", &rv);
273 NS_ASSERTION(NS_SUCCEEDED(rv),
"Failed to get metrics service");
274 if (NS_SUCCEEDED(rv)) {
275 nsString metricsCategory = NS_LITERAL_STRING(
"app");
276 nsString metricsId = NS_LITERAL_STRING(
"library.error");
277 rv = metrics->MetricsInc(metricsCategory, metricsId, EmptyString());
278 NS_ASSERTION(NS_SUCCEEDED(rv),
"Failed to post metric");
280 #endif // METRICS_ENABLED
288 sbLocalDatabaseLibraryLoader::EnsureDefaultLibrary(
const nsACString& aLibraryGUIDPref,
289 const nsAString& aDefaultDatabaseGUID,
290 const nsAString& aLibraryNameKey,
291 const nsAString& aCustomType,
292 const nsAString& aDefaultColumnSpec)
294 nsCAutoString resourceGUIDPrefKey(aLibraryGUIDPref);
297 nsAutoString resourceGUID;
298 PRInt32 libraryInfoIndex = -1;
301 nsCOMPtr<nsISupportsString> supportsString;
302 nsresult rv = mRootBranch->GetComplexValue(resourceGUIDPrefKey.get(),
304 getter_AddRefs(supportsString));
305 if (NS_SUCCEEDED(rv)) {
307 rv = supportsString->GetData(resourceGUID);
308 NS_ENSURE_SUCCESS(rv, rv);
310 NS_ASSERTION(!resourceGUID.IsEmpty(),
"Should have a resource GUID here!");
313 sbLibraryExistsInfo existsInfo(resourceGUID);
314 mLibraryInfoTable.EnumerateRead(LibraryExistsCallback, &existsInfo);
316 libraryInfoIndex = existsInfo.index;
320 if ((libraryInfoIndex == -1) ||
321 (!mLibraryInfoTable.Get(libraryInfoIndex, &libraryInfo))) {
324 PRUint32 index = GetNextLibraryIndex();
327 prefKey.AppendInt(index);
328 prefKey.AppendLiteral(
".");
330 nsAutoPtr<sbLibraryLoaderInfo>
331 newLibraryInfo(CreateDefaultLibraryInfo(prefKey, aDefaultDatabaseGUID,
332 nsnull, aLibraryNameKey));
333 if (!newLibraryInfo || !mLibraryInfoTable.Put(index, newLibraryInfo)) {
334 return NS_ERROR_FAILURE;
338 newLibraryInfo->GetResourceGUID(resourceGUID);
339 NS_ENSURE_FALSE(resourceGUID.IsEmpty(), NS_ERROR_UNEXPECTED);
341 supportsString = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
342 NS_ENSURE_SUCCESS(rv, rv);
344 rv = supportsString->SetData(resourceGUID);
345 NS_ENSURE_SUCCESS(rv, rv);
347 rv = mRootBranch->SetComplexValue(resourceGUIDPrefKey.get(),
350 NS_ENSURE_SUCCESS(rv, rv);
352 libraryInfo = newLibraryInfo.forget();
358 NS_ASSERTION(!guid.IsEmpty(),
"Empty GUID!");
364 NS_ENSURE_SUCCESS(rv, rv);
369 NS_ENSURE_TRUE(location, NS_ERROR_UNEXPECTED);
371 nsCOMPtr<sbILibraryFactory> localDatabaseLibraryFactory =
373 NS_ENSURE_SUCCESS(rv, rv);
378 nsCOMPtr<sbILibrary> library;
380 getter_AddRefs(library),
388 NS_ENSURE_TRUE(location, NS_ERROR_FAILURE);
392 getter_AddRefs(library));
393 NS_ENSURE_SUCCESS(rv, rv);
397 NS_ENSURE_SUCCESS(rv, rv);
400 NS_ENSURE_SUCCESS(rv, rv);
402 rv = library->GetGuid(resourceGUID);
403 NS_ENSURE_SUCCESS(rv, rv);
406 NS_ENSURE_SUCCESS(rv, rv);
410 rv = library->SetName(aLibraryNameKey);
411 NS_ENSURE_SUCCESS(rv, rv);
415 NS_ENSURE_SUCCESS(rv, rv);
418 NS_LITERAL_STRING(
"1"));
419 NS_ENSURE_SUCCESS(rv, rv);
421 if (!aDefaultColumnSpec.IsEmpty()) {
424 NS_ENSURE_SUCCESS(rv, rv);
434 sbLocalDatabaseLibraryLoader::CreateDefaultLibraryInfo(
const nsACString& aPrefKey,
435 const nsAString& aDatabaseGUID,
436 nsILocalFile* aDatabaseFile,
437 const nsAString& aLibraryNameKey)
440 NS_ENSURE_TRUE(newLibraryInfo, nsnull);
442 nsresult rv = newLibraryInfo->Init(aPrefKey);
443 NS_ENSURE_SUCCESS(rv, nsnull);
445 nsCOMPtr<sbILibraryFactory> localDatabaseLibraryFactory =
447 NS_ENSURE_SUCCESS(rv, nsnull);
452 nsAutoString databaseGUID;
454 if (!aDatabaseGUID.IsEmpty()) {
455 databaseGUID.Assign(aDatabaseGUID);
458 NS_ASSERTION(aDatabaseFile,
"You must supply either the GUID or file!");
462 NS_ENSURE_FALSE(databaseGUID.IsEmpty(), nsnull);
465 rv = newLibraryInfo->SetDatabaseGUID(databaseGUID);
466 NS_ENSURE_SUCCESS(rv, nsnull);
468 nsCOMPtr<nsILocalFile> location;
471 location = aDatabaseFile;
474 NS_ASSERTION(!aDatabaseGUID.IsEmpty(),
475 "You must specify either the GUID or file!");
479 NS_ENSURE_TRUE(location, nsnull);
482 rv = newLibraryInfo->SetDatabaseLocation(location);
483 NS_ENSURE_SUCCESS(rv, nsnull);
485 rv = newLibraryInfo->SetLoadAtStartup(PR_TRUE);
486 NS_ENSURE_SUCCESS(rv, nsnull);
489 nsCOMPtr<sbILibrary> library;
491 getter_AddRefs(library));
492 NS_ENSURE_SUCCESS(rv, nsnull);
494 if (!aLibraryNameKey.IsEmpty()) {
495 nsCOMPtr<sbIMediaList> mediaList = do_QueryInterface(library, &rv);
496 NS_ENSURE_SUCCESS(rv, nsnull);
499 rv = mediaList->SetName(aLibraryNameKey);
500 NS_ENSURE_SUCCESS(rv, nsnull);
503 nsAutoString resourceGUID;
504 rv = library->GetGuid(resourceGUID);
505 NS_ENSURE_SUCCESS(rv, nsnull);
507 rv = newLibraryInfo->SetResourceGUID(resourceGUID);
508 NS_ENSURE_SUCCESS(rv, nsnull);
510 nsCOMPtr<nsIPrefService> prefService = do_QueryInterface(mRootBranch, &rv);
511 NS_ENSURE_SUCCESS(rv, nsnull);
513 rv = prefService->SavePrefFile(nsnull);
514 NS_ENSURE_SUCCESS(rv, nsnull);
516 return newLibraryInfo.forget();
527 sbLocalDatabaseLibraryLoader::PromptToDeleteLibraries()
532 do_GetService(
"@mozilla.org/embedcomp/prompt-service;1", &rv);
533 NS_ENSURE_SUCCESS(rv, rv);
535 PRUint32 buttons = nsIPromptService::BUTTON_POS_0 * nsIPromptService::BUTTON_TITLE_IS_STRING +
536 nsIPromptService::BUTTON_POS_1 * nsIPromptService::BUTTON_TITLE_IS_STRING +
537 nsIPromptService::BUTTON_POS_1_DEFAULT;
539 PRInt32 promptResult;
544 nsAutoString dialogTitle = bundle.
Get(
"corruptdatabase.dialog.title");
546 nsAutoString dialogText = bundle.
Get(
"corruptdatabase.dialog.text");
548 nsAutoString deleteText = bundle.
Get(
"corruptdatabase.dialog.buttons.delete");
550 nsAutoString continueText =
551 bundle.
Get(
"corruptdatabase.dialog.buttons.cancel");
555 rv = promptService->ConfirmEx(nsnull,
556 dialogTitle.BeginReading(),
557 dialogText.BeginReading(),
559 deleteText.BeginReading(),
560 continueText.BeginReading(),
565 NS_ENSURE_SUCCESS(rv, rv);
569 if (promptResult == 0) {
570 m_DeleteLibrariesAtShutdown = PR_TRUE;
572 #ifdef METRICS_ENABLED
574 nsCOMPtr<sbIMetrics> metrics =
575 do_CreateInstance(
"@songbirdnest.com/Songbird/Metrics;1", &rv);
576 NS_ASSERTION(NS_SUCCEEDED(rv),
"Failed to get metrics service");
580 nsString metricsCategory = NS_LITERAL_STRING(
"app");
581 nsString metricsId = NS_LITERAL_STRING(
"library.error.reset");
582 rv = metrics->MetricsInc(metricsCategory, metricsId, EmptyString());
583 NS_ASSERTION(NS_SUCCEEDED(rv),
"Failed to post metric");
585 #endif // METRICS_ENABLED
588 nsCOMPtr<nsIAppStartup> appStartup =
589 (do_GetService(NS_APPSTARTUP_CONTRACTID, &rv));
590 NS_ENSURE_SUCCESS(rv, rv);
592 appStartup->Quit(nsIAppStartup::eForceQuit | nsIAppStartup::eRestart);
599 sbLocalDatabaseLibraryLoader::PromptInaccessibleLibraries()
603 nsCOMPtr<nsIPromptService> promptService =
604 do_GetService(
"@mozilla.org/embedcomp/prompt-service;1", &rv);
605 NS_ENSURE_SUCCESS(rv, rv);
607 PRUint32 buttons = nsIPromptService::BUTTON_POS_0 * nsIPromptService::BUTTON_TITLE_IS_STRING +
608 nsIPromptService::BUTTON_POS_0_DEFAULT;
610 PRInt32 promptResult;
615 nsTArray<nsString> params;
616 nsCOMPtr<nsIProperties> dirService =
617 do_GetService(
"@mozilla.org/file/directory_service;1", &rv);
618 NS_ENSURE_SUCCESS(rv, rv);
621 rv = dirService->Get(
"ProfD",
623 getter_AddRefs(profileDir));
624 NS_ENSURE_SUCCESS(rv, rv);
626 nsString profilePath;
627 rv = profileDir->GetPath(profilePath);
628 NS_ENSURE_SUCCESS(rv, rv);
630 params.AppendElement(profilePath);
632 nsCOMPtr<nsIPrefBranch> prefBranch =
634 NS_ENSURE_SUCCESS(rv, rv);
637 char* urlBuffer = nsnull;
639 if (NS_SUCCEEDED(rv)) {
640 url.Assign(NS_ConvertUTF8toUTF16(nsCString(urlBuffer)));
645 NS_ENSURE_SUCCESS_BODY(rv, rv);
647 url = bundle.
Get(
"database.inaccessible.dialog.url",
"<error>");
649 params.AppendElement(url);
651 nsAutoString dialogTitle = bundle.
Get(
"database.inaccessible.dialog.title");
652 nsAutoString dialogText = bundle.
Format(
"database.inaccessible.dialog.text",
654 nsAutoString buttonText = bundle.
Get(
"database.inaccessible.dialog.buttons.quit");
657 rv = promptService->ConfirmEx(nsnull,
658 dialogTitle.BeginReading(),
659 dialogText.BeginReading(),
661 buttonText.BeginReading(),
667 NS_ENSURE_SUCCESS(rv, rv);
670 nsCOMPtr<nsIAppStartup> appStartup =
671 (do_GetService(NS_APPSTARTUP_CONTRACTID, &rv));
672 NS_ENSURE_SUCCESS(rv, rv);
674 appStartup->Quit(nsIAppStartup::eForceQuit);
680 sbLocalDatabaseLibraryLoader::RemovePrefBranch(
const nsACString& aPrefBranch)
683 nsCOMPtr<nsIPrefService> prefService =
685 NS_ENSURE_SUCCESS(rv,);
687 nsCAutoString prefBranch(aPrefBranch);
689 nsCOMPtr<nsIPrefBranch> doomedBranch;
690 rv = prefService->GetBranch(prefBranch.get(), getter_AddRefs(doomedBranch));
691 NS_ENSURE_SUCCESS(rv,);
693 rv = doomedBranch->DeleteBranch(
"");
694 NS_ENSURE_SUCCESS(rv,);
696 rv = prefService->SavePrefFile(nsnull);
697 NS_ENSURE_SUCCESS(rv,);
701 sbLocalDatabaseLibraryLoader::GetNextLibraryIndex()
703 for (PRUint32 index = 0; ; index++) {
704 if (!mLibraryInfoTable.Get(index, nsnull)) {
708 NS_NOTREACHED(
"Shouldn't be here!");
712 PLDHashOperator PR_CALLBACK
713 sbLocalDatabaseLibraryLoader::LoadLibrariesCallback(nsUint32HashKey::KeyType aKey,
717 sbLoaderInfo* loaderInfo =
static_cast<sbLoaderInfo*
>(aUserData);
718 NS_ASSERTION(loaderInfo,
"Doh, how did this happen?!");
721 return PL_DHASH_NEXT;
725 NS_ASSERTION(databaseFile,
"Must have a file here!");
727 nsCOMPtr<sbILibrary> library;
729 loaderInfo->libraryFactory->CreateLibraryFromDatabase(databaseFile,
730 getter_AddRefs(library));
731 NS_ENSURE_SUCCESS(rv, PL_DHASH_NEXT);
733 rv = loaderInfo->libraryManager->RegisterLibrary(library, PR_TRUE);
734 NS_ENSURE_SUCCESS(rv, PL_DHASH_NEXT);
736 return PL_DHASH_NEXT;
739 PLDHashOperator PR_CALLBACK
740 sbLocalDatabaseLibraryLoader::LibraryExistsCallback(nsUint32HashKey::KeyType aKey,
744 sbLibraryExistsInfo* existsInfo =
745 static_cast<sbLibraryExistsInfo*
>(aUserData);
746 NS_ASSERTION(existsInfo,
"Doh, how did this happen?!");
748 nsAutoString resourceGUID;
750 NS_ASSERTION(!resourceGUID.IsEmpty(),
"GUID can't be empty here!");
752 if (resourceGUID.Equals(existsInfo->resourceGUID)) {
753 existsInfo->index = (PRInt32)aKey;
754 return PL_DHASH_STOP;
757 return PL_DHASH_NEXT;
760 PLDHashOperator PR_CALLBACK
761 sbLocalDatabaseLibraryLoader::VerifyEntriesCallback(nsUint32HashKey::KeyType aKey,
762 nsAutoPtr<sbLibraryLoaderInfo>& aEntry,
765 nsCAutoString prefBranch;
766 aEntry->GetPrefBranch(prefBranch);
767 NS_ASSERTION(!prefBranch.IsEmpty(),
"This can't be empty!");
769 nsAutoString databaseGUID;
770 aEntry->GetDatabaseGUID(databaseGUID);
771 if (databaseGUID.IsEmpty()) {
772 NS_WARNING(
"One of the libraries was missing a database GUID and will be removed.");
773 RemovePrefBranch(prefBranch);
774 return PL_DHASH_REMOVE;
777 nsAutoString resourceGUID;
778 aEntry->GetResourceGUID(resourceGUID);
779 if (resourceGUID.IsEmpty()) {
780 NS_WARNING(
"One of the libraries was missing a resource GUID and will be removed.");
781 RemovePrefBranch(prefBranch);
782 return PL_DHASH_REMOVE;
785 nsCOMPtr<nsILocalFile> location = aEntry->GetDatabaseLocation();
787 NS_WARNING(
"One of the libraries had no location and will be removed.");
788 RemovePrefBranch(prefBranch);
789 return PL_DHASH_REMOVE;
792 return PL_DHASH_NEXT;
799 sbLocalDatabaseLibraryLoader::OnRegisterStartupLibraries(
sbILibraryManager* aLibraryManager)
801 TRACE(
"sbLocalDatabaseLibraryLoader[0x%x] - LoadLibraries",
this);
803 nsresult rv =
Init();
804 NS_ENSURE_SUCCESS(rv, rv);
806 rv = EnsureDefaultLibraries();
807 NS_ENSURE_SUCCESS(rv, rv);
809 nsCOMPtr<sbILibraryFactory> localDatabaseLibraryFactory =
811 NS_ENSURE_SUCCESS(rv, rv);
816 sbLoaderInfo info(aLibraryManager, libraryFactory);
819 mLibraryInfoTable.EnumerateRead(LoadLibrariesCallback, &info);
826 sbLocalDatabaseLibraryLoader::OnLibraryStartupModified(
sbILibrary* aLibrary,
827 PRBool aLoadAtStartup)
829 NS_ENSURE_ARG_POINTER(aLibrary);
832 nsCOMPtr<sbILibraryFactory> factory;
833 nsresult rv = aLibrary->GetFactory(getter_AddRefs(factory));
834 NS_ENSURE_SUCCESS(rv, rv);
836 nsAutoString factoryType;
837 rv = factory->GetType(factoryType);
838 NS_ENSURE_SUCCESS(rv, rv);
841 NS_ERROR_NOT_AVAILABLE);
844 nsAutoString resourceGUID;
845 rv = aLibrary->GetGuid(resourceGUID);
846 NS_ENSURE_SUCCESS(rv, rv);
848 sbLibraryExistsInfo existsInfo(resourceGUID);
849 mLibraryInfoTable.EnumerateRead(LibraryExistsCallback, &existsInfo);
852 if ((existsInfo.index == -1) ||
853 (!mLibraryInfoTable.Get(existsInfo.index, &libraryInfo))) {
857 nsCOMPtr<nsIPropertyBag2> creationParameters;
858 rv = aLibrary->GetCreationParameters(getter_AddRefs(creationParameters));
859 NS_ENSURE_SUCCESS(rv, rv);
862 nsCOMPtr<nsILocalFile> databaseFile;
863 rv = creationParameters->GetPropertyAsInterface(fileKey,
864 NS_GET_IID(nsILocalFile),
865 getter_AddRefs(databaseFile));
866 NS_ENSURE_SUCCESS(rv, rv);
868 NS_ASSERTION(databaseFile,
"This can't be null!");
871 PRUint32 index = GetNextLibraryIndex();
874 prefKey.AppendInt(index);
875 prefKey.AppendLiteral(
".");
877 nsAutoPtr<sbLibraryLoaderInfo>
878 newLibraryInfo(CreateDefaultLibraryInfo(prefKey, EmptyString(),
880 if (!newLibraryInfo || !mLibraryInfoTable.Put(index, newLibraryInfo)) {
881 return NS_ERROR_FAILURE;
884 rv = newLibraryInfo->SetDatabaseLocation(databaseFile);
885 NS_ENSURE_SUCCESS(rv, rv);
887 libraryInfo = newLibraryInfo.forget();
891 NS_ENSURE_SUCCESS(rv, rv);
898 sbLocalDatabaseLibraryLoader::Observe(
nsISupports *aSubject,
900 const PRUnichar *
aData)
905 if (m_DetectedCorruptLibrary) {
909 nsCOMPtr<nsIProperties> dirService =
910 do_GetService(
"@mozilla.org/file/directory_service;1", &rv);
911 NS_ENSURE_SUCCESS(rv, rv);
913 nsCOMPtr<nsIFile> prefFile;
914 rv = dirService->Get(
"PrefF",
916 getter_AddRefs(prefFile));
917 NS_ENSURE_SUCCESS(rv, rv);
923 PRBool prefWritable = PR_TRUE;
924 rv = prefFile->Exists(&prefExists);
925 if (NS_SUCCEEDED(rv) && prefExists) {
926 rv = prefFile->IsWritable(&prefWritable);
927 NS_ENSURE_SUCCESS(rv, rv);
931 rv = PromptToDeleteLibraries();
932 NS_ENSURE_SUCCESS(rv, rv);
935 rv = PromptInaccessibleLibraries();
936 NS_ENSURE_SUCCESS(rv, rv);
939 }
else if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
942 if (m_DeleteLibrariesAtShutdown) {
944 nsCOMPtr<nsIProperties> directoryService(
945 do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv));
946 NS_ENSURE_SUCCESS(rv, rv);
948 nsCOMPtr<nsIFile> siteDBDir;
949 rv = directoryService->Get(
"ProfD", NS_GET_IID(nsIFile),
950 getter_AddRefs(siteDBDir));
951 NS_ENSURE_SUCCESS(rv, rv);
954 siteDBDir->Append(NS_LITERAL_STRING(
"db"));
959 nsCOMPtr<nsISimpleEnumerator> dirEnumerator;
960 rv = siteDBDir->GetDirectoryEntries(getter_AddRefs(dirEnumerator));
961 NS_ENSURE_SUCCESS(rv, rv);
964 dirEnumerator->HasMoreElements(&hasMore);
965 while (hasMore && NS_SUCCEEDED(rv)) {
966 nsCOMPtr<nsISupports> aSupport;
967 rv = dirEnumerator->GetNext(getter_AddRefs(aSupport));
968 if (NS_FAILED(rv))
break;
969 nsCOMPtr<nsIFile> curFile(do_QueryInterface(aSupport, &rv));
970 if (NS_FAILED(rv))
break;
973 rv = curFile->GetLeafName(leafName);
974 if (NS_FAILED(rv))
break;
976 #ifdef METRICS_ENABLED
977 if (leafName.Compare(NS_LITERAL_STRING(
"metrics.db")) != 0) {
978 rv = curFile->Remove(
false);
979 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Unable to delete file");
982 rv = curFile->Remove(
false);
983 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Unable to delete file");
986 dirEnumerator->HasMoreElements(&hasMore);
990 nsCAutoString scancompleteBranch(
"songbird.firstrun.scancomplete");
991 sbLocalDatabaseLibraryLoader::RemovePrefBranch(scancompleteBranch);
999 sbLocalDatabaseLibraryLoader::RemovePrefBranch(prefBranch);
1015 nsCOMPtr<nsIPrefService> prefService =
1017 NS_ENSURE_SUCCESS(rv, rv);
1019 nsCAutoString prefBranchString(aPrefKey);
1020 rv = prefService->GetBranch(prefBranchString.get(),
1021 getter_AddRefs(mPrefBranch));
1022 NS_ENSURE_SUCCESS(rv, rv);
1031 rv = mPrefBranch->PrefHasUserValue(mStartupKey.get(), &exists);
1032 NS_ENSURE_SUCCESS(rv, rv);
1036 NS_ENSURE_SUCCESS(rv, rv);
1045 NS_ENSURE_FALSE(aGUID.IsEmpty(), NS_ERROR_INVALID_ARG);
1048 nsCOMPtr<nsISupportsString> supportsString =
1049 do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
1050 NS_ENSURE_SUCCESS(rv, rv);
1052 rv = supportsString->SetData(aGUID);
1053 NS_ENSURE_SUCCESS(rv, rv);
1055 rv = mPrefBranch->SetComplexValue(mDatabaseGUIDKey.get(),
1067 nsCOMPtr<nsISupportsString> supportsString;
1068 nsresult rv = mPrefBranch->GetComplexValue(mDatabaseGUIDKey.get(),
1070 getter_AddRefs(supportsString));
1071 NS_ENSURE_SUCCESS(rv,);
1073 rv = supportsString->GetData(_retval);
1074 NS_ENSURE_SUCCESS(rv,);
1080 NS_ENSURE_ARG_POINTER(aLocation);
1083 nsCOMPtr<nsIFile>
file = do_QueryInterface(aLocation, &rv);
1084 NS_ENSURE_SUCCESS(rv, rv);
1086 nsCAutoString filePath;
1087 rv = file->GetNativePath(filePath);
1088 NS_ENSURE_SUCCESS(rv, rv);
1090 rv = mPrefBranch->SetCharPref(mLocationKey.get(), filePath.get());
1091 NS_ENSURE_SUCCESS(rv, rv);
1096 already_AddRefed<nsILocalFile>
1100 nsCOMPtr<nsILocalFile> location = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
1101 NS_ENSURE_SUCCESS(rv, nsnull);
1103 nsCAutoString filePath;
1104 rv = mPrefBranch->GetCharPref(mLocationKey.get(), getter_Copies(filePath));
1105 NS_ENSURE_SUCCESS(rv, nsnull);
1107 rv = location->InitWithNativePath(filePath);
1108 NS_ENSURE_SUCCESS(rv, nsnull);
1110 nsILocalFile* _retval;
1111 NS_ADDREF(_retval = location);
1118 nsresult rv = mPrefBranch->SetBoolPref(mStartupKey.get(), aLoadAtStartup);
1119 NS_ENSURE_SUCCESS(rv, rv);
1127 PRBool loadAtStartup;
1128 nsresult rv = mPrefBranch->GetBoolPref(mStartupKey.get(), &loadAtStartup);
1129 NS_ENSURE_SUCCESS(rv, PR_FALSE);
1131 return loadAtStartup;
1137 NS_ENSURE_FALSE(aGUID.IsEmpty(), NS_ERROR_INVALID_ARG);
1140 nsCOMPtr<nsISupportsString> supportsString =
1141 do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
1142 NS_ENSURE_SUCCESS(rv, rv);
1144 rv = supportsString->SetData(aGUID);
1145 NS_ENSURE_SUCCESS(rv, rv);
1147 rv = mPrefBranch->SetComplexValue(mResourceGUIDKey.get(),
1159 nsCOMPtr<nsISupportsString> supportsString;
1160 nsresult rv = mPrefBranch->GetComplexValue(mResourceGUIDKey.get(),
1162 getter_AddRefs(supportsString));
1163 NS_ENSURE_SUCCESS(rv,);
1165 rv = supportsString->GetData(_retval);
1166 NS_ENSURE_SUCCESS(rv,);
1174 nsCAutoString prefBranch;
1175 nsresult rv = mPrefBranch->GetRoot(getter_Copies(prefBranch));
1176 NS_ENSURE_SUCCESS(rv,);
1178 _retval.Assign(prefBranch);
#define DBENGINE_GUID_MAIN_LIBRARY
nsresult Init(const nsACString &aPrefKey)
#define SB_PRLOG_SETUP(x)
#define MINIMUM_LIBRARY_COUNT
#define PREF_DATABASE_LOCATION
static nsCOMPtr< nsIObserverService > observerService
const NS_PREFSERVICE_CONTRACTID
already_AddRefed< nsILocalFile > GetDatabaseLocation()
nsString Get(const nsAString &aKey, const nsAString &aDefault=SBVoidString())
nsString Format(const nsAString &aKey, nsTArray< nsString > &aParams, const nsAString &aDefault=SBVoidString())
#define SB_CUSTOMTYPE_PLAYQUEUE_LIBRARY
void GetGUIDFromFile(nsILocalFile *aFile, nsAString &aGUID)
#define LOADERINFO_VALUE_COUNT
Songbird Library Manager Definition.
#define SB_PROPERTY_ISSORTABLE
NS_IMPL_ISUPPORTS2(sbAlbumArtService, sbIAlbumArtService, nsIObserver) NS_IMETHODIMP sbAlbumArtService
nsresult SetLoadAtStartup(PRBool aLoadAtStartup)
#define PREF_RESOURCE_GUID
#define PREFBRANCH_LOADER
#define SB_LOCALDATABASE_LIBRARYFACTORY_CONTRACTID
Manages the lifecycle of libraries in the system.
#define PROPERTY_KEY_DATABASEFILE
#define SB_CUSTOMTYPE_MAIN_LIBRARY
already_AddRefed< nsILocalFile > GetFileForGUID(const nsAString &aGUID)
#define SB_PREF_PLAYQUEUE_LIBRARY
#define SB_NAMEKEY_MAIN_LIBRARY
#define SB_PROPERTY_DEFAULTCOLUMNSPEC
#define SB_CUSTOMTYPE_WEB_LIBRARY
#define DEFAULT_COLUMNSPEC_WEB_LIBRARY
nsresult SetDatabaseLocation(nsILocalFile *aLocation)
void GetPrefBranch(nsACString &_retval)
#define NS_FINAL_UI_STARTUP_CATEGORY
#define PREF_LOAD_AT_STARTUP
Media library abstraction.
void GetResourceGUID(nsAString &_retval)
Songbird String Bundle Definitions.
#define SB_PREF_MAIN_LIBRARY
#define SB_NAMEKEY_PLAYQUEUE_LIBRARY
#define DBENGINE_GUID_PLAYQUEUE_LIBRARY
#define PREF_SUPPORT_INACCESSIBLE_LIBRARY
#define DBENGINE_GUID_WEB_LIBRARY
StringArrayEnumerator prototype hasMore
PRBool GetLoadAtStartup()
#define SB_PREF_WEB_LIBRARY
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
void GetDatabaseGUID(nsAString &_retval)
#define SB_LOCALDATABASE_LIBRARYFACTORY_TYPE
nsresult SetDatabaseGUID(const nsAString &aGUID)
nsresult CreateLibraryFromDatabase(nsIFile *aDatabase, sbILibrary **_retval, nsIPropertyBag2 *aCreationParameters=nsnull, nsString aResourceGUID=EmptyString())
#define SB_PROPERTY_CUSTOMTYPE
#define SB_NAMEKEY_WEB_LIBRARY
nsresult SetResourceGUID(const nsAString &aGUID)
#define SB_UNUSED_IN_RELEASE(decl)
#define PREF_DATABASE_GUID
_updateTextAndScrollDataForFrame aData
Songbird Database Object Definition.