1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
/****************************************************************************
**
** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information
** to ensure GNU General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html. In addition, as a special
** exception, Nokia gives you certain additional rights. These rights
** are described in the Nokia Qt GPL Exception version 1.3, included in
** the file GPL_EXCEPTION.txt in this package.
**
** Qt for Windows(R) Licensees
** As a special exception, Nokia, as the sole copyright holder for Qt
** Designer, grants users of the Qt/Eclipse Integration plug-in the
** right for the Qt/Eclipse Integration to link to functionality
** provided by Qt Designer and its related libraries.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
****************************************************************************/
#include "xbel.h"
#include <QtCore/QFile>
BookmarkNode::BookmarkNode(BookmarkNode::Type type, BookmarkNode *parent) :
expanded(false)
, m_parent(parent)
, m_type(type)
{
if (parent)
parent->add(this);
}
BookmarkNode::~BookmarkNode()
{
if (m_parent)
m_parent->remove(this);
qDeleteAll(m_children);
m_parent = 0;
m_type = BookmarkNode::Root;
}
bool BookmarkNode::operator==(const BookmarkNode &other)
{
if (url != other.url
|| title != other.title
|| desc != other.desc
|| expanded != other.expanded
|| m_type != other.m_type
|| m_children.count() != other.m_children.count())
return false;
for (int i = 0; i < m_children.count(); ++i)
if (!((*(m_children[i])) == (*(other.m_children[i]))))
return false;
return true;
}
BookmarkNode::Type BookmarkNode::type() const
{
return m_type;
}
void BookmarkNode::setType(Type type)
{
m_type = type;
}
QList<BookmarkNode *> BookmarkNode::children() const
{
return m_children;
}
BookmarkNode *BookmarkNode::parent() const
{
return m_parent;
}
void BookmarkNode::add(BookmarkNode *child, int offset)
{
Q_ASSERT(child->m_type != Root);
if (child->m_parent)
child->m_parent->remove(child);
child->m_parent = this;
if (-1 == offset)
offset = m_children.size();
m_children.insert(offset, child);
}
void BookmarkNode::remove(BookmarkNode *child)
{
child->m_parent = 0;
m_children.removeAll(child);
}
XbelReader::XbelReader()
{
}
BookmarkNode *XbelReader::read(const QString &fileName)
{
QFile file(fileName);
if (!file.exists()) {
return new BookmarkNode(BookmarkNode::Root);
}
file.open(QFile::ReadOnly);
return read(&file);
}
BookmarkNode *XbelReader::read(QIODevice *device)
{
BookmarkNode *root = new BookmarkNode(BookmarkNode::Root);
setDevice(device);
while (!atEnd()) {
readNext();
if (isStartElement()) {
QString version = attributes().value(QLatin1String("version")).toString();
if (name() == QLatin1String("xbel")
&& (version.isEmpty() || version == QLatin1String("1.0"))) {
readXBEL(root);
} else {
raiseError(QObject::tr("The file is not an XBEL version 1.0 file."));
}
}
}
return root;
}
void XbelReader::readXBEL(BookmarkNode *parent)
{
Q_ASSERT(isStartElement() && name() == QLatin1String("xbel"));
while (!atEnd()) {
readNext();
if (isEndElement())
break;
if (isStartElement()) {
if (name() == QLatin1String("folder"))
readFolder(parent);
else if (name() == QLatin1String("bookmark"))
readBookmarkNode(parent);
else if (name() == QLatin1String("separator"))
readSeparator(parent);
else
skipUnknownElement();
}
}
}
void XbelReader::readFolder(BookmarkNode *parent)
{
Q_ASSERT(isStartElement() && name() == QLatin1String("folder"));
BookmarkNode *folder = new BookmarkNode(BookmarkNode::Folder, parent);
folder->expanded = (attributes().value(QLatin1String("folded")) == QLatin1String("no"));
while (!atEnd()) {
readNext();
if (isEndElement())
break;
if (isStartElement()) {
if (name() == QLatin1String("title"))
readTitle(folder);
else if (name() == QLatin1String("desc"))
readDescription(folder);
else if (name() == QLatin1String("folder"))
readFolder(folder);
else if (name() == QLatin1String("bookmark"))
readBookmarkNode(folder);
else if (name() == QLatin1String("separator"))
readSeparator(folder);
else
skipUnknownElement();
}
}
}
void XbelReader::readTitle(BookmarkNode *parent)
{
Q_ASSERT(isStartElement() && name() == QLatin1String("title"));
parent->title = readElementText();
}
void XbelReader::readDescription(BookmarkNode *parent)
{
Q_ASSERT(isStartElement() && name() == QLatin1String("desc"));
parent->desc = readElementText();
}
void XbelReader::readSeparator(BookmarkNode *parent)
{
new BookmarkNode(BookmarkNode::Separator, parent);
// empty elements have a start and end element
readNext();
}
void XbelReader::readBookmarkNode(BookmarkNode *parent)
{
Q_ASSERT(isStartElement() && name() == QLatin1String("bookmark"));
BookmarkNode *bookmark = new BookmarkNode(BookmarkNode::Bookmark, parent);
bookmark->url = attributes().value(QLatin1String("href")).toString();
while (!atEnd()) {
readNext();
if (isEndElement())
break;
if (isStartElement()) {
if (name() == QLatin1String("title"))
readTitle(bookmark);
else if (name() == QLatin1String("desc"))
readDescription(bookmark);
else
skipUnknownElement();
}
}
if (bookmark->title.isEmpty())
bookmark->title = QObject::tr("Unknown title");
}
void XbelReader::skipUnknownElement()
{
Q_ASSERT(isStartElement());
while (!atEnd()) {
readNext();
if (isEndElement())
break;
if (isStartElement())
skipUnknownElement();
}
}
XbelWriter::XbelWriter()
{
setAutoFormatting(true);
}
bool XbelWriter::write(const QString &fileName, const BookmarkNode *root)
{
QFile file(fileName);
if (!root || !file.open(QFile::WriteOnly))
return false;
return write(&file, root);
}
bool XbelWriter::write(QIODevice *device, const BookmarkNode *root)
{
setDevice(device);
writeStartDocument();
writeDTD(QLatin1String("<!DOCTYPE xbel>"));
writeStartElement(QLatin1String("xbel"));
writeAttribute(QLatin1String("version"), QLatin1String("1.0"));
if (root->type() == BookmarkNode::Root) {
for (int i = 0; i < root->children().count(); ++i)
writeItem(root->children().at(i));
} else {
writeItem(root);
}
writeEndDocument();
return true;
}
void XbelWriter::writeItem(const BookmarkNode *parent)
{
switch (parent->type()) {
case BookmarkNode::Folder:
writeStartElement(QLatin1String("folder"));
writeAttribute(QLatin1String("folded"), parent->expanded ? QLatin1String("no") : QLatin1String("yes"));
writeTextElement(QLatin1String("title"), parent->title);
for (int i = 0; i < parent->children().count(); ++i)
writeItem(parent->children().at(i));
writeEndElement();
break;
case BookmarkNode::Bookmark:
writeStartElement(QLatin1String("bookmark"));
if (!parent->url.isEmpty())
writeAttribute(QLatin1String("href"), parent->url);
writeTextElement(QLatin1String("title"), parent->title);
if (!parent->desc.isEmpty())
writeAttribute(QLatin1String("desc"), parent->desc);
writeEndElement();
break;
case BookmarkNode::Separator:
writeEmptyElement(QLatin1String("separator"));
break;
default:
break;
}
}
|