sbLocalDatabaseMediaListBase.h
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2010 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
25 #ifndef __SBLOCALDATABASEMEDIALISTBASE_H__
26 #define __SBLOCALDATABASEMEDIALISTBASE_H__
27 
30 #include <nsIClassInfo.h>
31 #include <nsIStringEnumerator.h>
32 #include <sbILocalDatabaseGUIDArray.h>
33 #include <sbIMediaList.h>
34 
35 #include <nsClassHashtable.h>
36 #include <nsCOMPtr.h>
37 #include <nsStringGlue.h>
38 #include <nsTArray.h>
39 #include <nsTHashtable.h>
40 #include <prmon.h>
41 #include <prlock.h>
42 
43 // Macros to help early returns within loops.
44 #define SB_CONTINUE_IF_FALSE(_expr) \
45  PR_BEGIN_MACRO \
46  if (!(_expr)) { \
47  NS_WARNING("SB_CONTINUE_IF_FALSE triggered"); \
48  continue; \
49  } \
50  PR_END_MACRO
51 
52 #define SB_CONTINUE_IF_FAILED(_rv) \
53  SB_CONTINUE_IF_FALSE(NS_SUCCEEDED(_rv))
54 
55 // This macro is for derived classes that want to ensure safe access to
56 // mFullArray.
57 #define SB_MEDIALIST_LOCK_FULLARRAY_AND_ENSURE_MUTABLE() \
58  PR_BEGIN_MACRO \
59  nsAutoMonitor mon(mFullArrayMonitor); \
60  if (mLockedEnumerationActive) { \
61  NS_ERROR("Operation not permitted during a locked enumeration"); \
62  return NS_ERROR_FAILURE; \
63  } \
64  PR_END_MACRO
65 
66 #define SB_ASYNC_NOTIFICATION_ITEMS 50
67 
68 class nsIArray;
69 class nsIMutableArray;
71 class nsStringHashKey;
73 class sbIDatabaseQuery;
76 class sbIMediaItem;
78 
81  public sbIMediaList
82 {
83  friend class sbAutoBatchHelper;
84 
85  typedef nsTArray<nsString> sbStringArray;
86  typedef nsClassHashtable<nsStringHashKey, sbStringArray> sbStringArrayHash;
87 
88 public:
89  NS_DECL_ISUPPORTS_INHERITED
90 
91  NS_FORWARD_SBILOCALDATABASEMEDIAITEM(sbLocalDatabaseMediaItem::)
92  NS_FORWARD_SBILIBRARYRESOURCE(sbLocalDatabaseMediaItem::)
93  NS_FORWARD_SBIMEDIAITEM(sbLocalDatabaseMediaItem::)
94 
95  NS_DECL_SBIMEDIALIST
96 
98  virtual ~sbLocalDatabaseMediaListBase();
99 
100  nsresult Init(sbLocalDatabaseLibrary* aLibrary,
101  const nsAString& aGuid,
102  PRBool aOwnsLibrary = PR_TRUE);
103 
104  already_AddRefed<sbLocalDatabaseLibrary> GetNativeLibrary();
105 
107  NS_ASSERTION(mFullArray, "mArray is null!");
108  return mFullArray;
109  }
110 
111  nsresult AddListener(sbIMediaListListener* aListener,
112  PRBool aOwnsWeak = PR_FALSE,
113  PRUint32 aFlags = 0) {
114  return AddListener(aListener, aOwnsWeak, aFlags, nsnull);
115  }
116 
117  void SetCachedListContentType(PRUint16 aContentType) {
118  mListContentType = aContentType;
119  }
120 
121  // These aren't meant to be called directly. Use sbAutoBatchHelper
122  // to avoid the risk of leaving a batch in progress
125  }
126  void EndUpdateBatch() {
128  }
129 
130 protected:
131  NS_IMETHOD GetDefaultSortProperty(nsAString& aProperty) = 0;
132 
133  nsresult MakeStandardQuery(sbIDatabaseQuery** _retval);
134 
136  sbIPropertyArray** _retval);
137 
138  // Set SB_PROPERTY_ORIGINLIBRARYGUID and SB_PROPERTY_ORIGINITEMGUID, and
139  // set SB_PROPERTY_ORIGIN_IS_IN_MAIN_LIBRARY to true in the given
140  // property array if aSourceItem is in the main library. Otherwise, set
141  // these properties only if they are not already set, and remove
142  // SB_PROPERTY_ORIGIN_IS_IN_MAIN_LIBRARY if it is false. aProperties
143  // should be properties for a new item that is being created by copying
144  // aSourceItem.
145  nsresult GetOriginProperties(sbIMediaItem * aSourceItem,
146  sbIMutablePropertyArray * aProperties);
147 
148 private:
149 
150  // Remove any values for the aProperty from aPropertyArray
151  static nsresult RemoveProperty(sbIMutablePropertyArray * aPropertyArray,
152  const nsAString & aProperty);
153 
154  // This callback is meant to be used with an sbStringArrayHash.
155  // aUserData should be a sbILocalDatabaseGUIDArray pointer.
156  static PLDHashOperator PR_CALLBACK
157  AddFilterToGUIDArrayCallback(nsStringHashKey::KeyType aKey,
158  sbStringArray* aEntry,
159  void* aUserData);
160 
161  nsresult EnumerateAllItemsInternal(sbIMediaListEnumerationListener* aEnumerationListener);
162 
163  nsresult EnumerateItemsByPropertyInternal(const nsAString& aID,
164  nsIStringEnumerator* aValueEnum,
165  sbIMediaListEnumerationListener* aEnumerationListener);
169  nsresult EnumerateItemsByPropertiesInternal(sbStringArrayHash* aPropertiesHash,
170  sbIMediaListEnumerationListener* aEnumerationListener);
171 
172  // Called for the enumeration methods.
173  nsresult EnumerateItemsInternal(sbGUIDArrayEnumerator* aEnumerator,
175 
176 protected:
177  void SetArray(sbILocalDatabaseGUIDArray * aArray);
178  // A monitor for changes to the media list.
179  PRMonitor* mFullArrayMonitor;
180 
181  // Cached list content type
183 
185 
186  // The mFilteredProperties hash table caches the property ids
187  // that we always want to filter out of the property arrays that
188  // are used to create media items or set multiple properties
189  // on a library resource.
190  nsTHashtable<nsStringHashKey> mFilteredProperties;
191 
192 private:
193  // The mFullArray is a cached version of the full contents of the media
194  // list this instance represents.
195  nsCOMPtr<sbILocalDatabaseGUIDArray> mFullArray;
196 };
197 
206 {
207 public:
209  : mList(aList)
210  {
211  mList.BeginUpdateBatch();
212  }
213 
215  {
216  mList.EndUpdateBatch();
217  }
218 
219 private:
220  // Not meant to be implemented. This makes it a compiler error to
221  // attempt to create an object on the heap.
222  static void* operator new(size_t /*size*/) CPP_THROW_NEW;
223  static void operator delete(void* /*memory*/);
224 
226 };
227 
229 {
230 public:
232  NS_DECL_NSISTRINGENUMERATOR
233 
235 
237 
238 private:
239  nsCOMPtr<sbILocalDatabaseGUIDArray> mArray;
240  PRUint32 mLength;
241  PRUint32 mNextIndex;
242 };
243 
244 #endif /* __SBLOCALDATABASEMEDIALISTBASE_H__ */
nsresult GetOriginProperties(sbIMediaItem *aSourceItem, sbIMutablePropertyArray *aProperties)
NS_DECL_ISUPPORTS NS_DECL_NSISTRINGENUMERATOR sbGUIDArrayValueEnumerator(sbILocalDatabaseGUIDArray *aArray)
Interface used to enumerate the items in a media list.
nsTArray< nsString > sbStringArray
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
nsresult AddListener(sbIMediaListListener *aListener, PRBool aOwnsWeak=PR_FALSE, PRUint32 aFlags=0)
void SetArray(sbILocalDatabaseGUIDArray *aArray)
A brief description of the contents of this interface.
Simple class to make sure we notify listeners that a batch operation has completed every time they ar...
Interface used to listen to changes to a media list.
An interface to carry around arrays of nsIProperty instances Note that implementations of the interfa...
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
already_AddRefed< sbLocalDatabaseLibrary > GetNativeLibrary()
sbILocalDatabaseGUIDArray * GetArray()
void SetCachedListContentType(PRUint16 aContentType)
nsresult MakeStandardQuery(sbIDatabaseQuery **_retval)
An object responsible for executing SQL queries on the database.
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.
Interface that defines a single item of media in the system.
An interface to carry around arrays of nsIProperty instances. Users of this interface should only QI ...
sbAutoBatchHelper(sbLocalDatabaseMediaListBase &aList)
nsTHashtable< nsStringHashKey > mFilteredProperties
NS_IMETHOD GetDefaultSortProperty(nsAString &aProperty)=0
nsresult GetFilteredPropertiesForNewItem(sbIPropertyArray *aProperties, sbIPropertyArray **_retval)