27 #ifndef __SBLOCALDATABASELIBRARY_H__
28 #define __SBLOCALDATABASELIBRARY_H__
30 #include <nsIObserver.h>
31 #include <sbIDatabaseQuery.h>
32 #include <sbILibrary.h>
33 #include <sbILocalDatabaseLibrary.h>
34 #include <sbILocalDatabaseSimpleMediaList.h>
39 #include <nsAutoLock.h>
40 #include <nsClassHashtable.h>
41 #include <nsDataHashtable.h>
42 #include <nsCOMArray.h>
44 #include <nsIClassInfo.h>
45 #include <nsInterfaceHashtable.h>
46 #include <nsIRunnable.h>
47 #include <nsIStreamListener.h>
49 #include <nsIThread.h>
50 #include <nsIThreadPool.h>
52 #include <nsStringGlue.h>
53 #include <nsVoidArray.h>
54 #include <sbIMediaListFactory.h>
55 #include <sbILibraryStatistics.h>
57 class nsIPropertyBag2;
58 class nsIWeakReference;
59 class nsStringHashKey;
74 typedef nsClassHashtable<nsISupportsHashKey, sbMediaItemArray>
77 typedef nsInterfaceHashtableMT<nsStringHashKey, nsIWeakReference>
82 #define SB_DECL_MEDIALISTBASE_OVERRIDES \
83 NS_IMETHOD GetType(nsAString& aType); \
84 NS_IMETHOD GetItemByGuid(const nsAString& aGuid, sbIMediaItem** _retval); \
85 NS_IMETHOD Contains(sbIMediaItem* aMediaItem, PRBool* _retval); \
86 NS_IMETHOD Add(sbIMediaItem* aMediaItem); \
87 NS_IMETHOD AddItem(sbIMediaItem* aMediaItem, sbIMediaItem ** aNewMediaItem); \
88 NS_IMETHOD AddAll(sbIMediaList* aMediaList); \
89 NS_IMETHOD AddSome(nsISimpleEnumerator* aMediaItems); \
90 NS_IMETHOD AddMediaItems(nsISimpleEnumerator *aMediaItems, \
91 sbIAddMediaItemsListener *aListener, \
93 NS_IMETHOD Remove(sbIMediaItem* aMediaItem); \
94 NS_IMETHOD RemoveByIndex(PRUint32 aIndex); \
95 NS_IMETHOD RemoveSome(nsISimpleEnumerator* aMediaItems); \
97 NS_IMETHOD CreateView(sbIMediaListViewState* aState, sbIMediaListView** _retval); \
100 #define SB_DECL_SBIMEDIAITEM_OVERRIDES \
101 NS_IMETHOD SetContentSrc(nsIURI* aContentSrc); \
104 #define SB_FORWARD_SBIMEDIAITEM(_to) \
105 NS_IMETHOD GetLibrary(sbILibrary * *aLibrary) { return _to GetLibrary(aLibrary); } \
106 NS_IMETHOD GetIsMutable(PRBool *aIsMutable) { return _to GetIsMutable(aIsMutable); } \
107 NS_IMETHOD GetItemController(sbIMediaItemController **aMediaItemController) { return _to GetItemController(aMediaItemController); } \
108 NS_IMETHOD GetMediaCreated(PRInt64 *aMediaCreated) { return _to GetMediaCreated(aMediaCreated); } \
109 NS_IMETHOD SetMediaCreated(PRInt64 aMediaCreated) { return _to SetMediaCreated(aMediaCreated); } \
110 NS_IMETHOD GetMediaUpdated(PRInt64 *aMediaUpdated) { return _to GetMediaUpdated(aMediaUpdated); } \
111 NS_IMETHOD SetMediaUpdated(PRInt64 aMediaUpdated) { return _to SetMediaUpdated(aMediaUpdated); } \
112 NS_IMETHOD GetContentSrc(nsIURI **aContentSrc) { return _to GetContentSrc(aContentSrc); } \
113 NS_IMETHOD GetContentLength(PRInt64 *aContentLength) { return _to GetContentLength(aContentLength); } \
114 NS_IMETHOD SetContentLength(PRInt64 aContentLength) { return _to SetContentLength(aContentLength); } \
115 NS_IMETHOD GetContentType(nsAString & aContentType) { return _to GetContentType(aContentType); } \
116 NS_IMETHOD SetContentType(const nsAString & aContentType) { return _to SetContentType(aContentType); } \
117 NS_IMETHOD TestIsURIAvailable(nsIObserver *aObserver) { return _to TestIsURIAvailable(aObserver); } \
118 NS_IMETHOD OpenInputStreamAsync(nsIStreamListener *aListener, nsISupports *aContext, nsIChannel **_retval) { return _to OpenInputStreamAsync(aListener, aContext, _retval); } \
119 NS_IMETHOD OpenInputStream(nsIInputStream **_retval) { return _to OpenInputStream(_retval); } \
120 NS_IMETHOD OpenOutputStream(nsIOutputStream **_retval) { return _to OpenOutputStream(_retval); } \
121 NS_IMETHOD ToString(nsAString & _retval) { return _to ToString(_retval); }
123 #define SB_FORWARD_SBIMEDIALIST(_to) \
124 NS_IMETHOD GetName(nsAString & aName) { return _to GetName(aName); } \
125 NS_IMETHOD SetName(const nsAString & aName) { return _to SetName(aName); } \
126 NS_IMETHOD GetLength(PRUint32 *aLength) { return _to GetLength(aLength); } \
127 NS_IMETHOD GetIsEmpty(PRBool *aIsEmpty) { return _to GetIsEmpty(aIsEmpty); } \
128 NS_IMETHOD GetUserEditableContent(PRBool *aUserEditableContent) { return _to GetUserEditableContent(aUserEditableContent); } \
129 NS_IMETHOD GetItemByIndex(PRUint32 aIndex, sbIMediaItem **_retval) { return _to GetItemByIndex(aIndex, _retval); } \
130 NS_IMETHOD GetListContentType(PRUint16 *_retval) { return _to GetListContentType(_retval); } \
131 NS_IMETHOD EnumerateAllItems(sbIMediaListEnumerationListener *aEnumerationListener, PRUint16 aEnumerationType) { return _to EnumerateAllItems(aEnumerationListener, aEnumerationType); } \
132 NS_IMETHOD EnumerateItemsByProperty(const nsAString & aPropertyID, const nsAString & aPropertyValue, sbIMediaListEnumerationListener *aEnumerationListener, PRUint16 aEnumerationType) { return _to EnumerateItemsByProperty(aPropertyID, aPropertyValue, aEnumerationListener, aEnumerationType); } \
133 NS_IMETHOD EnumerateItemsByProperties(sbIPropertyArray *aProperties, sbIMediaListEnumerationListener *aEnumerationListener, PRUint16 aEnumerationType) { return _to EnumerateItemsByProperties(aProperties, aEnumerationListener, aEnumerationType); } \
134 NS_IMETHOD GetItemsByProperty(const nsAString & aPropertyID, const nsAString & aPropertyValue, nsIArray **_retval) { return _to GetItemsByProperty(aPropertyID, aPropertyValue, _retval); } \
135 NS_IMETHOD GetItemCountByProperty(const nsAString & aPropertyID, const nsAString & aPropertyValue, PRUint32 *_retval) { return _to GetItemCountByProperty(aPropertyID, aPropertyValue, _retval); } \
136 NS_IMETHOD GetItemsByProperties(sbIPropertyArray *aProperties, nsIArray **_retval) { return _to GetItemsByProperties(aProperties, _retval); } \
137 NS_IMETHOD IndexOf(sbIMediaItem *aMediaItem, PRUint32 aStartFrom, PRUint32 *_retval) { return _to IndexOf(aMediaItem, aStartFrom, _retval); } \
138 NS_IMETHOD LastIndexOf(sbIMediaItem *aMediaItem, PRUint32 aStartFrom, PRUint32 *_retval) { return _to LastIndexOf(aMediaItem, aStartFrom, _retval); } \
139 NS_IMETHOD AddListener(sbIMediaListListener *aListener, PRBool aOwnsWeak, PRUint32 aFlags, sbIPropertyArray *aPropertyFilter) { return _to AddListener(aListener, aOwnsWeak, aFlags, aPropertyFilter); } \
140 NS_IMETHOD RemoveListener(sbIMediaListListener *aListener) { return _to RemoveListener(aListener); } \
141 NS_IMETHOD RunInBatchMode(sbIMediaListBatchCallback *aCallback, nsISupports *aUserData) { return _to RunInBatchMode(aCallback, aUserData); } \
142 NS_IMETHOD GetDistinctValuesForProperty(const nsAString & aPropertyID, nsIStringEnumerator **_retval) { return _to GetDistinctValuesForProperty(aPropertyID, _retval); }
156 struct sbMediaListFactoryInfo {
157 sbMediaListFactoryInfo()
167 nsCOMPtr<sbIMediaListFactory>
factory;
170 struct sbMediaItemInfo {
171 sbMediaItemInfo(PRPackedBool aHasListType = PR_FALSE,
172 PRPackedBool aHasAudioType = PR_FALSE,
173 PRPackedBool aHasVideoType = PR_FALSE)
176 hasListType(aHasListType),
177 hasAudioType(aHasAudioType),
178 hasVideoType(aHasVideoType) {
183 nsCOMPtr<nsIWeakReference> weakRef;
184 PRPackedBool hasItemID;
185 PRPackedBool hasListType;
186 PRPackedBool hasAudioType;
187 PRPackedBool hasVideoType;
190 struct sbMediaItemPair {
193 : sourceItem(aSource)
194 , destinationItem(aDestination)
197 nsCOMPtr<sbIMediaItem> sourceItem;
198 nsCOMPtr<sbIMediaItem> destinationItem;
201 struct sbMediaItemUpdatedInfo {
206 , newProperties(aProperties)
207 , mediaListTable(aMediaListTable)
210 nsCOMPtr<sbIMediaItem> item;
211 nsCOMPtr<sbIPropertyArray> newProperties;
215 typedef nsClassHashtable<nsStringHashKey, sbMediaListFactoryInfo>
216 sbMediaListFactoryInfoTable;
218 typedef nsClassHashtable<nsStringHashKey, sbMediaItemInfo>
219 sbMediaItemInfoTable;
222 NS_DECL_ISUPPORTS_INHERITED
224 NS_DECL_SBILOCALDATABASELIBRARY
227 NS_DECL_SBILIBRARYSTATISTICS
238 ~sbLocalDatabaseLibrary();
242 nsresult
Init(const nsAString& aDatabaseGuid,
243 nsIPropertyBag2* aCreationParameters,
245 nsIURI* aDatabaseLocation = nsnull);
265 nsresult CreateQueries();
268 PRBool aRunAsync = PR_FALSE);
271 const PRUint32 aMediaItemTypeID,
272 const nsAString& aURISpecOrPrefix,
281 sbMediaItemInfo* aItemInfo);
283 nsresult GetTypeForGUID(const nsAString& aGUID,
288 static PLDHashOperator PR_CALLBACK
289 AddTypesToArrayCallback(nsStringHashKey::KeyType aKey,
290 sbMediaListFactoryInfo* aEntry,
293 static PLDHashOperator PR_CALLBACK
294 NotifyCopyListeners(nsISupportsHashKey::KeyType aKey,
298 static PLDHashOperator PR_CALLBACK
299 NotifyListItemUpdated(nsStringHashKey::KeyType aKey,
300 nsCOMPtr<nsIWeakReference>& aEntry,
303 static PLDHashOperator PR_CALLBACK
304 NotifyListsBeforeItemRemoved(nsISupportsHashKey::KeyType aKey,
308 static PLDHashOperator PR_CALLBACK
309 NotifyListsAfterItemRemoved(nsISupportsHashKey::KeyType aKey,
318 static PLDHashOperator PR_CALLBACK
319 NotifyListsBeforeAfterItemRemoved(nsISupportsHashKey::KeyType aKey,
323 static PLDHashOperator PR_CALLBACK
324 EntriesToMediaListArray(nsISupportsHashKey* aEntry,
327 static PLDHashOperator PR_CALLBACK
328 RemoveIfNotList(nsStringHashKey::KeyType aKey,
329 nsAutoPtr<sbMediaItemInfo> &aEntry,
332 nsresult RegisterDefaultMediaListFactories();
334 nsresult DeleteDatabaseItem(const nsAString& aGuid);
336 nsresult AddItemToLocalDatabase(
sbIMediaItem* aMediaItem,
339 nsresult GetSimpleMediaListCopyProperties
347 nsresult GetAllListsByType(const nsAString& aType,
sbMediaListArray* aArray);
349 nsresult ConvertURIsToStrings(nsIArray* aURIs, nsStringArray** aStringArray);
352 PRBool* aContainsCopy);
354 nsresult FilterExistingItems(nsStringArray* aURIs,
355 nsIArray* aPropertyArrayArray,
356 nsTArray<PRUint32>* aFilteredIndexArray,
357 nsStringArray** aFilteredURIs,
358 nsIArray** aFilteredPropertyArrayArray);
360 nsresult GetGuidFromContentURI(nsIURI* aURI, nsAString& aGUID);
366 nsresult CreateMediaItemInternal(nsIURI* aUri,
368 PRBool aAllowDuplicates,
372 nsresult BatchCreateMediaItemsInternal(nsIArray* aURIArray,
373 nsIArray* aPropertyArrayArray,
374 PRBool aAllowDuplicates,
375 nsIArray** aMediaItemCreatedArray,
379 nsresult ClearInternal(PRBool aExcludeLists = PR_FALSE,
380 const nsAString &aContentType = EmptyString());
383 nsresult NeedsMigration(PRBool *aNeedsMigration,
384 PRUint32 *aFromVersion,
385 PRUint32 *aToVersion);
387 nsresult MigrateLibrary(PRUint32 aFromVersion, PRUint32 aToVersion);
389 nsresult NeedsReindexCollations(PRBool *aNeedsReindexCollations);
391 nsresult ReindexCollations();
398 nsString mDatabaseGuid;
402 nsCOMPtr<nsIURI> mDatabaseLocation;
405 nsCOMPtr<nsIURI> mContentSrc;
416 sbMediaListFactoryInfoTable mMediaListFactoryTable;
417 sbMediaItemInfoTable mMediaItemTable;
424 nsCOMArray<nsITimer> mBatchCreateTimers;
426 nsCOMPtr<nsIPropertyBag2> mCreationParameters;
429 PRUint32 mAnalyzeCountLimit;
431 PRBool mPreventAddedNotification;
437 nsInterfaceHashtableMT<nsISupportsHashKey,
441 nsresult InitializeLibraryStatistics();
446 nsresult FindMusicFolderURI(nsIURI ** aMusicFolderURI);
459 NS_DECL_SBIMEDIALISTENUMERATIONLISTENER
462 sbLocalDatabaseLibrary* aLibrary,
464 : mFriendLibrary(aLibrary),
465 mShouldInvalidate(PR_FALSE),
469 NS_ASSERTION(mFriendLibrary,
"Null pointer!");
473 sbLocalDatabaseLibrary* mFriendLibrary;
474 PRBool mShouldInvalidate;
485 nsCOMPtr<sbIAddMediaItemsListener> mListener;
495 NS_DECL_SBIMEDIALISTENUMERATIONLISTENER
498 : mFriendLibrary(aLibrary),
499 mItemEnumerated(PR_FALSE)
501 NS_ASSERTION(mFriendLibrary,
"Null pointer!");
506 nsCOMPtr<sbIDatabaseQuery> mDBQuery;
507 nsCOMArray<sbIMediaItem> mNotificationList;
508 nsTArray<PRUint32> mNotificationIndexes;
509 PRPackedBool mItemEnumerated;
521 NS_DECL_NSITIMERCALLBACK
529 nsresult SetQueryCount(PRUint32 aQueryCount);
531 nsresult NotifyInternal(PRBool* _retval);
537 nsCOMPtr<sbIBatchCreateMediaItemsListener> mListener;
538 nsRefPtr<sbBatchCreateHelper> mBatchHelper;
539 nsCOMPtr<sbIDatabaseQuery> mQuery;
541 PRUint32 mQueryCount;
548 NS_IMETHOD_(nsrefcnt) AddRef(
void);
549 NS_IMETHOD_(nsrefcnt) Release(
void);
555 nsStringArray* aURIArray,
556 nsIArray* aPropertyArrayArray);
558 nsresult NotifyAndGetItems(nsIArray** _retval);
569 nsAutoPtr<nsStringArray> mURIArray;
570 nsCOMPtr<nsIArray> mPropertiesArray;
571 nsTArray<nsString> mGuids;
581 NS_ASSERTION(aLists,
"Null pointer!");
582 for (PRInt32
i = 0;
i < mLists->Count();
i++) {
583 nsCOMPtr<sbILocalDatabaseSimpleMediaList> simple =
584 do_QueryInterface(mLists->ObjectAt(
i));
586 simple->NotifyListenersBatchBegin(mLists->ObjectAt(
i));
592 for (PRInt32
i = 0;
i < mLists->Count();
i++) {
593 nsCOMPtr<sbILocalDatabaseSimpleMediaList> simple =
594 do_QueryInterface(mLists->ObjectAt(
i));
596 simple->NotifyListenersBatchEnd(mLists->ObjectAt(
i));
603 static void*
operator new(
size_t ) CPP_THROW_NEW {
return 0;}
604 static void operator delete(
void* ) { }
nsDataHashtable< nsStringHashKey, PRUint32 > sbListItemIndexMap
#define SB_FORWARD_SBIMEDIAITEM(_to)
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
NS_DECL_ISUPPORTS NS_DECL_SBIMEDIALISTENUMERATIONLISTENER sbLibraryInsertingEnumerationListener(sbLocalDatabaseLibrary *aLibrary, sbIAddMediaItemsListener *aListener=nsnull)
nsresult RemoveSelected(nsISimpleEnumerator *aSelection, sbLocalDatabaseMediaListView *aView)
Bulk removes the selected items specified in aSelection from the view aView.
#define SB_DECL_SBIMEDIAITEM_OVERRIDES
nsClassHashtable< nsISupportsHashKey, sbMediaItemArray > sbMediaItemToListsMap
nsInterfaceHashtableMT< nsStringHashKey, nsIWeakReference > sbGUIDToListMap
Extract statistics from a media library.
NS_IMETHOD GetDefaultSortProperty(nsAString &aProperty)
Factory for new library instances.
#define SB_FORWARD_SBIMEDIALIST(_to)
Interface used to listen for items being copied from a library.
Simple class to make sure we notify listeners that a batch operation has completed every time they ar...
friend class sbBatchCreateHelper
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
NS_DECL_ISUPPORTS NS_DECL_SBIMEDIALISTENUMERATIONLISTENER sbLibraryRemovingEnumerationListener(sbLocalDatabaseLibrary *aLibrary)
friend class sbBatchCreateTimerCallback
Media library abstraction.
nsresult AddSomeAsyncInternal(nsISimpleEnumerator *aMediaItems, sbIAddMediaItemsListener *aListener)
nsresult GetLengthCache(sbILocalDatabaseGUIDArrayLengthCache **aLengthCache)
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
An object responsible for executing SQL queries on the database.
nsCOMArray< sbIMediaList > sbMediaListArray
nsCOMArray< sbIMediaItem > sbMediaItemArray
#define SB_DECL_MEDIALISTBASE_OVERRIDES
static void GetNowString(nsAString &_retval)
Make a string of the current time in milliseconds.
An interface to carry around arrays of nsIProperty instances. Users of this interface should only QI ...
_getSelectedPageStyle s i
readonly attribute sbILibraryFactory factory
Holds the factory that created this library.
A prepared database statement.