29 #include <sbILibraryUtils.h>
30 #include <sbIMediaItem.h>
31 #include <sbIPropertyInfo.h>
42 #include <nsDirectoryServiceDefs.h>
43 #include <nsStringAPI.h>
45 #include <nsIFileURL.h>
46 #include <nsIPrefService.h>
47 #include <nsIPropertyBag2.h>
48 #include <nsIStringBundle.h>
49 #include <nsUnicharUtils.h>
51 #define PERMISSIONS_FILE 0644
53 #ifndef MFM_MUSIC_FOLDER_KEY
54 # if defined(XP_MACOSX)
55 # define MFM_MUSIC_FOLDER_KEY NS_OSX_MUSIC_DOCUMENTS_DIR
56 # elif defined (XP_UNIX)
57 # define MFM_MUSIC_FOLDER_KEY NS_UNIX_XDG_MUSIC_DIR
59 # define MFM_MUSIC_FOLDER_KEY "Music"
61 #endif // #ifndef MFM_MUSIC_FOLDER_KEY
63 #ifndef MFM_VIDEO_FOLDER_KEY
64 # if defined(XP_MACOSX)
65 # define MFM_VIDEO_FOLDER_KEY NS_OSX_MOVIE_DOCUMENTS_DIR
66 # elif defined (XP_UNIX)
67 # define MFM_VIDEO_FOLDER_KEY NS_UNIX_XDG_VIDEOS_DIR
69 # define MFM_VIDEO_FOLDER_KEY "Video"
71 #endif // #ifndef MFM_VIDEO_FOLDER_KEY
73 #define _MFM_SEPARATOR ":"
86 static PRLogModuleInfo* gMediaFileManagerLog = nsnull;
87 #define TRACE(args) PR_LOG(gMediaFileManagerLog, PR_LOG_DEBUG, args)
88 #define LOG(args) PR_LOG(gMediaFileManagerLog, PR_LOG_WARN, args)
90 #define __FUNCTION__ __PRETTY_FUNCTION__
104 if (!gMediaFileManagerLog) {
105 gMediaFileManagerLog = PR_NewLogModule(
"sbMediaFileManager");
108 TRACE((
"%s", __FUNCTION__));
116 TRACE((
"%s", __FUNCTION__));
124 TRACE((
"%s", __FUNCTION__));
126 NS_NAMED_LITERAL_STRING(KEY_FILE_FORMAT,
"file-format");
131 nsCOMPtr<nsIPropertyBag2> properties = aProperties;
133 properties = do_CreateInstance(
"@mozilla.org/hash-property-bag;1");
134 NS_ENSURE_TRUE(properties, NS_ERROR_OUT_OF_MEMORY);
138 mNetUtil = do_GetService(
"@mozilla.org/network/util;1", &rv);
139 NS_ENSURE_SUCCESS(rv, rv);
143 NS_ENSURE_SUCCESS(rv, rv);
146 nsCOMPtr<nsIPrefService> prefRoot =
147 do_GetService(
"@mozilla.org/preferences-service;1", &rv);
148 NS_ENSURE_SUCCESS (rv, rv);
151 nsCOMPtr<nsIThread> mainThread;
152 rv = NS_GetMainThread(getter_AddRefs(mainThread));
153 NS_ENSURE_SUCCESS(rv, rv);
154 nsCOMPtr<nsIPrefBranch> prefBranch;
155 rv = prefRoot->GetBranch(
PREF_MFM_ROOT, getter_AddRefs(prefBranch));
156 NS_ENSURE_SUCCESS(rv, rv);
159 NS_PROXY_SYNC | NS_PROXY_ALWAYS,
160 getter_AddRefs(mPrefBranch));
161 NS_ENSURE_SUCCESS(rv, rv);
164 nsCString fileFormatString;
165 rv = properties->HasKey(KEY_FILE_FORMAT, &hasKey);
166 NS_ENSURE_SUCCESS(rv, rv);
168 rv = properties->GetPropertyAsACString(KEY_FILE_FORMAT, fileFormatString);
169 NS_ENSURE_SUCCESS(rv, rv);
172 getter_Copies(fileFormatString));
173 NS_ENSURE_SUCCESS(rv, rv);
179 NS_LITERAL_STRING(
","),
182 rv = InitFolderNameTemplates(properties);
183 NS_ENSURE_SUCCESS(rv, rv);
185 rv = InitMediaFoldersMap(properties);
186 NS_ENSURE_SUCCESS(rv, rv);
188 mInitialized = PR_TRUE;
201 sbMediaFileManager::OrganizeItem(
sbIMediaItem *aMediaItem,
202 unsigned short aManageType,
203 nsIFile *aForceTargetFile,
206 TRACE((
"%s", __FUNCTION__));
207 NS_ENSURE_ARG_POINTER(aMediaItem);
208 NS_ENSURE_ARG_POINTER(aRetVal);
209 NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
215 if (aManageType == 0) {
217 TRACE((
"%s - no action requested (%04x)", __FUNCTION__, aManageType));
218 return NS_ERROR_INVALID_ARG;
229 rv = CheckManagementFolder(aMediaItem);
230 NS_ENSURE_SUCCESS(rv, rv);
235 nsCOMPtr<nsIURI> itemUri;
236 rv = aMediaItem->GetContentSrc(getter_AddRefs(itemUri));
237 NS_ENSURE_SUCCESS (rv, rv);
240 nsCOMPtr<nsIFileURL> fileUrl(do_QueryInterface(itemUri, &rv));
241 if (NS_FAILED(rv) || !fileUrl) {
244 rv = itemUri->GetSpec(spec);
245 if (NS_SUCCEEDED(rv)) {
246 TRACE((
"%s: item %s is not a local file", __FUNCTION__, spec.get()));
249 return NS_ERROR_INVALID_ARG;
253 nsCOMPtr<nsIFile> itemFile;
254 rv = fileUrl->GetFile(getter_AddRefs(itemFile));
255 NS_ENSURE_SUCCESS(rv, rv);
259 rv = itemFile->Exists(&exists);
260 if (NS_FAILED(rv) || !exists) {
261 return NS_ERROR_FILE_NOT_FOUND;
265 rv =
Delete(itemFile, aRetVal);
266 NS_ENSURE_SUCCESS(rv, rv);
272 if (aManageType != sbIMediaFileManager::MANAGE_DELETE) {
273 LOG((
"%s: Recieved a MANAGE_DELETE with other flags (%04x).",
286 LOG((
"%s: nothing to manage", __FUNCTION__));
291 nsCOMPtr<nsIFile> newFile;
293 if (aForceTargetFile) {
296 rv = aForceTargetFile->GetPath(path);
297 if (NS_SUCCEEDED(rv)) {
298 TRACE((
"%s: has forced file [%s]",
300 NS_ConvertUTF16toUTF8(path).
get()));
302 TRACE((
"%s: has forced file, unknown path (error %08x)", __FUNCTION__, rv));
305 rv = aForceTargetFile->Clone(getter_AddRefs(newFile));
306 NS_ENSURE_SUCCESS(rv, rv);
308 rv = GetManagedPath(aMediaItem, aManageType, getter_AddRefs(newFile));
309 NS_ENSURE_SUCCESS(rv, rv);
310 if (rv == NS_SUCCESS_LOSS_OF_INSIGNIFICANT_DATA) {
312 LOG((
"%s: failed to get managed path", __FUNCTION__));
317 rv = newFile->GetPath(path);
318 if (NS_SUCCEEDED(rv)) {
319 TRACE((
"%s: has a managed file [%s]",
321 NS_ConvertUTF16toUTF8(path).
get()));
323 TRACE((
"%s: has managed file, unknown path (error %08x)",
331 PRBool isOrganized = PR_FALSE;
332 rv = newFile->Equals(itemFile, &isOrganized);
333 NS_ENSURE_SUCCESS(rv, rv);
337 TRACE((
"%s: Already organized", __FUNCTION__));
344 rv =
CopyRename(aMediaItem, itemFile, newFile, aRetVal);
345 NS_ENSURE_SUCCESS(rv, rv);
359 PRUint16 aManageType,
362 TRACE((
"%s", __FUNCTION__));
363 NS_ENSURE_ARG_POINTER(aItem);
364 NS_ENSURE_ARG_POINTER(_retval);
365 NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
367 if (aManageType == 0) {
369 return NS_ERROR_INVALID_ARG;
375 rv = CheckManagementFolder(aItem);
376 NS_ENSURE_SUCCESS(rv, rv);
381 nsCOMPtr<nsIURI> itemUri;
382 rv = aItem->GetContentSrc(getter_AddRefs(itemUri));
383 NS_ENSURE_SUCCESS (rv, rv);
386 nsCOMPtr<nsIFile> oldItemFile;
388 nsCOMPtr<nsIFileURL> fileUrl(do_QueryInterface(itemUri, &rv));
389 if (NS_SUCCEEDED(rv)) {
391 rv = fileUrl->GetFile(getter_AddRefs(oldItemFile));
392 NS_ENSURE_SUCCESS(rv, rv);
394 if (aManageType & sbIMediaFileManager::MANAGE_DELETE) {
396 oldItemFile.forget(_retval);
401 else if (aManageType & sbIMediaFileManager::MANAGE_DELETE) {
402 return NS_ERROR_INVALID_ARG;
406 nsCOMPtr<nsIFile> newItemFile;
413 if (aManageType & sbIMediaFileManager::MANAGE_MOVE) {
416 NS_ENSURE_SUCCESS(rv, rv);
417 NS_ENSURE_TRUE(success, NS_SUCCESS_LOSS_OF_INSIGNIFICANT_DATA);
418 nsCOMPtr<nsILocalFile> localFile =
419 do_CreateInstance(
"@mozilla.org/file/local;1", &rv);
420 NS_ENSURE_SUCCESS(rv, rv);
421 rv = localFile->InitWithPath(path);
422 NS_ENSURE_SUCCESS(rv, rv);
423 newItemFile = do_QueryInterface(localFile, &rv);
424 NS_ENSURE_SUCCESS(rv, rv);
425 }
else if (aManageType & sbIMediaFileManager::MANAGE_COPY) {
428 nsCOMPtr<nsIFile> mediaFolder;
429 rv = GetMediaFolder(aItem, getter_AddRefs(mediaFolder));
430 NS_ENSURE_SUCCESS(rv, NS_SUCCESS_LOSS_OF_INSIGNIFICANT_DATA);
431 rv = mediaFolder->Clone(getter_AddRefs(newItemFile));
432 NS_ENSURE_SUCCESS(rv, NS_SUCCESS_LOSS_OF_INSIGNIFICANT_DATA);
435 return NS_SUCCESS_LOSS_OF_INSIGNIFICANT_DATA;
438 if (aManageType & sbIMediaFileManager::MANAGE_RENAME) {
440 NS_ENSURE_SUCCESS(rv, rv);
443 NS_ENSURE_TRUE(success, NS_SUCCESS_LOSS_OF_INSIGNIFICANT_DATA);
444 }
else if (oldItemFile) {
446 nsCOMPtr<sbILibraryUtils> libUtils =
447 do_GetService(
"@songbirdnest.com/Songbird/library/Manager;1", &rv);
448 NS_ENSURE_SUCCESS(rv, rv);
449 nsCOMPtr<nsIFile> canonicalOldFile;
450 rv = libUtils->GetCanonicalPath(oldItemFile,
451 getter_AddRefs(canonicalOldFile));
452 NS_ENSURE_SUCCESS(rv, rv);
453 rv = canonicalOldFile->GetLeafName(filename);
454 NS_ENSURE_SUCCESS(rv, rv);
457 nsCAutoString cFileName;
459 NS_ENSURE_SUCCESS(rv, rv);
460 rv = itemURL->GetFileName(cFileName);
461 NS_ENSURE_SUCCESS(rv, rv);
462 filename = NS_ConvertUTF8toUTF16(cFileName);
464 rv = newItemFile->Append(filename);
465 NS_ENSURE_SUCCESS(rv, rv);
467 newItemFile.forget(_retval);
473 sbMediaFileManager::InitMediaFoldersMap(nsIPropertyBag2 * aProperties)
475 TRACE((
"%s", __FUNCTION__));
480 mMediaFolders.Init(3);
482 PRBool ok = PR_FALSE;
486 nsCOMPtr<nsIFile> customFolder;
488 NS_NAMED_LITERAL_STRING(KEY_CUSTOM_MEDIA_FOLDER,
"media-folder");
489 PRBool hasKey = PR_FALSE;
490 rv = aProperties->HasKey(KEY_CUSTOM_MEDIA_FOLDER, &hasKey);
491 NS_ENSURE_SUCCESS(rv, rv);
493 rv = aProperties->GetPropertyAsInterface(KEY_CUSTOM_MEDIA_FOLDER,
495 getter_AddRefs(customFolder));
496 NS_ENSURE_SUCCESS(rv, rv);
502 ok = mMediaFolders.Put(EmptyString(), customFolder);
503 NS_ENSURE_TRUE(ok, NS_ERROR_OUT_OF_MEMORY);
507 nsCOMPtr<nsIProperties> dirService =
508 do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
509 NS_ENSURE_SUCCESS(rv, rv);
514 nsCOMPtr<nsIFile> musicDir = customFolder;
516 NS_NAMED_LITERAL_STRING(KEY_CUSTOM_MUSIC_FOLDER,
"media-folder:audio");
517 PRBool hasKey = PR_FALSE;
518 rv = aProperties->HasKey(KEY_CUSTOM_MUSIC_FOLDER, &hasKey);
519 NS_ENSURE_SUCCESS(rv, rv);
521 aProperties->GetPropertyAsInterface(KEY_CUSTOM_MUSIC_FOLDER,
523 getter_AddRefs(musicDir));
524 NS_ENSURE_SUCCESS(rv, rv);
533 getter_AddRefs(musicDir));
538 ok = mMediaFolders.Put(NS_LITERAL_STRING(
"audio"), musicDir);
539 NS_ENSURE_TRUE(ok, NS_ERROR_OUT_OF_MEMORY);
542 NS_WARNING(
"Could not get music folder");
548 nsCOMPtr<nsIFile> videoDir = customFolder;
550 NS_NAMED_LITERAL_STRING(KEY_CUSTOM_VIDEO_FOLDER,
"media-folder:video");
551 PRBool hasKey = PR_FALSE;
552 rv = aProperties->HasKey(KEY_CUSTOM_VIDEO_FOLDER, &hasKey);
553 NS_ENSURE_SUCCESS(rv, rv);
555 aProperties->GetPropertyAsInterface(KEY_CUSTOM_VIDEO_FOLDER,
557 getter_AddRefs(videoDir));
558 NS_ENSURE_SUCCESS(rv, rv);
567 getter_AddRefs(videoDir));
572 ok = mMediaFolders.Put(NS_LITERAL_STRING(
"video"), videoDir);
573 NS_ENSURE_TRUE(ok, NS_ERROR_OUT_OF_MEMORY);
576 NS_WARNING(
"Could not get video folder");
583 sbMediaFileManager::GetMediaFolder(
sbIMediaItem * aMediaItem,
586 TRACE((
"%s", __FUNCTION__));
587 NS_ENSURE_ARG_POINTER(aMediaItem);
588 NS_ENSURE_ARG_POINTER(aFolder);
594 nsAutoString contentType;
595 rv = aMediaItem->GetContentType(contentType);
596 NS_ENSURE_SUCCESS(rv, rv);
598 PRBool defined = mMediaFolders.Get(contentType, aFolder);
602 defined = mMediaFolders.Get(EmptyString(), aFolder);
603 NS_ENSURE_TRUE(defined, NS_ERROR_NOT_AVAILABLE);
607 NS_ENSURE_TRUE(*aFolder, NS_ERROR_NOT_AVAILABLE);
608 (*aFolder)->AddRef();
616 struct OnEachMediaFolder_t
625 OnEachMediaFolder(nsStringHashKey::KeyType aKey,
629 TRACE((
"%s", __FUNCTION__));
633 NS_ENSURE_TRUE(aData, PL_DHASH_NEXT);
634 NS_ENSURE_TRUE(userArg, PL_DHASH_STOP);
638 const OnEachMediaFolder_t * context =
639 static_cast<const OnEachMediaFolder_t *
>(userArg);
640 NS_ENSURE_TRUE(context->mFile, PL_DHASH_STOP);
641 NS_ENSURE_TRUE(context->mFolder, PL_DHASH_STOP);
645 nsAutoString contentType;
647 nsAutoString mediaFolder;
648 aData->GetPath(mediaFolder);
649 nsAutoString itemPath;
650 context->mFile->GetPath(itemPath);
653 PRBool found = PR_FALSE;
654 rv = aData->Contains(context->mFile, PR_TRUE, &found);
655 NS_ENSURE_SUCCESS(rv, PL_DHASH_NEXT);
660 return PL_DHASH_NEXT;
664 NS_ADDREF(*context->mFolder = aData);
665 return PL_DHASH_STOP;
670 sbMediaFileManager::GetMediaFolder(nsIFile * aFile,
673 TRACE((
"%s", __FUNCTION__));
676 OnEachMediaFolder_t context;
677 context.mFile = aFile;
678 context.mFolder = aFolder;
679 mMediaFolders.EnumerateRead(OnEachMediaFolder, &context);
685 sbMediaFileManager::InitFolderNameTemplates(nsIPropertyBag2 * aProperties)
687 TRACE((
"%s", __FUNCTION__));
690 NameTemplate nameTemplate;
692 PRBool ok = PR_FALSE;
694 mFolderNameTemplates.Init();
699 NS_NAMED_LITERAL_STRING(KEY_CUSTOM_DIR_TEMPLATE,
"dir-format");
700 PRBool hasKey = PR_FALSE;
701 rv = aProperties->HasKey(KEY_CUSTOM_DIR_TEMPLATE, &hasKey);
702 NS_ENSURE_SUCCESS(rv, rv);
704 nsCString templateAsString;
705 rv = aProperties->GetPropertyAsACString(KEY_CUSTOM_DIR_TEMPLATE,
707 NS_ENSURE_SUCCESS(rv, rv);
713 NS_LITERAL_STRING(
","),
715 ok = mFolderNameTemplates.Put(EmptyString(), nameTemplate);
716 NS_ENSURE_TRUE(ok, NS_ERROR_OUT_OF_MEMORY);
744 nameTemplate.Clear();
745 ok = mFolderNameTemplates.Put(EmptyString(), nameTemplate);
746 NS_ENSURE_TRUE(ok, NS_ERROR_OUT_OF_MEMORY);
749 nameTemplate.Clear();
752 NS_ERROR_OUT_OF_MEMORY);
754 nameTemplate.AppendElement(NS_LITERAL_STRING(FILE_PATH_SEPARATOR)),
755 NS_ERROR_OUT_OF_MEMORY);
758 NS_ERROR_OUT_OF_MEMORY);
760 nameTemplate.AppendElement(NS_LITERAL_STRING(FILE_PATH_SEPARATOR)),
761 NS_ERROR_OUT_OF_MEMORY);
764 ok = mFolderNameTemplates.Put(key, nameTemplate);
765 NS_ENSURE_TRUE(ok, NS_ERROR_OUT_OF_MEMORY);
771 recordingsFolder.AppendLiteral(FILE_PATH_SEPARATOR);
773 nameTemplate.Clear();
775 nameTemplate.AppendElement(EmptyString()),
776 NS_ERROR_OUT_OF_MEMORY);
778 nameTemplate.AppendElement(recordingsFolder),
779 NS_ERROR_OUT_OF_MEMORY);
784 ok = mFolderNameTemplates.Put(key, nameTemplate);
785 NS_ENSURE_TRUE(ok, NS_ERROR_OUT_OF_MEMORY);
790 ok = mFolderNameTemplates.Put(key, nameTemplate);
791 NS_ENSURE_TRUE(ok, NS_ERROR_OUT_OF_MEMORY);
797 sbMediaFileManager::GetFolderNameTemplate(
sbIMediaItem * aMediaItem,
798 NameTemplate & aNameTemplate)
800 TRACE((
"%s", __FUNCTION__));
801 NS_ENSURE_ARG_POINTER(aMediaItem);
807 static const char *
const ORGANIZING_PROPERTIES[] = {
811 for (
size_t i = 0;
i < NS_ARRAY_LENGTH(ORGANIZING_PROPERTIES);
i++) {
819 key.AssignLiteral(ORGANIZING_PROPERTIES[
i]);
820 rv = aMediaItem->GetProperty(key, suffix);
821 NS_ENSURE_SUCCESS(rv, rv);
823 key.AppendLiteral(_MFM_SEPARATOR);
827 if (mFolderNameTemplates.Get(key, &aNameTemplate)) {
835 if (mFolderNameTemplates.Get(EmptyString(), &aNameTemplate)) {
841 aNameTemplate.Clear();
859 TRACE((
"%s", __FUNCTION__));
860 NS_ENSURE_ARG_POINTER(aMediaItem);
861 NS_ENSURE_ARG_POINTER(aItemUri);
862 NS_ENSURE_ARG_POINTER(aRetVal);
871 aFilename.Truncate();
876 nsCOMPtr<nsIURL>
url(do_QueryInterface(aItemUri, &rv));
885 rv = url->GetFileExtension(extension);
886 NS_ENSURE_SUCCESS(rv, rv);
889 nsString fullExtension;
890 if (!extension.IsEmpty()) {
891 fullExtension.Insert(PRUnichar(
'.'), 0);
892 fullExtension.Append(NS_ConvertUTF8toUTF16(extension));
895 rv = GetFormattedFileFolder(mTrackNameTemplate,
901 NS_ENSURE_SUCCESS(rv, rv);
905 if (aFilename.IsEmpty()) {
911 if (!fullExtension.IsEmpty()) {
912 aFilename.Append(fullExtension);
932 TRACE((
"%s", __FUNCTION__));
933 NS_ENSURE_ARG_POINTER(aMediaItem);
934 NS_ENSURE_ARG_POINTER(aRetVal);
943 rv = CheckManagementFolder(aMediaItem);
944 NS_ENSURE_SUCCESS(rv, rv);
947 nsCOMPtr<nsIFile> mediaFolder;
948 rv = GetMediaFolder(aMediaItem, getter_AddRefs(mediaFolder));
949 NS_ENSURE_SUCCESS(rv, rv);
950 rv = mediaFolder->GetPath(aPath);
951 NS_ENSURE_SUCCESS(rv, rv);
953 NS_ENSURE_SUCCESS(rv, rv);
956 NameTemplate folderNameTemplate;
957 rv = GetFolderNameTemplate(aMediaItem, folderNameTemplate);
958 NS_ENSURE_SUCCESS(rv, rv);
959 rv = GetFormattedFileFolder(folderNameTemplate,
965 NS_ENSURE_SUCCESS(rv, rv);
973 sbMediaFileManager::RemoveBadCharacters(nsString& aStringToParse)
975 TRACE((
"%s", __FUNCTION__));
979 aStringToParse.StripChars(FILE_ILLEGAL_CHARACTERS);
983 aStringToParse.StripChars(FILE_PATH_SEPARATOR);
989 aStringToParse.Trim(
" .", PR_TRUE, PR_TRUE);
993 sbMediaFileManager::GetUnknownValue(nsString aPropertyKey,
994 nsString& aUnknownValue)
996 TRACE((
"%s (%s)", __FUNCTION__, NS_ConvertUTF16toUTF8(aPropertyKey).BeginReading()));
1001 aUnknownValue.Truncate();
1006 nsCString defaultPrefKey;
1008 defaultPrefKey.Append(NS_ConvertUTF16toUTF8(aPropertyKey));
1011 rv = mPrefBranch->PrefHasUserValue(defaultPrefKey.get(), &prefExists);
1012 NS_ENSURE_SUCCESS(rv, rv);
1017 rv = mPrefBranch->GetCharPref(defaultPrefKey.get(),
1018 getter_Copies(value));
1019 NS_ENSURE_SUCCESS(rv, rv);
1020 aUnknownValue.Assign(NS_ConvertUTF8toUTF16(value));
1023 nsCOMPtr<sbIPropertyInfo> info;
1024 rv = mPropertyManager->GetPropertyInfo(aPropertyKey,
1025 getter_AddRefs(info));
1026 NS_ENSURE_SUCCESS(rv, rv);
1031 nsString unknownKey;
1032 rv = info->GetLocalizationKey(unknownKey);
1033 NS_ENSURE_SUCCESS(rv, rv);
1034 unknownKey.Insert(NS_LITERAL_STRING(
"."), 0);
1036 aUnknownValue.Assign(stringBundle.
Get(unknownKey, aPropertyKey));
1038 if (aUnknownValue.Equals(aPropertyKey) ||
1039 aUnknownValue.EqualsLiteral(
"%S"))
1043 nsString propertyDisplayName;
1044 rv = info->GetDisplayName(propertyDisplayName);
1045 NS_ENSURE_SUCCESS(rv, rv);
1047 if (propertyDisplayName.IsEmpty()) {
1051 nsTArray<nsString> params;
1052 params.AppendElement(propertyDisplayName);
1059 rv = mPrefBranch->SetCharPref(defaultPrefKey.get(),
1060 NS_ConvertUTF16toUTF8(aUnknownValue).get());
1061 NS_ENSURE_SUCCESS(rv, rv);
1085 sbMediaFileManager::GetFormattedFileFolder(
1086 const NameTemplate & aNameTemplate,
1088 PRBool aAppendProperty,
1089 PRBool aTrimEachProperty,
1090 nsString aFileExtension,
1093 TRACE((
"%s", __FUNCTION__));
1094 NS_ENSURE_ARG_POINTER(aMediaItem);
1098 for (PRUint32 i=0; i< aNameTemplate.Length(); i++) {
1099 nsString
const & configValue = aNameTemplate[
i];
1101 if (configValue.IsEmpty()) {
1108 nsCAutoString unescapedValue;
1109 rv = mNetUtil->UnescapeString(NS_ConvertUTF16toUTF8(configValue),
1110 nsINetUtil::ESCAPE_ALL,
1112 NS_ENSURE_SUCCESS(rv, rv);
1113 aRetVal.AppendLiteral(unescapedValue.get());
1117 nsString propertyValue;
1118 rv = aMediaItem->GetProperty(configValue, propertyValue);
1119 NS_ENSURE_SUCCESS(rv, rv);
1121 if (!propertyValue.IsEmpty()) {
1122 if (aTrimEachProperty) {
1123 RemoveBadCharacters(propertyValue);
1128 PRBool shouldPadTrackNum = PR_FALSE;
1130 if (shouldPadTrackNum &&
1135 nsString numTracksStr;
1136 aMediaItem->GetProperty(
1140 nsString formattedProperty;
1144 if (NS_SUCCEEDED(rv)) {
1145 propertyValue = formattedProperty;
1151 if (!aFileExtension.IsEmpty() &&
1153 if (StringEndsWith(propertyValue, aFileExtension)) {
1154 propertyValue.SetLength(propertyValue.Length() - aFileExtension.Length());
1160 if (propertyValue.IsEmpty()) {
1161 TRACE((
"%s: getting fallback for %s",
1163 NS_ConvertUTF16toUTF8(configValue).
get()));
1164 rv = GetUnknownValue(configValue, propertyValue);
1165 if (NS_FAILED(rv) || propertyValue.IsEmpty()) {
1171 if (aTrimEachProperty) {
1172 RemoveBadCharacters(propertyValue);
1177 aRetVal.Append(propertyValue);
1181 if (!aTrimEachProperty && !aRetVal.IsEmpty()) {
1182 RemoveBadCharacters(aRetVal);
1193 sbMediaFileManager::CheckManagementFolder(
sbIMediaItem * aMediaItem)
1195 TRACE((
"%s", __FUNCTION__));
1196 NS_ENSURE_ARG_POINTER(aMediaItem);
1200 nsCOMPtr<nsIFile> mediaFolder;
1201 rv = GetMediaFolder(aMediaItem, getter_AddRefs(mediaFolder));
1202 NS_ENSURE_SUCCESS (rv, rv);
1205 return NS_ERROR_FAILURE;
1210 PRBool exists = PR_FALSE;
1211 rv = mediaFolder->Exists(&exists);
1212 NS_ENSURE_SUCCESS(rv, rv);
1213 NS_ENSURE_TRUE(exists, NS_ERROR_FILE_TARGET_DOES_NOT_EXIST);
1225 TRACE((
"%s", __FUNCTION__));
1228 nsString separator = NS_LITERAL_STRING(FILE_PATH_SEPARATOR);
1229 if (!aDir.IsEmpty() &&
1230 aDir.CharAt(aDir.Length()-1) != separator.CharAt(0)) {
1231 aDir.Append(separator);
1247 const nsAString & aTotalTrackCountStr,
1248 nsString & aOutString)
1250 nsString format(aTrackNumStr);
1251 nsString totalStr(aTotalTrackCountStr);
1253 PRInt32 padCount = 0;
1256 if (format.Length() < totalStr.Length()) {
1257 padCount = totalStr.Length() - format.Length();
1259 else if (totalStr.Length() == 0) {
1262 padCount = 2 - format.Length();
1266 for (PRInt32 i = 0; i < padCount; i++) {
1267 format.Insert(NS_LITERAL_STRING(
"0").
get(), 0, 1);
1271 aOutString = format;
1290 TRACE((
"%s", __FUNCTION__));
1291 NS_ENSURE_ARG_POINTER(aMediaItem);
1292 NS_ENSURE_ARG_POINTER(aSrcFile);
1293 NS_ENSURE_ARG_POINTER(aDestFile);
1294 NS_ENSURE_ARG_POINTER(aRetVal);
1298 *aRetVal = PR_FALSE;
1303 rv = CheckManagementFolder(aMediaItem);
1304 NS_ENSURE_SUCCESS(rv, rv);
1307 PRBool isSrcDestSame = PR_FALSE;
1308 rv = aSrcFile->Equals(aDestFile, &isSrcDestSame);
1309 NS_ENSURE_SUCCESS(rv, rv);
1310 if (isSrcDestSame) {
1311 return NS_ERROR_INVALID_ARG;
1315 nsCOMPtr<nsIFile> mediaFolder;
1316 rv = GetMediaFolder(aDestFile, getter_AddRefs(mediaFolder));
1317 NS_ENSURE_SUCCESS(rv, rv);
1320 return NS_ERROR_INVALID_ARG;
1327 rv = aDestFile->GetPath(path);
1328 NS_ASSERTION(NS_SUCCEEDED(rv),
"Failed to get dest path");
1329 TRACE((
"%s: creating file %s",
1331 NS_ConvertUTF16toUTF8(path).
get()));
1335 NS_ENSURE_SUCCESS(rv, rv);
1338 rv = aDestFile->Remove(PR_FALSE);
1339 NS_ENSURE_SUCCESS(rv, rv);
1342 nsString newDestPath;
1343 rv = aDestFile->GetPath(newDestPath);
1344 if (NS_FAILED(rv)) {
1345 TRACE((
"%s: Unable to get path we are copying to?", __FUNCTION__));
1347 TRACE((
"%s: Copying to %s", __FUNCTION__,
1348 NS_ConvertUTF16toUTF8(newDestPath).
get()));
1354 rv = aSrcFile->GetPath(oldPath);
1355 NS_ENSURE_SUCCESS(rv, rv);
1359 rv = aDestFile->GetPath(newPath);
1360 NS_ENSURE_SUCCESS(rv, rv);
1362 if (!mWatchFolderService) {
1363 mWatchFolderService =
1364 do_GetService(
"@songbirdnest.com/watch-folder-service;1", &rv);
1365 NS_ENSURE_SUCCESS(rv, rv);
1367 rv = mWatchFolderService->AddIgnoreCount(oldPath, 1);
1368 NS_ENSURE_SUCCESS(rv, rv);
1369 rv = mWatchFolderService->AddIgnoreCount(newPath, 1);
1370 NS_ENSURE_SUCCESS(rv, rv);
1373 nsString newFilename;
1374 rv = aDestFile->GetLeafName(newFilename);
1375 NS_ENSURE_SUCCESS(rv, rv);
1378 nsCOMPtr<nsIFile> newParentDir;
1379 rv = aDestFile->GetParent(getter_AddRefs(newParentDir));
1380 NS_ENSURE_SUCCESS(rv, rv);
1383 mediaFolder = nsnull;
1384 rv = GetMediaFolder(aSrcFile, getter_AddRefs(mediaFolder));
1385 NS_ENSURE_SUCCESS(rv, rv);
1388 rv = aSrcFile->CopyTo(newParentDir, newFilename);
1389 NS_ENSURE_SUCCESS(rv, rv);
1394 nsCOMPtr<nsIFile> holdOldFile;
1395 rv = aSrcFile->Clone(getter_AddRefs(holdOldFile));
1396 NS_ENSURE_SUCCESS(rv, rv);
1398 rv = aSrcFile->MoveTo(newParentDir, newFilename);
1399 NS_ENSURE_SUCCESS(rv, rv);
1403 NS_ENSURE_SUCCESS(rv, rv);
1409 NS_ENSURE_SUCCESS(rv, rv);
1412 rv = aMediaItem->SetContentSrc(newURI);
1413 NS_ENSURE_SUCCESS(rv, rv);
1428 TRACE((
"%s", __FUNCTION__));
1429 NS_ENSURE_ARG_POINTER(aItemFile);
1433 nsCOMPtr<nsIFile> parent;
1434 rv = aItemFile->GetParent(getter_AddRefs(parent));
1435 NS_ENSURE_SUCCESS(rv, rv);
1438 rv = CheckDirectoryForDeletion_Recursive(parent);
1439 NS_ENSURE_SUCCESS(rv, rv);
1450 sbMediaFileManager::CheckDirectoryForDeletion_Recursive(nsIFile *aDirectory)
1452 TRACE((
"%s", __FUNCTION__));
1453 NS_ENSURE_ARG_POINTER(aDirectory);
1457 nsCOMPtr<nsIFile> mediaFolder;
1458 rv = GetMediaFolder(aDirectory, getter_AddRefs(mediaFolder));
1459 NS_ENSURE_SUCCESS(rv, rv);
1461 TRACE((
"%s: Folder Not Managed", __FUNCTION__));
1467 rv = aDirectory->GetDirectoryEntries(getter_AddRefs(dirEntries));
1470 rv = dirEntries->HasMoreElements(&hasMoreElements);
1471 NS_ENSURE_SUCCESS(rv, rv);
1473 if (hasMoreElements) {
1475 TRACE((
"%s: Folder Not Empty", __FUNCTION__));
1481 rv = aDirectory->Remove(PR_FALSE);
1482 NS_ENSURE_SUCCESS(rv, rv);
1485 nsCOMPtr<nsIFile> parent;
1486 rv = aDirectory->GetParent(getter_AddRefs(parent));
1487 NS_ENSURE_SUCCESS(rv, rv);
1490 return CheckDirectoryForDeletion_Recursive(parent);
1504 TRACE((
"%s", __FUNCTION__));
1505 NS_ENSURE_ARG_POINTER(aItemFile);
1506 NS_ENSURE_ARG_POINTER(aRetVal);
1512 *aRetVal = PR_FALSE;
1515 nsCOMPtr<nsIFile> mediaFolder;
1516 rv = GetMediaFolder(aItemFile, getter_AddRefs(mediaFolder));
1517 NS_ENSURE_SUCCESS(rv, rv);
1524 rv = aItemFile->Remove(PR_FALSE);
1525 NS_ENSURE_SUCCESS(rv, rv);
1529 NS_ENSURE_SUCCESS(rv, rv);
#define SB_PROPERTY_IMPORTTYPE
static nsresult sbGetFileExtensionFromURI(nsIURI *aURI, nsACString &_retval)
#define SB_VALUE_IMPORTTYPE_VOICE_RECORDING
static nsresult GetFileContentURI(nsIFile *aFile, nsIURI **_retval)
Return a library content URI for the file specified by aFile. Special processing is required to conve...
nsString Get(const nsAString &aKey, const nsAString &aDefault=SBVoidString())
nsString Format(const nsAString &aKey, nsTArray< nsString > &aParams, const nsAString &aDefault=SBVoidString())
nsresult do_GetProxyForObject(nsIEventTarget *aTarget, REFNSIID aIID, nsISupports *aObj, PRInt32 aProxyType, void **aProxyObject)
#define SB_PROPERTY_TOTALTRACKS
#define SB_PROPERTYMANAGER_CONTRACTID
sbMainThreadQueryInterface do_MainThreadQueryInterface(nsISupports *aSupports, nsresult *aResult=nsnull)
#define SB_PROPERTY_CONTENTTYPE
void nsString_Split(const nsAString &aString, const nsAString &aDelimiter, nsTArray< nsString > &aSubStringArray)
#define SB_PROPERTY_ARTISTNAME
function newURI(aURLString)
Songbird String Bundle Definitions.
SimpleArrayEnumerator prototype hasMoreElements
#define SB_PROPERTY_ALBUMNAME
#define SB_PROPERTY_TRACKNAME
#define SB_VALUE_IMPORTTYPE_FM_RECORDING
_getSelectedPageStyle s i
_updateTextAndScrollDataForFrame aData
#define SB_PROPERTY_TRACKNUMBER