LMMS
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
33
38
39void DirectoryContentsList::setIgnoresHiddenFiles (const bool shouldIgnoreHiddenFiles)
40{
41 setTypeFlags (shouldIgnoreHiddenFiles ? (fileTypeFlags | File::ignoreHiddenFiles)
43}
44
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)
65 }
66
67 auto newFlags = fileTypeFlags;
68
69 if (includeDirectories) newFlags |= File::findDirectories;
70 else newFlags &= ~File::findDirectories;
71
72 if (includeFiles) newFlags |= File::findFiles;
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
88{
89 shouldStop = true;
90 thread.removeTimeSliceClient (this);
91 isSearching = false;
92}
93
95{
97
98 if (! files.isEmpty())
99 {
100 files.clear();
101 changed();
102 }
103}
104
106{
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
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
176
177//==============================================================================
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
204{
205 if (fileFindHandle != nullptr)
206 {
208 {
209 const auto entry = *(*fileFindHandle)++;
210
211 if (addFile (entry.getFile(),
212 entry.isDirectory(),
213 entry.getFileSize(),
214 entry.getModificationTime(),
215 entry.getCreationTime(),
216 entry.isReadOnly()))
217 {
218 hasChanged = true;
219 }
220
221 return true;
222 }
223
224 fileFindHandle = nullptr;
225 isSearching = false;
226
227 if (! wasEmpty && files.isEmpty())
228 hasChanged = true;
229 }
230
231 return false;
232}
233
234bool DirectoryContentsList::addFile (const File& file, const bool isDir,
235 const int64 fileSize,
236 Time modTime, Time creationTime,
237 const bool isReadOnly)
238{
239 const ScopedLock sl (fileListLock);
240
241 if (fileFilter == nullptr
242 || ((! isDir) && fileFilter->isFileSuitable (file))
243 || (isDir && fileFilter->isDirectorySuitable (file)))
244 {
245 auto info = std::make_unique<FileInfo>();
246
247 info->filename = file.getFileName();
248 info->fileSize = fileSize;
249 info->modificationTime = modTime;
250 info->creationTime = creationTime;
251 info->isDirectory = isDir;
252 info->isReadOnly = isReadOnly;
253
254 for (int i = files.size(); --i >= 0;)
255 if (files.getUnchecked(i)->filename == info->filename)
256 return false;
257
258 files.add (std::move (info));
259
260 std::sort (files.begin(), files.end(), [] (const FileInfo* a, const FileInfo* b)
261 {
262 #if JUCE_WINDOWS
263 if (a->isDirectory != b->isDirectory)
264 return a->isDirectory;
265 #endif
266
267 return a->filename.compareNatural (b->filename) < 0;
268 });
269
270 return true;
271 }
272
273 return false;
274}
275
276} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
static bool findFiles(std::vector< water::File > &files, const BinaryType btype, const char *const pluginPath, const char *const wildcard)
Definition PluginDiscovery.cpp:622
static bool findDirectories(std::vector< water::File > &files, const char *const pluginPath, const char *const wildcard)
Definition PluginDiscovery.cpp:591
uint8_t a
Definition Spc_Cpu.h:141
void sendChangeMessage()
Definition juce_ChangeBroadcaster.cpp:65
void stopSearching()
Definition juce_DirectoryContentsList.cpp:87
void setFileFilter(const FileFilter *newFileFilter)
Definition juce_DirectoryContentsList.cpp:120
bool checkNextFile(bool &hasChanged)
Definition juce_DirectoryContentsList.cpp:203
bool getFileInfo(int index, FileInfo &resultInfo) const
Definition juce_DirectoryContentsList.cpp:133
OwnedArray< FileInfo > files
Definition juce_DirectoryContentsList.h:208
std::unique_ptr< RangedDirectoryIterator > fileFindHandle
Definition juce_DirectoryContentsList.h:210
bool ignoresHiddenFiles() const
Definition juce_DirectoryContentsList.cpp:45
void changed()
Definition juce_DirectoryContentsList.cpp:172
void setDirectory(const File &directory, bool includeDirectories, bool includeFiles)
Definition juce_DirectoryContentsList.cpp:51
bool addFile(const File &, bool isDir, int64 fileSize, Time modTime, Time creationTime, bool isReadOnly)
Definition juce_DirectoryContentsList.cpp:234
bool isStillLoading() const
Definition juce_DirectoryContentsList.cpp:167
bool wasEmpty
Definition juce_DirectoryContentsList.h:213
std::atomic< bool > isSearching
Definition juce_DirectoryContentsList.h:211
int getNumFiles() const noexcept
Definition juce_DirectoryContentsList.cpp:127
std::atomic< bool > shouldStop
Definition juce_DirectoryContentsList.h:211
void refresh()
Definition juce_DirectoryContentsList.cpp:105
File root
Definition juce_DirectoryContentsList.h:202
File getFile(int index) const
Definition juce_DirectoryContentsList.cpp:146
void clear()
Definition juce_DirectoryContentsList.cpp:94
void setIgnoresHiddenFiles(bool shouldIgnoreHiddenFiles)
Definition juce_DirectoryContentsList.cpp:39
DirectoryContentsList(const FileFilter *fileFilter, TimeSliceThread &threadToUse)
Definition juce_DirectoryContentsList.cpp:29
bool contains(const File &) const
Definition juce_DirectoryContentsList.cpp:156
int fileTypeFlags
Definition juce_DirectoryContentsList.h:205
CriticalSection fileListLock
Definition juce_DirectoryContentsList.h:207
TimeSliceThread & thread
Definition juce_DirectoryContentsList.h:204
const FileFilter * fileFilter
Definition juce_DirectoryContentsList.h:203
void setTypeFlags(int)
Definition juce_DirectoryContentsList.cpp:78
~DirectoryContentsList() override
Definition juce_DirectoryContentsList.cpp:34
int useTimeSlice() override
Definition juce_DirectoryContentsList.cpp:178
Definition juce_FileFilter.h:38
Definition juce_File.h:45
@ ignoreHiddenFiles
Definition juce_File.h:562
@ findDirectories
Definition juce_File.h:559
@ findFiles
Definition juce_File.h:560
Definition juce_RangedDirectoryIterator.h:100
Definition juce_Time.h:37
static uint32 getApproximateMillisecondCounter() noexcept
Definition juce_Time.cpp:261
friend class TimeSliceThread
Definition juce_TimeSliceThread.h:68
struct huft * t
Definition inflate.c:943
register unsigned i
Definition inflate.c:1575
unsigned f
Definition inflate.c:1572
struct backing_store_struct * info
Definition jmemsys.h:183
#define jassert(expression)
Definition carla_juce.cpp:31
CriticalSection::ScopedLockType ScopedLock
Definition juce_CriticalSection.h:186
long long int64
Definition juce_MathsFunctions.h:54
Definition juce_DirectoryContentsList.h:126
b
Definition crypt.c:628
int result
Definition process.c:1455
struct zdirent * file
Definition win32.c:1500
#define const
Definition zconf.h:137