[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Lesser General Public License Usage
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 20 | ** packaging of this file. Please review the following information to
|
---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 23 | **
|
---|
[561] | 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
[2] | 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 | **
|
---|
[561] | 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
[2] | 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
|
---|
[561] | 47 | # include <unistd.h>
|
---|
| 48 | # include <sys/types.h>
|
---|
[2] | 49 | #endif
|
---|
[561] | 50 | #if defined(Q_OS_VXWORKS)
|
---|
| 51 | # include "qplatformdefs.h"
|
---|
| 52 | #endif
|
---|
[2] | 53 |
|
---|
| 54 | QT_BEGIN_NAMESPACE
|
---|
| 55 |
|
---|
| 56 | #ifndef QT_NO_FILESYSTEMMODEL
|
---|
| 57 |
|
---|
[846] | 58 | #ifdef QT_BUILD_INTERNAL
|
---|
| 59 | static bool fetchedRoot = false;
|
---|
| 60 | Q_AUTOTEST_EXPORT void qt_test_resetFetchedRoot()
|
---|
| 61 | {
|
---|
| 62 | fetchedRoot = false;
|
---|
| 63 | }
|
---|
[2] | 64 |
|
---|
[846] | 65 | Q_AUTOTEST_EXPORT bool qt_test_isFetchedRoot()
|
---|
| 66 | {
|
---|
| 67 | return fetchedRoot;
|
---|
| 68 | }
|
---|
| 69 | #endif
|
---|
| 70 |
|
---|
[2] | 71 | /*!
|
---|
| 72 | Creates thread
|
---|
| 73 | */
|
---|
| 74 | QFileInfoGatherer::QFileInfoGatherer(QObject *parent)
|
---|
| 75 | : QThread(parent), abort(false),
|
---|
| 76 | #ifndef QT_NO_FILESYSTEMWATCHER
|
---|
| 77 | watcher(0),
|
---|
| 78 | #endif
|
---|
| 79 | m_resolveSymlinks(false), m_iconProvider(&defaultProvider)
|
---|
| 80 | {
|
---|
| 81 | #ifndef Q_OS_WIN
|
---|
| 82 | userId = getuid();
|
---|
| 83 | groupId = getgid();
|
---|
| 84 | #else
|
---|
| 85 | m_resolveSymlinks = true;
|
---|
| 86 | #endif
|
---|
| 87 | #ifndef QT_NO_FILESYSTEMWATCHER
|
---|
| 88 | watcher = new QFileSystemWatcher(this);
|
---|
[561] | 89 | connect(watcher, SIGNAL(directoryChanged(QString)), this, SLOT(list(QString)));
|
---|
| 90 | connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(updateFile(QString)));
|
---|
[2] | 91 | #endif
|
---|
| 92 | start(LowPriority);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /*!
|
---|
| 96 | Destroys thread
|
---|
| 97 | */
|
---|
| 98 | QFileInfoGatherer::~QFileInfoGatherer()
|
---|
| 99 | {
|
---|
[561] | 100 | QMutexLocker locker(&mutex);
|
---|
[2] | 101 | abort = true;
|
---|
| 102 | condition.wakeOne();
|
---|
[561] | 103 | locker.unlock();
|
---|
[2] | 104 | wait();
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | void QFileInfoGatherer::setResolveSymlinks(bool enable)
|
---|
| 108 | {
|
---|
| 109 | Q_UNUSED(enable);
|
---|
| 110 | #ifdef Q_OS_WIN
|
---|
[561] | 111 | QMutexLocker locker(&mutex);
|
---|
[2] | 112 | m_resolveSymlinks = enable;
|
---|
| 113 | #endif
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | bool QFileInfoGatherer::resolveSymlinks() const
|
---|
| 117 | {
|
---|
| 118 | return m_resolveSymlinks;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | void QFileInfoGatherer::setIconProvider(QFileIconProvider *provider)
|
---|
| 122 | {
|
---|
[561] | 123 | QMutexLocker locker(&mutex);
|
---|
[2] | 124 | m_iconProvider = provider;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | QFileIconProvider *QFileInfoGatherer::iconProvider() const
|
---|
| 128 | {
|
---|
| 129 | return m_iconProvider;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /*!
|
---|
| 133 | Fetch extended information for all \a files in \a path
|
---|
| 134 |
|
---|
| 135 | \sa updateFile(), update(), resolvedName()
|
---|
| 136 | */
|
---|
| 137 | void QFileInfoGatherer::fetchExtendedInformation(const QString &path, const QStringList &files)
|
---|
| 138 | {
|
---|
[561] | 139 | QMutexLocker locker(&mutex);
|
---|
[2] | 140 | // See if we already have this dir/file in our que
|
---|
| 141 | int loc = this->path.lastIndexOf(path);
|
---|
| 142 | while (loc > 0) {
|
---|
| 143 | if (this->files.at(loc) == files) {
|
---|
| 144 | return;
|
---|
| 145 | }
|
---|
| 146 | loc = this->path.lastIndexOf(path, loc - 1);
|
---|
| 147 | }
|
---|
| 148 | this->path.push(path);
|
---|
| 149 | this->files.push(files);
|
---|
| 150 | condition.wakeAll();
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | /*!
|
---|
| 154 | Fetch extended information for all \a filePath
|
---|
| 155 |
|
---|
| 156 | \sa fetchExtendedInformation()
|
---|
| 157 | */
|
---|
| 158 | void QFileInfoGatherer::updateFile(const QString &filePath)
|
---|
| 159 | {
|
---|
| 160 | QString dir = filePath.mid(0, filePath.lastIndexOf(QDir::separator()));
|
---|
| 161 | QString fileName = filePath.mid(dir.length() + 1);
|
---|
| 162 | fetchExtendedInformation(dir, QStringList(fileName));
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | /*
|
---|
| 166 | List all files in \a directoryPath
|
---|
| 167 |
|
---|
| 168 | \sa listed()
|
---|
| 169 | */
|
---|
| 170 | void QFileInfoGatherer::clear()
|
---|
| 171 | {
|
---|
| 172 | #ifndef QT_NO_FILESYSTEMWATCHER
|
---|
[561] | 173 | QMutexLocker locker(&mutex);
|
---|
[2] | 174 | watcher->removePaths(watcher->files());
|
---|
| 175 | watcher->removePaths(watcher->directories());
|
---|
| 176 | #endif
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | /*
|
---|
| 180 | Remove a \a path from the watcher
|
---|
| 181 |
|
---|
| 182 | \sa listed()
|
---|
| 183 | */
|
---|
| 184 | void QFileInfoGatherer::removePath(const QString &path)
|
---|
| 185 | {
|
---|
| 186 | #ifndef QT_NO_FILESYSTEMWATCHER
|
---|
[561] | 187 | QMutexLocker locker(&mutex);
|
---|
[2] | 188 | watcher->removePath(path);
|
---|
| 189 | #endif
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | /*
|
---|
| 193 | List all files in \a directoryPath
|
---|
| 194 |
|
---|
| 195 | \sa listed()
|
---|
| 196 | */
|
---|
| 197 | void QFileInfoGatherer::list(const QString &directoryPath)
|
---|
| 198 | {
|
---|
| 199 | fetchExtendedInformation(directoryPath, QStringList());
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | /*
|
---|
| 203 | Until aborted wait to fetch a directory or files
|
---|
| 204 | */
|
---|
| 205 | void QFileInfoGatherer::run()
|
---|
| 206 | {
|
---|
| 207 | forever {
|
---|
| 208 | bool updateFiles = false;
|
---|
[561] | 209 | QMutexLocker locker(&mutex);
|
---|
[2] | 210 | if (abort) {
|
---|
| 211 | return;
|
---|
| 212 | }
|
---|
| 213 | if (this->path.isEmpty())
|
---|
| 214 | condition.wait(&mutex);
|
---|
| 215 | QString path;
|
---|
| 216 | QStringList list;
|
---|
| 217 | if (!this->path.isEmpty()) {
|
---|
| 218 | path = this->path.first();
|
---|
| 219 | list = this->files.first();
|
---|
| 220 | this->path.pop_front();
|
---|
| 221 | this->files.pop_front();
|
---|
| 222 | updateFiles = true;
|
---|
| 223 | }
|
---|
[561] | 224 | locker.unlock();
|
---|
| 225 | if (updateFiles)
|
---|
| 226 | getFileInfos(path, list);
|
---|
[2] | 227 | }
|
---|
| 228 | }
|
---|
| 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 | // ### Not ready to listen all modifications
|
---|
| 237 | #if 0
|
---|
| 238 | // Enable the next two commented out lines to get updates when the file sizes change...
|
---|
| 239 | if (!fileInfo.exists() && !fileInfo.isSymLink()) {
|
---|
| 240 | info.size = -1;
|
---|
| 241 | //watcher->removePath(fileInfo.absoluteFilePath());
|
---|
| 242 | } else {
|
---|
| 243 | if (!fileInfo.absoluteFilePath().isEmpty() && fileInfo.exists() && fileInfo.isReadable()
|
---|
| 244 | && !watcher->files().contains(fileInfo.absoluteFilePath())) {
|
---|
| 245 | //watcher->addPath(fileInfo.absoluteFilePath());
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 | #endif
|
---|
| 249 | #endif
|
---|
| 250 |
|
---|
| 251 | if (fileInfo.isSymLink() && m_resolveSymlinks) {
|
---|
| 252 | QFileInfo resolvedInfo(fileInfo.symLinkTarget());
|
---|
| 253 | resolvedInfo = resolvedInfo.canonicalFilePath();
|
---|
| 254 | if (resolvedInfo.exists()) {
|
---|
| 255 | emit nameResolved(fileInfo.filePath(), resolvedInfo.fileName());
|
---|
| 256 | }
|
---|
| 257 | }
|
---|
| 258 | return info;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | QString QFileInfoGatherer::translateDriveName(const QFileInfo &drive) const
|
---|
| 262 | {
|
---|
| 263 | QString driveName = drive.absoluteFilePath();
|
---|
[172] | 264 | #if (defined(Q_OS_WIN) && !defined(Q_OS_WINCE)) || defined(Q_OS_OS2)
|
---|
[2] | 265 | if (driveName.startsWith(QLatin1Char('/'))) // UNC host
|
---|
| 266 | return drive.fileName();
|
---|
[561] | 267 | #endif
|
---|
[606] | 268 | #if (defined(Q_OS_WIN) && !defined(Q_OS_WINCE)) || defined(Q_OS_SYMBIAN) || defined(Q_OS_OS2)
|
---|
[2] | 269 | if (driveName.endsWith(QLatin1Char('/')))
|
---|
| 270 | driveName.chop(1);
|
---|
| 271 | #endif
|
---|
| 272 | return driveName;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | /*
|
---|
| 276 | Get specific file info's, batch the files so update when we have 100
|
---|
| 277 | items and every 200ms after that
|
---|
| 278 | */
|
---|
| 279 | void QFileInfoGatherer::getFileInfos(const QString &path, const QStringList &files)
|
---|
| 280 | {
|
---|
| 281 | #ifndef QT_NO_FILESYSTEMWATCHER
|
---|
| 282 | if (files.isEmpty()
|
---|
| 283 | && !watcher->directories().contains(path)
|
---|
| 284 | && !path.isEmpty()
|
---|
| 285 | && !path.startsWith(QLatin1String("//")) /*don't watch UNC path*/) {
|
---|
| 286 | watcher->addPath(path);
|
---|
| 287 | }
|
---|
| 288 | #endif
|
---|
| 289 |
|
---|
| 290 | // List drives
|
---|
| 291 | if (path.isEmpty()) {
|
---|
[846] | 292 | #ifdef QT_BUILD_INTERNAL
|
---|
[2] | 293 | fetchedRoot = true;
|
---|
| 294 | #endif
|
---|
| 295 | QFileInfoList infoList;
|
---|
| 296 | if (files.isEmpty()) {
|
---|
| 297 | infoList = QDir::drives();
|
---|
| 298 | } else {
|
---|
| 299 | for (int i = 0; i < files.count(); ++i)
|
---|
| 300 | infoList << QFileInfo(files.at(i));
|
---|
| 301 | }
|
---|
| 302 | for (int i = infoList.count() - 1; i >= 0; --i) {
|
---|
| 303 | QString driveName = translateDriveName(infoList.at(i));
|
---|
| 304 | QList<QPair<QString,QFileInfo> > updatedFiles;
|
---|
| 305 | updatedFiles.append(QPair<QString,QFileInfo>(driveName, infoList.at(i)));
|
---|
| 306 | emit updates(path, updatedFiles);
|
---|
| 307 | }
|
---|
| 308 | return;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
[846] | 311 | QElapsedTimer base;
|
---|
| 312 | base.start();
|
---|
[2] | 313 | QFileInfo fileInfo;
|
---|
| 314 | bool firstTime = true;
|
---|
| 315 | QList<QPair<QString, QFileInfo> > updatedFiles;
|
---|
| |
---|