33 #include <nsISimpleEnumerator.h>
34 #include <nsServiceManagerUtils.h>
36 #include <sbILibraryManager.h>
48 static PRLogModuleInfo* gPlayQueueExternalLibraryListenerLog = nsnull;
49 #define TRACE(args) PR_LOG(gPlayQueueExternalLibraryListenerLog, PR_LOG_DEBUG, args)
50 #define LOG(args) PR_LOG(gPlayQueueExternalLibraryListenerLog, PR_LOG_WARN, args)
52 #define __FUNCTION__ __PRETTY_FUNCTION__
74 rv =
mUpdate->GetLength(&lhLength);
75 NS_ENSURE_SUCCESS(rv,
false);
77 rv = rhs.
mUpdate->GetLength(&rhLength);
78 NS_ENSURE_SUCCESS(rv,
false);
79 if (lhLength != rhLength) {
83 for (PRUint32
i = 0;
i < lhLength;
i++) {
84 nsCOMPtr<sbIProperty> lhProp;
85 rv =
mUpdate->GetPropertyAt(
i, getter_AddRefs(lhProp));
86 NS_ENSURE_SUCCESS(rv,
false);
90 lhProp->GetValue(lhValue);
92 rv = rhs.
mUpdate->GetPropertyValue(
id, rhValue);
93 NS_ENSURE_SUCCESS(rv,
false);
94 if (!lhValue.Equals(rhValue)) {
112 : mUpdateLock(nsnull)
115 if (!gPlayQueueExternalLibraryListenerLog) {
116 gPlayQueueExternalLibraryListenerLog =
117 PR_NewLogModule(
"sbPlayQueueExternalLibraryListener");
121 TRACE((
"%s[%p]", __FUNCTION__,
this));
123 mUpdateLock = nsAutoLock::NewLock(
"sbPlayQueueExternalLibraryListener::mUpdateLock");
124 NS_ASSERTION(mUpdateLock,
"failed to create lock!");
127 sbPlayQueueExternalLibraryListener::~sbPlayQueueExternalLibraryListener()
129 TRACE((
"%s[%p]", __FUNCTION__,
this));
132 nsAutoLock::DestroyLock(mUpdateLock);
140 rv = mMasterLibrary->RemoveListener(
this);
141 NS_ENSURE_SUCCESS(rv, rv);
143 PRUint32 length = mExternalLibraries.Count();
144 for (PRUint32
i = 0;
i < length;
i++) {
145 nsCOMPtr<sbILibrary> library = mExternalLibraries.ObjectAt(
i);
146 NS_ENSURE_STATE(library);
148 rv = library->RemoveListener(
this);
149 NS_ENSURE_SUCCESS(rv, rv);
158 TRACE((
"%s[%p]", __FUNCTION__,
this));
161 mMasterLibrary = aLibrary;
162 rv = mMasterLibrary->AddListener(
this,
166 NS_ENSURE_SUCCESS(rv, rv);
174 TRACE((
"%s[%p]", __FUNCTION__,
this));
177 PRBool added = mExternalLibraries.AppendObject(aLibrary);
178 NS_ENSURE_TRUE(added, NS_ERROR_FAILURE);
180 rv = aLibrary->AddListener(
this,
184 NS_ENSURE_SUCCESS(rv, rv);
194 TRACE((
"%s[%p]", __FUNCTION__,
this));
199 nsCOMPtr<sbIPropertyArray> props;
200 rv = aMediaItem->GetProperties(aProperties, getter_AddRefs(props));
201 NS_ENSURE_SUCCESS(rv, rv);
205 nsAutoLock lock(mUpdateLock);
206 mUpdates.push_back(update);
209 rv = aMediaItem->SetProperties(aProperties);
210 NS_ENSURE_SUCCESS(rv, rv);
213 nsAutoLock lock(mUpdateLock);
214 mUpdates.remove(update);
221 sbPlayQueueExternalLibraryListener::GenerateUpdates(
226 TRACE((
"%s[%p]", __FUNCTION__,
this));
229 nsCOMPtr<sbILibrary> lib;
230 rv = aMediaItem->GetLibrary(getter_AddRefs(lib));
231 NS_ENSURE_SUCCESS(rv, rv);
235 nsCOMPtr<sbIMediaItem> internalMediaItem;
236 if (lib == mMasterLibrary) {
237 internalMediaItem = aMediaItem;
239 rv = mMasterLibrary->GetDuplicate(aMediaItem,
240 getter_AddRefs(internalMediaItem));
241 NS_ENSURE_SUCCESS(rv, rv);
242 if (internalMediaItem) {
244 updates.push_back(internalUpdate);
253 for (PRInt32
i = 0;
i < mExternalLibraries.Count();
i++)
255 if (mExternalLibraries[
i] != lib) {
256 nsCOMPtr<sbIMediaItem> externalMediaItem;
257 rv = mExternalLibraries[
i]->GetDuplicate(internalMediaItem,
258 getter_AddRefs(externalMediaItem));
259 if (NS_SUCCEEDED(rv) && externalMediaItem) {
261 updates.push_back(externalUpdate);
276 sbPlayQueueExternalLibraryListener::OnBeforeListCleared(
278 PRBool aExcludeLists,
279 PRBool *aNoMoreForBatch)
281 TRACE((
"%s[%p]", __FUNCTION__,
this));
282 if (aNoMoreForBatch) {
283 *aNoMoreForBatch = PR_TRUE;
289 sbPlayQueueExternalLibraryListener::OnListCleared(
sbIMediaList* aMediaList,
290 PRBool aExcludeLists,
291 PRBool* aNoMoreForBatch)
293 TRACE((
"%s[%p]", __FUNCTION__,
this));
294 if (aNoMoreForBatch) {
295 *aNoMoreForBatch = PR_TRUE;
302 sbPlayQueueExternalLibraryListener::OnBatchBegin(
sbIMediaList* aMediaList)
304 TRACE((
"%s[%p]", __FUNCTION__,
this));
309 sbPlayQueueExternalLibraryListener::OnBatchEnd(
sbIMediaList* aMediaList)
311 TRACE((
"%s[%p]", __FUNCTION__,
this));
316 sbPlayQueueExternalLibraryListener::OnItemAdded(
sbIMediaList* aMediaList,
319 PRBool* aNoMoreForBatch)
321 TRACE((
"%s[%p]", __FUNCTION__,
this));
322 if (aNoMoreForBatch) {
323 *aNoMoreForBatch = PR_TRUE;
329 sbPlayQueueExternalLibraryListener::OnBeforeItemRemoved(
333 PRBool* aNoMoreForBatch)
335 TRACE((
"%s[%p]", __FUNCTION__,
this));
336 if (aNoMoreForBatch) {
337 *aNoMoreForBatch = PR_TRUE;
343 sbPlayQueueExternalLibraryListener::OnAfterItemRemoved(
sbIMediaList* aMediaList,
346 PRBool* aNoMoreForBatch)
348 TRACE((
"%s[%p]", __FUNCTION__,
this));
349 if (aNoMoreForBatch) {
350 *aNoMoreForBatch = PR_TRUE;
356 sbPlayQueueExternalLibraryListener::OnItemUpdated(
sbIMediaList* aMediaList,
359 PRBool* aNoMoreForBatch)
361 TRACE((
"%s[%p]", __FUNCTION__,
this));
368 nsAutoLock lock(mUpdateLock);
371 it = std::find(mUpdates.begin(), mUpdates.end(), update);
372 if (it != mUpdates.end()) {
377 rv = GenerateUpdates(aMediaItem, aProperties, updates);
378 NS_ENSURE_SUCCESS(rv, rv);
379 if (updates.size() == 0) {
384 for (it = updates.begin(); it != updates.end(); it++) {
385 mUpdates.push_back(*it);
391 nsCOMPtr<sbIPropertyArray> props;
392 rv = aMediaItem->GetProperties(aProperties, getter_AddRefs(props));
393 NS_ENSURE_SUCCESS(rv, rv);
395 for (UpdateIter it = updates.begin(); it != updates.end(); it++) {
396 rv = it->mItem->SetProperties(props);
397 NS_ENSURE_SUCCESS(rv, rv);
401 nsAutoLock lock(mUpdateLock);
403 for (UpdateIter it = updates.begin(); it != updates.end(); it++) {
404 mUpdates.remove(*it);
412 sbPlayQueueExternalLibraryListener::OnItemMoved(
sbIMediaList* aMediaList,
415 PRBool* aNoMoreForBatch)
417 TRACE((
"%s[%p]", __FUNCTION__,
this));
418 if (aNoMoreForBatch) {
419 *aNoMoreForBatch = PR_TRUE;
nsresult AddExternalLibrary(sbILibrary *aLibrary)
Add an external library to listen to.
Class to allow items in a "master" internal library to stay in sync with duplicates in other librarie...
Helper class to listen to external libraries and sync properties.
NS_IMPL_THREADSAFE_ISUPPORTS1(sbPlayQueueExternalLibraryListener, sbIMediaListListener)
nsresult SetPropertiesNoSync(sbIMediaItem *aMediaItem, sbIPropertyArray *aProperties)
Set properties on an item, but don't synchronize to duplicates in other libraries.
bool operator==(sbPropertyUpdate rhs)
NS_DECL_ISUPPORTS NS_DECL_SBIMEDIALISTLISTENER sbPlayQueueExternalLibraryListener()
Media library abstraction.
nsresult RemoveListeners()
Removes all listeners so the libraries can shut down.
nsresult SetMasterLibrary(sbILibrary *aLibrary)
Set the master library. Items that are in this library with duplicates in external libraries...
Class to hold onto property updates to make sure we're not applying them more than once...
nsCOMPtr< sbIPropertyArray > mUpdate
nsCOMPtr< sbIMediaItem > mItem
An interface to carry around arrays of nsIProperty instances. Users of this interface should only QI ...
_getSelectedPageStyle s i