diff options
Diffstat (limited to 'src/gui/itemmodels/qfileinfogatherer_p.h')
-rw-r--r-- | src/gui/itemmodels/qfileinfogatherer_p.h | 230 |
1 files changed, 230 insertions, 0 deletions
diff --git a/src/gui/itemmodels/qfileinfogatherer_p.h b/src/gui/itemmodels/qfileinfogatherer_p.h new file mode 100644 index 0000000000..374331a588 --- /dev/null +++ b/src/gui/itemmodels/qfileinfogatherer_p.h @@ -0,0 +1,230 @@ +/**************************************************************************** +** +** 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 <QtGui/private/qtguiglobal_p.h> + +#include <qthread.h> +#include <qmutex.h> +#include <qwaitcondition.h> +#if QT_CONFIG(filesystemwatcher) +#include <qfilesystemwatcher.h> +#endif +#include <qabstractfileiconprovider.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() + && lastModified() == fileInfo.lastModified(); + } + +#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 = -1; + if (type() == QExtendedInformation::Dir) + size = 0; + if (type() == QExtendedInformation::File) + size = mFileInfo.size(); + if (!mFileInfo.exists() && !mFileInfo.isSymLink()) + size = -1; + return size; + } + + QString displayType; + QIcon icon; + +private : + QFileInfo mFileInfo; +}; + +class QFileIconProvider; + +class Q_GUI_EXPORT QFileInfoGatherer : public QThread +{ +Q_OBJECT + +Q_SIGNALS: + void updates(const QString &directory, const QList<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 = nullptr); + ~QFileInfoGatherer(); + + QStringList watchedFiles() const; + QStringList watchedDirectories() const; + void watchPaths(const QStringList &paths); + void unwatchPaths(const QStringList &paths); + + bool isWatching() const; + void setWatching(bool v); + + // only callable from this->thread(): + void clear(); + void removePath(const QString &path); + QExtendedInformation getInfo(const QFileInfo &info) const; + QAbstractFileIconProvider *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(QAbstractFileIconProvider *provider); + +private Q_SLOTS: + void driveAdded(); + void driveRemoved(); + +private: + void run() override; + // called by run(): + void getFileInfos(const QString &path, const QStringList &files); + void fetch(const QFileInfo &info, QElapsedTimer &base, bool &firstTime, + QList<QPair<QString, QFileInfo>> &updatedFiles, const QString &path); + +private: + void createWatcher(); + + mutable QMutex mutex; + // begin protected by mutex + QWaitCondition condition; + QStack<QString> path; + QStack<QStringList> files; + // end protected by mutex + QAtomicInt abort; + +#if QT_CONFIG(filesystemwatcher) + QFileSystemWatcher *m_watcher = nullptr; +#endif + QAbstractFileIconProvider *m_iconProvider; // not accessed by run() + QAbstractFileIconProvider defaultProvider; +#ifdef Q_OS_WIN + bool m_resolveSymlinks = true; // not accessed by run() +#endif +#if QT_CONFIG(filesystemwatcher) + bool m_watching = true; +#endif +}; + +QT_END_NAMESPACE +#endif // QFILEINFOGATHERER_H |