sbLocalDatabaseSmartMediaList.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 __SBLOCALDATABASESMARTMEDIALIST_H__
26 #define __SBLOCALDATABASESMARTMEDIALIST_H__
27 
28 #include <sbILocalDatabaseSmartMediaList.h>
29 #include <sbILocalDatabaseMediaItem.h>
30 
31 #include <nsTArray.h>
32 #include <nsAutoPtr.h>
33 #include <nsCOMPtr.h>
34 #include <nsCOMArray.h>
35 #include <nsDataHashtable.h>
36 #include <nsStringGlue.h>
37 #include <nsAutoLock.h>
38 
39 #include <nsIClassInfo.h>
40 #include <nsIObserver.h>
41 #include <nsIArray.h>
42 #include <sbIMediaListListener.h>
43 #include <sbIPropertyArray.h>
44 
45 #include <sbWeakReference.h>
46 
47 class sbIDatabaseQuery;
50 class sbIMediaItem;
51 class sbIMediaList;
52 class sbIPropertyInfo;
53 class sbIPropertyManager;
56 
57 typedef nsDataHashtable<nsStringHashKey, nsString> sbStringMap;
58 
59 /* Use this macro to declare functions that forward the behavior of this interface to another object in a safe way. */
60 #define NS_FORWARD_SAFE_SBIMEDIALIST_ALL_BUT_TYPEANDNAME(_to) \
61  NS_IMETHOD GetLength(PRUint32 *aLength) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetLength(aLength); } \
62  NS_IMETHOD GetItemByGuid(const nsAString & aGuid, sbIMediaItem **_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetItemByGuid(aGuid, _retval); } \
63  NS_IMETHOD GetItemByIndex(PRUint32 aIndex, sbIMediaItem **_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetItemByIndex(aIndex, _retval); } \
64  NS_IMETHOD EnumerateAllItems(sbIMediaListEnumerationListener *aEnumerationListener, PRUint16 aEnumerationType) { return !_to ? NS_ERROR_NULL_POINTER : _to->EnumerateAllItems(aEnumerationListener, aEnumerationType); } \
65  NS_IMETHOD EnumerateItemsByProperty(const nsAString & aPropertyID, const nsAString & aPropertyValue, sbIMediaListEnumerationListener *aEnumerationListener, PRUint16 aEnumerationType) { return !_to ? NS_ERROR_NULL_POINTER : _to->EnumerateItemsByProperty(aPropertyID, aPropertyValue, aEnumerationListener, aEnumerationType); } \
66  NS_IMETHOD EnumerateItemsByProperties(sbIPropertyArray *aProperties, sbIMediaListEnumerationListener *aEnumerationListener, PRUint16 aEnumerationType) { return !_to ? NS_ERROR_NULL_POINTER : _to->EnumerateItemsByProperties(aProperties, aEnumerationListener, aEnumerationType); } \
67  NS_IMETHOD GetItemsByProperty(const nsAString & aPropertyID, const nsAString & aPropertyValue, nsIArray **_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetItemsByProperty(aPropertyID, aPropertyValue, _retval); } \
68  NS_IMETHOD GetItemCountByProperty(const nsAString & aPropertyID, const nsAString & aPropertyValue, PRUint32 *_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetItemCountByProperty(aPropertyID, aPropertyValue, _retval); } \
69  NS_IMETHOD GetItemsByProperties(sbIPropertyArray *aProperties, nsIArray **_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetItemsByProperties(aProperties, _retval); } \
70  NS_IMETHOD IndexOf(sbIMediaItem *aMediaItem, PRUint32 aStartFrom, PRUint32 *_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->IndexOf(aMediaItem, aStartFrom, _retval); } \
71  NS_IMETHOD LastIndexOf(sbIMediaItem *aMediaItem, PRUint32 aStartFrom, PRUint32 *_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->LastIndexOf(aMediaItem, aStartFrom, _retval); } \
72  NS_IMETHOD Contains(sbIMediaItem *aMediaItem, PRBool *_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->Contains(aMediaItem, _retval); } \
73  NS_IMETHOD GetIsEmpty(PRBool *aIsEmpty) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetIsEmpty(aIsEmpty); } \
74  NS_IMETHOD GetUserEditableContent(PRBool *aUserEditableContent) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetUserEditableContent(aUserEditableContent); } \
75  NS_IMETHOD Add(sbIMediaItem *aMediaItem) { return !_to ? NS_ERROR_NULL_POINTER : _to->Add(aMediaItem); } \
76  NS_IMETHOD AddItem(sbIMediaItem *aMediaItem, sbIMediaItem ** aNewMediaItem) { return !_to ? NS_ERROR_NULL_POINTER : _to->AddItem(aMediaItem, aNewMediaItem); } \
77  NS_IMETHOD AddAll(sbIMediaList *aMediaList) { return !_to ? NS_ERROR_NULL_POINTER : _to->AddAll(aMediaList); } \
78  NS_IMETHOD AddSome(nsISimpleEnumerator *aMediaItems) { return !_to ? NS_ERROR_NULL_POINTER : _to->AddSome(aMediaItems); } \
79  NS_IMETHOD AddMediaItems(nsISimpleEnumerator *aMediaItems, sbIAddMediaItemsListener *aListener, PRBool aAsync) { return !_to ? NS_ERROR_NULL_POINTER : _to->AddMediaItems(aMediaItems, aListener, aAsync); } \
80  NS_IMETHOD Remove(sbIMediaItem *aMediaItem) { return !_to ? NS_ERROR_NULL_POINTER : _to->Remove(aMediaItem); } \
81  NS_IMETHOD RemoveByIndex(PRUint32 aIndex) { return !_to ? NS_ERROR_NULL_POINTER : _to->RemoveByIndex(aIndex); } \
82  NS_IMETHOD RemoveSome(nsISimpleEnumerator *aMediaItems) { return !_to ? NS_ERROR_NULL_POINTER : _to->RemoveSome(aMediaItems); } \
83  NS_IMETHOD Clear(void) { return !_to ? NS_ERROR_NULL_POINTER : _to->Clear(); } \
84  NS_IMETHOD AddListener(sbIMediaListListener *aListener, PRBool aOwnsWeak, PRUint32 aFlags, sbIPropertyArray *aPropertyFilter) { return !_to ? NS_ERROR_NULL_POINTER : _to->AddListener(aListener, aOwnsWeak, aFlags, aPropertyFilter); } \
85  NS_IMETHOD RemoveListener(sbIMediaListListener *aListener) { return !_to ? NS_ERROR_NULL_POINTER : _to->RemoveListener(aListener); } \
86  NS_IMETHOD CreateView(sbIMediaListViewState* aState, sbIMediaListView **_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->CreateView(aState, _retval); } \
87  NS_IMETHOD GetDistinctValuesForProperty(const nsAString& aPropertyID, nsIStringEnumerator** _retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetDistinctValuesForProperty(aPropertyID, _retval); } \
88  NS_IMETHOD RunInBatchMode(sbIMediaListBatchCallback *aCallback, nsISupports *aUserData) { return !_to ? NS_ERROR_NULL_POINTER : _to->RunInBatchMode(aCallback, aUserData); }
89 
90 /* Use this macro to declare functions that forward the behavior of this interface to another object in a safe way. */
91 #define SB_FORWARD_SAFE_SBILOCALDATABASEMEDIAITEM(_to) \
92  NS_SCRIPTABLE NS_IMETHOD GetMediaItemId(PRUint32 *aMediaItemId) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetMediaItemId(aMediaItemId); } \
93  NS_SCRIPTABLE NS_IMETHOD GetPropertyBag(sbILocalDatabaseResourcePropertyBag * *aPropertyBag) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetPropertyBag(aPropertyBag); } \
94  NS_SCRIPTABLE NS_IMETHOD SetPropertyBag(sbILocalDatabaseResourcePropertyBag * aPropertyBag) { return !_to ? NS_ERROR_NULL_POINTER : _to->SetPropertyBag(aPropertyBag); } \
95  NS_IMETHOD_(void) SetSuppressNotifications(PRBool aSuppress) { if (_to) _to->SetSuppressNotifications(aSuppress); }
96 
98 {
100 public:
102  NS_DECL_SBILOCALDATABASESMARTMEDIALISTCONDITION
103 
104  sbLocalDatabaseSmartMediaListCondition(const nsAString& aPropertyID,
105  const nsAString& aOperatorString,
106  const nsAString& aLeftValue,
107  const nsAString& aRightValue,
108  const nsAString& aDisplayUnit);
109 
111 
112  nsresult ToString(nsAString& _retval);
113 
114 protected:
115  PRLock* mLock;
116 
117  nsString mPropertyID;
118  nsString mOperatorString;
119 
120  nsString mLeftValue;
121  nsString mRightValue;
122  nsString mDisplayUnit;
123 
124 private:
125  nsCOMPtr<sbIPropertyOperator> mOperator;
126 };
127 
130  public sbIMediaListListener,
132  public nsIObserver,
133  public nsIClassInfo
134 {
135 typedef nsRefPtr<sbLocalDatabaseSmartMediaListCondition> sbRefPtrCondition;
136 typedef nsTArray<PRUint32> sbMediaItemIdArray;
137 public:
139  NS_DECL_SBILOCALDATABASESMARTMEDIALIST
140  NS_DECL_SBIMEDIALISTLISTENER
141  NS_DECL_NSIOBSERVER
142  NS_DECL_NSICLASSINFO
143 
144  NS_FORWARD_SAFE_SBIMEDIAITEM(mItem)
145  NS_FORWARD_SAFE_SBILIBRARYRESOURCE(mItem)
147 
148  /* Forward all media list functions to mList except for the
149  type getter */
151  NS_IMETHOD GetType(nsAString& aType);
152  NS_IMETHOD GetName(nsAString& aName);
153  NS_IMETHOD SetName(const nsAString& aName);
154 
155  /* Throws NS_ERROR_NOT_AVAILABLE if no condition is available */
156  NS_IMETHOD GetListContentType(PRUint16* aContentType);
157 
158  nsresult Init(sbIMediaItem* aMediaItem);
159 
162 
163 private:
164 
165  nsresult RebuildMatchTypeNoneNotRandom();
166 
167  nsresult RebuildMatchTypeNoneRandom();
168 
169  nsresult RebuildMatchTypeAnyAll();
170 
171  nsresult AddMediaItemsTempTable(const nsAutoString& tempTableName,
172  sbMediaItemIdArray& aArray,
173  PRUint32 aStart,
174  PRUint32 aLength);
175 
176  nsresult GetRollingLimit(const nsAString& aSql,
177  PRUint32 aRollingLimitColumnIndex,
178  PRUint32* aRow);
179 
180  nsresult CreateSQLForCondition(sbRefPtrCondition& aCondition,
181  PRBool aIsLastCondition,
182  nsAString& _retval);
183 
184  nsresult AddCriterionForCondition(sbISQLSelectBuilder* aBuilder,
185  sbRefPtrCondition& aCondition,
186  sbIPropertyInfo* aInfo);
187 
188  nsresult AddSelectColumnAndJoin(sbISQLSelectBuilder* aBuilder,
189  const nsAString& aBaseTableAlias,
190  PRBool aAddOrderBy);
191 
192  nsresult AddLimitColumnAndJoin(sbISQLSelectBuilder* aBuilder,
193  const nsAString& aBaseTableAlias);
194 
195  nsresult CreateQueries();
196 
197  nsresult GetCopyToListQuery(const nsAString& aTempTableName,
198  nsAString& aSql);
199 
200 
201  nsresult CreateTempTable(nsAString& aName);
202 
203  nsresult DropTempTable(const nsAString& aName);
204 
205  nsresult ExecuteQuery(const nsAString& aSql);
206 
207  nsresult MakeTempTableName(nsAString& aName);
208 
209  nsresult GetMediaItemIdRange(PRUint32* aMin, PRUint32* aMax);
210 
211  nsresult GetRowCount(const nsAString& aTableName,
212  PRUint32* _retval);
213 
214  void ShuffleArray(sbMediaItemIdArray& aArray);
215 
216  nsresult GetConditionNeedsNull(sbRefPtrCondition& aCondition,
217  sbIPropertyInfo* aInfo,
218  PRBool &bNeedIsNull);
219 
220  nsresult MediaListGuidToDB(nsAString &val, PRUint32 &v);
221 
222  PRInt64 ParseDateTime(nsAString &aDateTime);
223 
224  PRInt64 StripTime(PRInt64 aDateTime);
225 
226  void SPrintfInt64(nsAString &aString, PRInt64 aValue);
227  PRInt64 ScanfInt64d(nsAString &aString);
228  nsresult ScanfInt64(nsAString &aString, PRInt64 *aRetVal);
229 
230  nsresult ReadConfiguration();
231 
232  nsresult WriteConfiguration();
233 
234  PRMonitor* mInnerMonitor;
235 
236  nsCOMPtr<sbIMediaItem> mItem;
237  nsCOMPtr<sbILocalDatabaseMediaItem> mLocalDBItem;
238  nsCOMPtr<sbIMediaList> mList;
239 
240  PRMonitor* mConditionsMonitor;
241  nsTArray<sbRefPtrCondition> mConditions;
242 
243  PRUint32 mMatchType;
244  PRUint32 mLimitType;
245  PRUint64 mLimit;
246  nsString mSelectPropertyID;
247  PRBool mSelectDirection;
248  PRBool mRandomSelection;
249  PRMonitor * mAutoUpdateMonitor;
250  PRUint32 mAutoUpdate;
251  PRUint32 mNotExistsMode;
252 
253  nsCOMPtr<sbIPropertyManager> mPropMan;
254  nsCOMPtr<sbILocalDatabasePropertyCache> mPropertyCache;
255  nsCOMPtr<sbILocalDatabaseLibrary> mLocalDatabaseLibrary;
256 
257  PRMonitor * mListenersMonitor;
258  nsCOMArray<sbILocalDatabaseSmartMediaListListener> mListeners;
259 
260  nsString mClearListQuery;
261 
262  PRMonitor * mSourceMonitor;
263  nsString mSourceLibraryGuid;
264 };
265 
266 #endif /* __SBLOCALDATABASESMARTMEDIALIST_H__ */
NS_IMETHOD SetName(const nsAString &aName)
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
onPageChanged aValue
Definition: FeedWriter.js:1395
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
A brief description of the contents of this interface.
nsDataHashtable< nsStringHashKey, nsString > sbStringMap
Interface used to listen to changes to a media list.
Interface for building SELECT statements.
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
this _dialogInput val(dateText)
An interface used to describe a metadata property for use by the UI and other sbILibrary interfaces (...
Interface for SQL critera. This interface has no public methods as it is used only as a return type f...
_updateCookies aName
NS_IMETHOD GetListContentType(PRUint16 *aContentType)
An object responsible for executing SQL queries on the database.
#define NS_FORWARD_SAFE_SBIMEDIALIST_ALL_BUT_TYPEANDNAME(_to)
Interface that defines a single item of media in the system.
#define SB_FORWARD_SAFE_SBILOCALDATABASEMEDIAITEM(_to)
NS_DECL_ISUPPORTS NS_DECL_SBILOCALDATABASESMARTMEDIALISTCONDITION sbLocalDatabaseSmartMediaListCondition(const nsAString &aPropertyID, const nsAString &aOperatorString, const nsAString &aLeftValue, const nsAString &aRightValue, const nsAString &aDisplayUnit)
Manager for system wide metadata properties.
nsresult Init(sbIMediaItem *aMediaItem)