From 6e89bf230725e59e71a3273bf8492ed0a2066716 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 15 Nov 2019 17:02:39 +0200 Subject: Bookmarks: move xbel implementation to formats/ --- lib/bookmarks/bookmarkswidget.cpp | 8 ++- lib/bookmarks/formats/format.cpp | 26 ++++++++ lib/bookmarks/formats/format.h | 36 +++++++++++ lib/bookmarks/formats/xbel.cpp | 110 ++++++++++++++++++++++++++++++++++ lib/bookmarks/formats/xbel.h | 20 +++++++ lib/bookmarks/meson.build | 2 +- lib/bookmarks/model/bookmarkmodel.cpp | 2 +- lib/bookmarks/xbel.cpp | 110 ---------------------------------- lib/bookmarks/xbel.h | 20 ------- 9 files changed, 199 insertions(+), 135 deletions(-) create mode 100644 lib/bookmarks/formats/format.cpp create mode 100644 lib/bookmarks/formats/format.h create mode 100644 lib/bookmarks/formats/xbel.cpp create mode 100644 lib/bookmarks/formats/xbel.h delete mode 100644 lib/bookmarks/xbel.cpp delete mode 100644 lib/bookmarks/xbel.h diff --git a/lib/bookmarks/bookmarkswidget.cpp b/lib/bookmarks/bookmarkswidget.cpp index f3ef4df..aed7e97 100644 --- a/lib/bookmarks/bookmarkswidget.cpp +++ b/lib/bookmarks/bookmarkswidget.cpp @@ -11,7 +11,7 @@ #include "model/bookmarkitem.h" #include "model/bookmarkmodel.h" #include "ui_bookmarksform.h" -#include "xbel.h" +#include "formats/format.h" #include #include @@ -48,7 +48,8 @@ BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent) m_bookmarksPath = path; QFile bookmarksFile(m_bookmarksPath); if(bookmarksFile.open(QIODevice::ReadOnly | QIODevice::Text)) { - Xbel::read(&bookmarksFile, model->root()); + BookmarksFormat format(&bookmarksFile); + format.read(model->root()); bookmarksFile.close(); } model->resetModified(); @@ -123,7 +124,8 @@ void BookmarksWidget::save() QFile bookmarksFile(m_bookmarksPath); if(bookmarksFile.open(QIODevice::WriteOnly | QIODevice::Text)) { - Xbel::write(&bookmarksFile, model->root()); + BookmarksFormat format(&bookmarksFile); + format.write(model->root()); bookmarksFile.flush(); bookmarksFile.close(); model->resetModified(); diff --git a/lib/bookmarks/formats/format.cpp b/lib/bookmarks/formats/format.cpp new file mode 100644 index 0000000..551151c --- /dev/null +++ b/lib/bookmarks/formats/format.cpp @@ -0,0 +1,26 @@ +/* + * 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: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "format.h" +#include "xbel.h" +#include + +template<> +void BookmarksFormat::read(BookmarkItem *root) +{ + Q_CHECK_PTR(m_device); + Xbel::read(m_device, root); +} + +template<> +void BookmarksFormat::write(BookmarkItem *root) +{ + Q_CHECK_PTR(m_device); + Xbel::write(m_device, root); +} + diff --git a/lib/bookmarks/formats/format.h b/lib/bookmarks/formats/format.h new file mode 100644 index 0000000..e96dfcc --- /dev/null +++ b/lib/bookmarks/formats/format.h @@ -0,0 +1,36 @@ +/* + * 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: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef BOOKMARKSFORMAT_H +#define BOOKMARKSFORMAT_H + +class QIODevice; +class BookmarkItem; + +enum BookmarksFormats { + XbelFormat +}; + +template +class BookmarksFormat +{ +public: + explicit BookmarksFormat(QIODevice *device) + { + m_device = device; + } + + void read(BookmarkItem *root); + void write(BookmarkItem *root); + +protected: + QIODevice *m_device; +}; + +#endif // BOOKMARKSFORMAT_H + diff --git a/lib/bookmarks/formats/xbel.cpp b/lib/bookmarks/formats/xbel.cpp new file mode 100644 index 0000000..1cb5756 --- /dev/null +++ b/lib/bookmarks/formats/xbel.cpp @@ -0,0 +1,110 @@ +/* + * 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: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "xbel.h" +#include "model/bookmarkitem.h" +#include +#include + +inline void readChildElements(QXmlStreamReader &reader, BookmarkItem *parent) +{ + while(reader.readNextStartElement()) { + if(reader.name() == "title") { + parent->setData(BookmarkItem::Title, reader.readElementText()); + + } else if(reader.name() == "tags") { + parent->setData(BookmarkItem::Tags, reader.readElementText().split(";")); + + } else if(reader.name() == "description") { + parent->setData(BookmarkItem::Description, reader.readElementText()); + + } else if(reader.name() == "folder") { + auto *item = new BookmarkItem({}, BookmarkItem::Folder, parent); + item->setExpanded(!(reader.attributes().value("folded") == QLatin1Literal("yes"))); + parent->appendChild(item); + readChildElements(reader, item); + + } else if(reader.name() == "bookmark") { + auto *item = new BookmarkItem({}, BookmarkItem::Bookmark, parent); + item->setData(BookmarkItem::Href, reader.attributes().value("href").toString()); + parent->appendChild(item); + readChildElements(reader, item); + + } else { + reader.skipCurrentElement(); + } + } +} + +void Xbel::read(QIODevice *device, BookmarkItem *item) +{ + QXmlStreamReader qXmlStreamReader(device); + + if(qXmlStreamReader.readNextStartElement()) { + if(!(qXmlStreamReader.name() == "xbel" && qXmlStreamReader.attributes().value("version") == "1.0")) { + return; + } + + readChildElements(qXmlStreamReader, item); + } +} + +inline void writeChildElements(QXmlStreamWriter &writer, const BookmarkItem *item) +{ + switch(item->type()) { + case BookmarkItem::Root: + for(int i = 0; i < item->childCount(); ++i) { + writeChildElements(writer, item->child(i)); + } + break; + + case BookmarkItem::Folder: + writer.writeStartElement("folder"); + writer.writeAttribute("folded", !item->isExpanded() ? "yes" : "no"); + writer.writeTextElement("title", item->data(BookmarkItem::Title).toString()); + if(!item->data(BookmarkItem::Tags).isNull()) + writer.writeTextElement("tags", item->data(BookmarkItem::Tags).toStringList().join(";")); + if(!item->data(BookmarkItem::Description).isNull()) + writer.writeTextElement("description", item->data(BookmarkItem::Description).toString()); + + for(int i = 0; i < item->childCount(); ++i) { + writeChildElements(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()); + if(!item->data(BookmarkItem::Tags).isNull()) + writer.writeTextElement("tags", item->data(BookmarkItem::Tags).toStringList().join(";")); + if(!item->data(BookmarkItem::Description).isNull()) + writer.writeTextElement("description", item->data(BookmarkItem::Description).toString()); + + writer.writeEndElement(); + break; + } +} + +void Xbel::write(QIODevice *device, const BookmarkItem *item) +{ + QXmlStreamWriter xmlWriter(device); + xmlWriter.setAutoFormatting(true); + + xmlWriter.writeStartDocument(); + xmlWriter.writeDTD(""); + + xmlWriter.writeStartElement("xbel"); + xmlWriter.writeAttribute("version", "1.0"); + + writeChildElements(xmlWriter, item); + + xmlWriter.writeEndDocument(); +} diff --git a/lib/bookmarks/formats/xbel.h b/lib/bookmarks/formats/xbel.h new file mode 100644 index 0000000..44a65bb --- /dev/null +++ b/lib/bookmarks/formats/xbel.h @@ -0,0 +1,20 @@ +/* + * 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: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef XBEL_H +#define XBEL_H + +class QIODevice; +class BookmarkItem; +namespace Xbel +{ +void read(QIODevice *device, BookmarkItem *item); +void write(QIODevice *device, const BookmarkItem *item); +} + +#endif // XBEL_H diff --git a/lib/bookmarks/meson.build b/lib/bookmarks/meson.build index cdcdd47..3049e3c 100644 --- a/lib/bookmarks/meson.build +++ b/lib/bookmarks/meson.build @@ -7,7 +7,7 @@ bookmarks_moc = mod_qt5.preprocess( bookmarks_lib = static_library('bookmarks', ['bookmarkswidget.cpp', bookmarks_moc, - 'xbel.cpp', 'xbel.h', + 'formats/format.cpp', 'formats/xbel.cpp', 'model/bookmarkitem.cpp', 'model/bookmarkmodel.cpp', 'forms/editbookmarkdialog.cpp'], dependencies: dep_qt5 diff --git a/lib/bookmarks/model/bookmarkmodel.cpp b/lib/bookmarks/model/bookmarkmodel.cpp index 9214c24..895b178 100644 --- a/lib/bookmarks/model/bookmarkmodel.cpp +++ b/lib/bookmarks/model/bookmarkmodel.cpp @@ -8,7 +8,7 @@ #include "bookmarkmodel.h" #include "bookmarkitem.h" -#include "xbel.h" +#include "formats/xbel.h" #include #include #include diff --git a/lib/bookmarks/xbel.cpp b/lib/bookmarks/xbel.cpp deleted file mode 100644 index 1cb5756..0000000 --- a/lib/bookmarks/xbel.cpp +++ /dev/null @@ -1,110 +0,0 @@ -/* - * 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: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "xbel.h" -#include "model/bookmarkitem.h" -#include -#include - -inline void readChildElements(QXmlStreamReader &reader, BookmarkItem *parent) -{ - while(reader.readNextStartElement()) { - if(reader.name() == "title") { - parent->setData(BookmarkItem::Title, reader.readElementText()); - - } else if(reader.name() == "tags") { - parent->setData(BookmarkItem::Tags, reader.readElementText().split(";")); - - } else if(reader.name() == "description") { - parent->setData(BookmarkItem::Description, reader.readElementText()); - - } else if(reader.name() == "folder") { - auto *item = new BookmarkItem({}, BookmarkItem::Folder, parent); - item->setExpanded(!(reader.attributes().value("folded") == QLatin1Literal("yes"))); - parent->appendChild(item); - readChildElements(reader, item); - - } else if(reader.name() == "bookmark") { - auto *item = new BookmarkItem({}, BookmarkItem::Bookmark, parent); - item->setData(BookmarkItem::Href, reader.attributes().value("href").toString()); - parent->appendChild(item); - readChildElements(reader, item); - - } else { - reader.skipCurrentElement(); - } - } -} - -void Xbel::read(QIODevice *device, BookmarkItem *item) -{ - QXmlStreamReader qXmlStreamReader(device); - - if(qXmlStreamReader.readNextStartElement()) { - if(!(qXmlStreamReader.name() == "xbel" && qXmlStreamReader.attributes().value("version") == "1.0")) { - return; - } - - readChildElements(qXmlStreamReader, item); - } -} - -inline void writeChildElements(QXmlStreamWriter &writer, const BookmarkItem *item) -{ - switch(item->type()) { - case BookmarkItem::Root: - for(int i = 0; i < item->childCount(); ++i) { - writeChildElements(writer, item->child(i)); - } - break; - - case BookmarkItem::Folder: - writer.writeStartElement("folder"); - writer.writeAttribute("folded", !item->isExpanded() ? "yes" : "no"); - writer.writeTextElement("title", item->data(BookmarkItem::Title).toString()); - if(!item->data(BookmarkItem::Tags).isNull()) - writer.writeTextElement("tags", item->data(BookmarkItem::Tags).toStringList().join(";")); - if(!item->data(BookmarkItem::Description).isNull()) - writer.writeTextElement("description", item->data(BookmarkItem::Description).toString()); - - for(int i = 0; i < item->childCount(); ++i) { - writeChildElements(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()); - if(!item->data(BookmarkItem::Tags).isNull()) - writer.writeTextElement("tags", item->data(BookmarkItem::Tags).toStringList().join(";")); - if(!item->data(BookmarkItem::Description).isNull()) - writer.writeTextElement("description", item->data(BookmarkItem::Description).toString()); - - writer.writeEndElement(); - break; - } -} - -void Xbel::write(QIODevice *device, const BookmarkItem *item) -{ - QXmlStreamWriter xmlWriter(device); - xmlWriter.setAutoFormatting(true); - - xmlWriter.writeStartDocument(); - xmlWriter.writeDTD(""); - - xmlWriter.writeStartElement("xbel"); - xmlWriter.writeAttribute("version", "1.0"); - - writeChildElements(xmlWriter, item); - - xmlWriter.writeEndDocument(); -} diff --git a/lib/bookmarks/xbel.h b/lib/bookmarks/xbel.h deleted file mode 100644 index 44a65bb..0000000 --- a/lib/bookmarks/xbel.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - * 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: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef XBEL_H -#define XBEL_H - -class QIODevice; -class BookmarkItem; -namespace Xbel -{ -void read(QIODevice *device, BookmarkItem *item); -void write(QIODevice *device, const BookmarkItem *item); -} - -#endif // XBEL_H -- cgit v1.2.1