sbDeviceLibrary.h
Go to the documentation of this file.
1 /* vim: set sw=2 :miv */
2 /*
3  *=BEGIN SONGBIRD GPL
4  *
5  * This file is part of the Songbird web player.
6  *
7  * Copyright(c) 2005-2011 POTI, Inc.
8  * http://www.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 #ifndef __SBDEVICELIBRARY_H__
27 #define __SBDEVICELIBRARY_H__
28 
29 #include <nsAutoPtr.h>
30 #include <nsCOMPtr.h>
31 #include <nsInterfaceHashtable.h>
32 #include <nsIClassInfo.h>
33 #include <nsISimpleEnumerator.h>
34 
35 #include <sbIDeviceEventListener.h>
36 #include <sbIDeviceLibrary.h>
37 #include <sbILibrary.h>
38 #include <sbIMediaListListener.h>
39 #include <sbILocalDatabaseSimpleMediaList.h>
40 #include <sbIPropertyArray.h>
41 
42 #include <nsIPrefBranch.h>
43 
44 #include "sbDeviceLibraryHelpers.h"
45 
47 struct PRMonitor;
48 
49 // These are the methods from sbLibrary that we're going to
50 // override in sbDeviceLibrary.
51 #define SB_DECL_SBILIBRARY_OVERRIDES \
52  NS_IMETHOD CreateMediaItem(nsIURI *aContentUri, sbIPropertyArray *aProperties, PRBool aAllowDuplicates, sbIMediaItem **_retval); \
53  NS_IMETHOD CreateMediaItemIfNotExist(nsIURI *aContentUri, sbIPropertyArray *aProperties, sbIMediaItem **aResultItem, PRBool *_retval); \
54  NS_IMETHOD CreateMediaList(const nsAString & aType, sbIPropertyArray *aProperties, sbIMediaList **_retval); \
55  NS_IMETHOD GetDevice(sbIDevice * *aDevice); \
56  NS_IMETHOD ClearItems(); \
57  NS_IMETHOD ClearItemsByType(const nsAString &aContentType);
58 
59 // Use this macro to declare functions that forward the behavior of this
60 // interface to another object in a safe way.
61 #define SB_FORWARD_SAFE_SBILIBRARY(_to) \
62  NS_IMETHOD GetSupportsForeignMediaItems(PRBool *aSupportsForeignMediaItems) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetSupportsForeignMediaItems(aSupportsForeignMediaItems); } \
63  NS_IMETHOD GetCreationParameters(nsIPropertyBag2 * *aCreationParameters) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetCreationParameters(aCreationParameters); } \
64  NS_IMETHOD GetFactory(sbILibraryFactory * *aFactory) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetFactory(aFactory); } \
65  NS_IMETHOD ContainsItemWithSameIdentity(sbIMediaItem* aMediaItem, PRBool* _retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->ContainsItemWithSameIdentity(aMediaItem, _retval); } \
66  NS_IMETHOD GetItemsWithSameIdentity(sbIMediaItem* aMediaItem, nsIArray** _retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetItemsWithSameIdentity(aMediaItem, _retval); } \
67  NS_IMETHOD Resolve(nsIURI *aUri, nsIChannel **_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->Resolve(aUri, _retval); } \
68  NS_IMETHOD CopyMediaList(const nsAString & aType, sbIMediaList *aSource, PRBool aDontCopyContent, sbIMediaList **_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->CopyMediaList(aType, aSource, aDontCopyContent, _retval); } \
69  NS_IMETHOD GetMediaItem(const nsAString & aGuid, sbIMediaItem **_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetMediaItem(aGuid, _retval); } \
70  NS_IMETHOD GetDuplicate(sbIMediaItem *aMediaItem, sbIMediaItem **_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetDuplicate(aMediaItem, _retval); } \
71  NS_IMETHOD GetMediaListTypes(nsIStringEnumerator * *aMediaListTypes) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetMediaListTypes(aMediaListTypes); } \
72  NS_IMETHOD RegisterMediaListFactory(sbIMediaListFactory *aFactory) { return !_to ? NS_ERROR_NULL_POINTER : _to->RegisterMediaListFactory(aFactory); } \
73  NS_IMETHOD Optimize(PRBool aAnalyzeOnly) { return !_to ? NS_ERROR_NULL_POINTER : _to->Optimize(aAnalyzeOnly); } \
74  NS_IMETHOD Flush(void) { return !_to ? NS_ERROR_NULL_POINTER : _to->Flush(); } \
75  NS_IMETHOD BatchCreateMediaItems(nsIArray *aURIArray, nsIArray *aPropertyArrayArray, PRBool aAllowDuplicates, nsIArray **_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->BatchCreateMediaItems(aURIArray, aPropertyArrayArray, aAllowDuplicates, _retval); } \
76  NS_IMETHOD BatchCreateMediaItemsIfNotExist(nsIArray *aURIArray, nsIArray *aPropertyArrayArray, nsIArray **aResultItemArray, nsIArray **_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->BatchCreateMediaItemsIfNotExist(aURIArray, aPropertyArrayArray, aResultItemArray, _retval); } \
77  NS_IMETHOD BatchCreateMediaItemsAsync(sbIBatchCreateMediaItemsListener *aListener, nsIArray *aURIArray, nsIArray *aPropertyArrayArray, PRBool aAllowDuplicates) { return !_to ? NS_ERROR_NULL_POINTER : _to->BatchCreateMediaItemsAsync(aListener, aURIArray, aPropertyArrayArray, aAllowDuplicates); }
78 
79 // These are the methods from sbLibrary that we're going to
80 // override in sbDeviceLibrary.
81 #define SB_DECL_SBIMEDIALIST_OVERRIDES \
82  NS_IMETHOD Add(sbIMediaItem *aMediaItem); \
83  NS_IMETHOD AddItem(sbIMediaItem *aMediaItem, sbIMediaItem ** aNewMediaItem); \
84  NS_IMETHOD AddAll(sbIMediaList *aMediaList); \
85  NS_IMETHOD AddSome(nsISimpleEnumerator *aMediaItems); \
86  NS_IMETHOD AddMediaItems(nsISimpleEnumerator *aMediaItems, sbIAddMediaItemsListener *aListener, PRBool aAsync); \
87  NS_IMETHOD Clear(void);
88 
89 #define SB_FORWARD_SAFE_SBIMEDIALIST(_to) \
90  NS_IMETHOD GetName(nsAString & aName) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetName(aName); } \
91  NS_IMETHOD SetName(const nsAString & aName) { return !_to ? NS_ERROR_NULL_POINTER : _to->SetName(aName); } \
92  NS_IMETHOD GetType(nsAString & aType) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetType(aType); } \
93  NS_IMETHOD GetLength(PRUint32 *aLength) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetLength(aLength); } \
94  NS_IMETHOD GetIsEmpty(PRBool *aIsEmpty) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetIsEmpty(aIsEmpty); } \
95  NS_IMETHOD GetUserEditableContent(PRBool *aUserEditableContent) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetUserEditableContent(aUserEditableContent); } \
96  NS_IMETHOD GetItemByGuid(const nsAString & aGuid, sbIMediaItem **_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetItemByGuid(aGuid, _retval); } \
97  NS_IMETHOD GetItemByIndex(PRUint32 aIndex, sbIMediaItem **_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetItemByIndex(aIndex, _retval); } \
98  NS_IMETHOD GetListContentType(PRUint16 *_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetListContentType(_retval); } \
99  NS_IMETHOD EnumerateAllItems(sbIMediaListEnumerationListener *aEnumerationListener, PRUint16 aEnumerationType) { return !_to ? NS_ERROR_NULL_POINTER : _to->EnumerateAllItems(aEnumerationListener, aEnumerationType); } \
100  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); } \
101  NS_IMETHOD EnumerateItemsByProperties(sbIPropertyArray *aProperties, sbIMediaListEnumerationListener *aEnumerationListener, PRUint16 aEnumerationType) { return !_to ? NS_ERROR_NULL_POINTER : _to->EnumerateItemsByProperties(aProperties, aEnumerationListener, aEnumerationType); } \
102  NS_IMETHOD GetItemsByProperty(const nsAString & aPropertyID, const nsAString & aPropertyValue, nsIArray **_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetItemsByProperty(aPropertyID, aPropertyValue, _retval); } \
103  NS_IMETHOD GetItemCountByProperty(const nsAString & aPropertyID, const nsAString & aPropertyValue, PRUint32 *_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetItemCountByProperty(aPropertyID, aPropertyValue, _retval); } \
104  NS_IMETHOD GetItemsByProperties(sbIPropertyArray *aProperties, nsIArray **_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetItemsByProperties(aProperties, _retval); } \
105  NS_IMETHOD IndexOf(sbIMediaItem *aMediaItem, PRUint32 aStartFrom, PRUint32 *_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->IndexOf(aMediaItem, aStartFrom, _retval); } \
106  NS_IMETHOD LastIndexOf(sbIMediaItem *aMediaItem, PRUint32 aStartFrom, PRUint32 *_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->LastIndexOf(aMediaItem, aStartFrom, _retval); } \
107  NS_IMETHOD Contains(sbIMediaItem *aMediaItem, PRBool *_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->Contains(aMediaItem, _retval); } \
108  NS_IMETHOD Remove(sbIMediaItem *aMediaItem) { return !_to ? NS_ERROR_NULL_POINTER : _to->Remove(aMediaItem); } \
109  NS_IMETHOD RemoveByIndex(PRUint32 aIndex) { return !_to ? NS_ERROR_NULL_POINTER : _to->RemoveByIndex(aIndex); } \
110  NS_IMETHOD RemoveSome(nsISimpleEnumerator *aMediaItems) { return !_to ? NS_ERROR_NULL_POINTER : _to->RemoveSome(aMediaItems); } \
111  NS_IMETHOD AddListener(sbIMediaListListener *aListener, PRBool aOwnsWeak, PRUint32 aFlags, sbIPropertyArray *aPropertyFilter) { return !_to ? NS_ERROR_NULL_POINTER : _to->AddListener(aListener, aOwnsWeak, aFlags, aPropertyFilter); } \
112  NS_IMETHOD RemoveListener(sbIMediaListListener *aListener) { return !_to ? NS_ERROR_NULL_POINTER : _to->RemoveListener(aListener); } \
113  NS_IMETHOD CreateView(sbIMediaListViewState *aState, sbIMediaListView **_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->CreateView(aState, _retval); } \
114  NS_IMETHOD RunInBatchMode(sbIMediaListBatchCallback *aCallback, nsISupports *aUserData) { return !_to ? NS_ERROR_NULL_POINTER : _to->RunInBatchMode(aCallback, aUserData); } \
115  NS_IMETHOD GetDistinctValuesForProperty(const nsAString & aPropertyID, nsIStringEnumerator **_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetDistinctValuesForProperty(aPropertyID, _retval); }
116 
117 #define SB_FORWARD_SAFE_SBIMEDIAITEM_MINUS_OVERRIDES(_to) \
118  NS_SCRIPTABLE NS_IMETHOD GetIsMutable(PRBool *aIsMutable) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetIsMutable(aIsMutable); } \
119  NS_SCRIPTABLE NS_IMETHOD GetItemController(sbIMediaItemController **aMediaItemController) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetItemController(aMediaItemController); } \
120  NS_SCRIPTABLE NS_IMETHOD GetMediaCreated(PRInt64 *aMediaCreated) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetMediaCreated(aMediaCreated); } \
121  NS_SCRIPTABLE NS_IMETHOD SetMediaCreated(PRInt64 aMediaCreated) { return !_to ? NS_ERROR_NULL_POINTER : _to->SetMediaCreated(aMediaCreated); } \
122  NS_SCRIPTABLE NS_IMETHOD GetMediaUpdated(PRInt64 *aMediaUpdated) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetMediaUpdated(aMediaUpdated); } \
123  NS_SCRIPTABLE NS_IMETHOD SetMediaUpdated(PRInt64 aMediaUpdated) { return !_to ? NS_ERROR_NULL_POINTER : _to->SetMediaUpdated(aMediaUpdated); } \
124  NS_SCRIPTABLE NS_IMETHOD GetContentSrc(nsIURI * *aContentSrc) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetContentSrc(aContentSrc); } \
125  NS_SCRIPTABLE NS_IMETHOD SetContentSrc(nsIURI * aContentSrc) { return !_to ? NS_ERROR_NULL_POINTER : _to->SetContentSrc(aContentSrc); } \
126  NS_SCRIPTABLE NS_IMETHOD GetContentLength(PRInt64 *aContentLength) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetContentLength(aContentLength); } \
127  NS_SCRIPTABLE NS_IMETHOD SetContentLength(PRInt64 aContentLength) { return !_to ? NS_ERROR_NULL_POINTER : _to->SetContentLength(aContentLength); } \
128  NS_SCRIPTABLE NS_IMETHOD GetContentType(nsAString & aContentType) { return !_to ? NS_ERROR_NULL_POINTER : _to->GetContentType(aContentType); } \
129  NS_SCRIPTABLE NS_IMETHOD SetContentType(const nsAString & aContentType) { return !_to ? NS_ERROR_NULL_POINTER : _to->SetContentType(aContentType); } \
130  NS_SCRIPTABLE NS_IMETHOD TestIsURIAvailable(nsIObserver *aObserver) { return !_to ? NS_ERROR_NULL_POINTER : _to->TestIsURIAvailable(aObserver); } \
131  NS_SCRIPTABLE NS_IMETHOD OpenInputStreamAsync(nsIStreamListener *aListener, nsISupports *aContext, nsIChannel **_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->OpenInputStreamAsync(aListener, aContext, _retval); } \
132  NS_SCRIPTABLE NS_IMETHOD OpenInputStream(nsIInputStream **_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->OpenInputStream(_retval); } \
133  NS_SCRIPTABLE NS_IMETHOD OpenOutputStream(nsIOutputStream **_retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->OpenOutputStream(_retval); } \
134  NS_SCRIPTABLE NS_IMETHOD ToString(nsAString & _retval) { return !_to ? NS_ERROR_NULL_POINTER : _to->ToString(_retval); }
135 
137  public sbIMediaListListener,
140 {
141 public:
143  NS_DECL_NSICLASSINFO
144 
145  NS_DECL_SBIDEVICELIBRARY
146  NS_DECL_SBIMEDIALISTLISTENER
147  NS_DECL_SBILOCALDATABASEMEDIALISTCOPYLISTENER
148  NS_DECL_SBIDEVICEEVENTLISTENER
149 
150  sbDeviceLibrary(sbIDevice* aDevice);
151  virtual ~sbDeviceLibrary();
152 
153  NS_FORWARD_SAFE_SBILIBRARYRESOURCE(mDeviceLibrary)
157 
158  NS_IMETHODIMP GetLibrary(sbILibrary** _retval) {
159  NS_ENSURE_ARG_POINTER(_retval);
160  *_retval = this;
161  NS_ADDREF(*_retval);
162  return NS_OK;
163  }
164 
167 
169  (const nsAString& aDeviceIdentifier,
170  nsIFile** aDBFile);
171 
172 private:
183  static PLDHashOperator PR_CALLBACK
184  AddListenersToCOMArrayCallback(nsISupportsHashKey::KeyType aKey,
185  sbIDeviceLibraryListener* aEntry,
186  void* aUserData);
187 
196  nsresult GetMgmtTypePrefKey(PRUint32 aContentType, nsAString& aPrefKey);
197 
206  nsresult GetSyncListsPrefKey(PRUint32 aContentType, nsAString& aPrefKey);
207 
215  nsresult GetSyncRootPrefKey(PRUint32 aContentType, nsAString& aPrefKey);
216 
235  nsresult CreateDeviceLibrary(const nsAString &aDeviceIdentifier,
236  nsIURI *aDeviceDatabaseURI);
237 
246  nsresult RegisterDeviceLibrary();
247 
260  nsresult UnregisterDeviceLibrary();
261 
266  nsresult UpdateMainLibraryListeners(
267  sbIDeviceLibrarySyncSettings * aSyncSettings);
268 
273  nsresult UpdateIsReadOnly();
274 
282  nsresult GetIsMgmtTypeSyncAll(PRBool* aIsMgmtTypeSyncAll);
283 
291  nsresult GetIsMgmtTypeSyncList(PRBool* aIsMgmtTypeSyncList);
292 
298  nsresult SetSyncSettingsNoLock(sbIDeviceLibrarySyncSettings * aSyncSettings);
299 
300  sbDeviceLibrarySyncSettings * CreateSyncSettings();
301 
307  nsCOMPtr<sbILibrary> mDeviceLibrary;
308 
312  nsCOMPtr<sbIDevice> mDevice;
313 
317  nsRefPtr<sbLibraryUpdateListener> mMainLibraryListener;
318 
322  nsCOMPtr<sbIMutablePropertyArray> mMainLibraryListenerFilter;
323 
327  nsInterfaceHashtable<nsISupportsHashKey, sbIDeviceLibraryListener> mListeners;
328 
332  nsRefPtr<sbDeviceLibrarySyncSettings> mSyncSettings;
333  PRMonitor* mMonitor;
334 };
335 
336 #define PREF_SYNC_PREFIX "library."
337 #define PREF_SYNC_BRANCH ".sync."
338 #define PREF_SYNC_MGMTTYPE "mgmtType"
339 #define PREF_SYNC_IMPORT "import"
340 #define PREF_SYNC_LISTS "playlists"
341 #define PREF_SYNC_ROOT "root"
342 #define PREF_SYNC_FOLDER "syncFolder"
343 
344 extern char const * const gMediaType[];
345 
346 #endif /* __SBDEVICELIBRARY_H__ */
NS_DECL_ISUPPORTS NS_DECL_NSICLASSINFO NS_DECL_SBIDEVICELIBRARY NS_DECL_SBIMEDIALISTLISTENER NS_DECL_SBILOCALDATABASEMEDIALISTCOPYLISTENER NS_DECL_SBIDEVICEEVENTLISTENER sbDeviceLibrary(sbIDevice *aDevice)
char const *const gMediaType[]
return NS_OK
#define SB_DECL_SBIMEDIALIST_OVERRIDES
#define SB_FORWARD_SAFE_SBIMEDIAITEM_MINUS_OVERRIDES(_to)
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
virtual ~sbDeviceLibrary()
#define SB_FORWARD_SAFE_SBIMEDIALIST(_to)
Interface used to listen to changes to a device library.
NS_IMETHODIMP GetLibrary(sbILibrary **_retval)
Interface used to listen to changes to a media list.
#define SB_FORWARD_SAFE_SBILIBRARY(_to)
Media library abstraction.
Definition: sbILibrary.idl:82
static nsresult GetDefaultDeviceLibraryDatabaseFile(const nsAString &aDeviceIdentifier, nsIFile **aDBFile)
#define SB_DECL_SBILIBRARY_OVERRIDES