diff options
Diffstat (limited to 'lib/bookmarks/formats')
-rw-r--r-- | lib/bookmarks/formats/ffjson.cpp | 72 | ||||
-rw-r--r-- | lib/bookmarks/formats/ffjson.h | 19 | ||||
-rw-r--r-- | lib/bookmarks/formats/format.cpp | 26 | ||||
-rw-r--r-- | lib/bookmarks/formats/format.h | 36 | ||||
-rw-r--r-- | lib/bookmarks/formats/xbel.cpp | 26 |
5 files changed, 114 insertions, 65 deletions
diff --git a/lib/bookmarks/formats/ffjson.cpp b/lib/bookmarks/formats/ffjson.cpp new file mode 100644 index 0000000..1fe6a96 --- /dev/null +++ b/lib/bookmarks/formats/ffjson.cpp @@ -0,0 +1,72 @@ +/* + * 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 "ffjson.h" +#include "bookmarkitem.h" +#include <QJsonDocument> +#include <QJsonObject> +#include <QJsonArray> +#include <QDebug> +#include <QDateTime> + +inline auto asDate(const QJsonValue &v) +{ + // timestamps in bookmarks.json are, for some reason, in *micro*seconds + return QDateTime::fromMSecsSinceEpoch(v.toVariant().toLongLong() / 1000); +} + +inline void readElementData(const QJsonObject &object, BookmarkItem *item) +{ + item->setData(BookmarkItem::Title, object["title"].toString()); + item->setData(BookmarkItem::DateAdded, asDate(object["dateAdded"])); + item->setData(BookmarkItem::LastModified, asDate(object["lastModified"])); + + // tags + const auto tags = object["tags"].toString().split(","); + if(!tags.isEmpty()) + item->setData(BookmarkItem::Tags, tags); + + // descriptions + for(const auto &a : object["annos"].toArray()) { + const auto anno = a.toObject(); + if(anno["name"] == "bookmarkProperties/description") { + item->setData(BookmarkItem::Description, anno["value"].toString()); + } + } +} + +void readChildElements(const QJsonObject &object, BookmarkItem *item) +{ + for(const auto c : object["children"].toArray()) { + const auto child = c.toObject(); + const auto type = child["type"].toString(); + + if(type == "text/x-moz-place-container") { + auto *childItem = new BookmarkItem({}, BookmarkItem::Folder, item); + childItem->setExpanded(true); + readElementData(child, childItem); + item->appendChild(childItem); + readChildElements(child, childItem); + + } else if(type == "text/x-moz-place") { + auto *childItem = new BookmarkItem({}, BookmarkItem::Bookmark, item); + readElementData(child, childItem); + childItem->setData(BookmarkItem::Href, child["uri"].toString()); + item->appendChild(childItem); + + } else { + qDebug() << "!!! unknown type " << qUtf8Printable(type); + } + } +} + +void FFJson::read(QIODevice *device, BookmarkItem *item) +{ + const auto doc = QJsonDocument::fromJson(device->readAll()); + readChildElements(doc.object(), item); +} diff --git a/lib/bookmarks/formats/ffjson.h b/lib/bookmarks/formats/ffjson.h new file mode 100644 index 0000000..cc6ac24 --- /dev/null +++ b/lib/bookmarks/formats/ffjson.h @@ -0,0 +1,19 @@ +/* + * 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 FFJSON_H +#define FFJSON_H + +class QIODevice; +class BookmarkItem; +namespace FFJson +{ +void read(QIODevice *device, BookmarkItem *item); +} + +#endif // FFJSON_H diff --git a/lib/bookmarks/formats/format.cpp b/lib/bookmarks/formats/format.cpp deleted file mode 100644 index 551151c..0000000 --- a/lib/bookmarks/formats/format.cpp +++ /dev/null @@ -1,26 +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 "format.h" -#include "xbel.h" -#include <QIODevice> - -template<> -void BookmarksFormat<BookmarksFormats::XbelFormat>::read(BookmarkItem *root) -{ - Q_CHECK_PTR(m_device); - Xbel::read(m_device, root); -} - -template<> -void BookmarksFormat<BookmarksFormats::XbelFormat>::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 deleted file mode 100644 index e96dfcc..0000000 --- a/lib/bookmarks/formats/format.h +++ /dev/null @@ -1,36 +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 BOOKMARKSFORMAT_H -#define BOOKMARKSFORMAT_H - -class QIODevice; -class BookmarkItem; - -enum BookmarksFormats { - XbelFormat -}; - -template<BookmarksFormats format> -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 index 1cb5756..bac2bc8 100644 --- a/lib/bookmarks/formats/xbel.cpp +++ b/lib/bookmarks/formats/xbel.cpp @@ -7,9 +7,10 @@ */ #include "xbel.h" -#include "model/bookmarkitem.h" +#include "bookmarkitem.h" #include <QXmlStreamReader> #include <QXmlStreamWriter> +#include <QDateTime> inline void readChildElements(QXmlStreamReader &reader, BookmarkItem *parent) { @@ -17,6 +18,12 @@ inline void readChildElements(QXmlStreamReader &reader, BookmarkItem *parent) if(reader.name() == "title") { parent->setData(BookmarkItem::Title, reader.readElementText()); + } else if(reader.name() == "dateAdded") { + parent->setData(BookmarkItem::DateAdded, QDateTime::fromString(reader.readElementText(), Qt::RFC2822Date)); + + } else if(reader.name() == "lastModified") { + parent->setData(BookmarkItem::LastModified, QDateTime::fromString(reader.readElementText(), Qt::RFC2822Date)); + } else if(reader.name() == "tags") { parent->setData(BookmarkItem::Tags, reader.readElementText().split(";")); @@ -54,6 +61,19 @@ void Xbel::read(QIODevice *device, BookmarkItem *item) } } +inline void writeCommon(QXmlStreamWriter &writer, const BookmarkItem *item) +{ + writer.writeTextElement("title", item->data(BookmarkItem::Title).toString()); + + const auto dateAdded = item->data(BookmarkItem::DateAdded); + if(!dateAdded.isNull()) + writer.writeTextElement("dateAdded", dateAdded.toDateTime().toString(Qt::RFC2822Date)); + + const auto lastModified = item->data(BookmarkItem::LastModified); + if(!lastModified.isNull()) + writer.writeTextElement("lastModified", lastModified.toDateTime().toString(Qt::RFC2822Date)); +} + inline void writeChildElements(QXmlStreamWriter &writer, const BookmarkItem *item) { switch(item->type()) { @@ -66,7 +86,7 @@ inline void writeChildElements(QXmlStreamWriter &writer, const BookmarkItem *ite case BookmarkItem::Folder: writer.writeStartElement("folder"); writer.writeAttribute("folded", !item->isExpanded() ? "yes" : "no"); - writer.writeTextElement("title", item->data(BookmarkItem::Title).toString()); + writeCommon(writer, item); if(!item->data(BookmarkItem::Tags).isNull()) writer.writeTextElement("tags", item->data(BookmarkItem::Tags).toStringList().join(";")); if(!item->data(BookmarkItem::Description).isNull()) @@ -82,7 +102,7 @@ inline void writeChildElements(QXmlStreamWriter &writer, const BookmarkItem *ite case BookmarkItem::Bookmark: writer.writeStartElement("bookmark"); writer.writeAttribute("href", item->data(BookmarkItem::Href).toString()); - writer.writeTextElement("title", item->data(BookmarkItem::Title).toString()); + writeCommon(writer, item); if(!item->data(BookmarkItem::Tags).isNull()) writer.writeTextElement("tags", item->data(BookmarkItem::Tags).toStringList().join(";")); if(!item->data(BookmarkItem::Description).isNull()) |