1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "xbelsupport.h"
34 #include "bookmarkmanager.h"
36 #include <QtCore/QCoreApplication>
38 using namespace Help::Internal;
46 XbelWriter::XbelWriter(BookmarkModel *model)
50 setAutoFormatting(true);
53 void XbelWriter::writeToFile(QIODevice *device)
58 writeDTD(QLatin1String("<!DOCTYPE xbel>"));
59 writeStartElement(QLatin1String("xbel"));
60 writeAttribute(QLatin1String("version"), QLatin1String("1.0"));
62 QStandardItem *root = treeModel->invisibleRootItem();
63 for (int i = 0; i < root->rowCount(); ++i)
64 writeData(root->child(i));
69 void XbelWriter::writeData(QStandardItem *child)
72 entry.title = child->data(Qt::DisplayRole).toString();
73 entry.url = child->data(Qt::UserRole + 10).toString();
75 if (entry.url == QLatin1String("Folder")) {
76 writeStartElement(QLatin1String("folder"));
78 entry.folded = !child->data(Qt::UserRole + 11).toBool();
79 writeAttribute(QLatin1String("folded"),
80 entry.folded ? QLatin1String("yes") : QLatin1String("no"));
82 writeTextElement(QLatin1String("title"), entry.title);
84 for (int i = 0; i < child->rowCount(); ++i)
85 writeData(child->child(i));
89 writeStartElement(QLatin1String("bookmark"));
90 writeAttribute(QLatin1String("href"), entry.url);
91 writeTextElement(QLatin1String("title"), entry.title);
97 // #pragma mark -- XbelReader
100 XbelReader::XbelReader(BookmarkModel *tree, BookmarkModel *list)
105 bookmarkIcon = QIcon(QLatin1String(":/help/images/bookmark.png"));
106 folderIcon = QApplication::style()->standardIcon(QStyle::SP_DirClosedIcon);
109 bool XbelReader::readFromFile(QIODevice *device)
116 if (isStartElement()) {
117 if (name() == QLatin1String("xbel")
118 && attributes().value(QLatin1String("version"))
119 == QLatin1String("1.0")) {
122 raiseError(QCoreApplication::translate("Help::Internal::XbelReader", "The file is not an XBEL version 1.0 file."));
130 void XbelReader::readXBEL()
138 if (isStartElement()) {
139 if (name() == QLatin1String("folder"))
141 else if (name() == QLatin1String("bookmark"))
144 readUnknownElement();
149 void XbelReader::readUnknownElement()
157 if (isStartElement())
158 readUnknownElement();
162 void XbelReader::readFolder(QStandardItem *item)
164 QStandardItem *folder = createChildItem(item);
165 folder->setIcon(folderIcon);
166 folder->setData(QLatin1String("Folder"), Qt::UserRole + 10);
169 (attributes().value(QLatin1String("folded")) != QLatin1String("no"));
170 folder->setData(expanded, Qt::UserRole + 11);
178 if (isStartElement()) {
179 if (name() == QLatin1String("title"))
180 folder->setText(readElementText());
181 else if (name() == QLatin1String("folder"))
183 else if (name() == QLatin1String("bookmark"))
184 readBookmark(folder);
186 readUnknownElement();
191 void XbelReader::readBookmark(QStandardItem *item)
193 QStandardItem *bookmark = createChildItem(item);
194 bookmark->setIcon(bookmarkIcon);
195 bookmark->setText(QCoreApplication::translate("Help::Internal::XbelReader", "Unknown title"));
196 bookmark->setData(attributes().value(QLatin1String("href")).toString(),
205 if (isStartElement()) {
206 if (name() == QLatin1String("title"))
207 bookmark->setText(readElementText());
209 readUnknownElement();
213 listModel->appendRow(bookmark->clone());
216 QStandardItem *XbelReader::createChildItem(QStandardItem *item)
218 QStandardItem *childItem = new QStandardItem();
219 childItem->setEditable(false);
222 item->appendRow(childItem);
224 treeModel->appendRow(childItem);