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

Last change on this file since 561 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 14.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
37** Nokia 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), dirtyChildrenIndex(-1), 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 //On windows the root (My computer) has no path so we don't want to add a / for nothing (e.g. /C:/)
190 if (!path.isEmpty()) {
191 if (path.endsWith(QLatin1Char('/')))
192 iterator.value()->updateIcon(iconProvider, path + iterator.value()->fileName);
193 else
194 iterator.value()->updateIcon(iconProvider, path + QLatin1Char('/') + iterator.value()->fileName);
195 } else
196 iterator.value()->updateIcon(iconProvider, iterator.value()->fileName);
197 }
198 }
199
200 void retranslateStrings(QFileIconProvider *iconProvider, const QString &path) {
201 if (info)
202 info->displayType = iconProvider->type(QFileInfo(path));
203 QHash<FileNameKey, QFileSystemNode *>::const_iterator iterator;
204 for(iterator = children.constBegin() ; iterator != children.constEnd() ; ++iterator) {
205 //On windows the root (My computer) has no path so we don't want to add a / for nothing (e.g. /C:/)
206 if (!path.isEmpty()) {
207 if (path.endsWith(QLatin1Char('/')))
208 iterator.value()->retranslateStrings(iconProvider, path + iterator.value()->fileName);
209 else
210 iterator.value()->retranslateStrings(iconProvider, path + QLatin1Char('/') + iterator.value()->fileName);
211 } else
212 iterator.value()->retranslateStrings(iconProvider, iterator.value()->fileName);
213 }
214 }
215
216 bool populatedChildren;
217 bool isVisible;
218 QHash<FileNameKey, QFileSystemNode *> children;
219 QList<QString> visibleChildren;
220 int dirtyChildrenIndex;
221 QFileSystemNode *parent;
222
223
224 QExtendedInformation *info;
225
226 };
227
228 QFileSystemModelPrivate() :
229 forceSort(true),
230 sortColumn(0),
231 sortOrder(Qt::AscendingOrder),
232 readOnly(true),
233 setRootPath(false),
234 filters(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs),
235 nameFilterDisables(true), // false on windows, true on mac and unix
236 disableRecursiveSort(false)
237 {
238 delayedSortTimer.setSingleShot(true);
239 }
240
241 void init();
242 /*
243 \internal
244
245 Return true if index which is owned by node is hidden by the filter.
246 */
247 inline bool isHiddenByFilter(QFileSystemNode *indexNode, const QModelIndex &index) const
248 {
249 return (indexNode != &root && !index.isValid());
250 }
251 QFileSystemNode *node(const QModelIndex &index) const;
252 QFileSystemNode *node(const QString &path, bool fetch = true) const;
253 inline QModelIndex index(const QString &path) { return index(node(path)); }
254 QModelIndex index(const QFileSystemNode *node) const;
255 bool filtersAcceptsNode(const QFileSystemNode *node) const;
256 bool passNameFilters(const QFileSystemNode *node) const;
257 void removeNode(QFileSystemNode *parentNode, const QString &name);
258 QFileSystemNode* addNode(QFileSystemNode *parentNode, const QString &fileName, const QFileInfo &info);
259 void addVisibleFiles(QFileSystemNode *parentNode, const QStringList &newFiles);
260 void removeVisibleFile(QFileSystemNode *parentNode, int visibleLocation);
261 void sortChildren(int column, const QModelIndex &parent);
262
263 inline int translateVisibleLocation(QFileSystemNode *parent, int row) const {
264 if (sortOrder == Qt::AscendingOrder)
265 return row;
266 if (parent->dirtyChildrenIndex == -1 || row < parent->dirtyChildrenIndex)
267 if (parent->dirtyChildrenIndex != -1)
268 return parent->dirtyChildrenIndex - row - 1;
269 else
270 return parent->visibleChildren.count() - row - 1;
271 else
272 return row;
273 }
274
275 inline static QString myComputer() {
276 // ### TODO We should query the system to find out what the string should be
277 // XP == "My Computer",
278 // Vista == "Computer",
279 // OS X == "Computer" (sometime user generated) "Benjamin's PowerBook G4"
280#ifdef Q_OS_WIN
281 return QFileSystemModel::tr("My Computer");
282#else
283 return QFileSystemModel::tr("Computer");
284#endif
285 }
286
287 inline void delayedSort() {
288 if (!delayedSortTimer.isActive())
289 delayedSortTimer.start(0);
290 }
291
292 static bool caseInsensitiveLessThan(const QString &s1, const QString &s2)
293 {
294 return QString::compare(s1, s2, Qt::CaseInsensitive) < 0;
295 }
296
297 static bool nodeCaseInsensitiveLessThan(const QFileSystemModelPrivate::QFileSystemNode &s1, const QFileSystemModelPrivate::QFileSystemNode &s2)
298 {
299 return QString::compare(s1.fileName, s2.fileName, Qt::CaseInsensitive) < 0;
300 }
301
302 QIcon icon(const QModelIndex &index) const;
303 QString name(const QModelIndex &index) const;
304 QString filePath(const QModelIndex &index) const;
305 QString size(const QModelIndex &index) const;
306 static QString size(qint64 bytes);
307 QString type(const QModelIndex &index) const;
308 QString time(const QModelIndex &index) const;
309
310 void _q_directoryChanged(const QString &directory, const QStringList &list);
311 void _q_performDelayedSort();
312 void _q_fileSystemChanged(const QString &path, const QList<QPair<QString, QFileInfo> > &);
313 void _q_resolvedName(const QString &fileName, const QString &resolvedName);
314
315 static int naturalCompare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs);
316
317 QDir rootDir;
318#ifndef QT_NO_FILESYSTEMWATCHER
319 QFileInfoGatherer fileInfoGatherer;
320#endif
321 QTimer delayedSortTimer;
322 bool forceSort;
323 int sortColumn;
324 Qt::SortOrder sortOrder;
325 bool readOnly;
326 bool setRootPath;
327 QDir::Filters filters;
328 QHash<const QFileSystemNode*, bool> bypassFilters;
329 bool nameFilterDisables;
330 //This flag is an optimization for the QFileDialog
331 //It enable a sort which is not recursive, it means
332 //we sort only what we see.
333 bool disableRecursiveSort;
334#ifndef QT_NO_REGEXP
335 QList<QRegExp> nameFilters;
336#endif
337 // ### Qt 5: resolvedSymLinks goes away
338 QHash<QString, QString> resolvedSymLinks;
339
340 QFileSystemNode root;
341
342 QBasicTimer fetchingTimer;
343 struct Fetching {
344 QString dir;
345 QString file;
346 const QFileSystemNode *node;
347 };
348 QList<Fetching> toFetch;
349
350};
351#endif // QT_NO_FILESYSTEMMODEL
352
353QT_END_NAMESPACE
354
355#endif
356
Note: See TracBrowser for help on using the repository browser.