2023-10-30 06:33:08 +08:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
|
|
|
|
|
|
|
#include "xbelreader.h"
|
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
#include <QStyle>
|
|
|
|
#include <QTreeWidget>
|
|
|
|
|
|
|
|
using namespace Qt::StringLiterals;
|
|
|
|
|
2023-10-30 06:33:08 +08:00
|
|
|
//! [0]
|
2023-11-02 01:02:52 +08:00
|
|
|
XbelReader::XbelReader(QTreeWidget *treeWidget) : treeWidget(treeWidget)
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
|
|
|
QStyle *style = treeWidget->style();
|
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirClosedIcon), QIcon::Normal,
|
|
|
|
QIcon::Off);
|
|
|
|
folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirOpenIcon), QIcon::Normal, QIcon::On);
|
2023-10-30 06:33:08 +08:00
|
|
|
bookmarkIcon.addPixmap(style->standardPixmap(QStyle::SP_FileIcon));
|
|
|
|
}
|
|
|
|
//! [0]
|
|
|
|
|
|
|
|
//! [1]
|
|
|
|
bool XbelReader::read(QIODevice *device)
|
|
|
|
{
|
|
|
|
xml.setDevice(device);
|
|
|
|
|
|
|
|
if (xml.readNextStartElement()) {
|
2023-11-02 01:02:52 +08:00
|
|
|
if (xml.name() == "xbel"_L1 && xml.attributes().value("version"_L1) == "1.0"_L1)
|
2023-10-30 06:33:08 +08:00
|
|
|
readXBEL();
|
2023-11-02 01:02:52 +08:00
|
|
|
else
|
2023-10-30 06:33:08 +08:00
|
|
|
xml.raiseError(QObject::tr("The file is not an XBEL version 1.0 file."));
|
|
|
|
}
|
|
|
|
|
|
|
|
return !xml.error();
|
|
|
|
}
|
|
|
|
//! [1]
|
|
|
|
|
|
|
|
//! [2]
|
|
|
|
QString XbelReader::errorString() const
|
|
|
|
{
|
|
|
|
return QObject::tr("%1\nLine %2, column %3")
|
|
|
|
.arg(xml.errorString())
|
|
|
|
.arg(xml.lineNumber())
|
|
|
|
.arg(xml.columnNumber());
|
|
|
|
}
|
|
|
|
//! [2]
|
|
|
|
|
|
|
|
//! [3]
|
|
|
|
void XbelReader::readXBEL()
|
|
|
|
{
|
2023-11-02 01:02:52 +08:00
|
|
|
Q_ASSERT(xml.isStartElement() && xml.name() == "xbel"_L1);
|
2023-10-30 06:33:08 +08:00
|
|
|
|
|
|
|
while (xml.readNextStartElement()) {
|
2023-11-02 01:02:52 +08:00
|
|
|
if (xml.name() == "folder"_L1)
|
|
|
|
readFolder(nullptr);
|
|
|
|
else if (xml.name() == "bookmark"_L1)
|
|
|
|
readBookmark(nullptr);
|
|
|
|
else if (xml.name() == "separator"_L1)
|
|
|
|
readSeparator(nullptr);
|
2023-10-30 06:33:08 +08:00
|
|
|
else
|
|
|
|
xml.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//! [3]
|
|
|
|
|
|
|
|
//! [4]
|
2023-11-02 01:02:52 +08:00
|
|
|
void XbelReader::readBookmark(QTreeWidgetItem *item)
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
2023-11-02 01:02:52 +08:00
|
|
|
Q_ASSERT(xml.isStartElement() && xml.name() == "bookmark"_L1);
|
2023-10-30 06:33:08 +08:00
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
QTreeWidgetItem *bookmark = createChildItem(item);
|
|
|
|
bookmark->setFlags(bookmark->flags() | Qt::ItemIsEditable);
|
|
|
|
bookmark->setIcon(0, bookmarkIcon);
|
|
|
|
bookmark->setText(0, QObject::tr("Unknown title"));
|
|
|
|
bookmark->setText(1, xml.attributes().value("href"_L1).toString());
|
|
|
|
|
|
|
|
while (xml.readNextStartElement()) {
|
|
|
|
if (xml.name() == "title"_L1)
|
|
|
|
readTitle(bookmark);
|
|
|
|
else
|
|
|
|
xml.skipCurrentElement();
|
|
|
|
}
|
2023-10-30 06:33:08 +08:00
|
|
|
}
|
|
|
|
//! [4]
|
|
|
|
|
|
|
|
//! [5]
|
2023-11-02 01:02:52 +08:00
|
|
|
void XbelReader::readTitle(QTreeWidgetItem *item)
|
|
|
|
{
|
|
|
|
Q_ASSERT(xml.isStartElement() && xml.name() == "title"_L1);
|
|
|
|
item->setText(0, xml.readElementText());
|
|
|
|
}
|
|
|
|
//! [5]
|
|
|
|
|
|
|
|
//! [6]
|
2023-10-30 06:33:08 +08:00
|
|
|
void XbelReader::readSeparator(QTreeWidgetItem *item)
|
|
|
|
{
|
2023-11-02 01:02:52 +08:00
|
|
|
Q_ASSERT(xml.isStartElement() && xml.name() == "separator"_L1);
|
|
|
|
constexpr char16_t midDot = u'\xB7';
|
|
|
|
static const QString dots(30, midDot);
|
2023-10-30 06:33:08 +08:00
|
|
|
|
|
|
|
QTreeWidgetItem *separator = createChildItem(item);
|
2023-11-02 01:02:52 +08:00
|
|
|
separator->setFlags(item ? item->flags() & ~Qt::ItemIsSelectable : Qt::ItemFlags{});
|
|
|
|
separator->setText(0, dots);
|
2023-10-30 06:33:08 +08:00
|
|
|
xml.skipCurrentElement();
|
|
|
|
}
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [6]
|
2023-10-30 06:33:08 +08:00
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [7]
|
2023-10-30 06:33:08 +08:00
|
|
|
void XbelReader::readFolder(QTreeWidgetItem *item)
|
|
|
|
{
|
2023-11-02 01:02:52 +08:00
|
|
|
Q_ASSERT(xml.isStartElement() && xml.name() == "folder"_L1);
|
2023-10-30 06:33:08 +08:00
|
|
|
|
|
|
|
QTreeWidgetItem *folder = createChildItem(item);
|
2023-11-02 01:02:52 +08:00
|
|
|
bool folded = xml.attributes().value("folded"_L1) != "no"_L1;
|
2023-10-30 06:33:08 +08:00
|
|
|
folder->setExpanded(!folded);
|
|
|
|
|
|
|
|
while (xml.readNextStartElement()) {
|
2023-11-02 01:02:52 +08:00
|
|
|
if (xml.name() == "title"_L1)
|
2023-10-30 06:33:08 +08:00
|
|
|
readTitle(folder);
|
2023-11-02 01:02:52 +08:00
|
|
|
else if (xml.name() == "folder"_L1)
|
2023-10-30 06:33:08 +08:00
|
|
|
readFolder(folder);
|
2023-11-02 01:02:52 +08:00
|
|
|
else if (xml.name() == "bookmark"_L1)
|
2023-10-30 06:33:08 +08:00
|
|
|
readBookmark(folder);
|
2023-11-02 01:02:52 +08:00
|
|
|
else if (xml.name() == "separator"_L1)
|
2023-10-30 06:33:08 +08:00
|
|
|
readSeparator(folder);
|
|
|
|
else
|
|
|
|
xml.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [7]
|
2023-10-30 06:33:08 +08:00
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [8]
|
2023-10-30 06:33:08 +08:00
|
|
|
QTreeWidgetItem *XbelReader::createChildItem(QTreeWidgetItem *item)
|
|
|
|
{
|
2023-11-02 01:02:52 +08:00
|
|
|
QTreeWidgetItem *childItem = item ? new QTreeWidgetItem(item) : new QTreeWidgetItem(treeWidget);
|
2023-10-30 06:33:08 +08:00
|
|
|
childItem->setData(0, Qt::UserRole, xml.name().toString());
|
|
|
|
return childItem;
|
|
|
|
}
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [8]
|