| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the examples of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #include <QtWidgets> |
| 52 | |
| 53 | #include "xbelreader.h" |
| 54 | |
| 55 | //! [0] |
| 56 | XbelReader::XbelReader(QTreeWidget *treeWidget) |
| 57 | : treeWidget(treeWidget) |
| 58 | { |
| 59 | QStyle *style = treeWidget->style(); |
| 60 | |
| 61 | folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirClosedIcon), |
| 62 | QIcon::Normal, QIcon::Off); |
| 63 | folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirOpenIcon), |
| 64 | QIcon::Normal, QIcon::On); |
| 65 | bookmarkIcon.addPixmap(style->standardPixmap(QStyle::SP_FileIcon)); |
| 66 | } |
| 67 | //! [0] |
| 68 | |
| 69 | //! [1] |
| 70 | bool XbelReader::read(QIODevice *device) |
| 71 | { |
| 72 | xml.setDevice(device); |
| 73 | |
| 74 | if (xml.readNextStartElement()) { |
| 75 | if (xml.name() == QLatin1String("xbel" ) |
| 76 | && xml.attributes().value(versionAttribute()) == QLatin1String("1.0" )) { |
| 77 | readXBEL(); |
| 78 | } else { |
| 79 | xml.raiseError(QObject::tr("The file is not an XBEL version 1.0 file." )); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return !xml.error(); |
| 84 | } |
| 85 | //! [1] |
| 86 | |
| 87 | //! [2] |
| 88 | QString XbelReader::errorString() const |
| 89 | { |
| 90 | return QObject::tr("%1\nLine %2, column %3" ) |
| 91 | .arg(xml.errorString()) |
| 92 | .arg(xml.lineNumber()) |
| 93 | .arg(xml.columnNumber()); |
| 94 | } |
| 95 | //! [2] |
| 96 | |
| 97 | //! [3] |
| 98 | void XbelReader::readXBEL() |
| 99 | { |
| 100 | Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("xbel" )); |
| 101 | |
| 102 | while (xml.readNextStartElement()) { |
| 103 | if (xml.name() == QLatin1String("folder" )) |
| 104 | readFolder(0); |
| 105 | else if (xml.name() == QLatin1String("bookmark" )) |
| 106 | readBookmark(0); |
| 107 | else if (xml.name() == QLatin1String("separator" )) |
| 108 | readSeparator(0); |
| 109 | else |
| 110 | xml.skipCurrentElement(); |
| 111 | } |
| 112 | } |
| 113 | //! [3] |
| 114 | |
| 115 | //! [4] |
| 116 | void XbelReader::readTitle(QTreeWidgetItem *item) |
| 117 | { |
| 118 | Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("title" )); |
| 119 | |
| 120 | QString title = xml.readElementText(); |
| 121 | item->setText(0, title); |
| 122 | } |
| 123 | //! [4] |
| 124 | |
| 125 | //! [5] |
| 126 | void XbelReader::readSeparator(QTreeWidgetItem *item) |
| 127 | { |
| 128 | Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("separator" )); |
| 129 | |
| 130 | QTreeWidgetItem *separator = createChildItem(item); |
| 131 | separator->setFlags(item->flags() & ~Qt::ItemIsSelectable); |
| 132 | separator->setText(0, QString(30, u'\xB7')); |
| 133 | xml.skipCurrentElement(); |
| 134 | } |
| 135 | //! [5] |
| 136 | |
| 137 | void XbelReader::readFolder(QTreeWidgetItem *item) |
| 138 | { |
| 139 | Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("folder" )); |
| 140 | |
| 141 | QTreeWidgetItem *folder = createChildItem(item); |
| 142 | bool folded = (xml.attributes().value(foldedAttribute()) != QLatin1String("no" )); |
| 143 | folder->setExpanded(!folded); |
| 144 | |
| 145 | while (xml.readNextStartElement()) { |
| 146 | if (xml.name() == QLatin1String("title" )) |
| 147 | readTitle(folder); |
| 148 | else if (xml.name() == QLatin1String("folder" )) |
| 149 | readFolder(folder); |
| 150 | else if (xml.name() == QLatin1String("bookmark" )) |
| 151 | readBookmark(folder); |
| 152 | else if (xml.name() == QLatin1String("separator" )) |
| 153 | readSeparator(folder); |
| 154 | else |
| 155 | xml.skipCurrentElement(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void XbelReader::readBookmark(QTreeWidgetItem *item) |
| 160 | { |
| 161 | Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("bookmark" )); |
| 162 | |
| 163 | QTreeWidgetItem *bookmark = createChildItem(item); |
| 164 | bookmark->setFlags(bookmark->flags() | Qt::ItemIsEditable); |
| 165 | bookmark->setIcon(0, bookmarkIcon); |
| 166 | bookmark->setText(0, QObject::tr("Unknown title" )); |
| 167 | bookmark->setText(1, xml.attributes().value(hrefAttribute()).toString()); |
| 168 | |
| 169 | while (xml.readNextStartElement()) { |
| 170 | if (xml.name() == QLatin1String("title" )) |
| 171 | readTitle(bookmark); |
| 172 | else |
| 173 | xml.skipCurrentElement(); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | QTreeWidgetItem *XbelReader::createChildItem(QTreeWidgetItem *item) |
| 178 | { |
| 179 | QTreeWidgetItem *childItem; |
| 180 | if (item) { |
| 181 | childItem = new QTreeWidgetItem(item); |
| 182 | } else { |
| 183 | childItem = new QTreeWidgetItem(treeWidget); |
| 184 | } |
| 185 | childItem->setData(0, Qt::UserRole, xml.name().toString()); |
| 186 | return childItem; |
| 187 | } |
| 188 | |