source: trunk/src/gui/dialogs/qfilesystemmodel_p.h@ 460

Last change on this file since 460 was 173, checked in by Dmitry A. Kuminov, 16 years ago

gui/dialogs: Fixed: Duplicate file names were added to the file list in QFileDialog when an existing directory was typed directly in the filename entry field with a different case of letters than in the original (on platforms with case insensitive file systems), #65.

File size: 12.6 KB
Line 
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
72QT_BEGIN_NAMESPACE
73
74class ExtendedInformation;
75class QFileSystemModelPrivate;
76class QFileIconProvider;
77
78class Q_AUTOTEST_EXPORT QFileSystemModelPrivate : public QAbstractItemModelPrivate
79{
80 Q_DECLARE_PUBLIC(QFileSystemModel)
81
82public:
83
84#if defined(Q_OS_WIN) || defined(Q_OS_OS2)
85 // On Windows and OS/2, file names are case insensitive so use a lowercased
86 // string as a key in the hash map of name->node pairs. Note that strictly
87 // speaking we should use QAbstractFileEngine::caseSensitive() to check if
88 // the case should matter but on these platforms this method in QFSFileEngine
89 // always returns false while doing this check would require to add a
90 // QFileSystemNode* argument to the constructor and hence change almost every
91 // line that uses the hash because the automatic QString->FileNameKey would
92 // not work in this case. Note2: on Windows, evreything actually works without
93 // this class but that's just because we use qt_GetLongPathName() which
94 // always returns a real filename case and any changes in the case are
95 // instantly picked up by QFileSystemWatcher anyway (otherwise we'd get dups
96 // in the file ilst there too).
97 class FileNameKey : public QString
98 {
99 public:
100 FileNameKey(const QString &copy) : QString(copy.toLower()) {}
101 };
102#else
103 typedef QString FileNameKey;
104#endif
105
106 class QFileSystemNode
107 {
108 public:
109 QFileSystemNode(const QString &filename = QString(), QFileSystemNode *p = 0)
110 : fileName(filename), populatedChildren(false), isVisible(false), parent(p), info(0) {}
111 ~QFileSystemNode() {
112 QHash<FileNameKey, QFileSystemNode*>::const_iterator i = children.constBegin();
113 while (i != children.constEnd()) {
114 delete i.value();
115 ++i;
116 }
117 delete info;
118 info = 0;
119 parent = 0;
120 }
121
122 QString fileName;
123
124 inline qint64 size() const { if (info && !info->isDir()) return info->size(); return 0; }
125 inline QString type() const { if (info) return info->displayType; return QLatin1String(""); }
126 inline QDateTime lastModified() const { if (info) return info->lastModified(); return QDateTime(); }
127 inline QFile::Permissions permissions() const { if (info) return info->permissions(); return 0; }
128 inline bool isReadable() const { return ((permissions() & QFile::ReadUser) != 0); }
129 inline bool isWritable() const { return ((permissions() & QFile::WriteUser) != 0); }
130 inline bool isExecutable() const { return ((permissions() & QFile::ExeUser) != 0); }
131 inline bool isDir() const {
132 if (info)
133 return info->isDir();
134 if (children.count() > 0)
135 return true;
136 return false;
137 }
138 inline bool isFile() const { if (info) return info->isFile(); return true; }
139 inline bool isSystem() const { if (info) return info->isSystem(); return true; }
140 inline bool isHidden() const { if (info) return info->isHidden(); return false; }
141 inline bool isSymLink() const { if (info) return info->isSymLink(); return false; }
142 inline bool caseSensitive() const { if (info) return info->isCaseSensitive(); return false; }
143 inline QIcon icon() const { if (info) return info->icon; return QIcon(); }
144
145 inline bool operator <(const QFileSystemNode &node) const {
146 if (caseSensitive() || node.caseSensitive())
147 return fileName < node.fileName;
148 return QString::compare(fileName, node.fileName, Qt::CaseInsensitive) < 0;
149 }
150 inline bool operator >(const QString &name) const {
151 if (caseSensitive())
152 return fileName > name;
153 return QString::compare(fileName, name, Qt::CaseInsensitive) > 0;
154 }
155 inline bool operator <(const QString &name) const {
156 if (caseSensitive())
157 return fileName < name;
158 return QString::compare(fileName, name, Qt::CaseInsensitive) < 0;
159 }
160 inline bool operator !=(const QExtendedInformation &fileInfo) const {
161 return !operator==(fileInfo);
162 }
163 bool operator ==(const QString &name) const {
164 if (caseSensitive())
165 return fileName == name;
166 return QString::compare(fileName, name, Qt::CaseInsensitive) == 0;
167 }
168 bool operator ==(const QExtendedInformation &fileInfo) const {
169 return info && (*info == fileInfo);
170 }
171
172 inline bool hasInformation() const { return info != 0; }
173
174 void populate(const QExtendedInformation &fileInfo) {
175 if (!info)
176 info = new QExtendedInformation(fileInfo.fileInfo());
177 (*info) = fileInfo;
178 }
179
180 // children shouldn't normally be accessed directly, use node()
181 inline int visibleLocation(QString childName) {
182 return visibleChildren.indexOf(childName);
183 }
184 void updateIcon(QFileIconProvider *iconProvider, const QString &path) {
185 if (info)
186 info->icon = iconProvider->icon(QFileInfo(path));
187 QHash<FileNameKey, QFileSystemNode *>::const_iterator iterator;
188 for(iterator = children.constBegin() ; iterator != children.constEnd() ; ++iterator) {
189 iterator.value()->updateIcon(iconProvider, path + QLatin1Char('/') + iterator.value()->fileName);
190 }
191 }
192
193 void retranslateStrings(QFileIconProvider *iconProvider, const QString &path) {
194 if (info)
195 info->displayType = iconProvider->type(QFileInfo(path));
196 QHash<FileNameKey, QFileSystemNode *>::const_iterator iterator;
197 for(iterator = children.constBegin() ; iterator != children.constEnd() ; ++iterator) {
198 iterator.value()->retranslateStrings(iconProvider, path + QLatin1Char('/') + iterator.value()->fileName);
199 }
200 }
201
202 bool populatedChildren;
203 bool isVisible;
204 QHash<FileNameKey, QFileSystemNode *> children;
205 QList<QString> visibleChildren;
206 QFileSystemNode *parent;
207
208
209 QExtendedInformation *info;
210
211 };
212
213 QFileSystemModelPrivate() :
214 forceSort(true),
215 sortColumn(0),
216 sortOrder(Qt::AscendingOrder),
217 readOnly(true),
218 setRootPath(false),
219 filters(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs),
220 nameFilterDisables(true) // false on windows, true on mac and unix
221 {
222 delayedSortTimer.setSingleShot(true);
223 }
224
225 void init();
226 /*
227 \internal
228
229 Return true if index which is owned by node is hidden by the filter.
230 */
231 inline bool isHiddenByFilter(QFileSystemNode *indexNode, const QModelIndex &index) const
232 {
233 return (indexNode != &root && !index.isValid());
234 }
235 QFileSystemNode *node(const QModelIndex &index) const;
236 QFileSystemNode *node(const QString &path, bool fetch = true) const;
237 inline QModelIndex index(const QString &path) { return index(node(path)); }
238 QModelIndex index(const QFileSystemNode *node) const;
239 bool filtersAcceptsNode(const QFileSystemNode *node) const;
240 bool passNameFilters(const QFileSystemNode *node) const;
241 void removeNode(QFileSystemNode *parentNode, const QString &name);
242 QFileSystemNode* addNode(QFileSystemNode *parentNode, const QString &fileName, const QFileInfo &info);
243 void addVisibleFiles(QFileSystemNode *parentNode, const QStringList &newFiles);
244 void removeVisibleFile(QFileSystemNode *parentNode, int visibleLocation);
245 void sortChildren(int column, const QModelIndex &parent);
246
247 inline int translateVisibleLocation(QFileSystemNode *parent, int row) const {
248 return (sortOrder == Qt::AscendingOrder) ? row : parent->visibleChildren.count() - row - 1;
249 }
250
251 inline static QString myComputer() {
252 // ### TODO We should query the system to find out what the string should be
253 // XP == "My Computer",
254 // Vista == "Computer",
255 // OS X == "Computer" (sometime user generated) "Benjamin's PowerBook G4"
256#ifdef Q_OS_WIN
257 return QFileSystemModel::tr("My Computer");
258#else
259 return QFileSystemModel::tr("Computer");
260#endif
261 }
262
263 inline void delayedSort() {
264 if (!delayedSortTimer.isActive())
265 delayedSortTimer.start(0);
266 }
267
268 static bool caseInsensitiveLessThan(const QString &s1, const QString &s2)
269 {
270 return QString::compare(s1, s2, Qt::CaseInsensitive) < 0;
271 }
272
273 static bool nodeCaseInsensitiveLessThan(const QFileSystemModelPrivate::QFileSystemNode &s1, const QFileSystemModelPrivate::QFileSystemNode &s2)
274 {
275 return QString::compare(s1.fileName, s2.fileName, Qt::CaseInsensitive) < 0;
276 }
277
278 QIcon icon(const QModelIndex &index) const;
279 QString name(const QModelIndex &index) const;
280 QString filePath(const QModelIndex &index) const;
281 QString size(const QModelIndex &index) const;
282 static QString size(qint64 bytes);
283 QString type(const QModelIndex &index) const;
284 QString time(const QModelIndex &index) const;
285
286 void _q_directoryChanged(const QString &directory, const QStringList &list);
287 void _q_performDelayedSort();
288 void _q_fileSystemChanged(const QString &path, const QList<QPair<QString, QFileInfo> > &);
289 void _q_resolvedName(const QString &fileName, const QString &resolvedName);
290
291 static int naturalCompare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs);
292
293 QDir rootDir;
294#ifndef QT_NO_FILESYSTEMWATCHER
295 QFileInfoGatherer fileInfoGatherer;
296#endif
297 QTimer delayedSortTimer;
298 bool forceSort;
299 int sortColumn;
300 Qt::SortOrder sortOrder;
301 bool readOnly;
302 bool setRootPath;
303 QDir::Filters filters;
304 QHash<const QFileSystemNode*, bool> bypassFilters;
305 bool nameFilterDisables;
306#ifndef QT_NO_REGEXP
307 QList<QRegExp> nameFilters;
308#endif
309 // ### Qt 5: resolvedSymLinks goes away
310 QHash<QString, QString> resolvedSymLinks;
311
312 QFileSystemNode root;
313
314 QBasicTimer fetchingTimer;
315 struct Fetching {
316 QString dir;
317 QString file;
318 const QFileSystemNode *node;
319 };
320 QList<Fetching> toFetch;
321
322};
323#endif // QT_NO_FILESYSTEMMODEL
324
325QT_END_NAMESPACE
326
327#endif
328
Note: See TracBrowser for help on using the repository browser.