1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 |
|
---|
43 | #include "xbelreader.h"
|
---|
44 |
|
---|
45 | //! [0]
|
---|
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]
|
---|
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]
|
---|
76 | 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]
|
---|
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]
|
---|
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]
|
---|
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 |
|
---|
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 |
|
---|
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 |
|
---|
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 | }
|
---|