这篇文章中,探索Qt中的类qfileinfogatherer类,先给出私有类头文件。我们先想一想要形成一个信息采集者,需要什么?需要一个线程,当文件信息发生变化的时候,作为一个槽来接收信号。

  先预备一些小知识,关于QT_REQUIRE_CONFIG,

 /****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtWidgets module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/ #ifndef QFILEINFOGATHERER_H
#define QFILEINFOGATHERER_H //
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
// #include <QtWidgets/private/qtwidgetsglobal_p.h> #include <qthread.h>
#include <qmutex.h>
#include <qwaitcondition.h>
#include <qfilesystemwatcher.h>
#include <qfileiconprovider.h>
#include <qpair.h>
#include <qstack.h>
#include <qdatetime.h>
#include <qdir.h>
#include <qelapsedtimer.h> #include <private/qfilesystemengine_p.h> QT_REQUIRE_CONFIG(filesystemmodel); QT_BEGIN_NAMESPACE class QExtendedInformation {
public:
enum Type { Dir, File, System }; QExtendedInformation() {}
QExtendedInformation(const QFileInfo &info) : mFileInfo(info) {} inline bool isDir() { return type() == Dir; }
inline bool isFile() { return type() == File; }
inline bool isSystem() { return type() == System; } bool operator ==(const QExtendedInformation &fileInfo) const {
return mFileInfo == fileInfo.mFileInfo
&& displayType == fileInfo.displayType
&& permissions() == fileInfo.permissions();
} #ifndef QT_NO_FSFILEENGINE
bool isCaseSensitive() const {
return QFileSystemEngine::isCaseSensitive();
}
#endif QFile::Permissions permissions() const {
return mFileInfo.permissions();
} Type type() const {
if (mFileInfo.isDir()) {
return QExtendedInformation::Dir;
}
if (mFileInfo.isFile()) {
return QExtendedInformation::File;
}
if (!mFileInfo.exists() && mFileInfo.isSymLink()) {
return QExtendedInformation::System;
}
return QExtendedInformation::System;
} bool isSymLink(bool ignoreNtfsSymLinks = false) const
{
if (ignoreNtfsSymLinks) {
#ifdef Q_OS_WIN
return !mFileInfo.suffix().compare(QLatin1String("lnk"), Qt::CaseInsensitive);
#endif
}
return mFileInfo.isSymLink();
} bool isHidden() const {
return mFileInfo.isHidden();
} QFileInfo fileInfo() const {
return mFileInfo;
} QDateTime lastModified() const {
return mFileInfo.lastModified();
} qint64 size() const {
qint64 size = -;
if (type() == QExtendedInformation::Dir)
size = ;
if (type() == QExtendedInformation::File)
size = mFileInfo.size();
if (!mFileInfo.exists() && !mFileInfo.isSymLink())
size = -;
return size;
} QString displayType;
QIcon icon; private :
QFileInfo mFileInfo;
}; class QFileIconProvider; class Q_AUTOTEST_EXPORT QFileInfoGatherer : public QThread
{
Q_OBJECT Q_SIGNALS:
void updates(const QString &directory, const QVector<QPair<QString, QFileInfo> > &updates);
void newListOfFiles(const QString &directory, const QStringList &listOfFiles) const;
void nameResolved(const QString &fileName, const QString &resolvedName) const;
void directoryLoaded(const QString &path); public:
explicit QFileInfoGatherer(QObject *parent = );
~QFileInfoGatherer(); // only callable from this->thread():
void clear();
void removePath(const QString &path);
QExtendedInformation getInfo(const QFileInfo &info) const;
QFileIconProvider *iconProvider() const;
bool resolveSymlinks() const; public Q_SLOTS:
void list(const QString &directoryPath);
void fetchExtendedInformation(const QString &path, const QStringList &files);
void updateFile(const QString &path);
void setResolveSymlinks(bool enable);
void setIconProvider(QFileIconProvider *provider); private Q_SLOTS:
void driveAdded();
void driveRemoved(); private:
void run() Q_DECL_OVERRIDE;
// called by run():
void getFileInfos(const QString &path, const QStringList &files);
void fetch(const QFileInfo &info, QElapsedTimer &base, bool &firstTime, QVector<QPair<QString, QFileInfo> > &updatedFiles, const QString &path); private:
mutable QMutex mutex;
// begin protected by mutex
QWaitCondition condition;
QStack<QString> path;
QStack<QStringList> files;
// end protected by mutex
QAtomicInt abort; #ifndef QT_NO_FILESYSTEMWATCHER
QFileSystemWatcher *watcher;
#endif
#ifdef Q_OS_WIN
bool m_resolveSymlinks; // not accessed by run()
#endif
QFileIconProvider *m_iconProvider; // not accessed by run()
QFileIconProvider defaultProvider;
}; QT_END_NAMESPACE
#endif // QFILEINFOGATHERER_H

  接下来是它所对应的cpp文件:

 /****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtWidgets module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/ #include "qfileinfogatherer_p.h"
#include <qdebug.h>
#include <qdiriterator.h>
#ifndef Q_OS_WIN
# include <unistd.h>
# include <sys/types.h>
#endif
#if defined(Q_OS_VXWORKS)
# include "qplatformdefs.h"
#endif QT_BEGIN_NAMESPACE #ifdef QT_BUILD_INTERNAL
static QBasicAtomicInt fetchedRoot = Q_BASIC_ATOMIC_INITIALIZER(false);
Q_AUTOTEST_EXPORT void qt_test_resetFetchedRoot()
{
fetchedRoot.store(false);
} Q_AUTOTEST_EXPORT bool qt_test_isFetchedRoot()
{
return fetchedRoot.load();
}
#endif static QString translateDriveName(const QFileInfo &drive)
{
QString driveName = drive.absoluteFilePath();
#ifdef Q_OS_WIN
if (driveName.startsWith(QLatin1Char('/'))) // UNC host
return drive.fileName();
if (driveName.endsWith(QLatin1Char('/')))
driveName.chop();
#endif // Q_OS_WIN
return driveName;
} /*!
Creates thread
*/
QFileInfoGatherer::QFileInfoGatherer(QObject *parent)
: QThread(parent), abort(false),
#ifndef QT_NO_FILESYSTEMWATCHER
watcher(),
#endif
#ifdef Q_OS_WIN
m_resolveSymlinks(true),
#endif
m_iconProvider(&defaultProvider)
{
#ifndef QT_NO_FILESYSTEMWATCHER
watcher = new QFileSystemWatcher(this);
connect(watcher, SIGNAL(directoryChanged(QString)), this, SLOT(list(QString)));
connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(updateFile(QString))); # if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
const QVariant listener = watcher->property("_q_driveListener");
if (listener.canConvert<QObject *>()) {
if (QObject *driveListener = listener.value<QObject *>()) {
connect(driveListener, SIGNAL(driveAdded()), this, SLOT(driveAdded()));
connect(driveListener, SIGNAL(driveRemoved()), this, SLOT(driveRemoved()));
}
}
# endif // Q_OS_WIN && !Q_OS_WINRT
#endif
start(LowPriority);
} /*!
Destroys thread
*/
QFileInfoGatherer::~QFileInfoGatherer()
{
abort.store(true);
QMutexLocker locker(&mutex);
condition.wakeAll();
locker.unlock();
wait();
} void QFileInfoGatherer::setResolveSymlinks(bool enable)
{
Q_UNUSED(enable);
#ifdef Q_OS_WIN
m_resolveSymlinks = enable;
#endif
} void QFileInfoGatherer::driveAdded()
{
fetchExtendedInformation(QString(), QStringList());
} void QFileInfoGatherer::driveRemoved()
{
QStringList drives;
const QFileInfoList driveInfoList = QDir::drives();
for (const QFileInfo &fi : driveInfoList)
drives.append(translateDriveName(fi));
newListOfFiles(QString(), drives);
} bool QFileInfoGatherer::resolveSymlinks() const
{
#ifdef Q_OS_WIN
return m_resolveSymlinks;
#else
return false;
#endif
} void QFileInfoGatherer::setIconProvider(QFileIconProvider *provider)
{
m_iconProvider = provider;
} QFileIconProvider *QFileInfoGatherer::iconProvider() const
{
return m_iconProvider;
} /*!
Fetch extended information for all \a files in \a path \sa updateFile(), update(), resolvedName()
*/
void QFileInfoGatherer::fetchExtendedInformation(const QString &path, const QStringList &files)
{
QMutexLocker locker(&mutex);
// See if we already have this dir/file in our queue
int loc = this->path.lastIndexOf(path);
while (loc > ) {
if (this->files.at(loc) == files) {
return;
}
loc = this->path.lastIndexOf(path, loc - );
}
this->path.push(path);
this->files.push(files);
condition.wakeAll(); #ifndef QT_NO_FILESYSTEMWATCHER
if (files.isEmpty()
&& !path.isEmpty()
&& !path.startsWith(QLatin1String("//")) /*don't watch UNC path*/) {
if (!watcher->directories().contains(path))
watcher->addPath(path);
}
#endif
} /*!
Fetch extended information for all \a filePath \sa fetchExtendedInformation()
*/
void QFileInfoGatherer::updateFile(const QString &filePath)
{
QString dir = filePath.mid(, filePath.lastIndexOf(QDir::separator()));
QString fileName = filePath.mid(dir.length() + );
fetchExtendedInformation(dir, QStringList(fileName));
} /*
List all files in \a directoryPath \sa listed()
*/
void QFileInfoGatherer::clear()
{
#ifndef QT_NO_FILESYSTEMWATCHER
QMutexLocker locker(&mutex);
watcher->removePaths(watcher->files());
watcher->removePaths(watcher->directories());
#endif
} /*
Remove a \a path from the watcher \sa listed()
*/
void QFileInfoGatherer::removePath(const QString &path)
{
#ifndef QT_NO_FILESYSTEMWATCHER
QMutexLocker locker(&mutex);
watcher->removePath(path);
#else
Q_UNUSED(path);
#endif
} /*
List all files in \a directoryPath \sa listed()
*/
void QFileInfoGatherer::list(const QString &directoryPath)
{
fetchExtendedInformation(directoryPath, QStringList());
} /*
Until aborted wait to fetch a directory or files
*/
void QFileInfoGatherer::run()
{
forever {
QMutexLocker locker(&mutex);
while (!abort.load() && path.isEmpty())
condition.wait(&mutex);
if (abort.load())
return;
const QString thisPath = qAsConst(path).front();
path.pop_front();
const QStringList thisList = qAsConst(files).front();
files.pop_front();
locker.unlock(); getFileInfos(thisPath, thisList);
}
} QExtendedInformation QFileInfoGatherer::getInfo(const QFileInfo &fileInfo) const
{
QExtendedInformation info(fileInfo);
info.icon = m_iconProvider->icon(fileInfo);
info.displayType = m_iconProvider->type(fileInfo);
#ifndef QT_NO_FILESYSTEMWATCHER
// ### Not ready to listen all modifications
#if 0
// Enable the next two commented out lines to get updates when the file sizes change...
if (!fileInfo.exists() && !fileInfo.isSymLink()) {
info.size = -;
//watcher->removePath(fileInfo.absoluteFilePath());
} else {
if (!fileInfo.absoluteFilePath().isEmpty() && fileInfo.exists() && fileInfo.isReadable()
&& !watcher->files().contains(fileInfo.absoluteFilePath())) {
//watcher->addPath(fileInfo.absoluteFilePath());
}
}
#endif
#endif #ifdef Q_OS_WIN
if (m_resolveSymlinks && info.isSymLink(/* ignoreNtfsSymLinks = */ true)) {
QFileInfo resolvedInfo(fileInfo.symLinkTarget());
resolvedInfo = resolvedInfo.canonicalFilePath();
if (resolvedInfo.exists()) {
emit nameResolved(fileInfo.filePath(), resolvedInfo.fileName());
}
}
#endif
return info;
} /*
Get specific file info's, batch the files so update when we have 100
items and every 200ms after that
*/
void QFileInfoGatherer::getFileInfos(const QString &path, const QStringList &files)
{
// List drives
if (path.isEmpty()) {
#ifdef QT_BUILD_INTERNAL
fetchedRoot.store(true);
#endif
QFileInfoList infoList;
if (files.isEmpty()) {
infoList = QDir::drives();
} else {
infoList.reserve(files.count());
for (const auto &file : files)
infoList << QFileInfo(file);
}
for (int i = infoList.count() - ; i >= ; --i) {
QString driveName = translateDriveName(infoList.at(i));
QVector<QPair<QString,QFileInfo> > updatedFiles;
updatedFiles.append(QPair<QString,QFileInfo>(driveName, infoList.at(i)));
emit updates(path, updatedFiles);
}
return;
} QElapsedTimer base;
base.start();
QFileInfo fileInfo;
bool firstTime = true;
QVector<QPair<QString, QFileInfo> > updatedFiles;
QStringList filesToCheck = files; QString itPath = QDir::fromNativeSeparators(files.isEmpty() ? path : QLatin1String(""));
QDirIterator dirIt(itPath, QDir::AllEntries | QDir::System | QDir::Hidden);
QStringList allFiles;
while (!abort.load() && dirIt.hasNext()) {
dirIt.next();
fileInfo = dirIt.fileInfo();
allFiles.append(fileInfo.fileName());
fetch(fileInfo, base, firstTime, updatedFiles, path);
}
if (!allFiles.isEmpty())
emit newListOfFiles(path, allFiles); QStringList::const_iterator filesIt = filesToCheck.constBegin();
while (!abort.load() && filesIt != filesToCheck.constEnd()) {
fileInfo.setFile(path + QDir::separator() + *filesIt);
++filesIt;
fetch(fileInfo, base, firstTime, updatedFiles, path);
}
if (!updatedFiles.isEmpty())
emit updates(path, updatedFiles);
emit directoryLoaded(path);
} void QFileInfoGatherer::fetch(const QFileInfo &fileInfo, QElapsedTimer &base, bool &firstTime, QVector<QPair<QString, QFileInfo> > &updatedFiles, const QString &path) {
updatedFiles.append(QPair<QString, QFileInfo>(fileInfo.fileName(), fileInfo));
QElapsedTimer current;
current.start();
if ((firstTime && updatedFiles.count() > ) || base.msecsTo(current) > ) {
emit updates(path, updatedFiles);
updatedFiles.clear();
base = current;
firstTime = false;
}
} QT_END_NAMESPACE #include "moc_qfileinfogatherer_p.cpp"

