28 #include <nsArrayUtils.h>
30 #include <nsIClassInfo.h>
31 #include <nsIClassInfoImpl.h>
33 #include <nsIFileURL.h>
34 #include <nsIMutableArray.h>
35 #include <nsIObserverService.h>
36 #include <nsIPrefBranch.h>
37 #include <nsIPrefService.h>
38 #include <nsIProgrammingLanguage.h>
39 #include <nsIPropertyBag2.h>
40 #include <nsIProxyObjectManager.h>
41 #include <nsISimpleEnumerator.h>
42 #include <nsIStringEnumerator.h>
43 #include <nsISupportsPrimitives.h>
44 #include <nsIThread.h>
46 #include <nsIUUIDGenerator.h>
47 #include <nsIWritablePropertyBag2.h>
48 #include <sbIDatabaseQuery.h>
49 #include <sbIDatabaseResult.h>
50 #include <sbIDevice.h>
51 #include <sbIDeviceManager.h>
52 #include <sbILibraryFactory.h>
53 #include <sbILibraryManager.h>
54 #include <sbILibraryResource.h>
55 #include <sbILocalDatabaseLibraryCopyListener.h>
56 #include <sbILocalDatabaseGUIDArray.h>
57 #include <sbILocalDatabaseMigrationHelper.h>
58 #include <sbILocalDatabasePropertyCache.h>
59 #include <sbILocalDatabaseSimpleMediaList.h>
60 #include <sbIMediacoreTypeSniffer.h>
61 #include <sbIMediaItem.h>
62 #include <sbIMediaList.h>
63 #include <sbIMediaListView.h>
64 #include <sbISQLBuilder.h>
68 #include <sbIDatabaseEngine.h>
69 #include <nsAutoLock.h>
70 #include <nsAutoPtr.h>
72 #include <nsComponentManagerUtils.h>
73 #include <nsTHashtable.h>
74 #include <nsHashKeys.h>
76 #include <nsIInputStreamPump.h>
78 #include <nsNetUtil.h>
79 #include <nsServiceManagerUtils.h>
80 #include <nsThreadUtils.h>
82 #include <nsWeakReference.h>
100 #include <nsIVariant.h>
101 #include <nsUnicharUtils.h>
104 #define NS_UUID_GENERATOR_CONTRACTID "@mozilla.org/uuid-generator;1"
106 #define SB_MEDIAITEM_TYPEID 0
108 #define DEFAULT_ANALYZE_COUNT_LIMIT 1000
109 #define ANALYZE_COUNT_PREF "songbird.library.localdatabase.analyzeCountLimit"
110 #define INVALID_COLLATION_INDEX_PREF_PREFIX "songbird.databaseengine."
111 #define INVALID_COLLATION_INDEX_PREF_SUFFIX ".invalidCollationIndex"
113 #define DEFAULT_MEDIAITEM_CACHE_SIZE 2500
114 #define DEFAULT_MEDIALIST_CACHE_SIZE 25
116 #define SB_MEDIALIST_FACTORY_DEFAULT_TYPE 1
117 #define SB_MEDIALIST_FACTORY_URI_PREFIX "medialist('"
118 #define SB_MEDIALIST_FACTORY_URI_SUFFIX "')"
124 #define SHUTDOWN_ASYNC_GRANULARITY_MS 1000
128 #define BATCHCREATE_NOTIFICATION_INTERVAL_MS 100
131 #define SB_ILIBRESOURCE_CAST(_ptr) \
132 static_cast<sbILibraryResource*>(static_cast<sbIMediaItem*>(static_cast<sbLocalDatabaseMediaItem*>(_ptr)))
133 #define SB_IMEDIALIST_CAST(_ptr) \
134 static_cast<sbIMediaList*>(static_cast<sbLocalDatabaseMediaListBase*>(_ptr))
135 #define SB_IMEDIAITEM_CAST(_ptr) \
136 static_cast<sbIMediaItem*>(static_cast<sbLocalDatabaseMediaItem*>(_ptr))
138 #define DEFAULT_SORT_PROPERTY SB_PROPERTY_CREATED
140 #define DEFAULT_FETCH_SIZE 1000
147 static PRLogModuleInfo* gLibraryLog = nsnull;
150 #define TRACE(args) PR_LOG(gLibraryLog, PR_LOG_DEBUG, args)
151 #define LOG(args) PR_LOG(gLibraryLog, PR_LOG_WARN, args)
154 #define LOG_SUBMESSAGE_SPACE " - "
164 template<
class V,
typename T>
165 PLDHashOperator PR_CALLBACK
170 nsInterfaceHashtableMT<V, T> *newHash =
171 reinterpret_cast<nsInterfaceHashtableMT<V, T>*
>(aUserData);
173 NS_ASSERTION(newHash->IsInitialized(),
"copying to uninitialized hashtable!");
175 PRBool success = newHash->Put(aKey, aData);
177 return success ? PL_DHASH_NEXT : PL_DHASH_STOP;
193 : mLocalDatabaseLibrary(aLocalDatabaseLibrary)
194 , mAddItemsListener(aListener)
195 , mMediaItems(aMediaItems) {}
199 rv = mLocalDatabaseLibrary->AddMediaItems(mMediaItems,
202 NS_ENSURE_SUCCESS(rv, rv);
207 nsRefPtr<sbLocalDatabaseLibrary> mLocalDatabaseLibrary;
213 nsCOMPtr<sbIAddMediaItemsListener> mAddItemsListener;
218 nsCOMPtr<nsISimpleEnumerator> mMediaItems;
234 NS_ENSURE_ARG_POINTER(_retval);
237 nsresult rv = mFriendLibrary->GetLength(&mLength);
238 NS_ENSURE_SUCCESS(rv, rv);
249 sbLibraryInsertingEnumerationListener::OnEnumeratedItem(
sbIMediaList* aMediaList,
253 NS_ENSURE_ARG_POINTER(aMediaItem);
254 NS_ENSURE_ARG_POINTER(_retval);
257 nsresult rv = mFriendLibrary->ContainsCopy(aMediaItem, &containsCopy);
258 NS_ENSURE_SUCCESS(rv, rv);
260 nsCOMPtr<sbIMediaItem> newMediaItem;
261 rv = mFriendLibrary->AddItemToLocalDatabase(aMediaItem,
262 getter_AddRefs(newMediaItem));
263 NS_ENSURE_SUCCESS(rv, rv);
267 rv = mListener->OnItemAdded(newMediaItem);
273 NS_ENSURE_SUCCESS(rv, rv);
276 PRBool success = mNotificationList.AppendObject(newMediaItem);
277 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
279 success = mOriginalItemList.AppendObject(aMediaItem);
280 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
282 mShouldInvalidate = PR_TRUE;
293 sbLibraryInsertingEnumerationListener::OnEnumerationEnd(
sbIMediaList* aMediaList,
294 nsresult aStatusCode)
298 if (mShouldInvalidate) {
299 NS_ASSERTION(mFriendLibrary->
GetArray(),
"Uh, no full array?!");
303 rv = mFriendLibrary->
GetArray()->Invalidate(PR_TRUE);
304 NS_ENSURE_SUCCESS(rv, rv);
308 rv = mListener->OnComplete();
309 NS_ENSURE_SUCCESS(rv, rv);
312 nsCOMPtr<sbIMediaList> libraryList =
314 NS_ENSURE_SUCCESS(rv, rv);
317 PRUint32
count = mNotificationList.Count();
319 PRUint32 originalItemsCount = mOriginalItemList.Count();
323 NS_ENSURE_TRUE(count == originalItemsCount, NS_ERROR_UNEXPECTED);
325 nsCOMPtr<sbILibrary> originalLibrary;
326 nsCOMPtr<sbILocalDatabaseLibrary> originalLocalDatabaseLibrary;
328 for (PRUint32
i = 0;
i <
count;
i++) {
331 mNotificationList[
i],
336 rv = mOriginalItemList[
i]->GetLibrary(getter_AddRefs(originalLibrary));
337 NS_ENSURE_SUCCESS(rv, rv);
339 originalLocalDatabaseLibrary = do_QueryInterface(originalLibrary, &rv);
340 NS_ENSURE_SUCCESS(rv, rv);
343 originalLocalDatabaseLibrary->NotifyCopyListenersItemCopied(mOriginalItemList[i],
344 mNotificationList[i]);
360 NS_ENSURE_ARG_POINTER(_retval);
363 nsresult rv = mFriendLibrary->MakeStandardQuery(getter_AddRefs(mDBQuery));
364 NS_ENSURE_SUCCESS(rv, rv);
366 rv = mDBQuery->AddQuery(NS_LITERAL_STRING(
"begin"));
367 NS_ENSURE_SUCCESS(rv, rv);
378 sbLibraryRemovingEnumerationListener::OnEnumeratedItem(
sbIMediaList* aMediaList,
382 NS_ENSURE_ARG_POINTER(aMediaItem);
383 NS_ENSURE_ARG_POINTER(_retval);
386 nsresult rv = mFriendLibrary->
IndexOf(aMediaItem, 0, &index);
388 if (rv == NS_ERROR_NOT_AVAILABLE) {
391 NS_ENSURE_SUCCESS(rv, rv);
394 PRBool success = mNotificationList.AppendObject(aMediaItem);
395 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
397 PRUint32* added = mNotificationIndexes.AppendElement(index);
398 NS_ENSURE_TRUE(added, NS_ERROR_OUT_OF_MEMORY);
400 mItemEnumerated = PR_TRUE;
411 sbLibraryRemovingEnumerationListener::OnEnumerationEnd(
sbIMediaList* aMediaList,
412 nsresult aStatusCode)
414 if (!mItemEnumerated) {
415 NS_WARNING(
"OnEnumerationEnd called with no items enumerated");
420 nsCOMPtr<sbIMediaList> libraryList =
422 NS_ENSURE_SUCCESS(rv, rv);
426 PRBool success = map.Init();
427 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
430 rv = mFriendLibrary->GetContainingLists(&mNotificationList, &lists, &map);
431 NS_ENSURE_SUCCESS(rv, rv);
434 success = itemIndexes.Init();
435 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
437 map.EnumerateRead(sbLocalDatabaseLibrary::NotifyListsBeforeItemRemoved,
440 nsCOMPtr<sbIDatabasePreparedStatement> deleteItemPreparedStatement;
441 rv = mDBQuery->PrepareQuery(
442 NS_LITERAL_STRING(
"DELETE FROM media_items WHERE guid = ?"),
443 getter_AddRefs(deleteItemPreparedStatement));
444 NS_ENSURE_SUCCESS(rv, rv);
446 PRUint32 totalOffset = 1;
447 PRUint32 count = mNotificationList.Count();
449 for (PRUint32 i = 0; i <
count; i++) {
450 nsCOMPtr<sbIMediaItem> item = do_QueryInterface(mNotificationList[i], &rv);
451 NS_ENSURE_SUCCESS(rv, rv);
456 mNotificationIndexes[i]);
464 PRUint32
const j = i + 1;
465 if(j < count && mNotificationIndexes[j] > mNotificationIndexes[i]) {
466 mNotificationIndexes[j] -= totalOffset++;
470 rv = item->GetGuid(guid);
471 NS_ENSURE_SUCCESS(rv, rv);
474 mFriendLibrary->mMediaItemTable.Remove(guid);
477 rv = mDBQuery->AddPreparedStatement(deleteItemPreparedStatement);
478 NS_ENSURE_SUCCESS(rv, rv);
480 rv = mDBQuery->BindStringParameter(0, guid);
481 NS_ENSURE_SUCCESS(rv, rv);
484 rv = mDBQuery->AddQuery(NS_LITERAL_STRING(
"commit"));
485 NS_ENSURE_SUCCESS(rv, rv);
488 rv = mDBQuery->Execute(&dbSuccess);
489 NS_ENSURE_SUCCESS(rv, rv);
490 NS_ENSURE_TRUE(dbSuccess == 0, NS_ERROR_FAILURE);
494 rv = mFriendLibrary->
GetArray()->Invalidate(PR_TRUE);
495 NS_ENSURE_SUCCESS(rv, rv);
498 for (PRInt32 i = 0; i < lists.Count(); i++) {
499 nsCOMPtr<sbILocalDatabaseSimpleMediaList> simple =
500 do_QueryInterface(lists[i], &rv);
501 NS_ENSURE_SUCCESS(rv, rv);
505 rv = simple->Invalidate(PR_TRUE);
506 NS_ENSURE_SUCCESS(rv, rv);
510 map.EnumerateRead(sbLocalDatabaseLibrary::NotifyListsAfterItemRemoved,
514 for (PRUint32 i = 0; i <
count; i++) {
516 mNotificationList[i],
517 mNotificationIndexes[i]);
540 sbLocalDatabaseLibrary::sbLocalDatabaseLibrary()
542 mPreventAddedNotification(PR_FALSE),
547 gLibraryLog = PR_NewLogModule(
"sbLocalDatabaseLibrary");
550 TRACE((
"LocalDatabaseLibrary[0x%.8x] - Constructed",
this));
555 TRACE((
"LocalDatabaseLibrary[0x%.8x] - Destructed",
this));
557 nsAutoMonitor::DestroyMonitor(mMonitor);
563 nsIPropertyBag2* aCreationParameters,
565 nsIURI* aDatabaseLocation)
567 TRACE((
"LocalDatabaseLibrary[0x%.8x] - Init()",
this));
568 NS_ENSURE_FALSE(aDatabaseGuid.IsEmpty(), NS_ERROR_INVALID_ARG);
569 NS_ENSURE_ARG_POINTER(aCreationParameters);
570 NS_ENSURE_ARG_POINTER(aFactory);
572 mDatabaseGuid = aDatabaseGuid;
573 mCreationParameters = aCreationParameters;
577 mDatabaseLocation = aDatabaseLocation;
580 PRBool needsMigration = PR_FALSE;
582 PRUint32 fromVersion = 0;
583 PRUint32 toVersion = 0;
585 nsresult rv = NeedsMigration(&needsMigration, &fromVersion, &toVersion);
586 NS_ENSURE_SUCCESS(rv, rv);
589 rv = MigrateLibrary(fromVersion, toVersion);
590 NS_ENSURE_SUCCESS(rv, rv);
594 PRBool needsReindexCollations = PR_FALSE;
595 rv = NeedsReindexCollations(&needsReindexCollations);
596 NS_ENSURE_SUCCESS(rv, rv);
598 if (needsReindexCollations) {
599 rv = ReindexCollations();
600 NS_ENSURE_SUCCESS(rv, rv);
603 PRBool success = mCopyListeners.Init();
604 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
608 nsCOMPtr<sbIDatabaseQuery> query;
609 rv = MakeStandardQuery(getter_AddRefs(query));
610 NS_ENSURE_SUCCESS(rv, rv);
612 rv = query->AddQuery(NS_LITERAL_STRING(
"SELECT value FROM library_metadata WHERE name = 'resource-guid'"));
613 NS_ENSURE_SUCCESS(rv, rv);
616 rv = query->Execute(&dbOk);
617 NS_ENSURE_SUCCESS(rv, rv);
618 NS_ENSURE_TRUE(dbOk == 0, NS_ERROR_FAILURE);
620 nsCOMPtr<sbIDatabaseResult> result;
621 rv = query->GetResultObject(getter_AddRefs(result));
622 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
624 PRUint32 rowCount = 0;
625 rv = result->GetRowCount(&rowCount);
626 NS_ENSURE_SUCCESS(rv, rv);
628 NS_ENSURE_TRUE(rowCount == 1, NS_ERROR_UNEXPECTED);
631 rv = result->GetRowCell(0, 0, guid);
632 NS_ENSURE_SUCCESS(rv, rv);
635 nsRefPtr<sbLocalDatabasePropertyCache>
637 NS_ENSURE_TRUE(propCache, NS_ERROR_OUT_OF_MEMORY);
639 rv = propCache->Init(
this, guid);
640 NS_ENSURE_SUCCESS(rv, rv);
642 mPropertyCache = propCache;
645 NS_ENSURE_TRUE (mLengthCache, NS_ERROR_OUT_OF_MEMORY);
648 NS_ENSURE_TRUE(
GetArray(), NS_ERROR_OUT_OF_MEMORY);
650 rv =
GetArray()->SetDatabaseGUID(aDatabaseGuid);
651 NS_ENSURE_SUCCESS(rv, rv);
653 if (mDatabaseLocation) {
654 rv =
GetArray()->SetDatabaseLocation(aDatabaseLocation);
655 NS_ENSURE_SUCCESS(rv, rv);
658 rv =
GetArray()->SetBaseTable(NS_LITERAL_STRING(
"media_items"));
659 NS_ENSURE_SUCCESS(rv, rv);
662 NS_ENSURE_SUCCESS(rv, rv);
665 NS_ENSURE_SUCCESS(rv, rv);
667 rv =
GetArray()->SetPropertyCache(mPropertyCache);
668 NS_ENSURE_SUCCESS(rv, rv);
670 rv =
GetArray()->SetLengthCache(mLengthCache);
671 NS_ENSURE_SUCCESS(rv, rv);
673 rv = CreateQueries();
674 NS_ENSURE_SUCCESS(rv, rv);
682 NS_ENSURE_SUCCESS(rv, rv);
685 success = mMediaListFactoryTable.Init();
686 NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
688 rv = RegisterDefaultMediaListFactories();
689 NS_ENSURE_SUCCESS(rv, rv);
693 NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
697 NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
699 InitializeLibraryStatistics();
703 nsCOMPtr<nsIPrefBranch> prefBranch =
705 if (NS_SUCCEEDED(rv)) {
708 if (NS_SUCCEEDED(rv)) {
709 mAnalyzeCountLimit = PR_MAX(1, prefValue);
716 do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &rv);
717 NS_ENSURE_SUCCESS(rv, rv);
718 rv = observerService->AddObserver(
this,
721 NS_ENSURE_SUCCESS(rv, rv);
723 mMonitor = nsAutoMonitor::NewMonitor(
"sbLocalDatabaseLibrary::mMonitor");
724 NS_ENSURE_TRUE(mMonitor, NS_ERROR_OUT_OF_MEMORY);
727 nsCOMPtr<sbILocalDatabaseMediaItem> item =
728 do_QueryInterface(NS_ISUPPORTS_CAST(
sbILibrary *,
this), &rv);
729 NS_ENSURE_SUCCESS(rv, rv);
731 item->SetSuppressNotifications(PR_FALSE);
739 nsresult sbLocalDatabaseLibrary::CreateQueries()
741 nsCOMPtr<sbIDatabaseQuery> query;
742 nsresult rv = MakeStandardQuery(getter_AddRefs(query), PR_FALSE);
743 NS_ENSURE_SUCCESS(rv, rv);
745 rv = query->PrepareQuery(NS_LITERAL_STRING(
"\
746 INSERT INTO media_items \
747 (guid, created, updated, content_url, hidden, media_list_type_id, is_list) \
748 values (?, ?, ?, ?, ?, ?, ?)"), getter_AddRefs(mCreateMediaItemPreparedStatement));
749 NS_ENSURE_SUCCESS(rv, rv);
751 query->PrepareQuery(NS_LITERAL_STRING(
"\
752 SELECT _mlt.type, _mi.content_mime_type \
753 FROM media_items as _mi \
754 LEFT JOIN media_list_types as _mlt ON _mi.media_list_type_id = _mlt.media_list_type_id \
755 WHERE _mi.guid = ?"),
756 getter_AddRefs(mGetTypeForGUID));
757 NS_ENSURE_SUCCESS(rv, rv);
759 query->PrepareQuery(NS_LITERAL_STRING(
"\
762 WHERE metadata_hash_identity = ? and guid != ?"),
763 getter_AddRefs(mGetGUIDForIdentity));
764 NS_ENSURE_SUCCESS(rv, rv);
766 query->PrepareQuery(NS_LITERAL_STRING(
"\
767 SELECT count(media_item_id) \
769 WHERE metadata_hash_identity = ? and guid != ?"),
770 getter_AddRefs(mGetCountForIdentity));
771 NS_ENSURE_SUCCESS(rv, rv);
783 TRACE((
"LocalDatabaseLibrary[0x%.8x] - MakeStandardQuery()",
this));
785 nsCOMPtr<sbIDatabaseQuery> query =
787 NS_ENSURE_SUCCESS(rv, rv);
789 rv = query->SetDatabaseGUID(mDatabaseGuid);
790 NS_ENSURE_SUCCESS(rv, rv);
793 if (mDatabaseLocation) {
794 rv = query->SetDatabaseLocation(mDatabaseLocation);
795 NS_ENSURE_SUCCESS(rv, rv);
798 rv = query->SetAsyncQuery(aRunAsync);
799 NS_ENSURE_SUCCESS(rv, rv);
801 NS_ADDREF(*_retval = query);
811 PRUint32 len = PR_snprintf(buf,
sizeof(buf),
"%lld",
812 (PRUint64)(PR_Now() / PR_USEC_PER_MSEC));
813 _retval.Assign(NS_ConvertASCIItoUTF16(buf, len));
829 const PRUint32 aMediaItemTypeID,
830 const nsAString& aURISpec,
833 NS_ENSURE_ARG_POINTER(aQuery);
835 TRACE((
"LocalDatabaseLibrary[0x%.8x] - AddNewItemQuery(%d, %s)",
this,
836 aMediaItemTypeID, NS_LossyConvertUTF16toASCII(aURISpec).
get()));
838 nsresult rv = aQuery->AddPreparedStatement(mCreateMediaItemPreparedStatement);
839 NS_ENSURE_SUCCESS(rv, rv);
842 nsCOMPtr<nsIUUIDGenerator> uuidGen =
844 NS_ENSURE_SUCCESS(rv, rv);
847 rv = uuidGen->GenerateUUIDInPlace(&
id);
848 NS_ENSURE_SUCCESS(rv, rv);
850 char guidChars[NSID_LENGTH];
851 id.ToProvidedString(guidChars);
853 nsString
guid(NS_ConvertASCIItoUTF16(nsDependentCString(guidChars + 1,
857 rv = aQuery->BindStringParameter(0, guid);
858 NS_ENSURE_SUCCESS(rv, rv);
861 nsAutoString createdTimeString;
864 rv = aQuery->BindStringParameter(1, createdTimeString);
865 NS_ENSURE_SUCCESS(rv, rv);
867 rv = aQuery->BindStringParameter(2, createdTimeString);
868 NS_ENSURE_SUCCESS(rv, rv);
873 rv = aQuery->BindStringParameter(3, aURISpec);
874 NS_ENSURE_SUCCESS(rv, rv);
877 rv = aQuery->BindInt32Parameter(4, 0);
878 NS_ENSURE_SUCCESS(rv, rv);
881 rv = aQuery->BindNullParameter(5);
882 NS_ENSURE_SUCCESS(rv, rv);
885 rv = aQuery->BindInt32Parameter(6, 0);
886 NS_ENSURE_SUCCESS(rv, rv);
891 nsAutoString newSpec;
892 newSpec.AssignLiteral(
"songbird-medialist://");
893 newSpec.Append(
mGuid);
894 newSpec.AppendLiteral(
"/");
895 newSpec.Append(guid);
897 rv = aQuery->BindStringParameter(3, newSpec);
898 NS_ENSURE_SUCCESS(rv, rv);
901 rv = aQuery->BindInt32Parameter(4, 0);
902 NS_ENSURE_SUCCESS(rv, rv);
905 rv = aQuery->BindInt32Parameter(5, aMediaItemTypeID);
906 NS_ENSURE_SUCCESS(rv, rv);
909 rv = aQuery->BindInt32Parameter(6, 1);
910 NS_ENSURE_SUCCESS(rv, rv);
913 _retval.Assign(guid);
918 sbLocalDatabaseLibrary::SetDefaultItemProperties(
sbIMediaItem* aItem,
920 sbMediaItemInfo* aItemInfo)
922 NS_ASSERTION(aItem,
"aItem is null");
925 nsCOMPtr<sbIPropertyArray> properties(aProperties);
931 do_CreateInstance(
"@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1", &rv);
932 NS_ENSURE_SUCCESS(rv, rv);
938 NS_ENSURE_SUCCESS(rv, rv);
940 nsCOMPtr<nsIURI>
uri;
941 if (!NS_IsMainThread()) {
945 NS_ENSURE_SUCCESS(rv, rv);
947 rv = ioService->NewURI(NS_ConvertUTF16toUTF8(url), nsnull,
948 nsnull, getter_AddRefs(uri));
949 NS_ENSURE_SUCCESS(rv, rv);
951 nsCOMPtr<nsIThread>
target;
952 rv = NS_GetMainThread(getter_AddRefs(target));
953 NS_ENSURE_SUCCESS(rv, rv);
955 nsCOMPtr<nsIURI> proxiedURI;
959 NS_PROXY_SYNC | NS_PROXY_ALWAYS,
960 getter_AddRefs(proxiedURI));
961 NS_ENSURE_SUCCESS(rv, rv);
965 nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
966 NS_ENSURE_SUCCESS(rv, rv);
968 rv = ioService->NewURI(NS_ConvertUTF16toUTF8(url), nsnull,
969 nsnull, getter_AddRefs(uri));
970 NS_ENSURE_SUCCESS(rv, rv);
976 rv = properties->GetPropertyValue(
978 if (NS_FAILED(rv) || contentType.IsEmpty()) {
979 nsCOMPtr<sbIMediacoreTypeSniffer> typeSniffer =
980 do_CreateInstance(
"@songbirdnest.com/Songbird/Mediacore/TypeSniffer;1", &rv);
981 NS_ENSURE_SUCCESS(rv, rv);
983 PRBool isVideo = PR_FALSE;
984 rv = typeSniffer->IsValidVideoURL(uri, &isVideo);
985 if (NS_SUCCEEDED(rv) && isVideo) {
986 nsCOMPtr<sbIMutablePropertyArray> mutableProperties =
987 do_QueryInterface(properties, &rv);
988 NS_ENSURE_SUCCESS(rv, rv);
990 rv = mutableProperties->AppendProperty(
992 NS_LITERAL_STRING(
"video"));
993 NS_ENSURE_SUCCESS(rv, rv);
997 nsCOMPtr<sbIPropertyArray> filteredProperties;
1000 getter_AddRefs(filteredProperties));
1001 NS_ENSURE_SUCCESS(rv, rv);
1003 aItemInfo->hasAudioType = contentType.EqualsLiteral(
"audio");
1004 aItemInfo->hasVideoType = contentType.EqualsLiteral(
"video");
1009 nsCOMPtr<sbILocalDatabaseMediaItem> item =
1010 do_QueryInterface(aItem, &rv);
1011 NS_ENSURE_SUCCESS(rv, rv);
1012 item->SetSuppressNotifications(PR_TRUE);
1013 rv = aItem->SetProperties(filteredProperties);
1014 NS_ENSURE_SUCCESS(rv, rv);
1015 item->SetSuppressNotifications(PR_FALSE);
1028 sbLocalDatabaseLibrary::GetTypeForGUID(
const nsAString& aGUID,
1031 TRACE((
"LocalDatabaseLibrary[0x%.8x] - GetTypeForGUID(%s)",
this,
1032 NS_LossyConvertUTF16toASCII(aGUID).
get()));
1035 sbMediaItemInfo* itemInfo;
1036 if (mMediaItemTable.Get(aGUID, &itemInfo) && itemInfo->hasListType) {
1039 _retval.Assign(itemInfo->listType);
1044 nsCOMPtr<sbIDatabaseQuery> query;
1045 nsresult rv = MakeStandardQuery(getter_AddRefs(query));
1046 NS_ENSURE_SUCCESS(rv, rv);
1048 rv = query->AddPreparedStatement(mGetTypeForGUID);
1049 NS_ENSURE_SUCCESS(rv, rv);
1051 rv = query->BindStringParameter(0, aGUID);
1052 NS_ENSURE_SUCCESS(rv, rv);
1055 rv = query->Execute(&dbresult);
1056 NS_ENSURE_SUCCESS(rv, rv);
1057 NS_ENSURE_TRUE(dbresult == 0, NS_ERROR_FAILURE);
1059 nsCOMPtr<sbIDatabaseResult> result;
1060 rv = query->GetResultObject(getter_AddRefs(result));
1061 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
1064 rv = result->GetRowCount(&rowCount);
1065 NS_ENSURE_SUCCESS(rv, rv);
1067 if (rowCount == 0) {
1069 return NS_ERROR_NOT_AVAILABLE;
1073 rv = result->GetRowCell(0, 0, type);
1074 NS_ENSURE_SUCCESS(rv, rv);
1077 rv = result->GetRowCell(0, 1, contentType);
1078 NS_ENSURE_SUCCESS(rv, rv);
1082 nsAutoPtr<sbMediaItemInfo> newItemInfo(
new sbMediaItemInfo());
1083 NS_ENSURE_TRUE(newItemInfo, NS_ERROR_OUT_OF_MEMORY);
1085 PRBool success = mMediaItemTable.Put(aGUID, newItemInfo);
1086 NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
1088 itemInfo = newItemInfo.forget();
1091 itemInfo->listType.Assign(type);
1092 itemInfo->hasListType = PR_TRUE;
1093 itemInfo->hasAudioType = contentType.EqualsLiteral(
"audio");
1094 itemInfo->hasVideoType = contentType.EqualsLiteral(
"video");
1096 _retval.Assign(type);
1103 PLDHashOperator PR_CALLBACK
1104 sbLocalDatabaseLibrary::AddTypesToArrayCallback(nsStringHashKey::KeyType aKey,
1105 sbMediaListFactoryInfo* aEntry,
1108 NS_ASSERTION(aEntry,
"Null entry in the hash?!");
1109 NS_ASSERTION(aUserData,
"Null userData!");
1112 NS_ASSERTION(aEntry->factory,
"Null factory!");
1115 aEntry->factory->GetContractID(contractID);
1117 TRACE((
"LocalDatabaseLibrary - AddTypesToArrayCallback(%s, %s)",
1118 NS_LossyConvertUTF16toASCII(aKey).
get(), contractID.get()));
1122 nsTArray<nsString>*
array =
1123 static_cast<nsTArray<nsString>*
>(aUserData);
1124 NS_ENSURE_TRUE(array, PL_DHASH_STOP);
1126 nsString* newElement = array->AppendElement(aKey);
1127 NS_ENSURE_TRUE(newElement, PL_DHASH_STOP);
1129 return PL_DHASH_NEXT;
1136 sbLocalDatabaseLibrary::RegisterDefaultMediaListFactories()
1138 nsCOMPtr<sbIMediaListFactory>
factory;
1140 NS_ENSURE_TRUE(factory, NS_ERROR_OUT_OF_MEMORY);
1142 nsresult rv = RegisterMediaListFactory(factory);
1143 NS_ENSURE_SUCCESS(rv, rv);
1146 NS_ENSURE_TRUE(factory, NS_ERROR_OUT_OF_MEMORY);
1148 rv = RegisterMediaListFactory(factory);
1149 NS_ENSURE_SUCCESS(rv, rv);
1153 NS_ENSURE_SUCCESS(rv, rv);
1155 rv = RegisterMediaListFactory(factory);
1156 NS_ENSURE_SUCCESS(rv, rv);
1167 sbLocalDatabaseLibrary::DeleteDatabaseItem(
const nsAString& aGuid)
1169 nsCOMPtr<sbIDatabaseQuery> query;
1170 nsresult rv = MakeStandardQuery(getter_AddRefs(query));
1171 NS_ENSURE_SUCCESS(rv, rv);
1173 rv = query->AddQuery(NS_LITERAL_STRING(
"DELETE FROM media_items WHERE guid = ?"));
1174 NS_ENSURE_SUCCESS(rv, rv);
1176 rv = query->BindStringParameter(0, aGuid);
1177 NS_ENSURE_SUCCESS(rv, rv);
1180 rv = query->Execute(&dbOk);
1181 NS_ENSURE_SUCCESS(rv, rv);
1182 NS_ENSURE_TRUE(dbOk == 0, NS_ERROR_FAILURE);
1194 nsCOMPtr<sbIMediaItem> libraryAsItem = do_QueryInterface(aLibrary);
1195 if (!libraryAsItem) {
1199 nsString innderDeviceLibraryGuid;
1200 rv = libraryAsItem->GetProperty
1202 innderDeviceLibraryGuid);
1203 return NS_SUCCEEDED(rv) && !innderDeviceLibraryGuid.IsEmpty();
1209 sbLocalDatabaseLibrary::AddItemToLocalDatabase(
sbIMediaItem* aMediaItem,
1212 NS_ASSERTION(aMediaItem,
"Null pointer!");
1213 NS_ASSERTION(_retval,
"Null pointer!");
1216 nsCOMPtr<nsIURI> contentUri;
1217 nsresult rv = aMediaItem->GetContentSrc(getter_AddRefs(contentUri));
1218 NS_ENSURE_SUCCESS(rv, rv);
1220 nsCOMPtr<sbIPropertyArray> properties;
1221 rv = aMediaItem->GetProperties(nsnull, getter_AddRefs(properties));
1222 NS_ENSURE_SUCCESS(rv, rv);
1224 nsCOMPtr<sbIMutablePropertyArray> mutableProperties =
1225 do_QueryInterface(properties, &rv);
1226 NS_ENSURE_SUCCESS(rv, rv);
1228 nsCOMPtr<sbILibrary> oldLibrary;
1229 rv = aMediaItem->GetLibrary(getter_AddRefs(oldLibrary));
1230 NS_ENSURE_SUCCESS(rv, rv);
1234 NS_ENSURE_SUCCESS(rv, rv);
1236 nsCOMPtr<sbIMediaItem> newItem;
1239 nsCOMPtr<sbIMediaList> itemAsList = do_QueryInterface(aMediaItem, &rv);
1241 if (NS_SUCCEEDED(rv)) {
1243 rv = itemAsList->GetType(type);
1244 NS_ENSURE_SUCCESS(rv, rv);
1246 NS_ASSERTION(!type.IsEmpty(),
"GetType returned an empty type!");
1248 PRUint32 otherListLength;
1249 rv = itemAsList->GetLength(&otherListLength);
1250 NS_ENSURE_SUCCESS(rv, rv);
1255 mPreventAddedNotification = PR_TRUE;
1262 nsCOMPtr<sbILibrary> itemLibrary;
1263 nsresult rv = aMediaItem->GetLibrary(getter_AddRefs(itemLibrary));
1264 NS_ENSURE_SUCCESS(rv, rv);
1268 NS_ENSURE_SUCCESS(rv, rv);
1270 nsCOMPtr<sbIMediaList> newList;
1271 PRBool forceCreateAsSimple = !
equals;
1273 if (!forceCreateAsSimple) {
1274 rv = CreateMediaList(type, properties, getter_AddRefs(newList));
1275 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Couldn't create media list!");
1278 forceCreateAsSimple = PR_TRUE;
1281 if (forceCreateAsSimple) {
1282 nsCOMPtr<sbIPropertyArray> simpleProperties;
1283 rv = GetSimpleMediaListCopyProperties(itemAsList,
1284 getter_AddRefs(simpleProperties));
1285 NS_ENSURE_SUCCESS(rv, rv);
1287 rv = CreateMediaList(NS_LITERAL_STRING(
"simple"), simpleProperties,
1288 getter_AddRefs(newList));
1289 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Couldn't create simple media list!");
1292 if (NS_SUCCEEDED(rv) && otherListLength) {
1293 rv = newList->AddAll(itemAsList);
1296 if (NS_FAILED(rv)) {
1298 NS_WARNING(
"AddAll failed!");
1301 nsCOMPtr<sbIMediaItem> newListAsItem =
1302 do_QueryInterface(newList, &rvOther);
1303 NS_WARN_IF_FALSE(NS_SUCCEEDED(rvOther),
"Couldn't QI!");
1305 if (NS_SUCCEEDED(rvOther)) {
1306 rvOther =
Remove(newListAsItem);
1307 NS_WARN_IF_FALSE(NS_SUCCEEDED(rvOther),
"Couldn't remove new list!");
1311 mPreventAddedNotification = PR_FALSE;
1313 NS_ENSURE_SUCCESS(rv, rv);
1315 newItem = do_QueryInterface(newList, &rv);
1316 NS_ENSURE_SUCCESS(rv, rv);
1323 rv = properties->GetPropertyValue(PROP_ORIGINURL, originURL);
1324 if (rv == NS_ERROR_NOT_AVAILABLE) {
1326 rv = contentUri->GetSpec(spec);
1327 NS_ENSURE_SUCCESS(rv, rv);
1329 rv = mutableProperties->AppendProperty(PROP_ORIGINURL,
1330 NS_ConvertUTF8toUTF16(spec));
1331 NS_ENSURE_SUCCESS(rv, rv);
1338 if (isCopyingFromDevice || isCopyingToDevice) {
1340 NS_LITERAL_STRING(
"1"));
1346 mPreventAddedNotification = PR_TRUE;
1347 rv = CreateMediaItem(contentUri, properties, PR_TRUE,
1348 getter_AddRefs(newItem));
1349 mPreventAddedNotification = PR_FALSE;
1350 NS_ENSURE_SUCCESS(rv, rv);
1353 NS_ENSURE_SUCCESS(rv, rv);
1355 if (isCopyingFromDevice) {
1356 SubmitCopyRequest(aMediaItem, newItem);
1360 newItem.swap(*_retval);
1373 sbLocalDatabaseLibrary::GetSimpleMediaListCopyProperties
1377 NS_ASSERTION(aMediaList,
"Null pointer!");
1378 NS_ASSERTION(aSimpleProperties,
"Null pointer!");
1383 nsCOMPtr<sbIPropertyArray> properties;
1384 rv = aMediaList->GetProperties(nsnull, getter_AddRefs(properties));
1385 NS_ENSURE_SUCCESS(rv, rv);
1388 nsCOMPtr<sbIMutablePropertyArray> simpleProperties =
1390 NS_ENSURE_SUCCESS(rv, rv);
1394 rv = properties->GetLength(&propCount);
1395 NS_ENSURE_SUCCESS(rv, rv);
1396 for (PRUint32 i = 0; i < propCount; i++) {
1398 nsCOMPtr<sbIProperty> property;
1399 rv = properties->GetPropertyAt(i, getter_AddRefs(property));
1400 NS_ENSURE_SUCCESS(rv, rv);
1413 rv =
property->GetId(
id);
1414 NS_ENSURE_SUCCESS(rv, rv);
1426 rv =
property->GetValue(value);
1427 NS_ENSURE_SUCCESS(rv, rv);
1428 rv = simpleProperties->AppendProperty(
id, value);
1429 NS_ENSURE_SUCCESS(rv, rv);
1433 nsAutoString listName;
1434 rv = aMediaList->GetName(listName);
1435 NS_ENSURE_SUCCESS(rv, rv);
1436 rv = simpleProperties->AppendProperty
1439 NS_ENSURE_SUCCESS(rv, rv);
1443 NS_ENSURE_SUCCESS(rv, rv);
1446 nsCOMPtr<sbIPropertyArray>
1447 returnSimpleProperties = do_QueryInterface(simpleProperties, &rv);
1448 NS_ENSURE_SUCCESS(rv, rv);
1449 returnSimpleProperties.forget(aSimpleProperties);
1464 NS_ASSERTION(aItems,
"aItems is null");
1465 NS_ASSERTION(aLists,
"aLists is null");
1466 NS_ASSERTION(aMap,
"aMap is null");
1467 NS_ASSERTION(aMap->IsInitialized(),
"aMap not initalized");
1471 nsTHashtable<nsISupportsHashKey> distinctLists;
1472 PRBool success = distinctLists.Init();
1473 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
1475 nsCOMPtr<sbIDatabaseQuery> query;
1476 rv = MakeStandardQuery(getter_AddRefs(query), PR_FALSE);
1477 NS_ENSURE_SUCCESS(rv, rv);
1481 nsCOMPtr<sbIDatabasePreparedStatement> preparedStatement;
1482 rv = query->PrepareQuery(NS_LITERAL_STRING(
"\
1483 SELECT list.guid, item.guid FROM simple_media_lists as sml\
1484 JOIN media_items AS list ON list.media_item_id = sml.media_item_id\
1485 JOIN media_items AS item ON item.media_item_id = sml.member_media_item_id\
1486 WHERE sml.member_media_item_id = ?"), getter_AddRefs(preparedStatement));
1487 NS_ENSURE_SUCCESS(rv,rv);
1489 PRUint32 count = aItems->Count();
1490 for (PRUint32 i = 0; i <
count; i++) {
1494 rv = this->Equals(aItems->ObjectAt(i), &isLibrary);
1495 NS_ENSURE_SUCCESS(rv, rv);
1500 nsCOMPtr<sbILocalDatabaseMediaItem> item =
1501 do_QueryInterface(aItems->ObjectAt(i), &rv);
1502 if (rv == NS_NOINTERFACE) {
1508 rv = item->GetMediaItemId(&mediaItemId);
1509 NS_ENSURE_SUCCESS(rv, rv);
1511 rv = query->AddPreparedStatement(preparedStatement);
1512 NS_ENSURE_SUCCESS(rv, rv);
1514 rv = query->BindInt32Parameter(0, mediaItemId);
1515 NS_ENSURE_SUCCESS(rv, rv);
1519 rv = query->Execute(&dbresult);
1520 NS_ENSURE_SUCCESS(rv, rv);
1521 NS_ENSURE_TRUE(dbresult == 0, NS_ERROR_FAILURE);
1523 nsCOMPtr<sbIDatabaseResult> result;
1524 rv = query->GetResultObject(getter_AddRefs(result));
1525 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
1528 rv = result->GetRowCount(&rowCount);
1529 NS_ENSURE_SUCCESS(rv, rv);
1531 for (PRUint32 i = 0; i < rowCount; i++) {
1532 nsAutoString listGuid;
1533 rv = result->GetRowCell(i, 0, listGuid);
1534 NS_ENSURE_SUCCESS(rv, rv);
1536 nsCOMPtr<sbIMediaItem> list;
1537 rv = GetMediaItem(listGuid, getter_AddRefs(list));
1538 NS_ENSURE_SUCCESS(rv, rv);
1540 nsAutoString itemGuid;
1541 rv = result->GetRowCell(i, 1, itemGuid);
1542 NS_ENSURE_SUCCESS(rv, rv);
1544 nsCOMPtr<sbIMediaItem> containedItem;
1545 rv = GetMediaItem(itemGuid, getter_AddRefs(containedItem));
1546 NS_ENSURE_SUCCESS(rv, rv);
1550 if (!aMap->Get(containedItem, &lists)) {
1552 NS_ENSURE_TRUE(newLists, NS_ERROR_OUT_OF_MEMORY);
1553 PRBool success = aMap->Put(containedItem, newLists);
1554 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
1555 lists = newLists.forget();
1558 success = lists->AppendObject(list);
1559 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
1561 nsISupportsHashKey* addedList = distinctLists.PutEntry(list);
1562 NS_ENSURE_TRUE(addedList, NS_ERROR_OUT_OF_MEMORY);
1564 rv = query->ResetQuery();
1565 NS_ENSURE_SUCCESS(rv, rv);
1567 distinctLists.EnumerateEntries(EntriesToMediaListArray, aLists);
1573 sbLocalDatabaseLibrary::GetAllListsByType(
const nsAString& aType,
1576 NS_ASSERTION(aArray,
"aArray is null");
1580 sbMediaListFactoryInfo* factoryInfo;
1581 PRBool success = mMediaListFactoryTable.Get(aType, &factoryInfo);
1582 NS_ENSURE_TRUE(success, NS_ERROR_INVALID_ARG);
1584 nsCOMPtr<sbIDatabaseQuery> query;
1585 rv = MakeStandardQuery(getter_AddRefs(query), PR_FALSE);
1586 NS_ENSURE_SUCCESS(rv, rv);
1588 rv = query->AddQuery(NS_LITERAL_STRING(
1589 "SELECT guid FROM media_items WHERE media_list_type_id = ?"));
1590 NS_ENSURE_SUCCESS(rv, rv);
1592 rv = query->BindInt32Parameter(0, factoryInfo->typeID);
1593 NS_ENSURE_SUCCESS(rv, rv);
1596 rv = query->Execute(&dbresult);
1597 NS_ENSURE_SUCCESS(rv, rv);
1598 NS_ENSURE_TRUE(dbresult == 0, NS_ERROR_FAILURE);
1600 nsCOMPtr<sbIDatabaseResult> result;
1601 rv = query->GetResultObject(getter_AddRefs(result));
1602 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
1605 rv = result->GetRowCount(&rowCount);
1606 NS_ENSURE_SUCCESS(rv, rv);
1608 for (PRUint32 i = 0; i < rowCount; i++) {
1610 rv = result->GetRowCell(i, 0, guid);
1611 NS_ENSURE_SUCCESS(rv, rv);
1613 nsCOMPtr<sbIMediaItem> item;
1614 rv = GetMediaItem(guid, getter_AddRefs(item));
1615 NS_ENSURE_SUCCESS(rv, rv);
1617 nsCOMPtr<sbIMediaList> list = do_QueryInterface(item, &rv);
1618 NS_ENSURE_SUCCESS(rv, rv);
1620 PRBool success = aArray->AppendObject(list);
1621 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
1642 sbLocalDatabaseLibrary::ConvertURIsToStrings(nsIArray* aURIs, nsStringArray** aStringArray)
1644 TRACE((
"ConvertURIsToStrings[0x%.8x] - ConvertURIsToStrings()",
this));
1646 NS_ENSURE_ARG_POINTER(aURIs);
1647 NS_ENSURE_ARG_POINTER(aStringArray);
1651 rv = aURIs->GetLength(&length);
1652 NS_ENSURE_SUCCESS(rv, rv);
1654 nsAutoPtr<nsStringArray> strArray(
new nsStringArray(length));
1656 for (PRUint32 i = 0; i <
length; i++) {
1657 nsAutoString uriSpec;
1658 nsCOMPtr<nsISupportsString> uriStr = do_QueryElementAt(aURIs, i, &rv);
1659 if (!NS_SUCCEEDED(rv)) {
1661 nsCOMPtr<nsIURI> uri = do_QueryElementAt(aURIs, i, &rv);
1662 NS_ENSURE_SUCCESS(rv, rv);
1665 rv = uri->GetSpec(spec);
1666 NS_ENSURE_SUCCESS(rv, rv);
1667 uriSpec = NS_ConvertUTF8toUTF16(spec);
1669 rv = uriStr->GetData(uriSpec);
1670 NS_ENSURE_SUCCESS(rv, rv);
1672 strArray->AppendString(uriSpec);
1674 *aStringArray = strArray.forget();
1688 sbLocalDatabaseLibrary::ContainsCopy(
sbIMediaItem* aMediaItem,
1689 PRBool* aContainsCopy)
1691 NS_ENSURE_ARG_POINTER(aMediaItem);
1692 NS_ENSURE_ARG_POINTER(aContainsCopy);
1698 nsCOMPtr<sbILibrary> itemLibrary;
1699 rv = aMediaItem->GetLibrary(getter_AddRefs(itemLibrary));
1700 NS_ENSURE_SUCCESS(rv, rv);
1704 NS_ENSURE_SUCCESS(rv, rv);
1707 *aContainsCopy = PR_TRUE;
1713 static_cast<sbLocalDatabaseMediaListBase*>(
this),
1715 if (rv != NS_ERROR_NOT_AVAILABLE) {
1716 NS_ENSURE_SUCCESS(rv, rv);
1717 *aContainsCopy = PR_TRUE;
1723 static_cast<sbLocalDatabaseMediaListBase*>(
this),
1725 if (rv != NS_ERROR_NOT_AVAILABLE) {
1726 NS_ENSURE_SUCCESS(rv, rv);
1727 *aContainsCopy = PR_TRUE;
1731 *aContainsCopy = PR_FALSE;
1736 sbLocalDatabaseLibrary::FilterExistingItems
1737 (nsStringArray* aURIs,
1738 nsIArray* aPropertyArrayArray,
1739 nsTArray<PRUint32>* aFilteredIndexArray,
1740 nsStringArray** aFilteredURIs,
1741 nsIArray** aFilteredPropertyArrayArray)
1743 TRACE((
"LocalDatabaseLibrary[0x%.8x] - FilterExistingItems()",
this));
1745 NS_ENSURE_ARG_POINTER(aURIs);
1746 NS_ENSURE_ARG_POINTER(aFilteredURIs);
1748 if (aFilteredIndexArray)
1749 aFilteredIndexArray->Clear();
1751 PRUint32 length = aURIs->Count();
1754 *aFilteredURIs = aURIs;
1755 if (aPropertyArrayArray)
1756 NS_IF_ADDREF(*aFilteredPropertyArrayArray = aPropertyArrayArray);
1760 nsTHashtable<nsStringHashKey> uniques;
1761 PRBool success = uniques.Init(length);
1762 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
1765 nsCOMPtr<sbIDatabaseQuery> query;
1766 rv = MakeStandardQuery(getter_AddRefs(query), PR_FALSE);
1767 NS_ENSURE_SUCCESS(rv, rv);
1769 nsCOMPtr<sbISQLSelectBuilder> builder =
1771 NS_ENSURE_SUCCESS(rv, rv);
1773 rv = builder->AddColumn(EmptyString(), NS_LITERAL_STRING(
"content_url"));
1774 NS_ENSURE_SUCCESS(rv, rv);
1776 rv = builder->SetBaseTableName(NS_LITERAL_STRING(
"media_items"));
1777 NS_ENSURE_SUCCESS(rv, rv);
1779 nsCOMPtr<sbISQLBuilderCriterionIn> inCriterionContentURL;
1780 rv = builder->CreateMatchCriterionIn(EmptyString(),
1781 NS_LITERAL_STRING(
"content_url"),
1782 getter_AddRefs(inCriterionContentURL));
1783 NS_ENSURE_SUCCESS(rv, rv);
1785 rv = builder->AddCriterion(inCriterionContentURL);
1786 NS_ENSURE_SUCCESS(rv, rv);
1789 PRUint32 incount = 0;
1791 PRUint32 noOfDups = 0;
1792 for (PRUint32 i = 0; i <
length; i++) {
1793 nsAutoString uriSpec;
1794 aURIs->StringAt(i, uriSpec);
1797 if (!uniques.GetEntry(uriSpec)) {
1798 nsStringHashKey* hashKey = uniques.PutEntry(uriSpec);
1799 NS_ENSURE_TRUE(hashKey != nsnull, NS_ERROR_OUT_OF_MEMORY);
1801 rv = inCriterionContentURL->AddString(uriSpec);
1802 NS_ENSURE_SUCCESS(rv, rv);
1812 rv = builder->ToString(sql);
1813 NS_ENSURE_SUCCESS(rv, rv);
1816 rv = query->AddQuery(sql);
1817 NS_ENSURE_SUCCESS(rv, rv);
1819 rv = inCriterionContentURL->Clear();
1820 NS_ENSURE_SUCCESS(rv, rv);
1827 rv = query->Execute(&dbresult);
1828 NS_ENSURE_SUCCESS(rv, rv);
1829 NS_ENSURE_TRUE(dbresult == 0, NS_ERROR_FAILURE);
1831 nsCOMPtr<sbIDatabaseResult> result;
1832 rv = query->GetResultObject(getter_AddRefs(result));
1833 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
1836 rv = result->GetRowCount(&rowCount);
1837 NS_ENSURE_SUCCESS(rv, rv);
1839 if (rowCount == 0 && noOfDups == 0) {
1840 *aFilteredURIs = aURIs;
1841 if (aPropertyArrayArray)
1842 NS_IF_ADDREF(*aFilteredPropertyArrayArray = aPropertyArrayArray);
1843 if (aFilteredIndexArray) {
1844 for (PRUint32 i = 0; i <
length; i++) {
1845 NS_ENSURE_TRUE(aFilteredIndexArray->AppendElement(i),
1846 NS_ERROR_OUT_OF_MEMORY);
1852 for (PRUint32 i = 0; i < rowCount; i++) {
1856 rv = result->GetRowCell(i, 0, value);
1857 NS_ENSURE_SUCCESS(rv, rv);
1859 uniques.RemoveEntry(value);
1863 nsAutoPtr<nsStringArray> filteredURIs(
new nsStringArray(length - rowCount - noOfDups));
1864 nsCOMPtr<nsIMutableArray> filteredPropertyArrayArray;
1865 if (aPropertyArrayArray) {
1866 filteredPropertyArrayArray =
1867 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
1868 NS_ENSURE_SUCCESS(rv, rv);
1871 for (PRUint32 i = 0; i <
length; i++) {
1872 nsAutoString uriSpec;
1873 aURIs->StringAt(i, uriSpec);
1874 if (uniques.GetEntry(uriSpec)) {
1875 if (aFilteredIndexArray) {
1876 NS_ENSURE_TRUE(aFilteredIndexArray->AppendElement(i),
1877 NS_ERROR_OUT_OF_MEMORY);
1880 PRBool success = filteredURIs->AppendString(uriSpec);
1881 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
1883 if (aPropertyArrayArray && filteredPropertyArrayArray) {
1884 nsCOMPtr<sbIPropertyArray> properties =
1885 do_QueryElementAt(aPropertyArrayArray, i, &rv);
1886 NS_ENSURE_SUCCESS(rv, rv);
1887 rv = filteredPropertyArrayArray->AppendElement(properties, PR_FALSE);
1888 NS_ENSURE_SUCCESS(rv, rv);
1893 uniques.RemoveEntry(uriSpec);
1897 *aFilteredURIs = filteredURIs.forget();
1899 if (aFilteredPropertyArrayArray)
1900 NS_IF_ADDREF(*aFilteredPropertyArrayArray = filteredPropertyArrayArray);
1906 sbLocalDatabaseLibrary::GetGuidFromContentURI(nsIURI* aURI, nsAString& aGUID)
1908 NS_ASSERTION(aURI,
"aURIs is null");
1913 rv = aURI->GetSpec(spec);
1914 NS_ENSURE_SUCCESS(rv, rv);
1916 nsCOMPtr<sbIDatabaseQuery> query;
1917 rv = MakeStandardQuery(getter_AddRefs(query));
1918 NS_ENSURE_SUCCESS(rv, rv);
1920 rv = query->AddQuery(NS_LITERAL_STRING(
1921 "SELECT guid FROM media_items WHERE content_url = ?"
1923 NS_ENSURE_SUCCESS(rv, rv);
1925 rv = query->BindStringParameter(0, NS_ConvertUTF8toUTF16(spec));
1926 NS_ENSURE_SUCCESS(rv, rv);
1929 rv = query->Execute(&dbresult);
1930 NS_ENSURE_SUCCESS(rv, rv);
1931 NS_ENSURE_TRUE(dbresult == 0, NS_ERROR_FAILURE);
1933 nsCOMPtr<sbIDatabaseResult> result;
1934 rv = query->GetResultObject(getter_AddRefs(result));
1935 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
1938 rv = result->GetRowCount(&rowCount);
1939 NS_ENSURE_SUCCESS(rv, rv);
1941 if (rowCount == 0) {
1943 return NS_ERROR_NOT_AVAILABLE;
1946 rv = result->GetRowCell(0, 0, aGUID);
1947 NS_ENSURE_SUCCESS(rv, rv);
1953 sbLocalDatabaseLibrary::Shutdown()
1955 TRACE((
"LocalDatabaseLibrary[0x%.8x] - Shutdown()",
this));
1958 PRUint32 timerCount = (PRUint32)mBatchCreateTimers.Count();
1963 NS_ABORT_IF_FALSE(
currentThread,
"Failed to get current thread!");
1966 while (mBatchCreateTimers.Count()) {
1974 NS_ASSERTION(NS_SUCCEEDED(rv),
"NS_ProcessPendingEvents failed!");
1983 mPropertyCache = nsnull;
1985 mCreateMediaItemPreparedStatement = nsnull;
1986 mGetTypeForGUID = nsnull;
1987 mStatisticsSumPreparedStatement = nsnull;
1995 NS_ENSURE_ARG_POINTER(aLengthCache);
1996 NS_ENSURE_TRUE(mLengthCache, NS_ERROR_NOT_INITIALIZED);
1998 NS_ADDREF(*aLengthCache = mLengthCache);
2006 sbLocalDatabaseLibrary::GetContractIdForGuid(
const nsAString& aGUID,
2007 nsACString& aContractID)
2009 TRACE((
"LocalDatabaseLibrary[0x%.8x] - GetContractIdForGuid(%s)",
this,
2010 NS_LossyConvertUTF16toASCII(aGUID).
get()));
2011 nsAutoString mediaType;
2012 nsresult rv = GetTypeForGUID(aGUID, mediaType);
2013 NS_ENSURE_SUCCESS(rv, rv);
2015 if (mediaType.IsEmpty()) {
2017 aContractID.Truncate();
2021 sbMediaListFactoryInfo* factoryInfo;
2022 PRBool typeRegistered = mMediaListFactoryTable.Get(mediaType, &factoryInfo);
2023 NS_ENSURE_TRUE(typeRegistered, NS_ERROR_UNEXPECTED);
2025 NS_ASSERTION(factoryInfo->factory,
"Null factory pointer!");
2027 rv = factoryInfo->factory->GetContractID(contractID);
2028 NS_ENSURE_SUCCESS(rv, rv);
2030 aContractID.Assign(contractID);
2038 sbLocalDatabaseLibrary::GetMediaItemIdForGuid(
const nsAString& aGUID,
2039 PRUint32* aMediaItemID)
2041 TRACE((
"LocalDatabaseLibrary[0x%.8x] - GetMediaItemIdForGuid(%s)",
this,
2042 NS_LossyConvertUTF16toASCII(aGUID).
get()));
2043 NS_ENSURE_ARG_POINTER(aMediaItemID);
2045 sbMediaItemInfo* itemInfo;
2046 if (!mMediaItemTable.Get(aGUID, &itemInfo)) {
2048 nsAutoPtr<sbMediaItemInfo> newItemInfo(
new sbMediaItemInfo());
2049 NS_ENSURE_TRUE(newItemInfo, NS_ERROR_OUT_OF_MEMORY);
2051 PRBool success = mMediaItemTable.Put(aGUID, newItemInfo);
2052 NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
2054 itemInfo = newItemInfo.forget();
2056 else if (itemInfo->hasItemID) {
2057 LOG((
" - Found ID in cache!"));
2058 *aMediaItemID = itemInfo->itemID;
2062 nsCOMPtr<sbIDatabaseQuery> query;
2063 nsresult rv = MakeStandardQuery(getter_AddRefs(query));
2064 NS_ENSURE_SUCCESS(rv, rv);
2066 rv = query->AddQuery(NS_LITERAL_STRING(
"SELECT media_item_id, content_mime_type FROM \
2067 media_items WHERE guid = ?"));
2068 NS_ENSURE_SUCCESS(rv, rv);
2070 rv = query->BindStringParameter(0, aGUID);
2071 NS_ENSURE_SUCCESS(rv, rv);
2074 rv = query->Execute(&dbresult);
2075 NS_ENSURE_SUCCESS(rv, rv);
2076 NS_ENSURE_TRUE(dbresult == 0, NS_ERROR_FAILURE);
2078 nsCOMPtr<sbIDatabaseResult> result;
2079 rv = query->GetResultObject(getter_AddRefs(result));
2080 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
2083 rv = result->GetRowCount(&rowCount);
2084 NS_ENSURE_SUCCESS(rv, rv);
2088 NS_ENSURE_TRUE(rowCount, NS_ERROR_NOT_AVAILABLE);
2090 nsAutoString idString;
2091 rv = result->GetRowCell(0, 0, idString);
2092 NS_ENSURE_SUCCESS(rv, rv);
2094 PRUint32
id = idString.ToInteger(&rv);
2095 NS_ENSURE_SUCCESS(rv, rv);
2098 rv = result->GetRowCell(0, 1, contentType);
2099 NS_ENSURE_SUCCESS(rv, rv);
2101 itemInfo->itemID =
id;
2102 itemInfo->hasItemID = PR_TRUE;
2103 itemInfo->hasAudioType = contentType.EqualsLiteral(
"audio");
2104 itemInfo->hasVideoType = contentType.EqualsLiteral(
"video");
2115 sbLocalDatabaseLibrary::GetDatabaseGuid(nsAString& aDatabaseGuid)
2117 TRACE((
"LocalDatabaseLibrary[0x%.8x] - GetDatabaseGuid()",
this));
2118 aDatabaseGuid = mDatabaseGuid;
2126 sbLocalDatabaseLibrary::GetDatabaseLocation(nsIURI** aDatabaseLocation)
2128 TRACE((
"LocalDatabaseLibrary[0x%.8x] - GetDatabaseLocation()",
this));
2129 NS_ENSURE_ARG_POINTER(aDatabaseLocation);
2131 if (!mDatabaseLocation) {
2132 *aDatabaseLocation = nsnull;
2136 nsresult rv = mDatabaseLocation->Clone(aDatabaseLocation);
2137 NS_ENSURE_SUCCESS(rv, rv);
2148 TRACE((
"LocalDatabaseLibrary[0x%.8x] - GetPropertyCache()",
this));
2149 NS_ENSURE_ARG_POINTER(aPropertyCache);
2150 NS_ENSURE_TRUE(mPropertyCache, NS_ERROR_NOT_INITIALIZED);
2151 NS_ADDREF(*aPropertyCache = mPropertyCache);
2161 NS_ENSURE_ARG_POINTER(aCopyListener);
2163 nsCOMPtr<sbILocalDatabaseLibraryCopyListener> proxiedListener;
2168 NS_PROXY_ASYNC | NS_PROXY_ALWAYS,
2169 getter_AddRefs(proxiedListener));
2170 NS_ENSURE_SUCCESS(rv, rv);
2172 PRBool success = mCopyListeners.Put(aCopyListener, proxiedListener);
2173 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
2184 NS_ENSURE_ARG_POINTER(aCopyListener);
2185 mCopyListeners.Remove(aCopyListener);
2195 return MakeStandardQuery(_retval);
2205 NS_ENSURE_ARG_POINTER(aItem);
2206 NS_ENSURE_ARG_POINTER(aProperties);
2209 PRTime timer = PR_Now();
2216 sbMediaItemUpdatedInfo info(aItem, aProperties, &mMediaListTable);
2217 nsInterfaceHashtableMT<nsStringHashKey, nsIWeakReference> tableSnapshot;
2218 tableSnapshot.Init(mMediaListTable.Count());
2223 tableSnapshot.Enumerate(sbLocalDatabaseLibrary::NotifyListItemUpdated,
2236 TRACE((
"LocalDatabaseLibrary[0x%.8x] - NotifyListenersItemUpdated %d usec",
2237 this, PR_Now() - timer));
2246 sbLocalDatabaseLibrary::NotifyCopyListenersItemCopied(
sbIMediaItem *aSourceItem,
2249 NS_ENSURE_ARG_POINTER(aSourceItem);
2250 NS_ENSURE_ARG_POINTER(aDestinationItem);
2252 nsAutoPtr<sbMediaItemPair>
2253 mediaItemPair(
new sbMediaItemPair(aSourceItem, aDestinationItem));
2257 nsInterfaceHashtableMT<nsISupportsHashKey, sbILocalDatabaseLibraryCopyListener> tableSnapshot;
2258 tableSnapshot.Init(mCopyListeners.Count());
2263 mCopyListeners.EnumerateRead(sbLocalDatabaseLibrary::NotifyCopyListeners,
2269 PLDHashOperator PR_CALLBACK
2270 sbLocalDatabaseLibrary::NotifyCopyListeners(nsISupportsHashKey::KeyType aKey,
2274 NS_ASSERTION(aCopyListener,
"Null entry in the hash?!");
2275 NS_ASSERTION(aUserData,
"Null user data!");
2277 sbMediaItemPair *items =
static_cast<sbMediaItemPair *
>(aUserData);
2278 NS_ENSURE_TRUE(items, PL_DHASH_STOP);
2280 nsresult rv = aCopyListener->OnItemCopied(items->sourceItem,
2281 items->destinationItem);
2282 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2284 return PL_DHASH_NEXT;
2287 PLDHashOperator PR_CALLBACK
2288 sbLocalDatabaseLibrary::NotifyListItemUpdated(nsStringHashKey::KeyType aKey,
2289 nsCOMPtr<nsIWeakReference>& aEntry,
2292 NS_ASSERTION(aEntry,
"Null entry in the hash?!");
2293 NS_ASSERTION(aUserData,
"Null userData!");
2296 sbMediaItemUpdatedInfo* info =
2297 static_cast<sbMediaItemUpdatedInfo*
>(aUserData);
2298 NS_ENSURE_TRUE(info, PL_DHASH_STOP);
2300 nsCOMPtr<sbILocalDatabaseSimpleMediaList> simpleList;
2301 simpleList = do_QueryReferent(aEntry, &rv);
2302 if (NS_SUCCEEDED(rv)) {
2308 PRBool containsItem = PR_FALSE;
2309 nsCOMPtr<sbIMediaList> list = do_QueryInterface(simpleList, &rv);
2310 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2311 rv = list->Contains(info->item, &containsItem);
2312 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2316 rv = simpleList->NotifyListenersItemUpdated(
2317 info->item, 0, info->newProperties);
2318 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2323 info->mediaListTable->Remove(aKey);
2324 return PL_DHASH_REMOVE;
2327 return PL_DHASH_NEXT;
2330 PLDHashOperator PR_CALLBACK
2331 sbLocalDatabaseLibrary::NotifyListsBeforeItemRemoved(nsISupportsHashKey::KeyType aKey,
2335 NS_ASSERTION(aEntry,
"Null entry in the hash?!");
2336 NS_ASSERTION(aUserData,
"Null userData!");
2339 NS_ENSURE_TRUE(indexMap, PL_DHASH_STOP);
2342 nsCOMPtr<sbIMediaItem> item = do_QueryInterface(aKey, &rv);
2343 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2346 rv = item->GetGuid(itemGuid);
2347 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2349 PRUint32 count = aEntry->Count();
2350 for (PRUint32 i = 0; i <
count; i++) {
2351 nsCOMPtr<sbILocalDatabaseSimpleMediaList> simple =
2352 do_QueryInterface(aEntry->ObjectAt(i), &rv);
2353 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2355 nsCOMPtr<sbIMediaList> list = do_QueryInterface(simple, &rv);
2356 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2359 rv = list->GetGuid(listGuid);
2360 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2363 rv = list->IndexOf(item, 0, &index);
2364 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2366 listGuid.Append(itemGuid);
2367 PRBool success = indexMap->Put(listGuid, index);
2368 NS_ENSURE_TRUE(success, PL_DHASH_STOP);
2370 rv = simple->NotifyListenersBeforeItemRemoved(list, item, index);
2371 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2374 return PL_DHASH_NEXT;
2377 PLDHashOperator PR_CALLBACK
2378 sbLocalDatabaseLibrary::NotifyListsAfterItemRemoved(nsISupportsHashKey::KeyType aKey,
2382 NS_ASSERTION(aEntry,
"Null entry in the hash?!");
2383 NS_ASSERTION(aUserData,
"Null userData!");
2386 NS_ENSURE_TRUE(indexMap, PL_DHASH_STOP);
2389 nsCOMPtr<sbIMediaItem> item = do_QueryInterface(aKey, &rv);
2390 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2393 rv = item->GetGuid(itemGuid);
2394 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2396 PRUint32 count = aEntry->Count();
2397 for (PRUint32 i = 0; i <
count; i++) {
2398 nsCOMPtr<sbILocalDatabaseSimpleMediaList> simple =
2399 do_QueryInterface(aEntry->ObjectAt(i), &rv);
2400 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2402 nsCOMPtr<sbIMediaList> list = do_QueryInterface(simple, &rv);
2403 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2406 rv = list->GetGuid(listGuid);
2407 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2410 listGuid.Append(itemGuid);
2411 PRBool success = indexMap->Get(listGuid, &index);
2412 NS_ENSURE_TRUE(success, PL_DHASH_STOP);
2414 rv = simple->NotifyListenersAfterItemRemoved(list, item, index);
2415 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2418 return PL_DHASH_NEXT;
2421 PLDHashOperator PR_CALLBACK
2422 sbLocalDatabaseLibrary::NotifyListsBeforeAfterItemRemoved(nsISupportsHashKey::KeyType aKey,
2426 NS_PRECONDITION(aEntry,
"Null entry in the hash?!");
2427 NS_PRECONDITION(aUserData,
"Null userData!");
2429 sbMediaItemInfoTable* infoTable =
static_cast<sbMediaItemInfoTable*
>(aUserData);
2430 NS_ENSURE_TRUE(infoTable, PL_DHASH_STOP);
2433 nsCOMPtr<sbIMediaItem> item = do_QueryInterface(aKey, &rv);
2434 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2437 rv = item->GetGuid(itemGuid);
2438 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2440 PRUint32 count = aEntry->Count();
2441 for (PRUint32 i = 0; i <
count; i++) {
2442 nsCOMPtr<sbILocalDatabaseSimpleMediaList> simple =
2443 do_QueryInterface(aEntry->ObjectAt(i), &rv);
2444 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2446 nsCOMPtr<sbIMediaList> list = do_QueryInterface(simple, &rv);
2447 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2450 rv = list->IndexOf(item, 0, &index);
2451 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2453 rv = simple->NotifyListenersBeforeItemRemoved(list, item, index);
2454 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2456 rv = simple->NotifyListenersAfterItemRemoved(list, item, index);
2457 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2459 infoTable->Remove(itemGuid);
2462 return PL_DHASH_NEXT;
2466 PLDHashOperator PR_CALLBACK
2467 sbLocalDatabaseLibrary::EntriesToMediaListArray(nsISupportsHashKey* aEntry,
2470 NS_ASSERTION(aEntry,
"Null entry in the hash?!");
2471 NS_ASSERTION(aUserData,
"Null entry in the hash?!");
2477 nsCOMPtr<sbIMediaList> list = do_QueryInterface(aEntry->GetKey(), &rv);
2478 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
2480 PRBool success = array->AppendObject(list);
2481 NS_ENSURE_TRUE(success, PL_DHASH_STOP);
2483 return PL_DHASH_NEXT;
2486 PLDHashOperator PR_CALLBACK
2487 sbLocalDatabaseLibrary::RemoveIfNotList(nsStringHashKey::KeyType aKey,
2488 nsAutoPtr<sbMediaItemInfo> &aEntry,
2491 PRUint32 removeType = *
static_cast<PRUint32 *
>(aUserData);
2493 if (aEntry->hasListType && !aEntry->listType.IsEmpty())
2494 return PL_DHASH_NEXT;
2496 return PL_DHASH_REMOVE;
2498 return PL_DHASH_REMOVE;
2500 return PL_DHASH_REMOVE;
2502 return PL_DHASH_NEXT;
2509 sbLocalDatabaseLibrary::GetDevice(
sbIDevice** aDevice)
2511 TRACE((
"LocalDatabaseLibrary[0x%.8x] - GetDevice()",
this));
2512 return NS_ERROR_NOT_IMPLEMENTED;
2519 sbLocalDatabaseLibrary::GetSupportsForeignMediaItems(PRBool* aSupportsForeignMediaItems)
2521 TRACE((
"LocalDatabaseLibrary[0x%.8x] - GetSupportsForeignMediaItems()",
this));
2522 return NS_ERROR_NOT_IMPLEMENTED;
2529 sbLocalDatabaseLibrary::GetCreationParameters(nsIPropertyBag2** aCreationParameters)
2531 TRACE((
"LocalDatabaseLibrary[0x%.8x] - GetCreationParameters()",
this));
2532 NS_ENSURE_ARG_POINTER(aCreationParameters);
2533 NS_ENSURE_STATE(mCreationParameters);
2535 NS_ADDREF(*aCreationParameters = mCreationParameters);
2545 TRACE((
"LocalDatabaseLibrary[0x%.8x] - GetFactory()",
this));
2546 NS_ENSURE_ARG_POINTER(aFactory);
2547 NS_ENSURE_STATE(mFactory);
2549 NS_ADDREF(*aFactory = mFactory);
2557 sbLocalDatabaseLibrary::Resolve(nsIURI* aUri,
2560 NS_ENSURE_ARG_POINTER(aUri);
2561 NS_ENSURE_ARG_POINTER(_retval);
2564 nsresult rv = aUri->GetSpec(spec);
2565 NS_ENSURE_SUCCESS(rv, rv);
2567 TRACE((
"LocalDatabaseLibrary[0x%.8x] - Resolve(%s)",
this, spec.get()));
2568 return NS_ERROR_NOT_IMPLEMENTED;
2575 sbLocalDatabaseLibrary::ContainsItemWithSameIdentity
2579 NS_ENSURE_ARG_POINTER(aMediaItem);
2580 NS_ENSURE_ARG_POINTER(_retval);
2583 nsCOMPtr<sbIIdentityService> idService =
2584 do_GetService(
"@songbirdnest.com/Songbird/IdentityService;1", &rv);
2585 NS_ENSURE_SUCCESS(rv, rv);
2590 NS_ENSURE_SUCCESS(rv, rv);
2594 rv = idService->CalculateIdentityForMediaItem(aMediaItem, identity);
2595 NS_ENSURE_SUCCESS(rv, rv);
2597 nsCOMPtr<sbIDatabaseQuery> query;
2598 rv = MakeStandardQuery(getter_AddRefs(query));
2599 NS_ENSURE_SUCCESS(rv, rv);
2603 rv = query->AddPreparedStatement(mGetCountForIdentity);
2604 NS_ENSURE_SUCCESS(rv, rv);
2606 rv = query->BindStringParameter(0, identity);
2607 NS_ENSURE_SUCCESS(rv, rv);
2609 nsString paramsGuid;
2610 rv = aMediaItem->GetGuid(paramsGuid);
2611 NS_ENSURE_SUCCESS(rv, rv);
2613 rv = query->BindStringParameter(1, paramsGuid);
2614 NS_ENSURE_SUCCESS(rv, rv);
2617 rv = query->Execute(&dbOk);
2618 NS_ENSURE_SUCCESS(rv, rv);
2619 NS_ENSURE_TRUE(dbOk == 0, NS_ERROR_FAILURE);
2621 nsCOMPtr<sbIDatabaseResult> result;
2622 rv = query->GetResultObject(getter_AddRefs(result));
2623 NS_ENSURE_SUCCESS(rv, rv);
2624 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
2626 nsString foundCount;
2627 rv = result->GetRowCell(0, 0, foundCount);
2628 NS_ENSURE_SUCCESS(rv, rv);
2630 *_retval = !foundCount.EqualsLiteral(
"0");
2638 sbLocalDatabaseLibrary::GetItemsWithSameIdentity(
sbIMediaItem* aMediaItem,
2641 NS_ENSURE_ARG_POINTER(aMediaItem);
2642 NS_ENSURE_ARG_POINTER(_retval);
2645 nsCOMPtr<sbIIdentityService> idService =
2646 do_GetService(
"@songbirdnest.com/Songbird/IdentityService;1", &rv);
2647 NS_ENSURE_SUCCESS(rv, rv);
2652 NS_ENSURE_SUCCESS(rv, rv);
2656 rv = idService->CalculateIdentityForMediaItem(aMediaItem, identity);
2657 NS_ENSURE_SUCCESS(rv, rv);
2659 nsCOMPtr<sbIDatabaseQuery> query;
2660 rv = MakeStandardQuery(getter_AddRefs(query));
2661 NS_ENSURE_SUCCESS(rv, rv);
2665 rv = query->AddPreparedStatement(mGetGUIDForIdentity);
2666 NS_ENSURE_SUCCESS(rv, rv);
2668 rv = query->BindStringParameter(0, identity);
2669 NS_ENSURE_SUCCESS(rv, rv);
2671 nsString paramsGuid;
2672 rv = aMediaItem->GetGuid(paramsGuid);
2673 NS_ENSURE_SUCCESS(rv, rv);
2675 rv = query->BindStringParameter(1, paramsGuid);
2676 NS_ENSURE_SUCCESS(rv, rv);
2679 rv = query->Execute(&dbOk);
2680 NS_ENSURE_SUCCESS(rv, rv);
2681 NS_ENSURE_TRUE(dbOk == 0, NS_ERROR_FAILURE);
2683 nsCOMPtr<sbIDatabaseResult> result;
2684 rv = query->GetResultObject(getter_AddRefs(result));
2685 NS_ENSURE_SUCCESS(rv, rv);
2686 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
2690 nsCOMPtr<nsIMutableArray> sameIdentityItems =
2691 do_CreateInstance(NS_ARRAY_CONTRACTID);
2693 PRUint32 rowCount = 0;
2694 rv = result->GetRowCount(&rowCount);
2695 NS_ENSURE_SUCCESS(rv, rv);
2697 for (PRUint32 i = 0; i < rowCount; i++)
2700 rv = result->GetRowCell(i, 0, foundGUID);
2701 if (NS_SUCCEEDED(rv) && !foundGUID.IsEmpty())
2703 nsCOMPtr<sbIMediaItem> foundMediaItem;
2704 rv = GetMediaItem(foundGUID, getter_AddRefs(foundMediaItem));
2705 if (NS_SUCCEEDED(rv) && foundMediaItem)
2707 rv = sameIdentityItems->AppendElement(foundMediaItem, PR_FALSE);
2708 NS_ENSURE_SUCCESS(rv, rv);
2713 NS_ADDREF(*_retval = sameIdentityItems.get());
2722 sbLocalDatabaseLibrary::CreateMediaItem(nsIURI* aUri,
2724 PRBool aAllowDuplicates,
2727 NS_ENSURE_ARG_POINTER(aUri);
2728 NS_ENSURE_ARG_POINTER(_retval);
2731 return CreateMediaItemInternal(aUri,
2742 sbLocalDatabaseLibrary::CreateMediaItemIfNotExist(nsIURI *aContentUri,
2747 NS_ENSURE_ARG_POINTER(aContentUri);
2748 NS_ENSURE_ARG_POINTER(_retval);
2751 nsCOMPtr<sbIMediaItem> resultItem;
2753 rv = CreateMediaItemInternal(aContentUri,
2757 getter_AddRefs(resultItem));
2758 NS_ENSURE_SUCCESS(rv, rv);
2761 resultItem.forget(aResultItem);
2767 sbLocalDatabaseLibrary::CreateMediaItemInternal(nsIURI* aUri,
2769 PRBool aAllowDuplicates,
2770 PRBool* aWasCreated,
2773 NS_ENSURE_ARG_POINTER(aUri);
2774 NS_ENSURE_ARG_POINTER(aWasCreated);
2775 NS_ENSURE_ARG_POINTER(_retval);
2778 nsresult rv = aUri->GetSpec(spec);
2779 NS_ENSURE_SUCCESS(rv, rv);
2781 TRACE((
"LocalDatabaseLibrary[0x%.8x] - CreateMediaItem(%s)",
this,
2786 if (!aAllowDuplicates) {
2787 nsAutoPtr<nsStringArray> strArray(
new nsStringArray());
2788 PRBool success = strArray->AppendString(NS_ConvertUTF8toUTF16(spec));
2789 NS_ENSURE_SUCCESS(success, NS_ERROR_OUT_OF_MEMORY);
2791 nsAutoPtr<nsStringArray> filtered;
2793 rv = FilterExistingItems(strArray,
2796 getter_Transfers(filtered),
2799 NS_ENSURE_SUCCESS(rv, rv);
2801 PRUint32 length = filtered->Count();
2802 NS_ENSURE_SUCCESS(rv, rv);
2807 rv = GetGuidFromContentURI(aUri, guid);
2808 NS_ENSURE_SUCCESS(rv, rv);
2810 rv = GetMediaItem(guid, _retval);
2811 NS_ENSURE_SUCCESS(rv, rv);
2813 *aWasCreated = PR_FALSE;
2817 if (filtered == strArray)
2823 rv =
GetArray()->GetLength(&length);
2824 NS_ENSURE_SUCCESS(rv, rv);
2826 nsCOMPtr<sbIDatabaseQuery> query;
2827 rv = MakeStandardQuery(getter_AddRefs(query));
2828 NS_ENSURE_SUCCESS(rv, rv);
2831 rv = AddNewItemQuery(query,
2833 NS_ConvertUTF8toUTF16(spec),
2835 NS_ENSURE_SUCCESS(rv, rv);
2838 rv = query->Execute(&dbOk);
2839 NS_ENSURE_SUCCESS(rv, rv);
2840 NS_ENSURE_TRUE(dbOk == 0, NS_ERROR_FAILURE);
2843 nsAutoPtr<sbMediaItemInfo> newItemInfo(
new sbMediaItemInfo());
2844 NS_ENSURE_TRUE(newItemInfo, NS_ERROR_OUT_OF_MEMORY);
2846 newItemInfo->hasListType = PR_TRUE;
2848 NS_ASSERTION(!mMediaItemTable.Get(guid, nsnull),
2849 "Guid already exists!");
2851 PRBool success = mMediaItemTable.Put(guid, newItemInfo);
2852 NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
2854 nsCOMPtr<sbIMediaItem> mediaItem;
2855 rv = GetMediaItem(guid, getter_AddRefs(mediaItem));
2856 NS_ENSURE_SUCCESS(rv, rv);
2859 rv = SetDefaultItemProperties(mediaItem, aProperties, newItemInfo);
2860 NS_ENSURE_SUCCESS(rv, rv);
2862 newItemInfo.forget();
2865 rv =
GetArray()->Invalidate(PR_TRUE);
2866 NS_ENSURE_SUCCESS(rv, rv);
2869 if (!mPreventAddedNotification) {
2873 *aWasCreated = PR_TRUE;
2874 NS_ADDREF(*_retval = mediaItem);
2883 sbLocalDatabaseLibrary::SubmitCopyRequest(
sbIMediaItem * aSourceItem,
2888 nsCOMPtr<sbIDeviceManager2> deviceManager =
2889 do_GetService(
"@songbirdnest.com/Songbird/DeviceManager;2", &rv);
2890 NS_ENSURE_SUCCESS(rv, rv);
2892 nsCOMPtr<sbIDevice>
device;
2893 rv = deviceManager->GetDeviceForItem(aSourceItem, getter_AddRefs(device));
2894 if (NS_FAILED(rv) || !device) {
2901 nsCOMPtr<nsIWritablePropertyBag2> requestParams =
2902 do_CreateInstance(NS_HASH_PROPERTY_BAG_CONTRACTID, &rv);
2903 NS_ENSURE_SUCCESS(rv, rv);
2905 rv = requestParams->SetPropertyAsInterface(NS_LITERAL_STRING(
"item"),
2907 NS_ENSURE_SUCCESS(rv, rv);
2909 rv = requestParams->SetPropertyAsInterface(NS_LITERAL_STRING(
"list"),
2911 NS_ENSURE_SUCCESS(rv, rv);
2913 rv = requestParams->SetPropertyAsInterface(NS_LITERAL_STRING(
"data"),
2915 NS_ENSURE_SUCCESS(rv, rv);
2918 NS_ENSURE_SUCCESS(rv, rv);
2927 sbLocalDatabaseLibrary::CreateMediaList(
const nsAString& aType,
2931 TRACE((
"LocalDatabaseLibrary[0x%.8x] - CreateMediaList(%s)",
this,
2932 NS_LossyConvertUTF16toASCII(aType).
get()));
2933 NS_ENSURE_ARG_POINTER(_retval);
2935 sbMediaListFactoryInfo* factoryInfo;
2936 PRBool validType = mMediaListFactoryTable.Get(aType, &factoryInfo);
2937 NS_ENSURE_TRUE(validType, NS_ERROR_INVALID_ARG);
2939 nsCOMPtr<sbIDatabaseQuery> query;
2940 nsresult rv = MakeStandardQuery(getter_AddRefs(query));
2941 NS_ENSURE_SUCCESS(rv, rv);
2944 rv = AddNewItemQuery(query, factoryInfo->typeID, aType, guid);
2945 NS_ENSURE_SUCCESS(rv, rv);
2949 rv =
GetArray()->GetLength(&length);
2950 NS_ENSURE_SUCCESS(rv, rv);
2953 rv = query->Execute(&dbOk);
2954 NS_ENSURE_SUCCESS(rv, rv);
2955 NS_ENSURE_TRUE(dbOk == 0, NS_ERROR_FAILURE);
2958 nsAutoPtr<sbMediaItemInfo> newItemInfo(
new sbMediaItemInfo());
2959 NS_ENSURE_TRUE(newItemInfo, NS_ERROR_OUT_OF_MEMORY);
2961 newItemInfo->listType.Assign(aType);
2962 newItemInfo->hasListType = PR_TRUE;
2964 NS_ASSERTION(!mMediaItemTable.Get(guid, nsnull),
2965 "Guid already exists!");
2967 PRBool success = mMediaItemTable.Put(guid, newItemInfo);
2968 NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
2970 nsCOMPtr<sbIMediaItem> mediaItem;
2971 rv = GetMediaItem(guid, getter_AddRefs(mediaItem));
2972 NS_ENSURE_SUCCESS(rv, rv);
2974 nsCOMPtr<sbIMediaList> mediaList = do_QueryInterface(mediaItem, &rv);
2975 NS_ENSURE_SUCCESS(rv, rv);
2977 rv = SetDefaultItemProperties(mediaItem, aProperties, newItemInfo);
2978 NS_ENSURE_SUCCESS(rv, rv);
2984 rv = aProperties->GetPropertyValue(
2986 if (NS_SUCCEEDED(rv) && !name.IsEmpty() && mediaList) {
2987 nsCOMPtr<sbILocalDatabaseMediaItem> item =
2988 do_QueryInterface(mediaItem, &rv);
2989 NS_ENSURE_SUCCESS(rv, rv);
2991 item->SetSuppressNotifications(PR_TRUE);
2992 mediaList->SetName(name);
2993 item->SetSuppressNotifications(PR_FALSE);
2997 newItemInfo.forget();
3001 rv =
GetArray()->Invalidate(PR_TRUE);
3002 NS_ENSURE_SUCCESS(rv, rv);
3004 if (!mPreventAddedNotification) {
3008 NS_ADDREF(*_retval = mediaList);
3016 sbLocalDatabaseLibrary::CopyMediaList(
const nsAString& aType,
3018 PRBool aDontCopyContent,
3021 NS_ENSURE_FALSE(aType.IsEmpty(), NS_ERROR_INVALID_ARG);
3022 NS_ENSURE_ARG_POINTER(aSource);
3023 NS_ENSURE_ARG_POINTER(_retval);
3029 nsCOMPtr<sbIPropertyArray> properties;
3030 if (aType.EqualsLiteral(
"simple"))
3031 rv = GetSimpleMediaListCopyProperties(aSource, getter_AddRefs(properties));
3033 rv = aSource->GetProperties(nsnull, getter_AddRefs(properties));
3034 NS_ENSURE_SUCCESS(rv, rv);
3036 nsCOMPtr<sbIMediaList> newList;
3037 rv = CreateMediaList(aType, properties, getter_AddRefs(newList));
3038 NS_ENSURE_SUCCESS(rv, rv);
3041 NS_ENSURE_SUCCESS(rv, rv);
3043 if (!aDontCopyContent) {
3047 rv = newList->AddAll(aSource);
3048 if (NS_FAILED(rv)) {
3051 nsCOMPtr<sbIMediaItem> item = do_QueryInterface(newList, &rvOther);
3052 NS_ENSURE_SUCCESS(rvOther, rvOther);
3055 NS_ENSURE_SUCCESS(rvOther, rvOther);
3057 NS_ENSURE_SUCCESS(rv, rv);
3060 NS_ADDREF(*_retval = newList);
3068 sbLocalDatabaseLibrary::GetMediaItem(
const nsAString& aGUID,
3071 TRACE((
"LocalDatabaseLibrary[0x%.8x] - GetMediaItem(%s)",
this,
3072 NS_LossyConvertUTF16toASCII(aGUID).
get()));
3073 NS_ENSURE_ARG_POINTER(_retval);
3075 nsAutoMonitor mon(mMonitor);
3078 nsCOMPtr<sbIMediaItem> strongMediaItem;
3081 if (
mGuid.Equals(aGUID)) {
3083 NS_ADDREF(*_retval = item);
3087 sbMediaItemInfo* itemInfo;
3088 if (!mMediaItemTable.Get(aGUID, &itemInfo) ||
3089 !itemInfo->hasListType) {
3093 rv = GetTypeForGUID(aGUID, type);
3094 if (rv == NS_ERROR_NOT_AVAILABLE) {
3097 NS_WARNING(
"GetMediaItem called with a bad GUID!");
3098 return NS_ERROR_NOT_AVAILABLE;
3100 NS_ENSURE_SUCCESS(rv, rv);
3104 mMediaItemTable.Get(aGUID, &itemInfo);
3105 NS_ASSERTION(itemInfo,
"No entry in the table for this GUID!");
3108 else if (itemInfo->weakRef) {
3109 strongMediaItem = do_QueryReferent(itemInfo->weakRef, &rv);
3110 if (NS_SUCCEEDED(rv)) {
3114 NS_ADDREF(*_retval = strongMediaItem);
3121 nsCOMPtr<sbILocalDatabaseMediaItem> strongLocalItem =
3122 do_QueryInterface(strongMediaItem, &rv);
3123 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Failed to SetSuppressNotifications!");
3125 if (NS_SUCCEEDED(rv)) {
3126 strongLocalItem->SetSuppressNotifications(PR_FALSE);
3136 itemInfo->weakRef = nsnull;
3139 NS_ASSERTION(itemInfo->hasListType,
"Should have set a list type there!");
3141 nsRefPtr<sbLocalDatabaseMediaItem>
3143 NS_ENSURE_TRUE(newMediaItem, NS_ERROR_OUT_OF_MEMORY);
3145 rv = newMediaItem->Init(
this, aGUID);
3146 NS_ENSURE_SUCCESS(rv, rv);
3148 strongMediaItem = newMediaItem;
3150 if (!itemInfo->listType.IsEmpty()) {
3152 sbMediaListFactoryInfo* factoryInfo;
3153 PRBool success = mMediaListFactoryTable.Get(itemInfo->listType,
3155 NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
3157 NS_ASSERTION(factoryInfo->factory,
"Null factory!");
3159 nsCOMPtr<sbIMediaList> mediaList;
3160 rv = factoryInfo->factory->CreateMediaList(strongMediaItem,
3161 getter_AddRefs(mediaList));
3162 NS_ENSURE_SUCCESS(rv, rv);
3166 strongMediaItem = do_QueryInterface(mediaList, &rv);
3167 NS_ENSURE_SUCCESS(rv, rv);
3170 itemInfo->weakRef = do_GetWeakReference(strongMediaItem, &rv);
3171 NS_ENSURE_SUCCESS(rv, rv);
3176 if (!itemInfo->listType.IsEmpty()) {
3177 PRBool success = mMediaListTable.Put(aGUID, itemInfo->weakRef);
3178 NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
3181 nsCOMPtr<sbILocalDatabaseMediaItem> strongLocalItem =
3182 do_QueryInterface(strongMediaItem, &rv);
3183 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"SetSuppressNotifications will not get called!");
3185 if (NS_SUCCEEDED(rv)) {
3186 strongLocalItem->SetSuppressNotifications(PR_FALSE);
3189 NS_ADDREF(*_retval = strongMediaItem);
3197 sbLocalDatabaseLibrary::GetDuplicate(
sbIMediaItem* aMediaItem,
3200 TRACE((
"LocalDatabaseLibrary[0x%.8x] - GetDuplicate()",
this));
3201 NS_ENSURE_ARG_POINTER(aMediaItem);
3202 NS_ENSURE_ARG_POINTER(_retval);
3208 if(NS_SUCCEEDED(rv) && *_retval) {
3213 nsCOMPtr<nsIMutableArray> dupeItems =
3214 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
3215 NS_ENSURE_SUCCESS(rv, rv);
3217 static_cast<sbILibrary*>(
this),
3220 if (NS_FAILED(rv)) {
3227 rv = dupeItems->GetLength(&length);
3228 NS_ENSURE_SUCCESS(rv, rv);
3235 rv = dupeItems->QueryElementAt(0,
3237 reinterpret_cast<void**>(_retval));
3238 NS_ENSURE_SUCCESS(rv, rv);
3247 sbLocalDatabaseLibrary::ClearItems()
3249 return ClearInternal(PR_TRUE);
3256 sbLocalDatabaseLibrary::ClearItemsByType(
const nsAString &aContentType)
3258 return ClearInternal(PR_TRUE, aContentType);
3267 TRACE((
"LocalDatabaseLibrary[0x%.8x] - GetMediaListTypes()",
this));
3268 NS_ENSURE_ARG_POINTER(aMediaListTypes);
3271 nsTArray<nsString> typeArray;
3273 PRUint32 keyCount = mMediaListFactoryTable.Count();
3274 PRUint32 enumCount =
3275 mMediaListFactoryTable.EnumerateRead(AddTypesToArrayCallback, &typeArray);
3276 NS_ENSURE_TRUE(enumCount == keyCount, NS_ERROR_FAILURE);
3278 nsCOMPtr<nsIStringEnumerator> enumerator =
3280 NS_ENSURE_TRUE(enumerator, NS_ERROR_OUT_OF_MEMORY);
3282 NS_ADDREF(*aMediaListTypes = enumerator);
3292 NS_ENSURE_ARG_POINTER(aFactory);
3295 nsresult rv = aFactory->GetType(type);
3296 NS_ENSURE_SUCCESS(rv, rv);
3298 TRACE((
"LocalDatabaseLibrary[0x%.8x] - RegisterMediaListFactory(%s)",
this,
3299 NS_LossyConvertUTF16toASCII(type).
get()));
3302 PRBool alreadyRegistered = mMediaListFactoryTable.Get(type, nsnull);
3303 if (alreadyRegistered) {
3304 NS_WARNING(
"Registering a media list factory that was already registered!");
3309 nsCOMPtr<sbIDatabaseQuery> query;
3310 rv = MakeStandardQuery(getter_AddRefs(query));
3311 NS_ENSURE_SUCCESS(rv, rv);
3313 rv = query->AddQuery(NS_LITERAL_STRING(
3314 "SELECT media_list_type_id FROM media_list_types WHERE type = ?"));
3315 NS_ENSURE_SUCCESS(rv, rv);
3317 rv = query->BindStringParameter(0, type);
3318 NS_ENSURE_SUCCESS(rv, rv);
3321 rv = query->Execute(&dbresult);
3322 NS_ENSURE_SUCCESS(rv, rv);
3323 NS_ENSURE_TRUE(dbresult == 0, NS_ERROR_FAILURE);
3325 nsCOMPtr<sbIDatabaseResult> result;
3326 rv = query->GetResultObject(getter_AddRefs(result));
3327 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
3330 rv = result->GetRowCount(&rowCount);
3331 NS_ENSURE_SUCCESS(rv, rv);
3334 NS_ASSERTION(rowCount <= 1,
3335 "More than one entry for this type in the database!");
3340 rv = query->ResetQuery();
3341 NS_ENSURE_SUCCESS(rv, rv);
3343 rv = query->AddQuery(NS_LITERAL_STRING(
3344 "INSERT into media_list_types (type, factory_contractid) values (?, ?)"));
3345 NS_ENSURE_SUCCESS(rv, rv);
3347 rv = query->BindStringParameter(0, type);
3348 NS_ENSURE_SUCCESS(rv, rv);
3351 rv = aFactory->GetContractID(contractID);
3352 NS_ENSURE_SUCCESS(rv, rv);
3354 rv = query->BindStringParameter(1, NS_ConvertASCIItoUTF16(contractID));
3355 NS_ENSURE_SUCCESS(rv, rv);
3357 rv = query->Execute(&dbresult);
3358 NS_ENSURE_SUCCESS(rv, rv);
3359 NS_ENSURE_TRUE(dbresult == 0, NS_ERROR_FAILURE);
3362 rv = query->ResetQuery();
3363 NS_ENSURE_SUCCESS(rv, rv);
3365 rv = query->AddQuery(NS_LITERAL_STRING(
"select last_insert_rowid()"));
3366 NS_ENSURE_SUCCESS(rv, rv);
3368 rv = query->Execute(&dbresult);
3369 NS_ENSURE_SUCCESS(rv, rv);
3370 NS_ENSURE_TRUE(dbresult == 0, NS_ERROR_FAILURE);
3372 rv = query->GetResultObject(getter_AddRefs(result));
3373 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
3376 nsAutoString newTypeIDString;
3377 rv = result->GetRowCell(0, 0, newTypeIDString);
3378 NS_ENSURE_SUCCESS(rv, rv);
3380 PRUint32 newTypeID = newTypeIDString.ToInteger(&rv);
3381 NS_ENSURE_SUCCESS(rv, rv);
3384 nsAutoPtr<sbMediaListFactoryInfo>
3385 factoryInfo(
new sbMediaListFactoryInfo(newTypeID, aFactory));
3386 NS_ENSURE_TRUE(factoryInfo, NS_ERROR_OUT_OF_MEMORY);
3389 PRBool success = mMediaListFactoryTable.Put(type, factoryInfo);
3390 NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
3392 factoryInfo.forget();
3400 sbLocalDatabaseLibrary::Optimize(PRBool aAnalyzeOnly)
3402 TRACE((
"LocalDatabaseLibrary[0x%.8x] - Optimize()",
this));
3404 nsCOMPtr<sbIDatabaseQuery> query;
3405 nsresult rv = MakeStandardQuery(getter_AddRefs(query), PR_TRUE);
3406 NS_ENSURE_SUCCESS(rv, rv);
3408 if (!aAnalyzeOnly) {
3409 rv = query->AddQuery(NS_LITERAL_STRING(
"VACUUM"));
3410 NS_ENSURE_SUCCESS(rv, rv);
3413 rv = query->AddQuery(NS_LITERAL_STRING(
"ANALYZE"));
3414 NS_ENSURE_SUCCESS(rv, rv);
3417 rv = query->Execute(&dbresult);
3418 NS_ENSURE_SUCCESS(rv, rv);
3419 NS_ENSURE_TRUE(dbresult == 0, NS_ERROR_FAILURE);
3428 sbLocalDatabaseLibrary::Flush()
3430 TRACE((
"LocalDatabaseLibrary[0x%.8x] - Sync()",
this));
3431 NS_ENSURE_TRUE(mPropertyCache, NS_ERROR_NOT_INITIALIZED);
3433 nsresult rv = mPropertyCache->Write();
3434 NS_ENSURE_SUCCESS(rv, rv);
3443 sbLocalDatabaseLibrary::BatchCreateMediaItems(nsIArray* aURIArray,
3444 nsIArray* aPropertyArrayArray,
3445 PRBool aAllowDuplicates,
3448 NS_ENSURE_ARG_POINTER(aURIArray);
3449 NS_ENSURE_ARG_POINTER(_retval);
3451 TRACE((
"LocalDatabaseLibrary[0x%.8x] - BatchCreateMediaItems()",
this));
3453 return BatchCreateMediaItemsInternal(aURIArray,
3454 aPropertyArrayArray,
3465 sbLocalDatabaseLibrary::BatchCreateMediaItemsIfNotExist
3466 (nsIArray* aURIArray,
3467 nsIArray* aPropertyArrayArray,
3468 nsIArray** aResultItemArray,
3471 NS_ENSURE_ARG_POINTER(aURIArray);
3472 NS_ENSURE_ARG_POINTER(_retval);
3474 TRACE((
"LocalDatabaseLibrary[0x%.8x] - BatchCreateMediaItemsIfNotExist()",
3477 return BatchCreateMediaItemsInternal(aURIArray,
3478 aPropertyArrayArray,
3490 nsIArray* aURIArray,
3491 nsIArray* aPropertyArrayArray,
3492 PRBool aAllowDuplicates)
3494 NS_ENSURE_ARG_POINTER(aListener);
3495 NS_ENSURE_ARG_POINTER(aURIArray);
3497 TRACE((
"LocalDatabaseLibrary[0x%.8x] - BatchCreateMediaItemsAsync()",
this));
3499 return BatchCreateMediaItemsInternal(aURIArray,
3500 aPropertyArrayArray,
3508 sbLocalDatabaseLibrary::BatchCreateMediaItemsInternal(nsIArray* aURIArray,
3509 nsIArray* aPropertyArrayArray,
3510 PRBool aAllowDuplicates,
3511 nsIArray** aMediaItemCreatedArray,
3515 NS_ASSERTION((aListener && !_retval) || (!aListener && _retval),
3516 "Only one of |aListener| and |_retval| should be set!");
3518 TRACE((
"LocalDatabaseLibrary[0x%.8x] - BatchCreateMediaItemsInternal()",
3523 nsAutoPtr<nsStringArray> strArray;
3525 rv = ConvertURIsToStrings(aURIArray, getter_Transfers(strArray));
3526 NS_ENSURE_SUCCESS(rv, rv);
3528 nsAutoPtr<nsStringArray> filteredArray;
3529 nsCOMPtr<nsIArray> filteredPropertyArrayArray;
3530 nsTArray<PRUint32> createdMediaItemIndexArray;
3531 if (aAllowDuplicates) {
3532 filteredArray = strArray.forget();
3533 filteredPropertyArrayArray = aPropertyArrayArray;
3536 rv = FilterExistingItems(strArray,
3537 aPropertyArrayArray,
3538 &createdMediaItemIndexArray,
3539 getter_Transfers(filteredArray),
3540 getter_AddRefs(filteredPropertyArrayArray));
3541 NS_ENSURE_SUCCESS(rv, rv);
3542 if (strArray == filteredArray)
3546 PRBool runAsync = aListener ? PR_TRUE : PR_FALSE;
3548 nsCOMPtr<sbIDatabaseQuery> query;
3549 rv = MakeStandardQuery(getter_AddRefs(query), runAsync);
3550 NS_ENSURE_SUCCESS(rv, rv);
3552 nsRefPtr<sbBatchCreateTimerCallback>
callback;
3553 nsRefPtr<sbBatchCreateHelper> helper;
3557 NS_ENSURE_TRUE(callback, NS_ERROR_OUT_OF_MEMORY);
3559 rv = callback->Init();
3560 NS_ENSURE_SUCCESS(rv, rv);
3562 helper = callback->BatchHelper();
3566 NS_ENSURE_TRUE(helper, NS_ERROR_OUT_OF_MEMORY);
3570 rv = helper->InitQuery(query, filteredArray.forget(), filteredPropertyArrayArray);
3571 NS_ENSURE_SUCCESS(rv, rv);
3574 rv = query->Execute(&dbResult);
3575 NS_ENSURE_SUCCESS(rv, rv);
3576 NS_ENSURE_TRUE(dbResult == 0, NS_ERROR_FAILURE);
3580 nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
3581 NS_ENSURE_SUCCESS(rv, rv);
3586 PRBool success = mBatchCreateTimers.AppendObject(timer);
3587 NS_ENSURE_TRUE(success, rv);
3590 nsITimer::TYPE_REPEATING_SLACK);
3591 if (NS_FAILED(rv)) {
3592 NS_WARNING(
"InitWithCallback failed!");
3593 success = mBatchCreateTimers.RemoveObject(timer);
3594 NS_ASSERTION(success,
"Failed to remove a failed timer... Armageddon?");
3600 nsCOMPtr<nsIArray>
array;
3601 rv = helper->NotifyAndGetItems(getter_AddRefs(array));
3602 NS_ENSURE_SUCCESS(rv, rv);
3605 if (aMediaItemCreatedArray) {
3607 nsCOMPtr<nsIMutableArray> allItems =
3608 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1",
3610 nsCOMPtr<nsIMutableArray> mediaItemCreatedArray =
3611 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1",
3613 NS_ENSURE_SUCCESS(rv, rv);
3616 for (PRUint32 i = 0; i < createdMediaItemIndexArray.Length(); i++) {
3618 PRUint32 createdMediaItemIndex = createdMediaItemIndexArray[
i];
3621 nsCOMPtr<sbIMediaItem> mediaItem = do_QueryElementAt(array, i, &rv);
3622 NS_ENSURE_SUCCESS(rv, rv);
3625 rv = allItems->ReplaceElementAt(mediaItem,
3626 createdMediaItemIndex,
3628 NS_ENSURE_SUCCESS(rv, rv);
3631 rv = mediaItemCreatedArray->ReplaceElementAt
3633 createdMediaItemIndex,
3635 NS_ENSURE_SUCCESS(rv, rv);
3640 rv = aURIArray->GetLength(&length);
3641 NS_ENSURE_SUCCESS(rv, rv);
3642 for (PRUint32 i = 0; i <
length; i++) {
3644 if (createdMediaItemIndexArray.Contains(i))
3648 nsCOMPtr<sbIMediaItem> mediaItem;
3650 nsCOMPtr<nsIURI> uri = do_QueryElementAt(aURIArray, i, &rv);
3651 NS_ENSURE_SUCCESS(rv, rv);
3652 rv = GetGuidFromContentURI(uri, guid);
3653 NS_ENSURE_SUCCESS(rv, rv);
3654 rv = GetMediaItem(guid, getter_AddRefs(mediaItem));
3655 NS_ENSURE_SUCCESS(rv, rv);
3658 rv = allItems->ReplaceElementAt(mediaItem, i, PR_FALSE);
3659 NS_ENSURE_SUCCESS(rv, rv);
3662 rv = mediaItemCreatedArray->ReplaceElementAt
3666 NS_ENSURE_SUCCESS(rv, rv);
3669 rv = CallQueryInterface(allItems, _retval);
3670 NS_ENSURE_SUCCESS(rv, rv);
3671 rv = CallQueryInterface(mediaItemCreatedArray, aMediaItemCreatedArray);
3672 NS_ENSURE_SUCCESS(rv, rv);
3675 NS_ADDREF(*_retval = array);
3683 sbLocalDatabaseLibrary::ClearInternal(PRBool aExcludeLists ,
3684 const nsAString &aContentType )
3687 NS_ENSURE_TRUE(mPropertyCache, NS_ERROR_NOT_INITIALIZED);
3694 nsresult rv = mPropertyCache->Write();
3695 NS_ENSURE_SUCCESS(rv, rv);
3702 rv = GetAllListsByType(NS_LITERAL_STRING(
"simple"), &lists);
3703 NS_ENSURE_SUCCESS(rv, rv);
3706 for (PRInt32 i = 0; i < lists.Count(); i++) {
3707 nsCOMPtr<sbILocalDatabaseSimpleMediaList> simple =
3708 do_QueryInterface(lists[i], &rv);
3709 NS_ENSURE_SUCCESS(rv, rv);
3711 rv = simple->NotifyListenersBeforeListCleared(lists[i], aExcludeLists);
3712 NS_ENSURE_SUCCESS(rv, rv);
3715 nsCOMPtr<sbIDatabaseQuery> query;
3716 rv = MakeStandardQuery(getter_AddRefs(query));
3717 NS_ENSURE_SUCCESS(rv, rv);
3723 if(!aExcludeLists) {
3725 mMediaListTable.Clear();
3727 rv = query->AddQuery(NS_LITERAL_STRING(
"DELETE FROM media_items"));
3728 NS_ENSURE_SUCCESS(rv, rv);
3731 if(aContentType.IsEmpty()) {
3732 rv = query->AddQuery(NS_LITERAL_STRING(
"DELETE FROM media_items WHERE is_list = 0"));
3733 NS_ENSURE_SUCCESS(rv, rv);
3738 if(aContentType.EqualsLiteral(
"audio")) {
3741 else if(aContentType.EqualsLiteral(
"video")) {
3745 return NS_ERROR_INVALID_ARG;
3748 rv = query->AddQuery(NS_LITERAL_STRING(
"DELETE FROM media_items WHERE \
3750 content_mime_type = ?"));
3751 NS_ENSURE_SUCCESS(rv, rv);
3753 rv = query->BindStringParameter(0, aContentType);
3754 NS_ENSURE_SUCCESS(rv, rv);
3758 if (aExcludeLists) {
3766 mMediaItemTable.Enumerate(sbLocalDatabaseLibrary::RemoveIfNotList, &removeType);
3769 mMediaItemTable.Clear();
3773 rv = query->Execute(&dbOk);
3774 NS_ENSURE_SUCCESS(rv, rv);
3775 NS_ENSURE_TRUE(dbOk == 0, NS_ERROR_FAILURE);
3779 rv =
GetArray()->Invalidate(PR_TRUE);
3780 NS_ENSURE_SUCCESS(rv, rv);
3783 for (PRInt32 i = 0; i < lists.Count(); i++) {
3784 nsCOMPtr<sbILocalDatabaseSimpleMediaList> simple =
3785 do_QueryInterface(lists[i], &rv);
3786 NS_ENSURE_SUCCESS(rv, rv);
3789 rv = simple->Invalidate(PR_TRUE);
3790 NS_ENSURE_SUCCESS(rv, rv);
3793 rv = simple->NotifyListenersListCleared(lists[i], aExcludeLists);
3794 NS_ENSURE_SUCCESS(rv, rv);
3804 sbLocalDatabaseLibrary::NeedsMigration(PRBool *aNeedsMigration,
3805 PRUint32 *aFromVersion,
3806 PRUint32 *aToVersion)
3808 NS_ENSURE_ARG_POINTER(aNeedsMigration);
3809 NS_ENSURE_ARG_POINTER(aFromVersion);
3810 NS_ENSURE_ARG_POINTER(aToVersion);
3812 *aNeedsMigration = PR_FALSE;
3816 nsresult rv = NS_ERROR_UNEXPECTED;
3818 nsCOMPtr<sbIDatabaseQuery> query;
3819 rv = MakeStandardQuery(getter_AddRefs(query));
3820 NS_ENSURE_SUCCESS(rv, rv);
3822 rv = query->AddQuery(
3823 NS_LITERAL_STRING(
"SELECT value FROM library_metadata WHERE name = 'version'"));
3824 NS_ENSURE_SUCCESS(rv, rv);
3827 rv = query->Execute(&dbOk);
3828 NS_ENSURE_SUCCESS(rv, rv);
3829 NS_ENSURE_TRUE(dbOk == 0, NS_ERROR_FAILURE);
3831 nsCOMPtr<sbIDatabaseResult> result;
3832 rv = query->GetResultObject(getter_AddRefs(result));
3833 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
3836 rv = result->GetRowCount(&rowCount);
3837 NS_ENSURE_SUCCESS(rv, rv);
3840 NS_ENSURE_TRUE(rowCount == 1, NS_ERROR_UNEXPECTED);
3842 nsAutoString strCurrentVersion;
3843 rv = result->GetRowCell(0, 0, strCurrentVersion);
3844 NS_ENSURE_SUCCESS(rv, rv);
3846 PRUint32 currentVersion = strCurrentVersion.ToInteger(&rv);
3847 NS_ENSURE_SUCCESS(rv, rv);
3849 nsCOMPtr<sbILocalDatabaseMigrationHelper> migration =
3850 do_CreateInstance(
"@songbirdnest.com/Songbird/Library/LocalDatabase/MigrationHelper;1", &rv);
3851 NS_ENSURE_SUCCESS(rv, rv);
3853 PRUint32 latestVersion = 0;
3854 rv = migration->GetLatestSchemaVersion(&latestVersion);
3855 NS_ENSURE_SUCCESS(rv, rv);
3857 *aFromVersion = currentVersion;
3858 *aToVersion = latestVersion;
3860 *aNeedsMigration = currentVersion < latestVersion;
3862 LOG((
"++++----++++\nlatest version: %i\ncurrent schema version: %i\nneeds migration: %i\n\n",
3872 sbLocalDatabaseLibrary::MigrateLibrary(PRUint32 aFromVersion,
3873 PRUint32 aToVersion)
3875 nsresult rv = NS_ERROR_UNEXPECTED;
3877 nsCOMPtr<sbILocalDatabaseMigrationHelper> migration =
3878 do_CreateInstance(
"@songbirdnest.com/Songbird/Library/LocalDatabase/MigrationHelper;1", &rv);
3879 NS_ENSURE_SUCCESS(rv, rv);
3881 rv = migration->Migrate(aFromVersion, aToVersion,
this);
3882 NS_ENSURE_SUCCESS(rv, rv);
3884 nsCOMPtr<sbIDatabaseEngine> dbEngine =
3885 do_GetService(
"@songbirdnest.com/Songbird/DatabaseEngine;1", &rv);
3886 NS_ENSURE_SUCCESS(rv, rv);
3888 rv = dbEngine->ReleaseMemory();
3889 NS_ENSURE_SUCCESS(rv, rv);
3898 NS_ENSURE_ARG_POINTER(aSelection);
3899 NS_ENSURE_ARG_POINTER(aView);
3903 nsRefPtr<sbLocalDatabaseMediaListBase> viewMediaList =
3908 nsCOMPtr<sbILocalDatabaseGUIDArray> fullArray = viewMediaList->GetArray();
3914 nsCOMPtr<sbIMediaList> list =
3915 do_QueryInterface(NS_ISUPPORTS_CAST(
sbIMediaList*, viewMediaList), &rv);
3916 NS_ENSURE_SUCCESS(rv, rv);
3919 rv = this->Equals(list, &isLibrary);
3920 NS_ENSURE_SUCCESS(rv, rv);
3926 nsCOMPtr<sbIDatabaseQuery> query;
3927 rv = MakeStandardQuery(getter_AddRefs(query));
3928 NS_ENSURE_SUCCESS(rv, rv);
3930 rv = query->AddQuery(NS_LITERAL_STRING(
"BEGIN"));
3931 NS_ENSURE_SUCCESS(rv, rv);
3937 nsCOMPtr<sbIIndexedMediaItem> indexedMediaItem;
3943 nsString deleteQuery;
3944 deleteQuery.SetLength(1048576);
3945 deleteQuery.Assign(NS_LITERAL_STRING(
"DELETE FROM media_items WHERE media_item_id IN ("));
3947 while (NS_SUCCEEDED(aSelection->GetNext(getter_AddRefs(indexedMediaItem)))) {
3948 nsCOMPtr<sbIMediaItem> item;
3949 rv = indexedMediaItem->GetMediaItem(getter_AddRefs(item));
3950 NS_ENSURE_SUCCESS(rv, rv);
3953 rv = indexedMediaItem->GetIndex(&index);
3954 NS_ENSURE_SUCCESS(rv, rv);
3957 rv = viewArray->GetMediaItemIdByIndex(index, &mediaItemId);
3958 NS_ENSURE_SUCCESS(rv, rv);
3960 deleteQuery.AppendInt(mediaItemId);
3961 deleteQuery.AppendLiteral(
",");
3964 PRBool success = selectedItems.AppendObject(item);
3965 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
3969 rv = viewArray->GetRowidByIndex(index, &rowid);
3970 NS_ENSURE_SUCCESS(rv, rv);
3972 nsAutoString viewItemUID;
3974 viewItemUID.Append(
'-');
3975 viewItemUID.AppendInt(mediaItemId);
3977 PRUint32 fullArrayIndex;
3978 rv = fullArray->GetIndexByViewItemUID(viewItemUID, &fullArrayIndex);
3979 NS_ENSURE_SUCCESS(rv, rv);
3990 PRUint32 count = selectedItems.Count();
3995 PRBool success = map.Init(count);
3996 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
4000 rv = GetContainingLists(&selectedItems, &lists, &map);
4001 NS_ENSURE_SUCCESS(rv, rv);
4006 map.EnumerateRead(NotifyListsBeforeAfterItemRemoved, &mMediaItemTable);
4009 rv =
GetArray()->Invalidate(PR_TRUE);
4010 NS_ENSURE_SUCCESS(rv, rv);
4013 for (PRInt32 i = 0; i < lists.Count(); i++) {
4014 nsCOMPtr<sbILocalDatabaseSimpleMediaList> simple =
4015 do_QueryInterface(lists[i], &rv);
4016 NS_ENSURE_SUCCESS(rv, rv);
4019 rv = simple->Invalidate(PR_TRUE);
4020 NS_ENSURE_SUCCESS(rv, rv);
4023 deleteQuery.Replace(deleteQuery.Length() - 1, 1, NS_LITERAL_STRING(
")"));
4025 rv = query->AddQuery(deleteQuery);
4026 NS_ENSURE_SUCCESS(rv, rv);
4028 rv = query->AddQuery(NS_LITERAL_STRING(
"COMMIT"));
4029 NS_ENSURE_SUCCESS(rv, rv);
4033 rv = query->Execute(&dbSuccess);
4034 NS_ENSURE_SUCCESS(rv, rv);
4035 NS_ENSURE_TRUE(dbSuccess == 0, NS_ERROR_FAILURE);
4038 nsCOMPtr<sbIDatabasePreparedStatement> deletePreparedStatement;
4039 query->PrepareQuery(NS_LITERAL_STRING(
"DELETE FROM simple_media_lists WHERE media_item_id = ? AND ordinal = ?"), getter_AddRefs(deletePreparedStatement));
4041 PRUint32 mediaListId;
4042 rv = viewMediaList->GetMediaItemId(&mediaListId);
4043 NS_ENSURE_SUCCESS(rv, rv);
4048 nsCOMPtr<sbILocalDatabaseSimpleMediaList> simple =
4049 do_QueryInterface(NS_ISUPPORTS_CAST(
sbIMediaList*, viewMediaList), &rv);
4050 NS_ENSURE_SUCCESS(rv, rv);
4052 while (NS_SUCCEEDED(aSelection->GetNext(getter_AddRefs(indexedMediaItem)))) {
4053 nsCOMPtr<sbIMediaItem> item;
4054 rv = indexedMediaItem->GetMediaItem(getter_AddRefs(item));
4055 NS_ENSURE_SUCCESS(rv, rv);
4058 rv = indexedMediaItem->GetIndex(&index);
4059 NS_ENSURE_SUCCESS(rv, rv);
4062 rv = query->AddPreparedStatement(deletePreparedStatement);
4063 NS_ENSURE_SUCCESS(rv, rv);
4065 query->BindInt32Parameter(0, mediaListId);
4066 NS_ENSURE_SUCCESS(rv, rv);
4068 nsAutoString ordinal;
4069 rv = viewArray->GetOrdinalByIndex(index, ordinal);
4070 NS_ENSURE_SUCCESS(rv, rv);
4071 query->BindStringParameter(1, ordinal);
4072 NS_ENSURE_SUCCESS(rv, rv);
4074 nsString viewItemUID;
4075 rv = viewArray->GetViewItemUIDByIndex(index, viewItemUID);
4076 NS_ENSURE_SUCCESS(rv, rv);
4078 PRUint32 fullArrayIndex;
4079 rv = fullArray->GetIndexByViewItemUID(viewItemUID, &fullArrayIndex);
4080 NS_ENSURE_SUCCESS(rv, rv);
4082 rv = simple->NotifyListenersBeforeItemRemoved(viewMediaList,
4085 NS_ENSURE_SUCCESS(rv, rv);
4088 rv = simple->NotifyListenersAfterItemRemoved(viewMediaList,
4091 NS_ENSURE_SUCCESS(rv, rv);
4093 rv = item->GetGuid(guid);
4094 NS_ENSURE_SUCCESS(rv, rv);
4097 mMediaItemTable.Remove(guid);
4101 rv = simple->Invalidate(PR_TRUE);
4102 NS_ENSURE_SUCCESS(rv, rv);
4105 rv = query->AddQuery(NS_LITERAL_STRING(
"COMMIT"));
4106 NS_ENSURE_SUCCESS(rv, rv);
4109 rv = query->Execute(&dbSuccess);
4110 NS_ENSURE_SUCCESS(rv, rv);
4111 NS_ENSURE_TRUE(dbSuccess == 0, NS_ERROR_FAILURE);
4114 nsCOMPtr<sbIMediaList> list =
4117 NS_ENSURE_SUCCESS(rv, rv);
4124 sbLocalDatabaseLibrary::ForceBeginUpdateBatch() {
4130 sbLocalDatabaseLibrary::ForceEndUpdateBatch() {
4138 NS_ENSURE_ARG_POINTER(aLocalDatabaseLibrary);
4139 *aLocalDatabaseLibrary =
this;
4150 return NS_ERROR_FAILURE;
4159 aType.Assign(NS_LITERAL_STRING(
"library"));
4170 NS_ENSURE_ARG_POINTER(_retval);
4174 nsCOMPtr<sbIMediaItem> item;
4175 rv = GetMediaItem(aGuid, getter_AddRefs(item));
4176 NS_ENSURE_SUCCESS(rv, rv);
4178 NS_ADDREF(*_retval = item);
4189 NS_ENSURE_ARG_POINTER(aMediaItem);
4190 NS_ENSURE_ARG_POINTER(_retval);
4193 nsresult rv = aMediaItem->GetGuid(guid);
4194 NS_ENSURE_SUCCESS(rv, rv);
4197 rv = GetMediaItemIdForGuid(guid, &mediaItemId);
4198 if (NS_FAILED(rv) && (rv != NS_ERROR_NOT_AVAILABLE)) {
4199 NS_WARNING(
"GetMediaItemIdForGuid failed");
4203 *_retval = NS_SUCCEEDED(rv);
4213 return AddItem(aMediaItem, nsnull);
4220 NS_ENSURE_ARG_POINTER(aMediaItem);
4224 PRBool containsCopy;
4225 nsresult rv = ContainsCopy(aMediaItem, &containsCopy);
4226 NS_ENSURE_SUCCESS(rv, rv);
4232 rv =
GetArray()->GetLength(&length);
4233 NS_ENSURE_SUCCESS(rv, rv);
4235 nsCOMPtr<sbIMediaItem> newMediaItem;
4238 rv = AddItemToLocalDatabase(aMediaItem, getter_AddRefs(newMediaItem));
4239 NS_ENSURE_SUCCESS(rv, rv);
4243 rv =
GetArray()->Invalidate(PR_TRUE);
4244 NS_ENSURE_SUCCESS(rv, rv);
4251 nsCOMPtr<sbILibrary> itemLibrary;
4252 rv = aMediaItem->GetLibrary(getter_AddRefs(itemLibrary));
4253 NS_ENSURE_SUCCESS(rv, rv);
4254 nsCOMPtr<sbILocalDatabaseLibrary> originalLocalDatabaseLibrary;
4255 originalLocalDatabaseLibrary = do_QueryInterface(itemLibrary, &rv);
4256 NS_ENSURE_SUCCESS(rv, rv);
4259 originalLocalDatabaseLibrary->NotifyCopyListenersItemCopied(aMediaItem,
4261 if (aNewMediaItem) {
4262 newMediaItem.forget(aNewMediaItem);
4274 NS_ENSURE_ARG_POINTER(aMediaList);
4281 nsresult rv = aMediaList->EnumerateAllItems(&listener,
4283 NS_ENSURE_SUCCESS(rv, rv);
4302 NS_ENSURE_ARG_POINTER(aMediaItems);
4307 nsCOMPtr<nsIThread>
target;
4308 nsresult rv = NS_GetMainThread(getter_AddRefs(target));
4309 NS_ENSURE_SUCCESS(rv, rv);
4311 nsCOMPtr<sbIAddMediaItemsListener> proxiedListener;
4316 NS_PROXY_SYNC | NS_PROXY_ALWAYS,
4317 getter_AddRefs(proxiedListener));
4318 NS_ENSURE_SUCCESS(rv, rv);
4320 nsRefPtr<sbLocalDatabaseLibraryAsyncRunner> runner =
4322 NS_ENSURE_TRUE(runner, NS_ERROR_OUT_OF_MEMORY);
4324 nsCOMPtr<nsIThreadPool> threadPoolService =
4325 do_GetService(
"@songbirdnest.com/Songbird/ThreadPoolService;1", &rv);
4326 NS_ENSURE_SUCCESS(rv, rv);
4328 rv = threadPoolService->Dispatch(runner, NS_DISPATCH_NORMAL);
4329 NS_ENSURE_SUCCESS(rv, rv);
4334 PRUint16 stepResult;
4335 nsresult rv = listener.OnEnumerationBegin(nsnull, &stepResult);
4336 NS_ENSURE_SUCCESS(rv, rv);
4343 while (NS_SUCCEEDED(aMediaItems->HasMoreElements(&hasMore)) && hasMore) {
4344 nsCOMPtr<nsISupports> supports;
4345 rv = aMediaItems->GetNext(getter_AddRefs(supports));
4348 nsCOMPtr<sbIMediaItem> item = do_QueryInterface(supports, &rv);
4351 PRUint16 stepResult;
4352 rv = listener.OnEnumeratedItem(nsnull, item, &stepResult);
4353 if (NS_FAILED(rv) ||
4359 rv = listener.OnEnumerationEnd(nsnull,
NS_OK);
4360 NS_ENSURE_SUCCESS(rv, rv);
4369 NS_ENSURE_ARG_POINTER(aMediaItems);
4370 NS_ENSURE_ARG_POINTER(aListener);
4376 PRUint16 stepResult;
4377 nsresult rv = listener.OnEnumerationBegin(nsnull, &stepResult);
4378 NS_ENSURE_SUCCESS(rv, rv);
4385 PRUint32 itemsProcessed = 0;
4387 while (NS_SUCCEEDED(aMediaItems->HasMoreElements(&hasMore)) && hasMore) {
4389 nsCOMPtr<nsISupports> supports;
4390 rv = aMediaItems->GetNext(getter_AddRefs(supports));
4393 nsCOMPtr<sbIMediaItem> item = do_QueryInterface(supports, &rv);
4396 PRUint16 stepResult;
4397 rv = listener.OnEnumeratedItem(nsnull, item, &stepResult);
4398 if (NS_FAILED(rv) ||
4408 rv = aListener->OnProgress(itemsProcessed, PR_FALSE);
4409 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Failed to call async listener.");
4416 rv = listener.OnEnumerationEnd(nsnull,
NS_OK);
4417 NS_ENSURE_SUCCESS(rv, rv);
4419 rv = aListener->OnProgress(itemsProcessed, PR_TRUE);
4420 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Failed to call async listener.");
4431 NS_ENSURE_ARG_POINTER(aMediaItem);
4439 PRUint16 stepResult;
4440 nsresult rv = listener.OnEnumerationBegin(nsnull, &stepResult);
4441 NS_ENSURE_SUCCESS(rv, rv);
4443 rv = listener.OnEnumeratedItem(nsnull, aMediaItem, &stepResult);
4444 NS_ENSURE_SUCCESS(rv, rv);
4446 rv = listener.OnEnumerationEnd(nsnull,
NS_OK);
4447 NS_ENSURE_SUCCESS(rv, rv);
4463 nsresult rv =
GetArray()->GetGuidByIndex(aIndex, guid);
4464 NS_ENSURE_SUCCESS(rv, rv);
4466 nsCOMPtr<sbIMediaItem> mediaItem;
4467 rv = GetMediaItem(guid, getter_AddRefs(mediaItem));
4468 NS_ENSURE_SUCCESS(rv, rv);
4471 NS_ENSURE_SUCCESS(rv, rv);
4482 NS_ENSURE_ARG_POINTER(aMediaItems);
4492 PRUint16 stepResult;
4493 nsresult rv = listener.OnEnumerationBegin(nsnull, &stepResult);
4494 NS_ENSURE_SUCCESS(rv, rv);
4497 while (NS_SUCCEEDED(aMediaItems->HasMoreElements(&hasMore)) && hasMore) {
4499 nsCOMPtr<nsISupports> supports;
4500 rv = aMediaItems->GetNext(getter_AddRefs(supports));
4501 NS_ENSURE_SUCCESS(rv, rv);
4503 nsCOMPtr<sbIMediaItem> item = do_QueryInterface(supports, &rv);
4504 NS_ENSURE_SUCCESS(rv, rv);
4506 rv = listener.OnEnumeratedItem(nsnull, item, &stepResult);
4507 NS_ENSURE_SUCCESS(rv, rv);
4510 rv = listener.OnEnumerationEnd(nsnull,
NS_OK);
4511 NS_ENSURE_SUCCESS(rv, rv);
4522 return ClearInternal(PR_FALSE);
4532 NS_ENSURE_ARG_POINTER(_retval);
4539 nsRefPtr<sbLocalDatabaseMediaListView>
4541 NS_ENSURE_TRUE(view, NS_ERROR_OUT_OF_MEMORY);
4543 rv = view->Init(aState);
4544 NS_ENSURE_SUCCESS(rv, rv);
4546 NS_ADDREF(*_retval = view);
4564 sbLocalDatabaseLibrary::Observe(
nsISupports *aSubject,
4566 const PRUnichar *
aData)
4571 do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &rv);
4572 if (NS_SUCCEEDED(rv)) {
4577 NS_ENSURE_SUCCESS(rv, rv);
4580 NS_NOTREACHED(
"Observing a topic we don't care about!");
4590 sbLocalDatabaseLibrary::GetInterfaces(PRUint32* count, nsIID*** array)
4599 sbLocalDatabaseLibrary::GetHelperForLanguage(PRUint32
language,
4610 sbLocalDatabaseLibrary::GetContractID(
char** aContractID)
4612 *aContractID = nsnull;
4620 sbLocalDatabaseLibrary::GetClassDescription(
char** aClassDescription)
4622 *aClassDescription = nsnull;
4630 sbLocalDatabaseLibrary::GetClassID(nsCID** aClassID)
4640 sbLocalDatabaseLibrary::GetImplementationLanguage(PRUint32* aImplementationLanguage)
4642 *aImplementationLanguage = nsIProgrammingLanguage::CPLUSPLUS;
4650 sbLocalDatabaseLibrary::GetFlags(PRUint32 *aFlags)
4652 *aFlags = nsIClassInfo::THREADSAFE;
4660 sbLocalDatabaseLibrary::GetClassIDNoAlloc(nsCID* aClassIDNoAlloc)
4662 return NS_ERROR_NOT_AVAILABLE;
4673 mListener(aListener),
4678 NS_ASSERTION(aLibrary,
"Null library!");
4679 NS_ASSERTION(aListener,
"Null listener!");
4680 NS_ASSERTION(aQuery,
"Null query!");
4687 NS_ENSURE_TRUE(mBatchHelper, NS_ERROR_OUT_OF_MEMORY);
4695 mQueryCount = aQueryCount;
4702 NS_ASSERTION(mBatchHelper,
"This shouldn't be null, did you call Init?!");
4703 return mBatchHelper;
4707 sbBatchCreateTimerCallback::Notify(nsITimer* aTimer)
4709 NS_ENSURE_ARG_POINTER(aTimer);
4713 if (NS_SUCCEEDED(rv) && !complete) {
4722 mLibrary->mBatchCreateTimers.RemoveObject(aTimer);
4725 nsCOMPtr<nsIArray>
array;
4726 if (NS_SUCCEEDED(rv)) {
4727 rv = mBatchHelper->NotifyAndGetItems(getter_AddRefs(array));
4730 mListener->OnComplete(array, rv);
4733 NS_ENSURE_SUCCESS(rv, rv);
4741 NS_ASSERTION(_retval,
"Null retval!");
4752 PRBool isExecuting = PR_FALSE;
4753 rv = mQuery->IsExecuting(&isExecuting);
4754 NS_ENSURE_SUCCESS(rv, rv);
4756 PRUint32 currentQuery;
4757 rv = mQuery->CurrentQuery(¤tQuery);
4758 NS_ENSURE_SUCCESS(rv, rv);
4760 NS_ASSERTION(currentQuery <= mQueryCount,
"Invalid position!");
4768 if (currentQuery <= mQueryCount &&
4773 PRUint32 itemIndex = (currentQuery > 2) ? currentQuery - 2 : 0;
4774 mListener->OnProgress(itemIndex);
4776 *_retval = PR_FALSE;
4790 mCallback(aCallback),
4793 NS_ASSERTION(aLibrary,
"Null library!");
4798 nsStringArray* aURIArray,
4799 nsIArray* aPropertyArrayArray)
4801 TRACE((
"sbBatchCreateHelper[0x%.8x] - InitQuery()",
this));
4802 NS_ASSERTION(aQuery,
"aQuery is null");
4804 mURIArray = aURIArray;
4805 mPropertiesArray = aPropertyArrayArray;
4807 nsresult rv = aQuery->AddQuery(NS_LITERAL_STRING(
"begin"));
4808 NS_ENSURE_SUCCESS(rv, rv);
4811 PRUint32 listLength = mURIArray->Count();
4815 for (PRUint32 i = 0; i < listLength; i++) {
4816 nsAutoString uriSpec;
4817 mURIArray->StringAt(i, uriSpec);
4820 rv = mLibrary->AddNewItemQuery(aQuery,
4824 NS_ENSURE_SUCCESS(rv, rv);
4825 TRACE((
"sbBatchCreateHelper[0x%.8x] - InitQuery() -- added new itemQuery",
this));
4827 nsString* success = mGuids.AppendElement(guid);
4828 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
4832 rv = aQuery->AddQuery(NS_LITERAL_STRING(
"commit"));
4833 NS_ENSURE_SUCCESS(rv, rv);
4836 PRUint32 queryCount = 0;
4837 aQuery->GetQueryCount(&queryCount);
4843 NS_ENSURE_SUCCESS(rv, rv);
4851 TRACE((
"sbBatchCreateTimerCallback[0x%.8x] - NotifyAndGetItems()",
this ));
4852 NS_ASSERTION(_retval,
"_retval is null");
4855 nsCOMPtr<nsIMutableArray> array =
4856 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
4857 NS_ENSURE_SUCCESS(rv, rv);
4859 PRUint32 length = mGuids.Length();
4863 PRUint32 notifiedCount = 0;
4865 nsTArray<const PRUnichar*> guidArray(length);
4866 for (PRUint32 i = 0; i < length; i++) {
4867 const PRUnichar** addedPtr = guidArray.AppendElement(mGuids[i].
get());
4868 NS_ENSURE_TRUE(addedPtr, NS_ERROR_OUT_OF_MEMORY);
4873 rv = mLibrary->mPropertyCache->GetProperties(guidArray.Elements(),
4877 NS_ENSURE_SUCCESS(rv, rv);
4879 for (PRUint32 i = 0; i < length; i++) {
4882 nsAutoPtr<sbLocalDatabaseLibrary::sbMediaItemInfo>
4883 newItemInfo(
new sbLocalDatabaseLibrary::sbMediaItemInfo(PR_TRUE));
4884 NS_ENSURE_TRUE(newItemInfo, NS_ERROR_OUT_OF_MEMORY);
4886 NS_ASSERTION(!mLibrary->mMediaItemTable.Get(mGuids[i], nsnull),
4887 "Guid already exists!");
4889 PRBool success = mLibrary->mMediaItemTable.Put(mGuids[i], newItemInfo);
4890 NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
4892 nsCOMPtr<sbIMediaItem> mediaItem;
4893 rv = mLibrary->GetMediaItem(mGuids[i], getter_AddRefs(mediaItem));
4894 NS_ENSURE_SUCCESS(rv, rv);
4897 nsCOMPtr<sbILocalDatabaseMediaItem> ldbmi =
4898 do_QueryInterface(mediaItem, &rv);
4899 NS_ENSURE_SUCCESS(rv, rv);
4901 NS_ENSURE_TRUE(bags[i], NS_ERROR_NULL_POINTER);
4902 rv = ldbmi->SetPropertyBag(bags[i]);
4903 NS_ENSURE_SUCCESS(rv, rv);
4907 nsCOMPtr<sbIPropertyArray> properties = nsnull;
4908 if (mPropertiesArray) {
4909 properties = do_QueryElementAt(mPropertiesArray, i, &rv);
4910 NS_ENSURE_SUCCESS(rv, rv);
4915 do_CreateInstance(
"@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1", &rv);
4916 NS_ENSURE_SUCCESS(rv, rv);
4918 rv = mLibrary->SetDefaultItemProperties(mediaItem, properties, newItemInfo);
4919 NS_ENSURE_SUCCESS(rv, rv);
4921 newItemInfo.forget();
4923 rv = array->AppendElement(mediaItem, PR_FALSE);
4924 NS_ENSURE_SUCCESS(rv, rv);
4928 mLength + notifiedCount);
4932 rv = mLibrary->
GetArray()->Invalidate(PR_TRUE);
4933 NS_ENSURE_SUCCESS(rv, rv);
4934 NS_FREE_XPCOM_ISUPPORTS_POINTER_ARRAY(count, bags);
4937 NS_ADDREF(*_retval = array);
4944 sbLocalDatabaseLibrary::InitializeLibraryStatistics() {
4945 nsresult rv =
NS_OK;
4948 rv = MakeStandardQuery(getter_AddRefs(mStatisticsSumQuery));
4949 NS_ENSURE_SUCCESS(rv, rv);
4951 NS_NAMED_MULTILINE_LITERAL_STRING(sumQuery,
4952 NS_LL(
"SELECT value1.obj, SUM(value2.obj)")
4953 NS_LL(
" FROM properties AS property1")
4954 NS_LL(
" INNER JOIN resource_properties AS value1")
4955 NS_LL(
" ON value1.property_id = property1.property_id")
4956 NS_LL(
" INNER JOIN resource_properties AS value2")
4957 NS_LL(
" ON value1.media_item_id = value2.media_item_id")
4958 NS_LL(
" INNER JOIN properties AS property2")
4959 NS_LL(
" ON value2.property_id = property2.property_id")
4960 NS_LL(
" WHERE property1.property_name = ?")
4961 NS_LL(
" AND property2.property_name = ?")
4962 NS_LL(
" GROUP BY value1.obj")
4963 NS_LL(
" ORDER BY ? * SUM(value2.obj)")
4964 NS_LL(
" LIMIT ?;"));
4966 rv = mStatisticsSumQuery->PrepareQuery(sumQuery, getter_AddRefs(mStatisticsSumPreparedStatement));
4967 NS_ENSURE_SUCCESS(rv, rv);
4973 sbLocalDatabaseLibrary::CollectDistinctValues(
const nsAString & aProperty,
4974 PRUint32 aCollectionMethod,
4975 const nsAString & aOtherProperty,
4977 PRUint32 aMaxResults,
4980 NS_ENSURE_ARG_POINTER(_retval);
4983 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_FAILURE);
4985 nsresult rv =
NS_OK;
4987 nsCOMPtr<sbIDatabaseQuery> query;
4989 switch(aCollectionMethod) {
4991 query = mStatisticsSumQuery;
4992 query->AddPreparedStatement(mStatisticsSumPreparedStatement);
4993 query->BindStringParameter(0, aProperty);
4994 query->BindStringParameter(1, aOtherProperty);
4995 query->BindInt32Parameter(2, aAscending ? 1 : -1);
4996 query->BindInt32Parameter(3, aMaxResults);
4999 return NS_ERROR_INVALID_ARG;
5003 rv = query->Execute(&dbOk);
5004 NS_ENSURE_SUCCESS(rv, rv);
5005 NS_ENSURE_TRUE(dbOk == 0, NS_ERROR_FAILURE);
5007 nsCOMPtr<sbIDatabaseResult> result;
5008 rv = query->GetResultObject(getter_AddRefs(result));
5009 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
5011 PRUint32 rowCount = 0;
5012 rv = result->GetRowCount(&rowCount);
5013 NS_ENSURE_SUCCESS(rv, rv);
5015 nsCOMPtr<nsIMutableArray> array =
5016 do_CreateInstance(
"@mozilla.org/array;1", &rv);
5017 NS_ENSURE_SUCCESS(rv, rv);
5019 for (PRUint32 i=0; i<rowCount; i++) {
5021 nsCOMPtr<nsIWritableVariant> variant =
5022 do_CreateInstance(NS_VARIANT_CONTRACTID, &rv);
5023 NS_ENSURE_SUCCESS(rv, rv);
5027 rv = result->GetRowCell(i, 0, name);
5028 NS_ENSURE_SUCCESS(rv, rv);
5030 variant->SetAsAString(name);
5031 array->AppendElement(variant, PR_FALSE);
5034 return CallQueryInterface(array, _retval);
5038 sbLocalDatabaseLibrary::NeedsReindexCollations(PRBool *aNeedsReindexCollations) {
5043 nsCOMPtr<nsIPrefBranch> prefBranch =
5045 if (NS_SUCCEEDED(rv)) {
5048 key.Append(NS_ConvertUTF16toUTF8(mDatabaseGuid));
5050 rv = prefBranch->GetBoolPref(
key.get(), &prefValue);
5051 if (NS_SUCCEEDED(rv)) {
5053 *aNeedsReindexCollations = PR_TRUE;
5060 nsCOMPtr<sbIDatabaseQuery> query;
5061 rv = MakeStandardQuery(getter_AddRefs(query));
5062 NS_ENSURE_SUCCESS(rv, rv);
5064 rv = query->AddQuery(NS_LITERAL_STRING(
"SELECT value FROM library_metadata WHERE name = 'collation-locale'"));
5065 NS_ENSURE_SUCCESS(rv, rv);
5068 rv = query->Execute(&dbOk);
5069 NS_ENSURE_SUCCESS(rv, rv);
5070 NS_ENSURE_TRUE(dbOk == 0, NS_ERROR_FAILURE);
5072 nsCOMPtr<sbIDatabaseResult> result;
5073 rv = query->GetResultObject(getter_AddRefs(result));
5074 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
5076 PRUint32 rowCount = 0;
5077 rv = result->GetRowCount(&rowCount);
5078 NS_ENSURE_SUCCESS(rv, rv);
5081 if (rowCount == 0) {
5082 *aNeedsReindexCollations = PR_TRUE;
5086 NS_ENSURE_TRUE(rowCount == 1, NS_ERROR_UNEXPECTED);
5088 nsAutoString previousCollationLocale;
5089 rv = result->GetRowCell(0, 0, previousCollationLocale);
5090 NS_ENSURE_SUCCESS(rv, rv);
5092 nsCOMPtr<sbIDatabaseEngine> dbEngine =
5093 do_GetService(
"@songbirdnest.com/Songbird/DatabaseEngine;1", &rv);
5094 NS_ENSURE_SUCCESS(rv, rv);
5096 nsString currentCollationLocale;
5097 dbEngine->GetLocaleCollationID(currentCollationLocale);
5098 NS_ENSURE_SUCCESS(rv, rv);
5100 *aNeedsReindexCollations =
5101 !currentCollationLocale.Equals(previousCollationLocale);
5107 sbLocalDatabaseLibrary::ReindexCollations() {
5109 nsCOMPtr<sbIDatabaseQuery> query;
5110 nsresult rv = MakeStandardQuery(getter_AddRefs(query));
5111 NS_ENSURE_SUCCESS(rv, rv);
5115 nsAutoString queryStr;
5116 queryStr = NS_LITERAL_STRING(
"REINDEX 'library_collate'");
5118 rv = query->AddQuery(queryStr);
5119 NS_ENSURE_SUCCESS(rv, rv);
5123 nsCOMPtr<nsIPrefBranch> prefBranch =
5125 if (NS_SUCCEEDED(rv)) {
5128 key.Append(NS_ConvertUTF16toUTF8(mDatabaseGuid));
5130 rv = prefBranch->PrefHasUserValue(
key.get(), &prefValue);
5131 NS_ENSURE_SUCCESS(rv, rv);
5133 rv = prefBranch->ClearUserPref(
key.get());
5134 NS_ENSURE_SUCCESS(rv, rv);
5140 nsCOMPtr<sbIDatabaseEngine> dbEngine =
5141 do_GetService(
"@songbirdnest.com/Songbird/DatabaseEngine;1", &rv);
5142 NS_ENSURE_SUCCESS(rv, rv);
5144 nsString currentCollationLocale;
5145 dbEngine->GetLocaleCollationID(currentCollationLocale);
5146 NS_ENSURE_SUCCESS(rv, rv);
5149 NS_LITERAL_STRING(
"INSERT OR REPLACE INTO library_metadata VALUES('collation-locale', '");
5151 queryStr += currentCollationLocale;
5153 queryStr += NS_LITERAL_STRING(
"')");
5155 rv = query->AddQuery(queryStr);
5156 NS_ENSURE_SUCCESS(rv, rv);
5159 rv = query->Execute(&dbOk);
5160 NS_ENSURE_SUCCESS(rv, rv);
5161 NS_ENSURE_TRUE(dbOk == 0, NS_ERROR_FAILURE);
GeneratorThread currentThread
nsDataHashtable< nsStringHashKey, PRUint32 > sbListItemIndexMap
NS_IMETHOD IndexOf(sbIMediaItem *aMediaItem, PRUint32 aStartFrom, PRUint32 *_retval)
NS_IMETHOD AddAll(sbIMediaList *aMediaList)
#define SB_PROPERTY_MEDIALISTNAME
static nsresult LinkCopy(sbIMediaItem *aOriginal, sbIMediaItem *aCopy)
Links a copy to its original. It will take into account the libraries the items belong to...
#define SONGBIRD_DATABASEQUERY_CONTRACTID
static nsCOMPtr< nsIObserverService > observerService
PLDHashOperator PR_CALLBACK CopyInterfaceHashtableEntry(typename V::KeyType aKey, T *aData, void *aUserData)
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
static nsresult GetItemInLibrary(sbIMediaItem *aItem, sbILibrary *aLibrary, sbIMediaItem **_retval)
#define SB_LIBRARY_MANAGER_SHUTDOWN_TOPIC
#define SB_IMEDIAITEM_CAST(_ptr)
#define SB_PROPERTY_ORIGINURL
readonly attribute sbIDevice device
Reference to the device that this library is stored on.
NS_IMETHOD AddItem(sbIMediaItem *aMediaItem, sbIMediaItem **aNewMediaItem)
#define BATCHCREATE_NOTIFICATION_INTERVAL_MS
nsresult RemoveSelected(nsISimpleEnumerator *aSelection, sbLocalDatabaseMediaListView *aView)
Bulk removes the selected items specified in aSelection from the view aView.
const NS_PREFSERVICE_CONTRACTID
nsClassHashtable< nsISupportsHashKey, sbMediaItemArray > sbMediaItemToListsMap
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
#define SB_PROPERTY_ISCONTENTREADONLY
NS_IMETHOD AddMediaItems(nsISimpleEnumerator *aMediaItems, sbIAddMediaItemsListener *aListener, PRBool aAsync)
NS_DECL_ISUPPORTS NS_DECL_NSITIMERCALLBACK sbBatchCreateTimerCallback(sbLocalDatabaseLibrary *aLibrary, sbIBatchCreateMediaItemsListener *aListener, sbIDatabaseQuery *aQuery)
#define DEFAULT_MEDIALIST_CACHE_SIZE
sbDeviceFirmwareAutoCheckForUpdate prototype contractID
Extract statistics from a media library.
static nsresult FindCopiesByID(sbIMediaItem *aMediaItem, sbIMediaList *aList, nsIMutableArray *aCopies)
NS_IMETHOD GetDefaultSortProperty(nsAString &aProperty)
#define DEFAULT_MEDIAITEM_CACHE_SIZE
Factory for new library instances.
#define SB_PROPERTY_HIDDEN
nsresult NotifyAndGetItems(nsIArray **_retval)
#define SB_IMEDIALIST_CAST(_ptr)
Interface used to listen for items being copied from a library.
#define INVALID_COLLATION_INDEX_PREF_PREFIX
nsresult do_GetProxyForObject(nsIEventTarget *aTarget, REFNSIID aIID, nsISupports *aObj, PRInt32 aProxyType, void **aProxyObject)
Songbird Variant Utility Definitions.
const PRUint32 REMOVE_VIDEO_TYPE_ONLY
static PRBool IsDeviceLibrary(sbILibrary *aLibrary)
~sbLocalDatabaseLibrary()
NS_IMPL_THREADSAFE_ISUPPORTS1(sbLocalDatabaseLibraryAsyncRunner, nsIRunnable)
NS_IMPL_THREADSAFE_RELEASE(sbRequestItem)
Simple class to make sure we notify listeners that a batch operation has completed every time they ar...
void NotifyListenersItemUpdated(in sbIMediaItem aItem, in sbIPropertyArray aProperties)
NS_IMPL_THREADSAFE_ADDREF(sbRequestItem)
readonly attribute AString guid
The guid of this resource.
const unsigned long REQUEST_READ
#define SB_PROPERTY_STORAGEGUID
friend class sbLocalDatabasePropertyCache
nsresult NotifyInternal(PRBool *_retval)
friend class sbBatchCreateHelper
#define INVALID_COLLATION_INDEX_PREF_SUFFIX
NS_IMETHOD AddSome(nsISimpleEnumerator *aMediaItems)
#define SB_SQLBUILDER_SELECT_CONTRACTID
boolean equals(in sbILibraryResource aOtherLibraryResource)
Tests the equality of two library resources.
#define SB_PROPERTY_OUTERGUID
NS_IMETHOD Add(sbIMediaItem *aMediaItem)
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
const sbCreateProxiedComponent do_ProxiedGetService(const nsCID &aCID, nsresult *error=0)
#define ANALYZE_COUNT_PREF
#define SB_PROPERTY_UPDATED
#define SB_PROPERTY_CONTENTTYPE
NS_IMETHOD CreateView(sbIMediaListViewState *aState, sbIMediaListView **_retval)
NS_IMETHOD Remove(sbIMediaItem *aMediaItem)
General interface to data resources.
#define SB_ILIBRESOURCE_CAST(_ptr)
NS_IMETHOD GetLength(PRUint32 *aLength)
const PRUint32 REMOVE_AUDIO_TYPE_ONLY
friend class sbBatchCreateTimerCallback
#define SB_PROPERTY_DEVICE_LIBRARY_GUID
NS_IMETHOD RemoveByIndex(PRUint32 aIndex)
NS_IMETHOD Contains(sbIMediaItem *aMediaItem, PRBool *_retval)
Media library abstraction.
static nsresult FindOriginalsByID(sbIMediaItem *aMediaItem, sbIMediaList *aList, nsIMutableArray *aCopies)
NS_IMETHOD SetContentSrc(nsIURI *aContentSrc)
#define LOG_SUBMESSAGE_SPACE
static nsresult FindItemsWithSameURL(sbIMediaItem *aMediaItem, sbIMediaList *aMediaList, nsIMutableArray *aCopies)
#define DEFAULT_ANALYZE_COUNT_LIMIT
nsresult SetQueryCount(PRUint32 aQueryCount)
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
nsresult AddSomeAsyncInternal(nsISimpleEnumerator *aMediaItems, sbIAddMediaItemsListener *aListener)
StringArrayEnumerator prototype hasMore
nsresult GetLengthCache(sbILocalDatabaseGUIDArrayLengthCache **aLengthCache)
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
#define DEFAULT_SORT_PROPERTY
An object responsible for executing SQL queries on the database.
sbBatchCreateHelper * BatchHelper()
#define SB_PROPERTY_LISTTYPE
#define SHUTDOWN_ASYNC_GRANULARITY_MS
NS_IMETHOD RemoveSome(nsISimpleEnumerator *aMediaItems)
nsCOMArray< sbIMediaList > sbMediaListArray
nsCOMArray< sbIMediaItem > sbMediaItemArray
static void GetNowString(nsAString &_retval)
Make a string of the current time in milliseconds.
#define SB_PROPERTY_CUSTOMTYPE
#define SB_MEDIAITEM_TYPEID
#define SB_PROPERTY_CONTENTURL
const PRUint32 REMOVE_ALL_TYPES
An interface to carry around arrays of nsIProperty instances. Users of this interface should only QI ...
#define DEFAULT_FETCH_SIZE
_getSelectedPageStyle s i
readonly attribute sbILibraryFactory factory
Holds the factory that created this library.
NS_IMPL_ISUPPORTS1(sbLibraryRemovingEnumerationListener, sbIMediaListEnumerationListener) NS_IMETHODIMP sbLibraryRemovingEnumerationListener
#define SB_LOCALDATABASE_DYNAMICMEDIALISTFACTORY_CONTRACTID
NS_IMPL_ISUPPORTS_INHERITED5(sbLocalDatabaseLibrary, sbLocalDatabaseMediaListBase, nsIClassInfo, nsIObserver, sbILibrary, sbILocalDatabaseLibrary, sbILibraryStatistics) NS_IMPL_CI_INTERFACE_GETTER8(sbLocalDatabaseLibrary
_updateTextAndScrollDataForFrame aData
nsresult InitQuery(sbIDatabaseQuery *aQuery, nsStringArray *aURIArray, nsIArray *aPropertyArrayArray)
#define NS_UUID_GENERATOR_CONTRACTID
NS_DECL_ISUPPORTS sbLocalDatabaseLibraryAsyncRunner(sbLocalDatabaseLibrary *aLocalDatabaseLibrary, nsISimpleEnumerator *aMediaItems, sbIAddMediaItemsListener *aListener)
NS_IMETHOD GetItemByGuid(const nsAString &aGuid, sbIMediaItem **_retval)
const PRUint32 COLLECT_SUM
NS_IMETHOD GetType(nsAString &aType)
Songbird Database Object Definition.