sbIPDTrack.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 //------------------------------------------------------------------------------
28 //
29 // iPod device track services.
30 //
31 // These services may only be used within both the connect and request locks.
32 //
33 //------------------------------------------------------------------------------
34 
40 //------------------------------------------------------------------------------
41 //
42 // iPod device track imported services.
43 //
44 //------------------------------------------------------------------------------
45 
46 // Local imports.
47 #include "sbIPDDevice.h"
48 #include "sbIPDLog.h"
49 #include "sbIPDUtils.h"
50 
51 // Songbird imports.
52 #include <sbDeviceUtils.h>
53 #include <sbIDeviceEvent.h>
54 #include <sbIPropertyArray.h>
55 #include <sbIPropertyInfo.h>
58 #include <sbStandardProperties.h>
59 #include <sbStringUtils.h>
60 
61 // Mozila imports.
62 #include <nsArrayUtils.h>
63 #include <nsILocalFile.h>
64 #include <nsIMutableArray.h>
65 #include <nsIURI.h>
66 #include <nsIURL.h>
67 #include <nsTArray.h>
68 #include <prprf.h>
69 
70 // glib imports.
71 #include <glib/gstdio.h>
72 
73 
74 //------------------------------------------------------------------------------
75 //
76 // iPod device track configuration.
77 //
78 //------------------------------------------------------------------------------
79 
80 //
81 // IPOD_DEVICE_BATCH_SIZE Number of tracks to process in a batch.
82 //
83 
84 #define IPOD_DEVICE_BATCH_SIZE 100
85 
86 
87 //------------------------------------------------------------------------------
88 //
89 // iPod device track services.
90 //
91 //------------------------------------------------------------------------------
92 
97 nsresult
98 sbIPDDevice::ImportTracks()
99 {
100  // Allocate the track batch and set it up for auto-disposal.
101  Itdb_Track** trackBatch =
102  static_cast<Itdb_Track**>
103  (NS_Alloc(IPOD_DEVICE_BATCH_SIZE * sizeof (Itdb_Track *)));
104  NS_ENSURE_TRUE(trackBatch, NS_ERROR_OUT_OF_MEMORY);
105  sbAutoNSMemPtr autoTrackBatch(trackBatch);
106 
107  // Import all of the media tracks.
108  int trackCount = itdb_tracks_number(mITDB);
109  GList* trackList = mITDB->tracks;
110  PRUint32 trackNum = 0;
111  PRUint32 batchCount = 0;
112  while (trackList) {
113  // Check for abort.
114  NS_ENSURE_FALSE(IsRequestAborted(), NS_ERROR_ABORT);
115 
116  // Get the track.
117  Itdb_Track* track = (Itdb_Track*) trackList->data;
118  trackList = trackList->next;
119 
120  // Update status.
121  mIPDStatus->ItemStart(track, trackNum, trackCount);
122 
123  // Add the track to the batch.
124  trackBatch[batchCount] = track;
125  batchCount++;
126 
127  // Import the track batch.
128  if ((batchCount >= IPOD_DEVICE_BATCH_SIZE) || (!trackList)) {
129  ImportTrackBatch(trackBatch, batchCount);
130  batchCount = 0;
131  }
132 
133  // Import the next track.
134  trackNum++;
135  }
136 
137  return NS_OK;
138 }
139 
140 
147 nsresult
148 sbIPDDevice::UploadTrack(sbIMediaItem* aMediaItem)
149 {
150  // Validate arguments.
151  NS_ASSERTION(aMediaItem, "aMediaItem is null");
152 
153  // Function variables.
154  nsresult rv;
155 
156  // Add the track to the iPod.
157  Itdb_Track* track;
158  rv = AddTrack(aMediaItem, &track);
159  NS_ENSURE_SUCCESS(rv, rv);
160 
161  // Update the media item content source property to point to the device media
162  // file.
163  nsCOMPtr<nsIURI> trackURI;
164  rv = GetTrackURI(track, getter_AddRefs(trackURI));
165  NS_ENSURE_SUCCESS(rv, rv);
166  rv = aMediaItem->SetContentSrc(trackURI);
167  NS_ENSURE_SUCCESS(rv, rv);
168 
169  // Show the media item.
170  rv = aMediaItem->SetProperty(NS_LITERAL_STRING(SB_PROPERTY_HIDDEN),
171  NS_LITERAL_STRING("0"));
172  NS_ENSURE_SUCCESS(rv, rv);
173 
174  return NS_OK;
175 }
176 
177 
187 nsresult
188 sbIPDDevice::AddTrack(sbIMediaItem* aMediaItem,
189  Itdb_Track** aTrack)
190 {
191  // Validate arguments.
192  NS_ASSERTION(aMediaItem, "aMediaItem is null");
193  NS_ASSERTION(aTrack, "aTrack is null");
194 
195  // Function variables.
196  GError *gError = NULL;
197  nsresult rv;
198 
199  /* Return immediately if media item is not supported. */
200  if (!IsMediaSupported(aMediaItem)) {
201  AddUnsupportedMediaItem(aMediaItem);
202  return NS_ERROR_ILLEGAL_VALUE;
203  }
204 
205  // Create a new iPod track data record and set it up for auto-disposal.
206  Itdb_Track* track = itdb_track_new();
207  NS_ENSURE_TRUE(track, NS_ERROR_OUT_OF_MEMORY);
208  sbIPDAutoTrack autoTrack(track, this);
209 
210  // Set the track properties.
211  SetTrackProperties(track, aMediaItem);
212 
213  // Get the track file.
214  nsCOMPtr<nsIURI> trackURI;
215  nsCAutoString trackSpec;
216  nsCOMPtr<nsIFile> trackFile;
217  rv = aMediaItem->GetContentSrc(getter_AddRefs(trackURI));
218  NS_ENSURE_SUCCESS(rv, rv);
219  rv = trackURI->GetSpec(trackSpec);
220  NS_ENSURE_SUCCESS(rv, rv);
221  rv = mFileProtocolHandler->GetFileFromURLSpec(trackSpec,
222  getter_AddRefs(trackFile));
223  NS_ENSURE_SUCCESS(rv, rv);
224 
225  // Set up any FairPlay info.
226  rv = FPSetupTrackInfo(trackFile, track);
227  NS_ENSURE_SUCCESS(rv, rv);
228 
229  // Get the track path.
230  nsCAutoString trackPath;
231  rv = trackFile->GetNativePath(trackPath);
232  NS_ENSURE_SUCCESS(rv, rv);
233 
234  /* Trace execution. */
235  FIELD_LOG(("1: sbIPDDevice::CreateTrack %s\n", trackPath.get()));
236 
237  // Get the track size.
238  PRInt64 fileSize;
239  rv = trackFile->GetFileSize(&fileSize);
240  NS_ENSURE_SUCCESS(rv, rv);
241  if (fileSize > (1 << 30)) {
242  AddUnsupportedMediaItem(aMediaItem);
243  return NS_ERROR_ILLEGAL_VALUE;
244  }
245  track->size = (guint32) (fileSize & 0xFFFFFFFF);
246 
247  // Add the track to the iPod database.
248  itdb_track_add(mITDB, track, -1);
249 
250  // Add the track to the master playlist.
251  itdb_playlist_add_track(mMasterPlaylist, track, -1);
252 
253  // Copy the track media file to the iPod.
254  if (!itdb_cp_track_to_ipod(track, (gchar *) trackPath.get(), &gError)) {
255  if (gError) {
256  if (gError->message)
257  FIELD_LOG(("%s", gError->message));
258  g_error_free(gError);
259  gError = NULL;
260  }
261  NS_ENSURE_TRUE(PR_FALSE, NS_ERROR_FAILURE);
262  }
263  mIPDStatus->ItemProgress(1.0);
264 
265  // Set the track media item device persistent ID.
266  mLibraryListener->IgnoreMediaItem(aMediaItem);
267  rv = aMediaItem->SetProperty
268  (NS_LITERAL_STRING(SB_PROPERTY_DEVICE_PERSISTENT_ID),
269  sbAutoString(track->dbid));
270  mLibraryListener->UnignoreMediaItem(aMediaItem);
271  NS_ENSURE_SUCCESS(rv, rv);
272 
273  // Check for track FairPlay authorization. Do this last so the FairPlay
274  // information can be read from the track and a media item can be obtained
275  // from the track.
276  FPCheckTrackAuth(track);
277 
278  // Update statistics.
279  StatsUpdate(PR_FALSE);
280 
281  // Mark the iPod database as dirty.
282  mITDBDirty = PR_TRUE;
283 
284  // Return results.
285  *aTrack = track;
286  autoTrack.forget();
287 
288  return NS_OK;
289 }
290 
291 
297 nsresult
298 sbIPDDevice::DeleteTrack(sbIMediaItem* aMediaItem)
299 {
300  // Validate arguments.
301  NS_ASSERTION(aMediaItem, "aMediaItem is null");
302 
303  // Function variables.
304  nsresult rv;
305 
306  // Get the iPod track to delete.
307  Itdb_Track* track;
308  rv = GetTrack(aMediaItem, &track);
309  NS_ENSURE_SUCCESS(rv, rv);
310 
311  // Delete the track.
312  return DeleteTrack(track);
313 }
314 
315 
322 nsresult
323 sbIPDDevice::DeleteTrack(Itdb_Track* aTrack)
324 {
325  // Validate arguments.
326  NS_ASSERTION(aTrack, "aTrack is null");
327 
328  // Get the track database.
329  Itdb_iTunesDB* itdb = aTrack->itdb;
330 
331  // Remove track from all playlists.
332  RemoveTrackFromAllPlaylists(aTrack);
333 
334  // Remove track from the iPod.
335  if (itdb) {
336  // Remove track file.
337  gchar* fileName = itdb_filename_on_ipod(aTrack);
338  if (fileName) {
339  g_unlink(fileName);
340  g_free(fileName);
341  }
342 
343  // Remove track from database.
344  itdb_track_remove(aTrack);
345  }
346 
347  // Update statistics.
348  StatsUpdate(PR_FALSE);
349 
350  // Mark the iPod database as dirty.
351  mITDBDirty = PR_TRUE;
352 
353  // Update item progress.
354  mIPDStatus->ItemProgress(1.0);
355 
356  return NS_OK;
357 }
358 
362 nsresult
363 sbIPDDevice::WipeDevice() {
364 
365  // Import all of the media tracks.
366  int trackCount = itdb_tracks_number(mITDB);
367  GList* trackList = mITDB->tracks;
368  PRUint32 trackNum = 0;
369  while (trackList) {
370  // Check for abort.
371  NS_ENSURE_FALSE(IsRequestAborted(), NS_ERROR_ABORT);
372 
373  // Get the track.
374  Itdb_Track* track = (Itdb_Track*) trackList->data;
375  trackList = trackList->next;
376 
377  // Update status.
378  mIPDStatus->ItemStart(track, trackNum, trackCount);
379 
380  // Ignore error and hope for the best
381  DeleteTrack(track);
382  // Import the next track.
383  ++trackNum;
384  }
385 
386  return NS_OK;
387 }
388 
397 nsresult
398 sbIPDDevice::GetTrack(sbIMediaItem* aMediaItem,
399  Itdb_Track** aTrack)
400 {
401  // Validate arguments.
402  NS_ASSERTION(aMediaItem, "aMediaItem is null");
403  NS_ASSERTION(aTrack, "aTrack is null");
404 
405  // Function variables.
406  Itdb_Track* track;
407  nsresult rv;
408 
409  // Get the iPod ID.
410  guint64 iPodID;
411  rv = GetIPodID(aMediaItem, &iPodID);
412  NS_ENSURE_SUCCESS(rv, rv);
413 
414  // Get the track.
415  track = itdb_track_by_dbid(mITDB, iPodID);
416  NS_ENSURE_TRUE(track, NS_ERROR_NOT_AVAILABLE);
417 
418  // Return results.
419  *aTrack = track;
420 
421  return NS_OK;
422 }
423 
424 
425 //------------------------------------------------------------------------------
426 //
427 // iPod device track property services.
428 //
429 //------------------------------------------------------------------------------
430 
439 void
440 sbIPDDevice::SetTrackProperties(Itdb_Track* aTrack,
441  sbIMediaItem* aMediaItem)
442 {
443  // Validate arguments.
444  NS_ASSERTION(aTrack, "aTrack is null");
445  NS_ASSERTION(aMediaItem, "aMediaItem is null");
446 
447  // Get the track properties from the media item and set them in the iPod
448  // track.
449  GetTrackProp (aMediaItem, SB_PROPERTY_TRACKNAME, &(aTrack->title));
450  GetTrackProp (aMediaItem, SB_PROPERTY_ALBUMNAME, &(aTrack->album));
451  GetTrackProp (aMediaItem, SB_PROPERTY_ARTISTNAME, &(aTrack->artist));
452  GetTrackProp (aMediaItem, SB_PROPERTY_GENRE, &(aTrack->genre));
453  GetTrackProp (aMediaItem, SB_PROPERTY_COMPOSERNAME, &(aTrack->composer));
454  GetTrackPropDur(aMediaItem, SB_PROPERTY_DURATION, &(aTrack->tracklen));
455  GetTrackProp (aMediaItem, SB_PROPERTY_DISCNUMBER, &(aTrack->cd_nr));
456  GetTrackProp (aMediaItem, SB_PROPERTY_TOTALDISCS, &(aTrack->cds));
457  GetTrackProp (aMediaItem, SB_PROPERTY_TRACKNUMBER, &(aTrack->track_nr));
458  GetTrackProp (aMediaItem, SB_PROPERTY_TOTALTRACKS, &(aTrack->tracks));
459  GetTrackProp (aMediaItem, SB_PROPERTY_YEAR, &(aTrack->year));
460  GetTrackPropRating(aMediaItem, SB_PROPERTY_RATING, &(aTrack->rating));
461  GetTrackProp (aMediaItem, SB_PROPERTY_PLAYCOUNT, &(aTrack->playcount));
462  GetTrackPropFileType(aMediaItem, &(aTrack->filetype));
463 }
464 
465 
474 void
475 sbIPDDevice::SetTrackProperties(sbIMediaItem* aMediaItem,
476  Itdb_Track* aTrack)
477 {
478  // Validate arguments.
479  NS_ASSERTION(aTrack, "aTrack is null");
480  NS_ASSERTION(aMediaItem, "aMediaItem is null");
481 
482  // Function variables.
483  nsresult rv;
484 
485  // Get the track properties.
486  nsCOMPtr<sbIPropertyArray> props;
487  rv = GetTrackProperties(aTrack, getter_AddRefs(props));
488  NS_ENSURE_SUCCESS(rv, /* void */);
489 
490  // Set the media item properties.
491  mLibraryListener->IgnoreMediaItem(aMediaItem);
492  rv = aMediaItem->SetProperties(props);
493  mLibraryListener->UnignoreMediaItem(aMediaItem);
494  NS_ENSURE_SUCCESS(rv, /* void */);
495 }
496 
497 
506 nsresult
507 sbIPDDevice::GetTrackProperties(Itdb_Track* aTrack,
508  sbIPropertyArray** aPropertyArray)
509 {
510  // Validate arguments.
511  NS_ASSERTION(aTrack, "aTrack is null");
512  NS_ASSERTION(aPropertyArray, "aPropertyArray is null");
513 
514  // Function variables.
515  nsresult rv;
516 
517  // Get a mutable property array.
518  nsCOMPtr<sbIMutablePropertyArray>
519  props = do_CreateInstance
520  ("@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1",
521  &rv);
522  NS_ENSURE_SUCCESS(rv, rv);
523 
524  // Get the track properties from the iPod track and set them in the property
525  // array.
526  SetTrackProp (props, SB_PROPERTY_TRACKNAME, aTrack->title);
527  SetTrackProp (props, SB_PROPERTY_ALBUMNAME, aTrack->album);
528  SetTrackProp (props, SB_PROPERTY_ARTISTNAME, aTrack->artist);
529  SetTrackProp (props, SB_PROPERTY_GENRE, aTrack->genre);
530  SetTrackProp (props, SB_PROPERTY_COMPOSERNAME, aTrack->composer);
531  SetTrackPropDur(props, SB_PROPERTY_DURATION, aTrack->tracklen);
532  SetTrackProp (props, SB_PROPERTY_DISCNUMBER, aTrack->cd_nr);
533  SetTrackProp (props, SB_PROPERTY_TOTALDISCS, aTrack->cds);
534  SetTrackProp (props, SB_PROPERTY_TRACKNUMBER, aTrack->track_nr);
535  SetTrackProp (props, SB_PROPERTY_TOTALTRACKS, aTrack->tracks);
536  SetTrackProp (props, SB_PROPERTY_YEAR, aTrack->year);
537  SetTrackPropRating(props, SB_PROPERTY_RATING, aTrack->rating);
538  SetTrackProp (props, SB_PROPERTY_PLAYCOUNT, aTrack->playcount);
539  SetTrackProp (props, SB_PROPERTY_DEVICE_PERSISTENT_ID, aTrack->dbid);
540 
541  // The iPod track is available.
542  SetTrackProp(props, SB_PROPERTY_AVAILABILITY, NS_LITERAL_STRING("1"));
543 
544  // Return results.
545  NS_ADDREF(*aPropertyArray = props);
546 
547  return NS_OK;
548 }
549 
550 
559 nsresult
560 sbIPDDevice::GetTrackProperties(Itdb_Track* aTrack,
561  nsIMutableArray* aPropertiesArrayArray)
562 {
563  // Validate arguments.
564  NS_ASSERTION(aTrack, "aTrack is null");
565  NS_ASSERTION(aPropertiesArrayArray, "aPropertiesArrayArray is null");
566 
567  // Function variables.
568  nsresult rv;
569 
570  // Get the track properties.
571  nsCOMPtr<sbIPropertyArray> props;
572  rv = GetTrackProperties(aTrack, getter_AddRefs(props));
573  NS_ENSURE_SUCCESS(rv, rv);
574 
575  // Add the track properties to the properties array.
576  rv = aPropertiesArrayArray->AppendElement(props, PR_FALSE);
577  NS_ENSURE_SUCCESS(rv, rv);
578 
579  return NS_OK;
580 }
581 
582 
592 nsresult
593 sbIPDDevice::TrackUpdateProperties(sbIMediaItem* aMediaItem,
594  Itdb_Track* aTrack)
595 {
596  // Validate arguments.
597  NS_ASSERTION(aMediaItem, "aMediaItem is null");
598 
599  // Function variables.
600  nsresult rv;
601 
602  // Get the iPod track.
603  Itdb_Track* track = aTrack;
604  if (!track) {
605  rv = GetTrack(aMediaItem, &track);
606  NS_ENSURE_SUCCESS(rv, rv);
607  }
608 
609  // Update the properties.
610  SetTrackProperties(track, aMediaItem);
611 
612  // Mark the iPod database as dirty.
613  mITDBDirty = PR_TRUE;
614 
615  return NS_OK;
616 }
617 
618 
629 nsresult
630 sbIPDDevice::GetTrackProp(sbIMediaItem* aMediaItem,
631  const char* aPropName,
632  gchar** aProp)
633 {
634  // Validate arguments.
635  NS_ASSERTION(aMediaItem, "aMediaItem is null");
636  NS_ASSERTION(aPropName, "aPropName is null");
637  NS_ASSERTION(aProp, "aProp is null");
638 
639  // Function variables.
640  nsresult rv;
641 
642  // Get the property.
643  nsAutoString propName;
644  nsAutoString nsProp;
645  propName.AssignLiteral(aPropName);
646  rv = aMediaItem->GetProperty(propName, nsProp);
647  NS_ENSURE_SUCCESS(rv, rv);
648 
649  // Return property.
650  gchar* prop = NULL;
651  if (!nsProp.IsEmpty()) {
652  prop = g_strdup(NS_ConvertUTF16toUTF8(nsProp).get());
653  NS_ENSURE_TRUE(prop, NS_ERROR_OUT_OF_MEMORY);
654  }
655  if (*aProp)
656  g_free(*aProp);
657  *aProp = prop;
658 
659  return NS_OK;
660 }
661 
662 
672 nsresult
673 sbIPDDevice::SetTrackProp(sbIMediaItem* aMediaItem,
674  const char* aPropName,
675  gchar* aProp)
676 {
677  // Validate arguments.
678  NS_ASSERTION(aMediaItem, "aMediaItem is null");
679  NS_ASSERTION(aPropName, "aPropName is null");
680 
681  // Function variables.
682  nsresult rv;
683 
684  // Set the property.
685  nsAutoString propName;
686  nsAutoString prop;
687  propName.AssignLiteral(aPropName);
688  if (aProp)
689  prop.Assign(NS_ConvertUTF8toUTF16(aProp));
690  else
691  prop.SetIsVoid(PR_TRUE);
692  rv = aMediaItem->SetProperty(propName, prop);
693  NS_ENSURE_SUCCESS(rv, rv);
694 
695  return NS_OK;
696 }
697 
698 
708 nsresult
709 sbIPDDevice::SetTrackProp(sbIMutablePropertyArray* aPropertyArray,
710  const char* aPropName,
711  const nsAString& aProp)
712 {
713  // Validate arguments.
714  NS_ASSERTION(aPropertyArray, "aPropertyArray is null");
715  NS_ASSERTION(aPropName, "aPropName is null");
716 
717  // Function variables.
718  nsresult rv;
719 
720  // Get the property info.
721  nsAutoString propName;
722  nsCOMPtr<sbIPropertyInfo> propertyInfo;
723  propName.AssignLiteral(aPropName);
724  rv = mPropertyManager->GetPropertyInfo(propName,
725  getter_AddRefs(propertyInfo));
726  NS_ENSURE_SUCCESS(rv, rv);
727 
728  // Validate the property value.
729  PRBool valid;
730  rv = propertyInfo->Validate(aProp, &valid);
731  NS_ENSURE_SUCCESS(rv, rv);
732  NS_ENSURE_TRUE(valid, NS_ERROR_INVALID_ARG);
733 
734  // Set the property.
735  rv = aPropertyArray->AppendProperty(propName, aProp);
736  NS_ENSURE_SUCCESS(rv, rv);
737 
738  return NS_OK;
739 }
740 
741 
751 nsresult
752 sbIPDDevice::SetTrackProp(sbIMutablePropertyArray* aPropertyArray,
753  const char* aPropName,
754  gchar* aProp)
755 {
756  // Validate arguments.
757  NS_ASSERTION(aPropertyArray, "aPropertyArray is null");
758  NS_ASSERTION(aPropName, "aPropName is null");
759 
760  // Function variables.
761  nsresult rv;
762 
763  // Do nothing if property is null.
764  if (!aProp)
765  return NS_OK;
766 
767  // Get the property info.
768  nsAutoString propName;
769  nsCOMPtr<sbIPropertyInfo> propertyInfo;
770  propName.AssignLiteral(aPropName);
771  rv = mPropertyManager->GetPropertyInfo(propName,
772  getter_AddRefs(propertyInfo));
773  NS_ENSURE_SUCCESS(rv, rv);
774 
775  // Convert and validate the property value.
776  nsAutoString prop;
777  if (aProp) {
778  PRBool valid;
779  prop.Assign(NS_ConvertUTF8toUTF16(aProp));
780  rv = propertyInfo->Validate(prop, &valid);
781  NS_ENSURE_SUCCESS(rv, rv);
782  NS_ENSURE_TRUE(valid, NS_ERROR_INVALID_ARG);
783  } else {
784  prop.SetIsVoid(PR_TRUE);
785  }
786 
787  // Set the property.
788  rv = aPropertyArray->AppendProperty(propName, prop);
789  NS_ENSURE_SUCCESS(rv, rv);
790 
791  return NS_OK;
792 }
793 
794 
805 nsresult
806 sbIPDDevice::GetTrackProp(sbIMediaItem* aMediaItem,
807  const char* aPropName,
808  gint* aProp)
809 {
810  // Validate arguments.
811  NS_ASSERTION(aMediaItem, "aMediaItem is null");
812  NS_ASSERTION(aPropName, "aPropName is null");
813  NS_ASSERTION(aProp, "aProp is null");
814 
815  // Function variables.
816  nsresult rv;
817 
818  // Get the property as a string.
819  nsAutoString propName;
820  nsAutoString propStr;
821  propName.AssignLiteral(aPropName);
822  rv = aMediaItem->GetProperty(propName, propStr);
823  NS_ENSURE_SUCCESS(rv, rv);
824 
825  // If present, return the property as an integer.
826  if (!propStr.IsEmpty()) {
827  gint prop;
828  int numScanned;
829  numScanned = PR_sscanf(NS_ConvertUTF16toUTF8(propStr).get(), "%d", &prop);
830  if (numScanned >= 1)
831  *aProp = prop;
832  }
833 
834  return NS_OK;
835 }
836 
837 
847 nsresult
848 sbIPDDevice::SetTrackProp(sbIMediaItem* aMediaItem,
849  const char* aPropName,
850  gint aProp)
851 {
852  // Validate arguments.
853  NS_ASSERTION(aMediaItem, "aMediaItem is null");
854  NS_ASSERTION(aPropName, "aPropName is null");
855 
856  // Function variables.
857  nsresult rv;
858 
859  // Set the property.
860  nsAutoString propName;
861  propName.AssignLiteral(aPropName);
862  rv = aMediaItem->SetProperty(propName, sbAutoString(aProp));
863  NS_ENSURE_SUCCESS(rv, rv);
864 
865  return NS_OK;
866 }
867 
868 
878 nsresult
879 sbIPDDevice::SetTrackProp(sbIMutablePropertyArray* aPropertyArray,
880  const char* aPropName,
881  gint aProp)
882 {
883  // Validate arguments.
884  NS_ASSERTION(aPropertyArray, "aPropertyArray is null");
885  NS_ASSERTION(aPropName, "aPropName is null");
886 
887  // Function variables.
888  nsresult rv;
889 
890  // Get the property info.
891  nsAutoString propName;
892  nsCOMPtr<sbIPropertyInfo> propertyInfo;
893  propName.AssignLiteral(aPropName);
894  rv = mPropertyManager->GetPropertyInfo(propName,
895  getter_AddRefs(propertyInfo));
896  NS_ENSURE_SUCCESS(rv, rv);
897 
898  // Convert and validate the property value. Do not set the property if not
899  // valid.
900  sbAutoString prop(aProp);
901  PRBool valid;
902  rv = propertyInfo->Validate(prop, &valid);
903  NS_ENSURE_SUCCESS(rv, rv);
904  if (!valid)
905  return NS_OK;
906 
907  // Set the property.
908  rv = aPropertyArray->AppendProperty(propName, prop);
909  NS_ENSURE_SUCCESS(rv, rv);
910 
911  return NS_OK;
912 }
913 
914 
926 nsresult
927 sbIPDDevice::GetTrackProp(sbIMediaItem* aMediaItem,
928  const char* aPropName,
929  guint32* aProp)
930 {
931  // Validate arguments.
932  NS_ASSERTION(aMediaItem, "aMediaItem is null");
933  NS_ASSERTION(aPropName, "aPropName is null");
934  NS_ASSERTION(aProp, "aProp is null");
935 
936  // Function variables.
937  nsresult rv;
938 
939  // Get the property as a string.
940  nsAutoString propName;
941  nsAutoString propStr;
942  propName.AssignLiteral(aPropName);
943  rv = aMediaItem->GetProperty(propName, propStr);
944  NS_ENSURE_SUCCESS(rv, rv);
945 
946  // If present, return the property as an integer.
947  if (!propStr.IsEmpty()) {
948  guint32 prop;
949  int numScanned;
950  numScanned = PR_sscanf(NS_ConvertUTF16toUTF8(propStr).get(), "%u", &prop);
951  if (numScanned >= 1)
952  *aProp = prop;
953  }
954 
955  return NS_OK;
956 }
957 
958 
969 nsresult
970 sbIPDDevice::SetTrackProp(sbIMediaItem* aMediaItem,
971  const char* aPropName,
972  guint32 aProp)
973 {
974  // Validate arguments.
975  NS_ASSERTION(aMediaItem, "aMediaItem is null");
976  NS_ASSERTION(aPropName, "aPropName is null");
977 
978  // Function variables.
979  nsresult rv;
980 
981  // Set the property.
982  nsAutoString propName;
983  propName.AssignLiteral(aPropName);
984  rv = aMediaItem->SetProperty(propName, sbAutoString(aProp));
985  NS_ENSURE_SUCCESS(rv, rv);
986 
987  return NS_OK;
988 }
989 
990 
1001 nsresult
1002 sbIPDDevice::SetTrackProp(sbIMutablePropertyArray* aPropertyArray,
1003  const char* aPropName,
1004  guint32 aProp)
1005 {
1006  // Validate arguments.
1007  NS_ASSERTION(aPropertyArray, "aPropertyArray is null");
1008  NS_ASSERTION(aPropName, "aPropName is null");
1009 
1010  // Function variables.
1011  nsresult rv;
1012 
1013  // Get the property info.
1014  nsAutoString propName;
1015  nsCOMPtr<sbIPropertyInfo> propertyInfo;
1016  propName.AssignLiteral(aPropName);
1017  rv = mPropertyManager->GetPropertyInfo(propName,
1018  getter_AddRefs(propertyInfo));
1019  NS_ENSURE_SUCCESS(rv, rv);
1020 
1021  // Convert and validate the property value.
1022  sbAutoString prop(aProp);
1023  PRBool valid;
1024  rv = propertyInfo->Validate(prop, &valid);
1025  NS_ENSURE_SUCCESS(rv, rv);
1026  NS_ENSURE_TRUE(valid, NS_ERROR_INVALID_ARG);
1027 
1028  // Set the property.
1029  rv = aPropertyArray->AppendProperty(propName, prop);
1030  NS_ENSURE_SUCCESS(rv, rv);
1031 
1032  return NS_OK;
1033 }
1034 
1035 
1047 nsresult
1048 sbIPDDevice::GetTrackProp(sbIMediaItem* aMediaItem,
1049  const char* aPropName,
1050  guint64* aProp)
1051 {
1052  // Validate arguments.
1053  NS_ASSERTION(aMediaItem, "aMediaItem is null");
1054  NS_ASSERTION(aPropName, "aPropName is null");
1055  NS_ASSERTION(aProp, "aProp is null");
1056 
1057  // Function variables.
1058  nsresult rv;
1059 
1060  // Get the property as a string.
1061  nsAutoString propName;
1062  nsAutoString propStr;
1063  propName.AssignLiteral(aPropName);
1064  rv = aMediaItem->GetProperty(propName, propStr);
1065  NS_ENSURE_SUCCESS(rv, rv);
1066 
1067  // If present, return the property as an integer.
1068  if (!propStr.IsEmpty()) {
1069  guint64 prop;
1070  int numScanned;
1071  numScanned = PR_sscanf(NS_ConvertUTF16toUTF8(propStr).get(), "%llu", &prop);
1072  if (numScanned >= 1)
1073  *aProp = prop;
1074  }
1075 
1076  return NS_OK;
1077 }
1078 
1079 
1090 nsresult
1091 sbIPDDevice::SetTrackProp(sbIMediaItem* aMediaItem,
1092  const char* aPropName,
1093  guint64 aProp)
1094 {
1095  // Validate arguments.
1096  NS_ASSERTION(aMediaItem, "aMediaItem is null");
1097  NS_ASSERTION(aPropName, "aPropName is null");
1098 
1099  // Function variables.
1100  nsresult rv;
1101 
1102  // Set the property.
1103  nsAutoString propName;
1104  propName.AssignLiteral(aPropName);
1105  rv = aMediaItem->SetProperty(propName, sbAutoString(aProp));
1106  NS_ENSURE_SUCCESS(rv, rv);
1107 
1108  return NS_OK;
1109 }
1110 
1111 
1122 nsresult
1123 sbIPDDevice::SetTrackProp(sbIMutablePropertyArray* aPropertyArray,
1124  const char* aPropName,
1125  guint64 aProp)
1126 {
1127  // Validate arguments.
1128  NS_ASSERTION(aPropertyArray, "aPropertyArray is null");
1129  NS_ASSERTION(aPropName, "aPropName is null");
1130 
1131  // Function variables.
1132  nsresult rv;
1133 
1134  // Get the property info.
1135  nsAutoString propName;
1136  nsCOMPtr<sbIPropertyInfo> propertyInfo;
1137  propName.AssignLiteral(aPropName);
1138  rv = mPropertyManager->GetPropertyInfo(propName,
1139  getter_AddRefs(propertyInfo));
1140  NS_ENSURE_SUCCESS(rv, rv);
1141 
1142  // Convert and validate the property value.
1143  sbAutoString prop(aProp);
1144  PRBool valid;
1145  rv = propertyInfo->Validate(prop, &valid);
1146  NS_ENSURE_SUCCESS(rv, rv);
1147  NS_ENSURE_TRUE(valid, NS_ERROR_INVALID_ARG);
1148 
1149  // Set the property.
1150  rv = aPropertyArray->AppendProperty(propName, prop);
1151  NS_ENSURE_SUCCESS(rv, rv);
1152 
1153  return NS_OK;
1154 }
1155 
1156 
1168 nsresult
1169 sbIPDDevice::GetTrackPropDur(sbIMediaItem* aMediaItem,
1170  const char* aPropName,
1171  gint* aProp)
1172 {
1173  // Validate arguments.
1174  NS_ASSERTION(aMediaItem, "aMediaItem is null");
1175  NS_ASSERTION(aPropName, "aPropName is null");
1176  NS_ASSERTION(aProp, "aProp is null");
1177 
1178  // Function variables.
1179  nsresult rv;
1180 
1181  // Get the property as a string.
1182  nsAutoString propName;
1183  nsAutoString propStr;
1184  propName.AssignLiteral(aPropName);
1185  rv = aMediaItem->GetProperty(propName, propStr);
1186  NS_ENSURE_SUCCESS(rv, rv);
1187 
1188  // If present, return the property as an integer.
1189  if (!propStr.IsEmpty()) {
1190  PRInt64 prop;
1191  int numScanned;
1192  numScanned = PR_sscanf(NS_ConvertUTF16toUTF8(propStr).get(), "%lld", &prop);
1193  if (numScanned >= 1) {
1194  gint64 dur = prop / 1000;
1195  NS_ENSURE_TRUE(prop < (1 << 30), NS_ERROR_FAILURE);
1196  *aProp = (gint) (dur & 0xFFFFFFFF);
1197  }
1198  }
1199 
1200  return NS_OK;
1201 }
1202 
1203 
1214 nsresult
1215 sbIPDDevice::SetTrackPropDur(sbIMediaItem* aMediaItem,
1216  const char* aPropName,
1217  gint aProp)
1218 {
1219  // Validate arguments.
1220  NS_ASSERTION(aMediaItem, "aMediaItem is null");
1221  NS_ASSERTION(aPropName, "aPropName is null");
1222 
1223  // Function variables.
1224  nsresult rv;
1225 
1226  // Convert the iPod duration value to a Songbird duration value string.
1227  PRUint64 duration = ((PRUint64) aProp) * 1000;
1228  char durationStr[32];
1229  PRUint32 printCount;
1230  printCount = PR_snprintf(durationStr, sizeof(durationStr), "%lld", duration);
1231  NS_ENSURE_TRUE(printCount > 0, NS_ERROR_UNEXPECTED);
1232 
1233  // Set the property.
1234  nsAutoString propName;
1235  nsAutoString prop;
1236  propName.AssignLiteral(aPropName);
1237  prop.AssignLiteral(durationStr);
1238  rv = aMediaItem->SetProperty(propName, prop);
1239  NS_ENSURE_SUCCESS(rv, rv);
1240 
1241  return NS_OK;
1242 }
1243 
1244 
1254 nsresult
1255 sbIPDDevice::SetTrackPropDur(sbIMutablePropertyArray* aPropertyArray,
1256  const char* aPropName,
1257  gint aProp)
1258 {
1259  // Validate arguments.
1260  NS_ASSERTION(aPropertyArray, "aPropertyArray is null");
1261  NS_ASSERTION(aPropName, "aPropName is null");
1262 
1263  // Function variables.
1264  nsresult rv;
1265 
1266  // Get the property info.
1267  nsAutoString propName;
1268  nsCOMPtr<sbIPropertyInfo> propertyInfo;
1269  propName.AssignLiteral(aPropName);
1270  rv = mPropertyManager->GetPropertyInfo(propName,
1271  getter_AddRefs(propertyInfo));
1272  NS_ENSURE_SUCCESS(rv, rv);
1273 
1274  /* Convert the iPod duration value to a Songbird duration value string. */
1275  PRUint64 duration = ((PRUint64) aProp) * 1000;
1276  char durationStr[32];
1277  PRUint32 printCount;
1278  printCount = PR_snprintf(durationStr, sizeof(durationStr), "%lld", duration);
1279  NS_ENSURE_TRUE(printCount > 0, NS_ERROR_UNEXPECTED);
1280 
1281  // Convert and validate the property value.
1282  nsAutoString prop;
1283  PRBool valid;
1284  prop.AssignLiteral(durationStr);
1285  rv = propertyInfo->Validate(prop, &valid);
1286  NS_ENSURE_SUCCESS(rv, rv);
1287  NS_ENSURE_TRUE(valid, NS_ERROR_INVALID_ARG);
1288 
1289  // Set the property.
1290  rv = aPropertyArray->AppendProperty(propName, prop);
1291  NS_ENSURE_SUCCESS(rv, rv);
1292 
1293  return NS_OK;
1294 }
1295 
1296 
1308 nsresult
1309 sbIPDDevice::GetTrackPropRating(sbIMediaItem* aMediaItem,
1310  const char* aPropName,
1311  guint32* aProp)
1312 {
1313  // Validate arguments.
1314  NS_ASSERTION(aMediaItem, "aMediaItem is null");
1315  NS_ASSERTION(aPropName, "aPropName is null");
1316  NS_ASSERTION(aProp, "aProp is null");
1317 
1318  // Function variables.
1319  nsresult rv;
1320 
1321  // Get the property as a string.
1322  nsAutoString propName;
1323  nsAutoString propStr;
1324  propName.AssignLiteral(aPropName);
1325  rv = aMediaItem->GetProperty(propName, propStr);
1326  NS_ENSURE_SUCCESS(rv, rv);
1327 
1328  // If present, return the property as an integer scaled from 0-100.
1329  if (!propStr.IsEmpty()) {
1330  PRUint32 prop;
1331  int numScanned;
1332  numScanned = PR_sscanf(NS_ConvertUTF16toUTF8(propStr).get(), "%lu", &prop);
1333  if (numScanned >= 1) {
1334  guint32 rating = prop * 20;
1335  *aProp = rating;
1336  }
1337  }
1338 
1339  return NS_OK;
1340 }
1341 
1342 
1353 nsresult
1354 sbIPDDevice::SetTrackPropRating(sbIMediaItem* aMediaItem,
1355  const char* aPropName,
1356  guint32 aProp)
1357 {
1358  // Validate arguments.
1359  NS_ASSERTION(aMediaItem, "aMediaItem is null");
1360  NS_ASSERTION(aPropName, "aPropName is null");
1361 
1362  // Function variables.
1363  nsresult rv;
1364 
1365  // Convert the iPod rating value to a Songbird rating value string.
1366  PRUint32 rating = (aProp + 10) / 20;
1367  char ratingStr[32];
1368  PRUint32 printCount;
1369  printCount = PR_snprintf(ratingStr, sizeof(ratingStr), "%lu", rating);
1370  NS_ENSURE_TRUE(printCount > 0, NS_ERROR_UNEXPECTED);
1371 
1372  // Set the property.
1373  nsAutoString propName;
1374  nsAutoString prop;
1375  propName.AssignLiteral(aPropName);
1376  prop.AssignLiteral(ratingStr);
1377  rv = aMediaItem->SetProperty(propName, prop);
1378  NS_ENSURE_SUCCESS(rv, rv);
1379 
1380  return NS_OK;
1381 }
1382 
1383 
1394 nsresult
1395 sbIPDDevice::SetTrackPropRating(sbIMutablePropertyArray* aPropertyArray,
1396  const char* aPropName,
1397  guint32 aProp)
1398 {
1399  // Validate arguments.
1400  NS_ASSERTION(aPropertyArray, "aPropertyArray is null");
1401  NS_ASSERTION(aPropName, "aPropName is null");
1402 
1403  // Function variables.
1404  nsresult rv;
1405 
1406  // Get the property info.
1407  nsAutoString propName;
1408  nsCOMPtr<sbIPropertyInfo> propertyInfo;
1409  propName.AssignLiteral(aPropName);
1410  rv = mPropertyManager->GetPropertyInfo(propName,
1411  getter_AddRefs(propertyInfo));
1412  NS_ENSURE_SUCCESS(rv, rv);
1413 
1414  // Convert the iPod rating value to a Songbird rating value string.
1415  PRUint32 rating = (aProp + 10) / 20;
1416  char ratingStr[32];
1417  PRUint32 printCount;
1418  printCount = PR_snprintf(ratingStr, sizeof(ratingStr), "%lu", rating);
1419  NS_ENSURE_TRUE(printCount > 0, NS_ERROR_UNEXPECTED);
1420 
1421  // Convert and validate the property value.
1422  nsAutoString prop;
1423  PRBool valid;
1424  prop.AssignLiteral(ratingStr);
1425  rv = propertyInfo->Validate(prop, &valid);
1426  NS_ENSURE_SUCCESS(rv, rv);
1427  NS_ENSURE_TRUE(valid, NS_ERROR_INVALID_ARG);
1428 
1429  // Set the property.
1430  rv = aPropertyArray->AppendProperty(propName, prop);
1431  NS_ENSURE_SUCCESS(rv, rv);
1432 
1433  return NS_OK;
1434 }
1435 
1436 
1446 nsresult
1447 sbIPDDevice::GetTrackPropFileType(sbIMediaItem* aMediaItem,
1448  gchar** aProp)
1449 {
1450  // Validate parameters.
1451  NS_ASSERTION(aMediaItem, "aMediaItem is null");
1452  NS_ASSERTION(aProp, "aProp is null");
1453 
1454  // Function variables.
1455  nsresult rv;
1456 
1457  // Get the track media URL.
1458  nsCOMPtr<nsIURI> trackURI;
1459  rv = aMediaItem->GetContentSrc(getter_AddRefs(trackURI));
1460  NS_ENSURE_SUCCESS(rv, rv);
1461  nsCOMPtr<nsIURL> trackURL = do_QueryInterface(trackURI, &rv);
1462  NS_ENSURE_SUCCESS(rv, rv);
1463 
1464  // Get the track media file extension.
1465  nsCAutoString fileExtension;
1466  rv = trackURL->GetFileExtension(fileExtension);
1467  NS_ENSURE_SUCCESS(rv, rv);
1468 
1469  // If present, return property.
1470  if (!fileExtension.IsEmpty()) {
1471  gchar* prop = g_strdup(fileExtension.get());
1472  NS_ENSURE_TRUE(prop, NS_ERROR_OUT_OF_MEMORY);
1473  *aProp = prop;
1474  }
1475 
1476  return NS_OK;
1477 }
1478 
1479 
1480 //------------------------------------------------------------------------------
1481 //
1482 // iPod device track info services.
1483 //
1484 //------------------------------------------------------------------------------
1485 
1486 
1495 nsresult
1496 sbIPDDevice::GetTrackURI(Itdb_Track* aTrack,
1497  nsIURI** aTrackURI)
1498 {
1499  // Validate parameters.
1500  NS_ASSERTION(aTrack, "aTrack is null");
1501  NS_ASSERTION(aTrackURI, "aTrackURI is null");
1502 
1503  // Function variables.
1504  nsresult rv;
1505 
1506  // Get a track file object.
1507  nsCOMPtr<nsIFile> trackFile;
1508  rv = TrackGetFile(aTrack, getter_AddRefs(trackFile));
1509  NS_ENSURE_SUCCESS(rv, rv);
1510 
1511  // Get a track URI object.
1512  nsCOMPtr<nsIURI> trackURI;
1513  rv = mFileProtocolHandler->NewFileURI(trackFile,
1514  getter_AddRefs(trackURI));
1515  NS_ENSURE_SUCCESS(rv, rv);
1516 
1517  // Return results.
1518  NS_ADDREF(*aTrackURI = trackURI);
1519 
1520  return NS_OK;
1521 }
1522 
1523 
1532 nsresult
1533 sbIPDDevice::TrackGetFile(Itdb_Track* aTrack,
1534  nsIFile** aTrackFile)
1535 {
1536  // Validate parameters.
1537  NS_ASSERTION(aTrack, "aTrack is null");
1538  NS_ASSERTION(aTrackFile, "aTrackFile is null");
1539 
1540  // Function variables.
1541  nsresult rv;
1542 
1543  // Get the track file path and set it up for auto-disposal.
1544  gchar* trackFilePath = itdb_filename_on_ipod(aTrack);
1545  NS_ENSURE_TRUE(trackFilePath, NS_ERROR_FAILURE);
1546  sbAutoGMemPtr autoTrackFilePath(trackFilePath);
1547  nsCAutoString nsTrackFilePath(trackFilePath);
1548 
1549  // Get a track file object.
1550  nsCOMPtr<nsILocalFile> trackLocalFile =
1551  do_CreateInstance("@mozilla.org/file/local;1", &rv);
1552  NS_ENSURE_SUCCESS(rv, rv);
1553  rv = trackLocalFile->InitWithNativePath(nsTrackFilePath);
1554  NS_ENSURE_SUCCESS(rv, rv);
1555 
1556  // Return results.
1557  NS_ADDREF(*aTrackFile = trackLocalFile);
1558 
1559  return NS_OK;
1560 }
1561 
1562 
1563 //------------------------------------------------------------------------------
1564 //
1565 // Internal iPod device track services.
1566 //
1567 //------------------------------------------------------------------------------
1568 
1586 {
1587 public:
1589  nsRefPtr<sbIPDDevice> ipdDevice;
1590  Itdb_Track** trackBatch;
1591  PRUint32 batchCount;
1592 };
1593 NS_IMPL_ISUPPORTS0(ImportTrackBatchParams)
1594 
1595 nsresult
1596 sbIPDDevice::ImportTrackBatch(Itdb_Track** aTrackBatch,
1597  PRUint32 aBatchCount)
1598 {
1599  // Validate parameters.
1600  NS_ASSERTION(aTrackBatch, "aTrackBatch is null");
1601 
1602  // Function variables.
1603  nsresult rv;
1604 
1605  // Set up the batch callback function.
1606  nsCOMPtr<sbIMediaListBatchCallback>
1607  batchCallback = new sbMediaListBatchCallback(ImportTrackBatch1);
1608  NS_ENSURE_TRUE(batchCallback, NS_ERROR_OUT_OF_MEMORY);
1609 
1610  // Set up the batch callback function parameters.
1611  nsRefPtr<ImportTrackBatchParams> importTrackBatchParams =
1612  new ImportTrackBatchParams();
1613  NS_ENSURE_TRUE(importTrackBatchParams, NS_ERROR_OUT_OF_MEMORY);
1614  importTrackBatchParams->ipdDevice = this;
1615  importTrackBatchParams->trackBatch = aTrackBatch;
1616  importTrackBatchParams->batchCount = aBatchCount;
1617 
1618  // Import tracks in batch mode.
1619  rv = mDeviceLibraryML->RunInBatchMode(batchCallback,
1620  importTrackBatchParams);
1621  NS_ENSURE_SUCCESS(rv, rv);
1622 
1623  return NS_OK;
1624 }
1625 
1626 /* static */
1627 nsresult
1628 sbIPDDevice::ImportTrackBatch1(nsISupports* aUserData)
1629 {
1630  // Validate parameters.
1631  NS_ENSURE_ARG_POINTER(aUserData);
1632 
1633  // Function variables.
1634  nsresult rv;
1635 
1636  // Get the import track batch parameters.
1637  ImportTrackBatchParams* importTrackBatchParams =
1638  static_cast<ImportTrackBatchParams *>(aUserData);
1639  nsRefPtr<sbIPDDevice> ipdDevice = importTrackBatchParams->ipdDevice;
1640  Itdb_Track** trackBatch = importTrackBatchParams->trackBatch;
1641  int batchCount = importTrackBatchParams->batchCount;
1642 
1643  // Import a batch of tracks.
1644  rv = ipdDevice->ImportTrackBatch2(trackBatch, batchCount);
1645  NS_ENSURE_SUCCESS(rv, rv);
1646 
1647  return NS_OK;
1648 }
1649 
1650 nsresult
1651 sbIPDDevice::ImportTrackBatch2(Itdb_Track** aTrackBatch,
1652  PRUint32 aBatchCount)
1653 {
1654  // Validate parameters.
1655  NS_ASSERTION(aTrackBatch, "aTrackBatch is null");
1656 
1657  // Function variables.
1658  nsresult rv;
1659 
1660  // Create the track info arrays.
1661  nsTArray<PRUint32> trackArrayIndexMap(aBatchCount);
1662  nsCOMPtr<nsIMutableArray> trackURIArray =
1663  do_CreateInstance("@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
1664  NS_ENSURE_SUCCESS(rv, rv);
1665  nsCOMPtr<nsIMutableArray> propsArray =
1666  do_CreateInstance("@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
1667  NS_ENSURE_SUCCESS(rv, rv);
1668 
1669  // Ignore library changes and set up to automatically stop ignoring.
1670  mLibraryListener->SetIgnoreListener(PR_TRUE);
1671  sbIPDAutoStopIgnoreLibrary autoStopIgnoreLibrary(mLibraryListener);
1672 
1673  // Get the track URI's and properties.
1674  PRUint32 trackCount = 0;
1675  for (PRUint32 i = 0; i < aBatchCount; i++) {
1676  // Get the track URI. Skip track on failure. This can happen if the track
1677  // file doesn't exist.
1678  Itdb_Track* track = aTrackBatch[i];
1679  FIELD_LOG(("1: ImportTrack %s\n", itdb_filename_on_ipod(track)));
1680  nsCOMPtr<nsIURI> trackURI;
1681  rv = GetTrackURI(track, getter_AddRefs(trackURI));
1682  if (NS_FAILED(rv)) {
1683  FIELD_LOG(("2: ImportTrack failed to get track URI.\n"));
1684  continue;
1685  }
1686 
1687  // If track has previously been imported, just update it.
1688  nsCOMPtr<sbIMediaItem> mediaItem;
1690  (mDeviceLibrary,
1691  sbAutoString(track->dbid),
1692  getter_AddRefs(mediaItem));
1693  if (NS_SUCCEEDED(rv)) {
1694  SetTrackProperties(mediaItem, track);
1695  rv = mediaItem->SetContentSrc(trackURI);
1696  NS_ENSURE_SUCCESS(rv, rv);
1697  continue;
1698  } else {
1699  NS_ENSURE_TRUE(rv == NS_ERROR_NOT_AVAILABLE, rv);
1700  }
1701 
1702  // Map the track array indices.
1703  trackArrayIndexMap[trackCount] = i;
1704 
1705  // Add the track properties.
1706  rv = GetTrackProperties(track, propsArray);
1707  NS_ENSURE_SUCCESS(rv, rv);
1708 
1709  // Add the track URI.
1710  rv = trackURIArray->AppendElement(trackURI, PR_FALSE);
1711  NS_ENSURE_SUCCESS(rv, rv);
1712 
1713  // One more track.
1714  trackCount++;
1715  }
1716 
1717  // Create the track media items.
1718  nsCOMPtr<nsIArray> trackMediaItemArray;
1719  rv = mDeviceLibrary->BatchCreateMediaItems
1720  (trackURIArray,
1721  propsArray,
1722  PR_TRUE,
1723  getter_AddRefs(trackMediaItemArray));
1724  NS_ENSURE_SUCCESS(rv, rv);
1725 
1726  return NS_OK;
1727 }
1728 
1729 
1736 void
1737 sbIPDDevice::RemoveTrackFromAllPlaylists(Itdb_Track* aTrack)
1738 {
1739  // Remove the track from each playlist.
1740  GList* playlistList = mITDB->playlists;
1741  while (playlistList) {
1742  // Get the next playlist.
1743  Itdb_Playlist* playlist = (Itdb_Playlist *) playlistList->data;
1744  playlistList = playlistList->next;
1745 
1746  // Remove the track from the playlist.
1747  itdb_playlist_remove_track(playlist, aTrack);
1748  }
1749 }
1750 
1751 
1761 PRBool
1762 sbIPDDevice::IsMediaSupported(sbIMediaItem* aMediaItem)
1763 {
1764  // Validate arguments.
1765  NS_ASSERTION(aMediaItem, "aMediaItem is null");
1766 
1767  // Function variables.
1768  nsresult rv;
1769 
1770  // Get the media file extension.
1771  nsCOMPtr<nsIURI> itemURI;
1772  nsCOMPtr<nsIURL> itemURL;
1773  nsCAutoString fileExtension;
1774  rv = aMediaItem->GetContentSrc(getter_AddRefs(itemURI));
1775  NS_ENSURE_SUCCESS(rv, PR_FALSE);
1776  itemURL = do_QueryInterface(itemURI, &rv);
1777  NS_ENSURE_SUCCESS(rv, PR_FALSE);
1778  rv = itemURL->GetFileExtension(fileExtension);
1779  NS_ENSURE_SUCCESS(rv, PR_FALSE);
1780 
1781  // Check the media file extension against all supported extensions.
1782  PRUint32 i;
1783  for (i = 0; i < sbIPDSupportedMediaListLength; i++) {
1784  if (fileExtension.Equals(sbIPDSupportedMediaList[i],
1785  CaseInsensitiveCompare)) {
1786  return PR_TRUE;
1787  }
1788  }
1789 
1790  return PR_FALSE;
1791 }
1792 
1793 
1802 void
1803 sbIPDDevice::AddUnsupportedMediaItem(sbIMediaItem* aMediaItem)
1804 {
1805  // Validate arguments.
1806  NS_ASSERTION(aMediaItem, "aMediaItem is null");
1807 
1808  // Dispatch an unsupported media type event.
1811  sbIPDVariant(NS_GET_IID(sbIMediaItem), aMediaItem).get());
1812 }
1813 
1814 
#define SB_PROPERTY_TOTALDISCS
friend class sbIPDAutoTrack
Definition: sbIPDDevice.h:260
nsresult CreateAndDispatchEvent(PRUint32 aType, nsIVariant *aData, PRBool aAsync=PR_TRUE, sbIDeviceEventTarget *aTarget=nsnull)
return NS_OK
#define SB_PROPERTY_PLAYCOUNT
dataSBHighestRatedArtists SBProperties rating
Definition: tuner2.js:867
Songbird iPod Device Utility Definitions.
#define SB_PROPERTY_RATING
void ItemStart(PRInt32 aItemNum=-1, PRInt32 aItemCount=-1)
const NS_ERROR_ABORT
#define SB_PROPERTY_HIDDEN
PRUint32 sbIPDSupportedMediaListLength
#define SB_PROPERTY_DEVICE_PERSISTENT_ID
nsRefPtr< sbBaseDeviceLibraryListener > mLibraryListener
Definition: sbBaseDevice.h:693
#define SB_PROPERTY_TOTALTRACKS
virtual PRBool IsRequestAborted()
An interface to carry around arrays of nsIProperty instances Note that implementations of the interfa...
const char * propName
_hideDatepicker duration
NS_DECL_ISUPPORTS nsRefPtr< sbIPDDevice > ipdDevice
#define SB_PROPERTY_GENRE
Songbird iPod Device Definitions.
#define FIELD_LOG(args)
Definition: sbIPDLog.h:126
#define SB_PROPERTY_DURATION
#define SB_PROPERTY_DISCNUMBER
Songbird iPod Device Logging Definitions.
#define SB_PROPERTY_ARTISTNAME
const unsigned long EVENT_DEVICE_MEDIA_WRITE_UNSUPPORTED_TYPE
static nsresult GetMediaItemByDevicePersistentId(sbILibrary *aLibrary, const nsAString &aDevicePersistentId, sbIMediaItem **aItem)
Itdb_Track ** trackBatch
void ItemProgress(double aProgress)
#define IPOD_DEVICE_BATCH_SIZE
Definition: sbIPDTrack.cpp:84
#define SB_PROPERTY_ALBUMNAME
#define SB_PROPERTY_COMPOSERNAME
var dur
const char * sbIPDSupportedMediaList[]
Definition: sbIPDDevice.cpp:93
#define SB_PROPERTY_YEAR
Interface that defines a single item of media in the system.
#define SB_PROPERTY_TRACKNAME
#define SB_PROPERTY_AVAILABILITY
An interface to carry around arrays of nsIProperty instances. Users of this interface should only QI ...
_getSelectedPageStyle s i
#define SB_PROPERTY_TRACKNUMBER