/* * This file is part of smolbote. It's copyrighted by the contributors recorded * in the version control history of the file, available from its original * location: git://neueland.iserlohn-fortress.net/smolbote.git * * SPDX-License-Identifier: GPL-3.0 */ #include "xbel.h" #include Xbel::Xbel(const QString &path) { m_path = path; } BookmarkItem *Xbel::read() { BookmarkItem *root = new BookmarkItem(BookmarkItem::Root, nullptr); root->title = QObject::tr("Title"); root->href = QObject::tr("href"); // if the path is empty, there is nothing to load if(m_path.isEmpty()) { return root; } QFile file(m_path); if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) { // file cannot be opened for reading return root; } QXmlStreamReader xmlReader(&file); if(xmlReader.readNextStartElement()) { if(!(xmlReader.name() == "xbel" && xmlReader.attributes().value("version") == "1.0")) { // invalid xbel qWarning("Error parsing %s", qUtf8Printable(m_path)); return root; } readChildElements(xmlReader, root); } return root; } void Xbel::readChildElements(QXmlStreamReader &reader, BookmarkItem *parentItem) { while(reader.readNextStartElement()) { if(reader.name() == "title") { parentItem->title = reader.readElementText(); } else if(reader.name() == "folder") { BookmarkItem *item = new BookmarkItem(BookmarkItem::Folder, parentItem); item->href = reader.attributes().value("href").toString(); item->folded = reader.attributes().value("folded") == QLatin1String("yes"); readChildElements(reader, item); } else if(reader.name() == "bookmark") { BookmarkItem *item = new BookmarkItem(BookmarkItem::Bookmark, parentItem); item->href = reader.attributes().value("href").toString(); readChildElements(reader, item); } else { reader.skipCurrentElement(); } } }