sbDeviceLibraryMediaSyncSettings.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-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  */
26 
27 // Mozilla includes
28 #include <nsArrayUtils.h>
29 #include <nsAutoLock.h>
30 #include <nsAutoPtr.h>
31 #include <nsIProperties.h>
32 
33 // Songbird includes
34 #include <sbDeviceUtils.h>
36 #include <sbIDeviceLibrary.h>
37 #include <sbIDeviceLibrarySyncSettings.h>
38 #include <sbILibrary.h>
39 #include <sbILibraryManager.h>
40 #include <sbLibraryUtils.h>
41 #include <sbHashtableUtils.h>
42 
44 
45 PLDHashOperator ArrayBuilder(nsISupports * aKey,
46  PRBool aData,
47  void* userArg)
48 {
49  NS_ASSERTION(userArg, "ArrayBuilder passed a null arg");
50  nsIMutableArray * array = static_cast<nsIMutableArray*>(userArg);
51  if(aData) {
52  nsresult rv = array->AppendElement(aKey, PR_FALSE);
53  NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
54  }
55  return PL_DHASH_NEXT;
56 }
57 
58 PLDHashOperator ResetSelection(nsISupports * aKey,
59  PRBool & aData,
60  void* userArg)
61 {
62  aData = PR_FALSE;
63  return PL_DHASH_NEXT;
64 }
65 
67 sbDeviceLibraryMediaSyncSettings::New(
68  sbDeviceLibrarySyncSettings * aSyncSettings,
69  PRUint32 aMediaType,
70  PRLock * aLock)
71 {
72  return new sbDeviceLibraryMediaSyncSettings(aSyncSettings,
73  aMediaType,
74  aLock);
75 }
76 
77 sbDeviceLibraryMediaSyncSettings::sbDeviceLibraryMediaSyncSettings(
78  sbDeviceLibrarySyncSettings * aSyncSettings,
79  PRUint32 aMediaType,
80  PRLock * aLock) :
81  mSyncMgmtType(sbIDeviceLibraryMediaSyncSettings::SYNC_MGMT_NONE),
82  mLastActiveSyncMgmtType(sbIDeviceLibraryMediaSyncSettings::SYNC_MGMT_ALL),
83  mMediaType(aMediaType),
84  mImport(false),
85  mLock(aLock),
86  mSyncSettings(aSyncSettings)
87 {
88  mPlaylistsSelection.Init();
89 }
90 
91 sbDeviceLibraryMediaSyncSettings::~sbDeviceLibraryMediaSyncSettings()
92 {
93 }
94 
96 {
98  typedef nsISupports * KeyType;
99  typedef PRBool DataType;
100 };
101 
102 nsresult
103 sbDeviceLibraryMediaSyncSettings::Assign(
105 {
106  nsresult rv;
107 
108  mSyncMgmtType = aSettings->mSyncMgmtType;
109  mMediaType = aSettings->mMediaType;
110  mImport = aSettings->mImport;
111 
112  rv = sbCopyHashtable<PlaylistHashtableTraits>(
113  aSettings->mPlaylistsSelection,
114  mPlaylistsSelection);
115  NS_ENSURE_SUCCESS(rv, rv);
116  mSyncFolder = aSettings->mSyncFolder;
117  rv = aSettings->mSyncFromFolder->Clone(getter_AddRefs(mSyncFromFolder));
118  NS_ENSURE_SUCCESS(rv, rv);
119 
120  mLock = aSettings->mLock;
121 
122  return NS_OK;
123 }
124 
125 nsresult
126 sbDeviceLibraryMediaSyncSettings::CreateCopy(
128 {
129  NS_ENSURE_ARG_POINTER(aSettings);
130  nsresult rv;
131  nsRefPtr<sbDeviceLibraryMediaSyncSettings> newSettings =
132  sbDeviceLibraryMediaSyncSettings::New(mSyncSettings,
133  mMediaType,
134  mLock);
135 
136  newSettings->mSyncMgmtType = mSyncMgmtType;
137  rv = sbCopyHashtable<PlaylistHashtableTraits>(
138  mPlaylistsSelection,
139  newSettings->mPlaylistsSelection);
140  NS_ENSURE_SUCCESS(rv, rv);
141 
142  newSettings->mImport = mImport;
143 
144  newSettings->mSyncFolder = mSyncFolder;
145  if (mSyncFromFolder) {
146  rv = mSyncFromFolder->Clone(getter_AddRefs(newSettings->mSyncFromFolder));
147  NS_ENSURE_SUCCESS(rv, rv);
148  }
149  else {
150  mSyncFromFolder = nsnull;
151  }
152  newSettings.forget(aSettings);
153 
154  return NS_OK;
155 }
156 
157 nsresult
158 sbDeviceLibraryMediaSyncSettings::GetMgmtTypeNoLock(PRUint32 *aSyncMgmtType)
159 {
160  NS_ENSURE_ARG_POINTER(aSyncMgmtType);
161  *aSyncMgmtType = mSyncMgmtType;
162  return NS_OK;
163 }
164 
165 NS_IMETHODIMP
166 sbDeviceLibraryMediaSyncSettings::GetMgmtType(PRUint32 *aSyncMgmtType)
167 {
168  NS_ENSURE_ARG_POINTER(aSyncMgmtType);
169  NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY);
170  nsAutoLock lock(mLock);
171  *aSyncMgmtType = mSyncMgmtType;
172  return NS_OK;
173 }
174 
175 
176 NS_IMETHODIMP
177 sbDeviceLibraryMediaSyncSettings::GetLastActiveMgmtType
178  (PRUint32 *aLastActiveSyncMgmtType)
179 {
180  NS_ENSURE_ARG_POINTER(aLastActiveSyncMgmtType);
181  // jhawk do I actually need to lock this?
182  NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY);
183  nsAutoLock lock(mLock);
184  *aLastActiveSyncMgmtType = mLastActiveSyncMgmtType;
185  return NS_OK;
186 }
187 
188 NS_IMETHODIMP
189 sbDeviceLibraryMediaSyncSettings::SetMgmtType(PRUint32 aSyncMgmtType)
190 {
191  NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY);
192 
193  {
194  nsAutoLock lock(mLock);
196  mLastActiveSyncMgmtType = mSyncMgmtType;
197  }
198  mSyncMgmtType = aSyncMgmtType;
199  }
200 
201  return NS_OK;
202 }
203 
204 NS_IMETHODIMP
205 sbDeviceLibraryMediaSyncSettings::GetImport(PRBool *aImport)
206 {
207  NS_ENSURE_ARG_POINTER(aImport);
208  NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY);
209  nsAutoLock lock(mLock);
210  *aImport = mImport ? PR_TRUE : PR_FALSE;
211  return NS_OK;
212 }
213 
214 NS_IMETHODIMP
215 sbDeviceLibraryMediaSyncSettings::SetImport(PRBool aImport)
216 {
217  NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY);
218 
219  {
220  nsAutoLock lock(mLock);
221  mImport = aImport == PR_TRUE;
222  }
223 
224  return NS_OK;
225 }
226 
227 nsresult
228 sbDeviceLibraryMediaSyncSettings::GetSelectedPlaylistsNoLock(
229  nsIArray ** aSelectedPlaylists)
230 {
231  NS_ENSURE_ARG_POINTER(aSelectedPlaylists);
232  nsresult rv;
233 
234  nsCOMPtr<nsIMutableArray> selected =
235  do_CreateInstance("@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
236 
237  mPlaylistsSelection.EnumerateRead(ArrayBuilder, selected.get());
238 
239  rv = CallQueryInterface(selected, aSelectedPlaylists);
240  NS_ENSURE_SUCCESS(rv, rv);
241 
242  return NS_OK;
243 }
244 
245 NS_IMETHODIMP
246 sbDeviceLibraryMediaSyncSettings::GetSelectedPlaylists(
247  nsIArray ** aSelectedPlaylists)
248 {
249  NS_ENSURE_ARG_POINTER(aSelectedPlaylists);
250  NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY);
251  nsresult rv;
252 
253  nsAutoLock lock(mLock);
254 
255  rv = GetSelectedPlaylistsNoLock(aSelectedPlaylists);
256  NS_ENSURE_SUCCESS(rv, rv);
257 
258  return NS_OK;
259 }
260 
261 NS_IMETHODIMP
262 sbDeviceLibraryMediaSyncSettings::SetSelectedPlaylists(
263  nsIArray * aSelectedPlaylists)
264 {
265  NS_ENSURE_ARG_POINTER(aSelectedPlaylists);
266  NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY);
267 
268  nsresult rv;
269 
270  {
271  nsAutoLock lock(mLock);
272 
273  mPlaylistsSelection.Enumerate(ResetSelection, nsnull);
274 
275  nsCOMPtr<nsISupports> medialist;
276  PRUint32 length;
277  rv = aSelectedPlaylists->GetLength(&length);
278  NS_ENSURE_SUCCESS(rv, rv);
279 
280  nsCOMPtr<nsISupports> supports;
281  for (PRUint32 index = 0; index < length; ++index) {
282  supports = do_QueryElementAt(aSelectedPlaylists, index, &rv);
283  NS_ENSURE_SUCCESS(rv, rv);
284  mPlaylistsSelection.Put(supports, PR_TRUE);
285  }
286  }
287 
288  return NS_OK;
289 }
290 
291 NS_IMETHODIMP
292 sbDeviceLibraryMediaSyncSettings::SetPlaylistSelected(sbIMediaList *aPlaylist,
293  PRBool aSelected)
294 {
295  NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY);
296 
297  {
298  nsAutoLock lock(mLock);
299  nsCOMPtr<nsISupports> supports = do_QueryInterface(aPlaylist);
300  mPlaylistsSelection.Put(supports, aSelected);
301  }
302 
303  return NS_OK;
304 }
305 
306 NS_IMETHODIMP
307 sbDeviceLibraryMediaSyncSettings::GetPlaylistSelected(sbIMediaList *aPlaylist,
308  PRBool * aSelected)
309 {
310  NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY);
311  nsAutoLock lock(mLock);
312 
313  nsCOMPtr<nsISupports> supports = do_QueryInterface(aPlaylist);
314  PRBool exists = mPlaylistsSelection.Get(supports, aSelected);
315  if (!exists) {
316  *aSelected = PR_FALSE;
317  }
318  return NS_OK;
319 }
320 
321 NS_IMETHODIMP
322 sbDeviceLibraryMediaSyncSettings::ClearSelectedPlaylists()
323 {
324  NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY);
325 
326  {
327  nsAutoLock lock(mLock);
328  mPlaylistsSelection.Enumerate(ResetSelection, nsnull);
329  }
330 
331  return NS_OK;
332 }
333 
334 NS_IMETHODIMP
335 sbDeviceLibraryMediaSyncSettings::GetSyncFolder(nsAString & aSyncFolder)
336 {
337  NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY);
338  nsAutoLock lock(mLock);
339 
340  aSyncFolder = mSyncFolder;
341 
342  return NS_OK;
343 }
344 
345 NS_IMETHODIMP
346 sbDeviceLibraryMediaSyncSettings::SetSyncFolder(const nsAString & aSyncFolder)
347 {
348  NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY);
349 
350  {
351  nsAutoLock lock(mLock);
352  mSyncFolder = aSyncFolder;
353  }
354 
355  return NS_OK;
356 }
357 
358 NS_IMETHODIMP
359 sbDeviceLibraryMediaSyncSettings::GetSyncFromFolder(nsIFile ** aSyncFromFolder)
360 {
361  NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY);
362  nsAutoLock lock(mLock);
363 
364  nsresult rv;
365 
366  *aSyncFromFolder = nsnull;
367 
368  if (!mSyncFromFolder) {
369  nsCOMPtr<nsIProperties> directorySvc =
370  do_GetService("@mozilla.org/file/directory_service;1", &rv);
371  NS_ENSURE_SUCCESS(rv, rv);
372 
373  PRBool hasProperty;
374  rv = directorySvc->Has("Pics", &hasProperty);
375  NS_ENSURE_SUCCESS(rv, rv);
376  if (!hasProperty)
377  return NS_OK;
378 
379  rv = directorySvc->Get("Pics",
380  NS_GET_IID(nsIFile),
381  getter_AddRefs(mSyncFromFolder));
382  NS_ENSURE_SUCCESS(rv, rv);
383 
384  if (!mSyncFromFolder)
385  return NS_OK;
386  }
387 
388  rv = mSyncFromFolder->Clone(aSyncFromFolder);
389  NS_ENSURE_SUCCESS(rv, rv);
390  return NS_OK;
391 }
392 
393 NS_IMETHODIMP
394 sbDeviceLibraryMediaSyncSettings::SetSyncFromFolder(
395  nsIFile * aSyncFromFolder)
396 {
397  NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY);
398 
399  {
400  nsAutoLock lock(mLock);
401  nsresult rv = aSyncFromFolder->Clone(getter_AddRefs(mSyncFromFolder));
402  NS_ENSURE_SUCCESS(rv, rv);
403  }
404 
405  return NS_OK;
406 }
407 
408 nsresult
409 sbDeviceLibraryMediaSyncSettings::GetSyncPlaylistsNoLock(
410  nsIArray ** aSyncPlaylists)
411 {
412  NS_ENSURE_ARG_POINTER(aSyncPlaylists);
413 
414  nsresult rv;
415 
416  // Conver the media type to the content type
417  PRUint32 contentType;
418  switch (mMediaType) {
420  contentType = sbIMediaList::CONTENTTYPE_AUDIO;
421  break;
423  contentType = sbIMediaList::CONTENTTYPE_VIDEO;
424  break;
425  default:
426  return NS_ERROR_NOT_AVAILABLE;
427  }
428  nsCOMPtr<sbILibrary> library;
429  rv = GetMainLibrary(getter_AddRefs(library));
430  NS_ENSURE_SUCCESS(rv, rv);
431 
433  contentType,
434  aSyncPlaylists);
435  NS_ENSURE_SUCCESS(rv, rv);
436 
437  return NS_OK;
438 }
439 
440 NS_IMETHODIMP
441 sbDeviceLibraryMediaSyncSettings::GetSyncPlaylists(nsIArray ** aSyncPlaylists)
442 {
443  NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY);
444 
445  nsAutoLock lock(mLock);
446  return GetSyncPlaylistsNoLock(aSyncPlaylists);
447 }
448 
449 NS_IMETHODIMP
450 sbDeviceLibraryMediaSyncSettings::AddPlaylistListener(sbIDeviceLibraryPlaylistListener *aListener)
451 {
452  NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY);
453  return NS_ERROR_NOT_IMPLEMENTED;
454 }
455 
456 NS_IMETHODIMP
457 sbDeviceLibraryMediaSyncSettings::RemovePlaylistListener(sbIDeviceLibraryPlaylistListener *aListener)
458 {
459  NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY);
460  return NS_ERROR_NOT_IMPLEMENTED;
461 }
return NS_OK
sbDeviceLibraryMediaSyncSettings::PlaylistSelection Hashtable
inArray array
static nsresult GetMediaListByContentType(sbILibrary *aLibrary, PRUint32 aContentType, nsIArray **aMediaLists)
A brief description of the contents of this interface.
const unsigned long MEDIATYPE_AUDIO
const unsigned short CONTENTTYPE_AUDIO
const unsigned long MEDIATYPE_VIDEO
NS_IMPL_ISUPPORTS1(sbDeviceLibraryMediaSyncSettings, sbIDeviceLibraryMediaSyncSettings)
const unsigned short CONTENTTYPE_VIDEO
PLDHashOperator ArrayBuilder(nsISupports *aKey, PRBool aData, void *userArg)
this _document false
Definition: FeedWriter.js:1085
nsDataHashtable< nsISupportsHashKey, PRBool > PlaylistSelection
nsresult GetMainLibrary(sbILibrary **aMainLibrary)
_updateTextAndScrollDataForFrame aData
PLDHashOperator ResetSelection(nsISupports *aKey, PRBool &aData, void *userArg)