52 #include <sbIDeviceEvent.h>
56 #include <nsArrayUtils.h>
57 #include <nsThreadUtils.h>
58 #include <nsILocalFile.h>
82 sbIPDDevice::ProcessBatch(Batch & aBatch)
88 NS_ENSURE_TRUE(mConnected, NS_ERROR_NOT_AVAILABLE);
103 if (aBatch.empty()) {
107 PRUint32 batchType = aBatch.RequestType();
112 StatsUpdate(PR_TRUE);
114 PRUint32 deviceState;
116 NS_ENSURE_SUCCESS(rv, rv);
125 for (PRUint32 batchIndex = 0;
126 batchIndex < NS_ARRAY_LENGTH(batches);
128 Batch & batch = batches[batchIndex];
138 TransferRequest * request =
static_cast<TransferRequest *
>(*iter);
141 PRUint32 type = request->GetType();
143 FIELD_LOG((
"Processing request 0x%08x\n", type));
148 ReqHandleMount(request);
154 ReqHandleWrite(request, batch.CountableItems());
160 ReqHandleDelete(request, batch.CountableItems());
165 ReqHandleWipe(request);
170 ReqHandleNewPlaylist(request);
175 ReqHandleUpdate(request);
180 ReqHandleMovePlaylistTrack(request);
190 ReqHandleFactoryReset(request);
193 case REQUEST_WRITE_PREFS :
195 ReqHandleWritePrefs(request);
198 case REQUEST_SET_PROPERTY :
200 ReqHandleSetProperty(request);
203 case REQUEST_SET_PREF :
204 ReqHandleSetPref(static_cast<SetNamedValueRequest*>(request));
208 NS_WARNING(
"Invalid request type.");
216 request->SetIsProcessed(
true);
234 sbIPDDevice::ReqHandleMount(TransferRequest * aRequest)
237 NS_ASSERTION(aRequest,
"aRequest is null");
242 const PRUint32 batchIndex = aRequest->GetBatchIndex() + 1;
249 sbAutoStatusOperationFailure autoStatus(mIPDStatus);
252 StatsUpdate(PR_TRUE);
261 rv = ProcessOTGPlaylists();
262 NS_ENSURE_SUCCESS(rv, );
265 rv = ImportDatabase();
266 NS_ENSURE_SUCCESS(rv, );
269 NS_ENSURE_SUCCESS(rv, );
273 NS_LITERAL_STRING(
"0"));
274 NS_ENSURE_SUCCESS(rv, );
290 StatsUpdate(PR_TRUE);
307 sbIPDDevice::ReqHandleWrite(TransferRequest * aRequest, PRUint32 aBatchCount)
310 NS_ASSERTION(aRequest,
"aRequest is null");
313 if (aRequest->IsPlaylist())
314 ReqHandleWritePlaylistTrack(aRequest);
316 ReqHandleWriteTrack(aRequest, aBatchCount);
329 sbIPDDevice::ReqHandleWriteTrack(TransferRequest * aRequest,
330 PRUint32 aBatchCount)
333 NS_ASSERTION(aRequest,
"aRequest is null");
338 const PRUint32 batchIndex = aRequest->GetBatchIndex() + 1;
341 sbAutoStatusOperationComplete autoOperationStatus(mIPDStatus, NS_ERROR_FAILURE);
342 if (batchIndex == 1) {
346 if (batchIndex == aBatchCount) {
347 autoOperationStatus.Set(mIPDStatus,
NS_OK);
350 sbAutoStatusItemFailure autoItemStatus(mIPDStatus);
358 NS_ENSURE_SUCCESS(rv, );
360 rv =
DeleteItem(aRequest->list, aRequest->item);
361 NS_ENSURE_SUCCESS(rv, );
366 rv = UploadTrack(aRequest->item);
367 NS_ENSURE_SUCCESS(rv, );
370 autoItemStatus.forget();
384 sbIPDDevice::ReqHandleWritePlaylistTrack(TransferRequest* aRequest)
387 NS_ASSERTION(aRequest,
"aRequest is null");
393 FIELD_LOG((
"Add track to playlist at index %d.\n", aRequest->index));
396 rv = PlaylistAddTrack(aRequest->list, aRequest->item, aRequest->index);
397 NS_ENSURE_SUCCESS(rv, );
410 sbIPDDevice::ReqHandleWipe(TransferRequest * aRequest)
413 NS_ASSERTION(aRequest,
"aRequest is null");
419 nsCOMPtr<sbILibrary> library = do_QueryInterface(aRequest->item, &rv);
420 if (NS_SUCCEEDED(rv)) {
422 NS_ENSURE_SUCCESS(rv, );
426 nsCOMPtr<sbIMediaList> mediaList = do_QueryInterface(aRequest->item, &rv);
427 if (NS_SUCCEEDED(rv)) {
428 rv = PlaylistWipe(mediaList);
429 NS_ENSURE_SUCCESS(rv, );
432 NS_WARNING(
"Unexpected data provided to REQUEST_WIPE");
446 sbIPDDevice::ReqHandleDelete(TransferRequest* aRequest,
447 PRUint32 aBatchCount)
450 NS_ASSERTION(aRequest,
"aRequest is null");
456 PRBool deletePlaylist;
457 nsCOMPtr<sbIMediaList> mediaList = do_QueryInterface(aRequest->item, &rv);
458 if (NS_SUCCEEDED(rv))
459 deletePlaylist = PR_TRUE;
461 deletePlaylist = PR_FALSE;
464 if (deletePlaylist) {
465 ReqHandleDeletePlaylist(aRequest);
467 if (aRequest->IsPlaylist())
468 ReqHandleDeletePlaylistTrack(aRequest);
470 ReqHandleDeleteTrack(aRequest, aBatchCount);
484 sbIPDDevice::ReqHandleDeleteTrack(TransferRequest * aRequest,
485 PRUint32 aBatchCount)
488 NS_ASSERTION(aRequest,
"aRequest is null");
493 const PRUint32 batchIndex = aRequest->GetBatchIndex() + 1;
497 sbAutoStatusOperationComplete autoOperationStatus(mIPDStatus, NS_ERROR_FAILURE);
498 if (batchIndex == 1) {
502 if (batchIndex == aBatchCount) {
503 autoOperationStatus.Set(mIPDStatus,
NS_OK);
506 sbAutoStatusItemFailure autoItemStatus(mIPDStatus);
509 rv = DeleteTrack(aRequest->item);
510 NS_ENSURE_SUCCESS(rv, );
513 autoItemStatus.forget();
527 sbIPDDevice::ReqHandleDeletePlaylistTrack(TransferRequest * aRequest)
530 NS_ASSERTION(aRequest,
"aRequest is null");
536 FIELD_LOG((
"Delete track from playlist at index %d.\n", aRequest->index));
539 rv = PlaylistRemoveTrack(aRequest->list, aRequest->item, aRequest->index);
540 NS_ENSURE_SUCCESS(rv, );
553 sbIPDDevice::ReqHandleDeletePlaylist(TransferRequest * aRequest)
556 NS_ASSERTION(aRequest,
"aRequest is null");
565 nsCOMPtr<sbIMediaList> mediaList = do_QueryInterface(aRequest->item, &rv);
566 NS_ENSURE_SUCCESS(rv, );
567 rv = PlaylistDelete(mediaList);
568 NS_ENSURE_SUCCESS(rv, );
581 sbIPDDevice::ReqHandleNewPlaylist(TransferRequest * aRequest)
584 NS_ASSERTION(aRequest,
"aRequest is null");
593 Itdb_Playlist* playlist;
594 nsCOMPtr<sbIMediaList> mediaList = do_QueryInterface(aRequest->item, &rv);
595 NS_ENSURE_SUCCESS(rv, );
596 rv = PlaylistAdd(mediaList, &playlist);
597 NS_ENSURE_SUCCESS(rv, );
610 sbIPDDevice::ReqHandleUpdate(TransferRequest * aRequest)
613 NS_ASSERTION(aRequest,
"aRequest is null");
619 nsCOMPtr<sbIMediaItem> mediaItem = aRequest->item;
620 nsCOMPtr<sbIMediaList> mediaList = do_QueryInterface(mediaItem);
623 nsCOMPtr<sbILibrary> library;
625 rv = mediaItem->GetLibrary(getter_AddRefs(library));
626 NS_ENSURE_SUCCESS(rv, );
627 rv = library->Contains(mediaItem, &exists);
628 NS_ENSURE_SUCCESS(rv, );
637 rv = PlaylistUpdateProperties(mediaList);
639 rv = TrackUpdateProperties(mediaItem);
640 NS_ENSURE_SUCCESS(rv, );
653 sbIPDDevice::ReqHandleMovePlaylistTrack(TransferRequest * aRequest)
656 NS_ASSERTION(aRequest,
"aRequest is null");
662 FIELD_LOG((
"Move playlist track from index %d to index %d.\n",
664 aRequest->otherIndex));
667 NS_ENSURE_TRUE(aRequest->list, );
670 rv = PlaylistMoveTrack(aRequest->list,
672 aRequest->otherIndex);
673 NS_ENSURE_SUCCESS(rv, );
686 sbIPDDevice::ReqHandleFactoryReset(TransferRequest * aRequest)
689 NS_ASSERTION(aRequest,
"aRequest is null");
697 nsCOMPtr<nsILocalFile> mountDir =
698 do_CreateInstance(
"@mozilla.org/file/local;1");
699 mountDir->InitWithPath(mMountPath);
701 nsCOMPtr<nsISimpleEnumerator> files;
702 rv = mountDir->GetDirectoryEntries(getter_AddRefs(files));
703 NS_ENSURE_SUCCESS(rv, );
706 while (NS_SUCCEEDED(files->HasMoreElements(&hasMore)) && hasMore) {
707 nsCOMPtr<nsIFile>
file;
708 rv = files->GetNext(getter_AddRefs(file));
709 NS_ENSURE_SUCCESS(rv, );
715 FIELD_LOG((
"sbIPDDevice::ReqHandleFactoryReset deleting directory %s\n",
716 NS_LossyConvertUTF16toASCII(path).
get()));
719 rv = file->Remove(PR_TRUE);
720 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Unable to delete directory");
737 sbIPDDevice::ReqHandleWritePrefs(TransferRequest * aRequest)
740 NS_ASSERTION(aRequest,
"aRequest is null");
743 GError* gError = NULL;
751 nsAutoLock autoPrefLock(mPrefLock);
754 if (mIPodPrefsDirty) {
755 success = itdb_prefs_write(mITDB->device, mIPodPrefs, &gError);
759 g_error_free(gError);
763 NS_WARNING(
"Failed to write the iPod preferences file.");
765 mIPodPrefsDirty = PR_FALSE;
768 if (mSyncPlaylistListDirty) {
769 success = itdb_update_playlists_write(mITDB->device,
770 mSyncPlaylistList.Elements(),
771 mSyncPlaylistList.Length(),
776 g_error_free(gError);
780 NS_WARNING(
"Failed to write the iPod sync playlist file.");
782 mSyncPlaylistListDirty = PR_FALSE;
795 sbIPDDevice::ReqHandleSetProperty(TransferRequest * aRequest)
798 NS_ASSERTION(aRequest,
"aRequest is null");
803 SetNamedValueRequest* request =
static_cast<SetNamedValueRequest*
>(aRequest);
806 if (request->name.EqualsLiteral(
"FriendlyName")) {
808 nsAutoString friendlyName;
809 rv = request->value->GetAsAString(friendlyName);
810 NS_ENSURE_SUCCESS(rv, );
813 gchar* masterPlaylistName =
814 g_strdup(NS_ConvertUTF16toUTF8(friendlyName).
get());
815 NS_ENSURE_TRUE(masterPlaylistName, );
816 if (mMasterPlaylist->name)
817 g_free(mMasterPlaylist->name);
818 mMasterPlaylist->name = masterPlaylistName;
821 mITDBDirty = PR_TRUE;
835 sbIPDDevice::ReqHandleSetPref(SetNamedValueRequest * aRequest)
838 NS_ASSERTION(aRequest,
"aRequest is null");
845 if (aRequest->name.EqualsLiteral(
"SyncPartner")) {
847 nsAutoString sbSyncPartnerID;
848 rv = aRequest->value->GetAsAString(sbSyncPartnerID);
849 NS_ENSURE_SUCCESS(rv, );
853 guint64 iPodSyncPartnerID;
854 rv = IDMapGet(sbSyncPartnerID, &iPodSyncPartnerID);
855 if (rv == NS_ERROR_NOT_AVAILABLE) {
856 iPodSyncPartnerID = (((guint64) g_random_int()) << 32) |
858 IDMapAdd(sbSyncPartnerID, iPodSyncPartnerID);
860 NS_ENSURE_SUCCESS(rv, );
865 nsAutoLock autoPrefLock(mPrefLock);
866 mIPodPrefs->music_lib_link_id = iPodSyncPartnerID;
867 mIPodPrefsDirty = PR_TRUE;
872 NS_ENSURE_SUCCESS(rv, );
893 sbIPDDevice::ReqPushSetNamedValue(
int aType,
894 const nsAString&
aName,
898 NS_ASSERTION(aValue,
"aValue is null");
904 SetNamedValueRequest * request =
new SetNamedValueRequest(aType);
905 NS_ENSURE_TRUE(request, NS_ERROR_OUT_OF_MEMORY);
906 request->name =
aName;
913 NS_ENSURE_SUCCESS(rv, rv);
const unsigned long REQUEST_FACTORY_RESET
nsresult CreateAndDispatchEvent(PRUint32 aType, nsIVariant *aData, PRBool aAsync=PR_TRUE, sbIDeviceEventTarget *aTarget=nsnull)
void OperationComplete(nsresult aResult)
const unsigned long STATE_DELETING
void ItemComplete(nsresult aResult)
nsRefPtr< sbDeviceRequestThreadQueue > mRequestThreadQueue
nsresult ListenToMediaLists(sbILibrary *aLibrary)
friend class sbIPDAutoIdle
nsresult ChangeStatus(PRUint32 newState)
const unsigned long STATE_UPDATING
#define SB_PROPERTY_HIDDEN
nsRefPtr< sbBaseDeviceLibraryListener > mLibraryListener
virtual PRBool IsRequestAborted()
NS_IMETHOD GetState(PRUint32 *aState)
const unsigned long EVENT_DEVICE_READY
nsresult PushRequest(const PRUint32 aType, sbIMediaItem *aItem=nsnull, sbIMediaList *aList=nsnull, PRUint32 aIndex=PR_UINT32_MAX, PRUint32 aOtherIndex=PR_UINT32_MAX, nsISupports *aData=nsnull)
friend class sbIPDAutoDBFlush
NS_IMETHOD SupportsMediaItem(sbIMediaItem *aMediaItem, sbIDeviceSupportsItemCallback *aCallback)
RequestItems::const_iterator const_iterator
const unsigned long STATE_COPYING
Songbird iPod Device Definitions.
static const PRUint32 OPERATION_TYPE_DELETE
Songbird iPod Device Logging Definitions.
const unsigned long STATE_MOUNTING
nsresult DeleteItem(sbIMediaList *aLibrary, sbIMediaItem *aItem)
const unsigned long STATE_SYNCING
StringArrayEnumerator prototype hasMore
sbRequestThreadQueue::Batch Batch
nsresult HandleSyncRequest(TransferRequest *aRequest)
static const PRUint32 OPERATION_TYPE_MOUNT
void SBWriteRequestSplitBatches(const sbBaseDevice::Batch &aInput, sbBaseDevice::Batch &aNonTranscodeItems, sbBaseDevice::Batch &aTranscodeItems, sbBaseDevice::Batch &aPlaylistItems)
const unsigned long STATE_BUSY
static const PRUint32 OPERATION_TYPE_WRITE
void OperationStart(PRUint32 aOperationType, sbBaseDevice::TransferRequest *aRequest, PRUint32 aBatchCount)