JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_DirectoryContentsList.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
30 : fileFilter (f), thread (t)
31{
32}
33
38
40{
41 setTypeFlags (shouldIgnoreHiddenFiles ? (fileTypeFlags | File::ignoreHiddenFiles)
42 : (fileTypeFlags & ~File::ignoreHiddenFiles));
43}
44
46{
47 return (fileTypeFlags & File::ignoreHiddenFiles) != 0;
48}
49
50//==============================================================================
52 const bool includeDirectories,
53 const bool includeFiles)
54{
55 jassert (includeDirectories || includeFiles); // you have to specify at least one of these!
56
57 if (directory != root)
58 {
59 clear();
60 root = directory;
61 changed();
62
63 // (this forces a refresh when setTypeFlags() is called, rather than triggering two refreshes)
64 fileTypeFlags &= ~(File::findDirectories | File::findFiles);
65 }
66
67 auto newFlags = fileTypeFlags;
68
70 else newFlags &= ~File::findDirectories;
71
73 else newFlags &= ~File::findFiles;
74
75 setTypeFlags (newFlags);
76}
77
78void DirectoryContentsList::setTypeFlags (const int newFlags)
79{
80 if (fileTypeFlags != newFlags)
81 {
82 fileTypeFlags = newFlags;
83 refresh();
84 }
85}
86
87void DirectoryContentsList::stopSearching()
88{
89 shouldStop = true;
90 thread.removeTimeSliceClient (this);
91 isSearching = false;
92}
93
95{
96 stopSearching();
97
98 if (! files.isEmpty())
99 {
100 files.clear();
101 changed();
102 }
103}
104
106{
107 stopSearching();
108 wasEmpty = files.isEmpty();
109 files.clear();
110
111 if (root.isDirectory())
112 {
113 fileFindHandle = std::make_unique<RangedDirectoryIterator> (root, false, "*", fileTypeFlags);
114 shouldStop = false;
115 isSearching = true;
116 thread.addTimeSliceClient (this);
117 }
118}
119
121{
122 const ScopedLock sl (fileListLock);
123 fileFilter = newFileFilter;
124}
125
126//==============================================================================
128{
129 const ScopedLock sl (fileListLock);
130 return files.size();
131}
132
133bool DirectoryContentsList::getFileInfo (const int index, FileInfo& result) const
134{
135 const ScopedLock sl (fileListLock);
136
137 if (auto* info = files [index])
138 {
139 result = *info;
140 return true;
141 }
142
143 return false;
144}
145
147{
148 const ScopedLock sl (fileListLock);
149
150 if (auto* info = files [index])
151 return root.getChildFile (info->filename);
152
153 return {};
154}
155
156bool DirectoryContentsList::contains (const File& targetFile) const
157{
158 const ScopedLock sl (fileListLock);
159
160 for (int i = files.size(); --i >= 0;)
161 if (root.getChildFile (files.getUnchecked (i)->filename) == targetFile)
162 return true;
163
164 return false;
165}
166
168{
169 return isSearching;
170}
171
172void DirectoryContentsList::changed()
173{
175}
176
177//==============================================================================
178int DirectoryContentsList::useTimeSlice()
179{
181 bool hasChanged = false;
182
183 for (int i = 100; --i >= 0;)
184 {
185 if (! checkNextFile (hasChanged))
186 {
187 if (hasChanged)
188 changed();
189
190 return 500;
191 }
192
193 if (shouldStop || (Time::getApproximateMillisecondCounter() > startTime + 150))
194 break;
195 }
196
197 if (hasChanged)
198 changed();
199
200 return 0;
201}
202
203bool DirectoryContentsList::checkNextFile (bool& hasChanged)
204{
205 if (fileFindHandle == nullptr)
206 return false;
207
208 if (*fileFindHandle == RangedDirectoryIterator())
209 {
210 fileFindHandle = nullptr;
211 isSearching = false;
212 hasChanged = true;
213 return false;
214 }
215
216 const auto entry = *(*fileFindHandle)++;
217
218 hasChanged |= addFile (entry.getFile(),
219 entry.isDirectory(),
220 entry.getFileSize(),
221 entry.getModificationTime(),
222 entry.getCreationTime(),
223 entry.isReadOnly());
224
225 return true;
226}
227
228bool DirectoryContentsList::addFile (const File& file, const bool isDir,
229 const int64 fileSize,
230 Time modTime, Time creationTime,
231 const bool isReadOnly)
232{
233 const ScopedLock sl (fileListLock);
234
235 if (fileFilter == nullptr
236 || ((! isDir) && fileFilter->isFileSuitable (file))
237 || (isDir && fileFilter->isDirectorySuitable (file)))
238 {
239 auto info = std::make_unique<FileInfo>();
240
241 info->filename = file.getFileName();
242 info->fileSize = fileSize;
243 info->modificationTime = modTime;
244 info->creationTime = creationTime;
245 info->isDirectory = isDir;
246 info->isReadOnly = isReadOnly;
247
248 for (int i = files.size(); --i >= 0;)
249 if (files.getUnchecked (i)->filename == info->filename)
250 return false;
251
252 files.add (std::move (info));
253
254 std::sort (files.begin(), files.end(), [] (const FileInfo* a, const FileInfo* b)
255 {
256 #if JUCE_WINDOWS
257 if (a->isDirectory != b->isDirectory)
258 return a->isDirectory;
259 #endif
260
261 return a->filename.compareNatural (b->filename) < 0;
262 });
263
264 return true;
265 }
266
267 return false;
268}
269
270} // namespace juce
void sendChangeMessage()
Causes an asynchronous change message to be sent to all the registered listeners.
void setFileFilter(const FileFilter *newFileFilter)
Replaces the current FileFilter.
bool getFileInfo(int index, FileInfo &resultInfo) const
Returns the cached information about one of the files in the list.
bool ignoresHiddenFiles() const
Returns true if hidden files are ignored.
void setDirectory(const File &directory, bool includeDirectories, bool includeFiles)
Sets the directory to look in for files.
bool isStillLoading() const
True if the background thread hasn't yet finished scanning for files.
int getNumFiles() const noexcept
Returns the number of files currently available in the list.
void refresh()
Clears the list and restarts scanning the directory for files.
File getFile(int index) const
Returns one of the files in the list.
void clear()
Clears the list, and stops the thread scanning for files.
void setIgnoresHiddenFiles(bool shouldIgnoreHiddenFiles)
Tells the list whether or not to ignore hidden files.
DirectoryContentsList(const FileFilter *fileFilter, TimeSliceThread &threadToUse)
Creates a directory list.
bool contains(const File &) const
Returns true if the list contains the specified file.
Contains cached information about one of the files in a DirectoryContentsList.
Interface for deciding which files are suitable for something.
virtual bool isFileSuitable(const File &file) const =0
Should return true if this file is suitable for inclusion in whatever context the object is being use...
virtual bool isDirectorySuitable(const File &file) const =0
Should return true if this directory is suitable for inclusion in whatever context the object is bein...
Represents a local file or directory.
Definition juce_File.h:45
bool isDirectory() const
Checks whether the file is a directory that exists.
File getChildFile(StringRef relativeOrAbsolutePath) const
Returns a file that represents a relative (or absolute) sub-path of the current one.
@ ignoreHiddenFiles
Add this flag to avoid returning any hidden files in the results.
Definition juce_File.h:568
@ findDirectories
Use this flag to indicate that you want to find directories.
Definition juce_File.h:565
@ findFiles
Use this flag to indicate that you want to find files.
Definition juce_File.h:566
Automatically locks and unlocks a mutex object.
A thread that keeps a list of clients, and calls each one in turn, giving them all a chance to run so...
void removeTimeSliceClient(TimeSliceClient *clientToRemove)
Removes a client from the list.
void addTimeSliceClient(TimeSliceClient *clientToAdd, int millisecondsBeforeStarting=0)
Adds a client to the list.
static uint32 getApproximateMillisecondCounter() noexcept
Less-accurate but faster version of getMillisecondCounter().
#define jassert(expression)
Platform-independent assertion macro.
JUCE Namespace.
CriticalSection::ScopedLockType ScopedLock
Automatically locks and unlocks a CriticalSection object.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
long long int64
A platform-independent 64-bit integer type.
T sort(T... args)