45
|
1 /****************************************************************************
|
|
2 **
|
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 ** Contact: Qt Software Information (qt-info@nokia.com)
|
|
5 **
|
|
6 ** This file is part of the demonstration applications 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 qt-sales@nokia.com.
|
|
38 ** $QT_END_LICENSE$
|
|
39 **
|
|
40 ****************************************************************************/
|
|
41
|
|
42 module xbel;
|
|
43
|
|
44 import QtCore.QXmlStreamReader;
|
|
45 import QtCore.QDateTime;
|
|
46
|
|
47
|
|
48 import QtCore.QFile;
|
|
49
|
|
50 class BookmarkNode
|
|
51 {
|
|
52 public:
|
|
53 enum Type {
|
|
54 Root,
|
|
55 Folder,
|
|
56 Bookmark,
|
|
57 Separator
|
|
58 };
|
|
59
|
|
60 this(Type type = Root, BookmarkNode *parent = null)
|
|
61 {
|
|
62 expanded = false;
|
|
63 m_parent = parent;
|
|
64 m_type = type;
|
|
65
|
|
66 if (parent)
|
|
67 parent.add(this);
|
|
68 }
|
|
69
|
|
70 ~this()
|
|
71 {
|
|
72 if (m_parent)
|
|
73 m_parent.remove(this);
|
|
74 qDeleteAll(m_children);
|
|
75 m_parent = 0;
|
|
76 m_type = BookmarkNode::Root;
|
|
77 }
|
|
78
|
|
79 bool operator==(const BookmarkNode &other)
|
|
80 {
|
|
81 if (url != other.url
|
|
82 || title != other.title
|
|
83 || desc != other.desc
|
|
84 || expanded != other.expanded
|
|
85 || m_type != other.m_type
|
|
86 || m_children.count() != other.m_children.count())
|
|
87 return false;
|
|
88
|
|
89 for (int i = 0; i < m_children.count(); ++i)
|
|
90 if (!((*(m_children[i])) == (*(other.m_children[i]))))
|
|
91 return false;
|
|
92 return true;
|
|
93 }
|
|
94
|
|
95 Type type() const
|
|
96 {
|
|
97 return m_type;
|
|
98 }
|
|
99
|
|
100 void setType(Type type)
|
|
101 {
|
|
102 m_type = type;
|
|
103 }
|
|
104
|
|
105 QList<BookmarkNode *> children() const
|
|
106 {
|
|
107 return m_children;
|
|
108 }
|
|
109
|
|
110
|
|
111 BookmarkNode *parent() const
|
|
112 {
|
|
113 return m_parent;
|
|
114 }
|
|
115
|
|
116 void add(BookmarkNode *child, int offset = -1)
|
|
117 {
|
|
118 Q_ASSERT(child.m_type != Root);
|
|
119 if (child.m_parent)
|
|
120 child.m_parent.remove(child);
|
|
121 child.m_parent = this;
|
|
122 if (-1 == offset)
|
|
123 offset = m_children.size();
|
|
124 m_children.insert(offset, child);
|
|
125 }
|
|
126
|
|
127 void remove(BookmarkNode *child)
|
|
128 {
|
|
129 child.m_parent = 0;
|
|
130 m_children.removeAll(child);
|
|
131 }
|
|
132
|
|
133 QString url;
|
|
134 QString title;
|
|
135 QString desc;
|
|
136 bool expanded;
|
|
137
|
|
138 private:
|
|
139 BookmarkNode *m_parent;
|
|
140 Type m_type;
|
|
141 QList<BookmarkNode *> m_children;
|
|
142
|
|
143 };
|
|
144
|
|
145 class XbelReader : public QXmlStreamReader
|
|
146 {
|
|
147 public:
|
|
148 this()
|
|
149 {
|
|
150 }
|
|
151
|
|
152 BookmarkNode *read(const QString &fileName)
|
|
153 {
|
|
154 QFile file(fileName);
|
|
155 if (!file.exists()) {
|
|
156 return new BookmarkNode(BookmarkNode::Root);
|
|
157 }
|
|
158 file.open(QFile::ReadOnly);
|
|
159 return read(&file);
|
|
160 }
|
|
161
|
|
162 BookmarkNode *read(QIODevice *device)
|
|
163 {
|
|
164 BookmarkNode *root = new BookmarkNode(BookmarkNode::Root);
|
|
165 setDevice(device);
|
|
166 while (!atEnd()) {
|
|
167 readNext();
|
|
168 if (isStartElement()) {
|
|
169 QString version = attributes().value(QLatin1String("version")).toString();
|
|
170 if (name() == QLatin1String("xbel")
|
|
171 && (version.isEmpty() || version == QLatin1String("1.0"))) {
|
|
172 readXBEL(root);
|
|
173 } else {
|
|
174 raiseError(QObject::tr("The file is not an XBEL version 1.0 file."));
|
|
175 }
|
|
176 }
|
|
177 }
|
|
178 return root;
|
|
179 }
|
|
180
|
|
181 private:
|
|
182 void skipUnknownElement()
|
|
183 {
|
|
184 Q_ASSERT(isStartElement());
|
|
185
|
|
186 while (!atEnd()) {
|
|
187 readNext();
|
|
188
|
|
189 if (isEndElement())
|
|
190 break;
|
|
191
|
|
192 if (isStartElement())
|
|
193 skipUnknownElement();
|
|
194 }
|
|
195 }
|
|
196
|
|
197 void readXBEL(BookmarkNode *parent)
|
|
198 {
|
|
199 Q_ASSERT(isStartElement() && name() == QLatin1String("xbel"));
|
|
200
|
|
201 while (!atEnd()) {
|
|
202 readNext();
|
|
203 if (isEndElement())
|
|
204 break;
|
|
205
|
|
206 if (isStartElement()) {
|
|
207 if (name() == QLatin1String("folder"))
|
|
208 readFolder(parent);
|
|
209 else if (name() == QLatin1String("bookmark"))
|
|
210 readBookmarkNode(parent);
|
|
211 else if (name() == QLatin1String("separator"))
|
|
212 readSeparator(parent);
|
|
213 else
|
|
214 skipUnknownElement();
|
|
215 }
|
|
216 }
|
|
217 }
|
|
218
|
|
219 void readTitle(BookmarkNode *parent)
|
|
220 {
|
|
221 Q_ASSERT(isStartElement() && name() == QLatin1String("title"));
|
|
222 parent.title = readElementText();
|
|
223 }
|
|
224
|
|
225 void readDescription(BookmarkNode *parent)
|
|
226 {
|
|
227 Q_ASSERT(isStartElement() && name() == QLatin1String("desc"));
|
|
228 parent.desc = readElementText();
|
|
229 }
|
|
230
|
|
231 void readSeparator(BookmarkNode *parent)
|
|
232 {
|
|
233 new BookmarkNode(BookmarkNode::Separator, parent);
|
|
234 // empty elements have a start and end element
|
|
235 readNext();
|
|
236 }
|
|
237
|
|
238
|
|
239 void readFolder(BookmarkNode *parent)
|
|
240 {
|
|
241 Q_ASSERT(isStartElement() && name() == QLatin1String("folder"));
|
|
242
|
|
243 BookmarkNode *folder = new BookmarkNode(BookmarkNode::Folder, parent);
|
|
244 folder.expanded = (attributes().value(QLatin1String("folded")) == QLatin1String("no"));
|
|
245
|
|
246 while (!atEnd()) {
|
|
247 readNext();
|
|
248
|
|
249 if (isEndElement())
|
|
250 break;
|
|
251
|
|
252 if (isStartElement()) {
|
|
253 if (name() == QLatin1String("title"))
|
|
254 readTitle(folder);
|
|
255 else if (name() == QLatin1String("desc"))
|
|
256 readDescription(folder);
|
|
257 else if (name() == QLatin1String("folder"))
|
|
258 readFolder(folder);
|
|
259 else if (name() == QLatin1String("bookmark"))
|
|
260 readBookmarkNode(folder);
|
|
261 else if (name() == QLatin1String("separator"))
|
|
262 readSeparator(folder);
|
|
263 else
|
|
264 skipUnknownElement();
|
|
265 }
|
|
266 }
|
|
267 }
|
|
268
|
|
269 void readBookmarkNode(BookmarkNode *parent)
|
|
270 {
|
|
271 Q_ASSERT(isStartElement() && name() == QLatin1String("bookmark"));
|
|
272 BookmarkNode *bookmark = new BookmarkNode(BookmarkNode::Bookmark, parent);
|
|
273 bookmark.url = attributes().value(QLatin1String("href")).toString();
|
|
274 while (!atEnd()) {
|
|
275 readNext();
|
|
276 if (isEndElement())
|
|
277 break;
|
|
278
|
|
279 if (isStartElement()) {
|
|
280 if (name() == QLatin1String("title"))
|
|
281 readTitle(bookmark);
|
|
282 else if (name() == QLatin1String("desc"))
|
|
283 readDescription(bookmark);
|
|
284 else
|
|
285 skipUnknownElement();
|
|
286 }
|
|
287 }
|
|
288 if (bookmark.title.isEmpty())
|
|
289 bookmark.title = QObject::tr("Unknown title");
|
|
290 }
|
|
291
|
|
292 };
|
|
293
|
|
294 import QtCore.QXmlStreamWriter;
|
|
295
|
|
296 class XbelWriter : public QXmlStreamWriter
|
|
297 {
|
|
298 public:
|
|
299 this()
|
|
300 {
|
|
301 setAutoFormatting(true);
|
|
302 }
|
|
303 bool write(const QString &fileName, const BookmarkNode *root);
|
|
304 {
|
|
305 QFile file(fileName);
|
|
306 if (!root || !file.open(QFile::WriteOnly))
|
|
307 return false;
|
|
308 return write(&file, root);
|
|
309 }
|
|
310
|
|
311 bool write(QIODevice *device, const BookmarkNode *root);
|
|
312 {
|
|
313 setDevice(device);
|
|
314
|
|
315 writeStartDocument();
|
|
316 writeDTD(QLatin1String("<!DOCTYPE xbel>"));
|
|
317 writeStartElement(QLatin1String("xbel"));
|
|
318 writeAttribute(QLatin1String("version"), QLatin1String("1.0"));
|
|
319 if (root.type() == BookmarkNode::Root) {
|
|
320 for (int i = 0; i < root.children().count(); ++i)
|
|
321 writeItem(root.children().at(i));
|
|
322 } else {
|
|
323 writeItem(root);
|
|
324 }
|
|
325
|
|
326 writeEndDocument();
|
|
327 return true;
|
|
328 }
|
|
329
|
|
330
|
|
331 private:
|
|
332 void writeItem(const BookmarkNode *parent)
|
|
333 {
|
|
334 switch (parent.type()) {
|
|
335 case BookmarkNode::Folder:
|
|
336 writeStartElement(QLatin1String("folder"));
|
|
337 writeAttribute(QLatin1String("folded"), parent.expanded ? QLatin1String("no") : QLatin1String("yes"));
|
|
338 writeTextElement(QLatin1String("title"), parent.title);
|
|
339 for (int i = 0; i < parent.children().count(); ++i)
|
|
340 writeItem(parent.children().at(i));
|
|
341 writeEndElement();
|
|
342 break;
|
|
343 case BookmarkNode::Bookmark:
|
|
344 writeStartElement(QLatin1String("bookmark"));
|
|
345 if (!parent.url.isEmpty())
|
|
346 writeAttribute(QLatin1String("href"), parent.url);
|
|
347 writeTextElement(QLatin1String("title"), parent.title);
|
|
348 if (!parent.desc.isEmpty())
|
|
349 writeAttribute(QLatin1String("desc"), parent.desc);
|
|
350 writeEndElement();
|
|
351 break;
|
|
352 case BookmarkNode::Separator:
|
|
353 writeEmptyElement(QLatin1String("separator"));
|
|
354 break;
|
|
355 default:
|
|
356 break;
|
|
357 }
|
|
358 }
|
|
359 }
|