Add remaining files
[juce-lv2.git] / juce / source / src / gui / components / filebrowser / juce_DirectoryContentsList.cpp
blob0b9990951e48bb87e4df556f03c47f263a30d58a
1 /*
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"
28 BEGIN_JUCE_NAMESPACE
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_),
38 thread (thread_),
39 fileTypeFlags (File::ignoreHiddenFiles | File::findFiles),
40 shouldStop (true)
44 DirectoryContentsList::~DirectoryContentsList()
46 clear();
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
63 return root;
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)
74 clear();
75 root = directory;
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;
93 refresh();
97 void DirectoryContentsList::clear()
99 shouldStop = true;
100 thread.removeTimeSliceClient (this);
102 fileFindHandle = nullptr;
104 if (files.size() > 0)
106 files.clear();
107 changed();
111 void DirectoryContentsList::refresh()
113 clear();
115 if (root.isDirectory())
117 fileFindHandle = new DirectoryIterator (root, false, "*", fileTypeFlags);
118 shouldStop = false;
119 thread.addTimeSliceClient (this);
123 //==============================================================================
124 int DirectoryContentsList::getNumFiles() const
126 return files.size();
129 bool DirectoryContentsList::getFileInfo (const int index,
130 FileInfo& result) const
132 const ScopedLock sl (fileListLock);
133 const FileInfo* const info = files [index];
135 if (info != nullptr)
137 result = *info;
138 return true;
141 return false;
144 File DirectoryContentsList::getFile (const int index) const
146 const ScopedLock sl (fileListLock);
147 const FileInfo* const info = files [index];
149 if (info != nullptr)
150 return root.getChildFile (info->filename);
152 return File::nonexistent;
155 bool DirectoryContentsList::isStillLoading() const
157 return fileFindHandle != nullptr;
160 void DirectoryContentsList::changed()
162 sendChangeMessage();
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))
175 if (hasChanged)
176 changed();
178 return 500;
181 if (shouldStop || (Time::getApproximateMillisecondCounter() > startTime + 150))
182 break;
185 if (hasChanged)
186 changed();
188 return 0;
191 bool DirectoryContentsList::checkNextFile (bool& hasChanged)
193 if (fileFindHandle != nullptr)
195 bool fileFoundIsDir, isHidden, isReadOnly;
196 int64 fileSize;
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))
205 hasChanged = true;
208 return true;
210 else
212 fileFindHandle = nullptr;
216 return false;
219 int DirectoryContentsList::compareElements (const DirectoryContentsList::FileInfo* const first,
220 const DirectoryContentsList::FileInfo* const second)
222 #if JUCE_WINDOWS
223 if (first->isDirectory != second->isDirectory)
224 return first->isDirectory ? -1 : 1;
225 #endif
227 return first->filename.compareIgnoreCase (second->filename);
230 bool DirectoryContentsList::addFile (const File& file,
231 const bool isDir,
232 const int64 fileSize,
233 const Time& modTime,
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)
254 return false;
256 files.addSorted (*this, info.release());
257 return true;
260 return false;
263 END_JUCE_NAMESPACE