comparison demos/browser/xbel.d @ 73:7bfd46c330dc

more porting
author mandel
date Fri, 22 May 2009 10:59:00 +0000
parents 71b382c10ef6
children 37caa90ce503
comparison
equal deleted inserted replaced
72:b149ef2cb18b 73:7bfd46c330dc
39 ** 39 **
40 ****************************************************************************/ 40 ****************************************************************************/
41 41
42 module xbel; 42 module xbel;
43 43
44 import QtCore.QXmlStreamReader; 44
45 import QtCore.QDateTime; 45 import qt.core.QXmlStreamReader;
46 46 import qt.core.QDateTime;
47 47 import qt.core.QFile;
48 import QtCore.QFile; 48
49 49
50 class BookmarkNode 50 class BookmarkNode
51 { 51 {
52 public: 52 public:
53 enum Type { 53
54 Root, 54 enum Type {
55 Folder, 55 Root,
56 Bookmark, 56 Folder,
57 Separator 57 Bookmark,
58 }; 58 Separator
59 59 };
60 this(Type type = Root, BookmarkNode *parent = null) 60
61 { 61 this(Type type = Root, BookmarkNode parent = null)
62 expanded = false; 62 {
63 m_parent = parent; 63 expanded = false;
64 m_type = type; 64 m_parent = parent;
65 65 m_type = type;
66 if (parent) 66
67 parent.add(this); 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 }
79
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 }
91
92 Type type() const
93 {
94 return m_type;
95 }
96
97 void setType(Type type)
98 {
99 m_type = type;
100 }
101
102 QList<BookmarkNode > children() const
103 {
104 return m_children;
105 }
106
107
108 BookmarkNode parent() const
109 {
110 return m_parent;
111 }
112
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 }
123
124 void remove(BookmarkNode child)
125 {
126 child.m_parent = 0;
127 m_children.removeAll(child);
128 }
129
130 QString url;
131 QString title;
132 QString desc;
133 bool expanded;
134
135 private:
136
137 BookmarkNode m_parent;
138 Type m_type;
139 QList<BookmarkNode> m_children;
68 } 140 }
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 141
145 class XbelReader : public QXmlStreamReader 142 class XbelReader : public QXmlStreamReader
146 { 143 {
147 public: 144 public:
148 this() 145
149 { 146 this()
147 {
148 }
149
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 }
159
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 }
177
178 private:
179 void skipUnknownElement()
180 {
181 assert(isStartElement());
182
183 while (!atEnd()) {
184 readNext();
185
186 if (isEndElement())
187 break;
188
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;
202
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 }
215
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 }
227
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
235
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;
248
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 }
150 } 288 }
151 289
152 BookmarkNode *read(const QString &fileName) 290 import qt.core.QXmlStreamWriter;
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 291
296 class XbelWriter : public QXmlStreamWriter 292 class XbelWriter : public QXmlStreamWriter
297 { 293 {
298 public: 294 public:
299 this() 295
300 { 296 this()
301 setAutoFormatting(true); 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 }
308
309 bool write(QIODevice device, BookmarkNode root);
310 {
311 setDevice(device);
312
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 }
323
324 writeEndDocument();
325 return true;
326 }
327
328 private:
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 }
302 } 357 }
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 }