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 {
|
74
|
55 Root,
|
|
56 Folder,
|
|
57 Bookmark,
|
|
58 Separator
|
73
|
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
|
74
|
92 Type type()
|
73
|
93 {
|
|
94 return m_type;
|
|
95 }
|
45
|
96
|
73
|
97 void setType(Type type)
|
|
98 {
|
|
99 m_type = type;
|
|
100 }
|
45
|
101
|
74
|
102 BookmarkNode[] children()
|
73
|
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;
|
74
|
139 BookmarkNode[] m_children;
|
73
|
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 {
|
74
|
152 auto file = new QFile(fileName);
|
73
|
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:
|
74
|
179
|
73
|
180 void skipUnknownElement()
|
|
181 {
|
|
182 assert(isStartElement());
|
|
183
|
|
184 while (!atEnd()) {
|
|
185 readNext();
|
|
186
|
|
187 if (isEndElement())
|
|
188 break;
|
45
|
189
|
73
|
190 if (isStartElement())
|
|
191 skipUnknownElement();
|
|
192 }
|
|
193 }
|
|
194
|
|
195 void readXBEL(BookmarkNode parent)
|
|
196 {
|
|
197 assert(isStartElement() && name() == QLatin1String("xbel"));
|
|
198
|
|
199 while (!atEnd()) {
|
|
200 readNext();
|
|
201 if (isEndElement())
|
|
202 break;
|
45
|
203
|
73
|
204 if (isStartElement()) {
|
|
205 if (name() == QLatin1String("folder"))
|
|
206 readFolder(parent);
|
|
207 else if (name() == QLatin1String("bookmark"))
|
|
208 readBookmarkNode(parent);
|
|
209 else if (name() == QLatin1String("separator"))
|
|
210 readSeparator(parent);
|
|
211 else
|
|
212 skipUnknownElement();
|
|
213 }
|
|
214 }
|
|
215 }
|
45
|
216
|
73
|
217 void readTitle(BookmarkNode parent)
|
|
218 {
|
|
219 assert(isStartElement() && name() == QLatin1String("title"));
|
|
220 parent.title = readElementText();
|
|
221 }
|
|
222
|
|
223 void readDescription(BookmarkNode parent)
|
|
224 {
|
|
225 assert(isStartElement() && name() == QLatin1String("desc"));
|
|
226 parent.desc = readElementText();
|
|
227 }
|
45
|
228
|
73
|
229 void readSeparator(BookmarkNode parent)
|
|
230 {
|
|
231 new BookmarkNode(BookmarkNode.Separator, parent);
|
|
232 // empty elements have a start and end element
|
|
233 readNext();
|
|
234 }
|
|
235
|
45
|
236
|
73
|
237 void readFolder(BookmarkNode parent)
|
|
238 {
|
|
239 assert(isStartElement() && name() == QLatin1String("folder"));
|
|
240
|
|
241 BookmarkNode folder = new BookmarkNode(BookmarkNode.Folder, parent);
|
|
242 folder.expanded = (attributes().value(QLatin1String("folded")) == QLatin1String("no"));
|
|
243
|
|
244 while (!atEnd()) {
|
|
245 readNext();
|
|
246
|
|
247 if (isEndElement())
|
|
248 break;
|
45
|
249
|
73
|
250 if (isStartElement()) {
|
|
251 if (name() == QLatin1String("title"))
|
|
252 readTitle(folder);
|
|
253 else if (name() == QLatin1String("desc"))
|
|
254 readDescription(folder);
|
|
255 else if (name() == QLatin1String("folder"))
|
|
256 readFolder(folder);
|
|
257 else if (name() == QLatin1String("bookmark"))
|
|
258 readBookmarkNode(folder);
|
|
259 else if (name() == QLatin1String("separator"))
|
|
260 readSeparator(folder);
|
|
261 else
|
|
262 skipUnknownElement();
|
|
263 }
|
|
264 }
|
|
265 }
|
|
266
|
|
267 void readBookmarkNode(BookmarkNode parent)
|
|
268 {
|
|
269 assert(isStartElement() && name() == QLatin1String("bookmark"));
|
|
270 BookmarkNode bookmark = new BookmarkNode(BookmarkNode.Bookmark, parent);
|
|
271 bookmark.url = attributes().value(QLatin1String("href")).toString();
|
|
272 while (!atEnd()) {
|
|
273 readNext();
|
|
274 if (isEndElement())
|
|
275 break;
|
|
276
|
|
277 if (isStartElement()) {
|
|
278 if (name() == QLatin1String("title"))
|
|
279 readTitle(bookmark);
|
|
280 else if (name() == QLatin1String("desc"))
|
|
281 readDescription(bookmark);
|
|
282 else
|
|
283 skipUnknownElement();
|
|
284 }
|
|
285 }
|
|
286 if (bookmark.title.isEmpty())
|
|
287 bookmark.title = QObject.tr("Unknown title");
|
|
288 }
|
45
|
289 }
|
|
290
|
73
|
291 import qt.core.QXmlStreamWriter;
|
45
|
292
|
|
293 class XbelWriter : public QXmlStreamWriter
|
|
294 {
|
|
295 public:
|
|
296
|
73
|
297 this()
|
|
298 {
|
|
299 setAutoFormatting(true);
|
|
300 }
|
|
301
|
74
|
302 bool write(QString fileName, BookmarkNode root)
|
73
|
303 {
|
|
304 QFile file(fileName);
|
|
305 if (!root || !file.open(QFile.WriteOnly))
|
|
306 return false;
|
|
307 return write(&file, root);
|
|
308 }
|
45
|
309
|
74
|
310 bool write(QIODevice device, BookmarkNode root)
|
73
|
311 {
|
|
312 setDevice(device);
|
45
|
313
|
73
|
314 writeStartDocument();
|
|
315 writeDTD(QLatin1String("<!DOCTYPE xbel>"));
|
|
316 writeStartElement(QLatin1String("xbel"));
|
|
317 writeAttribute(QLatin1String("version"), QLatin1String("1.0"));
|
|
318 if (root.type() == BookmarkNode.Root) {
|
|
319 for (int i = 0; i < root.children().count(); ++i)
|
|
320 writeItem(root.children().at(i));
|
|
321 } else {
|
|
322 writeItem(root);
|
|
323 }
|
45
|
324
|
73
|
325 writeEndDocument();
|
|
326 return true;
|
|
327 }
|
45
|
328
|
|
329 private:
|
73
|
330
|
|
331 void writeItem(BookmarkNode parent)
|
|
332 {
|
|
333 switch (parent.type()) {
|
|
334 case BookmarkNode.Folder:
|
|
335 writeStartElement(QLatin1String("folder"));
|
|
336 writeAttribute(QLatin1String("folded"), parent.expanded ? QLatin1String("no") : QLatin1String("yes"));
|
|
337 writeTextElement(QLatin1String("title"), parent.title);
|
|
338 for (int i = 0; i < parent.children().count(); ++i)
|
|
339 writeItem(parent.children().at(i));
|
|
340 writeEndElement();
|
|
341 break;
|
|
342 case BookmarkNode.Bookmark:
|
|
343 writeStartElement(QLatin1String("bookmark"));
|
|
344 if (!parent.url.isEmpty())
|
|
345 writeAttribute(QLatin1String("href"), parent.url);
|
|
346 writeTextElement(QLatin1String("title"), parent.title);
|
|
347 if (!parent.desc.isEmpty())
|
|
348 writeAttribute(QLatin1String("desc"), parent.desc);
|
|
349 writeEndElement();
|
|
350 break;
|
|
351 case BookmarkNode.Separator:
|
|
352 writeEmptyElement(QLatin1String("separator"));
|
|
353 break;
|
|
354 default:
|
|
355 break;
|
|
356 }
|
|
357 }
|
45
|
358 }
|