| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include <QtGui>
|
|---|
| 43 |
|
|---|
| 44 | #include "xbelhandler.h"
|
|---|
| 45 |
|
|---|
| 46 | XbelHandler::XbelHandler(QTreeWidget *treeWidget)
|
|---|
| 47 | : treeWidget(treeWidget)
|
|---|
| 48 | {
|
|---|
| 49 | item = 0;
|
|---|
| 50 | metXbelTag = false;
|
|---|
| 51 |
|
|---|
| 52 | QStyle *style = treeWidget->style();
|
|---|
| 53 |
|
|---|
| 54 | folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirClosedIcon),
|
|---|
| 55 | QIcon::Normal, QIcon::Off);
|
|---|
| 56 | folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirOpenIcon),
|
|---|
| 57 | QIcon::Normal, QIcon::On);
|
|---|
| 58 | bookmarkIcon.addPixmap(style->standardPixmap(QStyle::SP_FileIcon));
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | bool XbelHandler::startElement(const QString & /* namespaceURI */,
|
|---|
| 62 | const QString & /* localName */,
|
|---|
| 63 | const QString &qName,
|
|---|
| 64 | const QXmlAttributes &attributes)
|
|---|
| 65 | {
|
|---|
| 66 | if (!metXbelTag && qName != "xbel") {
|
|---|
| 67 | errorStr = QObject::tr("The file is not an XBEL file.");
|
|---|
| 68 | return false;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | if (qName == "xbel") {
|
|---|
| 72 | QString version = attributes.value("version");
|
|---|
| 73 | if (!version.isEmpty() && version != "1.0") {
|
|---|
| 74 | errorStr = QObject::tr("The file is not an XBEL version 1.0 file.");
|
|---|
| 75 | return false;
|
|---|
| 76 | }
|
|---|
| 77 | metXbelTag = true;
|
|---|
| 78 | } else if (qName == "folder") {
|
|---|
| 79 | item = createChildItem(qName);
|
|---|
| 80 | item->setFlags(item->flags() | Qt::ItemIsEditable);
|
|---|
| 81 | item->setIcon(0, folderIcon);
|
|---|
| 82 | item->setText(0, QObject::tr("Folder"));
|
|---|
| 83 | bool folded = (attributes.value("folded") != "no");
|
|---|
| 84 | treeWidget->setItemExpanded(item, !folded);
|
|---|
| 85 | } else if (qName == "bookmark") {
|
|---|
| 86 | item = createChildItem(qName);
|
|---|
| 87 | item->setFlags(item->flags() | Qt::ItemIsEditable);
|
|---|
| 88 | item->setIcon(0, bookmarkIcon);
|
|---|
| 89 | item->setText(0, QObject::tr("Unknown title"));
|
|---|
| 90 | item->setText(1, attributes.value("href"));
|
|---|
| 91 | } else if (qName == "separator") {
|
|---|
| 92 | item = createChildItem(qName);
|
|---|
| 93 | item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
|
|---|
| 94 | item->setText(0, QString(30, 0xB7));
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | currentText.clear();
|
|---|
| 98 | return true;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | bool XbelHandler::endElement(const QString & /* namespaceURI */,
|
|---|
| 102 | const QString & /* localName */,
|
|---|
| 103 | const QString &qName)
|
|---|
| 104 | {
|
|---|
| 105 | if (qName == "title") {
|
|---|
| 106 | if (item)
|
|---|
| 107 | item->setText(0, currentText);
|
|---|
| 108 | } else if (qName == "folder" || qName == "bookmark"
|
|---|
| 109 | || qName == "separator") {
|
|---|
| 110 | item = item->parent();
|
|---|
| 111 | }
|
|---|
| 112 | return true;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | bool XbelHandler::characters(const QString &str)
|
|---|
| 116 | {
|
|---|
| 117 | currentText += str;
|
|---|
| 118 | return true;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | bool XbelHandler::fatalError(const QXmlParseException &exception)
|
|---|
| 122 | {
|
|---|
| 123 | QMessageBox::information(treeWidget->window(), QObject::tr("SAX Bookmarks"),
|
|---|
| 124 | QObject::tr("Parse error at line %1, column %2:\n"
|
|---|
| 125 | "%3")
|
|---|
| 126 | .arg(exception.lineNumber())
|
|---|
| 127 | .arg(exception.columnNumber())
|
|---|
| 128 | .arg(exception.message()));
|
|---|
| 129 | return false;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | QString XbelHandler::errorString() const
|
|---|
| 133 | {
|
|---|
| 134 | return errorStr;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | QTreeWidgetItem *XbelHandler::createChildItem(const QString &tagName)
|
|---|
| 138 | {
|
|---|
| 139 | QTreeWidgetItem *childItem;
|
|---|
| 140 | if (item) {
|
|---|
| 141 | childItem = new QTreeWidgetItem(item);
|
|---|
| 142 | } else {
|
|---|
| 143 | childItem = new QTreeWidgetItem(treeWidget);
|
|---|
| 144 | }
|
|---|
| 145 | childItem->setData(0, Qt::UserRole, tagName);
|
|---|
| 146 | return childItem;
|
|---|
| 147 | }
|
|---|