Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | | - |
11 | | - |
12 | | - |
13 | | - |
14 | | - |
15 | | - |
16 | | - |
17 | | - |
18 | | - |
19 | | - |
20 | | - |
21 | | - |
22 | | - |
23 | | - |
24 | | - |
25 | | - |
26 | | - |
27 | | - |
28 | | - |
29 | | - |
30 | | - |
31 | | - |
32 | | - |
33 | | - |
34 | | - |
35 | | - |
36 | | - |
37 | | - |
38 | | - |
39 | | - |
40 | #include "qfileinfogatherer_p.h" | - |
41 | #include <qdebug.h> | - |
42 | #include <qdiriterator.h> | - |
43 | #ifndef Q_OS_WIN | - |
44 | # include <unistd.h> | - |
45 | # include <sys/types.h> | - |
46 | #endif | - |
47 | #if defined(Q_OS_VXWORKS) | - |
48 | # include "qplatformdefs.h" | - |
49 | #endif | - |
50 | | - |
51 | QT_BEGIN_NAMESPACE | - |
52 | | - |
53 | #ifndef QT_NO_FILESYSTEMMODEL | - |
54 | | - |
55 | #ifdef QT_BUILD_INTERNAL | - |
56 | static QBasicAtomicInt fetchedRoot = Q_BASIC_ATOMIC_INITIALIZER(false); | - |
57 | Q_AUTOTEST_EXPORT void qt_test_resetFetchedRoot() | - |
58 | { | - |
59 | fetchedRoot.store(false); | - |
60 | } | - |
61 | | - |
62 | Q_AUTOTEST_EXPORT bool qt_test_isFetchedRoot() | - |
63 | { | - |
64 | return fetchedRoot.load(); | - |
65 | } | - |
66 | #endif | - |
67 | | - |
68 | | - |
69 | | - |
70 | | - |
71 | QFileInfoGatherer::QFileInfoGatherer(QObject *parent) | - |
72 | : QThread(parent), abort(false), | - |
73 | #ifndef QT_NO_FILESYSTEMWATCHER | - |
74 | watcher(0), | - |
75 | #endif | - |
76 | #ifdef Q_OS_WIN | - |
77 | m_resolveSymlinks(true), | - |
78 | #endif | - |
79 | m_iconProvider(&defaultProvider) | - |
80 | { | - |
81 | #ifndef QT_NO_FILESYSTEMWATCHER | - |
82 | watcher = new QFileSystemWatcher(this); | - |
83 | connect(watcher, SIGNAL(directoryChanged(QString)), this, SLOT(list(QString))); | - |
84 | connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(updateFile(QString))); | - |
85 | #endif | - |
86 | start(LowPriority); | - |
87 | } | - |
88 | | - |
89 | | - |
90 | | - |
91 | | - |
92 | QFileInfoGatherer::~QFileInfoGatherer() | - |
93 | { | - |
94 | abort.store(true); | - |
95 | QMutexLocker locker(&mutex); | - |
96 | condition.wakeAll(); | - |
97 | locker.unlock(); | - |
98 | wait(); | - |
99 | } | - |
100 | | - |
101 | void QFileInfoGatherer::setResolveSymlinks(bool enable) | - |
102 | { | - |
103 | Q_UNUSED(enable); | - |
104 | #ifdef Q_OS_WIN | - |
105 | m_resolveSymlinks = enable; | - |
106 | #endif | - |
107 | } | - |
108 | | - |
109 | bool QFileInfoGatherer::resolveSymlinks() const | - |
110 | { | - |
111 | #ifdef Q_OS_WIN | - |
112 | return m_resolveSymlinks; | - |
113 | #else | - |
114 | return false; | - |
115 | #endif | - |
116 | } | - |
117 | | - |
118 | void QFileInfoGatherer::setIconProvider(QFileIconProvider *provider) | - |
119 | { | - |
120 | m_iconProvider = provider; | - |
121 | } | - |
122 | | - |
123 | QFileIconProvider *QFileInfoGatherer::iconProvider() const | - |
124 | { | - |
125 | return m_iconProvider; | - |
126 | } | - |
127 | | - |
128 | | - |
129 | | - |
130 | | - |
131 | | - |
132 | | - |
133 | void QFileInfoGatherer::fetchExtendedInformation(const QString &path, const QStringList &files) | - |
134 | { | - |
135 | QMutexLocker locker(&mutex); | - |
136 | | - |
137 | int loc = this->path.lastIndexOf(path); | - |
138 | while (loc > 0) { | - |
139 | if (this->files.at(loc) == files) { | - |
140 | return; | - |
141 | } | - |
142 | loc = this->path.lastIndexOf(path, loc - 1); | - |
143 | } | - |
144 | this->path.push(path); | - |
145 | this->files.push(files); | - |
146 | condition.wakeAll(); | - |
147 | | - |
148 | #ifndef QT_NO_FILESYSTEMWATCHER | - |
149 | if (files.isEmpty() | - |
150 | && !path.isEmpty() | - |
151 | && !path.startsWith(QLatin1String("//")) ) { | - |
152 | if (!watcher->directories().contains(path)) | - |
153 | watcher->addPath(path); | - |
154 | } | - |
155 | #endif | - |
156 | } | - |
157 | | - |
158 | | - |
159 | | - |
160 | | - |
161 | | - |
162 | | - |
163 | void QFileInfoGatherer::updateFile(const QString &filePath) | - |
164 | { | - |
165 | QString dir = filePath.mid(0, filePath.lastIndexOf(QDir::separator())); | - |
166 | QString fileName = filePath.mid(dir.length() + 1); | - |
167 | fetchExtendedInformation(dir, QStringList(fileName)); | - |
168 | } | - |
169 | | - |
170 | | - |
171 | | - |
172 | | - |
173 | | - |
174 | | - |
175 | void QFileInfoGatherer::clear() | - |
176 | { | - |
177 | #ifndef QT_NO_FILESYSTEMWATCHER | - |
178 | QMutexLocker locker(&mutex); | - |
179 | watcher->removePaths(watcher->files()); | - |
180 | watcher->removePaths(watcher->directories()); | - |
181 | #endif | - |
182 | } | - |
183 | | - |
184 | | - |
185 | | - |
186 | | - |
187 | | - |
188 | | - |
189 | void QFileInfoGatherer::removePath(const QString &path) | - |
190 | { | - |
191 | #ifndef QT_NO_FILESYSTEMWATCHER | - |
192 | QMutexLocker locker(&mutex); | - |
193 | watcher->removePath(path); | - |
194 | #else | - |
195 | Q_UNUSED(path); | - |
196 | #endif | - |
197 | } | - |
198 | | - |
199 | | - |
200 | | - |
201 | | - |
202 | | - |
203 | | - |
204 | void QFileInfoGatherer::list(const QString &directoryPath) | - |
205 | { | - |
206 | fetchExtendedInformation(directoryPath, QStringList()); | - |
207 | } | - |
208 | | - |
209 | | - |
210 | | - |
211 | | - |
212 | void QFileInfoGatherer::run() | - |
213 | { | - |
214 | forever { | - |
215 | QMutexLocker locker(&mutex); | - |
216 | while (!abort.load() && path.isEmpty())TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
217 | condition.wait(&mutex); never executed: condition.wait(&mutex); | 0 |
218 | if (abort.load())TRUE | never evaluated | FALSE | never evaluated |
| 0 |
219 | return; never executed: return; | 0 |
220 | const QString thisPath = qAsConst(path.).front(); | - |
221 | path.pop_front(); | - |
222 | const QStringList thisList = qAsConst(files.).front(); | - |
223 | files.pop_front(); | - |
224 | locker.unlock(); | - |
225 | | - |
226 | getFileInfos(thisPath, thisList); | - |
227 | } never executed: end of block | 0 |
228 | } never executed: end of block | 0 |
229 | | - |
230 | QExtendedInformation QFileInfoGatherer::getInfo(const QFileInfo &fileInfo) const | - |
231 | { | - |
232 | QExtendedInformation info(fileInfo); | - |
233 | info.icon = m_iconProvider->icon(fileInfo); | - |
234 | info.displayType = m_iconProvider->type(fileInfo); | - |
235 | #ifndef QT_NO_FILESYSTEMWATCHER | - |
236 | | - |
237 | #if 0 | - |
238 | | - |
239 | if (!fileInfo.exists() && !fileInfo.isSymLink()) { | - |
240 | info.size = -1; | - |
241 | | - |
242 | } else { | - |
243 | if (!fileInfo.absoluteFilePath().isEmpty() && fileInfo.exists() && fileInfo.isReadable() | - |
244 | && !watcher->files().contains(fileInfo.absoluteFilePath())) { | - |
245 | | - |
246 | } | - |
247 | } | - |
248 | #endif | - |
249 | #endif | - |
250 | | - |
251 | #ifdef Q_OS_WIN | - |
252 | if (m_resolveSymlinks && info.isSymLink( true)) { | - |
253 | QFileInfo resolvedInfo(fileInfo.symLinkTarget()); | - |
254 | resolvedInfo = resolvedInfo.canonicalFilePath(); | - |
255 | if (resolvedInfo.exists()) { | - |
256 | emit nameResolved(fileInfo.filePath(), resolvedInfo.fileName()); | - |
257 | } | - |
258 | } | - |
259 | #endif | - |
260 | return info; | - |
261 | } | - |
262 | | - |
263 | static QString translateDriveName(const QFileInfo &drive) | - |
264 | { | - |
265 | QString driveName = drive.absoluteFilePath(); | - |
266 | #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) | - |
267 | if (driveName.startsWith(QLatin1Char('/'))) | - |
268 | return drive.fileName(); | - |
269 | if (driveName.endsWith(QLatin1Char('/'))) | - |
270 | driveName.chop(1); | - |
271 | #endif | - |
272 | return driveName; | - |
273 | } | - |
274 | | - |
275 | | - |
276 | | - |
277 | | - |
278 | | - |
279 | void QFileInfoGatherer::getFileInfos(const QString &path, const QStringList &files) | - |
280 | { | - |
281 | | - |
282 | if (path.isEmpty()) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
283 | #ifdef QT_BUILD_INTERNAL | - |
284 | fetchedRoot.store(true); | - |
285 | #endif | - |
286 | QFileInfoList infoList; | - |
287 | if (files.isEmpty()) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
288 | infoList = QDir::drives(); | - |
289 | } else { never executed: end of block | 0 |
290 | infoList.reserve(files.count()); | - |
291 | for (int i = 0; i <const auto &file : files.count(); ++i) | - |
292 | infoList << QFileInfo(files.at(i));file); never executed: infoList << QFileInfo(file); | 0 |
293 | } never executed: end of block | 0 |
294 | for (int i = infoList.count() - 1; i >= 0; --i) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
295 | QString driveName = translateDriveName(infoList.at(i)); | - |
296 | QVector<QPair<QString,QFileInfo> > updatedFiles; | - |
297 | updatedFiles.append(QPair<QString,QFileInfo>(driveName, infoList.at(i))); | - |
298 | emit updates(path, updatedFiles); | - |
299 | } never executed: end of block | 0 |
300 | return; never executed: return; | 0 |
301 | } | - |
302 | | - |
303 | QElapsedTimer base; | - |
304 | base.start(); | - |
305 | QFileInfo fileInfo; | - |
306 | bool firstTime = true; | - |
307 | QVector<QPair<QString, QFileInfo> > updatedFiles; | - |
308 | QStringList filesToCheck = files; | - |
309 | | - |
310 | QString itPath = QDir::fromNativeSeparators(files.isEmpty() ? path : QLatin1String("")); | - |
311 | QDirIterator dirIt(itPath, QDir::AllEntries | QDir::System | QDir::Hidden); | - |
312 | QStringList allFiles; | - |
313 | while (!abort.load() && dirIt.hasNext()) {TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
314 | dirIt.next(); | - |
315 | fileInfo = dirIt.fileInfo(); | - |
316 | allFiles.append(fileInfo.fileName()); | - |
317 | fetch(fileInfo, base, firstTime, updatedFiles, path); | - |
318 | } never executed: end of block | 0 |
319 | if (!allFiles.isEmpty())TRUE | never evaluated | FALSE | never evaluated |
| 0 |
320 | emit newListOfFiles(path, allFiles); never executed: newListOfFiles(path, allFiles); | 0 |
321 | | - |
322 | QStringList::const_iterator filesIt = filesToCheck.constBegin(); | - |
323 | while (!abort.load() && filesIt != filesToCheck.constEnd()) {TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
324 | fileInfo.setFile(path + QDir::separator() + *filesIt); | - |
325 | ++filesIt; | - |
326 | fetch(fileInfo, base, firstTime, updatedFiles, path); | - |
327 | } never executed: end of block | 0 |
328 | if (!updatedFiles.isEmpty())TRUE | never evaluated | FALSE | never evaluated |
| 0 |
329 | emit updates(path, updatedFiles); never executed: updates(path, updatedFiles); | 0 |
330 | emit directoryLoaded(path); | - |
331 | } never executed: end of block | 0 |
332 | | - |
333 | void QFileInfoGatherer::fetch(const QFileInfo &fileInfo, QElapsedTimer &base, bool &firstTime, QVector<QPair<QString, QFileInfo> > &updatedFiles, const QString &path) { | - |
334 | updatedFiles.append(QPair<QString, QFileInfo>(fileInfo.fileName(), fileInfo)); | - |
335 | QElapsedTimer current; | - |
336 | current.start(); | - |
337 | if ((firstTime && updatedFiles.count() > 100) || base.msecsTo(current) > 1000) { | - |
338 | emit updates(path, updatedFiles); | - |
339 | updatedFiles.clear(); | - |
340 | base = current; | - |
341 | firstTime = false; | - |
342 | } | - |
343 | } | - |
344 | | - |
345 | #endif // QT_NO_FILESYSTEMMODEL | - |
346 | | - |
347 | QT_END_NAMESPACE | - |
348 | | - |
349 | #include "moc_qfileinfogatherer_p.cpp" | - |
| | |