|
|
|
@ -30,38 +30,53 @@
@@ -30,38 +30,53 @@
|
|
|
|
|
|
|
|
|
|
#include "rss_article.h" |
|
|
|
|
|
|
|
|
|
#include <stdexcept> |
|
|
|
|
#include <QJsonObject> |
|
|
|
|
#include <QVariant> |
|
|
|
|
|
|
|
|
|
#include "rss_feed.h" |
|
|
|
|
|
|
|
|
|
const QString Str_Id(QStringLiteral("id")); |
|
|
|
|
const QString Str_Date(QStringLiteral("date")); |
|
|
|
|
const QString Str_Title(QStringLiteral("title")); |
|
|
|
|
const QString Str_Author(QStringLiteral("author")); |
|
|
|
|
const QString Str_Description(QStringLiteral("description")); |
|
|
|
|
const QString Str_TorrentURL(QStringLiteral("torrentURL")); |
|
|
|
|
const QString Str_Torrent_Url(QStringLiteral("torrent_url")); |
|
|
|
|
const QString Str_Link(QStringLiteral("link")); |
|
|
|
|
const QString Str_News_Link(QStringLiteral("news_link")); |
|
|
|
|
const QString Str_IsRead(QStringLiteral("isRead")); |
|
|
|
|
const QString Str_Read(QStringLiteral("read")); |
|
|
|
|
|
|
|
|
|
using namespace RSS; |
|
|
|
|
|
|
|
|
|
Article::Article(Feed *feed, QString guid, QDateTime date, QString title, QString author |
|
|
|
|
, QString description, QString torrentUrl, QString link, bool isRead) |
|
|
|
|
const QString Article::KeyId(QStringLiteral("id")); |
|
|
|
|
const QString Article::KeyDate(QStringLiteral("date")); |
|
|
|
|
const QString Article::KeyTitle(QStringLiteral("title")); |
|
|
|
|
const QString Article::KeyAuthor(QStringLiteral("author")); |
|
|
|
|
const QString Article::KeyDescription(QStringLiteral("description")); |
|
|
|
|
const QString Article::KeyTorrentURL(QStringLiteral("torrentURL")); |
|
|
|
|
const QString Article::KeyLink(QStringLiteral("link")); |
|
|
|
|
const QString Article::KeyIsRead(QStringLiteral("isRead")); |
|
|
|
|
|
|
|
|
|
Article::Article(Feed *feed, const QVariantHash &varHash) |
|
|
|
|
: QObject(feed) |
|
|
|
|
, m_feed(feed) |
|
|
|
|
, m_guid(guid) |
|
|
|
|
, m_date(date) |
|
|
|
|
, m_title(title) |
|
|
|
|
, m_author(author) |
|
|
|
|
, m_description(description) |
|
|
|
|
, m_torrentURL(torrentUrl) |
|
|
|
|
, m_link(link) |
|
|
|
|
, m_isRead(isRead) |
|
|
|
|
, m_guid(varHash.value(KeyId).toString()) |
|
|
|
|
, m_date(varHash.value(KeyDate).toDateTime()) |
|
|
|
|
, m_title(varHash.value(KeyTitle).toString()) |
|
|
|
|
, m_author(varHash.value(KeyAuthor).toString()) |
|
|
|
|
, m_description(varHash.value(KeyDescription).toString()) |
|
|
|
|
, m_torrentURL(varHash.value(KeyTorrentURL).toString()) |
|
|
|
|
, m_link(varHash.value(KeyLink).toString()) |
|
|
|
|
, m_isRead(varHash.value(KeyIsRead, false).toBool()) |
|
|
|
|
, m_data(varHash) |
|
|
|
|
{ |
|
|
|
|
// If item does not have a guid, fall back to some other identifier
|
|
|
|
|
if (m_guid.isEmpty()) |
|
|
|
|
m_guid = varHash.value(KeyTorrentURL).toString(); |
|
|
|
|
if (m_guid.isEmpty()) |
|
|
|
|
m_guid = varHash.value(KeyTitle).toString(); |
|
|
|
|
if (m_guid.isEmpty()) |
|
|
|
|
throw std::runtime_error("Bad RSS Article data"); |
|
|
|
|
|
|
|
|
|
m_data[KeyId] = m_guid; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Article::Article(Feed *feed, const QJsonObject &jsonObj) |
|
|
|
|
: Article(feed, jsonObj.toVariantHash()) |
|
|
|
|
{ |
|
|
|
|
// JSON object store DateTime as string so we need to convert it
|
|
|
|
|
m_date = QDateTime::fromString(jsonObj.value(KeyDate).toString(), Qt::RFC2822Date); |
|
|
|
|
m_data[KeyDate] = m_date; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
QString Article::guid() const |
|
|
|
@ -104,26 +119,27 @@ bool Article::isRead() const
@@ -104,26 +119,27 @@ bool Article::isRead() const
|
|
|
|
|
return m_isRead; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
QVariantHash Article::data() const |
|
|
|
|
{ |
|
|
|
|
return m_data; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Article::markAsRead() |
|
|
|
|
{ |
|
|
|
|
if (!m_isRead) { |
|
|
|
|
m_isRead = true; |
|
|
|
|
m_data[KeyIsRead] = m_isRead; |
|
|
|
|
emit read(this); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
QJsonObject Article::toJsonObject() const |
|
|
|
|
{ |
|
|
|
|
return { |
|
|
|
|
{Str_Id, m_guid}, |
|
|
|
|
{Str_Date, m_date.toString(Qt::RFC2822Date)}, |
|
|
|
|
{Str_Title, m_title}, |
|
|
|
|
{Str_Author, m_author}, |
|
|
|
|
{Str_Description, m_description}, |
|
|
|
|
{Str_TorrentURL, m_torrentURL}, |
|
|
|
|
{Str_Link, m_link}, |
|
|
|
|
{Str_IsRead, m_isRead} |
|
|
|
|
}; |
|
|
|
|
auto jsonObj = QJsonObject::fromVariantHash(m_data); |
|
|
|
|
// JSON object doesn't support DateTime so we need to convert it
|
|
|
|
|
jsonObj[KeyDate] = m_date.toString(Qt::RFC2822Date); |
|
|
|
|
|
|
|
|
|
return jsonObj; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool Article::articleDateRecentThan(Article *article, const QDateTime &date) |
|
|
|
@ -131,47 +147,6 @@ bool Article::articleDateRecentThan(Article *article, const QDateTime &date)
@@ -131,47 +147,6 @@ bool Article::articleDateRecentThan(Article *article, const QDateTime &date)
|
|
|
|
|
return article->date() > date; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Article *Article::fromJsonObject(Feed *feed, const QJsonObject &jsonObj) |
|
|
|
|
{ |
|
|
|
|
QString guid = jsonObj.value(Str_Id).toString(); |
|
|
|
|
// If item does not have a guid, fall back to some other identifier
|
|
|
|
|
if (guid.isEmpty()) |
|
|
|
|
guid = jsonObj.value(Str_Torrent_Url).toString(); |
|
|
|
|
if (guid.isEmpty()) |
|
|
|
|
guid = jsonObj.value(Str_Title).toString(); |
|
|
|
|
if (guid.isEmpty()) return nullptr; |
|
|
|
|
|
|
|
|
|
return new Article( |
|
|
|
|
feed, guid |
|
|
|
|
, QDateTime::fromString(jsonObj.value(Str_Date).toString(), Qt::RFC2822Date) |
|
|
|
|
, jsonObj.value(Str_Title).toString() |
|
|
|
|
, jsonObj.value(Str_Author).toString() |
|
|
|
|
, jsonObj.value(Str_Description).toString() |
|
|
|
|
, jsonObj.value(Str_TorrentURL).toString() |
|
|
|
|
, jsonObj.value(Str_Link).toString() |
|
|
|
|
, jsonObj.value(Str_IsRead).toBool(false)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Article *Article::fromVariantHash(Feed *feed, const QVariantHash &varHash) |
|
|
|
|
{ |
|
|
|
|
QString guid = varHash[Str_Id].toString(); |
|
|
|
|
// If item does not have a guid, fall back to some other identifier
|
|
|
|
|
if (guid.isEmpty()) |
|
|
|
|
guid = varHash.value(Str_Torrent_Url).toString(); |
|
|
|
|
if (guid.isEmpty()) |
|
|
|
|
guid = varHash.value(Str_Title).toString(); |
|
|
|
|
if (guid.isEmpty()) return nullptr; |
|
|
|
|
|
|
|
|
|
return new Article(feed, guid |
|
|
|
|
, varHash.value(Str_Date).toDateTime() |
|
|
|
|
, varHash.value(Str_Title).toString() |
|
|
|
|
, varHash.value(Str_Author).toString() |
|
|
|
|
, varHash.value(Str_Description).toString() |
|
|
|
|
, varHash.value(Str_Torrent_Url).toString() |
|
|
|
|
, varHash.value(Str_News_Link).toString() |
|
|
|
|
, varHash.value(Str_Read, false).toBool()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Feed *Article::feed() const |
|
|
|
|
{ |
|
|
|
|
return m_feed; |
|
|
|
|