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
|
80
|
80 int opEquals(BookmarkNode other)
|
73
|
81 {
|
|
82 if (url != other.url || title != other.title || desc != other.desc || expanded != other.expanded
|
77
|
83 || m_type != other.m_type || m_children.length != other.m_children.length)
|
80
|
84 return cast(int) false;
|
73
|
85
|
77
|
86 for (int i = 0; i < m_children.length; ++i)
|
73
|
87 if (!((*(m_children[i])) == (*(other.m_children[i]))))
|
80
|
88 return cast(int) false;
|
|
89 return cast(int) true;
|
73
|
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
|
77
|
107 BookmarkNode parent()
|
73
|
108 {
|
|
109 return m_parent;
|
|
110 }
|
45
|
111
|
73
|
112 void add(BookmarkNode child, int offset = -1)
|
|
113 {
|
|
114 assert(child.m_type != Root);
|
|
115 if (child.m_parent)
|
|
116 child.m_parent.remove(child);
|
|
117 child.m_parent = this;
|
|
118 if (-1 == offset)
|
77
|
119 offset = m_children.length;
|
73
|
120 m_children.insert(offset, child);
|
|
121 }
|
45
|
122
|
73
|
123 void remove(BookmarkNode child)
|
|
124 {
|
|
125 child.m_parent = 0;
|
|
126 m_children.removeAll(child);
|
|
127 }
|
45
|
128
|
77
|
129 string url;
|
|
130 string title;
|
|
131 string desc;
|
73
|
132 bool expanded;
|
45
|
133
|
|
134 private:
|
|
135
|
73
|
136 BookmarkNode m_parent;
|
|
137 Type m_type;
|
74
|
138 BookmarkNode[] m_children;
|
73
|
139 }
|
45
|
140
|
|
141 class XbelReader : public QXmlStreamReader
|
|
142 {
|
|
143 public:
|
73
|
144
|
|
145 this()
|
|
146 {
|
|
147 }
|
45
|
148
|
77
|
149 BookmarkNode read(string fileName)
|
73
|
150 {
|
74
|
151 auto file = new QFile(fileName);
|
73
|
152 if (!file.exists()) {
|
|
153 return new BookmarkNode(BookmarkNode.Root);
|
|
154 }
|
|
155 file.open(QFile.ReadOnly);
|
|
156 return read(&file);
|
|
157 }
|
45
|
158
|
73
|
159 BookmarkNode read(QIODevice device)
|
|
160 {
|
|
161 BookmarkNode root = new BookmarkNode(BookmarkNode.Root);
|
|
162 setDevice(device);
|
|
163 while (!atEnd()) {
|
|
164 readNext();
|
|
165 if (isStartElement()) {
|
80
|
166 string version_ = attributes().value("version").toString();
|
|
167 if (name() == "xbel" && (version_.isEmpty() || version_ == "1.0")) {
|
73
|
168 readXBEL(root);
|
|
169 } else {
|
|
170 raiseError(QObject.tr("The file is not an XBEL version 1.0 file."));
|
|
171 }
|
|
172 }
|
|
173 }
|
|
174 return root;
|
|
175 }
|
45
|
176
|
|
177 private:
|
74
|
178
|
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 {
|
80
|
196 assert(isStartElement() && name() == "xbel");
|
73
|
197
|
|
198 while (!atEnd()) {
|
|
199 readNext();
|
|
200 if (isEndElement())
|
|
201 break;
|
45
|
202
|
73
|
203 if (isStartElement()) {
|
80
|
204 if (name() == "folder")
|
73
|
205 readFolder(parent);
|
85
|
206 else if (name() == "bookmark")
|
73
|
207 readBookmarkNode(parent);
|
85
|
208 else if (name() == "separator")
|
73
|
209 readSeparator(parent);
|
|
210 else
|
|
211 skipUnknownElement();
|
|
212 }
|
|
213 }
|
|
214 }
|
45
|
215
|
73
|
216 void readTitle(BookmarkNode parent)
|
|
217 {
|
80
|
218 assert(isStartElement() && name() == "title");
|
73
|
219 parent.title = readElementText();
|
|
220 }
|
|
221
|
|
222 void readDescription(BookmarkNode parent)
|
|
223 {
|
80
|
224 assert(isStartElement() && name() == "desc");
|
73
|
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 {
|
80
|
238 assert(isStartElement() && name() == "folder");
|
73
|
239
|
|
240 BookmarkNode folder = new BookmarkNode(BookmarkNode.Folder, parent);
|
80
|
241 folder.expanded = (attributes().value("folded") == "no");
|
73
|
242
|
|
243 while (!atEnd()) {
|
|
244 readNext();
|
|
245
|
|
246 if (isEndElement())
|
|
247 break;
|
45
|
248
|
73
|
249 if (isStartElement()) {
|
80
|
250 if (name() == "title")
|
73
|
251 readTitle(folder);
|
80
|
252 else if (name() == "desc")
|
73
|
253 readDescription(folder);
|
80
|
254 else if (name() == "folder")
|
73
|
255 readFolder(folder);
|
80
|
256 else if (name() == "bookmark")
|
73
|
257 readBookmarkNode(folder);
|
80
|
258 else if (name() == "separator")
|
73
|
259 readSeparator(folder);
|
|
260 else
|
|
261 skipUnknownElement();
|
|
262 }
|
|
263 }
|
|
264 }
|
|
265
|
|
266 void readBookmarkNode(BookmarkNode parent)
|
|
267 {
|
80
|
268 assert(isStartElement() && name() == "bookmark");
|
73
|
269 BookmarkNode bookmark = new BookmarkNode(BookmarkNode.Bookmark, parent);
|
80
|
270 bookmark.url = attributes().value("href").toString();
|
73
|
271 while (!atEnd()) {
|
|
272 readNext();
|
|
273 if (isEndElement())
|
|
274 break;
|
|
275
|
|
276 if (isStartElement()) {
|
80
|
277 if (name() == "title")
|
73
|
278 readTitle(bookmark);
|
80
|
279 else if (name() == "desc")
|
73
|
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
|
77
|
301 bool write(string fileName, BookmarkNode root)
|
73
|
302 {
|
77
|
303 auto file = new QFile(fileName);
|
73
|
304 if (!root || !file.open(QFile.WriteOnly))
|
|
305 return false;
|
|
306 return write(&file, root);
|
|
307 }
|
45
|
308
|
74
|
309 bool write(QIODevice device, BookmarkNode root)
|
73
|
310 {
|
|
311 setDevice(device);
|
45
|
312
|
73
|
313 writeStartDocument();
|
80
|
314 writeDTD("<!DOCTYPE xbel>");
|
|
315 writeStartElement("xbel");
|
|
316 writeAttribute("version", "1.0");
|
73
|
317 if (root.type() == BookmarkNode.Root) {
|
77
|
318 for (int i = 0; i < root.children().length; ++i)
|
|
319 writeItem(root.children()[i]);
|
73
|
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:
|
80
|
334 writeStartElement("folder");
|
|
335 writeAttribute("folded", parent.expanded ? "no" : "yes");
|
|
336 writeTextElement("title", parent.title);
|
73
|
337 for (int i = 0; i < parent.children().count(); ++i)
|
77
|
338 writeItem(parent.children()[i]);
|
73
|
339 writeEndElement();
|
|
340 break;
|
|
341 case BookmarkNode.Bookmark:
|
80
|
342 writeStartElement("bookmark");
|
73
|
343 if (!parent.url.isEmpty())
|
80
|
344 writeAttribute("href", parent.url);
|
|
345 writeTextElement("title", parent.title);
|
73
|
346 if (!parent.desc.isEmpty())
|
80
|
347 writeAttribute("desc", parent.desc);
|
73
|
348 writeEndElement();
|
|
349 break;
|
|
350 case BookmarkNode.Separator:
|
80
|
351 writeEmptyElement("separator");
|
73
|
352 break;
|
|
353 default:
|
|
354 break;
|
|
355 }
|
|
356 }
|
45
|
357 }
|