1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
19 ** distribution.
20 ** * Neither the name of The Qt Company Ltd nor the names of its
21 ** contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include <QtGui>
42
43 #include "xbelreader.h"
44
45 //! [0]
XbelReader(QTreeWidget * treeWidget)46 XbelReader::XbelReader(QTreeWidget *treeWidget)
47 : treeWidget(treeWidget)
48 {
49 QStyle *style = treeWidget->style();
50
51 folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirClosedIcon),
52 QIcon::Normal, QIcon::Off);
53 folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirOpenIcon),
54 QIcon::Normal, QIcon::On);
55 bookmarkIcon.addPixmap(style->standardPixmap(QStyle::SP_FileIcon));
56 }
57 //! [0]
58
59 //! [1]
read(QIODevice * device)60 bool XbelReader::read(QIODevice *device)
61 {
62 xml.setDevice(device);
63
64 if (xml.readNextStartElement()) {
65 if (xml.name() == "xbel" && xml.attributes().value("version") == "1.0")
66 readXBEL();
67 else
68 xml.raiseError(QObject::tr("The file is not an XBEL version 1.0 file."));
69 }
70
71 return !xml.error();
72 }
73 //! [1]
74
75 //! [2]
errorString() const76 QString XbelReader::errorString() const
77 {
78 return QObject::tr("%1\nLine %2, column %3")
79 .arg(xml.errorString())
80 .arg(xml.lineNumber())
81 .arg(xml.columnNumber());
82 }
83 //! [2]
84
85 //! [3]
readXBEL()86 void XbelReader::readXBEL()
87 {
88 Q_ASSERT(xml.isStartElement() && xml.name() == "xbel");
89
90 while (xml.readNextStartElement()) {
91 if (xml.name() == "folder")
92 readFolder(0);
93 else if (xml.name() == "bookmark")
94 readBookmark(0);
95 else if (xml.name() == "separator")
96 readSeparator(0);
97 else
98 xml.skipCurrentElement();
99 }
100 }
101 //! [3]
102
103 //! [4]
readTitle(QTreeWidgetItem * item)104 void XbelReader::readTitle(QTreeWidgetItem *item)
105 {
106 Q_ASSERT(xml.isStartElement() && xml.name() == "title");
107
108 QString title = xml.readElementText();
109 item->setText(0, title);
110 }
111 //! [4]
112
113 //! [5]
readSeparator(QTreeWidgetItem * item)114 void XbelReader::readSeparator(QTreeWidgetItem *item)
115 {
116 Q_ASSERT(xml.isStartElement() && xml.name() == "separator");
117
118 QTreeWidgetItem *separator = createChildItem(item);
119 separator->setFlags(item->flags() & ~Qt::ItemIsSelectable);
120 separator->setText(0, QString(30, 0xB7));
121 xml.skipCurrentElement();
122 }
123 //! [5]
124
readFolder(QTreeWidgetItem * item)125 void XbelReader::readFolder(QTreeWidgetItem *item)
126 {
127 Q_ASSERT(xml.isStartElement() && xml.name() == "folder");
128
129 QTreeWidgetItem *folder = createChildItem(item);
130 bool folded = (xml.attributes().value("folded") != "no");
131 treeWidget->setItemExpanded(folder, !folded);
132
133 while (xml.readNextStartElement()) {
134 if (xml.name() == "title")
135 readTitle(folder);
136 else if (xml.name() == "folder")
137 readFolder(folder);
138 else if (xml.name() == "bookmark")
139 readBookmark(folder);
140 else if (xml.name() == "separator")
141 readSeparator(folder);
142 else
143 xml.skipCurrentElement();
144 }
145 }
146
readBookmark(QTreeWidgetItem * item)147 void XbelReader::readBookmark(QTreeWidgetItem *item)
148 {
149 Q_ASSERT(xml.isStartElement() && xml.name() == "bookmark");
150
151 QTreeWidgetItem *bookmark = createChildItem(item);
152 bookmark->setFlags(bookmark->flags() | Qt::ItemIsEditable);
153 bookmark->setIcon(0, bookmarkIcon);
154 bookmark->setText(0, QObject::tr("Unknown title"));
155 bookmark->setText(1, xml.attributes().value("href").toString());
156
157 while (xml.readNextStartElement()) {
158 if (xml.name() == "title")
159 readTitle(bookmark);
160 else
161 xml.skipCurrentElement();
162 }
163 }
164
createChildItem(QTreeWidgetItem * item)165 QTreeWidgetItem *XbelReader::createChildItem(QTreeWidgetItem *item)
166 {
167 QTreeWidgetItem *childItem;
168 if (item) {
169 childItem = new QTreeWidgetItem(item);
170 } else {
171 childItem = new QTreeWidgetItem(treeWidget);
172 }
173 childItem->setData(0, Qt::UserRole, xml.name().toString());
174 return childItem;
175 }
176