37 #include <nsIPropertyBag2.h>
38 #include <nsIVariant.h>
41 #include <nsArrayUtils.h>
44 #include <sbIDeviceEvent.h>
45 #include <sbIDeviceProperties.h>
52 #include <sbILibraryChangeset.h>
59 extern PRLogModuleInfo* gBaseDeviceLog;
63 #define LOG(args) PR_LOG(gBaseDeviceLog, PR_LOG_WARN, args)
70 mDevLibrary(aDevLibrary),
71 mChangeset(aChangeset),
80 sbDeviceEnsureSpaceForWrite::GetFreeSpace() {
82 nsAutoString freeSpaceStr;
85 NS_ENSURE_SUCCESS(rv, rv);
87 NS_ENSURE_SUCCESS(rv, rv);
90 PRInt64 freeMusicSpace;
92 NS_ENSURE_SUCCESS(rv, rv);
93 if (mFreeSpace >= freeMusicSpace)
94 mFreeSpace = freeMusicSpace;
100 sbDeviceEnsureSpaceForWrite::RemoveExtraItems() {
106 nsCOMPtr<nsIMutableArray> addChanges =
107 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
108 NS_ENSURE_SUCCESS(rv, rv);
110 nsCOMPtr<nsIMutableArray> otherChanges =
111 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
112 NS_ENSURE_SUCCESS(rv, rv);
115 nsCOMPtr<nsIArray> changes;
116 rv = mChangeset->GetChanges(getter_AddRefs(changes));
117 NS_ENSURE_SUCCESS(rv, rv);
122 rv = changes->GetLength(&length);
123 NS_ENSURE_SUCCESS(rv, rv);
124 PRInt64 totalChangeSize = 0;
125 for (PRUint32
i = 0;
i < length;
i++) {
126 nsCOMPtr<sbILibraryChange> change = do_QueryElementAt(changes,
i, &rv);
127 NS_ENSURE_SUCCESS(rv, rv);
130 rv = change->GetOperation(&operation);
131 NS_ENSURE_SUCCESS(rv, 0);
135 addChanges->AppendElement(change, PR_FALSE);
138 otherChanges->AppendElement(change, PR_FALSE);
142 PRInt64 changeSize = mDevice->
GetChangeSize(mDevLibrary, change);
143 totalChangeSize += changeSize;
147 if (totalChangeSize >= mFreeSpace) {
156 NS_ENSURE_SUCCESS(rv, rv);
173 nsCOMPtr<nsIMutableArray> changesToTake =
174 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
175 PRInt64 changesToTakeSize = 0;
181 PRUint32 otherChangesLength;
182 rv = otherChanges->GetLength(&otherChangesLength);
183 for (PRUint32
i = 0;
i < otherChangesLength;
i++) {
184 nsCOMPtr<sbILibraryChange> change = do_QueryElementAt(otherChanges,
i, &rv);
185 NS_ENSURE_SUCCESS(rv, rv);
189 PRInt64 changeSize = mDevice->
GetChangeSize(mDevLibrary, change);
190 if ((changesToTakeSize + changeSize) < mFreeSpace) {
191 changesToTake->AppendElement(change, PR_FALSE);
192 changesToTakeSize += changeSize;
199 srand((
unsigned int) t);
202 PRUint32 addChangesLength;
203 rv = addChanges->GetLength(&addChangesLength);
204 while (addChangesLength != 0) {
206 PRUint32 random = (
unsigned int) ((rand() / (RAND_MAX + 1.0)) * addChangesLength);
208 nsCOMPtr<sbILibraryChange> change = do_QueryElementAt(addChanges, random, &rv);
209 NS_ENSURE_SUCCESS(rv, rv);
213 addChanges->RemoveElementAt(random);
218 PRInt64 changeSize = mDevice->
GetChangeSize(mDevLibrary, change);
219 if ((changesToTakeSize + changeSize) < mFreeSpace) {
220 changesToTake->AppendElement(change, PR_FALSE);
221 changesToTakeSize += changeSize;
227 rv = mChangeset->SetChanges(changesToTake);
228 NS_ENSURE_SUCCESS(rv, rv);
236 nsresult rv = GetFreeSpace();
237 NS_ENSURE_SUCCESS(rv, rv);
240 rv = RemoveExtraItems();
241 NS_ENSURE_SUCCESS(rv, rv);
~sbDeviceEnsureSpaceForWrite()
nsresult GetMusicFreeSpace(sbILibrary *aLibrary, PRInt64 *aFreeMusicSpace)
Songbird Variant Utility Definitions.
static nsresult QueryUserSpaceExceeded(sbIDevice *aDevice, sbIDeviceLibrary *aLibrary, PRInt64 aSpaceNeeded, PRInt64 aSpaceAvailable, PRBool *aAbort)
void SetEnsureSpaceChecked(bool aChecked)
PRInt64 nsString_ToInt64(const nsAString &str, nsresult *rv)
bool GetEnsureSpaceChecked() const
sbDeviceEnsureSpaceForWrite(sbBaseDevice *aDevice, sbIDeviceLibrary *aDevLibrary, sbILibraryChangeset *aChangeset)
_getSelectedPageStyle s i
const unsigned long ADDED
PRInt64 GetChangeSize(sbIDeviceLibrary *aDestLibrary, sbILibraryChange *aChange)
#define SB_DEVICE_PROPERTY_FREE_SPACE