sbDeviceImport.cpp
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4  *=BEGIN SONGBIRD GPL
5  *
6  * This file is part of the Songbird web player.
7  *
8  * Copyright(c) 2005-2011 POTI, Inc.
9  * http://www.songbirdnest.com
10  *
11  * This file may be licensed under the terms of of the
12  * GNU General Public License Version 2 (the ``GPL'').
13  *
14  * Software distributed under the License is distributed
15  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied. See the GPL for the specific language
17  * governing rights and limitations.
18  *
19  * You should have received a copy of the GPL along with this
20  * program. If not, go to http://www.gnu.org/licenses/gpl.html
21  * or write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  *=END SONGBIRD GPL
25  */
26 
27 #include "sbBaseDevice.h"
28 
29 // Local includes
30 #include "sbDeviceUtils.h"
31 
32 // Mozilla includes
33 #include <nsArrayUtils.h>
34 #include <nsCOMArray.h>
35 
36 // Songbird Includes
37 #include <sbStandardProperties.h>
38 
39 // Songbird interfaces
40 #include <sbILibraryChangeset.h>
41 #include <sbIDeviceLibraryMediaSyncSettings.h>
42 #include <sbIDeviceLibrarySyncDiff.h>
43 #include <sbIDeviceLibrarySyncSettings.h>
44 #include <sbIMediaItem.h>
45 #include <sbIMediaList.h>
46 
47 NS_IMETHODIMP
49  sbILibraryChangeset * aImportChangeset)
50 {
51  NS_ENSURE_ARG_POINTER(aImportToLibrary);
52  NS_ENSURE_ARG_POINTER(aImportChangeset);
53 
54  nsresult rv;
55 
56  // Get the list of all changes.
57  nsCOMPtr<nsIArray> changeList;
58  rv = aImportChangeset->GetChanges(getter_AddRefs(changeList));
59  NS_ENSURE_SUCCESS(rv, rv);
60 
61  PRUint32 changeCount;
62  rv = changeList->GetLength(&changeCount);
63  NS_ENSURE_SUCCESS(rv, rv);
64 
65  // If there aren't any changes, don't bother setting up for the import
66  if (changeCount == 0) {
67  return NS_OK;
68  }
69  // Accumulators so we can batch the operations
70  nsCOMPtr<nsIMutableArray> mediaItemsToAdd =
71  do_CreateInstance("@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
72  NS_ENSURE_SUCCESS(rv, rv);
73  nsCOMPtr<nsIMutableArray> mediaListsToAdd =
74  do_CreateInstance("@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
75  NS_ENSURE_SUCCESS(rv, rv);
76  nsCOMPtr<nsIMutableArray> mediaItemsToRemove =
77  do_CreateInstance("@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
78  // Determine if playlists are supported
79  NS_ENSURE_SUCCESS(rv, rv);
80  nsCOMPtr<nsIMutableArray> mediaListsToUpdate =
81  do_CreateInstance("@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
82  NS_ENSURE_SUCCESS(rv, rv);
83 
84  bool const playlistsSupported = sbDeviceUtils::ArePlaylistsSupported(this);
85 
86 
87  // Accumulate changes for later processing to take advantage of batching
88  for (PRUint32 i = 0; i < changeCount; i++) {
89  if (IsRequestAborted()) {
90  return NS_ERROR_ABORT;
91  }
92  // Get the next change.
93  nsCOMPtr<sbILibraryChange> change = do_QueryElementAt(changeList, i, &rv);
94  NS_ENSURE_SUCCESS(rv, rv);
95 
96  // Put the change into the appropriate lists.
97  PRUint32 operation;
98  rv = change->GetOperation(&operation);
99  NS_ENSURE_SUCCESS(rv, rv);
100 
101  // Is this a media item list
102  PRBool itemIsList;
103  rv = change->GetItemIsList(&itemIsList);
104  NS_ENSURE_SUCCESS(rv, rv);
105 
106  // if this is a playlist and they're not supported ignore the change
107  if (itemIsList && !playlistsSupported) {
108  continue;
109  }
110 
111  switch (operation) {
113  // Get the source item to add.
114  nsCOMPtr<sbIMediaItem> mediaItem;
115  rv = change->GetSourceItem(getter_AddRefs(mediaItem));
116  NS_ENSURE_SUCCESS(rv, rv);
117 
118  if (itemIsList) {
119  rv = mediaListsToAdd->AppendElement(change, PR_FALSE);
120  NS_ENSURE_SUCCESS(rv, rv);
121  } else {
122  rv = mediaItemsToAdd->AppendElement(mediaItem, PR_FALSE);
123  NS_ENSURE_SUCCESS(rv, rv);
124  }
125  }
126  break;
128  nsCOMPtr<sbIMediaItem> destItem;
129  rv = change->GetDestinationItem(getter_AddRefs(destItem));
130  NS_ENSURE_SUCCESS(rv, rv);
131 
132  nsCOMPtr<sbIMediaList> destItemAsList =
133  do_QueryInterface(destItem);
134 
135  if (destItemAsList) {
136  // If the change is to a media list, add it to the media list change
137  // list.
138  PRBool success = mediaListsToUpdate->AppendElement(change, PR_FALSE);
139  NS_ENSURE_SUCCESS(success, NS_ERROR_OUT_OF_MEMORY);
140 
141  }
142  else {
143  rv = mediaItemsToRemove->AppendElement(destItem, PR_FALSE);
144  NS_ENSURE_SUCCESS(rv, rv);
145 
146  nsCOMPtr<sbIMediaItem> sourceItem;
147  rv = change->GetSourceItem(getter_AddRefs(sourceItem));
148 
149  rv = mediaItemsToAdd->AppendElement(sourceItem, PR_FALSE);
150  NS_ENSURE_SUCCESS(rv, rv);
151  }
152  }
153  break;
154  default: {
155  NS_WARNING("Unexpected change operation value in sbBaseDevice::Import");
156  }
157  break;
158  }
159  }
160 
161  // Remove items that need to be removed
162  nsCOMPtr<nsISimpleEnumerator> enumerator;
163  rv = mediaItemsToRemove->Enumerate(getter_AddRefs(enumerator));
164  NS_ENSURE_SUCCESS(rv, rv);
165 
166  rv = aImportToLibrary->RemoveSome(enumerator);
167  NS_ENSURE_SUCCESS(rv, rv);
168 
169  // Add media lists.
170  rv = mediaItemsToAdd->Enumerate(getter_AddRefs(enumerator));
171  NS_ENSURE_SUCCESS(rv, rv);
172 
173  rv = aImportToLibrary->AddSome(enumerator);
174  NS_ENSURE_SUCCESS(rv, rv);
175 
176  rv = AddMediaLists(aImportToLibrary, mediaListsToAdd);
177  NS_ENSURE_SUCCESS(rv, rv);
178 
179  rv = UpdateMediaLists(mediaListsToUpdate);
180  NS_ENSURE_SUCCESS(rv, rv);
181 
182  return NS_OK;
183 }
return NS_OK
nsresult AddMediaLists(sbILibrary *aLibrary, nsIArray *aMediaLists)
const NS_ERROR_ABORT
virtual PRBool IsRequestAborted()
static bool ArePlaylistsSupported(sbIDevice *aDevice)
const unsigned long MODIFIED
Media library abstraction.
Definition: sbILibrary.idl:82
nsresult UpdateMediaLists(nsIArray *aMediaLists)
NS_IMETHOD ImportFromDevice(sbILibrary *aImportToLibrary, sbILibraryChangeset *aImportChangeset)
_getSelectedPageStyle s i
const unsigned long ADDED