1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
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 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #ifndef QFILESYSTEMMODEL_P_H
|
---|
43 | #define QFILESYSTEMMODEL_P_H
|
---|
44 |
|
---|
45 | //
|
---|
46 | // W A R N I N G
|
---|
47 | // -------------
|
---|
48 | //
|
---|
49 | // This file is not part of the Qt API. It exists purely as an
|
---|
50 | // implementation detail. This header file may change from version to
|
---|
51 | // version without notice, or even be removed.
|
---|
52 | //
|
---|
53 | // We mean it.
|
---|
54 | //
|
---|
55 |
|
---|
56 | #include "qfilesystemmodel.h"
|
---|
57 |
|
---|
58 | #ifndef QT_NO_FILESYSTEMMODEL
|
---|
59 |
|
---|
60 | #include <private/qabstractitemmodel_p.h>
|
---|
61 | #include <qabstractitemmodel.h>
|
---|
62 | #include "qfileinfogatherer_p.h"
|
---|
63 | #include <qpair.h>
|
---|
64 | #include <qdir.h>
|
---|
65 | #include <qicon.h>
|
---|
66 | #include <qdir.h>
|
---|
67 | #include <qicon.h>
|
---|
68 | #include <qfileinfo.h>
|
---|
69 | #include <qtimer.h>
|
---|
70 | #include <qhash.h>
|
---|
71 |
|
---|
72 | QT_BEGIN_NAMESPACE
|
---|
73 |
|
---|
74 | class ExtendedInformation;
|
---|
75 | class QFileSystemModelPrivate;
|
---|
76 | class QFileIconProvider;
|
---|
77 |
|
---|
78 | class Q_AUTOTEST_EXPORT QFileSystemModelPrivate : public QAbstractItemModelPrivate
|
---|
79 | {
|
---|
80 | Q_DECLARE_PUBLIC(QFileSystemModel)
|
---|
81 |
|
---|
82 | public:
|
---|
83 | class QFileSystemNode
|
---|
84 | {
|
---|
85 | public:
|
---|
86 | QFileSystemNode(const QString &filename = QString(), QFileSystemNode *p = 0)
|
---|
87 | : fileName(filename), populatedChildren(false), isVisible(false), parent(p), info(0) {}
|
---|
88 | ~QFileSystemNode() {
|
---|
89 | QHash<QString, QFileSystemNode*>::const_iterator i = children.constBegin();
|
---|
90 | while (i != children.constEnd()) {
|
---|
91 | delete i.value();
|
---|
92 | ++i;
|
---|
93 | }
|
---|
94 | delete info;
|
---|
95 | info = 0;
|
---|
96 | parent = 0;
|
---|
97 | }
|
---|
98 |
|
---|
99 | QString fileName;
|
---|
100 |
|
---|
101 | inline qint64 size() const { if (info && !info->isDir()) return info->size(); return 0; }
|
---|
102 | inline QString type() const { if (info) return info->displayType; return QLatin1String(""); }
|
---|
103 | inline QDateTime lastModified() const { if (info) return info->lastModified(); return QDateTime(); }
|
---|
104 | inline QFile::Permissions permissions() const { if (info) return info->permissions(); return 0; }
|
---|
105 | inline bool isReadable() const { return ((permissions() & QFile::ReadUser) != 0); }
|
---|
106 | inline bool isWritable() const { return ((permissions() & QFile::WriteUser) != 0); }
|
---|
107 | inline bool isExecutable() const { return ((permissions() & QFile::ExeUser) != 0); }
|
---|
108 | inline bool isDir() const {
|
---|
109 | if (info)
|
---|
110 | return info->isDir();
|
---|
111 | if (children.count() > 0)
|
---|
112 | return true;
|
---|
113 | return false;
|
---|
114 | }
|
---|
115 | inline bool isFile() const { if (info) return info->isFile(); return true; }
|
---|
116 | inline bool isSystem() const { if (info) return info->isSystem(); return true; }
|
---|
117 | inline bool isHidden() const { if (info) return info->isHidden(); return false; }
|
---|
118 | inline bool isSymLink() const { if (info) return info->isSymLink(); return false; }
|
---|
119 | inline bool caseSensitive() const { if (info) return info->isCaseSensitive(); return false; }
|
---|
120 | inline QIcon icon() const { if (info) return info->icon; return QIcon(); }
|
---|
121 |
|
---|
122 | inline bool operator <(const QFileSystemNode &node) const {
|
---|
123 | if (caseSensitive() || node.caseSensitive())
|
---|
124 | return fileName < node.fileName;
|
---|
125 | return QString::compare(fileName, node.fileName, Qt::CaseInsensitive) < 0;
|
---|
126 | }
|
---|
127 | inline bool operator >(const QString &name) const {
|
---|
128 | if (caseSensitive())
|
---|
129 | return fileName > name;
|
---|
130 | return QString::compare(fileName, name, Qt::CaseInsensitive) > 0;
|
---|
131 | }
|
---|
132 | inline bool operator <(const QString &name) const {
|
---|
133 | if (caseSensitive())
|
---|
134 | return fileName < name;
|
---|
135 | return QString::compare(fileName, name, Qt::CaseInsensitive) < 0;
|
---|
136 | }
|
---|
137 | inline bool operator !=(const QExtendedInformation &fileInfo) const {
|
---|
138 | return !operator==(fileInfo);
|
---|
139 | }
|
---|
140 | bool operator ==(const QString &name) const {
|
---|
141 | if (caseSensitive())
|
---|
142 | return fileName == name;
|
---|
143 | return QString::compare(fileName, name, Qt::CaseInsensitive) == 0;
|
---|
144 | }
|
---|
145 | bool operator ==(const QExtendedInformation &fileInfo) const {
|
---|
146 | return info && (*info == fileInfo);
|
---|
147 | }
|
---|
148 |
|
---|
149 | inline bool hasInformation() const { return info != 0; }
|
---|
150 |
|
---|
151 | void populate(const QExtendedInformation &fileInfo) {
|
---|
152 | if (!info)
|
---|
153 | info = new QExtendedInformation(fileInfo.fileInfo());
|
---|
154 | (*info) = fileInfo;
|
---|
155 | }
|
---|
156 |
|
---|
157 | // children shouldn't normally be accessed directly, use node()
|
---|
158 | inline int visibleLocation(QString childName) {
|
---|
159 | return visibleChildren.indexOf(childName);
|
---|
160 | }
|
---|
161 | void updateIcon(QFileIconProvider *iconProvider, const QString &path) {
|
---|
162 | if (info)
|
---|
163 | info->icon = iconProvider->icon(QFileInfo(path));
|
---|
164 | QHash<QString, QFileSystemNode *>::const_iterator iterator;
|
---|
165 | for(iterator = children.constBegin() ; iterator != children.constEnd() ; ++iterator) {
|
---|
166 | iterator.value()->updateIcon(iconProvider, path + QLatin1Char('/') + iterator.value()->fileName);
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | void retranslateStrings(QFileIconProvider *iconProvider, const QString &path) {
|
---|
171 | if (info)
|
---|
172 | info->displayType = iconProvider->type(QFileInfo(path));
|
---|
173 | QHash<QString, QFileSystemNode *>::const_iterator iterator;
|
---|
174 | for(iterator = children.constBegin() ; iterator != children.constEnd() ; ++iterator) {
|
---|
175 | iterator.value()->retranslateStrings(iconProvider, path + QLatin1Char('/') + iterator.value()->fileName);
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | bool populatedChildren;
|
---|
180 | bool isVisible;
|
---|
181 | QHash<QString,QFileSystemNode *> children;
|
---|
182 | QList<QString> visibleChildren;
|
---|
183 | QFileSystemNode *parent;
|
---|
184 |
|
---|
185 |
|
---|
186 | QExtendedInformation *info;
|
---|
187 |
|
---|
188 | };
|
---|
189 |
|
---|
190 | QFileSystemModelPrivate() :
|
---|
191 | forceSort(true),
|
---|
192 | sortColumn(0),
|
---|
193 | sortOrder(Qt::AscendingOrder),
|
---|
194 | readOnly(true),
|
---|
195 | setRootPath(false),
|
---|
196 | filters(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs),
|
---|
197 | nameFilterDisables(true) // false on windows, true on mac and unix
|
---|
198 | {
|
---|
199 | delayedSortTimer.setSingleShot(true);
|
---|
200 | }
|
---|
201 |
|
---|
202 | void init();
|
---|
203 | /*
|
---|
204 | \internal
|
---|
205 |
|
---|
206 | Return true if index which is owned by node is hidden by the filter.
|
---|
207 | */
|
---|
208 | inline bool isHiddenByFilter(QFileSystemNode *indexNode, const QModelIndex &index) const
|
---|
209 | {
|
---|
210 | return (indexNode != &root && !index.isValid());
|
---|
211 | }
|
---|
212 | QFileSystemNode *node(const QModelIndex &index) const;
|
---|
213 | QFileSystemNode *node(const QString &path, bool fetch = true) const;
|
---|
214 | inline QModelIndex index(const QString &path) { return index(node(path)); }
|
---|
215 | QModelIndex index(const QFileSystemNode *node) const;
|
---|
216 | bool filtersAcceptsNode(const QFileSystemNode *node) const;
|
---|
217 | bool passNameFilters(const QFileSystemNode *node) const;
|
---|
218 | void removeNode(QFileSystemNode *parentNode, const QString &name);
|
---|
219 | QFileSystemNode* addNode(QFileSystemNode *parentNode, const QString &fileName, const QFileInfo &info);
|
---|
220 | void addVisibleFiles(QFileSystemNode *parentNode, const QStringList &newFiles);
|
---|
221 | void removeVisibleFile(QFileSystemNode *parentNode, int visibleLocation);
|
---|
222 | void sortChildren(int column, const QModelIndex &parent);
|
---|
223 |
|
---|
224 | inline int translateVisibleLocation(QFileSystemNode *parent, int row) const {
|
---|
225 | return (sortOrder == Qt::AscendingOrder) ? row : parent->visibleChildren.count() - row - 1;
|
---|
226 | }
|
---|
227 |
|
---|
228 | inline static QString myComputer() {
|
---|
229 | // ### TODO We should query the system to find out what the string should be
|
---|
230 | // XP == "My Computer",
|
---|
231 | // Vista == "Computer",
|
---|
232 | // OS X == "Computer" (sometime user generated) "Benjamin's PowerBook G4"
|
---|
233 | #ifdef Q_OS_WIN
|
---|
234 | return QFileSystemModel::tr("My Computer");
|
---|
235 | #else
|
---|
236 | return QFileSystemModel::tr("Computer");
|
---|
237 | #endif
|
---|
238 | }
|
---|
239 |
|
---|
240 | inline void delayedSort() {
|
---|
241 | if (!delayedSortTimer.isActive())
|
---|
242 | delayedSortTimer.start(0);
|
---|
243 | }
|
---|
244 |
|
---|
245 | static bool caseInsensitiveLessThan(const QString &s1, const QString &s2)
|
---|
246 | {
|
---|
247 | return QString::compare(s1, s2, Qt::CaseInsensitive) < 0;
|
---|
248 | }
|
---|
249 |
|
---|
250 | static bool nodeCaseInsensitiveLessThan(const QFileSystemModelPrivate::QFileSystemNode &s1, const QFileSystemModelPrivate::QFileSystemNode &s2)
|
---|
251 | {
|
---|
252 | return QString::compare(s1.fileName, s2.fileName, Qt::CaseInsensitive) < 0;
|
---|
253 | }
|
---|
254 |
|
---|
255 | QIcon icon(const QModelIndex &index) const;
|
---|
256 | QString name(const QModelIndex &index) const;
|
---|
257 | QString filePath(const QModelIndex &index) const;
|
---|
258 | QString size(const QModelIndex &index) const;
|
---|
259 | static QString size(qint64 bytes);
|
---|
260 | QString type(const QModelIndex &index) const;
|
---|
261 | QString time(const QModelIndex &index) const;
|
---|
262 |
|
---|
263 | void _q_directoryChanged(const QString &directory, const QStringList &list);
|
---|
264 | void _q_performDelayedSort();
|
---|
265 | void _q_fileSystemChanged(const QString &path, const QList<QPair<QString, QFileInfo> > &);
|
---|
266 | void _q_resolvedName(const QString &fileName, const QString &resolvedName);
|
---|
267 |
|
---|
268 | static int naturalCompare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs);
|
---|
269 |
|
---|
270 | QDir rootDir;
|
---|
271 | #ifndef QT_NO_FILESYSTEMWATCHER
|
---|
272 | QFileInfoGatherer fileInfoGatherer;
|
---|
273 | #endif
|
---|
274 | QTimer delayedSortTimer;
|
---|
275 | bool forceSort;
|
---|
276 | int sortColumn;
|
---|
277 | Qt::SortOrder sortOrder;
|
---|
278 | bool readOnly;
|
---|
279 | bool setRootPath;
|
---|
280 | QDir::Filters filters;
|
---|
281 | QHash<const QFileSystemNode*, bool> bypassFilters;
|
---|
282 | bool nameFilterDisables;
|
---|
283 | #ifndef QT_NO_REGEXP
|
---|
284 | QList<QRegExp> nameFilters;
|
---|
285 | #endif
|
---|
286 | // ### Qt 5: resolvedSymLinks goes away
|
---|
287 | QHash<QString, QString> resolvedSymLinks;
|
---|
288 |
|
---|
289 | QFileSystemNode root;
|
---|
290 |
|
---|
291 | QBasicTimer fetchingTimer;
|
---|
292 | struct Fetching {
|
---|
293 | QString dir;
|
---|
294 | QString file;
|
---|
295 | const QFileSystemNode *node;
|
---|
296 | };
|
---|
297 | QList<Fetching> toFetch;
|
---|
298 |
|
---|
299 | };
|
---|
300 | #endif // QT_NO_FILESYSTEMMODEL
|
---|
301 |
|
---|
302 | QT_END_NAMESPACE
|
---|
303 |
|
---|
304 | #endif
|
---|
305 |
|
---|