examples/xml/saxbookmarks/xbelhandler.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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 }