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 examples 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 | #include <QtCore/QUrl>
|
---|
43 | #include <QtCore/QVariant>
|
---|
44 | #include <QtXmlPatterns/QXmlNamePool>
|
---|
45 | #include "filetree.h"
|
---|
46 |
|
---|
47 | /*
|
---|
48 | The model has two types of nodes: elements & attributes.
|
---|
49 |
|
---|
50 | <directory name="">
|
---|
51 | <file name="">
|
---|
52 | </file>
|
---|
53 | </directory>
|
---|
54 |
|
---|
55 | In QXmlNodeModelIndex we store two values. QXmlNodeIndex::data()
|
---|
56 | is treated as a signed int, and it is an index into m_fileInfos
|
---|
57 | unless it is -1, in which case it has no meaning and the value
|
---|
58 | of QXmlNodeModelIndex::additionalData() is a Type name instead.
|
---|
59 | */
|
---|
60 |
|
---|
61 | /*!
|
---|
62 | The constructor passes \a pool to the base class, then loads an
|
---|
63 | internal vector with an instance of QXmlName for each of the
|
---|
64 | strings "file", "directory", "fileName", "filePath", "size",
|
---|
65 | "mimeType", and "suffix".
|
---|
66 | */
|
---|
67 | //! [2]
|
---|
68 | FileTree::FileTree(const QXmlNamePool& pool)
|
---|
69 | : QSimpleXmlNodeModel(pool),
|
---|
70 | m_filterAllowAll(QDir::AllEntries |
|
---|
71 | QDir::AllDirs |
|
---|
72 | QDir::NoDotAndDotDot |
|
---|
73 | QDir::Hidden),
|
---|
74 | m_sortFlags(QDir::Name)
|
---|
75 | {
|
---|
76 | QXmlNamePool np = namePool();
|
---|
77 | m_names.resize(7);
|
---|
78 | m_names[File] = QXmlName(np, QLatin1String("file"));
|
---|
79 | m_names[Directory] = QXmlName(np, QLatin1String("directory"));
|
---|
80 | m_names[AttributeFileName] = QXmlName(np, QLatin1String("fileName"));
|
---|
81 | m_names[AttributeFilePath] = QXmlName(np, QLatin1String("filePath"));
|
---|
82 | m_names[AttributeSize] = QXmlName(np, QLatin1String("size"));
|
---|
83 | m_names[AttributeMIMEType] = QXmlName(np, QLatin1String("mimeType"));
|
---|
84 | m_names[AttributeSuffix] = QXmlName(np, QLatin1String("suffix"));
|
---|
85 | }
|
---|
86 | //! [2]
|
---|
87 |
|
---|
88 | /*!
|
---|
89 | Returns the QXmlNodeModelIndex for the model node representing
|
---|
90 | the directory \a dirName.
|
---|
91 |
|
---|
92 | It calls QDir::cleanPath(), because an instance of QFileInfo
|
---|
93 | constructed for a path ending in '/' will return the empty string in
|
---|
94 | fileName(), instead of the directory name.
|
---|
95 | */
|
---|
96 | QXmlNodeModelIndex FileTree::nodeFor(const QString& dirName) const
|
---|
97 | {
|
---|
98 | QFileInfo dirInfo(QDir::cleanPath(dirName));
|
---|
99 | Q_ASSERT(dirInfo.exists());
|
---|
100 | return toNodeIndex(dirInfo);
|
---|
101 | }
|
---|
102 |
|
---|
103 | /*!
|
---|
104 | Since the value will always be in m_fileInfos, it is safe for
|
---|
105 | us to return a const reference to it.
|
---|
106 | */
|
---|
107 | //! [6]
|
---|
108 | const QFileInfo&
|
---|
109 | FileTree::toFileInfo(const QXmlNodeModelIndex &nodeIndex) const
|
---|
110 | {
|
---|
111 | return m_fileInfos.at(nodeIndex.data());
|
---|
112 | }
|
---|
113 | //! [6]
|
---|
114 |
|
---|
115 | /*!
|
---|
116 | Returns the model node index for the node specified by the
|
---|
117 | QFileInfo and node Type.
|
---|
118 | */
|
---|
119 | //! [1]
|
---|
120 | QXmlNodeModelIndex
|
---|
121 | FileTree::toNodeIndex(const QFileInfo &fileInfo, Type attributeName) const
|
---|
122 | {
|
---|
123 | const int indexOf = m_fileInfos.indexOf(fileInfo);
|
---|
124 |
|
---|
125 | if (indexOf == -1) {
|
---|
126 | m_fileInfos.append(fileInfo);
|
---|
127 | return createIndex(m_fileInfos.count()-1, attributeName);
|
---|
128 | }
|
---|
129 | else
|
---|
130 | return createIndex(indexOf, attributeName);
|
---|
131 | }
|
---|
132 | //! [1]
|
---|
133 |
|
---|
134 | /*!
|
---|
135 | Returns the model node index for the node specified by the
|
---|
136 | QFileInfo, which must be a Type::File or Type::Directory.
|
---|
137 | */
|
---|
138 | //! [0]
|
---|
139 | QXmlNodeModelIndex FileTree::toNodeIndex(const QFileInfo &fileInfo) const
|
---|
140 | {
|
---|
141 | return toNodeIndex(fileInfo, fileInfo.isDir() ? Directory : File);
|
---|
142 | }
|
---|
143 | //! [0]
|
---|
144 |
|
---|
145 | /*!
|
---|
146 | This private helper function is only called by nextFromSimpleAxis().
|
---|
147 | It is called whenever nextFromSimpleAxis() is called with an axis
|
---|
148 | parameter of either \c{PreviousSibling} or \c{NextSibling}.
|
---|
149 | */
|
---|
150 | //! [5]
|
---|
151 | QXmlNodeModelIndex FileTree::nextSibling(const QXmlNodeModelIndex &nodeIndex,
|
---|
152 | const QFileInfo &fileInfo,
|
---|
153 | qint8 offset) const
|
---|
154 | {
|
---|
155 | Q_ASSERT(offset == -1 || offset == 1);
|
---|
156 |
|
---|
157 | // Get the context node's parent.
|
---|
158 | const QXmlNodeModelIndex parent(nextFromSimpleAxis(Parent, nodeIndex));
|
---|
159 |
|
---|
160 | if (parent.isNull())
|
---|
161 | return QXmlNodeModelIndex();
|
---|
162 |
|
---|
163 | // Get the parent's child list.
|
---|
164 | const QFileInfo parentFI(toFileInfo(parent));
|
---|
165 | Q_ASSERT(Type(parent.additionalData()) == Directory);
|
---|
166 | const QFileInfoList siblings(QDir(parentFI.absoluteFilePath()).entryInfoList(QStringList(),
|
---|
167 | m_filterAllowAll,
|
---|
168 | m_sortFlags));
|
---|
169 | Q_ASSERT_X(!siblings.isEmpty(), Q_FUNC_INFO, "Can't happen! We started at a child.");
|
---|
170 |
|
---|
171 | // Find the index of the child where we started.
|
---|
172 | const int indexOfMe = siblings.indexOf(fileInfo);
|
---|
173 |
|
---|
174 | // Apply the offset.
|
---|
175 | const int siblingIndex = indexOfMe + offset;
|
---|
176 | if (siblingIndex < 0 || siblingIndex > siblings.count() - 1)
|
---|
177 | return QXmlNodeModelIndex();
|
---|
178 | else
|
---|
179 | return toNodeIndex(siblings.at(siblingIndex));
|
---|
180 | }
|
---|
181 | //! [5]
|
---|
182 |
|
---|
183 | /*!
|
---|
184 | This function is called by the QtXmlPatterns query engine when it
|
---|
185 | wants to move to the next node in the model. It moves along an \a
|
---|
186 | axis, \e from the node specified by \a nodeIndex.
|
---|
187 |
|
---|
188 | This function is usually the one that requires the most design and
|
---|
189 | implementation work, because the implementation depends on the
|
---|
190 | perhaps unique structure of your non-XML data.
|
---|
191 |
|
---|
192 | There are \l {QAbstractXmlNodeModel::SimpleAxis} {four values} for
|
---|
193 | \a axis that the implementation must handle, but there are really
|
---|
194 | only two axes, i.e., vertical and horizontal. Two of the four values
|
---|
195 | specify direction on the vertical axis (\c{Parent} and
|
---|
196 | \c{FirstChild}), and the other two values specify direction on the
|
---|
197 | horizontal axis (\c{PreviousSibling} and \c{NextSibling}).
|
---|
198 |
|
---|
199 | The typical implementation will be a \c switch statement with
|
---|
200 | a case for each of the four \a axis values.
|
---|
201 | */
|
---|
202 | //! [4]
|
---|
203 | QXmlNodeModelIndex
|
---|
204 | FileTree::nextFromSimpleAxis(SimpleAxis axis, const QXmlNodeModelIndex &nodeIndex) const
|
---|
205 | {
|
---|
206 | const QFileInfo fi(toFileInfo(nodeIndex));
|
---|
207 | const Type type = Type(nodeIndex.additionalData());
|
---|
208 |
|
---|
209 | if (type != File && type != Directory) {
|
---|
210 | Q_ASSERT_X(axis == Parent, Q_FUNC_INFO, "An attribute only has a parent!");
|
---|
211 | return toNodeIndex(fi, Directory);
|
---|
212 | }
|
---|
213 |
|
---|
214 | switch (axis) {
|
---|
215 | case Parent:
|
---|
216 | return toNodeIndex(QFileInfo(fi.path()), Directory);
|
---|
217 |
|
---|
218 | case FirstChild:
|
---|
219 | {
|
---|
220 | if (type == File) // A file has no children.
|
---|
221 | return QXmlNodeModelIndex();
|
---|
222 | else {
|
---|
223 | Q_ASSERT(type == Directory);
|
---|
224 | Q_ASSERT_X(fi.isDir(), Q_FUNC_INFO, "It isn't really a directory!");
|
---|
225 | const QDir dir(fi.absoluteFilePath());
|
---|
226 | Q_ASSERT(dir.exists());
|
---|
227 |
|
---|
228 | const QFileInfoList children(dir.entryInfoList(QStringList(),
|
---|
229 | m_filterAllowAll,
|
---|
230 | m_sortFlags));
|
---|
231 | if (children.isEmpty())
|
---|
232 | return QXmlNodeModelIndex();
|
---|
233 | const QFileInfo firstChild(children.first());
|
---|
234 | return toNodeIndex(firstChild);
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | case PreviousSibling:
|
---|
239 | return nextSibling(nodeIndex, fi, -1);
|
---|
240 |
|
---|
241 | case NextSibling:
|
---|
242 | return nextSibling(nodeIndex, fi, 1);
|
---|
243 | }
|
---|
244 |
|
---|
245 | Q_ASSERT_X(false, Q_FUNC_INFO, "Don't ever get here!");
|
---|
246 | return QXmlNodeModelIndex();
|
---|
247 | }
|
---|
248 | //! [4]
|
---|
249 |
|
---|
250 | /*!
|
---|
251 | No matter what part of the file system we model (the whole file
|
---|
252 | tree or a subtree), \a node will always have \c{file:///} as
|
---|
253 | the document URI.
|
---|
254 | */
|
---|
255 | QUrl FileTree::documentUri(const QXmlNodeModelIndex &node) const
|
---|
256 | {
|
---|
257 | Q_UNUSED(node);
|
---|
258 | return QUrl("file:///");
|
---|
259 | }
|
---|
260 |
|
---|
261 | /*!
|
---|
262 | This function returns QXmlNodeModelIndex::Element if \a node
|
---|
263 | is a directory or a file, and QXmlNodeModelIndex::Attribute
|
---|
264 | otherwise.
|
---|
|
---|