29 #include <nsIClassInfoImpl.h>
30 #include <nsIMutableArray.h>
31 #include <nsIProgrammingLanguage.h>
32 #include <nsISimpleEnumerator.h>
34 #include <sbIDatabaseQuery.h>
35 #include <sbIDatabaseResult.h>
36 #include <sbILibrary.h>
37 #include <sbILocalDatabaseGUIDArray.h>
38 #include <sbILocalDatabasePropertyCache.h>
39 #include <sbIMediaListView.h>
40 #include <sbISQLBuilder.h>
48 #include <nsArrayUtils.h>
49 #include <nsAutoLock.h>
50 #include <nsAutoPtr.h>
52 #include <nsComponentManagerUtils.h>
54 #include <nsXPCOMCID.h>
61 #define DEFAULT_SORT_PROPERTY NS_LITERAL_STRING(SB_PROPERTY_ORDINAL)
62 #define DEFAULT_FETCH_SIZE 1000
69 static PRLogModuleInfo* gLocalDatabaseSimpleMediaListLog = nsnull;
72 #define TRACE(args) PR_LOG(gLocalDatabaseSimpleMediaListLog, PR_LOG_DEBUG, args)
73 #define LOG(args) PR_LOG(gLocalDatabaseSimpleMediaListLog, PR_LOG_WARN, args)
76 #define ASSERT_LIST_IS_LIBRARY(_mediaList) \
79 nsCOMPtr<sbILibrary> library = do_QueryInterface(mLibrary, &rv); \
80 NS_ASSERTION(NS_SUCCEEDED(rv), "Library won't QI to sbILibrary!"); \
82 if (NS_SUCCEEDED(rv)) { \
83 PRBool listIsLibrary; \
84 rv = library->Equals(_mediaList, &listIsLibrary); \
85 NS_ASSERTION(NS_SUCCEEDED(rv), "Equals failed!"); \
87 if (NS_SUCCEEDED(rv)) { \
88 NS_ASSERTION(listIsLibrary, "Watching the wrong list!"); \
93 #define ASSERT_LIST_IS_LIBRARY(_mediaList) \
94 PR_BEGIN_MACRO PR_END_MACRO
97 #define SB_ENSURE_INDEX_BEGIN \
101 rv = GetArray()->GetLength(&length); \
102 NS_ENSURE_SUCCESS(rv, rv);
104 #define SB_ENSURE_INDEX_END \
107 #define SB_ENSURE_INDEX1(_index) \
108 SB_ENSURE_INDEX_BEGIN \
109 NS_ENSURE_ARG_MAX(_index, length - 1); \
112 #define SB_ENSURE_INDEX2(_index1, _index2) \
113 SB_ENSURE_INDEX_BEGIN \
114 NS_ENSURE_ARG_MAX(_index1, length - 1); \
115 NS_ENSURE_ARG_MAX(_index2, length - 1); \
122 NS_DECL_SBIADDMEDIAITEMSLISTENER
125 nsIArray * aItemsToAdd,
126 nsInterfaceHashtable<nsISupportsHashKey, sbIMediaItem> & aForeignItems):
127 mItemsToAdd(aItemsToAdd),
128 mForeignItems(aForeignItems),
133 nsIArray * mItemsToAdd;
134 nsInterfaceHashtable<nsISupportsHashKey, sbIMediaItem> & mForeignItems;
142 sbAddSomeEnumListener::OnItemAdded(
sbIMediaItem * aMediaItem)
145 NS_ENSURE_ARG_POINTER(aMediaItem);
147 nsCOMPtr<sbIMediaItem> existingItem = do_QueryElementAt(mItemsToAdd,
150 NS_ENSURE_SUCCESS(rv, rv);
152 NS_ASSERTION(mForeignItems.Get(existingItem, nsnull),
153 "The old item should be in the hashtable!");
155 PRBool success = mForeignItems.Put(existingItem, aMediaItem);
156 NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
162 sbAddSomeEnumListener::OnComplete()
168 sbAddSomeEnumListener::OnProgress(PRUint32 aItemsCompleted, PRBool aCompleted)
186 PRUint32 aStartingIndex,
187 const nsAString& aStartingOrdinal)
188 : mLocalDatabaseSimpleMediaList(aLocalDatabaseSimpleMediaList)
189 , mListener(aListener)
190 , mMediaItems(aMediaItems)
191 , mStartingIndex(aStartingIndex)
192 , mStartingOrdinal(aStartingOrdinal) {}
195 mLocalDatabaseSimpleMediaList->AddSomeAsyncInternal(mMediaItems,
199 NS_ENSURE_SUCCESS(rv, rv);
204 nsRefPtr<sbLocalDatabaseSimpleMediaList> mLocalDatabaseSimpleMediaList;
205 nsCOMPtr<nsISupports> mListener;
206 nsCOMPtr<nsISimpleEnumerator> mMediaItems;
207 PRUint32 mStartingIndex;
208 nsString mStartingOrdinal;
238 NS_ENSURE_ARG_POINTER(_retval);
240 NS_ASSERTION(aMediaList != mFriendList,
241 "Can't enumerate our friend media list!");
243 PRBool success = mItemsToCreateOrAdd.Init();
244 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
246 nsresult rv = mFriendList->GetLibrary(getter_AddRefs(mListLibrary));
247 NS_ENSURE_SUCCESS(rv, rv);
259 sbSimpleMediaListInsertingEnumerationListener::OnEnumeratedItem(
sbIMediaList* aMediaList,
263 NS_ENSURE_ARG_POINTER(aMediaItem);
264 NS_ENSURE_ARG_POINTER(_retval);
266 NS_ASSERTION(aMediaList != mFriendList,
267 "Can't enumerate our friend media list!");
269 nsCOMPtr<sbILibrary> itemLibrary;
270 nsresult rv = aMediaItem->GetLibrary(getter_AddRefs(itemLibrary));
271 NS_ENSURE_SUCCESS(rv, rv);
274 rv = itemLibrary->Equals(mListLibrary, &sameLibrary);
275 NS_ENSURE_SUCCESS(rv, rv);
279 nsString listLibGuid;
280 rv = mListLibrary->GetGuid(listLibGuid);
281 NS_ENSURE_SUCCESS(rv, rv);
288 if (!sameLibrary && !mItemsToCreateOrAdd.Get(aMediaItem, nsnull)) {
295 nsString originLibGuid, originItemGuid;
296 rv = aMediaItem->GetProperty(PROP_LIBRARY, originLibGuid);
297 NS_ENSURE_SUCCESS(rv, rv);
298 if (originLibGuid.IsEmpty() || itemIsInMainLibrary) {
299 rv = itemLibrary->GetGuid(originLibGuid);
300 NS_ENSURE_SUCCESS(rv, rv);
302 rv = aMediaItem->GetProperty(PROP_ITEM, originItemGuid);
303 NS_ENSURE_SUCCESS(rv, rv);
304 if (originItemGuid.IsEmpty() || itemIsInMainLibrary) {
305 rv = aMediaItem->GetGuid(originItemGuid);
306 NS_ENSURE_SUCCESS(rv, rv);
310 if (listLibGuid.Equals(originLibGuid)) {
312 rv = mListLibrary->GetMediaItem(originItemGuid, getter_AddRefs(foundItem));
318 nsCOMPtr<sbIMutablePropertyArray> originGuidArray =
320 NS_ENSURE_SUCCESS(rv, rv);
322 rv = originGuidArray->AppendProperty(PROP_LIBRARY, originLibGuid);
323 NS_ENSURE_SUCCESS(rv, rv);
325 rv = originGuidArray->AppendProperty(PROP_ITEM, originItemGuid);
326 NS_ENSURE_SUCCESS(rv, rv);
328 nsRefPtr<sbMediaListEnumSingleItemHelper> guidCheckHelper =
330 NS_ENSURE_TRUE(guidCheckHelper, NS_ERROR_OUT_OF_MEMORY);
332 rv = mListLibrary->EnumerateItemsByProperties(originGuidArray,
335 NS_ENSURE_SUCCESS(rv, rv);
337 foundItem = guidCheckHelper->GetItem();
341 success = mItemsToCreateOrAdd.Put(aMediaItem, foundItem);
342 NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
348 success = mItemList.AppendObject(foundItem ? foundItem.get() : aMediaItem);
349 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
357 sbSimpleMediaListInsertingEnumerationListener::UpdateItemsInForeignLib(
358 nsIArray * aExistingItems,
359 nsIArray * aNewItems)
361 NS_ENSURE_ARG_POINTER(aExistingItems);
362 NS_ENSURE_ARG_POINTER(aNewItems);
366 PRUint32 existingItemsCount;
367 rv = aExistingItems->GetLength(&existingItemsCount);
368 NS_ENSURE_SUCCESS(rv, rv);
370 PRUint32 newItemsCount;
371 rv = aNewItems->GetLength(&newItemsCount);
372 NS_ENSURE_SUCCESS(rv, rv);
374 NS_ASSERTION(existingItemsCount == newItemsCount,
375 "The number of existing items much match the number of new items");
377 for (PRUint32 index = 0; index < existingItemsCount; index++) {
378 nsCOMPtr<sbIMediaItem> existingItem = do_QueryElementAt(aExistingItems,
381 NS_ENSURE_SUCCESS(rv, rv);
383 nsCOMPtr<sbIMediaItem> newItem = do_QueryElementAt(aNewItems, index, &rv);
384 NS_ENSURE_SUCCESS(rv, rv);
386 NS_ASSERTION(mItemsToCreateOrAdd.Get(existingItem, nsnull),
387 "The old item should be in the hashtable!");
389 PRBool success = mItemsToCreateOrAdd.Put(existingItem, newItem);
390 NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
400 sbSimpleMediaListInsertingEnumerationListener::OnEnumerationEnd(
sbIMediaList* aMediaList,
401 nsresult aStatusCode)
403 TRACE((
"LocalDatabaseSimpleMediaList[0x%.8x] - "
404 "InsertingEnumerator::OnEnumerationEnd",
this));
406 NS_ASSERTION(aMediaList != mFriendList,
407 "Can't enumerate our friend media list!");
409 const PRUint32 itemCount = mItemList.Count();
411 NS_WARNING(
"OnEnumerationEnd called with no items enumerated");
418 nsCOMPtr<nsIMutableArray> itemsToAdd =
419 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
420 NS_ENSURE_SUCCESS(rv, rv);
423 nsCOMPtr<nsIMutableArray> itemsToCreate =
424 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
425 NS_ENSURE_SUCCESS(rv, rv);
428 nsCOMPtr<nsIMutableArray> itemsToCreateURIs =
429 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
430 NS_ENSURE_SUCCESS(rv, rv);
435 PRUint32 numItems = mItemList.Count();
436 for (PRUint32
i = 0;
i < numItems;
i++) {
439 nsCOMPtr<sbIMediaItem> existing;
447 const PRBool found = mItemsToCreateOrAdd.Get(item,
448 getter_AddRefs(existing));
451 rv = itemsToAdd->AppendElement(existing, PR_FALSE);
452 NS_ENSURE_SUCCESS(rv, rv);
455 nsCOMPtr<nsIURI>
uri;
456 rv = item->GetContentSrc(getter_AddRefs(uri));
457 NS_ENSURE_SUCCESS(rv, rv);
459 rv = itemsToCreate->AppendElement(item, PR_FALSE);
460 NS_ENSURE_SUCCESS(rv, rv);
462 rv = itemsToCreateURIs->AppendElement(uri, PR_FALSE);
463 NS_ENSURE_SUCCESS(rv, rv);
468 PRUint32 itemsToCreateCount;
469 rv = itemsToCreate->GetLength(&itemsToCreateCount);
470 NS_ENSURE_SUCCESS(rv, rv);
473 if (itemsToCreateCount) {
475 nsCOMPtr<nsIMutableArray> propertyArrayArray =
476 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
477 NS_ENSURE_SUCCESS(rv, rv);
479 for (PRUint32
i = 0;
i < itemsToCreateCount;
i++) {
480 nsCOMPtr<sbIMediaItem> item = do_QueryElementAt(itemsToCreate,
i, &rv);
481 NS_ENSURE_SUCCESS(rv, rv);
483 nsCOMPtr<sbIPropertyArray> properties;
484 rv = item->GetProperties(nsnull, getter_AddRefs(properties));
485 NS_ENSURE_SUCCESS(rv, rv);
487 nsCOMPtr<sbIPropertyArray> filteredProperties;
489 getter_AddRefs(filteredProperties));
490 NS_ENSURE_SUCCESS(rv, rv);
492 nsCOMPtr<sbIMutablePropertyArray> mutableProperties =
493 do_QueryInterface(filteredProperties, &rv);
494 NS_ENSURE_SUCCESS(rv, rv);
498 NS_ENSURE_SUCCESS(rv, rv);
500 rv = propertyArrayArray->AppendElement(filteredProperties, PR_FALSE);
501 NS_ENSURE_SUCCESS(rv, rv);
506 nsCOMPtr<nsIArray> newItems;
507 rv = mListLibrary->BatchCreateMediaItems(itemsToCreateURIs,
510 getter_AddRefs(newItems));
511 NS_ENSURE_SUCCESS(rv, rv);
513 PRUint32 newItemCount;
514 rv = newItems->GetLength(&newItemCount);
515 NS_ENSURE_SUCCESS(rv, rv);
518 PRUint32 itemsToCreateCount;
519 rv = itemsToCreateURIs->GetLength(&itemsToCreateCount);
520 NS_ASSERTION(NS_SUCCEEDED(rv) && newItemCount == itemsToCreateCount,
521 "BatchCreateMediaItems didn't make the right number of items!");
524 rv = UpdateItemsInForeignLib(itemsToCreate, newItems);
525 NS_ENSURE_SUCCESS(rv, rv);
529 PRUint32 itemsToAddCount;
530 rv = itemsToAdd->GetLength(&itemsToAddCount);
531 NS_ENSURE_SUCCESS(rv, rv);
533 if (itemsToAddCount) {
536 mItemsToCreateOrAdd);
538 nsCOMPtr<nsISimpleEnumerator> itemsToAddEnumerator;
539 rv = itemsToAdd->Enumerate(getter_AddRefs(itemsToAddEnumerator));
540 NS_ENSURE_SUCCESS(rv, rv);
541 rv = mListLibrary->AddMediaItems(itemsToAddEnumerator,
544 NS_ENSURE_SUCCESS(rv, rv);
546 nsCOMPtr<sbIDatabaseQuery> query;
548 NS_ENSURE_SUCCESS(rv, rv);
550 rv = query->AddQuery(NS_LITERAL_STRING(
"begin"));
551 NS_ENSURE_SUCCESS(rv, rv);
553 nsString ordinal = mStartingOrdinal;
557 for (PRUint32 index = 0; index < itemCount; index++) {
559 nsCOMPtr<sbIMediaItem> mediaItem = mItemList[index];
565 nsCOMPtr<sbIMediaItem> newMediaItem;
566 PRBool success = mItemsToCreateOrAdd.Get(mediaItem,
567 getter_AddRefs(newMediaItem));
571 rv = mFriendList->NotifyCopyListener(mediaItem, newMediaItem);
572 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Failed to notify copy listener!");
577 success = mItemList.ReplaceObjectAt(newMediaItem, index);
578 NS_ENSURE_SUCCESS(rv, rv);
580 mediaItem = newMediaItem;
583 nsCOMPtr<sbILocalDatabaseMediaItem> ldbmi =
584 do_QueryInterface(mediaItem, &rv);
585 NS_ENSURE_SUCCESS(rv, rv);
587 PRUint32 mediaItemId;
588 rv = ldbmi->GetMediaItemId(&mediaItemId);
589 NS_ENSURE_SUCCESS(rv, rv);
591 rv = query->AddQuery(mFriendList->mInsertIntoListQuery);
592 NS_ENSURE_SUCCESS(rv, rv);
594 rv = query->BindInt32Parameter(0, mediaItemId);
595 NS_ENSURE_SUCCESS(rv, rv);
597 rv = query->BindStringParameter(1, ordinal);
598 NS_ENSURE_SUCCESS(rv, rv);
601 rv = mFriendList->AddToLastPathSegment(ordinal, 1);
602 NS_ENSURE_SUCCESS(rv, rv);
605 rv = query->AddQuery(NS_LITERAL_STRING(
"commit"));
606 NS_ENSURE_SUCCESS(rv, rv);
609 rv = query->Execute(&dbSuccess);
610 NS_ENSURE_SUCCESS(rv, rv);
611 NS_ENSURE_TRUE(dbSuccess == 0, NS_ERROR_FAILURE);
614 rv = mFriendList->
GetArray()->Invalidate(PR_TRUE);
615 NS_ENSURE_SUCCESS(rv, rv);
617 rv = mFriendList->UpdateLastModifiedTime();
618 NS_ENSURE_SUCCESS(rv, rv);
622 for (PRUint32 index = 0; index < itemCount; index++) {
625 rv = mAddListener->OnItemAdded(item);
626 NS_ENSURE_SUCCESS(rv, rv);
630 mStartingIndex + index);
635 rv = mAddListener->OnComplete();
636 NS_ENSURE_SUCCESS(rv, rv);
669 NS_ENSURE_ARG_POINTER(_retval);
672 nsresult rv = mFriendList->MakeStandardQuery(getter_AddRefs(mDBQuery));
673 NS_ENSURE_SUCCESS(rv, rv);
675 rv = mDBQuery->AddQuery(NS_LITERAL_STRING(
"begin"));
676 NS_ENSURE_SUCCESS(rv, rv);
687 sbSimpleMediaListRemovingEnumerationListener::OnEnumeratedItem(
sbIMediaList* aMediaList,
691 NS_ENSURE_ARG_POINTER(aMediaItem);
692 NS_ENSURE_ARG_POINTER(_retval);
695 nsresult rv = mFriendList->IndexOf(aMediaItem, 0, &index);
697 if (rv == NS_ERROR_NOT_AVAILABLE) {
700 NS_ENSURE_SUCCESS(rv, rv);
703 PRBool success = mNotificationList.AppendObject(aMediaItem);
704 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
706 PRUint32* added = mNotificationIndexes.AppendElement(index);
707 NS_ENSURE_TRUE(added, NS_ERROR_OUT_OF_MEMORY);
709 nsCOMPtr<sbILocalDatabaseMediaItem> ldbmi =
710 do_QueryInterface(aMediaItem, &rv);
711 NS_ENSURE_SUCCESS(rv, rv);
713 rv = mDBQuery->AddQuery(mFriendList->mDeleteFirstListItemQuery);
714 NS_ENSURE_SUCCESS(rv, rv);
716 PRUint32 mediaItemId;
717 rv = ldbmi->GetMediaItemId(&mediaItemId);
718 NS_ENSURE_SUCCESS(rv, rv);
720 rv = mDBQuery->BindInt32Parameter(0, mediaItemId);
721 NS_ENSURE_SUCCESS(rv, rv);
725 mItemEnumerated = PR_TRUE;
733 sbSimpleMediaListRemovingEnumerationListener::OnEnumerationEnd(
sbIMediaList* aMediaList,
734 nsresult aStatusCode)
739 PRUint32
count = mNotificationList.Count();
741 for (PRUint32
i = 0;
i <
count;
i++) {
743 mNotificationList[
i],
744 mNotificationIndexes[i]);
748 if (mItemEnumerated) {
749 rv = mDBQuery->AddQuery(NS_LITERAL_STRING(
"commit"));
750 NS_ENSURE_SUCCESS(rv, rv);
753 rv = mDBQuery->Execute(&dbSuccess);
754 NS_ENSURE_SUCCESS(rv, rv);
755 NS_ENSURE_TRUE(dbSuccess == 0, NS_ERROR_FAILURE);
759 rv = mFriendList->
GetArray()->Invalidate(PR_TRUE);
760 NS_ENSURE_SUCCESS(rv, rv);
762 rv = mFriendList->UpdateLastModifiedTime();
763 NS_ENSURE_SUCCESS(rv, rv);
767 for (PRUint32
i = 0;
i <
count;
i++) {
769 mNotificationList[
i],
770 mNotificationIndexes[i]);
795 sbLocalDatabaseSimpleMediaList::sbLocalDatabaseSimpleMediaList()
797 MOZ_COUNT_CTOR(sbLocalDatabaseSimpleMediaList);
799 if (!gLocalDatabaseSimpleMediaListLog) {
800 gLocalDatabaseSimpleMediaListLog =
801 PR_NewLogModule(
"sbLocalDatabaseSimpleMediaList");
813 const nsAString& aGuid)
816 NS_ENSURE_SUCCESS(rv, rv);
819 NS_ENSURE_TRUE(
GetArray(), NS_ERROR_OUT_OF_MEMORY);
822 rv = GetMediaItemId(&mediaItemId);
823 NS_ENSURE_SUCCESS(rv, rv);
825 nsAutoString databaseGuid;
826 rv =
mLibrary->GetDatabaseGuid(databaseGuid);
827 NS_ENSURE_SUCCESS(rv, rv);
829 rv =
GetArray()->SetDatabaseGUID(databaseGuid);
830 NS_ENSURE_SUCCESS(rv, rv);
832 nsCOMPtr<nsIURI> databaseLocation;
833 rv =
mLibrary->GetDatabaseLocation(getter_AddRefs(databaseLocation));
834 NS_ENSURE_SUCCESS(rv, rv);
836 if (databaseLocation) {
837 rv =
GetArray()->SetDatabaseLocation(databaseLocation);
838 NS_ENSURE_SUCCESS(rv, rv);
841 rv =
GetArray()->SetBaseTable(NS_LITERAL_STRING(
"simple_media_lists"));
842 NS_ENSURE_SUCCESS(rv, rv);
844 rv =
GetArray()->SetBaseConstraintColumn(NS_LITERAL_STRING(
"media_item_id"));
845 NS_ENSURE_SUCCESS(rv, rv);
847 rv =
GetArray()->SetBaseConstraintValue(mediaItemId);
848 NS_ENSURE_SUCCESS(rv, rv);
851 NS_ENSURE_SUCCESS(rv, rv);
854 NS_ENSURE_SUCCESS(rv, rv);
856 nsCOMPtr<sbILocalDatabasePropertyCache> propertyCache;
857 rv = aLibrary->GetPropertyCache(getter_AddRefs(propertyCache));
858 NS_ENSURE_SUCCESS(rv, rv);
860 rv =
GetArray()->SetPropertyCache(propertyCache);
861 NS_ENSURE_SUCCESS(rv, rv);
863 nsCOMPtr<sbILocalDatabaseGUIDArrayLengthCache> lengthCache;
865 NS_ENSURE_SUCCESS(rv, rv);
867 rv =
GetArray()->SetLengthCache(lengthCache);
868 NS_ENSURE_SUCCESS(rv, rv);
870 rv = CreateQueries();
871 NS_ENSURE_SUCCESS(rv, rv);
873 PRBool success = mShouldNotifyAfterRemove.Init();
874 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
882 aType.Assign(NS_LITERAL_STRING(
"simple"));
890 NS_ENSURE_ARG_POINTER(_retval);
893 nsCOMPtr<sbIMediaItem> item;
894 rv = sbLocalDatabaseMediaListBase::GetItemByGuid(aGuid, getter_AddRefs(item));
895 NS_ENSURE_SUCCESS(rv, rv);
899 NS_ENSURE_SUCCESS(rv, rv);
902 NS_ADDREF(*_retval = item);
906 return NS_ERROR_NOT_AVAILABLE;
915 NS_ENSURE_ARG_POINTER(aMediaItem);
916 NS_ENSURE_ARG_POINTER(_retval);
922 rv = aMediaItem->GetGuid(guid);
923 NS_ENSURE_SUCCESS(rv, rv);
926 rv =
GetArray()->ContainsGuid(guid, _retval);
927 NS_ENSURE_SUCCESS(rv, rv);
935 return AddItem(aMediaItem, nsnull);
942 NS_ENSURE_ARG_POINTER(aMediaItem);
946 PRUint32 startingIndex;
947 nsresult rv = GetLength(&startingIndex);
948 NS_ENSURE_SUCCESS(rv, rv);
950 nsString startingOrdinal;
951 rv = GetNextOrdinal(startingOrdinal);
952 NS_ENSURE_SUCCESS(rv, rv);
959 rv = listener.OnEnumerationBegin(nsnull, &stepResult);
960 NS_ENSURE_SUCCESS(rv, rv);
962 rv = listener.OnEnumeratedItem(nsnull, aMediaItem, &stepResult);
963 NS_ENSURE_SUCCESS(rv, rv);
965 rv = listener.OnEnumerationEnd(nsnull,
NS_OK);
966 NS_ENSURE_SUCCESS(rv, rv);
969 rv = GetItemByIndex(startingIndex, aNewMediaItem);
970 NS_ENSURE_SUCCESS(rv, rv);
978 NS_ENSURE_ARG_POINTER(aMediaList);
984 PRUint32 startingIndex;
985 nsresult rv = GetLength(&startingIndex);
986 NS_ENSURE_SUCCESS(rv, rv);
988 nsString startingOrdinal;
989 rv = GetNextOrdinal(startingOrdinal);
990 NS_ENSURE_SUCCESS(rv, rv);
996 aMediaList->EnumerateAllItems(&listener,
998 NS_ENSURE_SUCCESS(rv, rv);
1004 sbLocalDatabaseSimpleMediaList::InsertAllBefore(PRUint32 aIndex,
1007 NS_ENSURE_ARG_POINTER(aMediaList);
1013 PRUint32 startingIndex;
1014 nsresult rv = GetLength(&startingIndex);
1015 NS_ENSURE_SUCCESS(rv, rv);
1017 nsString startingOrdinal;
1018 rv = GetBeforeOrdinal(aIndex, startingOrdinal);
1019 NS_ENSURE_SUCCESS(rv, rv);
1021 startingOrdinal.AppendLiteral(
".0");
1027 aMediaList->EnumerateAllItems(&
listener,
1029 NS_ENSURE_SUCCESS(rv, rv);
1046 NS_ENSURE_ARG_POINTER(aMediaItems);
1050 PRUint32 startingIndex;
1051 nsresult rv = GetLength(&startingIndex);
1052 NS_ENSURE_SUCCESS(rv, rv);
1054 nsString startingOrdinal;
1055 rv = GetNextOrdinal(startingOrdinal);
1056 NS_ENSURE_SUCCESS(rv, rv);
1058 nsCOMPtr<nsIThread>
target;
1059 rv = NS_GetMainThread(getter_AddRefs(target));
1060 NS_ENSURE_SUCCESS(rv, rv);
1062 nsCOMPtr<sbIAddMediaItemsListener> proxiedListener;
1067 NS_PROXY_SYNC | NS_PROXY_ALWAYS,
1068 getter_AddRefs(proxiedListener));
1069 NS_ENSURE_SUCCESS(rv, rv);
1071 nsRefPtr<sbLocalDatabaseSimpleMediaListAddSomeAsyncRunner> runner =
1073 this, aMediaItems, proxiedListener, startingIndex, startingOrdinal);
1074 NS_ENSURE_TRUE(runner, NS_ERROR_OUT_OF_MEMORY);
1076 nsCOMPtr<nsIThreadPool> threadPoolService =
1077 do_GetService(
"@songbirdnest.com/Songbird/ThreadPoolService;1", &rv);
1078 NS_ENSURE_SUCCESS(rv, rv);
1080 rv = threadPoolService->Dispatch(runner, NS_DISPATCH_NORMAL);
1081 NS_ENSURE_SUCCESS(rv, rv);
1088 PRUint32 startingIndex;
1089 nsresult rv = GetLength(&startingIndex);
1090 NS_ENSURE_SUCCESS(rv, rv);
1092 nsString startingOrdinal;
1093 rv = GetNextOrdinal(startingOrdinal);
1094 NS_ENSURE_SUCCESS(rv, rv);
1101 PRUint16 stepResult;
1102 rv = listener.OnEnumerationBegin(nsnull, &stepResult);
1103 NS_ENSURE_SUCCESS(rv, rv);
1108 while (NS_SUCCEEDED(aMediaItems->HasMoreElements(&hasMore)) && hasMore) {
1109 nsCOMPtr<nsISupports> supports;
1110 rv = aMediaItems->GetNext(getter_AddRefs(supports));
1113 nsCOMPtr<sbIMediaItem> item = do_QueryInterface(supports, &rv);
1116 rv = listener.OnEnumeratedItem(nsnull, item, &stepResult);
1117 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"OnEnumeratedItem failed!");
1120 rv = listener.OnEnumerationEnd(nsnull,
NS_OK);
1121 NS_ENSURE_SUCCESS(rv, rv);
1129 PRUint32 aStartingIndex,
1130 nsAString& aStartingOrdinal)
1132 NS_ENSURE_ARG_POINTER(aMediaItems);
1133 NS_ENSURE_ARG_POINTER(aListener);
1137 NS_ASSERTION(!NS_IsMainThread(),
1138 "AddSomeAsyncInternal should never be called on the main thread!");
1140 nsCOMPtr<sbIMediaListAsyncListener> asyncListener;
1143 asyncListener = do_QueryInterface(aListener);
1144 addListener = do_QueryInterface(aListener);
1153 PRUint16 stepResult;
1154 rv = listener.OnEnumerationBegin(nsnull, &stepResult);
1155 NS_ENSURE_SUCCESS(rv, rv);
1160 PRUint32 itemsProcessed = 0;
1162 while (NS_SUCCEEDED(aMediaItems->HasMoreElements(&hasMore)) && hasMore) {
1163 nsCOMPtr<nsISupports> supports;
1164 rv = aMediaItems->GetNext(getter_AddRefs(supports));
1167 nsCOMPtr<sbIMediaItem> item = do_QueryInterface(supports, &rv);
1170 rv = listener.OnEnumeratedItem(nsnull, item, &stepResult);
1171 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"OnEnumeratedItem failed!");
1178 if (asyncListener) {
1179 rv = asyncListener->OnProgress(itemsProcessed, PR_FALSE);
1180 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Failed to call async listener.");
1182 else if (addListener) {
1183 rv = addListener->OnProgress(itemsProcessed, PR_FALSE);
1184 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Failed to call add listener.");
1192 rv = listener.OnEnumerationEnd(nsnull,
NS_OK);
1193 NS_ENSURE_SUCCESS(rv, rv);
1195 if (asyncListener) {
1196 rv = asyncListener->OnProgress(itemsProcessed, PR_TRUE);
1197 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Failed to call async listener.");
1199 else if (addListener) {
1200 rv = addListener->OnProgress(itemsProcessed, PR_TRUE);
1201 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Failed to call add listener.");
1207 sbLocalDatabaseSimpleMediaList::InsertBefore(PRUint32 aIndex,
1210 NS_ENSURE_ARG_POINTER(aMediaItem);
1214 nsString startingOrdinal;
1215 nsresult rv = GetBeforeOrdinal(aIndex, startingOrdinal);
1216 NS_ENSURE_SUCCESS(rv, rv);
1222 PRUint16 stepResult;
1223 rv =
listener.OnEnumerationBegin(nsnull, &stepResult);
1224 NS_ENSURE_SUCCESS(rv, rv);
1226 rv =
listener.OnEnumeratedItem(nsnull, aMediaItem, &stepResult);
1227 NS_ENSURE_SUCCESS(rv, rv);
1230 NS_ENSURE_SUCCESS(rv, rv);
1236 sbLocalDatabaseSimpleMediaList::MoveBefore(PRUint32 aFromIndex,
1240 if (aFromIndex == aToIndex) {
1250 nsAutoString ordinal;
1251 rv = GetBeforeOrdinal(aToIndex, ordinal);
1252 NS_ENSURE_SUCCESS(rv, rv);
1254 rv = UpdateOrdinalByIndex(aFromIndex, ordinal);
1255 NS_ENSURE_SUCCESS(rv, rv);
1258 rv =
GetArray()->Invalidate(PR_FALSE);
1259 NS_ENSURE_SUCCESS(rv, rv);
1261 rv = UpdateLastModifiedTime();
1262 NS_ENSURE_SUCCESS(rv, rv);
1264 nsCOMPtr<sbIMediaList> list =
1266 NS_ENSURE_SUCCESS(rv, rv);
1270 if (aFromIndex < aToIndex) {
1282 sbLocalDatabaseSimpleMediaList::MoveLast(PRUint32 aIndex)
1290 nsAutoString ordinal;
1291 rv = GetNextOrdinal(ordinal);
1292 NS_ENSURE_SUCCESS(rv, rv);
1294 rv = UpdateOrdinalByIndex(aIndex, ordinal);
1295 NS_ENSURE_SUCCESS(rv, rv);
1299 rv =
GetArray()->GetLength(&length);
1300 NS_ENSURE_SUCCESS(rv, rv);
1303 rv =
GetArray()->Invalidate(PR_FALSE);
1304 NS_ENSURE_SUCCESS(rv, rv);
1306 rv = UpdateLastModifiedTime();
1307 NS_ENSURE_SUCCESS(rv, rv);
1309 nsCOMPtr<sbIMediaList> list =
1311 NS_ENSURE_SUCCESS(rv, rv);
1321 sbLocalDatabaseSimpleMediaList::InsertSomeBefore(PRUint32 aIndex,
1324 NS_ENSURE_ARG_POINTER(aMediaItems);
1328 nsString startingOrdinal;
1329 nsresult rv = GetBeforeOrdinal(aIndex, startingOrdinal);
1330 NS_ENSURE_SUCCESS(rv, rv);
1332 startingOrdinal.AppendLiteral(
".0");
1338 PRUint16 stepResult;
1339 rv =
listener.OnEnumerationBegin(nsnull, &stepResult);
1340 NS_ENSURE_SUCCESS(rv, rv);
1345 while (NS_SUCCEEDED(aMediaItems->HasMoreElements(&hasMore)) && hasMore) {
1346 nsCOMPtr<nsISupports> supports;
1347 rv = aMediaItems->GetNext(getter_AddRefs(supports));
1350 nsCOMPtr<sbIMediaItem> item = do_QueryInterface(supports, &rv);
1353 rv =
listener.OnEnumeratedItem(nsnull, item, &stepResult);
1354 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"OnEnumeratedItem failed!");
1358 NS_ENSURE_SUCCESS(rv, rv);
1364 sbLocalDatabaseSimpleMediaList::InsertSomeBeforeAsync(
1369 NS_ENSURE_ARG_POINTER(aMediaItems);
1370 NS_ENSURE_ARG_POINTER(aListener);
1375 nsString startingOrdinal;
1376 rv = GetBeforeOrdinal(aIndex, startingOrdinal);
1377 NS_ENSURE_SUCCESS(rv, rv);
1379 startingOrdinal.AppendLiteral(
".0");
1381 nsCOMPtr<nsIThread>
target;
1382 rv = NS_GetMainThread(getter_AddRefs(target));
1383 NS_ENSURE_SUCCESS(rv, rv);
1385 nsCOMPtr<sbIMediaListAsyncListener> proxiedListener;
1389 NS_PROXY_SYNC | NS_PROXY_ALWAYS,
1390 getter_AddRefs(proxiedListener));
1391 NS_ENSURE_SUCCESS(rv, rv);
1393 nsRefPtr<sbLocalDatabaseSimpleMediaListAddSomeAsyncRunner> runner =
1395 this, aMediaItems, proxiedListener, aIndex, startingOrdinal);
1396 NS_ENSURE_TRUE(runner, NS_ERROR_OUT_OF_MEMORY);
1398 nsCOMPtr<nsIThreadPool> threadPoolService =
1399 do_GetService(
"@songbirdnest.com/Songbird/ThreadPoolService;1", &rv);
1400 NS_ENSURE_SUCCESS(rv, rv);
1402 rv = threadPoolService->Dispatch(runner, NS_DISPATCH_NORMAL);
1403 NS_ENSURE_SUCCESS(rv, rv);
1409 sbLocalDatabaseSimpleMediaList::MoveSomeBefore(PRUint32* aFromIndexArray,
1410 PRUint32 aFromIndexArrayCount,
1413 NS_ENSURE_ARG_POINTER(aFromIndexArray);
1420 nsAutoString ordinal;
1421 rv = GetBeforeOrdinal(aToIndex, ordinal);
1422 NS_ENSURE_SUCCESS(rv, rv);
1424 ordinal.AppendLiteral(
".");
1425 rv = MoveSomeInternal(aFromIndexArray,
1426 aFromIndexArrayCount,
1429 NS_ENSURE_SUCCESS(rv, rv);
1435 sbLocalDatabaseSimpleMediaList::MoveSomeLast(PRUint32* aIndexArray,
1436 PRUint32 aIndexArrayCount)
1440 NS_ENSURE_ARG_POINTER(aIndexArray);
1442 nsAutoString ordinal;
1443 nsresult rv = GetNextOrdinal(ordinal);
1444 NS_ENSURE_SUCCESS(rv, rv);
1446 ordinal.AppendLiteral(
".");
1449 rv =
GetArray()->GetLength(&length);
1450 NS_ENSURE_SUCCESS(rv, rv);
1452 rv = MoveSomeInternal(aIndexArray, aIndexArrayCount, length, ordinal);
1453 NS_ENSURE_SUCCESS(rv, rv);
1461 NS_ENSURE_ARG_POINTER(aMediaItem);
1469 PRUint16 stepResult;
1470 nsresult rv = listener.OnEnumerationBegin(nsnull, &stepResult);
1471 NS_ENSURE_SUCCESS(rv, rv);
1473 rv = listener.OnEnumeratedItem(nsnull, aMediaItem, &stepResult);
1474 NS_ENSURE_SUCCESS(rv, rv);
1476 rv = listener.OnEnumerationEnd(nsnull,
NS_OK);
1477 NS_ENSURE_SUCCESS(rv, rv);
1489 nsAutoString ordinal;
1490 rv =
GetArray()->GetSortPropertyValueByIndex(aIndex, ordinal);
1491 NS_ENSURE_SUCCESS(rv, rv);
1493 nsCOMPtr<sbIMediaItem> item;
1494 rv = GetItemByIndex(aIndex, getter_AddRefs(item));
1497 nsCOMPtr<sbIDatabaseQuery> dbQuery;
1499 NS_ENSURE_SUCCESS(rv, rv);
1501 rv = dbQuery->AddQuery(mDeleteListItemByOrdinalQuery);
1502 NS_ENSURE_SUCCESS(rv, rv);
1504 rv = dbQuery->BindStringParameter(0, ordinal);
1505 NS_ENSURE_SUCCESS(rv, rv);
1508 rv = dbQuery->Execute(&dbSuccess);
1509 NS_ENSURE_SUCCESS(rv, rv);
1510 NS_ENSURE_TRUE(dbSuccess == 0, NS_ERROR_FAILURE);
1512 rv =
GetArray()->RemoveByIndex(aIndex);
1513 NS_ENSURE_SUCCESS(rv, rv);
1515 rv = UpdateLastModifiedTime();
1516 NS_ENSURE_SUCCESS(rv, rv);
1518 nsCOMPtr<sbIMediaList> mediaList =
1520 NS_ENSURE_SUCCESS(rv, rv);
1533 NS_ENSURE_ARG_POINTER(aMediaItems);
1541 PRUint16 stepResult;
1542 nsresult rv = listener.OnEnumerationBegin(nsnull, &stepResult);
1543 NS_ENSURE_SUCCESS(rv, rv);
1546 while (NS_SUCCEEDED(aMediaItems->HasMoreElements(&hasMore)) && hasMore) {
1548 nsCOMPtr<nsISupports> supports;
1549 rv = aMediaItems->GetNext(getter_AddRefs(supports));
1552 nsCOMPtr<sbIMediaItem> item = do_QueryInterface(supports, &rv);
1555 rv = listener.OnEnumeratedItem(nsnull, item, &stepResult);
1556 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"OnEnumeratedItem failed!");
1559 rv = listener.OnEnumerationEnd(nsnull,
NS_OK);
1560 NS_ENSURE_SUCCESS(rv, rv);
1573 nsCOMPtr<sbIMediaList> mediaList =
1575 NS_ENSURE_SUCCESS(rv, rv);
1580 nsCOMPtr<sbIDatabaseQuery> query;
1582 NS_ENSURE_SUCCESS(rv, rv);
1584 rv = query->AddQuery(mDeleteAllQuery);
1585 NS_ENSURE_SUCCESS(rv, rv);
1587 rv = query->Execute(&dbOk);
1588 NS_ENSURE_SUCCESS(rv, rv);
1589 NS_ENSURE_TRUE(dbOk == 0, NS_ERROR_FAILURE);
1592 rv =
GetArray()->Invalidate(PR_TRUE);
1593 NS_ENSURE_SUCCESS(rv, rv);
1595 rv = UpdateLastModifiedTime();
1596 NS_ENSURE_SUCCESS(rv, rv);
1617 sbLocalDatabaseSimpleMediaList::NotifyContentChanged()
1621 nsresult rv =
GetArray()->Invalidate(PR_TRUE);
1622 NS_ENSURE_SUCCESS(rv, rv);
1627 nsCOMPtr<sbIMediaList> mediaList =
1629 NS_ENSURE_SUCCESS(rv, rv);
1641 (mediaList, PR_FALSE);
1643 (mediaList, PR_FALSE);
1647 rv =
GetArray()->GetLength(&length);
1648 NS_ENSURE_SUCCESS(rv, rv);
1650 for (PRUint32 index=0; index<
length; index++) {
1651 nsCOMPtr<sbIMediaItem> item;
1652 rv = GetItemByIndex(index, getter_AddRefs(item));
1664 NS_ENSURE_ARG_POINTER(_retval);
1668 nsAutoString defaultSortProperty;
1670 NS_ENSURE_SUCCESS(rv, rv);
1673 rv = GetMediaItemId(&mediaItemId);
1674 NS_ENSURE_SUCCESS(rv, rv);
1676 nsRefPtr<sbLocalDatabaseMediaListView>
1679 defaultSortProperty,
1681 NS_ENSURE_TRUE(view, NS_ERROR_OUT_OF_MEMORY);
1682 rv = view->Init(aState);
1683 NS_ENSURE_SUCCESS(rv, rv);
1685 NS_ADDREF(*_retval = view);
1691 sbLocalDatabaseSimpleMediaList::GetCopyListener(
1694 NS_ENSURE_ARG_POINTER(aCopyListener);
1696 *aCopyListener = nsnull;
1699 NS_ADDREF(*aCopyListener = mCopyListener);
1705 NS_IMETHODIMP sbLocalDatabaseSimpleMediaList::SetCopyListener(
1708 NS_ENSURE_ARG_POINTER(aCopyListener);
1710 mCopyListener = aCopyListener;
1716 sbLocalDatabaseSimpleMediaList::GetIndexByOrdinal(
const nsAString& aOrdinal,
1719 NS_ENSURE_ARG_POINTER(_retval);
1725 rv =
GetArray()->GetLength(&length);
1726 NS_ENSURE_SUCCESS(rv, rv);
1730 rv =
GetArray()->IsIndexCached(
i, &isCached);
1731 NS_ENSURE_SUCCESS(rv, rv);
1734 nsAutoString ordinal;
1735 rv =
GetArray()->GetOrdinalByIndex(
i, ordinal);
1736 NS_ENSURE_SUCCESS(rv, rv);
1738 if (ordinal.Equals(aOrdinal)) {
1747 rv =
GetArray()->GetFirstIndexByPrefix(aOrdinal, &index);
1748 if (NS_SUCCEEDED(rv)) {
1753 return NS_ERROR_NOT_AVAILABLE;
1757 sbLocalDatabaseSimpleMediaList::Invalidate(PRBool aInvalidateLength)
1759 nsresult rv =
GetArray()->Invalidate(aInvalidateLength);
1760 NS_ENSURE_SUCCESS(rv, rv);
1765 nsCOMPtr<sbIMediaList> list =
1767 NS_ENSURE_SUCCESS(rv, rv);
1769 nsCOMPtr<sbIMediaItem> item = do_QueryInterface(list, &rv);
1770 NS_ENSURE_SUCCESS(rv, rv);
1772 nsCOMPtr<sbIMutablePropertyArray> properties =
1774 NS_ENSURE_SUCCESS(rv, rv);
1789 NS_ENSURE_ARG_POINTER(aItem);
1790 NS_ENSURE_ARG_POINTER(aProperties);
1793 nsCOMPtr<sbIMediaList> list =
1795 NS_ENSURE_SUCCESS(rv, rv);
1807 NS_ENSURE_ARG_POINTER(aList);
1808 NS_ENSURE_ARG_POINTER(aItem);
1821 NS_ENSURE_ARG_POINTER(aList);
1822 NS_ENSURE_ARG_POINTER(aItem);
1833 PRBool aExcludeLists)
1835 NS_ENSURE_ARG_POINTER(aList);
1838 (aList, aExcludeLists);
1845 PRBool aExcludeLists)
1847 NS_ENSURE_ARG_POINTER(aList);
1857 NS_ENSURE_ARG_POINTER(aList);
1866 NS_ENSURE_ARG_POINTER(aList);
1873 sbLocalDatabaseSimpleMediaList::UpdateLastModifiedTime()
1879 NS_ENSURE_SUCCESS(rv, rv);
1885 sbLocalDatabaseSimpleMediaList::ExecuteAggregateQuery(
const nsAString& aQuery,
1891 nsCOMPtr<sbIDatabaseQuery> query;
1893 NS_ENSURE_SUCCESS(rv, rv);
1895 rv = query->AddQuery(aQuery);
1896 NS_ENSURE_SUCCESS(rv, rv);
1898 rv = query->Execute(&dbOk);
1899 NS_ENSURE_SUCCESS(rv, rv);
1900 NS_ENSURE_TRUE(dbOk == 0, NS_ERROR_FAILURE);
1902 nsCOMPtr<sbIDatabaseResult> result;
1903 rv = query->GetResultObject(getter_AddRefs(result));
1904 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
1907 rv = result->GetRowCount(&rowCount);
1908 NS_ENSURE_SUCCESS(rv, rv);
1910 if (rowCount == 0) {
1911 return NS_ERROR_UNEXPECTED;
1914 rv = result->GetRowCell(0, 0, aValue);
1915 NS_ENSURE_SUCCESS(rv, rv);
1921 sbLocalDatabaseSimpleMediaList::UpdateOrdinalByIndex(PRUint32 aIndex,
1922 const nsAString& aOrdinal)
1929 rv =
GetArray()->GetMediaItemIdByIndex(aIndex, &mediaItemId);
1930 NS_ENSURE_SUCCESS(rv, rv);
1933 nsString oldOrdinal;
1934 rv =
GetArray()->GetOrdinalByIndex(aIndex, oldOrdinal);
1935 NS_ENSURE_SUCCESS(rv, rv);
1938 nsCOMPtr<sbIDatabaseQuery> query;
1940 NS_ENSURE_SUCCESS(rv, rv);
1942 rv = query->AddQuery(mUpdateListItemOrdinalQuery);
1943 NS_ENSURE_SUCCESS(rv, rv);
1945 rv = query->BindStringParameter(0, aOrdinal);
1946 NS_ENSURE_SUCCESS(rv, rv);
1948 rv = query->BindInt32Parameter(1, mediaItemId);
1949 NS_ENSURE_SUCCESS(rv, rv);
1951 rv = query->BindStringParameter(2, oldOrdinal);
1952 NS_ENSURE_SUCCESS(rv, rv);
1954 rv = query->Execute(&dbOk);
1955 NS_ENSURE_SUCCESS(rv, rv);
1956 NS_ENSURE_TRUE(dbOk == 0, NS_ERROR_FAILURE);
1962 sbLocalDatabaseSimpleMediaList::MoveSomeInternal(PRUint32* aFromIndexArray,
1963 PRUint32 aFromIndexArrayCount,
1965 const nsAString& aOrdinalRoot)
1967 NS_ASSERTION(aFromIndexArray,
"aFromIndexArray is null");
1970 nsresult rv =
GetArray()->GetLength(&length);
1971 NS_ENSURE_SUCCESS(rv, rv);
1974 for (PRUint32
i = 0;
i < aFromIndexArrayCount;
i++) {
1975 NS_ENSURE_ARG_MAX(aFromIndexArray[
i], length - 1);
1978 nsCOMPtr<sbIDatabaseQuery> query;
1980 NS_ENSURE_SUCCESS(rv, rv);
1984 rv = query->AddQuery(NS_LITERAL_STRING(
"begin"));
1985 NS_ENSURE_SUCCESS(rv, rv);
1987 for (PRUint32
i = 0;
i < aFromIndexArrayCount;
i++) {
1988 nsAutoString ordinal(aOrdinalRoot);
1989 ordinal.AppendInt(
i);
1992 rv =
GetArray()->GetMediaItemIdByIndex(aFromIndexArray[
i], &mediaItemId);
1993 NS_ENSURE_SUCCESS(rv, rv);
1995 nsString oldOrdinal;
1996 rv =
GetArray()->GetOrdinalByIndex(aFromIndexArray[i], oldOrdinal);
1997 NS_ENSURE_SUCCESS(rv, rv);
1999 rv = query->AddQuery(mUpdateListItemOrdinalQuery);
2000 NS_ENSURE_SUCCESS(rv, rv);
2002 rv = query->BindStringParameter(0, ordinal);
2003 NS_ENSURE_SUCCESS(rv, rv);
2005 rv = query->BindInt32Parameter(1, mediaItemId);
2006 NS_ENSURE_SUCCESS(rv, rv);
2008 rv = query->BindStringParameter(2, oldOrdinal);
2009 NS_ENSURE_SUCCESS(rv, rv);
2012 rv = query->AddQuery(NS_LITERAL_STRING(
"commit"));
2013 NS_ENSURE_SUCCESS(rv, rv);
2016 rv = query->Execute(&dbOk);
2017 NS_ENSURE_SUCCESS(rv, rv);
2018 NS_ENSURE_TRUE(dbOk == 0, NS_ERROR_FAILURE);
2021 rv =
GetArray()->Invalidate(PR_FALSE);
2022 NS_ENSURE_SUCCESS(rv, rv);
2024 nsCOMPtr<sbIMediaList> list =
2026 NS_ENSURE_SUCCESS(rv, rv);
2028 nsTArray<PRUint32> shiftedIndexes;
2029 PRUint32* success = shiftedIndexes.AppendElements(aFromIndexArray,
2030 aFromIndexArrayCount);
2031 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
2033 for (PRUint32 i = 0; i < aFromIndexArrayCount; i++) {
2034 PRUint32 fromIndex = shiftedIndexes[
i];
2039 if (fromIndex < aToIndex) {
2048 for (PRUint32 j = i + 1; j < aFromIndexArrayCount; j++) {
2051 if (fromIndex < shiftedIndexes[j] && aToIndex > shiftedIndexes[j]) {
2052 shiftedIndexes[j]--;
2057 if (aToIndex <= shiftedIndexes[j] && fromIndex > shiftedIndexes[j]) {
2058 shiftedIndexes[j]++;
2065 if (fromIndex > aToIndex) {
2071 rv = UpdateLastModifiedTime();
2072 NS_ENSURE_SUCCESS(rv, rv);
2078 sbLocalDatabaseSimpleMediaList::GetNextOrdinal(nsAString& aValue)
2083 rv =
GetArray()->GetLength(&length);
2084 NS_ENSURE_SUCCESS(rv, rv);
2087 aValue.AssignLiteral(
"0");
2092 rv =
GetArray()->IsIndexCached(length - 1, &cached);
2093 NS_ENSURE_SUCCESS(rv, rv);
2096 rv =
GetArray()->GetSortPropertyValueByIndex(length - 1, aValue);
2097 NS_ENSURE_SUCCESS(rv, rv);
2100 rv = ExecuteAggregateQuery(mGetLastOrdinalQuery, aValue);
2101 NS_ENSURE_SUCCESS(rv, rv);
2104 rv = AddToLastPathSegment(aValue, 1);
2105 NS_ENSURE_SUCCESS(rv, rv);
2111 sbLocalDatabaseSimpleMediaList::GetBeforeOrdinal(PRUint32 aIndex,
2120 rv =
GetArray()->IsIndexCached(0, &cached);
2121 NS_ENSURE_SUCCESS(rv, rv);
2123 nsAutoString ordinal;
2125 rv =
GetArray()->GetSortPropertyValueByIndex(0, ordinal);
2126 NS_ENSURE_SUCCESS(rv, rv);
2129 rv = ExecuteAggregateQuery(mGetFirstOrdinalQuery, ordinal);
2130 NS_ENSURE_SUCCESS(rv, rv);
2134 PRUint32
pos = ordinal.FindChar(
'.');
2136 ordinal.SetLength(pos);
2139 PRInt32
value = ordinal.ToInteger(&rv) - 1;
2140 NS_ENSURE_SUCCESS(rv, rv);
2142 nsAutoString newOrdinal;
2143 newOrdinal.AppendInt(value);
2144 aValue = newOrdinal;
2150 nsAutoString aboveOrdinal;
2151 nsAutoString belowOrdinal;
2153 rv =
GetArray()->GetSortPropertyValueByIndex(aIndex - 1, aboveOrdinal);
2154 NS_ENSURE_SUCCESS(rv, rv);
2156 rv =
GetArray()->GetSortPropertyValueByIndex(aIndex, belowOrdinal);
2157 NS_ENSURE_SUCCESS(rv, rv);
2159 PRUint32 aboveLevels = CountLevels(aboveOrdinal);
2160 PRUint32 belowLevels = CountLevels(belowOrdinal);
2164 if (aboveLevels == belowLevels) {
2165 belowOrdinal.AppendLiteral(
".0");
2166 aValue = belowOrdinal;
2172 if (belowLevels > aboveLevels) {
2173 rv = AddToLastPathSegment(belowOrdinal, -1);
2174 NS_ENSURE_SUCCESS(rv, rv);
2175 aValue = belowOrdinal;
2181 rv = AddToLastPathSegment(aboveOrdinal, 1);
2182 NS_ENSURE_SUCCESS(rv, rv);
2183 aValue = aboveOrdinal;
2188 sbLocalDatabaseSimpleMediaList::AddToLastPathSegment(nsAString&
aPath,
2192 PRUint32 startPos = aPath.RFindChar(
'.') + 1;
2193 PRUint32 length = aPath.Length() - startPos;
2196 PRInt32 value = Substring(aPath, startPos, length).ToInteger(&rv);
2197 NS_ENSURE_SUCCESS(rv, rv);
2201 nsAutoString newValue;
2202 newValue.AppendInt(value);
2203 aPath.Replace(startPos, length, newValue);
2209 sbLocalDatabaseSimpleMediaList::CountLevels(
const nsAString& aPath)
2212 PRInt32 foundpos = aPath.FindChar(
'.');
2213 while(foundpos >= 0) {
2215 foundpos = aPath.FindChar(
'.', foundpos + 1);
2221 sbLocalDatabaseSimpleMediaList::CreateQueries()
2226 rv = GetMediaItemId(&mediaItemId);
2227 NS_ENSURE_SUCCESS(rv, rv);
2231 nsCOMPtr<sbISQLSelectBuilder> builder =
2233 NS_ENSURE_SUCCESS(rv, rv);
2235 nsCOMPtr<sbISQLBuilderCriterion> criterion;
2237 rv = builder->SetBaseTableName(NS_LITERAL_STRING(
"media_items"));
2238 NS_ENSURE_SUCCESS(rv, rv);
2240 rv = builder->SetBaseTableAlias(NS_LITERAL_STRING(
"_mi"));
2241 NS_ENSURE_SUCCESS(rv, rv);
2244 NS_LITERAL_STRING(
"simple_media_lists"),
2245 NS_LITERAL_STRING(
"_sml"),
2246 NS_LITERAL_STRING(
"member_media_item_id"),
2247 NS_LITERAL_STRING(
"_mi"),
2248 NS_LITERAL_STRING(
"media_item_id"));
2249 NS_ENSURE_SUCCESS(rv, rv);
2251 rv = builder->CreateMatchCriterionLong(NS_LITERAL_STRING(
"_sml"),
2252 NS_LITERAL_STRING(
"media_item_id"),
2255 getter_AddRefs(criterion));
2256 NS_ENSURE_SUCCESS(rv, rv);
2258 rv = builder->AddCriterion(criterion);
2259 NS_ENSURE_SUCCESS(rv, rv);
2262 rv = builder->ClearColumns();
2263 NS_ENSURE_SUCCESS(rv, rv);
2265 rv = builder->AddColumn(EmptyString(),
2266 NS_LITERAL_STRING(
"max(ordinal)"));
2267 NS_ENSURE_SUCCESS(rv, rv);
2269 rv = builder->ToString(mGetLastOrdinalQuery);
2270 NS_ENSURE_SUCCESS(rv, rv);
2273 rv = builder->ClearColumns();
2274 NS_ENSURE_SUCCESS(rv, rv);
2276 rv = builder->AddColumn(EmptyString(),
2277 NS_LITERAL_STRING(
"min(ordinal)"));
2278 NS_ENSURE_SUCCESS(rv, rv);
2280 rv = builder->ToString(mGetFirstOrdinalQuery);
2281 NS_ENSURE_SUCCESS(rv, rv);
2284 rv = builder->ClearColumns();
2285 NS_ENSURE_SUCCESS(rv, rv);
2287 rv = builder->AddColumn(NS_LITERAL_STRING(
"_mi"),
2288 NS_LITERAL_STRING(
"media_item_id"));
2289 NS_ENSURE_SUCCESS(rv, rv);
2291 rv = builder->CreateMatchCriterionParameter(NS_LITERAL_STRING(
"_mi"),
2292 NS_LITERAL_STRING(
"guid"),
2294 getter_AddRefs(criterion));
2295 NS_ENSURE_SUCCESS(rv, rv);
2297 rv = builder->AddCriterion(criterion);
2298 NS_ENSURE_SUCCESS(rv, rv);
2300 rv = builder->ToString(mGetMediaItemIdForGuidQuery);
2301 NS_ENSURE_SUCCESS(rv, rv);
2304 nsCOMPtr<sbISQLInsertBuilder>
insert =
2307 rv = insert->SetIntoTableName(NS_LITERAL_STRING(
"simple_media_lists"));
2308 NS_ENSURE_SUCCESS(rv, rv);
2310 rv = insert->AddColumn(NS_LITERAL_STRING(
"media_item_id"));
2311 NS_ENSURE_SUCCESS(rv, rv);
2313 rv = insert->AddColumn(NS_LITERAL_STRING(
"member_media_item_id"));
2314 NS_ENSURE_SUCCESS(rv, rv);
2316 rv = insert->AddColumn(NS_LITERAL_STRING(
"ordinal"));
2317 NS_ENSURE_SUCCESS(rv, rv);
2319 rv = insert->AddValueLong(mediaItemId);
2320 NS_ENSURE_SUCCESS(rv, rv);
2322 rv = insert->AddValueParameter();
2323 NS_ENSURE_SUCCESS(rv, rv);
2325 rv = insert->AddValueParameter();
2326 NS_ENSURE_SUCCESS(rv, rv);
2328 rv = insert->ToString(mInsertIntoListQuery);
2329 NS_ENSURE_SUCCESS(rv, rv);
2332 nsCOMPtr<sbISQLUpdateBuilder> update =
2335 rv = update->SetTableName(NS_LITERAL_STRING(
"simple_media_lists"));
2336 NS_ENSURE_SUCCESS(rv, rv);
2338 rv = update->AddAssignmentParameter(NS_LITERAL_STRING(
"ordinal"));
2339 NS_ENSURE_SUCCESS(rv, rv);
2341 rv = update->CreateMatchCriterionLong(EmptyString(),
2342 NS_LITERAL_STRING(
"media_item_id"),
2345 getter_AddRefs(criterion));
2346 NS_ENSURE_SUCCESS(rv, rv);
2348 rv = update->AddCriterion(criterion);
2349 NS_ENSURE_SUCCESS(rv, rv);
2351 rv = update->CreateMatchCriterionParameter(EmptyString(),
2352 NS_LITERAL_STRING(
"member_media_item_id"),
2354 getter_AddRefs(criterion));
2355 NS_ENSURE_SUCCESS(rv, rv);
2357 rv = update->AddCriterion(criterion);
2358 NS_ENSURE_SUCCESS(rv, rv);
2360 rv = update->CreateMatchCriterionParameter(EmptyString(),
2361 NS_LITERAL_STRING(
"ordinal"),
2363 getter_AddRefs(criterion));
2364 NS_ENSURE_SUCCESS(rv, rv);
2366 rv = update->AddCriterion(criterion);
2367 NS_ENSURE_SUCCESS(rv, rv);
2369 rv = update->ToString(mUpdateListItemOrdinalQuery);
2370 NS_ENSURE_SUCCESS(rv, rv);
2385 nsCOMPtr<sbISQLDeleteBuilder> deleteb =
2388 rv = deleteb->SetTableName(NS_LITERAL_STRING(
"simple_media_lists"));
2389 NS_ENSURE_SUCCESS(rv, rv);
2391 rv = deleteb->CreateMatchCriterionLong(EmptyString(),
2392 NS_LITERAL_STRING(
"media_item_id"),
2395 getter_AddRefs(criterion));
2396 NS_ENSURE_SUCCESS(rv, rv);
2398 rv = deleteb->AddCriterion(criterion);
2399 NS_ENSURE_SUCCESS(rv, rv);
2402 nsCOMPtr<sbISQLSelectBuilder> subquery =
2404 NS_ENSURE_SUCCESS(rv, rv);
2406 rv = subquery->AddColumn(EmptyString(), NS_LITERAL_STRING(
"rowid"));
2407 NS_ENSURE_SUCCESS(rv, rv);
2409 rv = subquery->SetBaseTableName(NS_LITERAL_STRING(
"simple_media_lists"));
2410 NS_ENSURE_SUCCESS(rv, rv);
2412 rv = subquery->CreateMatchCriterionParameter(EmptyString(),
2413 NS_LITERAL_STRING(
"member_media_item_id"),
2415 getter_AddRefs(criterion));
2416 NS_ENSURE_SUCCESS(rv, rv);
2418 rv = subquery->AddCriterion(criterion);
2419 NS_ENSURE_SUCCESS(rv, rv);
2421 rv = subquery->CreateMatchCriterionLong(EmptyString(),
2422 NS_LITERAL_STRING(
"media_item_id"),
2425 getter_AddRefs(criterion));
2426 NS_ENSURE_SUCCESS(rv, rv);
2428 rv = subquery->AddCriterion(criterion);
2429 NS_ENSURE_SUCCESS(rv, rv);
2431 rv = subquery->AddOrder(EmptyString(), NS_LITERAL_STRING(
"ordinal"), PR_TRUE);
2432 NS_ENSURE_SUCCESS(rv, rv);
2434 rv = subquery->SetLimit(1);
2435 NS_ENSURE_SUCCESS(rv, rv);
2437 nsCOMPtr<sbISQLBuilderCriterionIn> inCriterion;
2438 rv = deleteb->CreateMatchCriterionIn(EmptyString(),
2439 NS_LITERAL_STRING(
"rowid"),
2440 getter_AddRefs(inCriterion));
2441 NS_ENSURE_SUCCESS(rv, rv);
2443 rv = inCriterion->AddSubquery(subquery);
2444 NS_ENSURE_SUCCESS(rv, rv);
2446 rv = deleteb->AddCriterion(inCriterion);
2447 NS_ENSURE_SUCCESS(rv, rv);
2449 rv = deleteb->ToString(mDeleteFirstListItemQuery);
2450 NS_ENSURE_SUCCESS(rv, rv);
2453 rv = deleteb->Reset();
2454 NS_ENSURE_SUCCESS(rv, rv);
2456 rv = deleteb->SetTableName(NS_LITERAL_STRING(
"simple_media_lists"));
2457 NS_ENSURE_SUCCESS(rv, rv);
2459 rv = deleteb->CreateMatchCriterionLong(EmptyString(),
2460 NS_LITERAL_STRING(
"media_item_id"),
2463 getter_AddRefs(criterion));
2464 NS_ENSURE_SUCCESS(rv, rv);
2466 rv = deleteb->AddCriterion(criterion);
2467 NS_ENSURE_SUCCESS(rv, rv);
2469 rv = deleteb->CreateMatchCriterionParameter(EmptyString(),
2470 NS_LITERAL_STRING(
"ordinal"),
2472 getter_AddRefs(criterion));
2473 NS_ENSURE_SUCCESS(rv, rv);
2475 rv = deleteb->AddCriterion(criterion);
2476 NS_ENSURE_SUCCESS(rv, rv);
2478 rv = deleteb->ToString(mDeleteListItemByOrdinalQuery);
2479 NS_ENSURE_SUCCESS(rv, rv);
2482 rv = deleteb->Reset();
2483 NS_ENSURE_SUCCESS(rv, rv);
2485 rv = deleteb->SetTableName(NS_LITERAL_STRING(
"simple_media_lists"));
2486 NS_ENSURE_SUCCESS(rv, rv);
2488 rv = deleteb->CreateMatchCriterionLong(EmptyString(),
2489 NS_LITERAL_STRING(
"media_item_id"),
2492 getter_AddRefs(criterion));
2493 NS_ENSURE_SUCCESS(rv, rv);
2495 rv = deleteb->AddCriterion(criterion);
2496 NS_ENSURE_SUCCESS(rv, rv);
2498 rv = deleteb->ToString(mDeleteAllQuery);
2499 NS_ENSURE_SUCCESS(rv, rv);
2505 sbLocalDatabaseSimpleMediaList::NotifyCopyListener(
sbIMediaItem *aSourceItem,
2508 NS_ENSURE_ARG_POINTER(aSourceItem);
2509 NS_ENSURE_ARG_POINTER(aDestItem);
2512 return mCopyListener->OnItemCopied(aSourceItem, aDestItem);
2528 sbLocalDatabaseSimpleMediaList::GetInterfaces(PRUint32* count, nsIID***
array)
2534 sbLocalDatabaseSimpleMediaList::GetHelperForLanguage(PRUint32
language,
2542 sbLocalDatabaseSimpleMediaList::GetContractID(
char** aContractID)
2544 *aContractID = nsnull;
2549 sbLocalDatabaseSimpleMediaList::GetClassDescription(
char** aClassDescription)
2551 *aClassDescription = nsnull;
2556 sbLocalDatabaseSimpleMediaList::GetClassID(nsCID** aClassID)
2563 sbLocalDatabaseSimpleMediaList::GetImplementationLanguage(PRUint32* aImplementationLanguage)
2565 *aImplementationLanguage = nsIProgrammingLanguage::CPLUSPLUS;
2570 sbLocalDatabaseSimpleMediaList::GetFlags(PRUint32 *aFlags)
2572 *aFlags = nsIClassInfo::THREADSAFE;
2577 sbLocalDatabaseSimpleMediaList::GetClassIDNoAlloc(nsCID* aClassIDNoAlloc)
2579 return NS_ERROR_NOT_AVAILABLE;
#define SB_SQLBUILDER_UPDATE_CONTRACTID
#define SB_PROPERTY_ORIGINLIBRARYGUID
#define SB_SQLBUILDER_INSERT_CONTRACTID
nsresult do_GetProxyForObject(nsIEventTarget *aTarget, REFNSIID aIID, nsISupports *aObj, PRInt32 aProxyType, void **aProxyObject)
Simple class to make sure we notify listeners that a batch operation has completed every time they ar...
readonly attribute AString guid
The guid of this resource.
#define SB_SQLBUILDER_DELETE_CONTRACTID
#define SB_SQLBUILDER_SELECT_CONTRACTID
#define SB_PROPERTY_UPDATED
General interface to data resources.
PRBool sbIsMainLibrary(sbILibrary *aLibrary)
function insert(dbq, size, name)
NS_IMETHODIMP_(nsresult) sbVariant
const unsigned long JOIN_INNER
StringArrayEnumerator prototype hasMore
nsresult GetLengthCache(sbILocalDatabaseGUIDArrayLengthCache **aLengthCache)
An interface to carry around arrays of nsIProperty instances. Users of this interface should only QI ...
#define SB_PROPERTY_ORIGINITEMGUID
NS_DECL_ISUPPORTS NS_DECL_SBIADDMEDIAITEMSLISTENER sbAddSomeEnumListener(nsIArray *aItemsToAdd, nsInterfaceHashtable< nsISupportsHashKey, sbIMediaItem > &aForeignItems)
_getSelectedPageStyle s i
const unsigned long MATCH_EQUALS
Songbird Database Object Definition.