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 "xbeltree.h"
|
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
#include <QHeaderView>
|
|
|
|
#include <QMenu>
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
|
|
|
#include <QDesktopServices>
|
|
|
|
#include <QGuiApplication>
|
|
|
|
#if QT_CONFIG(clipboard) && QT_CONFIG(contextmenu)
|
|
|
|
# include <QClipboard>
|
|
|
|
# include <QContextMenuEvent>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <QUrl>
|
|
|
|
|
|
|
|
using namespace Qt::StringLiterals;
|
|
|
|
|
2023-10-30 06:33:08 +08:00
|
|
|
enum { DomElementRole = Qt::UserRole + 1 };
|
|
|
|
|
|
|
|
Q_DECLARE_METATYPE(QDomElement)
|
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
static const auto titleElement = u"title"_s;
|
|
|
|
static const auto folderElement = u"folder"_s;
|
|
|
|
static const auto bookmarkElement = u"bookmark"_s;
|
2023-10-30 06:33:08 +08:00
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
static const auto versionAttribute = u"version"_s;
|
|
|
|
static const auto hrefAttribute = u"href"_s;
|
|
|
|
static const auto foldedAttribute = u"folded"_s;
|
2023-10-30 06:33:08 +08:00
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [0]
|
2023-10-30 06:33:08 +08:00
|
|
|
XbelTree::XbelTree(QWidget *parent)
|
|
|
|
: QTreeWidget(parent)
|
|
|
|
{
|
|
|
|
header()->setSectionResizeMode(QHeaderView::Stretch);
|
2023-11-02 01:02:52 +08:00
|
|
|
setHeaderLabels({tr("Title"), tr("Location")});
|
2023-10-30 06:33:08 +08:00
|
|
|
|
|
|
|
folderIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirClosedIcon),
|
|
|
|
QIcon::Normal, QIcon::Off);
|
|
|
|
folderIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirOpenIcon),
|
|
|
|
QIcon::Normal, QIcon::On);
|
|
|
|
bookmarkIcon.addPixmap(style()->standardPixmap(QStyle::SP_FileIcon));
|
|
|
|
}
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [0]
|
2023-10-30 06:33:08 +08:00
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
#if QT_CONFIG(clipboard) && QT_CONFIG(contextmenu)
|
2023-10-30 06:33:08 +08:00
|
|
|
void XbelTree::contextMenuEvent(QContextMenuEvent *event)
|
|
|
|
{
|
|
|
|
const QTreeWidgetItem *item = itemAt(event->pos());
|
|
|
|
if (!item)
|
|
|
|
return;
|
|
|
|
const QString url = item->text(1);
|
|
|
|
QMenu contextMenu;
|
|
|
|
QAction *copyAction = contextMenu.addAction(tr("Copy Link to Clipboard"));
|
|
|
|
QAction *openAction = contextMenu.addAction(tr("Open"));
|
|
|
|
QAction *action = contextMenu.exec(event->globalPos());
|
|
|
|
if (action == copyAction)
|
|
|
|
QGuiApplication::clipboard()->setText(url);
|
|
|
|
else if (action == openAction)
|
|
|
|
QDesktopServices::openUrl(QUrl(url));
|
|
|
|
}
|
2023-11-02 01:02:52 +08:00
|
|
|
#endif // QT_CONFIG(clipboard) && QT_CONFIG(contextmenu)
|
2023-10-30 06:33:08 +08:00
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [1]
|
2023-10-30 06:33:08 +08:00
|
|
|
bool XbelTree::read(QIODevice *device)
|
|
|
|
{
|
|
|
|
QDomDocument::ParseResult result =
|
|
|
|
domDocument.setContent(device, QDomDocument::ParseOption::UseNamespaceProcessing);
|
|
|
|
if (!result) {
|
|
|
|
QMessageBox::information(window(), tr("DOM Bookmarks"),
|
|
|
|
tr("Parse error at line %1, column %2:\n%3")
|
|
|
|
.arg(result.errorLine)
|
|
|
|
.arg(result.errorColumn)
|
|
|
|
.arg(result.errorMessage));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDomElement root = domDocument.documentElement();
|
|
|
|
if (root.tagName() != "xbel") {
|
|
|
|
QMessageBox::information(window(), tr("DOM Bookmarks"),
|
|
|
|
tr("The file is not an XBEL file."));
|
|
|
|
return false;
|
2023-11-02 01:02:52 +08:00
|
|
|
} else if (root.hasAttribute(versionAttribute)
|
|
|
|
&& root.attribute(versionAttribute) != "1.0"_L1) {
|
2023-10-30 06:33:08 +08:00
|
|
|
QMessageBox::information(window(), tr("DOM Bookmarks"),
|
|
|
|
tr("The file is not an XBEL version 1.0 "
|
|
|
|
"file."));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
clear();
|
|
|
|
|
|
|
|
disconnect(this, &QTreeWidget::itemChanged, this, &XbelTree::updateDomElement);
|
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
QDomElement child = root.firstChildElement(folderElement);
|
2023-10-30 06:33:08 +08:00
|
|
|
while (!child.isNull()) {
|
|
|
|
parseFolderElement(child);
|
2023-11-02 01:02:52 +08:00
|
|
|
child = child.nextSiblingElement(folderElement);
|
2023-10-30 06:33:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
connect(this, &QTreeWidget::itemChanged, this, &XbelTree::updateDomElement);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [1]
|
2023-10-30 06:33:08 +08:00
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [2]
|
2023-10-30 06:33:08 +08:00
|
|
|
bool XbelTree::write(QIODevice *device) const
|
|
|
|
{
|
|
|
|
const int IndentSize = 4;
|
|
|
|
|
|
|
|
QTextStream out(device);
|
|
|
|
domDocument.save(out, IndentSize);
|
|
|
|
return true;
|
|
|
|
}
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [2]
|
2023-10-30 06:33:08 +08:00
|
|
|
|
|
|
|
void XbelTree::updateDomElement(const QTreeWidgetItem *item, int column)
|
|
|
|
{
|
|
|
|
QDomElement element = qvariant_cast<QDomElement>(item->data(0, DomElementRole));
|
|
|
|
if (!element.isNull()) {
|
|
|
|
if (column == 0) {
|
2023-11-02 01:02:52 +08:00
|
|
|
QDomElement oldTitleElement = element.firstChildElement(titleElement);
|
|
|
|
QDomElement newTitleElement = domDocument.createElement(titleElement);
|
2023-10-30 06:33:08 +08:00
|
|
|
|
|
|
|
QDomText newTitleText = domDocument.createTextNode(item->text(0));
|
|
|
|
newTitleElement.appendChild(newTitleText);
|
|
|
|
|
|
|
|
element.replaceChild(newTitleElement, oldTitleElement);
|
|
|
|
} else {
|
2023-11-02 01:02:52 +08:00
|
|
|
if (element.tagName() == bookmarkElement)
|
|
|
|
element.setAttribute(hrefAttribute, item->text(1));
|
2023-10-30 06:33:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [3]
|
2023-10-30 06:33:08 +08:00
|
|
|
void XbelTree::parseFolderElement(const QDomElement &element,
|
|
|
|
QTreeWidgetItem *parentItem)
|
|
|
|
{
|
|
|
|
QTreeWidgetItem *item = createItem(element, parentItem);
|
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
QString title = element.firstChildElement(titleElement).text();
|
2023-10-30 06:33:08 +08:00
|
|
|
if (title.isEmpty())
|
2023-11-02 01:02:52 +08:00
|
|
|
title = tr("Folder");
|
2023-10-30 06:33:08 +08:00
|
|
|
|
|
|
|
item->setFlags(item->flags() | Qt::ItemIsEditable);
|
|
|
|
item->setIcon(0, folderIcon);
|
|
|
|
item->setText(0, title);
|
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
bool folded = (element.attribute(foldedAttribute) != "no"_L1);
|
2023-10-30 06:33:08 +08:00
|
|
|
item->setExpanded(!folded);
|
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
constexpr char16_t midDot = u'\xB7';
|
|
|
|
static const QString dots = QString(30, midDot);
|
2023-10-30 06:33:08 +08:00
|
|
|
QDomElement child = element.firstChildElement();
|
|
|
|
while (!child.isNull()) {
|
2023-11-02 01:02:52 +08:00
|
|
|
if (child.tagName() == folderElement) {
|
2023-10-30 06:33:08 +08:00
|
|
|
parseFolderElement(child, item);
|
2023-11-02 01:02:52 +08:00
|
|
|
} else if (child.tagName() == bookmarkElement) {
|
2023-10-30 06:33:08 +08:00
|
|
|
QTreeWidgetItem *childItem = createItem(child, item);
|
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
QString title = child.firstChildElement(titleElement).text();
|
2023-10-30 06:33:08 +08:00
|
|
|
if (title.isEmpty())
|
2023-11-02 01:02:52 +08:00
|
|
|
title = tr("Folder");
|
2023-10-30 06:33:08 +08:00
|
|
|
|
|
|
|
childItem->setFlags(item->flags() | Qt::ItemIsEditable);
|
|
|
|
childItem->setIcon(0, bookmarkIcon);
|
|
|
|
childItem->setText(0, title);
|
2023-11-02 01:02:52 +08:00
|
|
|
childItem->setText(1, child.attribute(hrefAttribute));
|
|
|
|
} else if (child.tagName() == "separator"_L1) {
|
2023-10-30 06:33:08 +08:00
|
|
|
QTreeWidgetItem *childItem = createItem(child, item);
|
|
|
|
childItem->setFlags(item->flags() & ~(Qt::ItemIsSelectable | Qt::ItemIsEditable));
|
2023-11-02 01:02:52 +08:00
|
|
|
childItem->setText(0, dots);
|
2023-10-30 06:33:08 +08:00
|
|
|
}
|
|
|
|
child = child.nextSiblingElement();
|
|
|
|
}
|
|
|
|
}
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [3]
|
2023-10-30 06:33:08 +08:00
|
|
|
|
|
|
|
QTreeWidgetItem *XbelTree::createItem(const QDomElement &element,
|
|
|
|
QTreeWidgetItem *parentItem)
|
|
|
|
{
|
|
|
|
QTreeWidgetItem *item;
|
|
|
|
if (parentItem) {
|
|
|
|
item = new QTreeWidgetItem(parentItem);
|
|
|
|
} else {
|
|
|
|
item = new QTreeWidgetItem(this);
|
|
|
|
}
|
|
|
|
item->setData(0, DomElementRole, QVariant::fromValue(element));
|
|
|
|
return item;
|
|
|
|
}
|