Qt 4.8
qfilesystemmodel_p.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
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 
73 
74 class ExtendedInformation;
76 class QFileIconProvider;
77 
79 {
81 
82 public:
84  {
85  public:
86  QFileSystemNode(const QString &filename = QString(), QFileSystemNode *p = 0)
87  : fileName(filename), populatedChildren(false), isVisible(false), dirtyChildrenIndex(-1), parent(p), info(0) {}
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 
100 #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
101  QString volumeName;
102 #endif
103 
104  inline qint64 size() const { if (info && !info->isDir()) return info->size(); return 0; }
105  inline QString type() const { if (info) return info->displayType; return QLatin1String(""); }
106  inline QDateTime lastModified() const { if (info) return info->lastModified(); return QDateTime(); }
107  inline QFile::Permissions permissions() const { if (info) return info->permissions(); return 0; }
108  inline bool isReadable() const { return ((permissions() & QFile::ReadUser) != 0); }
109  inline bool isWritable() const { return ((permissions() & QFile::WriteUser) != 0); }
110  inline bool isExecutable() const { return ((permissions() & QFile::ExeUser) != 0); }
111  inline bool isDir() const {
112  if (info)
113  return info->isDir();
114  if (children.count() > 0)
115  return true;
116  return false;
117  }
118  inline bool isFile() const { if (info) return info->isFile(); return true; }
119  inline bool isSystem() const { if (info) return info->isSystem(); return true; }
120  inline bool isHidden() const { if (info) return info->isHidden(); return false; }
121  inline bool isSymLink(bool ignoreNtfsSymLinks = false) const { return info && info->isSymLink(ignoreNtfsSymLinks); }
122  inline bool caseSensitive() const { if (info) return info->isCaseSensitive(); return false; }
123  inline QIcon icon() const { if (info) return info->icon; return QIcon(); }
124 
125  inline bool operator <(const QFileSystemNode &node) const {
126  if (caseSensitive() || node.caseSensitive())
127  return fileName < node.fileName;
128  return QString::compare(fileName, node.fileName, Qt::CaseInsensitive) < 0;
129  }
130  inline bool operator >(const QString &name) const {
131  if (caseSensitive())
132  return fileName > name;
133  return QString::compare(fileName, name, Qt::CaseInsensitive) > 0;
134  }
135  inline bool operator <(const QString &name) const {
136  if (caseSensitive())
137  return fileName < name;
138  return QString::compare(fileName, name, Qt::CaseInsensitive) < 0;
139  }
140  inline bool operator !=(const QExtendedInformation &fileInfo) const {
141  return !operator==(fileInfo);
142  }
143  bool operator ==(const QString &name) const {
144  if (caseSensitive())
145  return fileName == name;
146  return QString::compare(fileName, name, Qt::CaseInsensitive) == 0;
147  }
148  bool operator ==(const QExtendedInformation &fileInfo) const {
149  return info && (*info == fileInfo);
150  }
151 
152  inline bool hasInformation() const { return info != 0; }
153 
154  void populate(const QExtendedInformation &fileInfo) {
155  if (!info)
156  info = new QExtendedInformation(fileInfo.fileInfo());
157  (*info) = fileInfo;
158  }
159 
160  // children shouldn't normally be accessed directly, use node()
161  inline int visibleLocation(QString childName) {
162  return visibleChildren.indexOf(childName);
163  }
164  void updateIcon(QFileIconProvider *iconProvider, const QString &path) {
165  if (info)
166  info->icon = iconProvider->icon(QFileInfo(path));
168  for(iterator = children.constBegin() ; iterator != children.constEnd() ; ++iterator) {
169  //On windows the root (My computer) has no path so we don't want to add a / for nothing (e.g. /C:/)
170  if (!path.isEmpty()) {
171  if (path.endsWith(QLatin1Char('/')))
172  iterator.value()->updateIcon(iconProvider, path + iterator.value()->fileName);
173  else
174  iterator.value()->updateIcon(iconProvider, path + QLatin1Char('/') + iterator.value()->fileName);
175  } else
176  iterator.value()->updateIcon(iconProvider, iterator.value()->fileName);
177  }
178  }
179 
180  void retranslateStrings(QFileIconProvider *iconProvider, const QString &path) {
181  if (info)
182  info->displayType = iconProvider->type(QFileInfo(path));
184  for(iterator = children.constBegin() ; iterator != children.constEnd() ; ++iterator) {
185  //On windows the root (My computer) has no path so we don't want to add a / for nothing (e.g. /C:/)
186  if (!path.isEmpty()) {
187  if (path.endsWith(QLatin1Char('/')))
188  iterator.value()->retranslateStrings(iconProvider, path + iterator.value()->fileName);
189  else
190  iterator.value()->retranslateStrings(iconProvider, path + QLatin1Char('/') + iterator.value()->fileName);
191  } else
192  iterator.value()->retranslateStrings(iconProvider, iterator.value()->fileName);
193  }
194  }
195 
197  bool isVisible;
202 
203 
205 
206  };
207 
209  forceSort(true),
210  sortColumn(0),
211  sortOrder(Qt::AscendingOrder),
212  readOnly(true),
213  setRootPath(false),
214  filters(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs),
215  nameFilterDisables(true), // false on windows, true on mac and unix
216  disableRecursiveSort(false)
217  {
218  delayedSortTimer.setSingleShot(true);
219  }
220 
221  void init();
222  /*
223  \internal
224 
225  Return true if index which is owned by node is hidden by the filter.
226  */
227  inline bool isHiddenByFilter(QFileSystemNode *indexNode, const QModelIndex &index) const
228  {
229  return (indexNode != &root && !index.isValid());
230  }
231  QFileSystemNode *node(const QModelIndex &index) const;
232  QFileSystemNode *node(const QString &path, bool fetch = true) const;
233  inline QModelIndex index(const QString &path) { return index(node(path)); }
234  QModelIndex index(const QFileSystemNode *node) const;
235  bool filtersAcceptsNode(const QFileSystemNode *node) const;
236  bool passNameFilters(const QFileSystemNode *node) const;
237  void removeNode(QFileSystemNode *parentNode, const QString &name);
238  QFileSystemNode* addNode(QFileSystemNode *parentNode, const QString &fileName, const QFileInfo &info);
239  void addVisibleFiles(QFileSystemNode *parentNode, const QStringList &newFiles);
240  void removeVisibleFile(QFileSystemNode *parentNode, int visibleLocation);
241  void sortChildren(int column, const QModelIndex &parent);
242 
243  inline int translateVisibleLocation(QFileSystemNode *parent, int row) const {
244  if (sortOrder != Qt::AscendingOrder) {
245  if (parent->dirtyChildrenIndex == -1)
246  return parent->visibleChildren.count() - row - 1;
247 
248  if (row < parent->dirtyChildrenIndex)
249  return parent->dirtyChildrenIndex - row - 1;
250  }
251 
252  return row;
253  }
254 
255  inline static QString myComputer() {
256  // ### TODO We should query the system to find out what the string should be
257  // XP == "My Computer",
258  // Vista == "Computer",
259  // OS X == "Computer" (sometime user generated) "Benjamin's PowerBook G4"
260 #ifdef Q_OS_WIN
261  return QFileSystemModel::tr("My Computer");
262 #else
263  return QFileSystemModel::tr("Computer");
264 #endif
265  }
266 
267  inline void delayedSort() {
268  if (!delayedSortTimer.isActive())
269  delayedSortTimer.start(0);
270  }
271 
272  static bool caseInsensitiveLessThan(const QString &s1, const QString &s2)
273  {
274  return QString::compare(s1, s2, Qt::CaseInsensitive) < 0;
275  }
276 
278  {
280  }
281 
282  QIcon icon(const QModelIndex &index) const;
283  QString name(const QModelIndex &index) const;
284  QString displayName(const QModelIndex &index) const;
285  QString filePath(const QModelIndex &index) const;
286  QString size(const QModelIndex &index) const;
287  static QString size(qint64 bytes);
288  QString type(const QModelIndex &index) const;
289  QString time(const QModelIndex &index) const;
290 
291  void _q_directoryChanged(const QString &directory, const QStringList &list);
292  void _q_performDelayedSort();
293  void _q_fileSystemChanged(const QString &path, const QList<QPair<QString, QFileInfo> > &);
294  void _q_resolvedName(const QString &fileName, const QString &resolvedName);
295 
296  static int naturalCompare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs);
297 
299 #ifndef QT_NO_FILESYSTEMWATCHER
301 #endif
303  bool forceSort;
306  bool readOnly;
308  QDir::Filters filters;
311  //This flag is an optimization for the QFileDialog
312  //It enable a sort which is not recursive, it means
313  //we sort only what we see.
315 #ifndef QT_NO_REGEXP
317 #endif
318  // ### Qt 5: resolvedSymLinks goes away
320 
322 
324  struct Fetching {
328  };
330 
331 };
332 #endif // QT_NO_FILESYSTEMMODEL
333 
335 
336 #endif
337 
The QDir class provides access to directory structures and their contents.
Definition: qdir.h:58
void populate(const QExtendedInformation &fileInfo)
QHash< QString, QFileSystemNode * > children
The QHash::const_iterator class provides an STL-style const iterator for QHash and QMultiHash...
Definition: qhash.h:395
The QFileSystemModel class provides a data model for the local filesystem.
int type
Definition: qmetatype.cpp:239
QList< QRegExp > nameFilters
static bool caseInsensitiveLessThan(const QString &s1, const QString &s2)
static mach_timebase_info_data_t info
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static bool nodeCaseInsensitiveLessThan(const QFileSystemModelPrivate::QFileSystemNode &s1, const QFileSystemModelPrivate::QFileSystemNode &s2)
bool operator>(const QByteArray &a1, const QByteArray &a2)
Definition: qbytearray.h:551
virtual QIcon icon(IconType type) const
Returns an icon set for the given type.
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
bool operator!=(QBool b1, bool b2)
Definition: qglobal.h:2026
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
bool operator<(int priority, const QPair< QRunnable *, int > &p)
Definition: qthreadpool.cpp:50
virtual QString type(const QFileInfo &info) const
Returns the type of the file described by info.
SortOrder
Definition: qnamespace.h:189
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static bool init
const char * name
QFileInfo fileInfo() const
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
const T & value() const
Returns the current item&#39;s value.
Definition: qhash.h:420
The QFileIconProvider class provides file icons for the QDirModel and the QFileSystemModel classes...
__int64 qint64
Definition: qglobal.h:942
QHash< const QFileSystemNode *, bool > bypassFilters
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
CaseSensitivity
Definition: qnamespace.h:1451
static const char *const filters[3]
QModelIndex index(const QString &path)
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
int compare(const QString &s) const
Definition: qstring.cpp:5037
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
The QModelIndex class is used to locate data in a data model.
Definition: qnamespace.h:54
#define Q_AUTOTEST_EXPORT
Definition: qglobal.h:1510
QFileSystemNode(const QString &filename=QString(), QFileSystemNode *p=0)
quint16 index
bool isSymLink(bool ignoreNtfsSymLinks=false) const
QHash< QString, QString > resolvedSymLinks
The QBasicTimer class provides timer events for objects.
Definition: qbasictimer.h:55
int translateVisibleLocation(QFileSystemNode *parent, int row) const
bool isHiddenByFilter(QFileSystemNode *indexNode, const QModelIndex &index) const
The QTimer class provides repetitive and single-shot timers.
Definition: qtimer.h:56
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition: qstring.cpp:3796
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
static QString fileName(const QString &fileUrl)
bool operator==(QBool b1, bool b2)
Definition: qglobal.h:2023
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
void retranslateStrings(QFileIconProvider *iconProvider, const QString &path)
void updateIcon(QFileIconProvider *iconProvider, const QString &path)
QFileInfoGatherer fileInfoGatherer
The QIcon class provides scalable icons in different modes and states.
Definition: qicon.h:60