qBittorrent
rss_article.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2017 Vladimir Golovnev <[email protected]>
4  * Copyright (C) 2010 Christophe Dumez <[email protected]>
5  * Copyright (C) 2010 Arnaud Demaiziere <[email protected]>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * In addition, as a special exception, the copyright holders give permission to
22  * link this program with the OpenSSL project's "OpenSSL" library (or with
23  * modified versions of it that use the same license as the "OpenSSL" library),
24  * and distribute the linked executables. You must obey the GNU General Public
25  * License in all respects for all of the code used other than "OpenSSL". If you
26  * modify file(s), you may extend this exception to your version of the file(s),
27  * but you are not obligated to do so. If you do not wish to do so, delete this
28  * exception statement from your version.
29  */
30 
31 #include "rss_article.h"
32 
33 #include <QJsonObject>
34 #include <QVariant>
35 
36 #include "rss_feed.h"
37 
38 using namespace RSS;
39 
40 namespace
41 {
42  QVariantHash articleDataFromJSON(const QJsonObject &jsonObj)
43  {
44  auto varHash = jsonObj.toVariantHash();
45  // JSON object store DateTime as string so we need to convert it
46  varHash[Article::KeyDate] =
47  QDateTime::fromString(jsonObj.value(Article::KeyDate).toString(), Qt::RFC2822Date);
48 
49  return varHash;
50  }
51 }
52 
53 const QString Article::KeyId(QStringLiteral("id"));
54 const QString Article::KeyDate(QStringLiteral("date"));
55 const QString Article::KeyTitle(QStringLiteral("title"));
56 const QString Article::KeyAuthor(QStringLiteral("author"));
57 const QString Article::KeyDescription(QStringLiteral("description"));
58 const QString Article::KeyTorrentURL(QStringLiteral("torrentURL"));
59 const QString Article::KeyLink(QStringLiteral("link"));
60 const QString Article::KeyIsRead(QStringLiteral("isRead"));
61 
62 Article::Article(Feed *feed, const QVariantHash &varHash)
63  : QObject(feed)
64  , m_feed(feed)
65  , m_guid(varHash.value(KeyId).toString())
66  , m_date(varHash.value(KeyDate).toDateTime())
67  , m_title(varHash.value(KeyTitle).toString())
68  , m_author(varHash.value(KeyAuthor).toString())
69  , m_description(varHash.value(KeyDescription).toString())
70  , m_torrentURL(varHash.value(KeyTorrentURL).toString())
71  , m_link(varHash.value(KeyLink).toString())
72  , m_isRead(varHash.value(KeyIsRead, false).toBool())
73  , m_data(varHash)
74 {
75 }
76 
77 Article::Article(Feed *feed, const QJsonObject &jsonObj)
78  : Article(feed, articleDataFromJSON(jsonObj))
79 {
80 }
81 
82 QString Article::guid() const
83 {
84  return m_guid;
85 }
86 
87 QDateTime Article::date() const
88 {
89  return m_date;
90 }
91 
92 QString Article::title() const
93 {
94  return m_title;
95 }
96 
97 QString Article::author() const
98 {
99  return m_author;
100 }
101 
102 QString Article::description() const
103 {
104  return m_description;
105 }
106 
107 QString Article::torrentUrl() const
108 {
109  return (m_torrentURL.isEmpty() ? m_link : m_torrentURL);
110 }
111 
112 QString Article::link() const
113 {
114  return m_link;
115 }
116 
117 bool Article::isRead() const
118 {
119  return m_isRead;
120 }
121 
122 QVariantHash Article::data() const
123 {
124  return m_data;
125 }
126 
128 {
129  if (!m_isRead)
130  {
131  m_isRead = true;
133  emit read(this);
134  }
135 }
136 
137 QJsonObject Article::toJsonObject() const
138 {
139  auto jsonObj = QJsonObject::fromVariantHash(m_data);
140  // JSON object doesn't support DateTime so we need to convert it
141  jsonObj[KeyDate] = m_date.toString(Qt::RFC2822Date);
142 
143  return jsonObj;
144 }
145 
146 bool Article::articleDateRecentThan(const Article *article, const QDateTime &date)
147 {
148  return article->date() > date;
149 }
150 
152 {
153  return m_feed;
154 }
Definition: rss_article.h:43
QDateTime m_date
Definition: rss_article.h:85
Feed * m_feed
Definition: rss_article.h:83
QString m_link
Definition: rss_article.h:90
QString m_description
Definition: rss_article.h:88
QString m_title
Definition: rss_article.h:86
static const QString KeyIsRead
Definition: rss_article.h:60
Article(Feed *feed, const QVariantHash &varHash)
Definition: rss_article.cpp:62
static const QString KeyAuthor
Definition: rss_article.h:56
static const QString KeyDate
Definition: rss_article.h:54
QJsonObject toJsonObject() const
QString guid() const
Definition: rss_article.cpp:82
void read(Article *article=nullptr)
static bool articleDateRecentThan(const Article *article, const QDateTime &date)
QString m_guid
Definition: rss_article.h:84
static const QString KeyDescription
Definition: rss_article.h:57
QVariantHash m_data
Definition: rss_article.h:92
void markAsRead()
QString torrentUrl() const
QString link() const
static const QString KeyLink
Definition: rss_article.h:59
bool m_isRead
Definition: rss_article.h:91
QString title() const
Definition: rss_article.cpp:92
QString m_author
Definition: rss_article.h:87
static const QString KeyId
Definition: rss_article.h:53
QString author() const
Definition: rss_article.cpp:97
static const QString KeyTitle
Definition: rss_article.h:55
static const QString KeyTorrentURL
Definition: rss_article.h:58
QString description() const
QVariantHash data() const
Feed * feed() const
QDateTime date() const
Definition: rss_article.cpp:87
bool isRead() const
QString m_torrentURL
Definition: rss_article.h:89
T value(const QString &key, const T &defaultValue={})
Definition: preferences.cpp:64
QVariantHash articleDataFromJSON(const QJsonObject &jsonObj)
Definition: rss_article.cpp:42
QString toString(const lt::socket_type_t socketType)
Definition: session.cpp:183