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