[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[2] | 11 | **
|
---|
[846] | 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.
|
---|
[2] | 25 | **
|
---|
[846] | 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."
|
---|
[2] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************************/
|
---|
| 40 |
|
---|
| 41 | #include <QtGui>
|
---|
| 42 |
|
---|
| 43 | #include "xbelhandler.h"
|
---|
| 44 |
|
---|
| 45 | XbelHandler::XbelHandler(QTreeWidget *treeWidget)
|
---|
| 46 | : treeWidget(treeWidget)
|
---|
| 47 | {
|
---|
| 48 | item = 0;
|
---|
| 49 | metXbelTag = false;
|
---|
| 50 |
|
---|
| 51 | QStyle *style = treeWidget->style();
|
---|
| 52 |
|
---|
| 53 | folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirClosedIcon),
|
---|
| 54 | QIcon::Normal, QIcon::Off);
|
---|
| 55 | folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirOpenIcon),
|
---|
| 56 | QIcon::Normal, QIcon::On);
|
---|
| 57 | bookmarkIcon.addPixmap(style->standardPixmap(QStyle::SP_FileIcon));
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | bool XbelHandler::startElement(const QString & /* namespaceURI */,
|
---|
| 61 | const QString & /* localName */,
|
---|
| 62 | const QString &qName,
|
---|
| 63 | const QXmlAttributes &attributes)
|
---|
| 64 | {
|
---|
| 65 | if (!metXbelTag && qName != "xbel") {
|
---|
| 66 | errorStr = QObject::tr("The file is not an XBEL file.");
|
---|
| 67 | return false;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | if (qName == "xbel") {
|
---|
| 71 | QString version = attributes.value("version");
|
---|
| 72 | if (!version.isEmpty() && version != "1.0") {
|
---|
| 73 | errorStr = QObject::tr("The file is not an XBEL version 1.0 file.");
|
---|
| 74 | return false;
|
---|
| 75 | }
|
---|
| 76 | metXbelTag = true;
|
---|
| 77 | } else if (qName == "folder") {
|
---|
| 78 | item = createChildItem(qName);
|
---|
| 79 | item->setFlags(item->flags() | Qt::ItemIsEditable);
|
---|
| 80 | item->setIcon(0, folderIcon);
|
---|
| 81 | item->setText(0, QObject::tr("Folder"));
|
---|
| 82 | bool folded = (attributes.value("folded") != "no");
|
---|
| 83 | treeWidget->setItemExpanded(item, !folded);
|
---|
| 84 | } else if (qName == "bookmark") {
|
---|
| 85 | item = createChildItem(qName);
|
---|
| 86 | item->setFlags(item->flags() | Qt::ItemIsEditable);
|
---|
| 87 | item->setIcon(0, bookmarkIcon);
|
---|
| 88 | item->setText(0, QObject::tr("Unknown title"));
|
---|
| 89 | item->setText(1, attributes.value("href"));
|
---|
| 90 | } else if (qName == "separator") {
|
---|
| 91 | item = createChildItem(qName);
|
---|
| 92 | item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
|
---|
| 93 | item->setText(0, QString(30, 0xB7));
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | currentText.clear();
|
---|
| 97 | return true;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | bool XbelHandler::endElement(const QString & /* namespaceURI */,
|
---|
| 101 | const QString & /* localName */,
|
---|
| 102 | const QString &qName)
|
---|
| 103 | {
|
---|
| 104 | if (qName == "title") {
|
---|
| 105 | if (item)
|
---|
| 106 | item->setText(0, currentText);
|
---|
| 107 | } else if (qName == "folder" || qName == "bookmark"
|
---|
| 108 | || qName == "separator") {
|
---|
| 109 | item = item->parent();
|
---|
| 110 | }
|
---|
| 111 | return true;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | bool XbelHandler::characters(const QString &str)
|
---|
| 115 | {
|
---|
| 116 | currentText += str;
|
---|
| 117 | return true;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | bool XbelHandler::fatalError(const QXmlParseException &exception)
|
---|
| 121 | {
|
---|
| 122 | QMessageBox::information(treeWidget->window(), QObject::tr("SAX Bookmarks"),
|
---|
| 123 | QObject::tr("Parse error at line %1, column %2:\n"
|
---|
| 124 | "%3")
|
---|
| 125 | .arg(exception.lineNumber())
|
---|
| 126 | .arg(exception.columnNumber())
|
---|
| 127 | .arg(exception.message()));
|
---|
| 128 | return false;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | QString XbelHandler::errorString() const
|
---|
| 132 | {
|
---|
| 133 | return errorStr;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | QTreeWidgetItem *XbelHandler::createChildItem(const QString &tagName)
|
---|
| 137 | {
|
---|
| 138 | QTreeWidgetItem *childItem;
|
---|
| 139 | if (item) {
|
---|
| 140 | childItem = new QTreeWidgetItem(item);
|
---|
| 141 | } else {
|
---|
| 142 | childItem = new QTreeWidgetItem(treeWidget);
|
---|
| 143 | }
|
---|
| 144 | childItem->setData(0, Qt::UserRole, tagName);
|
---|
| 145 | return childItem;
|
---|
| 146 | }
|
---|