1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
#include "rssitem.h"
#include <algorithm>
#include <cinttypes>
#include <langinfo.h>
#include "cache.h"
#include "dbexception.h"
#include "rssfeed.h"
#include "scopemeasure.h"
#include "strprintf.h"
#include "utils.h"
namespace newsboat {
RssItem::RssItem(Cache* c)
: ch(c)
, idx(0)
, size_(0)
, pubDate_(0)
, unread_(true)
, enqueued_(false)
, deleted_(0)
, override_unread_(false)
{
}
RssItem::~RssItem() {}
// RssItem setters
void RssItem::set_title(const std::string& t)
{
title_ = utils::consolidate_whitespace(t);
utils::trim(title_);
}
void RssItem::set_link(const std::string& l)
{
link_ = l;
utils::trim(link_);
}
void RssItem::set_author(const std::string& a)
{
author_ = a;
}
void RssItem::set_description(const std::string& content,
const std::string& mime_type)
{
std::lock_guard<std::mutex> guard(description_mutex);
description_ = {content, mime_type};
}
void RssItem::set_size(unsigned int size)
{
size_ = size;
}
std::string RssItem::length() const
{
std::string::size_type l(size_);
if (!l) {
return "";
}
if (l < 1000) {
return strprintf::fmt("%" PRIu64 " ", static_cast<uint64_t>(l));
}
if (l < 1024 * 1000) {
return strprintf::fmt("%.1fK", l / 1024.0);
}
return strprintf::fmt("%.1fM", l / 1024.0 / 1024.0);
}
void RssItem::set_pubDate(time_t t)
{
pubDate_ = t;
}
void RssItem::set_guid(const std::string& g)
{
guid_ = g;
}
void RssItem::set_unread_nowrite(bool u)
{
unread_ = u;
}
void RssItem::set_unread_nowrite_notify(bool u, bool notify)
{
unread_ = u;
std::shared_ptr<RssFeed> feedptr = feedptr_.lock();
if (feedptr && notify) {
feedptr->get_item_by_guid(guid_)->set_unread_nowrite(
unread_); // notify parent feed
}
}
void RssItem::set_unread(bool u)
{
if (unread_ != u) {
bool old_u = unread_;
unread_ = u;
std::shared_ptr<RssFeed> feedptr = feedptr_.lock();
if (feedptr)
feedptr->get_item_by_guid(guid_)->set_unread_nowrite(
unread_); // notify parent feed
try {
if (ch) {
ch->update_rssitem_unread_and_enqueued(
*this, feedurl_);
}
} catch (const DbException& e) {
// if the update failed, restore the old unread flag and
// rethrow the exception
unread_ = old_u;
throw;
}
}
}
std::string RssItem::pubDate() const
{
return utils::mt_strf_localtime(_("%a, %d %b %Y %T %z"), pubDate_);
}
const std::string& RssItem::feedurl() const
{
return feedurl_;
}
const std::string& RssItem::enclosure_url() const
{
return enclosure_url_;
}
const std::string& RssItem::enclosure_type() const
{
return enclosure_type_;
}
const std::string& RssItem::enclosure_description() const
{
return enclosure_description_;
}
const std::string& RssItem::enclosure_description_mime_type() const
{
return enclosure_description_mime_type_;
}
void RssItem::set_enclosure_url(const std::string& url)
{
enclosure_url_ = url;
}
void RssItem::set_enclosure_type(const std::string& type)
{
enclosure_type_ = type;
}
void RssItem::set_enclosure_description(const std::string& description)
{
enclosure_description_ = description;
}
void RssItem::set_enclosure_description_mime_type(const std::string& type)
{
enclosure_description_mime_type_ = type;
}
nonstd::optional<std::string> RssItem::attribute_value(const std::string&
attribname) const
{
if (attribname == "title") {
return utils::utf8_to_locale(title());
} else if (attribname == "link") {
return link();
} else if (attribname == "author") {
return utils::utf8_to_locale(author());
} else if (attribname == "content") {
ScopeMeasure sm("RssItem::attribute_value(\"content\")");
std::lock_guard<std::mutex> guard(description_mutex);
if (description_.has_value()) {
const std::string content = description_.value().text;
return utils::utf8_to_locale(content);
} else if (ch) {
std::string description = ch->fetch_description(*this);
return utils::utf8_to_locale(description);
}
return "";
} else if (attribname == "date") {
return pubDate();
} else if (attribname == "guid") {
return guid();
} else if (attribname == "unread") {
return unread_ ? "yes" : "no";
} else if (attribname == "enclosure_url") {
return enclosure_url();
} else if (attribname == "enclosure_type") {
return enclosure_type();
} else if (attribname == "flags") {
return flags();
} else if (attribname == "age")
return std::to_string(
(time(nullptr) - pubDate_timestamp()) / 86400);
else if (attribname == "articleindex") {
return std::to_string(idx);
}
// if we have a feed, then forward the request
std::shared_ptr<RssFeed> feedptr = feedptr_.lock();
if (feedptr) {
return feedptr->RssFeed::attribute_value(attribname);
}
return nonstd::nullopt;
}
void RssItem::update_flags()
{
if (ch) {
ch->update_rssitem_flags(this);
}
}
void RssItem::set_flags(const std::string& ff)
{
oldflags_ = flags_;
flags_ = ff;
sort_flags();
}
void RssItem::sort_flags()
{
std::sort(flags_.begin(), flags_.end());
// Erase non-alpha characters
flags_.erase(std::remove_if(flags_.begin(),
flags_.end(),
[](const char c) {
return !isalpha(c);
}),
flags_.end());
// Erase doubled characters
flags_.erase(std::unique(flags_.begin(), flags_.end()), flags_.end());
}
void RssItem::set_feedptr(std::shared_ptr<RssFeed> ptr)
{
feedptr_ = std::weak_ptr<RssFeed>(ptr);
}
void RssItem::set_feedptr(const std::weak_ptr<RssFeed>& ptr)
{
feedptr_ = ptr;
}
} // namespace newsboat
|