33 #include <nsIFileURL.h>
34 #include <nsIProxyObjectManager.h>
35 #include <nsIThread.h>
38 #include <nsArrayUtils.h>
39 #include <nsAutoPtr.h>
40 #include <nsComponentManagerUtils.h>
41 #include <nsNetUtil.h>
42 #include <nsStringAPI.h>
43 #include <nsThreadUtils.h>
45 #include <sbIDevice.h>
46 #include <sbIDeviceManager.h>
47 #include <sbILibrary.h>
48 #include <sbILocalDatabaseSmartMediaList.h>
49 #include <sbIMediaList.h>
50 #include <sbIMediaItem.h>
51 #include <sbIPropertyArray.h>
52 #include <sbIPropertyInfo.h>
53 #include <sbIPropertyManager.h>
56 #include <sbFileUtils.h>
68 NS_ENSURE_ARG_POINTER(aList);
69 NS_ENSURE_ARG_POINTER(aProperties);
73 nsCOMPtr<sbIMediaListEnumerationListener> enumerator;
74 nsRefPtr<sbMediaListEnumSingleItemHelper> single;
80 enumerator = do_QueryInterface(single);
82 NS_ENSURE_TRUE(enumerator, NS_ERROR_OUT_OF_MEMORY);
84 rv = aList->EnumerateItemsByProperties(
88 NS_ENSURE_SUCCESS(rv, rv);
93 nsCOMPtr<sbIMediaItem> item = single->GetItem();
95 return NS_ERROR_NOT_AVAILABLE;
104 nsString
const & aOriginLibGuid,
105 nsString
const & aOriginItemGuid,
108 NS_ENSURE_ARG_POINTER(aList);
112 nsCOMPtr<sbIMutablePropertyArray> properties =
114 NS_ENSURE_SUCCESS(rv, rv);
116 if (!aOriginLibGuid.IsEmpty()) {
117 rv = properties->AppendProperty(
120 NS_ENSURE_SUCCESS(rv, rv);
125 NS_ENSURE_SUCCESS(rv, rv);
132 nsString
const & aContentURL,
135 NS_ENSURE_ARG_POINTER(aList);
139 nsCOMPtr<sbIMutablePropertyArray> properties =
141 NS_ENSURE_SUCCESS(rv, rv);
145 NS_ENSURE_SUCCESS(rv, rv);
152 nsString
const & aOriginURL,
155 NS_ENSURE_ARG_POINTER(aList);
159 nsCOMPtr<sbIMutablePropertyArray> properties =
161 NS_ENSURE_SUCCESS(rv, rv);
165 NS_ENSURE_SUCCESS(rv, rv);
176 NS_ASSERTION(aMediaItem,
"no item to look up!");
177 NS_ASSERTION(aLibrary,
"no library to search in!");
178 NS_ASSERTION(aItemCopy,
"null return value pointer!");
182 nsCOMPtr<nsIMutableArray> theCopies =
183 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
184 NS_ENSURE_SUCCESS(rv, rv);
187 NS_ENSURE_SUCCESS(rv, rv);
190 rv = theCopies->GetLength(&count);
191 NS_ENSURE_SUCCESS(rv, rv);
196 NS_ENSURE_SUCCESS(rv, rv);
199 rv = theCopies->GetLength(&count);
200 NS_ENSURE_SUCCESS(rv, rv);
203 nsCOMPtr<sbIMediaItem> item = do_QueryElementAt(theCopies, 0, &rv);
204 NS_ENSURE_SUCCESS(rv, rv);
206 item.forget(aItemCopy);
218 nsIMutableArray * aCopies)
220 NS_ENSURE_ARG_POINTER(aMediaItem);
221 NS_ENSURE_ARG_POINTER(aMediaList);
224 PRBool foundOne = PR_FALSE;
229 if (rv != NS_ERROR_NOT_AVAILABLE) {
230 NS_ENSURE_SUCCESS(rv, rv);
236 if (rv != NS_ERROR_NOT_AVAILABLE) {
237 NS_ENSURE_SUCCESS(rv, rv);
241 if (!url.IsEmpty()) {
245 if (rv != NS_ERROR_NOT_AVAILABLE) {
246 NS_ENSURE_SUCCESS(rv, rv);
249 rv = aCopies->GetLength(&length);
250 NS_ENSURE_SUCCESS(rv, rv);
251 foundOne = length != 0;
260 if (rv != NS_ERROR_NOT_AVAILABLE) {
261 NS_ENSURE_SUCCESS(rv, rv);
264 rv = aCopies->GetLength(&length);
265 NS_ENSURE_SUCCESS(rv, rv);
266 foundOne |= length != 0;
273 return (aCopies || foundOne) ?
NS_OK : NS_ERROR_NOT_AVAILABLE;
278 nsIMutableArray * aCopies)
280 NS_ENSURE_ARG_POINTER(aMediaItem);
281 NS_ENSURE_ARG_POINTER(aList);
286 rv = aMediaItem->GetGuid(guid);
287 NS_ENSURE_SUCCESS(rv, rv);
290 if (rv != NS_ERROR_NOT_AVAILABLE) {
291 NS_ENSURE_SUCCESS(rv, rv);
296 nsString originLibID;
299 if (rv == NS_ERROR_NOT_AVAILABLE || originLibID.IsEmpty()) {
301 return NS_ERROR_NOT_AVAILABLE;
304 NS_ENSURE_SUCCESS(rv, rv);
306 nsString originItemID;
309 if (rv == NS_ERROR_NOT_AVAILABLE || originItemID.IsEmpty()) {
311 return NS_ERROR_NOT_AVAILABLE;
314 NS_ENSURE_SUCCESS(rv, rv);
316 nsCOMPtr<sbIMutablePropertyArray> properties =
318 NS_ENSURE_SUCCESS(rv, rv);
320 rv = properties->AppendProperty(
323 NS_ENSURE_SUCCESS(rv, rv);
326 NS_ENSURE_SUCCESS(rv, rv);
329 if (rv == NS_ERROR_NOT_AVAILABLE) {
331 return NS_ERROR_NOT_AVAILABLE;
334 NS_ENSURE_SUCCESS(rv, rv);
347 nsIMutableArray * aCopies)
349 NS_ENSURE_ARG_POINTER(aMediaItem);
350 NS_ENSURE_ARG_POINTER(aList);
357 if (rv == NS_ERROR_NOT_AVAILABLE || originID.IsEmpty()) {
358 NS_ENSURE_SUCCESS(rv, rv);
360 return NS_ERROR_NOT_AVAILABLE;
364 nsCOMPtr<nsIArray> copies;
367 getter_AddRefs(copies));
368 NS_ENSURE_SUCCESS(rv, rv);
371 rv = copies->GetLength(&count);
372 NS_ENSURE_SUCCESS(rv, rv);
376 NS_ENSURE_SUCCESS(rv, rv);
378 else if (count == 0) {
379 return NS_ERROR_NOT_AVAILABLE;
388 NS_ENSURE_ARG_POINTER(aItem);
391 nsresult rv = aItem->GetContentLength(&contentLength);
393 if(NS_FAILED(rv) || !contentLength) {
395 nsCOMPtr<sbIMediaItem> item(aItem);
397 if (!NS_IsMainThread()) {
401 nsCOMPtr<nsIThread>
target;
402 rv = NS_GetMainThread(getter_AddRefs(target));
407 NS_PROXY_SYNC | NS_PROXY_ALWAYS,
408 getter_AddRefs(item));
409 NS_ENSURE_SUCCESS(rv, rv);
412 nsCOMPtr<nsIURI> contentURI;
413 rv = item->GetContentSrc(getter_AddRefs(contentURI));
414 NS_ENSURE_SUCCESS(rv, rv);
416 nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(contentURI, &rv);
422 nsCOMPtr<nsIFile>
file;
423 rv = fileURL->GetFile(getter_AddRefs(file));
424 NS_ENSURE_SUCCESS(rv, rv);
426 rv = file->GetFileSize(&contentLength);
427 NS_ENSURE_SUCCESS(rv, rv);
431 NS_ENSURE_SUCCESS(rv, rv);
444 NS_ENSURE_ARG_POINTER(aItem);
445 NS_ENSURE_ARG_POINTER(aURI);
449 nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(aURI, &rv);
450 NS_ENSURE_SUCCESS(rv, rv);
454 nsCOMPtr<nsIFile>
file;
455 rv = fileURL->GetFile(getter_AddRefs(file));
456 NS_ENSURE_SUCCESS(rv, rv);
459 rv = file->GetFileSize(&length);
460 NS_ENSURE_SUCCESS(rv, rv);
464 NS_ENSURE_SUCCESS(rv, rv);
473 NS_ENSURE_ARG_POINTER(aItem);
474 NS_ENSURE_ARG_POINTER(_retval);
479 nsAutoString originLibraryGUID;
480 nsAutoString originItemGUID;
483 NS_ENSURE_SUCCESS(rv, rv);
486 NS_ENSURE_SUCCESS(rv, rv);
490 do_GetService(
"@songbirdnest.com/Songbird/library/Manager;1", &rv);
491 NS_ENSURE_SUCCESS(rv, rv);
492 nsCOMPtr<sbILibrary> library;
493 rv = libraryManager->GetLibrary(originLibraryGUID, getter_AddRefs(library));
494 NS_ENSURE_SUCCESS(rv, rv);
495 rv = library->GetItemByGuid(originItemGUID, _retval);
496 NS_ENSURE_SUCCESS(rv, rv);
505 if (NS_IsMainThread()) {
506 return do_GetIOService(&rv);
514 nsIIOService * aIOService)
516 NS_ENSURE_ARG_POINTER(aURI);
517 NS_ENSURE_ARG_POINTER(_retval);
520 nsCOMPtr<nsIURI>
uri = aURI;
523 PRBool compatible = PR_TRUE;
524 #if XP_UNIX && !XP_MACOSX
525 compatible = PR_FALSE;
529 rv = uri->SchemeIs(
"file", &isFileScheme);
530 NS_ENSURE_SUCCESS(rv, rv);
532 if (isFileScheme && compatible) {
535 ioService = aIOService;
538 NS_ENSURE_SUCCESS(rv, rv);
545 rv = uri->GetSpec(spec);
546 NS_ENSURE_SUCCESS(rv, rv);
563 nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(uri, &rv);
564 NS_ENSURE_SUCCESS(rv, rv);
566 nsCOMPtr<nsIFile>
file;
567 rv = fileURL->GetFile(getter_AddRefs(file));
568 NS_ENSURE_SUCCESS(rv, rv);
570 nsCOMPtr<nsIProtocolHandler> protocolHandler;
571 rv = ioService->GetProtocolHandler(
"file",
572 getter_AddRefs(protocolHandler));
573 NS_ENSURE_SUCCESS(rv, rv);
575 nsCOMPtr<nsIFileProtocolHandler> fileProtocolHandler =
576 do_QueryInterface(protocolHandler, &rv);
577 NS_ENSURE_SUCCESS(rv, rv);
579 rv = fileProtocolHandler->GetURLSpecFromActualFile(file, spec);
580 NS_ENSURE_SUCCESS(rv, rv);
583 rv = ioService->NewURI(spec, nsnull, nsnull, getter_AddRefs(uri));
584 NS_ENSURE_SUCCESS(rv, rv);
588 NS_ADDREF(*_retval = uri);
595 PRUint16 *aListContentType)
597 NS_ENSURE_ARG_POINTER(aMediaList);
598 NS_ENSURE_ARG_POINTER(aListContentType);
604 nsCOMPtr<sbILocalDatabaseSmartMediaList> smartList =
605 do_QueryInterface(aMediaList, &rv);
607 rv = aMediaList->GetListContentType(aListContentType);
609 rv = smartList->GetListContentType(aListContentType);
611 NS_ENSURE_SUCCESS(rv, rv);
620 NS_ENSURE_ARG_POINTER(aFile);
621 NS_ENSURE_ARG_POINTER(_retval);
623 nsCOMPtr<nsIURI>
uri;
628 NS_ENSURE_SUCCESS(rv, rv);
633 NS_ENSURE_SUCCESS(rv, rv);
635 NS_ADDREF(*_retval = uri);
648 mMediaItems(aMediaItems)
651 NS_DECL_SBIMEDIALISTENUMERATIONLISTENER
653 nsCOMArray<sbIMediaItem> & mMediaItems;
662 NS_ENSURE_ARG_POINTER(_retval);
667 NS_IMETHODIMP MediaItemArrayCreator::OnEnumeratedItem(
sbIMediaList*,
671 NS_ENSURE_ARG_POINTER(aItem);
672 NS_ENSURE_ARG_POINTER(_retval);
674 PRBool
const added = mMediaItems.AppendObject(aItem);
675 NS_ENSURE_TRUE(added, NS_ERROR_OUT_OF_MEMORY);
682 NS_IMETHODIMP MediaItemArrayCreator::OnEnumerationEnd(
sbIMediaList*,
691 nsAString
const & aPropertyName,
693 nsCOMArray<sbIMediaItem> & aMediaItems) {
695 return aMediaList->EnumerateItemsByProperty(aPropertyName,
708 : mContentType(aContentType)
711 NS_DECL_SBIMEDIALISTENUMERATIONLISTENER
715 return CallQueryInterface(mMediaLists, aMediaLists);
718 nsCOMPtr<nsIMutableArray> mMediaLists;
719 PRUint32 mContentType;
728 NS_ENSURE_ARG_POINTER(_retval);
731 mMediaLists = do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
732 NS_ENSURE_SUCCESS(rv, rv);
738 NS_IMETHODIMP sbLUMediaListEnumerator::OnEnumeratedItem(
sbIMediaList*,
742 NS_ENSURE_ARG_POINTER(aItem);
743 NS_ENSURE_ARG_POINTER(_retval);
744 NS_ENSURE_TRUE(mMediaLists, NS_ERROR_NOT_INITIALIZED);
748 nsCOMPtr<sbIMediaList> list = do_QueryInterface(aItem);
752 PRUint16 contentType;
753 rv = list->GetListContentType(&contentType);
754 NS_ENSURE_SUCCESS(rv, rv);
756 include = (contentType & mContentType) != 0;
759 rv = mMediaLists->AppendElement(list, PR_FALSE);
760 NS_ENSURE_SUCCESS(rv, rv);
769 NS_IMETHODIMP sbLUMediaListEnumerator::OnEnumerationEnd(
sbIMediaList*,
776 PRUint32 aContentType,
777 nsIArray ** aMediaLists)
779 NS_ENSURE_ARG_POINTER(aLibrary);
780 NS_ENSURE_ARG_POINTER(aMediaLists);
785 nsString propTrue(NS_LITERAL_STRING(
"1"));
786 nsString propFalse(NS_LITERAL_STRING(
"0"));
789 nsRefPtr<sbLUMediaListEnumerator> enumerator =
792 nsCOMPtr<sbIMutablePropertyArray> properties =
794 NS_ENSURE_SUCCESS(rv, rv);
796 rv = properties->AppendProperty(propIsList, propTrue);
797 NS_ENSURE_SUCCESS(rv, rv);
798 rv = properties->AppendProperty(propIsHidden, propFalse);
799 NS_ENSURE_SUCCESS(rv, rv);
801 rv = aLibrary->EnumerateItemsByProperties(properties, enumerator, enumType);
802 NS_ENSURE_SUCCESS(rv, rv);
804 rv = enumerator->GetMediaLists(aMediaLists);
805 NS_ENSURE_SUCCESS(rv, rv);
818 nsCOMPtr<sbIPropertyManager> manager =
819 do_GetService(
"@songbirdnest.com/Songbird/Properties/PropertyManager;1",
821 nsCOMPtr<sbIPropertyInfo> info;
823 getter_AddRefs(info));
824 NS_ENSURE_SUCCESS(rv, rv);
827 rv = info->GetOPERATOR_EQUALS(opName);
828 NS_ENSURE_SUCCESS(rv, rv);
831 rv = info->GetOperator(opName, aOperator);
832 NS_ENSURE_SUCCESS(rv, rv);
842 nsAString
const & aListName,
848 nsCOMPtr<nsIArray> mediaLists;
850 NS_LITERAL_STRING(
"1"),
851 getter_AddRefs(mediaLists));
852 if (rv != NS_ERROR_NOT_AVAILABLE) {
853 NS_ENSURE_SUCCESS(rv, rv);
857 rv = mediaLists->GetLength(&listLength);
858 NS_ENSURE_SUCCESS(rv, rv);
860 PRUint32 nameLength = aListName.Length();
861 nsTArray<PRUint64> listIDs;
863 PRUint64 availableId = 1;
864 for (PRUint32
i = 0;
i < listLength; ++
i) {
865 nsCOMPtr<sbIMediaList> mediaList = do_QueryElementAt(mediaLists,
i, &rv);
866 NS_ENSURE_SUCCESS(rv, rv);
868 rv = mediaList->GetName(listName);
869 NS_ENSURE_SUCCESS(rv, rv);
871 if (!listName.IsEmpty()) {
872 nsDependentSubstring subString(listName, 0, nameLength);
873 if (subString.Equals(aListName)) {
874 PRUint32 listNameLength = listName.Length();
875 if (listNameLength == nameLength) {
876 listIDs.AppendElement(1);
879 nsDependentSubstring idString(listName,
883 if (rv == NS_ERROR_INVALID_ARG)
885 listIDs.AppendElement(
id);
892 if (!listIDs.Contains(availableId))
898 if (availableId > 1) {
899 aName.Append(NS_LITERAL_STRING(
" "));
909 NS_ENSURE_ARG_POINTER(aOriginal);
910 NS_ENSURE_ARG_POINTER(aCopy);
914 nsCOMPtr<sbILibrary> originalLibrary;
915 rv = aOriginal->GetLibrary(getter_AddRefs(originalLibrary));
916 NS_ENSURE_SUCCESS(rv, rv);
918 nsCOMPtr<sbILibrary> newLibrary;
919 rv = aCopy->GetLibrary(getter_AddRefs(newLibrary));
920 NS_ENSURE_SUCCESS(rv, rv);
932 if (!originalIsMain && copyIsMain) {
939 else if ((!originalIsMain && !copyIsMain) ||
940 (originalIsMain && copyIsMain)) {
944 nsCOMPtr<sbIMutablePropertyArray> props =
946 "@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1",
948 NS_ENSURE_SUCCESS(rv, rv);
950 nsString originalGuid;
951 rv = aOriginal->GetGuid(originalGuid);
952 NS_ENSURE_SUCCESS(rv, rv);
956 NS_ENSURE_SUCCESS(rv, rv);
958 nsCOMPtr<sbILibrary> originalLib;
959 rv = aOriginal->GetLibrary(getter_AddRefs(originalLib));
960 NS_ENSURE_SUCCESS(rv, rv);
962 nsString originalLibGuid;
963 rv = originalLib->GetGuid(originalLibGuid);
964 NS_ENSURE_SUCCESS(rv, rv);
968 NS_ENSURE_SUCCESS(rv, rv);
971 nsCOMPtr<sbIDeviceManager2> deviceMgr =
972 do_GetService(
"@songbirdnest.com/Songbird/DeviceManager;2", &rv);
973 NS_ENSURE_SUCCESS(rv, rv);
975 nsCOMPtr<sbIDevice> targetDev;
976 rv = deviceMgr->GetDeviceForItem(aCopy, getter_AddRefs(targetDev));
977 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"GetDeviceForItem() failed");
984 nsCOMPtr<sbILibrary> mainLib;
986 NS_ENSURE_SUCCESS(rv, rv);
989 rv = originalLib->Equals(mainLib, &isMainLib);
990 NS_ENSURE_SUCCESS(rv, rv);
992 rv = props->AppendProperty(
994 NS_LITERAL_STRING(
"1"));
995 NS_ENSURE_SUCCESS(rv, rv);
999 rv = aCopy->SetProperties(props);
1000 NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr< nsIIOService > GetIOService(nsresult &rv)
static nsresult GetItemsByProperty(sbIMediaList *aMediaList, nsAString const &aPropertyName, nsAString const &aValue, nsCOMArray< sbIMediaItem > &aMediaItems)
static nsresult LinkCopy(sbIMediaItem *aOriginal, sbIMediaItem *aCopy)
Links a copy to its original. It will take into account the libraries the items belong to...
static nsresult GetContentLength(sbIMediaItem *aItem, PRInt64 *_retval=nsnull)
static nsresult FindByProperties(sbIMediaList *aList, sbIPropertyArray *aProperties, nsIArray *aCopies)
#define SB_PROPERTY_ORIGINLIBRARYGUID
static nsresult GetItemInLibrary(sbIMediaItem *aItem, sbILibrary *aLibrary, sbIMediaItem **_retval)
#define SB_PROPERTY_ORIGINURL
PRUint64 nsString_ToUint64(const nsAString &str, nsresult *rv)
static nsresult GetMediaListByContentType(sbILibrary *aLibrary, PRUint32 aContentType, nsIArray **aMediaLists)
static nsresult GetFileContentURI(nsIFile *aFile, nsIURI **_retval)
Return a library content URI for the file specified by aFile. Special processing is required to conve...
nsresult sbAppendnsIArray(nsIArray *aSrc, nsIMutableArray *aDest, PRBool aWeak=PR_FALSE, PRUint32 aElementsToCopy=0)
static nsresult FindByOrigin(sbIMediaList *aList, nsString const &aOriginLibGuid, nsString const &aOriginItemGuid, nsIArray *aCopies)
static nsresult FindCopiesByID(sbIMediaItem *aMediaItem, sbIMediaList *aList, nsIMutableArray *aCopies)
#define SB_PROPERTY_HIDDEN
static nsresult SetContentLength(sbIMediaItem *aItem, nsIURI *aURI)
static nsresult GetOriginItem(sbIMediaItem *aItem, sbIMediaItem **_retval)
nsresult do_GetProxyForObject(nsIEventTarget *aTarget, REFNSIID aIID, nsISupports *aObj, PRInt32 aProxyType, void **aProxyObject)
static nsresult GetContentURI(nsIURI *aURI, nsIURI **_retval, nsIIOService *aIOService=nsnull)
Return a library content URI for the URI specified by aURI. A library content URI is a specially form...
#define SB_PROPERTY_CONTENTLENGTH
const sbCreateProxiedComponent do_ProxiedGetService(const nsCID &aCID, nsresult *error=0)
An interface to represent an operator that may act upon a property.
#define SB_PROPERTY_CONTENTTYPE
BogusChannel prototype contentLength
PRBool sbIsMainLibrary(sbILibrary *aLibrary)
static nsresult SuggestUniqueNameForPlaylist(sbILibrary *aLibrary, nsAString const &aListName, nsAString &aName)
Suggest a unique name for playlist.
static nsresult GetMediaListContentType(sbIMediaList *aMediaList, PRUint16 *aListContentType)
Return a media list content type.
#define SB_PROPERTY_ORIGIN_IS_IN_MAIN_LIBRARY
Media library abstraction.
static nsresult FindOriginalsByID(sbIMediaItem *aMediaItem, sbIMediaList *aList, nsIMutableArray *aCopies)
NS_IMPL_ISUPPORTS1(MediaItemArrayCreator, sbIMediaListEnumerationListener) NS_IMETHODIMP MediaItemArrayCreator
static nsresult FindItemsWithSameURL(sbIMediaItem *aMediaItem, sbIMediaList *aMediaList, nsIMutableArray *aCopies)
static nsresult FindByContentURL(sbIMediaList *aList, nsString const &aContentURL, nsIArray *aCopies)
nsresult GetMainLibrary(sbILibrary **aMainLibrary)
#define SB_PROPERTY_ISLIST
static PRUint32 GetEqualOperator(sbIPropertyOperator **aOperator)
#define SB_PROPERTY_CONTENTURL
An interface to carry around arrays of nsIProperty instances. Users of this interface should only QI ...
nsresult sbNewFileURI(nsIFile *aFile, nsIURI **aURI)
#define SB_PROPERTY_ORIGINITEMGUID
_getSelectedPageStyle s i
static nsresult FindByOriginURL(sbIMediaList *aList, nsString const &aOriginURL, nsIArray *aCopies)