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

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 14.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
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 qt-info@nokia.com.
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#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
124 QString volumeName;
125#endif
126
127 inline qint64 size() const { if (info && !info->isDir()) return info->size(); return 0; }
128 inline QString type() const { if (info) return info->displayType; return QLatin1String(""); }
129 inline QDateTime lastModified() const { if (info) return info->lastModified(); return QDateTime(); }
130 inline QFile::Permissions permissions() const { if (info) return info->permissions(); return 0; }
131 inline bool isReadable() const { return ((permissions() & QFile::ReadUser) != 0); }
132 inline bool isWritable() const { return ((permissions() & QFile::WriteUser) != 0); }
133 inline bool isExecutable() const { return ((permissions() & QFile::ExeUser) != 0); }
134 inline bool isDir() const {
135 if (info)
136 return info->isDir();
137 if (children.count() > 0)
138 return true;
139 return false;
140 }
141 inline bool isFile() const { if (info) return info->isFile(); return true; }
142 inline bool isSystem() const { if (info) return info->isSystem(); return true; }
143 inline bool isHidden() const { if (info) return info->isHidden(); return false; }
144 inline bool isSymLink() const { if (info) return info->isSymLink(); return false; }
145 inline bool caseSensitive() const { if (info) return info->isCaseSensitive(); return false; }
146 inline QIcon icon() const { if (info) return info->icon; return QIcon(); }
147
148 inline bool operator <(const QFileSystemNode &node) const {
149 if (caseSensitive() || node.caseSensitive())
150 return fileName < node.fileName;
151 return QString::compare(fileName, node.fileName, Qt::CaseInsensitive) < 0;
152 }
153 inline bool operator >(const QString &name) const {
154 if (caseSensitive())
155 return fileName > name;
156 return QString::compare(fileName, name, Qt::CaseInsensitive) > 0;
157 }
158 inline bool operator <(const QString &name) const {
159 if (caseSensitive())
160 return fileName < name;
161 return QString::compare(fileName, name, Qt::CaseInsensitive) < 0;
162 }
163 inline bool operator !=(const QExtendedInformation &fileInfo) const {
164 return !operator==(fileInfo);
165 }
166 bool operator ==(const QString &name) const {
167 if (caseSensitive())
168 return fileName == name;
169 return QString::compare(fileName, name, Qt::CaseInsensitive) == 0;
170 }
171 bool operator ==(const QExtendedInformation &fileInfo) const {
172 return info && (*info == fileInfo);
173 }
174
175 inline bool hasInformation() const { return info != 0; }
176
177 void populate(const QExtendedInformation &fileInfo) {
178 if (!info)
179 info = new QExtendedInformation(fileInfo.fileInfo());
180 (*info) = fileInfo;
181 }
182
183 // children shouldn't normally be accessed directly, use node()
184 inline int visibleLocation(QString childName) {
185 return visibleChildren.indexOf(childName);
186 }
187 void updateIcon(QFileIconProvider *iconProvider, const QString &path) {
188 if (info)
189 info->icon = iconProvider->icon(QFileInfo(path));
190 QHash<FileNameKey, QFileSystemNode *>::const_iterator iterator;
191 for(iterator = children.constBegin() ; iterator != children.constEnd() ; ++iterator) {
192 //On windows the root (My computer) has no path so we don't want to add a / for nothing (e.g. /C:/)
193 if (!path.isEmpty()) {
194 if (path.endsWith(QLatin1Char('/')))
195 iterator.value()->updateIcon(iconProvider, path + iterator.value()->fileName);
196 else
197 iterator.value()->updateIcon(iconProvider, path + QLatin1Char('/') + iterator.value()->fileName);
198 } else
199 iterator.value()->updateIcon(iconProvider, iterator.value()->fileName);
200 }
201 }
202
203 void retranslateStrings(QFileIconProvider *iconProvider, const QString &path) {
204 if (info)
205 info->displayType = iconProvider->type(QFileInfo(path));
206 QHash<FileNameKey, QFileSystemNode *>::const_iterator iterator;
207 for(iterator = children.constBegin() ; iterator != children.constEnd() ; ++iterator) {
208 //On windows the root (My computer) has no path so we don't want to add a / for nothing (e.g. /C:/)
209 if (!path.isEmpty()) {
210 if (path.endsWith(QLatin1Char('/')))
211 iterator.value()->retranslateStrings(iconProvider, path + iterator.value()->fileName);
212 else
213 iterator.value()->retranslateStrings(iconProvider, path + QLatin1Char('/') + iterator.value()->fileName);
214 } else
215 iterator.value()->retranslateStrings(iconProvider, iterator.value()->fileName);
216 }
217 }
218
219 bool populatedChildren;
220 bool isVisible;
221 QHash<FileNameKey, QFileSystemNode *> children;
222 QList<QString> visibleChildren;
223 int dirtyChildrenIndex;
224 QFileSystemNode *parent;
225
226
227 QExtendedInformation *info;
228
229 };
230
231 QFileSystemModelPrivate() :
232 forceSort(true),
233 sortColumn(0),
234 sortOrder(Qt::AscendingOrder),
235 readOnly(true),
236 setRootPath(false),
237 filters(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs),
238 nameFilterDisables(true), // false on windows, true on mac and unix
239 disableRecursiveSort(false)
240 {
241 delayedSortTimer.setSingleShot(true);
242 }
243
244 void init();
245 /*
246 \internal
247
248 Return true if index which is owned by node is hidden by the filter.
249 */
250 inline bool isHiddenByFilter(QFileSystemNode *indexNode, const QModelIndex &index) const
251 {
252 return (indexNode != &root && !index.isValid());
253 }
254 QFileSystemNode *node(const QModelIndex &index) const;
255 QFileSystemNode *node(const QString &path, bool fetch = true) const;
256 inline QModelIndex index(const QString &path) { return index(node(path)); }
257 QModelIndex index(const QFileSystemNode *node) const;
258 bool filtersAcceptsNode(const QFileSystemNode *node) const;
259 bool passNameFilters(const QFileSystemNode *node) const;
260 void removeNode(QFileSystemNode *parentNode, const QString &name);
261 QFileSystemNode* addNode(QFileSystemNode *parentNode, const QString &fileName, const QFileInfo &info);
262 void addVisibleFiles(QFileSystemNode *parentNode, const QStringList &newFiles);
263 void removeVisibleFile(QFileSystemNode *parentNode, int visibleLocation);
264 void sortChildren(int column, const QModelIndex &parent);
265
266 inline int translateVisibleLocation(QFileSystemNode *parent, int row) const {
267 if (sortOrder != Qt::AscendingOrder) {
268 if (parent->dirtyChildrenIndex == -1)
269 return parent->visibleChildren.count() - row - 1;
270
271 if (row < parent->dirtyChildrenIndex)
272 return parent->dirtyChildrenIndex - row - 1;
273 }
274
275 return row;
276 }
277
278 inline static QString myComputer() {
279 // ### TODO We should query the system to find out what the string should be
280 // XP == "My Computer",
281 // Vista == "Computer",
282 // OS X == "Computer" (sometime user generated) "Benjamin's PowerBook G4"
283#ifdef Q_OS_WIN
284 return QFileSystemModel::tr("My Computer");
285#else
286 return QFileSystemModel::tr("Computer");
287#endif
288 }
289
290 inline void delayedSort() {
291 if (!delayedSortTimer.isActive())
292 delayedSortTimer.start(0);
293 }
294
295 static bool caseInsensitiveLessThan(const QString &s1, const QString &s2)
296 {
297 return QString::compare(s1, s2, Qt::CaseInsensitive) < 0;
298 }
299
300 static bool nodeCaseInsensitiveLessThan(const QFileSystemModelPrivate::QFileSystemNode &s1, const QFileSystemModelPrivate::QFileSystemNode &s2)
301 {
302 return QString::compare(s1.fileName, s2.fileName, Qt::CaseInsensitive) < 0;
303 }
304
305 QIcon icon(const QModelIndex &index) const;
306 QString name(const QModelIndex &index) const;
307 QString displayName(const QModelIndex &index) const;
308 QString filePath(const QModelIndex &index) const;
309 QString size(const QModelIndex &index) const;
310 static QString size(qint64 bytes);
311 QString type(const QModelIndex &index) const;
312 QString time(const QModelIndex &index) const;
313
314 void _q_directoryChanged(const QString &directory, const QStringList &list);
315 void _q_performDelayedSort();
316 void _q_fileSystemChanged(const QString &path, const QList<QPair<QString, QFileInfo> > &);
317 void _q_resolvedName(const QString &fileName, const QString &resolvedName);
318
319 static int naturalCompare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs);
320
321 QDir rootDir;
322#ifndef QT_NO_FILESYSTEMWATCHER
323 QFileInfoGatherer fileInfoGatherer;
324#endif
325 QTimer delayedSortTimer;
326 bool forceSort;
327 int sortColumn;
328 Qt::SortOrder sortOrder;
329 bool readOnly;
330 bool setRootPath;
331 QDir::Filters filters;
332 QHash<const QFileSystemNode*, bool> bypassFilters;
333 bool nameFilterDisables;
334 //This flag is an optimization for the QFileDialog
335 //It enable a sort which is not recursive, it means
336 //we sort only what we see.
337 bool disableRecursiveSort;
338#ifndef QT_NO_REGEXP
339 QList<QRegExp> nameFilters;
340#endif
341 // ### Qt 5: resolvedSymLinks goes away
342 QHash<QString, QString> resolvedSymLinks;
343
344 QFileSystemNode root;
345
346 QBasicTimer fetchingTimer;
347 struct Fetching {
348 QString dir;
349 QString file;
350 const QFileSystemNode *node;
351 };
352 QList<Fetching> toFetch;
353
354};
355#endif // QT_NO_FILESYSTEMMODEL
356
357QT_END_NAMESPACE
358
359#endif
360
Note: See TracBrowser for help on using the repository browser.