sbAlbumArtCommon.cpp
Go to the documentation of this file.
1 
2 #include "sbAlbumArtCommon.h"
3 
4 #include <nsCOMPtr.h>
5 #include <sbStandardProperties.h>
6 #include <nsISimpleEnumerator.h>
8 #include <nsStringGlue.h>
9 #include <sbIFileMetadataService.h>
10 #include <nsServiceManagerUtils.h>
11 #include <sbIJobProgress.h>
12 #include <nsIMutableArray.h>
13 #include <nsComponentManagerUtils.h>
14 #include <nsArrayUtils.h>
15 #include <sbILibrary.h>
16 
17 nsresult SetItemArtwork(nsIURI* aImageLocation, sbIMediaItem* aMediaItem) {
18  NS_ENSURE_ARG_POINTER(aImageLocation);
19  NS_ENSURE_ARG_POINTER(aMediaItem);
20 
21  nsresult rv;
22  nsCAutoString imageFileURISpec;
23  rv = aImageLocation->GetSpec(imageFileURISpec);
24  if (NS_SUCCEEDED(rv)) {
25  rv = aMediaItem->SetProperty(
26  NS_LITERAL_STRING(SB_PROPERTY_PRIMARYIMAGEURL),
27  NS_ConvertUTF8toUTF16(imageFileURISpec));
28  NS_ENSURE_SUCCESS(rv, rv);
29  }
30 
31  return NS_OK;
32 }
33 
34 nsresult SetItemsArtwork(nsIURI* aImageLocation, nsIArray* aMediaItems) {
35  NS_ENSURE_ARG_POINTER(aMediaItems);
36  NS_ENSURE_ARG_POINTER(aMediaItems);
37 
38  nsresult rv;
39  nsCOMPtr<nsISimpleEnumerator> listEnum;
40  rv = aMediaItems->Enumerate(getter_AddRefs(listEnum));
41  NS_ENSURE_SUCCESS(rv, rv);
42  PRBool hasMore;
43  while (NS_SUCCEEDED(listEnum->HasMoreElements(&hasMore)) && hasMore) {
44  nsCOMPtr<nsISupports> next;
45  if (NS_SUCCEEDED(listEnum->GetNext(getter_AddRefs(next))) && next) {
46  nsCOMPtr<sbIMediaItem> mediaItem(do_QueryInterface(next));
47  rv = SetItemArtwork(aImageLocation, mediaItem);
48  NS_ENSURE_SUCCESS(rv, rv);
49  }
50  }
51 
52  return NS_OK;
53 }
54 
55 nsresult WriteImageMetadata(nsIArray* aMediaItems) {
56  NS_ENSURE_ARG_POINTER(aMediaItems);
57 
58  nsresult rv;
59  PRUint32 numItems;
60  rv = aMediaItems->GetLength(&numItems);
61  NS_ENSURE_SUCCESS(rv, rv);
62  if (numItems > 0) {
63  // Do nothing if the media items' library is marked to not write metadata.
64  nsCOMPtr<sbIMediaItem> mediaItem = do_QueryElementAt(aMediaItems, 0, &rv);
65  NS_ENSURE_SUCCESS(rv, rv);
66  nsCOMPtr<sbILibrary> library;
67  rv = mediaItem->GetLibrary(getter_AddRefs(library));
68  NS_ENSURE_SUCCESS(rv, rv);
69  nsAutoString dontWriteMetadata;
70  rv = library->GetProperty
71  (NS_LITERAL_STRING(SB_PROPERTY_DONT_WRITE_METADATA),
72  dontWriteMetadata);
73  NS_ENSURE_SUCCESS(rv, rv);
74  if (dontWriteMetadata.Equals(NS_LITERAL_STRING("1")))
75  return NS_OK;
76 
77  nsTArray<nsString> propArray;
78  if (!propArray.AppendElement(NS_LITERAL_STRING(SB_PROPERTY_PRIMARYIMAGEURL)))
79  {
80  return NS_ERROR_OUT_OF_MEMORY;
81  }
82 
83  nsCOMPtr<nsIStringEnumerator> propsToWrite =
84  new sbTArrayStringEnumerator(&propArray);
85  NS_ENSURE_TRUE(propsToWrite, NS_ERROR_OUT_OF_MEMORY);
86 
87  nsCOMPtr<sbIFileMetadataService> metadataService =
88  do_GetService( "@songbirdnest.com/Songbird/FileMetadataService;1", &rv );
89  NS_ENSURE_SUCCESS(rv, rv);
90 
91  nsCOMPtr<sbIJobProgress> job;
92  rv = metadataService->Write(aMediaItems, propsToWrite, getter_AddRefs(job));
93  NS_ENSURE_SUCCESS(rv, rv);
94  }
95 
96  return NS_OK;
97 }
98 
return NS_OK
#define SB_PROPERTY_DONT_WRITE_METADATA
nsresult SetItemsArtwork(nsIURI *aImageLocation, nsIArray *aMediaItems)
StringArrayEnumerator prototype hasMore
Interface that defines a single item of media in the system.
nsresult WriteImageMetadata(nsIArray *aMediaItems)
nsresult SetItemArtwork(nsIURI *aImageLocation, sbIMediaItem *aMediaItem)
#define SB_PROPERTY_PRIMARYIMAGEURL
function next()