From 9203de811f049c8e604a9c3065781157fa506155 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Tue, 25 Sep 2018 14:44:01 +0200 Subject: Bookmarks: integrate model/view - fix addBookmark and search signals - fix drag'n'drop - add xbel::write --- lib/bookmarks/xbel.cpp | 95 ++++++++++++++------------------------------------ 1 file changed, 26 insertions(+), 69 deletions(-) (limited to 'lib/bookmarks/xbel.cpp') diff --git a/lib/bookmarks/xbel.cpp b/lib/bookmarks/xbel.cpp index a1bf2f6..c23f0be 100644 --- a/lib/bookmarks/xbel.cpp +++ b/lib/bookmarks/xbel.cpp @@ -18,28 +18,6 @@ XbelReader::XbelReader(QIODevice *file) m_file = file; } -void readChildElements(QXmlStreamReader &reader, BookmarksView *widget, QTreeWidgetItem *parentItem = nullptr) -{ - while(reader.readNextStartElement()) { - if(reader.name() == "title") { - Q_CHECK_PTR(parentItem); - parentItem->setText(0, reader.readElementText()); - - } else if(reader.name() == "folder") { - auto *item = widget->createFolder(parentItem); - widget->setItemExpanded(item, !(reader.attributes().value("folded") == QLatin1String("yes"))); - readChildElements(reader, widget, item); - - } else if(reader.name() == "bookmark") { - auto *item = widget->createBookmark(parentItem); - item->setText(1, reader.attributes().value("href").toString()); - readChildElements(reader, widget, item); - - } else { - reader.skipCurrentElement(); - } - } -} void readChildElements(QXmlStreamReader &reader, BookmarkItem *parent) { @@ -65,20 +43,7 @@ void readChildElements(QXmlStreamReader &reader, BookmarkItem *parent) } } -void XbelReader::read(BookmarksView *treeWidget) -{ - QXmlStreamReader qXmlStreamReader(m_file); - - if(qXmlStreamReader.readNextStartElement()) { - if(!(qXmlStreamReader.name() == "xbel" && qXmlStreamReader.attributes().value("version") == "1.0")) { - return; - } - - readChildElements(qXmlStreamReader, treeWidget); - } -} - -void XbelReader::read(BookmarkModel *model) +void XbelReader::read(BookmarkItem *item) { QXmlStreamReader qXmlStreamReader(m_file); @@ -87,46 +52,40 @@ void XbelReader::read(BookmarkModel *model) return; } - readChildElements(qXmlStreamReader, model->root()); + readChildElements(qXmlStreamReader, item); } } -XbelWriter::XbelWriter(QIODevice *file) +XbelWriter::XbelWriter() { - Q_CHECK_PTR(file); - m_file = file; } -void writeChildElements(QXmlStreamWriter &writer, BookmarksView *treeWidget, QTreeWidgetItem *parentItem) +inline void writeChildElement(QXmlStreamWriter &writer, const BookmarkItem *item) { - switch(treeWidget->itemType(parentItem)) { - case BookmarksView::Folder: - writer.writeStartElement("folder"); - writer.writeAttribute("folded", !treeWidget->isItemExpanded(parentItem) ? "yes" : "no"); - writer.writeTextElement("title", parentItem->text(0)); - - for(int i = 0; i < parentItem->childCount(); ++i) { - writeChildElements(writer, treeWidget, parentItem->child(i)); - } - - writer.writeEndElement(); - break; - - case BookmarksView::Bookmark: - writer.writeStartElement("bookmark"); - writer.writeAttribute("href", parentItem->text(1)); - writer.writeTextElement("title", parentItem->text(0)); - writer.writeEndElement(); - break; - - case BookmarksView::Invalid: - break; + switch (item->type()) { + case BookmarkItem::Folder: + writer.writeStartElement("folder"); + writer.writeAttribute("folded", !item->isExpanded() ? "yes" : "no"); + writer.writeTextElement("title", item->data(BookmarkItem::Title).toString()); + + for(int i = 0; i < item->childCount(); ++i) { + writeChildElement(writer, item->child(i)); + } + + writer.writeEndElement(); + break; + + case BookmarkItem::Bookmark: + writer.writeStartElement("bookmark"); + writer.writeAttribute("href", item->data(BookmarkItem::Href).toString()); + writer.writeTextElement("title", item->data(BookmarkItem::Title).toString()); + writer.writeEndElement(); + break; } } - -void XbelWriter::write(BookmarksView *treeWidget) +void XbelWriter::write(QIODevice *device, const BookmarkItem *item) { - QXmlStreamWriter xmlWriter(m_file); + QXmlStreamWriter xmlWriter(device); xmlWriter.setAutoFormatting(true); xmlWriter.writeStartDocument(); @@ -135,9 +94,7 @@ void XbelWriter::write(BookmarksView *treeWidget) xmlWriter.writeStartElement("xbel"); xmlWriter.writeAttribute("version", "1.0"); - for(int i = 0; i < treeWidget->topLevelItemCount(); ++i) { - writeChildElements(xmlWriter, treeWidget, treeWidget->topLevelItem(i)); - } + writeChildElement(xmlWriter, item); xmlWriter.writeEndDocument(); } -- cgit v1.2.1