最新文章

  1. 《Node即学即用》—— 读后总结
  2. &lt;转&gt;DevExpress使用经验总结
  3. php常用函数(不定时更新)
  4. Swagger 增加 DocumentFilter 隐藏不需要显示的接口
  5. Swift - 文本输入框(UITextField)
  6. Swift - 让程序挂起后,能在后台继续运行任务
  7. Shell脚本学习入门(一)
  8. kaili 2.0 开启ssh远程
  9. [译]一个灵活的 Trello 敏捷工作流
  10. LINQ对List列表随机排序,取N条数据
  11. vs2005中的WebBrowser控件的简单应用
  12. java操作DBF的使用
  13. 如何在GitHub上生成ssh公钥并用NetBeans克隆项目
  14. uva10603 倒水问题
  15. day 6 - 1 知识点小节
  16. HTTP 各状态码大全
  17. 12种不宜使用的javascript的语法
  18. python 时间模块 -- time
  19. fun() 的 拆分和 for 遍历 的结合---------&gt; 函数容器
  20. SQL 上线平台(内含全部完整资料)

热门文章

  1. CDH 报错:UnicodeEncodeError: &#39;ascii&#39; codec can&#39;t encode characters in position 0-11: ordinal not in range
  2. python - xml转excel
  3. Python实现:汉诺塔问题
  4. winfrom导出DataGridView为Excel方法
  5. js 获取昨天,今天,本周,上周,季度等时间范围(封装的js)
  6. x变成y的最少操作次数(层次遍历)
  7. 学习node.js的一些笔记
  8. MySQL—增删改查,分组,连表,limit,union,alter,排序,去重
  9. pycharm与python环境配置
  10. python 模块 不可不知的知识点