sbDeviceLibraryHelpers.cpp
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-2010 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 /*** sbLibraryUpdateListener ***/
27 
28 #include "sbDeviceLibraryHelpers.h"
29 
30 #include <nsCOMPtr.h>
31 
32 #include <sbIDevice.h>
33 #include <sbIDeviceLibraryMediaSyncSettings.h>
34 #include <sbIDeviceLibrarySyncSettings.h>
35 #include <sbIOrderableMediaList.h>
36 #include <sbIPropertyArray.h>
37 
38 #include "sbDeviceLibrary.h"
39 
40 #include <sbDeviceUtils.h>
41 #include <sbLibraryUtils.h>
43 #include <sbStandardProperties.h>
44 
45 // note: this isn't actually threadsafe, but may be used on multiple threads
47 
49  sbILibrary * aTargetLibrary,
50  bool aIgnorePlaylists,
51  sbIDevice * aDevice)
52  : mTargetLibrary(aTargetLibrary),
53  mIgnorePlaylists(aIgnorePlaylists),
54  mDevice(aDevice)
55 {
56 }
57 
59 {
60 }
61 
62 NS_IMETHODIMP
63 sbLibraryUpdateListener::OnItemAdded(sbIMediaList *aMediaList,
64  sbIMediaItem *aMediaItem,
65  PRUint32 aIndex,
66  PRBool *_retval)
67 {
68 
69  // Nothing to do on adding to the main library
70  return NS_OK;
71 }
72 
73 NS_IMETHODIMP
74 sbLibraryUpdateListener::OnBeforeItemRemoved(sbIMediaList *aMediaList,
75  sbIMediaItem *aMediaItem,
76  PRUint32 aIndex,
77  PRBool *_retval)
78 {
79  NS_ENSURE_ARG_POINTER(aMediaList);
80  NS_ENSURE_ARG_POINTER(aMediaItem);
81  NS_ENSURE_TRUE(mTargetLibrary, NS_ERROR_NOT_INITIALIZED);
82 
83  nsresult rv;
84 
85  nsCOMPtr<sbIMediaList> list = do_QueryInterface(aMediaItem);
86  if (!list || !mIgnorePlaylists) {
89  PR_FALSE);
90  NS_ENSURE_SUCCESS(rv, rv);
91  }
92 
93  if (_retval) {
94  *_retval = PR_FALSE; /* keep going */
95  }
96  return NS_OK;
97 }
98 
99 NS_IMETHODIMP
100 sbLibraryUpdateListener::OnAfterItemRemoved(sbIMediaList *aMediaList,
101  sbIMediaItem *aMediaItem,
102  PRUint32 aIndex,
103  PRBool *_retval)
104 {
105  NS_NOTREACHED("Why are we here?");
106  if (_retval) {
107  *_retval = PR_TRUE; /* stop */
108  }
109 
110  return NS_OK;
111 }
112 
113 NS_IMETHODIMP
114 sbLibraryUpdateListener::OnItemUpdated(sbIMediaList *aMediaList,
115  sbIMediaItem *aMediaItem,
116  sbIPropertyArray *aProperties,
117  PRBool *_retval)
118 {
119  if (_retval) {
120  *_retval = PR_FALSE; /* keep going */
121  }
122 
123  return NS_OK;
124 }
125 
126 NS_IMETHODIMP
127 sbLibraryUpdateListener::OnItemMoved(sbIMediaList *aMediaList,
128  PRUint32 aFromIndex,
129  PRUint32 aToIndex,
130  PRBool *_retval)
131 {
132  NS_NOTREACHED("Why are we here?");
133 
134  if (_retval) {
135  *_retval = PR_TRUE; /* STOP */
136  }
137  return NS_OK;
138 }
139 
140 namespace
141 {
148  class EnumerateForOriginIsInMainLibrary :
151  {
152  public:
154 
158  void Initialize(sbILibrary * aLibrary,
159  bool aIgnorePlaylists)
160  {
161  mTargetLibrary = aLibrary;
162  mIgnorePlaylists = aIgnorePlaylists;
163  }
164 
165  // sbIMediaListEnumerationListener interface implementation
175  /* unsigned short onEnumerationBegin (in sbIMediaList aMediaList); */
176  NS_SCRIPTABLE NS_IMETHOD OnEnumerationBegin(sbIMediaList *,
177  PRUint16 *_retval)
178  {
179  NS_ENSURE_STATE(mTargetLibrary);
180  if (_retval) {
181  *_retval = CONTINUE;
182  }
183  return NS_OK;
184  }
185 
197  NS_SCRIPTABLE NS_IMETHOD OnEnumeratedItem(sbIMediaList *aMediaList,
198  sbIMediaItem *aMediaItem,
199  PRUint16 *_retval)
200  {
201  nsCOMPtr<sbIMediaList> list = do_QueryInterface(aMediaItem);
202  if (!list || !mIgnorePlaylists)
203  {
204  NS_NAMED_LITERAL_STRING(SB_PROPERTY_FALSE, "0");
205 
206  nsresult rv;
207 
208  rv = aMediaItem->SetProperty(
209  NS_LITERAL_STRING(SB_PROPERTY_ORIGIN_IS_IN_MAIN_LIBRARY),
210  SB_PROPERTY_FALSE);
211  NS_ENSURE_SUCCESS(rv, rv);
212  }
213  *_retval = CONTINUE;
214  return NS_OK;
215  }
216 
223  NS_SCRIPTABLE NS_IMETHOD OnEnumerationEnd(sbIMediaList *,
224  nsresult aStatusCode)
225  {
226  return NS_OK;
227  }
228 
229  // sbIMediaListBatchCallback interface
230 
235  NS_SCRIPTABLE NS_IMETHOD RunBatched(nsISupports *)
236  {
237  nsresult rv;
238 
239  NS_ENSURE_STATE(mTargetLibrary);
240 
241  rv = mTargetLibrary->EnumerateAllItems(
242  this,
244  NS_ENSURE_SUCCESS(rv, rv);
245 
246  return NS_OK;
247  }
248  private:
249  nsCOMPtr<sbILibrary> mTargetLibrary;
250  bool mIgnorePlaylists;
251  };
252 
253  NS_IMPL_ISUPPORTS1(EnumerateForOriginIsInMainLibrary,
255 
256 }
257 
258 NS_IMETHODIMP
259 sbLibraryUpdateListener::OnListCleared(sbIMediaList *aMediaList,
260  PRBool aExcludeLists,
261  PRBool *_retval)
262 {
263  NS_NOTREACHED("Why are we here?");
264  // Make sure we're dealing with the library
265  if (_retval) {
266  *_retval = PR_TRUE; /* stop */
267  }
268  return NS_OK;
269 }
270 
271 NS_IMETHODIMP
272 sbLibraryUpdateListener::OnBeforeListCleared(sbIMediaList *aMediaList,
273  PRBool aExcludeLists,
274  PRBool *_retval)
275 {
276 
277  // We clear the origin is in main library property for all device library
278  // items. This is a bit of a short cut which is fine for the current
279  // behavior. This will need to be changed, if we ever would import to
280  // a non-main library.
281  nsCOMPtr<sbILibrary> library = do_QueryInterface(aMediaList);
282  if (library) {
283  EnumerateForOriginIsInMainLibrary * enumerator;
284  NS_NEWXPCOM(enumerator, EnumerateForOriginIsInMainLibrary);
285  NS_ENSURE_TRUE(enumerator, NS_ERROR_OUT_OF_MEMORY);
286  enumerator->Initialize(mTargetLibrary,
288  nsresult rv = mTargetLibrary->RunInBatchMode(enumerator, nsnull);
289  NS_RELEASE(enumerator);
290  NS_ENSURE_SUCCESS(rv, rv);
291  }
292  if (_retval) {
293  *_retval = PR_TRUE; /* stop */
294  }
295  return NS_OK;
296 }
297 
298 NS_IMETHODIMP
299 sbLibraryUpdateListener::OnBatchBegin(sbIMediaList *aMediaList)
300 {
301  return NS_OK;
302 }
303 
304 NS_IMETHODIMP
305 sbLibraryUpdateListener::OnBatchEnd(sbIMediaList *aMediaList)
306 {
307  return NS_OK;
308 }
return NS_OK
Interface used to enumerate the items in a media list.
NS_IMPL_ISUPPORTS1(sbDeviceCapabilitiesUtils, sbIDeviceCapabilitiesUtils) sbDeviceCapabilitiesUtils
static nsresult SetOriginIsInMainLibrary(sbIMediaItem *aMediaItem, sbILibrary *aDevLibrary, PRBool aMark)
A brief description of the contents of this interface.
Interface used to listen to changes to a media list.
NS_IMPL_THREADSAFE_ISUPPORTS1(sbDeviceCapsCompatibility, sbIDeviceCapsCompatibility) sbDeviceCapsCompatibility
const unsigned short ENUMERATIONTYPE_SNAPSHOT
This flag means that the list being enumerated is a copy that may become out of date.
#define SB_PROPERTY_ORIGIN_IS_IN_MAIN_LIBRARY
Media library abstraction.
Definition: sbILibrary.idl:82
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 ...