25 #include <nsArrayUtils.h>
26 #include <sbIFileScan.h>
27 #include <nsIFileURL.h>
28 #include <nsNetUtil.h>
30 #include <nsUnicharUtils.h>
35 #include <sbFileUtils.h>
36 #include <sbIDeviceCapabilities.h>
37 #include <sbIDeviceLibrarySyncSettings.h>
43 NS_ASSERTION(aDevLib,
"aDevLib is null");
44 NS_ASSERTION(aFile,
"aFile is null");
46 nsCOMPtr<sbIDeviceLibrarySyncSettings> syncSettings;
47 nsresult rv = aDevLib->GetSyncSettings(getter_AddRefs(syncSettings));
48 NS_ENSURE_SUCCESS(rv, rv);
50 nsCOMPtr<sbIDeviceLibraryMediaSyncSettings> mediaSyncSettings;
52 getter_AddRefs(mediaSyncSettings));
53 NS_ENSURE_SUCCESS(rv, rv);
55 nsString syncFromFolder;
56 nsCOMPtr<nsIFile> syncFromFile;
57 rv = mediaSyncSettings->GetSyncFromFolder(getter_AddRefs(syncFromFile));
58 NS_ENSURE_SUCCESS(rv, rv);
60 return NS_ERROR_NOT_AVAILABLE;
62 rv = syncFromFile->GetPath(syncFromFolder);
63 NS_ENSURE_SUCCESS(rv, rv);
65 if (syncFromFolder.IsEmpty())
66 return NS_ERROR_NOT_AVAILABLE;
68 nsCOMPtr<nsILocalFile> baseDir;
69 rv = NS_NewLocalFile(syncFromFolder, PR_TRUE, getter_AddRefs(baseDir));
70 NS_ENSURE_SUCCESS(rv, rv);
72 return CallQueryInterface(baseDir, aFile);
75 sbDeviceImages::sbDeviceImages(
sbBaseDevice * aBaseDevice) :
76 mBaseDevice(aBaseDevice)
87 nsIArray *aDeviceImageArray,
88 const nsTArray<nsString> &aFileExtensionList,
89 nsIArray **retCopyArray)
91 NS_ENSURE_ARG_POINTER(retCopyArray);
94 nsCOMPtr<nsIFile> baseDir;
95 rv = GetImagesRootFolder(aLibrary, getter_AddRefs(baseDir));
96 NS_ENSURE_SUCCESS(rv, rv);
98 nsCOMPtr<nsIArray> subDirs;
100 getter_AddRefs(subDirs));
101 NS_ENSURE_SUCCESS(rv, rv);
104 nsCOMPtr<nsIMutableArray> syncCopyArray =
105 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
106 NS_ENSURE_SUCCESS(rv, rv);
110 rv = aDeviceImageArray->GetLength(&len);
111 NS_ENSURE_SUCCESS(rv, rv);
113 nsTArray< sbIDeviceImage* > searchableDeviceImageArray;
114 for (PRUint32
i=0;
i<len;
i++) {
115 nsCOMPtr<sbIDeviceImage> image =
116 do_QueryElementAt(aDeviceImageArray,
i, &rv);
117 NS_ENSURE_SUCCESS(rv, rv);
118 searchableDeviceImageArray.AppendElement(image);
121 searchableDeviceImageArray.Sort(comp);
124 nsCOMPtr<nsIMutableArray> localImagesArray =
125 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
126 NS_ENSURE_SUCCESS(rv, rv);
130 rv = subDirs->GetLength(&numEntries);
131 NS_ENSURE_SUCCESS(rv, rv);
133 for (PRUint32
i=0;
i<numEntries;
i++) {
134 nsCOMPtr<nsIFile> subDir =
135 do_QueryElementAt(subDirs,
i, &rv);
136 NS_ENSURE_SUCCESS(rv, rv);
138 rv = AddLocalImages(baseDir,
143 NS_ENSURE_SUCCESS(rv, rv);
147 DiffImages(syncCopyArray, searchableDeviceImageArray, localImagesArray);
150 rv = CallQueryInterface(syncCopyArray, retCopyArray);
151 NS_ENSURE_SUCCESS(rv, rv);
164 const nsTArray<nsString> &aFileExtensionList,
166 nsIArray **retImageArray) {
170 nsCOMPtr<nsIURI> scanDirUri;
171 rv = NS_NewFileURI(getter_AddRefs(scanDirUri), aScanDir);
172 NS_ENSURE_SUCCESS(rv, rv);
175 nsCOMPtr<sbIFileScanQuery> fileScanQuery;
176 rv = ScanForImageFiles(scanDirUri,
179 getter_AddRefs(fileScanQuery));
180 NS_ENSURE_SUCCESS(rv, rv);
184 rv = fileScanQuery->GetFileCount(&fileCount);
185 NS_ENSURE_SUCCESS(rv, rv);
189 rv = aBaseDir->GetPath(basePath);
190 NS_ENSURE_SUCCESS(rv, rv);
193 nsCOMPtr<nsIMutableArray> imageArray =
194 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
195 NS_ENSURE_SUCCESS(rv, rv);
197 for (PRUint32
i = 0;
i < fileCount;
i++) {
202 nsAutoString fileURISpec;
203 rv = fileScanQuery->GetFilePath(
i, fileURISpec);
204 NS_ENSURE_SUCCESS(rv, rv);
210 if (NS_SUCCEEDED(rv))
216 nsCOMPtr<nsIURI> fileURI;
217 rv = NS_NewURI(getter_AddRefs(fileURI),
218 NS_ConvertUTF16toUTF8(fileURISpec).
get());
219 NS_ENSURE_SUCCESS(rv, rv);
221 nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(fileURI, &rv);
222 NS_ENSURE_SUCCESS(rv, rv);
225 nsCOMPtr<nsIFile>
file;
226 rv = fileURL->GetFile(getter_AddRefs(file));
227 NS_ENSURE_SUCCESS(rv, rv);
230 nsCOMPtr<nsIFile> parent;
231 rv = file->GetParent(getter_AddRefs(parent));
232 NS_ENSURE_SUCCESS(rv, rv);
236 rv = parent->GetPath(parentPath);
237 NS_ENSURE_SUCCESS(rv, rv);
240 NS_ENSURE_TRUE(parentPath.Length() >= basePath.Length(),
241 NS_ERROR_UNEXPECTED);
242 nsString partialPath;
243 partialPath = parentPath.BeginReading() + basePath.Length();
246 if (partialPath.First() == *FILE_PATH_SEPARATOR)
247 partialPath = partialPath.BeginReading() + 1;
251 rv = file->GetLeafName(filename);
252 NS_ENSURE_SUCCESS(rv, rv);
256 rv = file->GetFileSize(&size);
257 NS_ENSURE_SUCCESS(rv, rv);
261 image->SetFilename(filename);
262 image->SetSubdirectory(partialPath);
263 image->SetSize(size);
266 rv = imageArray->AppendElement(image, PR_FALSE);
267 NS_ENSURE_SUCCESS(rv, rv);
273 return CallQueryInterface(imageArray, retImageArray);
284 NS_ENSURE_ARG_POINTER(aMediaItem);
290 nsCOMPtr<nsIFile> baseDir;
292 NS_ENSURE_SUCCESS(rv, rv);
293 NS_ENSURE_TRUE(baseDir, NS_ERROR_UNEXPECTED);
296 nsCOMPtr<nsIURI> imageURI;
297 nsCOMPtr<nsIFile> imageFile;
298 rv =
MakeFile(aImage, baseDir, PR_TRUE, PR_FALSE, getter_AddRefs(imageFile));
299 NS_ENSURE_SUCCESS(rv, rv);
300 rv = NS_NewFileURI(getter_AddRefs(imageURI), imageFile);
301 NS_ENSURE_SUCCESS(rv, rv);
304 nsCOMPtr<sbIMediaItem>
305 item = do_CreateInstance
306 (
"@songbirdnest.com/Songbird/Library/TemporaryMediaItem;1", &rv);
307 NS_ENSURE_SUCCESS(rv, rv);
310 rv = item->SetContentType(NS_LITERAL_STRING(
"image"));
311 NS_ENSURE_SUCCESS(rv, rv);
312 rv = item->SetContentSrc(imageURI);
313 NS_ENSURE_SUCCESS(rv, rv);
316 item.forget(aMediaItem);
327 PRBool aWithFilename,
328 PRBool aCreateDirectories,
331 NS_ENSURE_ARG_POINTER(retFile);
332 NS_ENSURE_ARG_POINTER(aBaseDir);
337 nsCOMPtr<nsIFile>
file;
338 rv = aBaseDir->Clone(getter_AddRefs(file));
339 NS_ENSURE_SUCCESS(rv, rv);
342 nsString subDirectories;
343 rv = aImage->GetSubdirectory(subDirectories);
344 NS_ENSURE_SUCCESS(rv, rv);
348 if (!subDirectories.IsEmpty()) {
349 nsTArray<nsString> subDirectoryList;
351 NS_LITERAL_STRING(FILE_PATH_SEPARATOR),
353 for (PRUint32
i = 0;
i < subDirectoryList.Length(); ++
i) {
354 const nsString& subDirectory = subDirectoryList[
i];
355 if (!subDirectory.IsEmpty()) {
357 rv = file->Append(subDirectory);
358 NS_ENSURE_SUCCESS(rv, rv);
360 if (aCreateDirectories) {
362 rv = file->Exists(&exists);
363 NS_ENSURE_SUCCESS(rv, rv);
365 rv = file->Create(nsIFile::DIRECTORY_TYPE,
367 NS_ENSURE_SUCCESS(rv, rv);
377 rv = aImage->GetFilename(filename);
378 NS_ENSURE_SUCCESS(rv, rv);
379 rv = file->Append(filename);
380 NS_ENSURE_SUCCESS(rv, rv);
383 file.forget(retFile);
393 AddLocalImages(nsIFile *baseDir,
395 const nsTArray<nsString> aFileExtensionList,
397 nsIMutableArray *localImageArray)
402 nsCOMPtr<nsIArray> items;
407 getter_AddRefs(items));
408 NS_ENSURE_SUCCESS(rv, rv);
412 rv = items->GetLength(&len);
413 NS_ENSURE_SUCCESS(rv, rv);
415 for (PRUint32
i=0;
i<len;
i++) {
416 nsCOMPtr<sbIDeviceImage> image = do_QueryElementAt(items,
i, &rv);
417 NS_ENSURE_SUCCESS(rv, rv);
418 localImageArray->AppendElement(image, PR_FALSE);
427 sbDeviceImages::DiffImages(nsIMutableArray *diffResultsArray,
428 nsTArray< sbIDeviceImage* > &searchableImageArray,
429 nsIArray *searchItems)
432 nsresult rv = searchItems->GetLength(&len);
433 NS_ENSURE_SUCCESS(rv, rv);
437 for (PRUint32
i=0;
i<len;
i++) {
439 nsCOMPtr<sbIDeviceImage> image = do_QueryElementAt(searchItems,
i, &rv);
440 NS_ENSURE_SUCCESS(rv, rv);
442 if (searchableImageArray.BinaryIndexOf(image, comp) ==
443 searchableImageArray.NoIndex) {
445 diffResultsArray->AppendElement(image, PR_FALSE);
454 sbDeviceImages::ScanForImageFiles(nsIURI *aImageFilesPath,
455 const nsTArray<nsString> &aFileExtensionList,
459 NS_ENSURE_ARG_POINTER(aFileScanQuery);
464 nsCOMPtr<sbIFileScanQuery> fileScanQuery =
465 do_CreateInstance(
"@songbirdnest.com/Songbird/FileScanQuery;1", &rv);
466 NS_ENSURE_SUCCESS(rv, rv);
468 nsCOMPtr<nsIFileURL> _url = do_QueryInterface(aImageFilesPath, &rv);
469 NS_ENSURE_SUCCESS(rv, rv);
470 nsCOMPtr<nsIFile>
_file;
471 rv = _url->GetFile(getter_AddRefs(_file));
472 NS_ENSURE_SUCCESS(rv, rv);
474 rv = _file->GetPath(path);
475 NS_ENSURE_SUCCESS(rv, rv);
477 rv = fileScanQuery->SetDirectory(path);
478 NS_ENSURE_SUCCESS(rv, rv);
479 rv = fileScanQuery->SetRecurse(recursive);
480 NS_ENSURE_SUCCESS(rv, rv);
481 rv = fileScanQuery->SetSearchHidden(PR_TRUE);
482 NS_ENSURE_SUCCESS(rv, rv);
483 rv = fileScanQuery->SetWantLibraryContentURIs(PR_FALSE);
484 NS_ENSURE_SUCCESS(rv, rv);
487 nsCOMPtr<sbIDeviceCapabilities> caps;
488 rv = mBaseDevice->GetCapabilities(getter_AddRefs(caps));
489 NS_ENSURE_SUCCESS(rv, rv);
492 for (PRUint32
i = 0;
i < aFileExtensionList.Length(); ++
i) {
493 rv = fileScanQuery->AddFileExtension(aFileExtensionList[
i]);
494 NS_ENSURE_SUCCESS(rv, rv);
498 nsCOMPtr<sbIFileScan>
499 fileScan = do_CreateInstance(
"@songbirdnest.com/Songbird/FileScan;1", &rv);
500 NS_ENSURE_SUCCESS(rv, rv);
501 rv = fileScan->SubmitQuery(fileScanQuery);
502 NS_ENSURE_SUCCESS(rv, rv);
507 PRBool isScanning = PR_TRUE;
513 rv = fileScanQuery->IsScanning(&isScanning);
514 NS_ENSURE_SUCCESS(rv, rv);
518 PR_Sleep(PR_MillisecondsToInterval(100));
522 fileScanQuery.forget(aFileScanQuery);
543 NS_IMETHODIMP sbDeviceImage::GetSize(PRUint64 *aSizePtr)
545 NS_ENSURE_ARG_POINTER(aSizePtr);
551 NS_IMETHODIMP sbDeviceImage::SetSize(PRUint64 aSize)
557 NS_IMETHODIMP sbDeviceImage::GetFilename(nsAString &aFilename)
559 aFilename = mFilename;
564 NS_IMETHODIMP sbDeviceImage::SetFilename(
const nsAString &aFilename)
566 mFilename = aFilename;
572 NS_IMETHODIMP sbDeviceImage::GetSubdirectory(nsAString &aSubdirectory)
574 aSubdirectory = mSubdirectory;
579 NS_IMETHODIMP sbDeviceImage::SetSubdirectory(
const nsAString &aSubdirectory)
581 mSubdirectory = aSubdirectory;
592 PRInt32 comp = a_subdir.Compare(b_subdir, CaseInsensitiveCompare);
595 }
else if (comp > 0) {
602 return a_filename.Compare(b_filename, CaseInsensitiveCompare) < 0;
612 if (!a_subdir.Equals(b_subdir, CaseInsensitiveCompare)) {
619 return a_filename.Equals(b_filename, CaseInsensitiveCompare);
static nsresult GetImagesRootFolder(sbIDeviceLibrary *aDevLib, nsIFile **aFile)
const PRUint32 CONTENT_IMAGE
const PRUint32 CONTENT_UNKNOWN
PRBool Equals(const sbIDeviceImage *a, const sbIDeviceImage *b) const
const unsigned long MEDIATYPE_IMAGE
virtual PRBool IsRequestAborted()
static nsresult GetFormatTypeForURL(const nsAString &aURL, sbExtensionToContentFormatEntry_t &aFormatType)
For a URL, get format information describing it (extension, mime type, etc.
nsresult ComputeImageSyncArray(sbIDeviceLibrary *aLibrary, nsIArray *aDeviceImageArray, const nsTArray< nsString > &aFileExtensionList, nsIArray **retCopyArray)
void nsString_Split(const nsAString &aString, const nsAString &aDelimiter, nsTArray< nsString > &aSubStringArray)
nsresult ScanImages(nsIFile *aScanDir, nsIFile *aBaseDir, const nsTArray< nsString > &aFileExtensionList, PRBool recursive, nsIArray **retImageArray)
NS_DECL_ISUPPORTS NS_DECL_SBIDEVICEIMAGE sbDeviceImage()
nsresult CreateTemporaryLocalMediaItem(sbIDeviceImage *aImage, sbIDeviceLibrary *aLibrary, sbIMediaItem **aMediaItem)
PRBool LessThan(const sbIDeviceImage *a, const sbIDeviceImage *b) const
nsresult MakeFile(sbIDeviceImage *aImage, nsIFile *aBaseDir, PRBool aWithFilename, PRBool aCreateDirectories, nsIFile **retFile)
An object to scan through the files of a folder (and optionally subfolders)
#define SB_DEFAULT_DIRECTORY_PERMISSIONS
_getSelectedPageStyle s i
NS_IMPL_ISUPPORTS1(sbDeviceImage, sbIDeviceImage)