source: trunk/src/gui/dialogs/qfileinfogatherer.cpp@ 182

Last change on this file since 182 was 172, checked in by Dmitry A. Kuminov, 16 years ago

corelib/io, gui/dialogs: Handle DOS-like filesystem details (drives, slashes) on OS/2 similarly to Win32.

File size: 11.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qfileinfogatherer_p.h"
43#include <qdebug.h>
44#include <qfsfileengine.h>
45#include <qdiriterator.h>
46#ifndef Q_OS_WIN
47#include <unistd.h>
48#include <sys/types.h>
49#endif
50
51QT_BEGIN_NAMESPACE
52
53#ifndef QT_NO_FILESYSTEMMODEL
54
55bool QFileInfoGatherer::fetchedRoot = false;
56
57/*!
58 Creates thread
59*/
60QFileInfoGatherer::QFileInfoGatherer(QObject *parent)
61 : QThread(parent), abort(false),
62#ifndef QT_NO_FILESYSTEMWATCHER
63 watcher(0),
64#endif
65 m_resolveSymlinks(false), m_iconProvider(&defaultProvider)
66{
67#ifndef Q_OS_WIN
68 userId = getuid();
69 groupId = getgid();
70#else
71 m_resolveSymlinks = true;
72#endif
73#ifndef QT_NO_FILESYSTEMWATCHER
74 watcher = new QFileSystemWatcher(this);
75 connect(watcher, SIGNAL(directoryChanged(const QString &)), this, SLOT(list(const QString &)));
76 connect(watcher, SIGNAL(fileChanged(const QString &)), this, SLOT(updateFile(const QString &)));
77#endif
78 start(LowPriority);
79}
80
81/*!
82 Destroys thread
83*/
84QFileInfoGatherer::~QFileInfoGatherer()
85{
86 mutex.lock();
87 abort = true;
88 condition.wakeOne();
89 mutex.unlock();
90 wait();
91}
92
93void QFileInfoGatherer::setResolveSymlinks(bool enable)
94{
95 Q_UNUSED(enable);
96#ifdef Q_OS_WIN
97 mutex.lock();
98 m_resolveSymlinks = enable;
99 mutex.unlock();
100#endif
101}
102
103bool QFileInfoGatherer::resolveSymlinks() const
104{
105 return m_resolveSymlinks;
106}
107
108void QFileInfoGatherer::setIconProvider(QFileIconProvider *provider)
109{
110 mutex.lock();
111 m_iconProvider = provider;
112 mutex.unlock();
113}
114
115QFileIconProvider *QFileInfoGatherer::iconProvider() const
116{
117 return m_iconProvider;
118}
119
120/*!
121 Fetch extended information for all \a files in \a path
122
123 \sa updateFile(), update(), resolvedName()
124*/
125void QFileInfoGatherer::fetchExtendedInformation(const QString &path, const QStringList &files)
126{
127 mutex.lock();
128 // See if we already have this dir/file in our que
129 int loc = this->path.lastIndexOf(path);
130 while (loc > 0) {
131 if (this->files.at(loc) == files) {
132 mutex.unlock();
133 return;
134 }
135 loc = this->path.lastIndexOf(path, loc - 1);
136 }
137 this->path.push(path);
138 this->files.push(files);
139 condition.wakeAll();
140 mutex.unlock();
141}
142
143/*!
144 Fetch extended information for all \a filePath
145
146 \sa fetchExtendedInformation()
147*/
148void QFileInfoGatherer::updateFile(const QString &filePath)
149{
150 QString dir = filePath.mid(0, filePath.lastIndexOf(QDir::separator()));
151 QString fileName = filePath.mid(dir.length() + 1);
152 fetchExtendedInformation(dir, QStringList(fileName));
153}
154
155/*
156 List all files in \a directoryPath
157
158 \sa listed()
159*/
160void QFileInfoGatherer::clear()
161{
162#ifndef QT_NO_FILESYSTEMWATCHER
163 mutex.lock();
164 watcher->removePaths(watcher->files());
165 watcher->removePaths(watcher->directories());
166 mutex.unlock();
167#endif
168}
169
170/*
171 Remove a \a path from the watcher
172
173 \sa listed()
174*/
175void QFileInfoGatherer::removePath(const QString &path)
176{
177#ifndef QT_NO_FILESYSTEMWATCHER
178 mutex.lock();
179 watcher->removePath(path);
180 mutex.unlock();
181#endif
182}
183
184/*
185 List all files in \a directoryPath
186
187 \sa listed()
188*/
189void QFileInfoGatherer::list(const QString &directoryPath)
190{
191 fetchExtendedInformation(directoryPath, QStringList());
192}
193
194/*
195 Until aborted wait to fetch a directory or files
196*/
197void QFileInfoGatherer::run()
198{
199 forever {
200 bool updateFiles = false;
201 mutex.lock();
202 if (abort) {
203 mutex.unlock();
204 return;
205 }
206 if (this->path.isEmpty())
207 condition.wait(&mutex);
208 QString path;
209 QStringList list;
210 if (!this->path.isEmpty()) {
211 path = this->path.first();
212 list = this->files.first();
213 this->path.pop_front();
214 this->files.pop_front();
215 updateFiles = true;
216 }
217 mutex.unlock();
218 if (updateFiles) getFileInfos(path, list);
219 }
220}
221
222/*
223 QFileInfo::permissions is different depending upon your platform.
224
225 "normalize this" so they can mean the same to us.
226*/
227QFile::Permissions QFileInfoGatherer::translatePermissions(const QFileInfo &fileInfo) const {
228 QFile::Permissions permissions = fileInfo.permissions();
229#ifdef Q_OS_WIN
230 return permissions;
231#else
232 QFile::Permissions p = permissions;
233 p &= ~(QFile::ReadUser|QFile::WriteUser|QFile::ExeUser);
234 if ( permissions & QFile::ReadOther
235 || (fileInfo.ownerId() == userId && permissions & QFile::ReadOwner)
236 || (fileInfo.groupId() == groupId && permissions & QFile::ReadGroup))
237 p |= QFile::ReadUser;
238
239 if ( permissions & QFile::WriteOther
240 || (fileInfo.ownerId() == userId && permissions & QFile::WriteOwner)
241 || (fileInfo.groupId() == groupId && permissions & QFile::WriteGroup))
242 p |= QFile::WriteUser;
243
244 if ( permissions & QFile::ExeOther
245 || (fileInfo.ownerId() == userId && permissions & QFile::ExeOwner)
246 || (fileInfo.groupId() == groupId && permissions & QFile::ExeGroup))
247 p |= QFile::ExeUser;
248 return p;
249#endif
250}
251
252QExtendedInformation QFileInfoGatherer::getInfo(const QFileInfo &fileInfo) const
253{
254 QExtendedInformation info(fileInfo);
255 info.icon = m_iconProvider->icon(fileInfo);
256 info.setPermissions(translatePermissions(fileInfo));
257 info.displayType = m_iconProvider->type(fileInfo);
258#ifndef QT_NO_FILESYSTEMWATCHER
259 // ### Not ready to listen all modifications
260 #if 0
261 // Enable the next two commented out lines to get updates when the file sizes change...
262 if (!fileInfo.exists() && !fileInfo.isSymLink()) {
263 info.size = -1;
264 //watcher->removePath(fileInfo.absoluteFilePath());
265 } else {
266 if (!fileInfo.absoluteFilePath().isEmpty() && fileInfo.exists() && fileInfo.isReadable()
267 && !watcher->files().contains(fileInfo.absoluteFilePath())) {
268 //watcher->addPath(fileInfo.absoluteFilePath());
269 }
270 }
271 #endif
272#endif
273
274 if (fileInfo.isSymLink() && m_resolveSymlinks) {
275 QFileInfo resolvedInfo(fileInfo.symLinkTarget());
276 resolvedInfo = resolvedInfo.canonicalFilePath();
277 if (resolvedInfo.exists()) {
278 emit nameResolved(fileInfo.filePath(), resolvedInfo.fileName());
279 }
280 }
281 return info;
282}
283
284QString QFileInfoGatherer::translateDriveName(const QFileInfo &drive) const
285{
286 QString driveName = drive.absoluteFilePath();
287#if (defined(Q_OS_WIN) && !defined(Q_OS_WINCE)) || defined(Q_OS_OS2)
288 if (driveName.startsWith(QLatin1Char('/'))) // UNC host
289 return drive.fileName();
290 if (driveName.endsWith(QLatin1Char('/')))
291 driveName.chop(1);
292#endif
293 return driveName;
294}
295
296/*
297 Get specific file info's, batch the files so update when we have 100
298 items and every 200ms after that
299 */
300void QFileInfoGatherer::getFileInfos(const QString &path, const QStringList &files)
301{
302#ifndef QT_NO_FILESYSTEMWATCHER
303 if (files.isEmpty()
304 && !watcher->directories().contains(path)
305 && !path.isEmpty()
306 && !path.startsWith(QLatin1String("//")) /*don't watch UNC path*/) {
307 watcher->addPath(path);
308 }
309#endif
310
311 // List drives
312 if (path.isEmpty()) {
313#if defined Q_AUTOTEST_EXPORT
314 fetchedRoot = true;
315#endif
316 QFileInfoList infoList;
317 if (files.isEmpty()) {
318 infoList = QDir::drives();
319 } else {
320 for (int i = 0; i < files.count(); ++i)
321 infoList << QFileInfo(files.at(i));
322 }
323 for (int i = infoList.count() - 1; i >= 0; --i) {
324 QString driveName = translateDriveName(infoList.at(i));
325 QList<QPair<QString,QFileInfo> > updatedFiles;
326 updatedFiles.append(QPair<QString,QFileInfo>(driveName, infoList.at(i)));
327 emit updates(path, updatedFiles);
328 }
329 return;
330 }
331
332 QTime base = QTime::currentTime();
333 QFileInfo fileInfo;
334 bool firstTime = true;
335 QList<QPair<QString, QFileInfo> > updatedFiles;
336 QStringList filesToCheck = files;
337
338 QString itPath = QDir::fromNativeSeparators(files.isEmpty() ? path : QLatin1String(""));
339 QDirIterator dirIt(itPath, QDir::AllEntries | QDir::System | QDir::Hidden);
340 QStringList allFiles;
341 while(!abort && dirIt.hasNext()) {
342 dirIt.next();
343 fileInfo = dirIt.fileInfo();
344 allFiles.append(fileInfo.fileName());
345 fetch(fileInfo, base, firstTime, updatedFiles, path);
346 }
347 if (!allFiles.isEmpty())
348 emit newListOfFiles(path, allFiles);
349
350 QStringList::const_iterator filesIt = filesToCheck.constBegin();
351 while(!abort && filesIt != filesToCheck.constEnd()) {
352 fileInfo.setFile(path + QDir::separator() + *filesIt);
353 ++filesIt;
354 fetch(fileInfo, base, firstTime, updatedFiles, path);
355 }
356 if (!updatedFiles.isEmpty())
357 emit updates(path, updatedFiles);
358}
359
360void QFileInfoGatherer::fetch(const QFileInfo &fileInfo, QTime &base, bool &firstTime, QList<QPair<QString, QFileInfo> > &updatedFiles, const QString &path) {
361 updatedFiles.append(QPair<QString, QFileInfo>(fileInfo.fileName(), fileInfo));
362 QTime current = QTime::currentTime();
363 if ((firstTime && updatedFiles.count() > 100) || base.msecsTo(current) > 1000) {
364 emit updates(path, updatedFiles);
365 updatedFiles.clear();
366 base = current;
367 firstTime = false;
368 }
369}
370
371#endif // QT_NO_FILESYSTEMMODEL
372
373QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.