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 QFILESYSTEMMODEL_P_H |
5 | #define QFILESYSTEMMODEL_P_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 | #include "qfilesystemmodel.h" |
20 | |
21 | #include <private/qabstractitemmodel_p.h> |
22 | #include <qabstractitemmodel.h> |
23 | #include "qfileinfogatherer_p.h" |
24 | #include <qpair.h> |
25 | #include <qdir.h> |
26 | #include <qicon.h> |
27 | #include <qfileinfo.h> |
28 | #include <qtimer.h> |
29 | #include <qhash.h> |
30 | |
31 | #include <vector> |
32 | |
33 | QT_REQUIRE_CONFIG(filesystemmodel); |
34 | |
35 | QT_BEGIN_NAMESPACE |
36 | |
37 | class ExtendedInformation; |
38 | class QFileSystemModelPrivate; |
39 | class QFileIconProvider; |
40 | |
41 | #if defined(Q_OS_WIN) |
42 | class QFileSystemModelNodePathKey : public QString |
43 | { |
44 | public: |
45 | QFileSystemModelNodePathKey() {} |
46 | QFileSystemModelNodePathKey(const QString &other) : QString(other) {} |
47 | QFileSystemModelNodePathKey(const QFileSystemModelNodePathKey &other) : QString(other) {} |
48 | bool operator==(const QFileSystemModelNodePathKey &other) const { return !compare(other, Qt::CaseInsensitive); } |
49 | }; |
50 | |
51 | Q_DECLARE_TYPEINFO(QFileSystemModelNodePathKey, Q_RELOCATABLE_TYPE); |
52 | |
53 | inline size_t qHash(const QFileSystemModelNodePathKey &key, size_t seed = 0) |
54 | { |
55 | return qHash(key.toCaseFolded(), seed); |
56 | } |
57 | #else // Q_OS_WIN |
58 | typedef QString QFileSystemModelNodePathKey; |
59 | #endif |
60 | |
61 | class Q_GUI_EXPORT QFileSystemModelPrivate : public QAbstractItemModelPrivate |
62 | { |
63 | Q_DECLARE_PUBLIC(QFileSystemModel) |
64 | |
65 | public: |
66 | enum { |
67 | NameColumn, |
68 | SizeColumn, |
69 | TypeColumn, |
70 | TimeColumn, |
71 | NumColumns = 4 |
72 | }; |
73 | |
74 | class QFileSystemNode |
75 | { |
76 | public: |
77 | Q_DISABLE_COPY_MOVE(QFileSystemNode) |
78 | |
79 | explicit QFileSystemNode(const QString &filename = QString(), QFileSystemNode *p = nullptr) |
80 | : fileName(filename), parent(p) {} |
81 | ~QFileSystemNode() { |
82 | qDeleteAll(c: children); |
83 | delete info; |
84 | } |
85 | |
86 | QString fileName; |
87 | #if defined(Q_OS_WIN) |
88 | QString volumeName; |
89 | #endif |
90 | |
91 | inline qint64 size() const { if (info && !info->isDir()) return info->size(); return 0; } |
92 | inline QString type() const { if (info) return info->displayType; return QLatin1StringView(""); } |
93 | inline QDateTime lastModified(const QTimeZone &tz) const { return info ? info->lastModified(tz) : QDateTime(); } |
94 | inline QFile::Permissions permissions() const { if (info) return info->permissions(); return { }; } |
95 | inline bool isReadable() const { return ((permissions() & QFile::ReadUser) != 0); } |
96 | inline bool isWritable() const { return ((permissions() & QFile::WriteUser) != 0); } |
97 | inline bool isExecutable() const { return ((permissions() & QFile::ExeUser) != 0); } |
98 | inline bool isDir() const { |
99 | if (info) |
100 | return info->isDir(); |
101 | if (children.size() > 0) |
102 | return true; |
103 | return false; |
104 | } |
105 | inline QFileInfo fileInfo() const { if (info) return info->fileInfo(); return QFileInfo(); } |
106 | inline bool isFile() const { if (info) return info->isFile(); return true; } |
107 | inline bool isSystem() const { if (info) return info->isSystem(); return true; } |
108 | inline bool isHidden() const { if (info) return info->isHidden(); return false; } |
109 | inline bool isSymLink(bool ignoreNtfsSymLinks = false) const { return info && info->isSymLink(ignoreNtfsSymLinks); } |
110 | inline bool caseSensitive() const { if (info) return info->isCaseSensitive(); return false; } |
111 | inline QIcon icon() const { if (info) return info->icon; return QIcon(); } |
112 | |
113 | inline bool operator <(const QFileSystemNode &node) const { |
114 | if (caseSensitive() || node.caseSensitive()) |
115 | return fileName < node.fileName; |
116 | return QString::compare(s1: fileName, s2: node.fileName, cs: Qt::CaseInsensitive) < 0; |
117 | } |
118 | inline bool operator >(const QString &name) const { |
119 | if (caseSensitive()) |
120 | return fileName > name; |
121 | return QString::compare(s1: fileName, s2: name, cs: Qt::CaseInsensitive) > 0; |
122 | } |
123 | inline bool operator <(const QString &name) const { |
124 | if (caseSensitive()) |
125 | return fileName < name; |
126 | return QString::compare(s1: fileName, s2: name, cs: Qt::CaseInsensitive) < 0; |
127 | } |
128 | inline bool operator !=(const QExtendedInformation &fileInfo) const { |
129 | return !operator==(fileInfo); |
130 | } |
131 | bool operator ==(const QString &name) const { |
132 | if (caseSensitive()) |
133 | return fileName == name; |
134 | return QString::compare(s1: fileName, s2: name, cs: Qt::CaseInsensitive) == 0; |
135 | } |
136 | bool operator ==(const QExtendedInformation &fileInfo) const { |
137 | return info && (*info == fileInfo); |
138 | } |
139 | |
140 | inline bool hasInformation() const { return info != nullptr; } |
141 | |
142 | void populate(const QExtendedInformation &fileInfo) { |
143 | if (!info) |
144 | info = new QExtendedInformation(fileInfo.fileInfo()); |
145 | (*info) = fileInfo; |
146 | } |
147 | |
148 | // children shouldn't normally be accessed directly, use node() |
149 | inline int visibleLocation(const QString &childName) { |
150 | return visibleChildren.indexOf(str: childName); |
151 | } |
152 | void updateIcon(QAbstractFileIconProvider *iconProvider, const QString &path) { |
153 | if (!iconProvider) |
154 | return; |
155 | |
156 | if (info) |
157 | info->icon = iconProvider->icon(QFileInfo(path)); |
158 | |
159 | for (QFileSystemNode *child : std::as_const(t&: children)) { |
160 | //On windows the root (My computer) has no path so we don't want to add a / for nothing (e.g. /C:/) |
161 | if (!path.isEmpty()) { |
162 | if (path.endsWith(c: u'/')) |
163 | child->updateIcon(iconProvider, path: path + child->fileName); |
164 | else |
165 | child->updateIcon(iconProvider, path: path + u'/' + child->fileName); |
166 | } else |
167 | child->updateIcon(iconProvider, path: child->fileName); |
168 | } |
169 | } |
170 | |
171 | void retranslateStrings(QAbstractFileIconProvider *iconProvider, const QString &path) { |
172 | if (!iconProvider) |
173 | return; |
174 | |
175 | if (info) |
176 | info->displayType = iconProvider->type(QFileInfo(path)); |
177 | for (QFileSystemNode *child : std::as_const(t&: children)) { |
178 | //On windows the root (My computer) has no path so we don't want to add a / for nothing (e.g. /C:/) |
179 | if (!path.isEmpty()) { |
180 | if (path.endsWith(c: u'/')) |
181 | child->retranslateStrings(iconProvider, path: path + child->fileName); |
182 | else |
183 | child->retranslateStrings(iconProvider, path: path + u'/' + child->fileName); |
184 | } else |
185 | child->retranslateStrings(iconProvider, path: child->fileName); |
186 | } |
187 | } |
188 | |
189 | QHash<QFileSystemModelNodePathKey, QFileSystemNode *> children; |
190 | QList<QString> visibleChildren; |
191 | QExtendedInformation *info = nullptr; |
192 | QFileSystemNode *parent; |
193 | int dirtyChildrenIndex = -1; |
194 | bool populatedChildren = false; |
195 | bool isVisible = false; |
196 | }; |
197 | |
198 | QFileSystemModelPrivate(); |
199 | ~QFileSystemModelPrivate(); |
200 | void init(); |
201 | /* |
202 | \internal |
203 | |
204 | Return true if index which is owned by node is hidden by the filter. |
205 | */ |
206 | inline bool isHiddenByFilter(QFileSystemNode *indexNode, const QModelIndex &index) const |
207 | { |
208 | return (indexNode != &root && !index.isValid()); |
209 | } |
210 | QFileSystemNode *node(const QModelIndex &index) const; |
211 | QFileSystemNode *node(const QString &path, bool fetch = true) const; |
212 | inline QModelIndex index(const QString &path, int column = 0) { return index(node: node(path), column); } |
213 | QModelIndex index(const QFileSystemNode *node, int column = 0) const; |
214 | bool filtersAcceptsNode(const QFileSystemNode *node) const; |
215 | bool passNameFilters(const QFileSystemNode *node) const; |
216 | void removeNode(QFileSystemNode *parentNode, const QString &name); |
217 | QFileSystemNode* addNode(QFileSystemNode *parentNode, const QString &fileName, const QFileInfo &info); |
218 | void addVisibleFiles(QFileSystemNode *parentNode, const QStringList &newFiles); |
219 | void removeVisibleFile(QFileSystemNode *parentNode, int visibleLocation); |
220 | void sortChildren(int column, const QModelIndex &parent); |
221 | |
222 | inline int translateVisibleLocation(QFileSystemNode *parent, int row) const { |
223 | if (sortOrder != Qt::AscendingOrder) { |
224 | if (parent->dirtyChildrenIndex == -1) |
225 | return parent->visibleChildren.size() - row - 1; |
226 | |
227 | if (row < parent->dirtyChildrenIndex) |
228 | return parent->dirtyChildrenIndex - row - 1; |
229 | } |
230 | |
231 | return row; |
232 | } |
233 | |
234 | inline static QString myComputer() { |
235 | // ### TODO We should query the system to find out what the string should be |
236 | // XP == "My Computer", |
237 | // Vista == "Computer", |
238 | // OS X == "Computer" (sometime user generated) "Benjamin's PowerBook G4" |
239 | #ifdef Q_OS_WIN |
240 | return QFileSystemModel::tr("My Computer"); |
241 | #else |
242 | return QFileSystemModel::tr(s: "Computer"); |
243 | #endif |
244 | } |
245 | |
246 | inline void delayedSort() { |
247 | if (!delayedSortTimer.isActive()) |
248 | delayedSortTimer.start(msec: 0); |
249 | } |
250 | |
251 | QIcon icon(const QModelIndex &index) const; |
252 | QString name(const QModelIndex &index) const; |
253 | QString displayName(const QModelIndex &index) const; |
254 | QString filePath(const QModelIndex &index) const; |
255 | QString size(const QModelIndex &index) const; |
256 | static QString size(qint64 bytes); |
257 | QString type(const QModelIndex &index) const; |
258 | QString time(const QModelIndex &index) const; |
259 | |
260 | void directoryChanged(const QString &directory, const QStringList &list); |
261 | void performDelayedSort(); |
262 | void fileSystemChanged(const QString &path, const QList<std::pair<QString, QFileInfo>> &); |
263 | void resolvedName(const QString &fileName, const QString &resolvedName); |
264 | |
265 | QDir rootDir; |
266 | #if QT_CONFIG(filesystemwatcher) |
267 | # ifdef Q_OS_WIN |
268 | QStringList unwatchPathsAt(const QModelIndex &); |
269 | void watchPaths(const QStringList &paths) { fileInfoGatherer->watchPaths(paths); } |
270 | # endif // Q_OS_WIN |
271 | std::unique_ptr<QFileInfoGatherer> fileInfoGatherer; |
272 | #endif // filesystemwatcher |
273 | QTimer delayedSortTimer; |
274 | QHash<const QFileSystemNode*, bool> bypassFilters; |
275 | #if QT_CONFIG(regularexpression) |
276 | QStringList nameFilters; |
277 | std::vector<QRegularExpression> nameFiltersRegexps; |
278 | void rebuildNameFilterRegexps(); |
279 | #endif |
280 | QHash<QString, QString> resolvedSymLinks; |
281 | |
282 | QFileSystemNode root; |
283 | |
284 | struct Fetching { |
285 | QString dir; |
286 | QString file; |
287 | const QFileSystemNode *node; |
288 | }; |
289 | QList<Fetching> toFetch; |
290 | |
291 | QBasicTimer fetchingTimer; |
292 | |
293 | QDir::Filters filters = QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs; |
294 | int sortColumn = 0; |
295 | Qt::SortOrder sortOrder = Qt::AscendingOrder; |
296 | bool forceSort = true; |
297 | bool readOnly = true; |
298 | bool setRootPath = false; |
299 | bool nameFilterDisables = true; // false on windows, true on mac and unix |
300 | // This flag is an optimization for QFileDialog. It enables a sort which is |
301 | // not recursive, meaning we sort only what we see. |
302 | bool disableRecursiveSort = false; |
303 | }; |
304 | Q_DECLARE_TYPEINFO(QFileSystemModelPrivate::Fetching, Q_RELOCATABLE_TYPE); |
305 | |
306 | QT_END_NAMESPACE |
307 | |
308 | #endif |
309 |
Definitions
- QFileSystemModelPrivate
- QFileSystemNode
- QFileSystemNode
- QFileSystemNode
- ~QFileSystemNode
- size
- type
- lastModified
- permissions
- isReadable
- isWritable
- isExecutable
- isDir
- fileInfo
- isFile
- isSystem
- isHidden
- isSymLink
- caseSensitive
- icon
- operator <
- operator >
- operator <
- operator !=
- operator ==
- operator ==
- hasInformation
- populate
- visibleLocation
- updateIcon
- retranslateStrings
- isHiddenByFilter
- index
- translateVisibleLocation
- myComputer
- delayedSort
Learn Advanced QML with KDAB
Find out more