aboutsummaryrefslogtreecommitdiff
path: root/lib/bookmarks
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2020-01-17 10:25:27 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2020-01-17 11:18:34 +0200
commit5f708d4618d739d14442b85c466fdbac84a74cc8 (patch)
treee253289a1686f302df62d0f0865b9dcd6bfa6412 /lib/bookmarks
parentAdd Firefox bookmarks.json format to libbookmarks (diff)
downloadsmolbote-5f708d4618d739d14442b85c466fdbac84a74cc8.tar.xz
BookmarkItem::DateAdded and LastModified fields
- add read support in FFJson for DateAdded and LastModified fields - add read/write support in Xbel for DateAdded and LastModified fields - BookmarkModel: set DateAdded on appendBookmark and appendFolder - EditBookmarkDialog: set LastModified field when saving changes
Diffstat (limited to 'lib/bookmarks')
-rw-r--r--lib/bookmarks/bookmarkformat.h2
-rw-r--r--lib/bookmarks/bookmarkitem.h2
-rw-r--r--lib/bookmarks/bookmarkmodel.cpp3
-rw-r--r--lib/bookmarks/formats/ffjson.cpp18
-rw-r--r--lib/bookmarks/formats/xbel.cpp24
5 files changed, 44 insertions, 5 deletions
diff --git a/lib/bookmarks/bookmarkformat.h b/lib/bookmarks/bookmarkformat.h
index 5cd1860..8b64420 100644
--- a/lib/bookmarks/bookmarkformat.h
+++ b/lib/bookmarks/bookmarkformat.h
@@ -24,7 +24,7 @@ class BookmarkFormat
public:
explicit BookmarkFormat(QIODevice *device)
{
- Q_CHECK_PTR(m_device);
+ Q_CHECK_PTR(device);
m_device = device;
}
~BookmarkFormat()
diff --git a/lib/bookmarks/bookmarkitem.h b/lib/bookmarks/bookmarkitem.h
index 8c9463f..6751526 100644
--- a/lib/bookmarks/bookmarkitem.h
+++ b/lib/bookmarks/bookmarkitem.h
@@ -27,6 +27,8 @@ public:
Href,
Tags,
Description,
+ DateAdded,
+ LastModified,
FieldCount
};
diff --git a/lib/bookmarks/bookmarkmodel.cpp b/lib/bookmarks/bookmarkmodel.cpp
index 895b178..05df3d7 100644
--- a/lib/bookmarks/bookmarkmodel.cpp
+++ b/lib/bookmarks/bookmarkmodel.cpp
@@ -12,6 +12,7 @@
#include <QBuffer>
#include <QMimeData>
#include <QRegularExpression>
+#include <QDateTime>
BookmarkModel::BookmarkModel(QObject *parent)
: QAbstractItemModel(parent)
@@ -132,6 +133,7 @@ QModelIndex BookmarkModel::appendBookmark(const QString &title, const QString &u
int row = parentItem->childCount();
beginInsertRows(parent, row, row);
auto *childItem = new BookmarkItem({ title, url }, BookmarkItem::Bookmark, parentItem);
+ childItem->setData(BookmarkItem::DateAdded, QDateTime::currentDateTime());
parentItem->appendChild(childItem);
endInsertRows();
@@ -146,6 +148,7 @@ QModelIndex BookmarkModel::appendFolder(const QString &title, const QModelIndex
beginInsertRows(parent, row, row);
auto *childItem = new BookmarkItem({ title }, BookmarkItem::Folder, parentItem);
+ childItem->setData(BookmarkItem::DateAdded, QDateTime::currentDateTime());
parentItem->appendChild(childItem);
endInsertRows();
diff --git a/lib/bookmarks/formats/ffjson.cpp b/lib/bookmarks/formats/ffjson.cpp
index f173904..98f8e60 100644
--- a/lib/bookmarks/formats/ffjson.cpp
+++ b/lib/bookmarks/formats/ffjson.cpp
@@ -12,6 +12,20 @@
#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"]));
+}
void readChildElements(const QJsonObject &object, BookmarkItem *item)
{
@@ -22,13 +36,13 @@ void readChildElements(const QJsonObject &object, BookmarkItem *item)
if(type == "text/x-moz-place-container") {
auto *childItem = new BookmarkItem({}, BookmarkItem::Folder, item);
childItem->setExpanded(true);
- childItem->setData(BookmarkItem::Title, child["title"].toString());
+ readElementData(child, childItem);
item->appendChild(childItem);
readChildElements(child, childItem);
} else if(type == "text/x-moz-place") {
auto *childItem = new BookmarkItem({}, BookmarkItem::Bookmark, item);
- childItem->setData(BookmarkItem::Title, child["title"].toString());
+ readElementData(child, childItem);
childItem->setData(BookmarkItem::Href, child["uri"].toString());
item->appendChild(childItem);
diff --git a/lib/bookmarks/formats/xbel.cpp b/lib/bookmarks/formats/xbel.cpp
index 174995d..bac2bc8 100644
--- a/lib/bookmarks/formats/xbel.cpp
+++ b/lib/bookmarks/formats/xbel.cpp
@@ -10,6 +10,7 @@
#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())