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
|
94
|
45 import qt.xml.QXmlStreamReader;
|
|
46
|
73
|
47 import qt.core.QDateTime;
|
|
48 import qt.core.QFile;
|
45
|
49
|
|
50
|
|
51 class BookmarkNode
|
|
52 {
|
|
53 public:
|
73
|
54
|
|
55 enum Type {
|
74
|
56 Root,
|
|
57 Folder,
|
|
58 Bookmark,
|
|
59 Separator
|
73
|
60 };
|
45
|
61
|
94
|
62 this(Type type = Type.Root, BookmarkNode parent = null)
|
73
|
63 {
|
|
64 expanded = false;
|
|
65 m_parent = parent;
|
|
66 m_type = type;
|
|
67
|
|
68 if (parent)
|
|
69 parent.add(this);
|
|
70 }
|
|
71
|
|
72 ~this()
|
|
73 {
|
|
74 if (m_parent)
|
|
75 m_parent.remove(this);
|
|
76 qDeleteAll(m_children);
|
|
77 m_parent = 0;
|
|
78 m_type = BookmarkNode.Root;
|
|
79 }
|
45
|
80
|
80
|
81 int opEquals(BookmarkNode other)
|
73
|
82 {
|
|
83 if (url != other.url || title != other.title || desc != other.desc || expanded != other.expanded
|
77
|
84 || m_type != other.m_type || m_children.length != other.m_children.length)
|
80
|
85 return cast(int) false;
|
73
|
86
|
77
|
87 for (int i = 0; i < m_children.length; ++i)
|
73
|
88 if (!((*(m_children[i])) == (*(other.m_children[i]))))
|
80
|
89 return cast(int) false;
|
|
90 return cast(int) true;
|
73
|
91 }
|
45
|
92
|
74
|
93 Type type()
|
73
|
94 {
|
|
95 return m_type;
|
|
96 }
|
45
|
97
|
73
|
98 void setType(Type type)
|
|
99 {
|
|
100 m_type = type;
|
|
101 }
|
45
|
102
|
74
|
103 BookmarkNode[] children()
|
73
|
104 {
|
|
105 return m_children;
|
|
106 }
|
45
|
107
|
77
|
108 BookmarkNode parent()
|
73
|
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)
|
77
|
120 offset = m_children.length;
|
73
|
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
|
77
|
130 string url;
|
|
131 string title;
|
|
132 string desc;
|
73
|
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
|
77
|
150 BookmarkNode read(string fileName)
|
73
|
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()) {
|
80
|
167 string version_ = attributes().value("version").toString();
|
|
168 if (name() == "xbel" && (version_.isEmpty() || version_ == "1.0")) {
|
73
|
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 {
|
80
|
197 assert(isStartElement() && name() == "xbel");
|
73
|
198
|
|
199 while (!atEnd()) {
|
|
200 readNext();
|
|
201 if (isEndElement())
|
|
202 break;
|
45
|
203
|
73
|
204 if (isStartElement()) {
|
80
|
205 if (name() == "folder")
|
73
|
206 readFolder(parent);
|
85
|
207 else if (name() == "bookmark")
|
73
|
208 readBookmarkNode(parent);
|
85
|
209 else if (name() == "separator")
|
73
|
210 readSeparator(parent);
|
|
211 else
|
|
212 skipUnknownElement();
|
|
213 }
|
|
214 }
|
|
215 }
|
45
|
216
|
73
|
217 void readTitle(BookmarkNode parent)
|
|
218 {
|
80
|
219 assert(isStartElement() && name() == "title");
|
73
|
220 parent.title = readElementText();
|
|
221 }
|
|
222
|
|
223 void readDescription(BookmarkNode parent)
|
|
224 {
|
80
|
225 assert(isStartElement() && name() == "desc");
|
73
|
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 {
|
80
|
239 assert(isStartElement() && name() == "folder");
|
73
|
240
|
|
241 BookmarkNode folder = new BookmarkNode(BookmarkNode.Folder, parent);
|
80
|
242 folder.expanded = (attributes().value("folded") == "no");
|
73
|
243
|
|
244 while (!atEnd()) {
|
|
245 readNext();
|
|
246
|
|
247 if (isEndElement())
|
|
248 break;
|
45
|
249
|
73
|
250 if (isStartElement()) {
|
80
|
251 if (name() == "title")
|
73
|
252 readTitle(folder);
|
80
|
253 else if (name() == "desc")
|
73
|
254 readDescription(folder);
|
80
|
255 else if (name() == "folder")
|
73
|
256 readFolder(folder);
|
80
|
257 else if (name() == "bookmark")
|
73
|
258 readBookmarkNode(folder);
|
80
|
259 else if (name() == "separator")
|
73
|
260 readSeparator(folder);
|
|
261 else
|
|
262 skipUnknownElement();
|
|
263 }
|
|
264 }
|
|
265 }
|
|
266
|
|
267 void readBookmarkNode(BookmarkNode parent)
|
|
268 {
|
80
|
269 assert(isStartElement() && name() == "bookmark");
|
73
|
270 BookmarkNode bookmark = new BookmarkNode(BookmarkNode.Bookmark, parent);
|
80
|
271 bookmark.url = attributes().value("href").toString();
|
73
|
272 while (!atEnd()) {
|
|
273 readNext();
|
|
274 if (isEndElement())
|
|
275 break;
|
|
276
|
|
277 if (isStartElement()) {
|
80
|
278 if (name() == "title")
|
73
|
279 readTitle(bookmark);
|
80
|
280 else if (name() == "desc")
|
73
|
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
|
94
|
291 import qt.xml.QXmlStreamWriter;
|
45
|
292
|
|
293 class XbelWriter : public QXmlStreamWriter
|
|
294 {
|
|
295 public:
|
|
296
|
73
|
297 this()
|
|
298 {
|
|
299 setAutoFormatting(true);
|
|
300 }
|
|
301
|
77
|
302 bool write(string fileName, BookmarkNode root)
|
73
|
303 {
|
77
|
304 auto file = new QFile(fileName);
|
73
|
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();
|
80
|
315 writeDTD("<!DOCTYPE xbel>");
|
|
316 writeStartElement("xbel");
|
|
317 writeAttribute("version", "1.0");
|
73
|
318 if (root.type() == BookmarkNode.Root) {
|
77
|
319 for (int i = 0; i < root.children().length; ++i)
|
|
320 writeItem(root.children()[i]);
|
73
|
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:
|
80
|
335 writeStartElement("folder");
|
|
336 writeAttribute("folded", parent.expanded ? "no" : "yes");
|
|
337 writeTextElement("title", parent.title);
|
73
|
338 for (int i = 0; i < parent.children().count(); ++i)
|
77
|
339 writeItem(parent.children()[i]);
|
73
|
340 writeEndElement();
|
|
341 break;
|
|
342 case BookmarkNode.Bookmark:
|
80
|
343 writeStartElement("bookmark");
|
73
|
344 if (!parent.url.isEmpty())
|
80
|
345 writeAttribute("href", parent.url);
|
|
346 writeTextElement("title", parent.title);
|
73
|
347 if (!parent.desc.isEmpty())
|
80
|
348 writeAttribute("desc", parent.desc);
|
73
|
349 writeEndElement();
|
|
350 break;
|
|
351 case BookmarkNode.Separator:
|
80
|
352 writeEmptyElement("separator");
|
73
|
353 break;
|
|
354 default:
|
|
355 break;
|
|
356 }
|
|
357 }
|
45
|
358 }
|