DeviceBase.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 
32 #ifndef __DEVICE_BASE_H__
33 #define __DEVICE_BASE_H__
34 
35 #include "sbIDeviceBase.h"
36 
37 #include <sbILibrary.h>
38 #include <sbIMediaList.h>
39 #include <sbIMediaListListener.h>
40 #include <sbIMediaItem.h>
41 
42 #include <sbILocalDatabaseLibrary.h>
43 #include <sbILocalDatabaseSimpleMediaList.h>
44 
45 #include <nsAutoPtr.h>
46 #include <nsIMutableArray.h>
47 #include <nsInterfaceHashtable.h>
48 #include <nsClassHashtable.h>
49 #include <nsDataHashtable.h>
50 #include <nsIThread.h>
51 #include <nsIRunnable.h>
52 #include <nsIURI.h>
53 #include <nsRefPtrHashtable.h>
54 
55 #include <nsCOMPtr.h>
56 #include <nsStringGlue.h>
57 
58 #define SONGBIRD_DeviceBase_CONTRACTID \
59  "@songbirdnest.com/Songbird/Device/DeviceBase;1"
60 #define SONGBIRD_DeviceBase_CLASSNAME \
61  "Songbird Device Base"
62 #define SONGBIRD_DeviceBase_CID \
63 { /* 937d0ae7-9ba1-475d-82fd-609ff5f78508 */ \
64  0x937d0ae7, \
65  0x9ba1, \
66  0x475d, \
67  {0x82, 0xfd, 0x60, 0x9f, 0xf5, 0xf7, 0x85, 0x8} \
68 }
69 
70 // FUNCTIONS ==================================================================
71 void
72 ReplaceChars(nsAString& aOldString,
73  const nsAString& aOldChars,
74  const PRUnichar aNewChar);
75 
76 void
77 ReplaceChars(nsACString& aOldString,
78  const nsACString& aOldChars,
79  const char aNewChar);
80 
81 // CLASSES ====================================================================
83 {
84 public:
86  NS_DECL_SBIMEDIALISTLISTENER
87 
90 
91  nsresult Init(const nsAString &aDeviceIdentifier,
92  sbIDeviceBase* aDevice);
93 
94  nsresult SetIgnoreListener(PRBool aIgnoreListener);
95  nsresult SetManagePlaylists(PRBool aManagePlaylists);
96 
97 protected:
98  static PLDHashOperator PR_CALLBACK
99  ShiftIndexesCallback(nsISupportsHashKey::KeyType aKey,
100  PRUint32& aEntry,
101  void* aUserData);
102 
103  // The device owns the listener, so use a non-owning reference here
106 
109 
110  typedef nsDataHashtable<nsISupportsHashKey, PRUint32> sbRemovedItemIndexes;
111  nsClassHashtable<nsISupportsHashKey, sbRemovedItemIndexes> mBeforeRemoveIndexes;
112 };
113 
115 {
116 public:
118  NS_DECL_SBILOCALDATABASEMEDIALISTCOPYLISTENER
119 
122 
123  nsresult Init(const nsAString &aDeviceIdentifier,
124  sbIDeviceBase* aDevice);
125 
126 protected:
127  nsCOMPtr<sbIDeviceBase> mDevice;
129 
130 };
131 
133 {
134 public:
135  NS_IMETHOD_(nsrefcnt) AddRef(void);
136  NS_IMETHOD_(nsrefcnt) Release(void);
137 
139  virtual ~sbDeviceBaseCallbackProxy();
140  nsresult Init(sbIDeviceBaseCallback* aCallback);
141 
142  nsCOMPtr<sbIDeviceBaseCallback> mCallbackProxy;
143  nsCOMPtr<nsIEventTarget> mOwningThread;
144 
145 protected:
146  nsAutoRefCnt mRefCnt;
147  NS_DECL_OWNINGTHREAD
148 };
149 
151 {
152  friend class sbDeviceThread;
153 
154 public:
155  sbDeviceBase();
156  virtual ~sbDeviceBase();
157 
161  nsresult Init();
162 
184  nsresult CreateDeviceLibrary(const nsAString &aDeviceIdentifier,
185  nsIURI *aDeviceDatabaseURI,
186  sbIDeviceBase *aDevice);
187 
196  nsresult RemoveDeviceLibrary(const nsAString &aDeviceIdentifier);
197 
207  nsresult GetLibraryForDevice(const nsAString &aDeviceIdentifier,
208  sbILibrary* *aDeviceLibrary);
209 
219  nsresult RegisterDeviceLibrary(sbILibrary* aDeviceLibrary);
220 
234  nsresult UnregisterDeviceLibrary(sbILibrary* aDeviceLibrary);
235 
244  nsresult CreateTransferQueue(const nsAString &aDeviceIdentifier);
245 
255  nsresult RemoveTransferQueue(const nsAString &aDeviceIdentifier);
256 
262  nsresult AddItemToTransferQueue(const nsAString &aDeviceIdentifier,
263  sbIMediaItem* aMediaItem);
264 
270  nsresult RemoveItemFromTransferQueue(const nsAString &aDeviceIdentifier,
271  sbIMediaItem* aMediaItem);
278  nsresult GetNextItemFromTransferQueue(const nsAString &aDeviceIdentifier,
279  sbIMediaItem* *aMediaItem);
287  nsresult GetItemByIndexFromTransferQueue(const nsAString &aDeviceIdentifier,
288  PRUint32 aItemIndex,
289  sbIMediaItem* *aMediaItem);
295  nsresult GetTransferQueue(const nsAString &aDeviceIdentifier,
296  nsIMutableArray* *aTransferQueue);
297 
306  nsresult ClearTransferQueue(const nsAString &aDeviceIdentifier);
307 
308  nsresult IsTransferQueueEmpty(const nsAString &aDeviceIdentifier,
309  PRBool &aEmpty);
310 
314  nsresult AddCallback(sbIDeviceBaseCallback* aCallback);
315 
319  nsresult RemoveCallback(sbIDeviceBaseCallback* aCallback);
320 
324  void DoTransferStartCallback(sbIMediaItem* aMediaItem);
325 
329  void DoTransferCompleteCallback(sbIMediaItem* aMediaItem,
330  PRInt32 aStatus);
331 
335  void DoDeviceConnectCallback(const nsAString& aDeviceIdentifier);
336 
340  void DoDeviceDisconnectCallback(const nsAString& aDeviceIdentifier);
341 
345  void DoStateChangedCallback(const nsAString& aDeviceIdentifier,
346  PRUint32 aState);
347 
351  nsresult GetDeviceState(const nsAString& aDeviceIdentifier,
352  PRUint32* aDeviceState);
356  nsresult SetDeviceState(const nsAString& aDeviceIdentifier,
357  PRUint32 aDeviceState);
358 
362  nsresult InitDeviceState(const nsAString& aDeviceIdentifier);
363 
364  nsresult ClearDeviceState(const nsAString& aDeviceIdentifier);
365 
366  nsresult SetListenerForDeviceLibrary(const nsAString& aDeviceIdentifier,
367  sbIMediaListListener *aMediaListListener);
368  nsresult GetListenerForDeviceLibrary(const nsAString& aDeviceIdentifier,
369  sbIMediaListListener* *aMediaListListener);
370  nsresult RemoveListenerForDeviceLibrary(const nsAString& aDeviceIdentifier);
371 protected:
372  nsInterfaceHashtableMT<nsStringHashKey, sbILibrary> mDeviceLibraries;
373  nsInterfaceHashtableMT<nsStringHashKey, nsIMutableArray> mDeviceQueues;
374  nsRefPtrHashtableMT<nsISupportsHashKey, sbDeviceBaseCallbackProxy> mDeviceCallbacks;
375  nsDataHashtableMT<nsStringHashKey, PRUint32> mDeviceStates;
376 
377  nsInterfaceHashtableMT<nsStringHashKey, sbIMediaListListener> mDeviceLibraryListeners;
378 };
379 
380 #endif // __DEVICE_BASE_H__
381 
nsresult GetListenerForDeviceLibrary(const nsAString &aDeviceIdentifier, sbIMediaListListener **aMediaListListener)
nsresult RemoveItemFromTransferQueue(const nsAString &aDeviceIdentifier, sbIMediaItem *aMediaItem)
Remove an item from the internal transfer queue.
nsresult SetIgnoreListener(PRBool aIgnoreListener)
Definition: DeviceBase.cpp:169
void DoStateChangedCallback(const nsAString &aDeviceIdentifier, PRUint32 aState)
Definition: DeviceBase.cpp:852
void ReplaceChars(nsAString &aOldString, const nsAString &aOldChars, const PRUnichar aNewChar)
Definition: DeviceBase.cpp:95
nsresult SetDeviceState(const nsAString &aDeviceIdentifier, PRUint32 aDeviceState)
nsCOMPtr< sbIDeviceBase > mDevice
Definition: DeviceBase.h:127
nsresult Init(sbBaseDevice *aDevice)
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
nsresult GetDeviceState(const nsAString &aDeviceIdentifier, PRUint32 *aDeviceState)
NS_IMETHOD_(nsrefcnt) AddRef(void)
nsresult IsTransferQueueEmpty(const nsAString &aDeviceIdentifier, PRBool &aEmpty)
nsresult UnregisterDeviceLibrary(sbILibrary *aDeviceLibrary)
Unregister a device library with the library manager.
nsresult GetLibraryForDevice(const nsAString &aDeviceIdentifier, sbILibrary **aDeviceLibrary)
Get the library instance for a device.
nsInterfaceHashtableMT< nsStringHashKey, nsIMutableArray > mDeviceQueues
Definition: DeviceBase.h:373
nsresult SetManagePlaylists(PRBool aManagePlaylists)
Definition: DeviceBase.cpp:176
nsClassHashtable< nsISupportsHashKey, sbRemovedItemIndexes > mBeforeRemoveIndexes
Definition: DeviceBase.h:111
[SOON TO BE DEPRECATED AFTER 0.3] The callback class for sbIDeviceBase
nsCOMPtr< nsIEventTarget > mOwningThread
Definition: DeviceBase.h:143
nsresult Init(const nsAString &aDeviceIdentifier, sbIDeviceBase *aDevice)
Definition: DeviceBase.cpp:155
nsresult CreateTransferQueue(const nsAString &aDeviceIdentifier)
Create an internal transfer queue for a device instance.
nsresult GetTransferQueue(const nsAString &aDeviceIdentifier, nsIMutableArray **aTransferQueue)
Get the internnal transfer queue for a device instance.
Interface used to listen to changes to a media list.
[SOON TO BE DEPRECATED AFTER 0.3] Base interface for all supported devices.
nsresult InitDeviceState(const nsAString &aDeviceIdentifier)
nsDataHashtableMT< nsStringHashKey, PRUint32 > mDeviceStates
Definition: DeviceBase.h:375
nsCOMPtr< sbIDeviceBaseCallback > mCallbackProxy
Definition: DeviceBase.h:142
NS_DECL_ISUPPORTS NS_DECL_SBILOCALDATABASEMEDIALISTCOPYLISTENER sbDeviceBaseLibraryCopyListener()
nsresult RemoveTransferQueue(const nsAString &aDeviceIdentifier)
Remove an internal transfer queue for a device instance.
nsresult AddCallback(sbIDeviceBaseCallback *aCallback)
Definition: DeviceBase.cpp:706
virtual ~sbDeviceBaseLibraryListener()
Definition: DeviceBase.cpp:150
nsInterfaceHashtableMT< nsStringHashKey, sbILibrary > mDeviceLibraries
Definition: DeviceBase.h:372
nsresult SetListenerForDeviceLibrary(const nsAString &aDeviceIdentifier, sbIMediaListListener *aMediaListListener)
static PLDHashOperator PR_CALLBACK ShiftIndexesCallback(nsISupportsHashKey::KeyType aKey, PRUint32 &aEntry, void *aUserData)
Definition: DeviceBase.cpp:127
nsresult AddItemToTransferQueue(const nsAString &aDeviceIdentifier, sbIMediaItem *aMediaItem)
Add an item to the internal transfer queue.
nsresult GetNextItemFromTransferQueue(const nsAString &aDeviceIdentifier, sbIMediaItem **aMediaItem)
Get the next item in the transfer queue.
Media library abstraction.
Definition: sbILibrary.idl:82
nsresult CreateDeviceLibrary(const nsAString &aDeviceIdentifier, nsIURI *aDeviceDatabaseURI, sbIDeviceBase *aDevice)
Create a library for a device instance.
Definition: DeviceBase.cpp:879
sbIDeviceBase * mDevice
Definition: DeviceBase.h:104
nsresult Init()
Initialize the base device class for use.
Definition: DeviceBase.cpp:694
void DoDeviceDisconnectCallback(const nsAString &aDeviceIdentifier)
Definition: DeviceBase.cpp:826
nsresult RegisterDeviceLibrary(sbILibrary *aDeviceLibrary)
Register a device library with the library manager.
NS_DECL_ISUPPORTS NS_DECL_SBIMEDIALISTLISTENER sbDeviceBaseLibraryListener()
Definition: DeviceBase.cpp:143
nsresult RemoveCallback(sbIDeviceBaseCallback *aCallback)
Definition: DeviceBase.cpp:727
void DoTransferStartCallback(sbIMediaItem *aMediaItem)
Definition: DeviceBase.cpp:745
virtual ~sbDeviceBase()
Definition: DeviceBase.cpp:689
nsresult RemoveListenerForDeviceLibrary(const nsAString &aDeviceIdentifier)
nsInterfaceHashtableMT< nsStringHashKey, sbIMediaListListener > mDeviceLibraryListeners
Definition: DeviceBase.h:377
nsresult RemoveDeviceLibrary(const nsAString &aDeviceIdentifier)
Remove a library for a device instance.
Definition: DeviceBase.cpp:994
nsresult GetItemByIndexFromTransferQueue(const nsAString &aDeviceIdentifier, PRUint32 aItemIndex, sbIMediaItem **aMediaItem)
Get an item from the transfer queue by index.
Interface that defines a single item of media in the system.
nsRefPtrHashtableMT< nsISupportsHashKey, sbDeviceBaseCallbackProxy > mDeviceCallbacks
Definition: DeviceBase.h:374
nsresult ClearTransferQueue(const nsAString &aDeviceIdentifier)
Clear the entire transfer queue.
void DoDeviceConnectCallback(const nsAString &aDeviceIdentifier)
Definition: DeviceBase.cpp:800
nsresult ClearDeviceState(const nsAString &aDeviceIdentifier)
friend class sbDeviceThread
Definition: DeviceBase.h:152
nsresult Init(sbIDeviceBaseCallback *aCallback)
Definition: DeviceBase.cpp:650
void DoTransferCompleteCallback(sbIMediaItem *aMediaItem, PRInt32 aStatus)
Definition: DeviceBase.cpp:772
nsDataHashtable< nsISupportsHashKey, PRUint32 > sbRemovedItemIndexes
Definition: DeviceBase.h:110
restoreWindow aState
virtual ~sbDeviceBaseCallbackProxy()
Definition: DeviceBase.cpp:639