1/****************************************************************************
2**
3** Copyright (C) 2020 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtWidgets module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qfileinfogatherer_p.h"
41#include <qdebug.h>
42#include <qdiriterator.h>
43#include <private/qfileinfo_p.h>
44#ifndef Q_OS_WIN
45# include <unistd.h>
46# include <sys/types.h>
47#endif
48#if defined(Q_OS_VXWORKS)
49# include "qplatformdefs.h"
50#endif
51
52QT_BEGIN_NAMESPACE
53
54#ifdef QT_BUILD_INTERNAL
55static QBasicAtomicInt fetchedRoot = Q_BASIC_ATOMIC_INITIALIZER(false);
56Q_AUTOTEST_EXPORT void qt_test_resetFetchedRoot()
57{
58 fetchedRoot.storeRelaxed(newValue: false);
59}
60
61Q_AUTOTEST_EXPORT bool qt_test_isFetchedRoot()
62{
63 return fetchedRoot.loadRelaxed();
64}
65#endif
66
67static QString translateDriveName(const QFileInfo &drive)
68{
69 QString driveName = drive.absoluteFilePath();
70#ifdef Q_OS_WIN
71 if (driveName.startsWith(QLatin1Char('/'))) // UNC host
72 return drive.fileName();
73 if (driveName.endsWith(QLatin1Char('/')))
74 driveName.chop(1);
75#endif // Q_OS_WIN
76 return driveName;
77}
78
79/*!
80 Creates thread
81*/
82QFileInfoGatherer::QFileInfoGatherer(QObject *parent)
83 : QThread(parent)
84 , m_iconProvider(&defaultProvider)
85{
86 start(LowPriority);
87}
88
89/*!
90 Destroys thread
91*/
92QFileInfoGatherer::~QFileInfoGatherer()
93{
94 abort.storeRelaxed(newValue: true);
95 QMutexLocker locker(&mutex);
96 condition.wakeAll();
97 locker.unlock();
98 wait();
99}
100
101void QFileInfoGatherer::setResolveSymlinks(bool enable)
102{
103 Q_UNUSED(enable);
104#ifdef Q_OS_WIN
105 m_resolveSymlinks = enable;
106#endif
107}
108
109void QFileInfoGatherer::driveAdded()
110{
111 fetchExtendedInformation(path: QString(), files: QStringList());
112}
113
114void QFileInfoGatherer::driveRemoved()
115{
116 QStringList drives;
117 const QFileInfoList driveInfoList = QDir::drives();
118 for (const QFileInfo &fi : driveInfoList)
119 drives.append(t: translateDriveName(drive: fi));
120 newListOfFiles(directory: QString(), listOfFiles: drives);
121}
122
123bool QFileInfoGatherer::resolveSymlinks() const
124{
125#ifdef Q_OS_WIN
126 return m_resolveSymlinks;
127#else
128 return false;
129#endif
130}
131
132void QFileInfoGatherer::setIconProvider(QFileIconProvider *provider)
133{
134 m_iconProvider = provider;
135}
136
137QFileIconProvider *QFileInfoGatherer::iconProvider() const
138{
139 return m_iconProvider;
140}
141
142/*!
143 Fetch extended information for all \a files in \a path
144
145 \sa updateFile(), update(), resolvedName()
146*/
147void QFileInfoGatherer::fetchExtendedInformation(const QString &path, const QStringList &files)
148{
149 QMutexLocker locker(&mutex);
150 // See if we already have this dir/file in our queue
151 int loc = this->path.lastIndexOf(t: path);
152 while (loc > 0) {
153 if (this->files.at(i: loc) == files) {
154 return;
155 }
156 loc = this->path.lastIndexOf(t: path, from: loc - 1);
157 }
158 this->path.push(t: path);
159 this->files.push(t: files);
160 condition.wakeAll();
161
162#if QT_CONFIG(filesystemwatcher)
163 if (files.isEmpty()
164 && !path.isEmpty()
165 && !path.startsWith(s: QLatin1String("//")) /*don't watch UNC path*/) {
166 if (!watchedDirectories().contains(str: path))
167 watchPaths(paths: QStringList(path));
168 }
169#endif
170}
171
172/*!
173 Fetch extended information for all \a filePath
174
175 \sa fetchExtendedInformation()
176*/
177void QFileInfoGatherer::updateFile(const QString &filePath)
178{
179 QString dir = filePath.mid(position: 0, n: filePath.lastIndexOf(c: QLatin1Char('/')));
180 QString fileName = filePath.mid(position: dir.length() + 1);
181 fetchExtendedInformation(path: dir, files: QStringList(fileName));
182}
183
184QStringList QFileInfoGatherer::watchedFiles() const
185{
186#if QT_CONFIG(filesystemwatcher)
187 if (m_watcher)
188 return m_watcher->files();
189#endif
190 return {};
191}
192
193QStringList QFileInfoGatherer::watchedDirectories() const
194{
195#if QT_CONFIG(filesystemwatcher)
196 if (m_watcher)
197 return m_watcher->directories();
198#endif
199 return {};
200}
201
202void QFileInfoGatherer::createWatcher()
203{
204#if QT_CONFIG(filesystemwatcher)
205 m_watcher = new QFileSystemWatcher(this);
206 connect(sender: m_watcher, signal: &QFileSystemWatcher::directoryChanged, receiver: this, slot: &QFileInfoGatherer::list);
207 connect(sender: m_watcher, signal: &QFileSystemWatcher::fileChanged, receiver: this, slot: &QFileInfoGatherer::updateFile);
208# if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
209 const QVariant listener = m_watcher->property("_q_driveListener");
210 if (listener.canConvert<QObject *>()) {
211 if (QObject *driveListener = listener.value<QObject *>()) {
212 connect(driveListener, SIGNAL(driveAdded()), this, SLOT(driveAdded()));
213 connect(driveListener, SIGNAL(driveRemoved()), this, SLOT(driveRemoved()));
214 }
215 }
216# endif // Q_OS_WIN && !Q_OS_WINRT
217#endif
218}
219
220void QFileInfoGatherer::watchPaths(const QStringList &paths)
221{
222#if QT_CONFIG(filesystemwatcher)
223 if (m_watching) {
224 if (m_watcher == nullptr)
225 createWatcher();
226 m_watcher->addPaths(files: paths);
227 }
228#else
229 Q_UNUSED(paths);
230#endif
231}
232
233void QFileInfoGatherer::unwatchPaths(const QStringList &paths)
234{
235#if QT_CONFIG(filesystemwatcher)
236 if (m_watcher && !paths.isEmpty())
237 m_watcher->removePaths(files: paths);
238#else
239 Q_UNUSED(paths);
240#endif
241}
242
243bool QFileInfoGatherer::isWatching() const
244{
245 bool result = false;
246#if QT_CONFIG(filesystemwatcher)
247 QMutexLocker locker(&mutex);
248 result = m_watching;
249#endif
250 return result;
251}
252
253void QFileInfoGatherer::setWatching(bool v)
254{
255#if QT_CONFIG(filesystemwatcher)
256 QMutexLocker locker(&mutex);
257 if (v != m_watching) {
258 if (!v) {
259 delete m_watcher;
260 m_watcher = nullptr;
261 }
262 m_watching = v;
263 }
264#else
265 Q_UNUSED(v);
266#endif
267}
268
269/*
270 List all files in \a directoryPath
271
272 \sa listed()
273*/
274void QFileInfoGatherer::clear()
275{
276#if QT_CONFIG(filesystemwatcher)
277 QMutexLocker locker(&mutex);
278 unwatchPaths(paths: watchedFiles());
279 unwatchPaths(paths: watchedDirectories());
280#endif
281}
282
283/*
284 Remove a \a path from the watcher
285
286 \sa listed()
287*/
288void QFileInfoGatherer::removePath(const QString &path)
289{
290#if QT_CONFIG(filesystemwatcher)
291 QMutexLocker locker(&mutex);
292 unwatchPaths(paths: QStringList(path));
293#else
294 Q_UNUSED(path);
295#endif
296}
297
298/*
299 List all files in \a directoryPath
300
301 \sa listed()
302*/
303void QFileInfoGatherer::list(const QString &directoryPath)
304{
305 fetchExtendedInformation(path: directoryPath, files: QStringList());
306}
307
308/*
309 Until aborted wait to fetch a directory or files
310*/
311void QFileInfoGatherer::run()
312{
313 forever {
314 QMutexLocker locker(&mutex);
315 while (!abort.loadRelaxed() && path.isEmpty())
316 condition.wait(lockedMutex: &mutex);
317 if (abort.loadRelaxed())
318 return;
319 const QString thisPath = qAsConst(t&: path).front();
320 path.pop_front();
321 const QStringList thisList = qAsConst(t&: files).front();
322 files.pop_front();
323 locker.unlock();
324
325 getFileInfos(path: thisPath, files: thisList);
326 }
327}
328
329QExtendedInformation QFileInfoGatherer::getInfo(const QFileInfo &fileInfo) const
330{
331 QExtendedInformation info(fileInfo);
332 info.icon = m_iconProvider->icon(info: fileInfo);
333 info.displayType = m_iconProvider->type(info: fileInfo);
334#if QT_CONFIG(filesystemwatcher)
335 // ### Not ready to listen all modifications by default
336 static const bool watchFiles = qEnvironmentVariableIsSet(varName: "QT_FILESYSTEMMODEL_WATCH_FILES");
337 if (watchFiles) {
338 if (!fileInfo.exists() && !fileInfo.isSymLink()) {
339 const_cast<QFileInfoGatherer *>(this)->
340 unwatchPaths(paths: QStringList(fileInfo.absoluteFilePath()));
341 } else {
342 const QString path = fileInfo.absoluteFilePath();
343 if (!path.isEmpty() && fileInfo.exists() && fileInfo.isFile() && fileInfo.isReadable()
344 && !watchedFiles().contains(str: path)) {
345 const_cast<QFileInfoGatherer *>(this)->watchPaths(paths: QStringList(path));
346 }
347 }
348 }
349#endif // filesystemwatcher
350
351#ifdef Q_OS_WIN
352 if (m_resolveSymlinks && info.isSymLink(/* ignoreNtfsSymLinks = */ true)) {
353 QFileInfo resolvedInfo(fileInfo.symLinkTarget());
354 resolvedInfo = resolvedInfo.canonicalFilePath();
355 if (resolvedInfo.exists()) {
356 emit nameResolved(fileInfo.filePath(), resolvedInfo.fileName());
357 }
358 }
359#endif
360 return info;
361}
362
363/*
364 Get specific file info's, batch the files so update when we have 100
365 items and every 200ms after that
366 */
367void QFileInfoGatherer::getFileInfos(const QString &path, const QStringList &files)
368{
369 // List drives
370 if (path.isEmpty()) {
371#ifdef QT_BUILD_INTERNAL
372 fetchedRoot.storeRelaxed(newValue: true);
373#endif
374 QFileInfoList infoList;
375 if (files.isEmpty()) {
376 infoList = QDir::drives();
377 } else {
378 infoList.reserve(alloc: files.count());
379 for (const auto &file : files)
380 infoList << QFileInfo(file);
381 }
382 QVector<QPair<QString,QFileInfo> > updatedFiles;
383 updatedFiles.reserve(asize: infoList.count());
384 for (int i = infoList.count() - 1; i >= 0; --i) {
385 QFileInfo driveInfo = infoList.at(i);
386 driveInfo.stat();
387 QString driveName = translateDriveName(drive: driveInfo);
388 updatedFiles.append(t: QPair<QString,QFileInfo>(driveName, driveInfo));
389 }
390 emit updates(directory: path, updates: updatedFiles);
391 return;
392 }
393
394 QElapsedTimer base;
395 base.start();
396 QFileInfo fileInfo;
397 bool firstTime = true;
398 QVector<QPair<QString, QFileInfo> > updatedFiles;
399 QStringList filesToCheck = files;
400
401 QStringList allFiles;
402 if (files.isEmpty()) {
403 QDirIterator dirIt(path, QDir::AllEntries | QDir::System | QDir::Hidden);
404 while (!abort.loadRelaxed() && dirIt.hasNext()) {
405 dirIt.next();
406 fileInfo = dirIt.fileInfo();
407 fileInfo.stat();
408 allFiles.append(t: fileInfo.fileName());
409 fetch(info: fileInfo, base, firstTime, updatedFiles, path);
410 }
411 }
412 if (!allFiles.isEmpty())
413 emit newListOfFiles(directory: path, listOfFiles: allFiles);
414
415 QStringList::const_iterator filesIt = filesToCheck.constBegin();
416 while (!abort.loadRelaxed() && filesIt != filesToCheck.constEnd()) {
417 fileInfo.setFile(path + QDir::separator() + *filesIt);
418 ++filesIt;
419 fileInfo.stat();
420 fetch(info: fileInfo, base, firstTime, updatedFiles, path);
421 }
422 if (!updatedFiles.isEmpty())
423 emit updates(directory: path, updates: updatedFiles);
424 emit directoryLoaded(path);
425}
426
427void QFileInfoGatherer::fetch(const QFileInfo &fileInfo, QElapsedTimer &base, bool &firstTime, QVector<QPair<QString, QFileInfo> > &updatedFiles, const QString &path) {
428 updatedFiles.append(t: QPair<QString, QFileInfo>(fileInfo.fileName(), fileInfo));
429 QElapsedTimer current;
430 current.start();
431 if ((firstTime && updatedFiles.count() > 100) || base.msecsTo(other: current) > 1000) {
432 emit updates(directory: path, updates: updatedFiles);
433 updatedFiles.clear();
434 base = current;
435 firstTime = false;
436 }
437}
438
439QT_END_NAMESPACE
440
441#include "moc_qfileinfogatherer_p.cpp"
442

source code of qtbase/src/widgets/dialogs/qfileinfogatherer.cpp