2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../../../core/juce_StandardHeader.h"
30 #include "juce_DirectoryContentsList.h"
31 #include "../../graphics/imaging/juce_ImageCache.h"
34 //==============================================================================
35 DirectoryContentsList::DirectoryContentsList (const FileFilter
* const fileFilter_
,
36 TimeSliceThread
& thread_
)
37 : fileFilter (fileFilter_
),
39 fileTypeFlags (File::ignoreHiddenFiles
| File::findFiles
),
44 DirectoryContentsList::~DirectoryContentsList()
49 void DirectoryContentsList::setIgnoresHiddenFiles (const bool shouldIgnoreHiddenFiles
)
51 setTypeFlags (shouldIgnoreHiddenFiles
? (fileTypeFlags
| File::ignoreHiddenFiles
)
52 : (fileTypeFlags
& ~File::ignoreHiddenFiles
));
55 bool DirectoryContentsList::ignoresHiddenFiles() const
57 return (fileTypeFlags
& File::ignoreHiddenFiles
) != 0;
60 //==============================================================================
61 const File
& DirectoryContentsList::getDirectory() const
66 void DirectoryContentsList::setDirectory (const File
& directory
,
67 const bool includeDirectories
,
68 const bool includeFiles
)
70 jassert (includeDirectories
|| includeFiles
); // you have to speciify at least one of these!
72 if (directory
!= root
)
77 // (this forces a refresh when setTypeFlags() is called, rather than triggering two refreshes)
78 fileTypeFlags
&= ~(File::findDirectories
| File::findFiles
);
81 int newFlags
= fileTypeFlags
;
82 if (includeDirectories
) newFlags
|= File::findDirectories
; else newFlags
&= ~File::findDirectories
;
83 if (includeFiles
) newFlags
|= File::findFiles
; else newFlags
&= ~File::findFiles
;
85 setTypeFlags (newFlags
);
88 void DirectoryContentsList::setTypeFlags (const int newFlags
)
90 if (fileTypeFlags
!= newFlags
)
92 fileTypeFlags
= newFlags
;
97 void DirectoryContentsList::clear()
100 thread
.removeTimeSliceClient (this);
102 fileFindHandle
= nullptr;
104 if (files
.size() > 0)
111 void DirectoryContentsList::refresh()
115 if (root
.isDirectory())
117 fileFindHandle
= new DirectoryIterator (root
, false, "*", fileTypeFlags
);
119 thread
.addTimeSliceClient (this);
123 //==============================================================================
124 int DirectoryContentsList::getNumFiles() const
129 bool DirectoryContentsList::getFileInfo (const int index
,
130 FileInfo
& result
) const
132 const ScopedLock
sl (fileListLock
);
133 const FileInfo
* const info
= files
[index
];
144 File
DirectoryContentsList::getFile (const int index
) const
146 const ScopedLock
sl (fileListLock
);
147 const FileInfo
* const info
= files
[index
];
150 return root
.getChildFile (info
->filename
);
152 return File::nonexistent
;
155 bool DirectoryContentsList::isStillLoading() const
157 return fileFindHandle
!= nullptr;
160 void DirectoryContentsList::changed()
165 //==============================================================================
166 int DirectoryContentsList::useTimeSlice()
168 const uint32 startTime
= Time::getApproximateMillisecondCounter();
169 bool hasChanged
= false;
171 for (int i
= 100; --i
>= 0;)
173 if (! checkNextFile (hasChanged
))
181 if (shouldStop
|| (Time::getApproximateMillisecondCounter() > startTime
+ 150))
191 bool DirectoryContentsList::checkNextFile (bool& hasChanged
)
193 if (fileFindHandle
!= nullptr)
195 bool fileFoundIsDir
, isHidden
, isReadOnly
;
197 Time modTime
, creationTime
;
199 if (fileFindHandle
->next (&fileFoundIsDir
, &isHidden
, &fileSize
,
200 &modTime
, &creationTime
, &isReadOnly
))
202 if (addFile (fileFindHandle
->getFile(), fileFoundIsDir
,
203 fileSize
, modTime
, creationTime
, isReadOnly
))
212 fileFindHandle
= nullptr;
219 int DirectoryContentsList::compareElements (const DirectoryContentsList::FileInfo
* const first
,
220 const DirectoryContentsList::FileInfo
* const second
)
223 if (first
->isDirectory
!= second
->isDirectory
)
224 return first
->isDirectory
? -1 : 1;
227 return first
->filename
.compareIgnoreCase (second
->filename
);
230 bool DirectoryContentsList::addFile (const File
& file
,
232 const int64 fileSize
,
234 const Time
& creationTime
,
235 const bool isReadOnly
)
237 if (fileFilter
== nullptr
238 || ((! isDir
) && fileFilter
->isFileSuitable (file
))
239 || (isDir
&& fileFilter
->isDirectorySuitable (file
)))
241 ScopedPointer
<FileInfo
> info (new FileInfo());
243 info
->filename
= file
.getFileName();
244 info
->fileSize
= fileSize
;
245 info
->modificationTime
= modTime
;
246 info
->creationTime
= creationTime
;
247 info
->isDirectory
= isDir
;
248 info
->isReadOnly
= isReadOnly
;
250 const ScopedLock
sl (fileListLock
);
252 for (int i
= files
.size(); --i
>= 0;)
253 if (files
.getUnchecked(i
)->filename
== info
->filename
)
256 files
.addSorted (*this, info
.release());