sbLocalDatabaseMediaListListener.h
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
27 #ifndef __SB_LOCALDATABASE_MEDIALISTLISTENER_H__
28 #define __SB_LOCALDATABASE_MEDIALISTLISTENER_H__
29 
30 #include <nsAutoPtr.h>
31 #include <nsCOMPtr.h>
32 #include <nsCOMArray.h>
33 #include <nsTHashtable.h>
34 #include <nsHashKeys.h>
35 #include <nsTArray.h>
36 #include <prlock.h>
37 #include <sbIMediaList.h>
38 #include <sbIMediaListListener.h>
39 #include <sbIPropertyArray.h>
40 
41 class nsIProxyObjectManager;
42 class nsIWeakReference;
43 class nsIWeakReference;
44 class sbIMediaItem;
45 class sbIMediaList;
47 
49 {
51 public:
54 
55  nsresult Init(nsIProxyObjectManager *aProxyObjMgr,
56  sbIMediaListListener* aListener,
57  PRUint32 aCurrentBatchDepth,
58  PRUint32 aFlags,
59  sbIPropertyArray* aPropertyFilter);
60  nsresult Init(nsIProxyObjectManager *aProxyObjMgr,
61  nsIWeakReference* aWeakListener,
62  PRUint32 aCurrentBatchDepth,
63  PRUint32 aFlags,
64  sbIPropertyArray* aPropertyFilter);
65 
66  PRBool ShouldNotify(PRUint32 aFlag, sbIPropertyArray* aProperties = nsnull);
67 
68  void BeginBatch();
69  void EndBatch();
70  void SetShouldStopNotifying(PRUint32 aFlag);
71  void GetDebugAddress(nsAString& mDebugAddress);
72 
73 private:
74 
75  nsresult InitPropertyFilter(sbIPropertyArray* aPropertyFilter);
76 
77  PRBool mIsGone;
78  nsCOMPtr<nsISupports> mRef;
79  nsCOMPtr<nsIWeakReference> mWeak;
80  nsCOMPtr<sbIMediaListListener> mProxy;
81  PRUint32 mFlags;
82  PRBool mHasPropertyFilter;
83  nsTHashtable<nsStringHashKey> mPropertyFilter;
84  nsTArray<PRUint32> mStopNotifiyingStack;
85  nsString mDebugAddress;
86 };
87 
89 {
90 public:
92  NS_DECL_SBIMEDIALISTLISTENER
93 
94  sbWeakMediaListListenerWrapper(nsIWeakReference* aWeakListener);
96 
97 private:
98  already_AddRefed<sbIMediaListListener> GetListener();
99 
100  nsCOMPtr<nsIWeakReference> mWrappedWeak;
101 };
102 
104 {
105  friend class sbAutoBatchHelper;
106 
107  struct ListenerAndDebugAddress {
108  ListenerAndDebugAddress(sbIMediaListListener* aListener,
109  const nsAString& aDebugAddress) :
110  listener(aListener),
111  debugAddress(aDebugAddress)
112  {}
113 
114  nsCOMPtr<sbIMediaListListener> listener;
115  nsString debugAddress;
116  };
117 
118  // This is used to mark things to stop notifying for the sweep
119  struct StopNotifyFlags {
120  StopNotifyFlags(sbIMediaListListener* aListener,
121  PRUint32 aListenerFlags,
122  PRBool aIsGone) :
123  listener(aListener),
124  listenerFlags(aListenerFlags),
125  isGone(aIsGone)
126  {}
127  nsCOMPtr<sbIMediaListListener> listener;
128  PRUint32 listenerFlags;
129  PRBool isGone;
130  };
131 
132  typedef nsTArray<ListenerAndDebugAddress> sbMediaListListenersArray;
133  typedef nsTArray<StopNotifyFlags> sbStopNotifyArray;
134  typedef nsAutoPtr<sbListenerInfo> sbListenerInfoAutoPtr;
135 
136 public:
139 
140 protected:
141  // Initialize the instance.
142  nsresult Init();
143 
144  // Add a listener to the array
146  sbIMediaListListener* aListener,
147  PRBool aOwnsWeak = PR_FALSE,
148  PRUint32 aFlags = sbIMediaList::LISTENER_FLAGS_ALL,
149  sbIPropertyArray* aPropertyFilter = nsnull);
150 
151  // Remove a listener from the array
153  sbIMediaListListener* aListener);
154 
155  // Return the number of listeners
156  PRUint32 ListenerCount();
157 
158  // Enumerate listeners and call OnItemAdded
160  sbIMediaItem* aItem,
161  PRUint32 aIndex);
162 
163  // Enumerate listeners and call OnBeforeItemRemoved
165  sbIMediaItem* aItem,
166  PRUint32 aIndex);
167 
168  // Enumerate listeners and call OnAfterItemRemoved
170  sbIMediaItem* aItem,
171  PRUint32 aIndex);
172 
173  // Enumerate listeners and call OnItemUpdated
175  sbIMediaItem* aItem,
176  sbIPropertyArray* aProperties);
177 
178  // Enumerate listeners and call OnItemMoved
180  PRUint32 aFromIndex,
181  PRUint32 aToIndex);
182 
183  // Enumerate listeners and call OnBeforeListCleared
185  PRBool aExcludeLists);
186 
187  // Enumerate listeners and call OnListCleared
189  PRBool aExcludeLists);
190 
191  // Enumerate listeners and call OnBatchBegin
193 
194  // Enumerate listeners and call OnBatchEnd
196 
197 private:
198 
199  nsresult SnapshotListenerArray(sbMediaListListenersArray& aArray,
200  PRUint32 aFlags,
201  sbIPropertyArray* aPropertyFilter = nsnull);
202  void SweepListenerArray(sbStopNotifyArray& aStopNotifying);
203 
204  nsTArray<sbListenerInfoAutoPtr> mListenerArray;
205 
206  PRLock* mListenerArrayLock;
207 
208  PRUint32 mBatchDepth;
209 };
210 
211 #endif /* __SB_LOCALDATABASE_MEDIALISTLISTENER_H__ */
212 
void NotifyListenersAfterItemRemoved(sbIMediaList *aList, sbIMediaItem *aItem, PRUint32 aIndex)
Notifies all listeners that an item has been removed from the list.
void NotifyListenersBeforeListCleared(sbIMediaList *aList, PRBool aExcludeLists)
Notifies all listeners before the list has been cleared.
void NotifyListenersItemAdded(sbIMediaList *aList, sbIMediaItem *aItem, PRUint32 aIndex)
Notifies all listeners that an item has been added to the list.
A brief description of the contents of this interface.
nsresult RemoveListener(sbLocalDatabaseMediaListBase *aList, sbIMediaListListener *aListener)
Simple class to make sure we notify listeners that a batch operation has completed every time they ar...
void NotifyListenersItemUpdated(sbIMediaList *aList, sbIMediaItem *aItem, sbIPropertyArray *aProperties)
Notifies all listeners that an item has been updated.
nsresult AddListener(sbLocalDatabaseMediaListBase *aList, sbIMediaListListener *aListener, PRBool aOwnsWeak=PR_FALSE, PRUint32 aFlags=sbIMediaList::LISTENER_FLAGS_ALL, sbIPropertyArray *aPropertyFilter=nsnull)
Interface used to listen to changes to a media list.
NS_DECL_ISUPPORTS NS_DECL_SBIMEDIALISTLISTENER sbWeakMediaListListenerWrapper(nsIWeakReference *aWeakListener)
void GetDebugAddress(nsAString &mDebugAddress)
void NotifyListenersBeforeItemRemoved(sbIMediaList *aList, sbIMediaItem *aItem, PRUint32 aIndex)
Notifies all listeners that an item is about to be removed from the list.
void SetShouldStopNotifying(PRUint32 aFlag)
PRBool ShouldNotify(PRUint32 aFlag, sbIPropertyArray *aProperties=nsnull)
void NotifyListenersListCleared(sbIMediaList *aList, PRBool aExcludeLists)
Notifies all listeners that the list has been cleared.
void NotifyListenersBatchEnd(sbIMediaList *aList)
Notifies all listeners that multiple items have been changed.
void NotifyListenersBatchBegin(sbIMediaList *aList)
Notifies all listeners that multiple items are about to be changed.
const unsigned long LISTENER_FLAGS_ALL
Interface that defines a single item of media in the system.
void NotifyListenersItemMoved(sbIMediaList *aList, PRUint32 aFromIndex, PRUint32 aToIndex)
Notifies all listeners that an item has been moved.
An interface to carry around arrays of nsIProperty instances. Users of this interface should only QI ...
nsresult Init(nsIProxyObjectManager *aProxyObjMgr, sbIMediaListListener *aListener, PRUint32 aCurrentBatchDepth, PRUint32 aFlags, sbIPropertyArray *aPropertyFilter)