| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QFILEINFOGATHERER_H |
| 5 | #define QFILEINFOGATHERER_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtGui/private/qtguiglobal_p.h> |
| 19 | |
| 20 | #include <qthread.h> |
| 21 | #include <qmutex.h> |
| 22 | #include <qwaitcondition.h> |
| 23 | #if QT_CONFIG(filesystemwatcher) |
| 24 | #include <qfilesystemwatcher.h> |
| 25 | #endif |
| 26 | #include <qabstractfileiconprovider.h> |
| 27 | #include <qstack.h> |
| 28 | #include <qdatetime.h> |
| 29 | #include <qdir.h> |
| 30 | #include <qelapsedtimer.h> |
| 31 | |
| 32 | #include <private/qfileinfo_p.h> |
| 33 | #include <private/qfilesystemengine_p.h> |
| 34 | |
| 35 | #include <utility> |
| 36 | |
| 37 | QT_REQUIRE_CONFIG(filesystemmodel); |
| 38 | |
| 39 | QT_BEGIN_NAMESPACE |
| 40 | |
| 41 | class QExtendedInformation { |
| 42 | public: |
| 43 | enum Type { Dir, File, System }; |
| 44 | |
| 45 | QExtendedInformation() {} |
| 46 | QExtendedInformation(const QFileInfo &info) : mFileInfo(info) {} |
| 47 | |
| 48 | inline bool isDir() { return type() == Dir; } |
| 49 | inline bool isFile() { return type() == File; } |
| 50 | inline bool isSystem() { return type() == System; } |
| 51 | |
| 52 | bool operator ==(const QExtendedInformation &fileInfo) const { |
| 53 | return mFileInfo == fileInfo.mFileInfo |
| 54 | && displayType == fileInfo.displayType |
| 55 | && permissions() == fileInfo.permissions() |
| 56 | && lastModified(tz: QTimeZone::UTC) == fileInfo.lastModified(tz: QTimeZone::UTC); |
| 57 | } |
| 58 | |
| 59 | #ifndef QT_NO_FSFILEENGINE |
| 60 | bool isCaseSensitive() const { |
| 61 | auto *fiPriv = QFileInfoPrivate::get(fi: const_cast<QFileInfo*>(&mFileInfo)); |
| 62 | return qt_isCaseSensitive(entry: fiPriv->fileEntry, data&: fiPriv->metaData); |
| 63 | } |
| 64 | #endif |
| 65 | |
| 66 | QFile::Permissions permissions() const { |
| 67 | return mFileInfo.permissions(); |
| 68 | } |
| 69 | |
| 70 | Type type() const { |
| 71 | if (mFileInfo.isDir()) { |
| 72 | return QExtendedInformation::Dir; |
| 73 | } |
| 74 | if (mFileInfo.isFile()) { |
| 75 | return QExtendedInformation::File; |
| 76 | } |
| 77 | if (!mFileInfo.exists() && mFileInfo.isSymLink()) { |
| 78 | return QExtendedInformation::System; |
| 79 | } |
| 80 | return QExtendedInformation::System; |
| 81 | } |
| 82 | |
| 83 | bool isSymLink(bool ignoreNtfsSymLinks = false) const |
| 84 | { |
| 85 | if (ignoreNtfsSymLinks) { |
| 86 | #ifdef Q_OS_WIN |
| 87 | return !mFileInfo.suffix().compare(QLatin1StringView("lnk" ), Qt::CaseInsensitive); |
| 88 | #endif |
| 89 | } |
| 90 | return mFileInfo.isSymLink(); |
| 91 | } |
| 92 | |
| 93 | bool isHidden() const { |
| 94 | return mFileInfo.isHidden(); |
| 95 | } |
| 96 | |
| 97 | QFileInfo fileInfo() const { |
| 98 | return mFileInfo; |
| 99 | } |
| 100 | |
| 101 | QDateTime lastModified(const QTimeZone &tz) const { |
| 102 | return mFileInfo.lastModified(tz); |
| 103 | } |
| 104 | |
| 105 | qint64 size() const { |
| 106 | qint64 size = -1; |
| 107 | if (type() == QExtendedInformation::Dir) |
| 108 | size = 0; |
| 109 | if (type() == QExtendedInformation::File) |
| 110 | size = mFileInfo.size(); |
| 111 | if (!mFileInfo.exists() && !mFileInfo.isSymLink()) |
| 112 | size = -1; |
| 113 | return size; |
| 114 | } |
| 115 | |
| 116 | QString displayType; |
| 117 | QIcon icon; |
| 118 | |
| 119 | private : |
| 120 | QFileInfo mFileInfo; |
| 121 | }; |
| 122 | |
| 123 | class QFileIconProvider; |
| 124 | |
| 125 | class Q_GUI_EXPORT QFileInfoGatherer : public QThread |
| 126 | { |
| 127 | Q_OBJECT |
| 128 | |
| 129 | Q_SIGNALS: |
| 130 | void updates(const QString &directory, const QList<std::pair<QString, QFileInfo>> &updates); |
| 131 | void newListOfFiles(const QString &directory, const QStringList &listOfFiles) const; |
| 132 | void nameResolved(const QString &fileName, const QString &resolvedName) const; |
| 133 | void directoryLoaded(const QString &path); |
| 134 | |
| 135 | public: |
| 136 | explicit QFileInfoGatherer(QObject *parent = nullptr); |
| 137 | ~QFileInfoGatherer(); |
| 138 | |
| 139 | QStringList watchedFiles() const; |
| 140 | QStringList watchedDirectories() const; |
| 141 | void watchPaths(const QStringList &paths); |
| 142 | void unwatchPaths(const QStringList &paths); |
| 143 | |
| 144 | bool isWatching() const; |
| 145 | void setWatching(bool v); |
| 146 | |
| 147 | // only callable from this->thread(): |
| 148 | void clear(); |
| 149 | void removePath(const QString &path); |
| 150 | QExtendedInformation getInfo(const QFileInfo &info) const; |
| 151 | QAbstractFileIconProvider *iconProvider() const; |
| 152 | bool resolveSymlinks() const; |
| 153 | |
| 154 | void requestAbort(); |
| 155 | |
| 156 | public Q_SLOTS: |
| 157 | void list(const QString &directoryPath); |
| 158 | void fetchExtendedInformation(const QString &path, const QStringList &files); |
| 159 | void updateFile(const QString &path); |
| 160 | void setResolveSymlinks(bool enable); |
| 161 | void setIconProvider(QAbstractFileIconProvider *provider); |
| 162 | |
| 163 | private Q_SLOTS: |
| 164 | void driveAdded(); |
| 165 | void driveRemoved(); |
| 166 | |
| 167 | protected: |
| 168 | bool event(QEvent *event) override; |
| 169 | |
| 170 | private: |
| 171 | void run() override; |
| 172 | // called by run(): |
| 173 | void getFileInfos(const QString &path, const QStringList &files); |
| 174 | void fetch(const QFileInfo &info, QElapsedTimer &base, bool &firstTime, |
| 175 | QList<std::pair<QString, QFileInfo>> &updatedFiles, const QString &path); |
| 176 | |
| 177 | private: |
| 178 | void createWatcher(); |
| 179 | |
| 180 | mutable QMutex mutex; |
| 181 | // begin protected by mutex |
| 182 | QWaitCondition condition; |
| 183 | QStack<QString> path; |
| 184 | QStack<QStringList> files; |
| 185 | // end protected by mutex |
| 186 | |
| 187 | #if QT_CONFIG(filesystemwatcher) |
| 188 | QFileSystemWatcher *m_watcher = nullptr; |
| 189 | #endif |
| 190 | QAbstractFileIconProvider *m_iconProvider; // not accessed by run() |
| 191 | QAbstractFileIconProvider defaultProvider; |
| 192 | #ifdef Q_OS_WIN |
| 193 | bool m_resolveSymlinks = true; // not accessed by run() |
| 194 | #endif |
| 195 | #if QT_CONFIG(filesystemwatcher) |
| 196 | bool m_watching = true; |
| 197 | #endif |
| 198 | }; |
| 199 | |
| 200 | QT_END_NAMESPACE |
| 201 | #endif // QFILEINFOGATHERER_H |
| 202 | |