21
21
#include <functional>
23
using namespace newsbeuter;
23
namespace newsbeuter {
25
rss_item::rss_item(cache * c) : unread_(true), ch(c), enqueued_(false), deleted_(0) {
26
// GetLogger().log(LOG_CRITICAL, "new rss_item");
25
rss_item::rss_item(cache * c) : unread_(true), ch(c), enqueued_(false), deleted_(0), idx(0) {
26
// LOG(LOG_CRITICAL, "new rss_item");
29
29
rss_item::~rss_item() {
30
// GetLogger().log(LOG_CRITICAL, "delete rss_item");
30
// LOG(LOG_CRITICAL, "delete rss_item");
33
rss_feed::rss_feed(cache * c) : ch(c), empty(true), is_rtl_(false) {
34
// GetLogger().log(LOG_CRITICAL, "new rss_feed");
33
rss_feed::rss_feed(cache * c) : ch(c), empty(true), is_rtl_(false), idx(0) {
34
// LOG(LOG_CRITICAL, "new rss_feed");
37
37
rss_feed::rss_feed() : ch(NULL), empty(true), is_rtl_(false) {
38
// GetLogger().log(LOG_CRITICAL, "new rss_feed");
38
// LOG(LOG_CRITICAL, "new rss_feed");
41
41
rss_feed::~rss_feed() {
42
// GetLogger().log(LOG_CRITICAL, "delete rss_feed");
42
// LOG(LOG_CRITICAL, "delete rss_feed");
45
45
// rss_item setters
189
188
std::tr1::shared_ptr<rss_item> rss_feed::get_item_by_guid(const std::string& guid) {
189
scope_mutex lock(&item_mutex);
190
190
for (std::vector<std::tr1::shared_ptr<rss_item> >::iterator it=items_.begin();it!=items_.end();++it) {
191
191
if ((*it)->guid() == guid) {
195
GetLogger().log(LOG_DEBUG, "rss_feed::get_item_by_guid: hit dummy item!");
195
LOG(LOG_DEBUG, "rss_feed::get_item_by_guid: hit dummy item!");
197
197
return std::tr1::shared_ptr<rss_item>(new rss_item(ch)); // should never happen!
200
200
bool rss_item::has_attribute(const std::string& attribname) {
201
// GetLogger().log(LOG_DEBUG, "rss_item::has_attribute(%s) called", attribname.c_str());
201
// LOG(LOG_DEBUG, "rss_item::has_attribute(%s) called", attribname.c_str());
202
202
if (attribname == "title" ||
203
203
attribname == "link" ||
204
204
attribname == "author" ||
303
310
return utils::to_s(items_.size());
304
311
} else if (attribname == "tags") {
305
312
return get_tags();
313
} else if (attribname == "feedindex") {
314
return utils::to_s(idx);
310
action_handler_status rss_ignores::handle_action(const std::string& action, const std::vector<std::string>& params) {
319
void rss_ignores::handle_action(const std::string& action, const std::vector<std::string>& params) {
311
320
if (action == "ignore-article") {
312
if (params.size() >= 2) {
313
std::string ignore_rssurl = params[0];
314
std::string ignore_expr = params[1];
316
if (m.parse(ignore_expr)) {
317
ignores.push_back(feedurl_expr_pair(ignore_rssurl, new matcher(ignore_expr)));
320
return AHS_INVALID_PARAMS;
323
return AHS_TOO_FEW_PARAMS;
321
if (params.size() < 2)
322
throw confighandlerexception(AHS_TOO_FEW_PARAMS);
323
std::string ignore_rssurl = params[0];
324
std::string ignore_expr = params[1];
326
if (!m.parse(ignore_expr))
327
throw confighandlerexception(utils::strprintf(_("couldn't parse filter expression `%s': %s"), ignore_expr.c_str(), m.get_parse_error().c_str()));
328
ignores.push_back(feedurl_expr_pair(ignore_rssurl, new matcher(ignore_expr)));
325
329
} else if (action == "always-download") {
326
330
for (std::vector<std::string>::const_iterator it=params.begin();it!=params.end();++it) {
327
331
ignores_lastmodified.push_back(*it);
330
333
} else if (action == "reset-unread-on-update") {
331
334
for (std::vector<std::string>::const_iterator it=params.begin();it!=params.end();++it) {
332
335
resetflag.push_back(*it);
336
return AHS_INVALID_COMMAND;
338
throw confighandlerexception(AHS_INVALID_COMMAND);
341
void rss_ignores::dump_config(std::vector<std::string>& config_output) {
342
for (std::vector<feedurl_expr_pair>::iterator it = ignores.begin();it!=ignores.end();it++) {
343
std::string configline = "ignore-article ";
344
if (it->first == "*")
345
configline.append("*");
347
configline.append(utils::quote(it->first));
348
configline.append(" ");
349
configline.append(utils::quote(it->second->get_expression()));
350
config_output.push_back(configline);
352
for (std::vector<std::string>::iterator it=ignores_lastmodified.begin();it!=ignores_lastmodified.end();it++) {
353
config_output.push_back(utils::strprintf("always-download %s", utils::quote(*it).c_str()));
355
for (std::vector<std::string>::iterator it=resetflag.begin();it!=resetflag.end();it++) {
356
config_output.push_back(utils::strprintf("reset-unread-on-update %s", utils::quote(*it).c_str()));
339
360
rss_ignores::~rss_ignores() {
404
426
gettimeofday(&tv2, NULL);
405
427
unsigned long diff = (((tv2.tv_sec - tv1.tv_sec) * 1000000) + tv2.tv_usec) - tv1.tv_usec;
406
428
unsigned long diffx = (((tv2.tv_sec - tvx.tv_sec) * 1000000) + tv2.tv_usec) - tvx.tv_usec;
407
GetLogger().log(LOG_DEBUG, "rss_feed::update_items matching took %lu.%06lu s", diff / 1000000, diff % 1000000);
408
GetLogger().log(LOG_DEBUG, "rss_feed::update_items sorting took %lu.%06lu s", diffx / 1000000, diffx % 1000000);
429
LOG(LOG_DEBUG, "rss_feed::update_items matching took %lu.%06lu s", diff / 1000000, diff % 1000000);
430
LOG(LOG_DEBUG, "rss_feed::update_items sorting took %lu.%06lu s", diffx / 1000000, diffx % 1000000);
411
433
void rss_feed::set_rssurl(const std::string& u) {
413
435
if (rssurl_.substr(0,6) == "query:") {
414
436
std::vector<std::string> tokens = utils::tokenize_quoted(u, ":");
415
GetLogger().log(LOG_DEBUG, "rss_feed::set_rssurl: query name = `%s' expr = `%s'", tokens[1].c_str(), tokens[2].c_str());
437
LOG(LOG_DEBUG, "rss_feed::set_rssurl: query name = `%s' expr = `%s'", tokens[1].c_str(), tokens[2].c_str());
416
438
set_title(tokens[1]);
417
439
set_query(tokens[2]);
421
struct sort_item_by_title : public std::binary_function<const std::tr1::shared_ptr<rss_item>&, const std::tr1::shared_ptr<rss_item>&, bool> {
443
struct sort_item_by_title : public std::binary_function<std::tr1::shared_ptr<rss_item>, std::tr1::shared_ptr<rss_item>, bool> {
422
444
sort_item_by_title() { }
423
bool operator()(const std::tr1::shared_ptr<rss_item>& a, const std::tr1::shared_ptr<rss_item>& b) {
445
bool operator()(std::tr1::shared_ptr<rss_item> a, std::tr1::shared_ptr<rss_item> b) {
424
446
return strcasecmp(a->title().c_str(), b->title().c_str()) < 0;
428
struct sort_item_by_flags : public std::binary_function<const std::tr1::shared_ptr<rss_item>&, const std::tr1::shared_ptr<rss_item>&, bool> {
450
struct sort_item_by_flags : public std::binary_function<std::tr1::shared_ptr<rss_item>, std::tr1::shared_ptr<rss_item>, bool> {
429
451
sort_item_by_flags() { }
430
bool operator()(const std::tr1::shared_ptr<rss_item>& a, const std::tr1::shared_ptr<rss_item>& b) {
452
bool operator()(std::tr1::shared_ptr<rss_item> a, std::tr1::shared_ptr<rss_item> b) {
431
453
return strcmp(a->flags().c_str(), b->flags().c_str()) < 0;
435
struct sort_item_by_author : public std::binary_function<const std::tr1::shared_ptr<rss_item>&, const std::tr1::shared_ptr<rss_item>&, bool> {
457
struct sort_item_by_author : public std::binary_function<std::tr1::shared_ptr<rss_item>, std::tr1::shared_ptr<rss_item>, bool> {
436
458
sort_item_by_author() { }
437
bool operator()(const std::tr1::shared_ptr<rss_item>& a, const std::tr1::shared_ptr<rss_item>& b) {
459
bool operator()(std::tr1::shared_ptr<rss_item> a, std::tr1::shared_ptr<rss_item> b) {
438
460
return strcmp(a->author().c_str(), b->author().c_str()) < 0;
442
struct sort_item_by_link : public std::binary_function<const std::tr1::shared_ptr<rss_item>&, const std::tr1::shared_ptr<rss_item>&, bool> {
464
struct sort_item_by_link : public std::binary_function<std::tr1::shared_ptr<rss_item>, std::tr1::shared_ptr<rss_item>, bool> {
443
465
sort_item_by_link() { }
444
bool operator()(const std::tr1::shared_ptr<rss_item>& a, const std::tr1::shared_ptr<rss_item>& b) {
466
bool operator()(std::tr1::shared_ptr<rss_item> a, std::tr1::shared_ptr<rss_item> b) {
445
467
return strcmp(a->link().c_str(), b->link().c_str()) < 0;
449
struct sort_item_by_guid : public std::binary_function<const std::tr1::shared_ptr<rss_item>&, const std::tr1::shared_ptr<rss_item>&, bool> {
471
struct sort_item_by_guid : public std::binary_function<std::tr1::shared_ptr<rss_item>, std::tr1::shared_ptr<rss_item>, bool> {
450
472
sort_item_by_guid() { }
451
bool operator()(const std::tr1::shared_ptr<rss_item>& a, const std::tr1::shared_ptr<rss_item>& b) {
473
bool operator()(std::tr1::shared_ptr<rss_item> a, std::tr1::shared_ptr<rss_item> b) {
452
474
return strcmp(a->guid().c_str(), b->guid().c_str()) < 0;
478
struct sort_item_by_date : public std::binary_function<std::tr1::shared_ptr<rss_item>, std::tr1::shared_ptr<rss_item>, bool> {
479
sort_item_by_date() { }
480
bool operator()(std::tr1::shared_ptr<rss_item> a, std::tr1::shared_ptr<rss_item> b) {
481
return a->pubDate_timestamp() < b->pubDate_timestamp();
456
485
void rss_feed::sort(const std::string& method) {
486
scope_mutex lock(&item_mutex);
487
sort_unlocked(method);
490
void rss_feed::sort_unlocked(const std::string& method) {
457
491
std::vector<std::string> methods = utils::tokenize(method,"-");
458
492
bool reverse = false;