diff options
-rw-r--r-- | lib/bookmarks/bookmarkformat.h | 2 | ||||
-rw-r--r-- | lib/bookmarks/bookmarkitem.h | 2 | ||||
-rw-r--r-- | lib/bookmarks/bookmarkmodel.cpp | 3 | ||||
-rw-r--r-- | lib/bookmarks/formats/ffjson.cpp | 18 | ||||
-rw-r--r-- | lib/bookmarks/formats/xbel.cpp | 24 | ||||
-rw-r--r-- | src/bookmarks/editbookmarkdialog.cpp | 4 |
6 files changed, 48 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()) diff --git a/src/bookmarks/editbookmarkdialog.cpp b/src/bookmarks/editbookmarkdialog.cpp index 7df90b8..4461840 100644 --- a/src/bookmarks/editbookmarkdialog.cpp +++ b/src/bookmarks/editbookmarkdialog.cpp @@ -10,6 +10,7 @@ #include "bookmarkitem.h" #include "bookmarkmodel.h" #include "ui_editbookmarkdialog.h" +#include <QDateTime> EditBookmarkDialog::EditBookmarkDialog(BookmarkModel *model, const QModelIndex &index, QWidget *parent) : QDialog(parent) @@ -67,4 +68,7 @@ void EditBookmarkDialog::saveChanges() if(descriptionChanged) { m_model->setData(m_index, ui->description->toPlainText(), BookmarkItem::Description, Qt::DisplayRole); } + + if(titleChanged || hrefChanged || tagsChanged || descriptionChanged) + m_model->setData(m_index, QDateTime::currentDateTime(), BookmarkItem::LastModified, Qt::DisplayRole); } |