29 #include <nsArrayUtils.h>
30 #include <nsAutoLock.h>
31 #include <nsAutoPtr.h>
32 #include <nsILocalFile.h>
33 #include <nsThreadUtils.h>
43 #include <sbIDevice.h>
44 #include <sbIPropertyArray.h>
45 #include <sbIPropertyManager.h>
65 PRUint32
const LEGACY_SYNC_NONE = 0x0;
66 PRUint32
const LEGACY_SYNC_ALL = 0x2;
67 PRUint32
const LEGACY_MANUAL_SYNC_ALL = 0x3;
68 PRUint32
const LEGACY_SYNC_PLAYLISTS = 0x4;
69 PRUint32
const LEGACY_MANUAL_SYNC_PLAYLISTS = 0x5;
77 case LEGACY_SYNC_NONE:
81 case LEGACY_MANUAL_SYNC_ALL:
84 case LEGACY_SYNC_PLAYLISTS:
85 case LEGACY_MANUAL_SYNC_PLAYLISTS:
98 nsID
const & aDeviceID,
99 nsAString
const & aDeviceLibraryGuid)
105 sbDeviceLibrarySyncSettings::sbDeviceLibrarySyncSettings(
106 nsID
const & aDeviceID,
107 nsAString
const & aDeviceLibraryGuid) :
108 mDeviceID(aDeviceID),
109 mDeviceLibraryGuid(aDeviceLibraryGuid),
110 mLock(nsAutoLock::NewLock(
"sbDeviceLibrarySyncSettings"))
115 sbDeviceLibrarySyncSettings::~sbDeviceLibrarySyncSettings()
117 nsAutoLock::DestroyLock(mLock);
123 NS_ENSURE_ARG_POINTER(aSource);
130 if (
this == aSource) {
134 mDeviceID = aSource->mDeviceID;
135 mDeviceLibraryGuid = aSource->mDeviceLibraryGuid;
138 nsRefPtr<sbDeviceLibraryMediaSyncSettings> mediaSettings;
139 nsRefPtr<sbDeviceLibraryMediaSyncSettings> newMediaSettings;
143 mediaSettings = aSource->mMediaSettings[mediaType];
145 rv = mediaSettings->CreateCopy(getter_AddRefs(newMediaSettings));
146 NS_ENSURE_SUCCESS(rv, rv);
148 mMediaSettings[mediaType] = newMediaSettings;
157 NS_ENSURE_ARG_POINTER(aSettings);
160 nsRefPtr<sbDeviceLibrarySyncSettings>
settings =
162 NS_ENSURE_TRUE(settings, NS_ERROR_OUT_OF_MEMORY);
164 rv = settings->Assign(
this);
165 NS_ENSURE_SUCCESS(rv, rv);
167 settings.forget(aSettings);
174 sbDeviceLibrarySyncSettings::GetMediaSettings(
178 NS_ASSERTION(mLock,
"sbDeviceLibrarySyncSettings not initialized");
180 nsAutoLock lock(mLock);
181 return GetMediaSettingsNoLock(aMediaType, aMediaSettings);
185 sbDeviceLibrarySyncSettings::GetMediaSettingsNoLock(
189 NS_ASSERTION(aMediaSettings,
190 "aMediaSettings is null in "
191 "DeviceLibrarySyncSettings::GetMediaSettingsNoLock");
192 nsRefPtr<sbDeviceLibraryMediaSyncSettings> newSettings =
193 mMediaSettings[aMediaType];
196 newSettings = sbDeviceLibraryMediaSyncSettings::New(
this,
199 NS_ENSURE_TRUE(newSettings, NS_ERROR_OUT_OF_MEMORY);
201 mMediaSettings[aMediaType] = newSettings;
204 newSettings->SetSyncSettings(
this);
209 newSettings.forget();
215 sbDeviceLibrarySyncSettings::GetSyncPlaylists(nsIArray ** aMediaLists)
217 NS_ASSERTION(mLock,
"sbDeviceLibrarySyncSettings not initialized");
220 nsCOMPtr<nsIMutableArray> allPlaylists =
221 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
222 NS_ENSURE_SUCCESS(rv, rv);
224 nsAutoLock lock(mLock);
226 nsCOMPtr<sbDeviceLibraryMediaSyncSettings> mediaSettings;
231 mediaSettings = mMediaSettings[mediaType];
232 if (!mediaSettings) {
237 rv = mediaSettings->GetMgmtTypeNoLock(&mgmtType);
238 NS_ENSURE_SUCCESS(rv, rv);
240 nsCOMPtr<nsIArray> playlists;
247 rv = mediaSettings->GetSelectedPlaylistsNoLock(getter_AddRefs(playlists));
248 NS_ENSURE_SUCCESS(rv, rv);
253 NS_ENSURE_SUCCESS(rv, rv);
257 rv = CallQueryInterface(allPlaylists, aMediaLists);
258 NS_ENSURE_SUCCESS(rv, rv);
264 sbDeviceLibrarySyncSettings::GetMgmtTypePref(
sbIDevice * aDevice,
265 PRUint32 aContentType,
266 PRUint32 & aMgmtTypes)
268 NS_ENSURE_ARG_POINTER(aDevice);
270 NS_ENSURE_ARG_RANGE(aContentType,
272 sbIDeviceLibrary::MEDIATYPE_COUNT - 1);
277 rv = GetMgmtTypePrefKey(aContentType, prefKey);
278 NS_ENSURE_SUCCESS(rv, rv);
280 nsCOMPtr<nsIVariant> var;
281 rv = aDevice->GetPreference(prefKey, getter_AddRefs(var));
282 NS_ENSURE_SUCCESS(rv, rv);
286 rv = var->GetDataType(&dataType);
289 if (dataType == nsIDataType::VTYPE_VOID ||
290 dataType == nsIDataType::VTYPE_EMPTY)
295 rv = var->GetAsUint32(&mgmtType);
296 NS_ENSURE_SUCCESS(rv, rv);
309 aMgmtTypes = mgmtType;
315 sbDeviceLibrarySyncSettings::GetImportPref(
sbIDevice * aDevice,
316 PRUint32 aContentType,
319 NS_ENSURE_ARG_POINTER(aDevice);
321 NS_ENSURE_ARG_RANGE(aContentType,
323 sbIDeviceLibrary::MEDIATYPE_COUNT - 1);
328 rv = GetImportPrefKey(aContentType, prefKey);
329 NS_ENSURE_SUCCESS(rv, rv);
331 nsCOMPtr<nsIVariant> var;
332 rv = aDevice->GetPreference(prefKey, getter_AddRefs(var));
333 NS_ENSURE_SUCCESS(rv, rv);
337 rv = var->GetDataType(&dataType);
339 if (dataType == nsIDataType::VTYPE_VOID ||
340 dataType == nsIDataType::VTYPE_EMPTY)
345 rv = var->GetAsBool(&aImport);
346 NS_ENSURE_SUCCESS(rv, rv);
354 sbDeviceLibrarySyncSettings::WritePref(
sbIDevice * aDevice,
355 nsAString
const & aPrefKey,
358 NS_ENSURE_ARG_POINTER(aDevice);
362 rv = aDevice->SetPreference(aPrefKey,
sbNewVariant(aValue));
363 NS_ENSURE_SUCCESS(rv, rv);
369 sbDeviceLibrarySyncSettings::ReadAString(
sbIDevice * aDevice,
370 nsAString
const & aPrefKey,
372 nsAString
const & aDefault)
374 NS_ENSURE_ARG_POINTER(aDevice);
378 nsCOMPtr<nsIVariant> var;
379 rv = aDevice->GetPreference(aPrefKey, getter_AddRefs(var));
380 NS_ENSURE_SUCCESS(rv, rv);
383 rv = var->GetDataType(&dataType);
384 if (dataType == nsIDataType::VTYPE_VOID) {
387 rv = var->GetAsAString(aString);
388 NS_ENSURE_SUCCESS(rv, rv);
394 sbDeviceLibrarySyncSettings::ReadMediaSyncSettings(
400 NS_ENSURE_ARG_POINTER(aDevice);
401 NS_ENSURE_ARG_POINTER(aMediaSyncSettings);
405 nsRefPtr<sbDeviceLibraryMediaSyncSettings>
settings =
406 sbDeviceLibraryMediaSyncSettings::New(
this,
409 NS_ENSURE_TRUE(settings, NS_ERROR_OUT_OF_MEMORY);
413 rv = GetImportPref(aDevice, aMediaType,
import);
414 NS_ENSURE_SUCCESS(rv, rv);
415 rv = settings->SetImport(
import);
416 NS_ENSURE_SUCCESS(rv, rv);
419 rv = GetMgmtTypePref(aDevice, aMediaType, mgmtType);
420 NS_ENSURE_SUCCESS(rv, rv);
421 settings->SetMgmtType(mgmtType);
423 nsCOMPtr<nsIArray> mediaLists;
425 rv = settings->GetSyncPlaylistsNoLock(getter_AddRefs(mediaLists));
427 if (rv != NS_ERROR_NOT_AVAILABLE) {
428 NS_ENSURE_SUCCESS(rv, rv);
430 settings->mPlaylistsSelection.Clear();
433 rv = mediaLists->GetLength(&count);
434 NS_ENSURE_SUCCESS(rv, rv);
436 for (PRUint32 index = 0; index <
count; ++index) {
437 nsCOMPtr<sbIMediaList> mediaList = do_QueryElementAt(mediaLists,
440 NS_ENSURE_SUCCESS(rv, rv);
442 nsCOMPtr<nsISupports> supports = do_QueryInterface(mediaList, &rv);
443 NS_ENSURE_SUCCESS(rv, rv);
445 PRBool added = settings->mPlaylistsSelection.Put(supports, PR_FALSE);
446 NS_ENSURE_TRUE(added, NS_ERROR_OUT_OF_MEMORY);
448 rv = GetSyncListsPrefKey(aMediaType, prefKey);
449 NS_ENSURE_SUCCESS(rv, rv);
451 nsString mediaListGuids;
452 rv = ReadAString(aDevice, prefKey, mediaListGuids, nsString());
454 nsCOMPtr<sbILibraryManager> libManager =
455 do_GetService(
"@songbirdnest.com/Songbird/library/Manager;1", &rv);
456 NS_ENSURE_SUCCESS(rv, rv);
458 nsCOMPtr<sbILibrary> mainLibrary;
459 rv = libManager->GetMainLibrary(getter_AddRefs(mainLibrary));
460 NS_ENSURE_SUCCESS(rv, rv);
462 nsTArray<nsString> mediaListGuidArray;
463 nsString_Split(mediaListGuids, NS_LITERAL_STRING(
","), mediaListGuidArray);
464 count = mediaListGuidArray.Length();
465 for (PRUint32 index = 0; index <
count; ++index) {
466 nsCOMPtr<sbIMediaItem> mediaItem;
467 rv = mainLibrary->GetItemByGuid(mediaListGuidArray[index],
468 getter_AddRefs(mediaItem));
469 if (rv != NS_ERROR_NOT_AVAILABLE) {
470 NS_ENSURE_SUCCESS(rv, rv);
471 nsCOMPtr<nsISupports> supports = do_QueryInterface(mediaItem, &rv);
472 NS_ENSURE_SUCCESS(rv, rv);
476 PRBool exists = settings->mPlaylistsSelection.Get(supports, &selected);
478 PRBool added = settings->mPlaylistsSelection.Put(supports, PR_TRUE);
479 NS_ENSURE_TRUE(added, NS_ERROR_OUT_OF_MEMORY);
485 rv = GetSyncFolderPrefKey(aMediaType, prefKey);
486 NS_ENSURE_SUCCESS(rv, rv);
487 rv = ReadAString(aDevice, prefKey, settings->mSyncFolder, nsString());
488 NS_ENSURE_SUCCESS(rv, rv);
490 rv = GetSyncFromFolderPrefKey(aMediaType, prefKey);
491 NS_ENSURE_SUCCESS(rv, rv);
493 nsString syncFromFolderString;
494 rv = ReadAString(aDevice, prefKey, syncFromFolderString, nsString());
495 NS_ENSURE_SUCCESS(rv, rv);
497 if (!syncFromFolderString.IsEmpty()) {
498 nsCOMPtr<nsILocalFile> folder;
499 rv = NS_NewLocalFile(syncFromFolderString,
501 getter_AddRefs(folder));
502 NS_ENSURE_SUCCESS(rv, rv);
504 settings->mSyncFromFolder = folder;
506 settings.forget(aMediaSyncSettings);
512 sbDeviceLibrarySyncSettings::WriteMediaSyncSettings(
517 NS_ENSURE_ARG_POINTER(aDevice);
518 NS_ENSURE_ARG_POINTER(aMediaSyncSettings);
523 rv = GetMgmtTypePrefKey(aMediaType, prefKey);
524 NS_ENSURE_SUCCESS(rv, rv);
526 rv = WritePref(aDevice,
528 aMediaSyncSettings->mSyncMgmtType);
529 NS_ENSURE_SUCCESS(rv, rv);
531 rv = GetImportPrefKey(aMediaType, prefKey);
532 NS_ENSURE_SUCCESS(rv, rv);
533 rv = WritePref(aDevice,
535 aMediaSyncSettings->mImport);
536 NS_ENSURE_SUCCESS(rv, rv);
538 rv = GetSyncFromFolderPrefKey(aMediaType, prefKey);
539 NS_ENSURE_SUCCESS(rv, rv);
541 nsString syncFromFolderString;
542 if (aMediaSyncSettings->mSyncFromFolder) {
543 rv = aMediaSyncSettings->mSyncFromFolder->GetPath(syncFromFolderString);
544 NS_ENSURE_SUCCESS(rv, rv);
546 rv = WritePref(aDevice, prefKey, syncFromFolderString);
547 NS_ENSURE_SUCCESS(rv, rv);
549 rv = GetSyncFolderPrefKey(aMediaType, prefKey);
550 NS_ENSURE_SUCCESS(rv, rv);
552 rv = WritePref(aDevice, prefKey, aMediaSyncSettings->mSyncFolder);
553 NS_ENSURE_SUCCESS(rv, rv);
559 rv = GetSyncListsPrefKey(aMediaType, prefKey);
560 NS_ENSURE_SUCCESS(rv, rv);
562 nsCOMPtr<nsIMutableArray> selected =
563 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
564 aMediaSyncSettings->mPlaylistsSelection.EnumerateRead(
ArrayBuilder,
568 rv = selected->GetLength(&count);
569 NS_ENSURE_SUCCESS(rv, rv);
571 nsString mediaListGuids;
572 nsCOMPtr<sbIMediaList> mediaList;
573 for (PRUint32 index = 0; index <
count; ++index) {
575 mediaListGuids.Append(NS_LITERAL_STRING(
","));
577 mediaList = do_QueryElementAt(selected, index, &rv);
578 NS_ENSURE_SUCCESS(rv, rv);
581 rv = mediaList->GetGuid(guid);
582 NS_ENSURE_SUCCESS(rv, rv);
584 mediaListGuids.Append(guid);
586 rv = WritePref(aDevice, prefKey, mediaListGuids);
587 NS_ENSURE_SUCCESS(rv, rv);
596 NS_ENSURE_ARG_POINTER(aDevice);
600 nsRefPtr<sbDeviceLibraryMediaSyncSettings> mediaSettings;
604 if (mMediaSettings[mediaType]) {
605 rv = mediaSettings->Assign(mMediaSettings[mediaType]);
606 NS_ENSURE_SUCCESS(rv, rv);
609 rv = ReadMediaSyncSettings(aDevice,
612 getter_AddRefs(mediaSettings));
613 NS_ENSURE_SUCCESS(rv, rv);
615 mMediaSettings[mediaType] = mediaSettings;
622 sbDeviceLibrarySyncSettings::Write(
sbIDevice * aDevice)
624 NS_ENSURE_ARG_POINTER(aDevice);
628 nsRefPtr<sbDeviceLibraryMediaSyncSettings> mediaSettings;
632 mediaSettings = mMediaSettings[mediaType];
634 rv = WriteMediaSyncSettings(aDevice,
637 NS_ENSURE_SUCCESS(rv, rv);
644 sbDeviceLibrarySyncSettings::GetMgmtTypePrefKey(PRUint32 aContentType,
647 NS_ENSURE_ARG_RANGE(aContentType,
649 sbIDeviceLibrary::MEDIATYPE_COUNT - 1);
650 NS_ENSURE_STATE(!mDeviceLibraryGuid.IsEmpty());
655 aPrefKey.Append(mDeviceLibraryGuid);
658 aPrefKey.AppendLiteral(
gMediaType[aContentType]);
664 sbDeviceLibrarySyncSettings::GetImportPrefKey(PRUint32 aContentType,
667 NS_ENSURE_ARG_RANGE(aContentType,
669 sbIDeviceLibrary::MEDIATYPE_COUNT - 1);
670 NS_ENSURE_STATE(!mDeviceLibraryGuid.IsEmpty());
675 aPrefKey.Append(mDeviceLibraryGuid);
678 aPrefKey.AppendLiteral(
gMediaType[aContentType]);
684 sbDeviceLibrarySyncSettings::GetSyncListsPrefKey(PRUint32 aContentType,
687 NS_ENSURE_ARG_RANGE(aContentType,
689 sbIDeviceLibrary::MEDIATYPE_COUNT - 1);
690 NS_ENSURE_STATE(!mDeviceLibraryGuid.IsEmpty());
694 aPrefKey.Append(mDeviceLibraryGuid);
697 aPrefKey.AppendLiteral(
gMediaType[aContentType]);
703 sbDeviceLibrarySyncSettings::GetSyncFromFolderPrefKey(PRUint32 aContentType,
706 NS_ENSURE_ARG_RANGE(aContentType,
708 sbIDeviceLibrary::MEDIATYPE_COUNT - 1);
709 NS_ENSURE_STATE(!mDeviceLibraryGuid.IsEmpty());
713 aPrefKey.Append(mDeviceLibraryGuid);
716 aPrefKey.AppendLiteral(
gMediaType[aContentType]);
722 sbDeviceLibrarySyncSettings::GetSyncFolderPrefKey(PRUint32 aContentType,
725 NS_ENSURE_ARG_RANGE(aContentType,
727 sbIDeviceLibrary::MEDIATYPE_COUNT - 1);
728 NS_ENSURE_STATE(!mDeviceLibraryGuid.IsEmpty());
732 aPrefKey.Append(mDeviceLibraryGuid);
735 aPrefKey.AppendLiteral(
gMediaType[aContentType]);
#define PREF_SYNC_MGMTTYPE
nsresult CreateCopy(sbDeviceLibrarySyncSettings **aSettings)
PRUint32 const LEGACY_MGMT_TYPE_MANUAL
nsresult sbAppendnsIArray(nsIArray *aSrc, nsIMutableArray *aDest, PRBool aWeak=PR_FALSE, PRUint32 aElementsToCopy=0)
Songbird Variant Utility Definitions.
const unsigned long MEDIATYPE_AUDIO
NS_DECL_ISUPPORTS NS_DECL_SBIDEVICELIBRARYSYNCSETTINGS nsresult Assign(sbDeviceLibrarySyncSettings *aSource)
const unsigned long MEDIATYPE_IMAGE
void nsString_Split(const nsAString &aString, const nsAString &aDelimiter, nsTArray< nsString > &aSubStringArray)
static sbDeviceLibrarySyncSettings * New(nsID const &aDeviceID, nsAString const &aDeviceLibraryGuid)
_dialogDatepicker settings
NS_IMPL_ISUPPORTS1(sbDeviceLibrarySyncSettings, sbIDeviceLibrarySyncSettings)
char const *const gMediaType[]
PLDHashOperator ArrayBuilder(nsISupports *aKey, PRBool aData, void *userArg)
static void MigrateLegacyMgmtValues(PRUint32 &aValue)
const unsigned long MEDIATYPE_COUNT
nsresult Read(sbIDevice *aDevice, sbIDeviceLibrary *aDeviceLibrary)
_updateTextAndScrollDataForFrame aData