dialogs/qfileinfogatherer.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtGui 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 Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
38** $QT_END_LICENSE$ -
39** -
40****************************************************************************/ -
41 -
42#include "qfileinfogatherer_p.h" -
43#include <qdebug.h> -
44#include <qdiriterator.h> -
45#ifndef Q_OS_WIN -
46# include <unistd.h> -
47# include <sys/types.h> -
48#endif -
49#if defined(Q_OS_VXWORKS) -
50# include "qplatformdefs.h" -
51#endif -
52 -
53QT_BEGIN_NAMESPACE -
54 -
55#ifndef QT_NO_FILESYSTEMMODEL -
56 -
57#ifdef QT_BUILD_INTERNAL -
58static bool fetchedRoot = false; -
59Q_AUTOTEST_EXPORT void qt_test_resetFetchedRoot() -
60{ -
61 fetchedRoot = false; -
62} -
63 -
64Q_AUTOTEST_EXPORT bool qt_test_isFetchedRoot() -
65{ -
66 return fetchedRoot; -
67} -
68#endif -
69 -
70/*! -
71 Creates thread -
72*/ -
73QFileInfoGatherer::QFileInfoGatherer(QObject *parent) -
74 : QThread(parent), abort(false), -
75#ifndef QT_NO_FILESYSTEMWATCHER -
76 watcher(0), -
77#endif -
78#ifdef Q_OS_WIN -
79 m_resolveSymlinks(true), -
80#endif -
81 m_iconProvider(&defaultProvider) -
82{ -
83#ifndef QT_NO_FILESYSTEMWATCHER -
84 watcher = new QFileSystemWatcher(this);
executed (the execution status of this line is deduced): watcher = new QFileSystemWatcher(this);
-
85 connect(watcher, SIGNAL(directoryChanged(QString)), this, SLOT(list(QString)));
executed (the execution status of this line is deduced): connect(watcher, "2""directoryChanged(QString)", this, "1""list(QString)");
-
86 connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(updateFile(QString)));
executed (the execution status of this line is deduced): connect(watcher, "2""fileChanged(QString)", this, "1""updateFile(QString)");
-
87#endif -
88 start(LowPriority);
executed (the execution status of this line is deduced): start(LowPriority);
-
89}
executed: }
Execution Count:273
273
90 -
91/*! -
92 Destroys thread -
93*/ -
94QFileInfoGatherer::~QFileInfoGatherer() -
95{ -
96 abort.store(true);
executed (the execution status of this line is deduced): abort.store(true);
-
97 condition.wakeAll();
executed (the execution status of this line is deduced): condition.wakeAll();
-
98 wait();
executed (the execution status of this line is deduced): wait();
-
99}
executed: }
Execution Count:273
273
100 -
101void QFileInfoGatherer::setResolveSymlinks(bool enable) -
102{ -
103 Q_UNUSED(enable);
executed (the execution status of this line is deduced): (void)enable;;
-
104#ifdef Q_OS_WIN -
105 m_resolveSymlinks = enable; -
106#endif -
107}
executed: }
Execution Count:2
2
108 -
109bool QFileInfoGatherer::resolveSymlinks() const -
110{ -
111#ifdef Q_OS_WIN -
112 return m_resolveSymlinks; -
113#else -
114 return false;
executed: return false;
Execution Count:593
593
115#endif -
116} -
117 -
118void QFileInfoGatherer::setIconProvider(QFileIconProvider *provider) -
119{ -
120 m_iconProvider = provider;
executed (the execution status of this line is deduced): m_iconProvider = provider;
-
121}
executed: }
Execution Count:4
4
122 -
123QFileIconProvider *QFileInfoGatherer::iconProvider() const -
124{ -
125 return m_iconProvider;
executed: return m_iconProvider;
Execution Count:302
302
126} -
127 -
128/*! -
129 Fetch extended information for all \a files in \a path -
130 -
131 \sa updateFile(), update(), resolvedName() -
132*/ -
133void QFileInfoGatherer::fetchExtendedInformation(const QString &path, const QStringList &files) -
134{ -
135 QMutexLocker locker(&mutex);
executed (the execution status of this line is deduced): QMutexLocker locker(&mutex);
-
136 // See if we already have this dir/file in our queue -
137 int loc = this->path.lastIndexOf(path);
executed (the execution status of this line is deduced): int loc = this->path.lastIndexOf(path);
-
138 while (loc > 0) {
evaluated: loc > 0
TRUEFALSE
yes
Evaluation Count:7
yes
Evaluation Count:448
7-448
139 if (this->files.at(loc) == files) {
partially evaluated: this->files.at(loc) == files
TRUEFALSE
yes
Evaluation Count:7
no
Evaluation Count:0
0-7
140 return;
executed: return;
Execution Count:7
7
141 } -
142 loc = this->path.lastIndexOf(path, loc - 1);
never executed (the execution status of this line is deduced): loc = this->path.lastIndexOf(path, loc - 1);
-
143 }
never executed: }
0
144 this->path.push(path);
executed (the execution status of this line is deduced): this->path.push(path);
-
145 this->files.push(files);
executed (the execution status of this line is deduced): this->files.push(files);
-
146 condition.wakeAll();
executed (the execution status of this line is deduced): condition.wakeAll();
-
147}
executed: }
Execution Count:448
448
148 -
149/*! -
150 Fetch extended information for all \a filePath -
151 -
152 \sa fetchExtendedInformation() -
153*/ -
154void QFileInfoGatherer::updateFile(const QString &filePath) -
155{ -
156 QString dir = filePath.mid(0, filePath.lastIndexOf(QDir::separator()));
never executed (the execution status of this line is deduced): QString dir = filePath.mid(0, filePath.lastIndexOf(QDir::separator()));
-
157 QString fileName = filePath.mid(dir.length() + 1);
never executed (the execution status of this line is deduced): QString fileName = filePath.mid(dir.length() + 1);
-
158 fetchExtendedInformation(dir, QStringList(fileName));
never executed (the execution status of this line is deduced): fetchExtendedInformation(dir, QStringList(fileName));
-
159}
never executed: }
0
160 -
161/* -
162 List all files in \a directoryPath -
163 -
164 \sa listed() -
165*/ -
166void QFileInfoGatherer::clear() -
167{ -
168#ifndef QT_NO_FILESYSTEMWATCHER -
169 QMutexLocker locker(&mutex);
never executed (the execution status of this line is deduced): QMutexLocker locker(&mutex);
-
170 watcher->removePaths(watcher->files());
never executed (the execution status of this line is deduced): watcher->removePaths(watcher->files());
-
171 watcher->removePaths(watcher->directories());
never executed (the execution status of this line is deduced): watcher->removePaths(watcher->directories());
-
172#endif -
173}
never executed: }
0
174 -
175/* -
176 Remove a \a path from the watcher -
177 -
178 \sa listed() -
179*/ -
180void QFileInfoGatherer::removePath(const QString &path) -
181{ -
182#ifndef QT_NO_FILESYSTEMWATCHER -
183 QMutexLocker locker(&mutex);
executed (the execution status of this line is deduced): QMutexLocker locker(&mutex);
-
184 watcher->removePath(path);
executed (the execution status of this line is deduced): watcher->removePath(path);
-
185#endif -
186}
executed: }
Execution Count:52
52
187 -
188/* -
189 List all files in \a directoryPath -
190 -
191 \sa listed() -
192*/ -
193void QFileInfoGatherer::list(const QString &directoryPath) -
194{ -
195 fetchExtendedInformation(directoryPath, QStringList());
executed (the execution status of this line is deduced): fetchExtendedInformation(directoryPath, QStringList());
-
196}
executed: }
Execution Count:455
455
197 -
198/* -
199 Until aborted wait to fetch a directory or files -
200*/ -
201void QFileInfoGatherer::run() -
202{ -
203 forever {
executed (the execution status of this line is deduced): for(;;) {
-
204 QMutexLocker locker(&mutex);
executed (the execution status of this line is deduced): QMutexLocker locker(&mutex);
-
205 while (!abort.load() && path.isEmpty())
evaluated: !abort.load()
TRUEFALSE
yes
Evaluation Count:1020
yes
Evaluation Count:273
evaluated: path.isEmpty()
TRUEFALSE
yes
Evaluation Count:581
yes
Evaluation Count:439
273-1020
206 condition.wait(&mutex);
executed: condition.wait(&mutex);
Execution Count:581
581
207 if (abort.load())
evaluated: abort.load()
TRUEFALSE
yes
Evaluation Count:273
yes
Evaluation Count:439
273-439
208 return;
executed: return;
Execution Count:273
273
209 const QString thisPath = path.front();
executed (the execution status of this line is deduced): const QString thisPath = path.front();
-
210 path.pop_front();
executed (the execution status of this line is deduced): path.pop_front();
-
211 const QStringList thisList = files.front();
executed (the execution status of this line is deduced): const QStringList thisList = files.front();
-
212 files.pop_front();
executed (the execution status of this line is deduced): files.pop_front();
-
213 locker.unlock();
executed (the execution status of this line is deduced): locker.unlock();
-
214 -
215 getFileInfos(thisPath, thisList);
executed (the execution status of this line is deduced): getFileInfos(thisPath, thisList);
-
216 }
executed: }
Execution Count:439
439
217}
never executed: }
0
218 -
219QExtendedInformation QFileInfoGatherer::getInfo(const QFileInfo &fileInfo) const -
220{ -
221 QExtendedInformation info(fileInfo);
executed (the execution status of this line is deduced): QExtendedInformation info(fileInfo);
-
222 info.icon = m_iconProvider->icon(fileInfo);
executed (the execution status of this line is deduced): info.icon = m_iconProvider->icon(fileInfo);
-
223 info.displayType = m_iconProvider->type(fileInfo);
executed (the execution status of this line is deduced): info.displayType = m_iconProvider->type(fileInfo);
-
224#ifndef QT_NO_FILESYSTEMWATCHER -
225 // ### Not ready to listen all modifications -
226 #if 0 -
227 // Enable the next two commented out lines to get updates when the file sizes change... -
228 if (!fileInfo.exists() && !fileInfo.isSymLink()) { -
229 info.size = -1; -
230 //watcher->removePath(fileInfo.absoluteFilePath()); -
231 } else { -
232 if (!fileInfo.absoluteFilePath().isEmpty() && fileInfo.exists() && fileInfo.isReadable() -
233 && !watcher->files().contains(fileInfo.absoluteFilePath())) { -
234 //watcher->addPath(fileInfo.absoluteFilePath()); -
235 } -
236 } -
237 #endif -
238#endif -
239 -
240#ifdef Q_OS_WIN -
241 if (fileInfo.isSymLink() && m_resolveSymlinks) { -
242 QFileInfo resolvedInfo(fileInfo.symLinkTarget()); -
243 resolvedInfo = resolvedInfo.canonicalFilePath(); -
244 if (resolvedInfo.exists()) { -
245 emit nameResolved(fileInfo.filePath(), resolvedInfo.fileName()); -
246 } -
247 } -
248#endif -
249 return info;
executed: return info;
Execution Count:28393
28393
250} -
251 -
252static QString translateDriveName(const QFileInfo &drive) -
253{ -
254 QString driveName = drive.absoluteFilePath();
executed (the execution status of this line is deduced): QString driveName = drive.absoluteFilePath();
-
255#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) -
256 if (driveName.startsWith(QLatin1Char('/'))) // UNC host -
257 return drive.fileName(); -
258 if (driveName.endsWith(QLatin1Char('/'))) -
259 driveName.chop(1); -
260#endif -
261 return driveName;
executed: return driveName;
Execution Count:97
97
262} -
263 -
264/* -
265 Get specific file info's, batch the files so update when we have 100 -
266 items and every 200ms after that -
267 */ -
268void QFileInfoGatherer::getFileInfos(const QString &path, const QStringList &files) -
269{ -
270#ifndef QT_NO_FILESYSTEMWATCHER -
271 if (files.isEmpty()
partially evaluated: files.isEmpty()
TRUEFALSE
yes
Evaluation Count:439
no
Evaluation Count:0
0-439
272 && !path.isEmpty()
evaluated: !path.isEmpty()
TRUEFALSE
yes
Evaluation Count:342
yes
Evaluation Count:97
97-342
273 && !path.startsWith(QLatin1String("//")) /*don't watch UNC path*/) {
partially evaluated: !path.startsWith(QLatin1String("//"))
TRUEFALSE
yes
Evaluation Count:342
no
Evaluation Count:0
0-342
274 QMutexLocker locker(&mutex);
executed (the execution status of this line is deduced): QMutexLocker locker(&mutex);
-
275 if (!watcher->directories().contains(path))
evaluated: !watcher->directories().contains(path)
TRUEFALSE
yes
Evaluation Count:299
yes
Evaluation Count:43
43-299
276 watcher->addPath(path);
executed: watcher->addPath(path);
Execution Count:299
299
277 }
executed: }
Execution Count:342
342
278#endif -
279 -
280 // List drives -
281 if (path.isEmpty()) {
evaluated: path.isEmpty()
TRUEFALSE
yes
Evaluation Count:97
yes
Evaluation Count:342
97-342
282#ifdef QT_BUILD_INTERNAL -
283 fetchedRoot = true; -
284#endif -
285 QFileInfoList infoList;
executed (the execution status of this line is deduced): QFileInfoList infoList;
-
286 if (files.isEmpty()) {
partially evaluated: files.isEmpty()
TRUEFALSE
yes
Evaluation Count:97
no
Evaluation Count:0
0-97
287 infoList = QDir::drives();
executed (the execution status of this line is deduced): infoList = QDir::drives();
-
288 } else {
executed: }
Execution Count:97
97
289 infoList.reserve(files.count());
never executed (the execution status of this line is deduced): infoList.reserve(files.count());
-
290 for (int i = 0; i < files.count(); ++i)
never evaluated: i < files.count()
0
291 infoList << QFileInfo(files.at(i));
never executed: infoList << QFileInfo(files.at(i));
0
292 }
never executed: }
0
293 for (int i = infoList.count() - 1; i >= 0; --i) {
evaluated: i >= 0
TRUEFALSE
yes
Evaluation Count:97
yes
Evaluation Count:97
97
294 QString driveName = translateDriveName(infoList.at(i));
executed (the execution status of this line is deduced): QString driveName = translateDriveName(infoList.at(i));
-
295 QList<QPair<QString,QFileInfo> > updatedFiles;
executed (the execution status of this line is deduced): QList<QPair<QString,QFileInfo> > updatedFiles;
-
296 updatedFiles.append(QPair<QString,QFileInfo>(driveName, infoList.at(i)));
executed (the execution status of this line is deduced): updatedFiles.append(QPair<QString,QFileInfo>(driveName, infoList.at(i)));
-
297 emit updates(path, updatedFiles);
executed (the execution status of this line is deduced): updates(path, updatedFiles);
-
298 }
executed: }
Execution Count:97
97
299 return;
executed: return;
Execution Count:97
97
300 } -
301 -
302 QElapsedTimer base;
executed (the execution status of this line is deduced): QElapsedTimer base;
-
303 base.start();
executed (the execution status of this line is deduced): base.start();
-
304 QFileInfo fileInfo;
executed (the execution status of this line is deduced): QFileInfo fileInfo;
-
305 bool firstTime = true;
executed (the execution status of this line is deduced): bool firstTime = true;
-
306 QList<QPair<QString, QFileInfo> > updatedFiles;
executed (the execution status of this line is deduced): QList<QPair<QString, QFileInfo> > updatedFiles;
-
307 QStringList filesToCheck = files;
executed (the execution status of this line is deduced): QStringList filesToCheck = files;
-
308 -
309 QString itPath = QDir::fromNativeSeparators(files.isEmpty() ? path : QLatin1String(""));
executed (the execution status of this line is deduced): QString itPath = QDir::fromNativeSeparators(files.isEmpty() ? path : QLatin1String(""));
-
310 QDirIterator dirIt(itPath, QDir::AllEntries | QDir::System | QDir::Hidden);
executed (the execution status of this line is deduced): QDirIterator dirIt(itPath, QDir::AllEntries | QDir::System | QDir::Hidden);
-
311 QStringList allFiles;
executed (the execution status of this line is deduced): QStringList allFiles;
-
312 while (!abort.load() && dirIt.hasNext()) {
evaluated: !abort.load()
TRUEFALSE
yes
Evaluation Count:50672
yes
Evaluation Count:43
evaluated: dirIt.hasNext()
TRUEFALSE
yes
Evaluation Count:50373
yes
Evaluation Count:299
43-50672
313 dirIt.next();
executed (the execution status of this line is deduced): dirIt.next();
-
314 fileInfo = dirIt.fileInfo();
executed (the execution status of this line is deduced): fileInfo = dirIt.fileInfo();
-
315 allFiles.append(fileInfo.fileName());
executed (the execution status of this line is deduced): allFiles.append(fileInfo.fileName());
-
316 fetch(fileInfo, base, firstTime, updatedFiles, path);
executed (the execution status of this line is deduced): fetch(fileInfo, base, firstTime, updatedFiles, path);
-
317 }
executed: }
Execution Count:50374
50374
318 if (!allFiles.isEmpty())
partially evaluated: !allFiles.isEmpty()
TRUEFALSE
yes
Evaluation Count:342
no
Evaluation Count:0
0-342
319 emit newListOfFiles(path, allFiles);
executed: newListOfFiles(path, allFiles);
Execution Count:342
342
320 -
321 QStringList::const_iterator filesIt = filesToCheck.constBegin();
executed (the execution status of this line is deduced): QStringList::const_iterator filesIt = filesToCheck.constBegin();
-
322 while (!abort.load() && filesIt != filesToCheck.constEnd()) {
evaluated: !abort.load()
TRUEFALSE
yes
Evaluation Count:299
yes
Evaluation Count:43
partially evaluated: filesIt != filesToCheck.constEnd()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:299
0-299
323 fileInfo.setFile(path + QDir::separator() + *filesIt);
never executed (the execution status of this line is deduced): fileInfo.setFile(path + QDir::separator() + *filesIt);
-
324 ++filesIt;
never executed (the execution status of this line is deduced): ++filesIt;
-
325 fetch(fileInfo, base, firstTime, updatedFiles, path);
never executed (the execution status of this line is deduced): fetch(fileInfo, base, firstTime, updatedFiles, path);
-
326 }
never executed: }
0
327 if (!updatedFiles.isEmpty())
partially evaluated: !updatedFiles.isEmpty()
TRUEFALSE
yes
Evaluation Count:342
no
Evaluation Count:0
0-342
328 emit updates(path, updatedFiles);
executed: updates(path, updatedFiles);
Execution Count:342
342
329 emit directoryLoaded(path);
executed (the execution status of this line is deduced): directoryLoaded(path);
-
330}
executed: }
Execution Count:342
342
331 -
332void QFileInfoGatherer::fetch(const QFileInfo &fileInfo, QElapsedTimer &base, bool &firstTime, QList<QPair<QString, QFileInfo> > &updatedFiles, const QString &path) { -
333 updatedFiles.append(QPair<QString, QFileInfo>(fileInfo.fileName(), fileInfo));
executed (the execution status of this line is deduced): updatedFiles.append(QPair<QString, QFileInfo>(fileInfo.fileName(), fileInfo));
-
334 QElapsedTimer current;
executed (the execution status of this line is deduced): QElapsedTimer current;
-
335 current.start();
executed (the execution status of this line is deduced): current.start();
-
336 if ((firstTime && updatedFiles.count() > 100) || base.msecsTo(current) > 1000) {
evaluated: firstTime
TRUEFALSE
yes
Evaluation Count:11022
yes
Evaluation Count:39354
evaluated: updatedFiles.count() > 100
TRUEFALSE
yes
Evaluation Count:79
yes
Evaluation Count:10943
partially evaluated: base.msecsTo(current) > 1000
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:50297
0-50297
337 emit updates(path, updatedFiles);
executed (the execution status of this line is deduced): updates(path, updatedFiles);
-
338 updatedFiles.clear();
executed (the execution status of this line is deduced): updatedFiles.clear();
-
339 base = current;
executed (the execution status of this line is deduced): base = current;
-
340 firstTime = false;
executed (the execution status of this line is deduced): firstTime = false;
-
341 }
executed: }
Execution Count:79
79
342}
executed: }
Execution Count:50374
50374
343 -
344#endif // QT_NO_FILESYSTEMMODEL -
345 -
346QT_END_NAMESPACE -
347 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial