summaryrefslogtreecommitdiff
path: root/src/feedcontainer.cpp
blob: cbf4766e955a397a9e6f60ac909312a8f0b541dc (plain) (blame)
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
#include "feedcontainer.h"

#include <algorithm> // stable_sort
#include <numeric>   // accumulate

#include "utils.h"

namespace newsboat {

void FeedContainer::sort_feeds(const FeedSortStrategy& sort_strategy)
{
	std::lock_guard<std::mutex> feedslock(feeds_mutex);

	switch (sort_strategy.sm) {
	case FeedSortMethod::NONE:
		std::stable_sort(feeds.begin(),
			feeds.end(),
			[](std::shared_ptr<RssFeed> a,
				std::shared_ptr<RssFeed> b) {
				return a->get_order() < b->get_order();
			});
		break;
	case FeedSortMethod::FIRST_TAG:
		std::stable_sort(feeds.begin(),
			feeds.end(),
			[](std::shared_ptr<RssFeed> a,
				std::shared_ptr<RssFeed> b) {
				if (a->get_firsttag().length() == 0 ||
					b->get_firsttag().length() == 0) {
					return a->get_firsttag().length() >
						b->get_firsttag().length();
				}
				return utils::strnaturalcmp(a->get_firsttag().c_str(),
					       b->get_firsttag().c_str()) < 0;
			});
		break;
	case FeedSortMethod::TITLE:
		std::stable_sort(feeds.begin(),
			feeds.end(),
			[](std::shared_ptr<RssFeed> a,
				std::shared_ptr<RssFeed> b) {
				return utils::strnaturalcmp(a->title().c_str(),
					       b->title().c_str()) < 0;
			});
		break;
	case FeedSortMethod::ARTICLE_COUNT:
		std::stable_sort(feeds.begin(),
			feeds.end(),
			[](std::shared_ptr<RssFeed> a,
				std::shared_ptr<RssFeed> b) {
				return a->total_item_count() <
					b->total_item_count();
			});
		break;
	case FeedSortMethod::UNREAD_ARTICLE_COUNT:
		std::stable_sort(feeds.begin(),
			feeds.end(),
			[](std::shared_ptr<RssFeed> a,
				std::shared_ptr<RssFeed> b) {
				return a->unread_item_count() <
					b->unread_item_count();
			});
		break;
	case FeedSortMethod::LAST_UPDATED:
		std::stable_sort(feeds.begin(),
			feeds.end(),
			[](std::shared_ptr<RssFeed> a,
				std::shared_ptr<RssFeed> b) {
				if (a->items().size() == 0 ||
					b->items().size() == 0) {
					return a->items().size() >
						b->items().size();
				}
				auto cmp =
					[](std::shared_ptr<RssItem> a,
						std::shared_ptr<RssItem> b) {
						return *a < *b;
					};
				auto& a_item =
					*std::min_element(a->items().begin(),
						a->items().end(),
						cmp);
				auto& b_item =
					*std::min_element(b->items().begin(),
						b->items().end(),
						cmp);
				return cmp(a_item, b_item);
			});
		break;
	}

	switch (sort_strategy.sd) {
	case SortDirection::ASC:
		std::reverse(feeds.begin(), feeds.end());
		break;
	case SortDirection::DESC:
		break;
	}
}

std::shared_ptr<RssFeed> FeedContainer::get_feed(const unsigned int pos)
{
	std::lock_guard<std::mutex> feedslock(feeds_mutex);
	if (pos >= feeds.size()) {
		throw std::out_of_range(_("invalid feed index (bug)"));
	}
	std::shared_ptr<RssFeed> feed = feeds[pos];
	return feed;
}

void FeedContainer::mark_all_feed_items_read(const unsigned int feed_pos)
{
	const auto feed = get_feed(feed_pos);
	std::lock_guard<std::mutex> lock(feed->item_mutex);
	std::vector<std::shared_ptr<RssItem>>& items = feed->items();
	if (items.size() > 0) {
		bool notify = items[0]->feedurl() != feed->rssurl();
		LOG(Level::DEBUG,
			"FeedContainer::mark_all_read: notify = %s",
			notify ? "yes" : "no");
		for (const auto& item : items) {
			item->set_unread_nowrite_notify(false, notify);
		}
	}
}

void FeedContainer::mark_all_feeds_read()
{
	std::lock_guard<std::mutex> feedslock(feeds_mutex);
	for (const auto& feed : feeds) {
		feed->mark_all_items_read();
	}
}

void FeedContainer::add_feed(const std::shared_ptr<RssFeed> feed)
{
	std::lock_guard<std::mutex> feedslock(feeds_mutex);
	feeds.push_back(feed);
}

void FeedContainer::populate_query_feeds()
{
	std::lock_guard<std::mutex> feedslock(feeds_mutex);
	for (const auto& feed : feeds) {
		if (feed->is_query_feed()) {
			feed->update_items(feeds);
		}
	}
}

unsigned int FeedContainer::get_feed_count_per_tag(const std::string& tag)
{
	unsigned int count = 0;
	std::lock_guard<std::mutex> feedslock(feeds_mutex);
	for (const auto& feed : feeds) {
		if (feed->matches_tag(tag)) {
			count++;
		}
	}

	return count;
}

std::shared_ptr<RssFeed> FeedContainer::get_feed_by_url(
	const std::string& feedurl)
{
	for (const auto& feed : feeds) {
		if (feedurl == feed->rssurl())
			return feed;
	}
	LOG(Level::ERROR,
		"FeedContainer:get_feed_by_url failed for %s",
		feedurl);
	return std::shared_ptr<RssFeed>();
}

unsigned int FeedContainer::get_pos_of_next_unread(unsigned int pos)
{
	std::lock_guard<std::mutex> feedslock(feeds_mutex);
	for (pos++; pos < feeds.size(); pos++) {
		if (feeds[pos]->unread_item_count() > 0)
			break;
	}
	return pos;
}

unsigned int FeedContainer::feeds_size()
{
	std::lock_guard<std::mutex> feedslock(feeds_mutex);
	return feeds.size();
}

void FeedContainer::reset_feeds_status()
{
	std::lock_guard<std::mutex> feedlock(feeds_mutex);
	for (const auto& feed : feeds) {
		feed->reset_status();
	}
}

void FeedContainer::set_feeds(
	const std::vector<std::shared_ptr<RssFeed>> new_feeds)
{
	std::lock_guard<std::mutex> feedslock(feeds_mutex);
	feeds = new_feeds;
}

std::vector<std::shared_ptr<RssFeed>> FeedContainer::get_all_feeds()
{
	std::vector<std::shared_ptr<RssFeed>> tmpfeeds;
	{
		std::lock_guard<std::mutex> feedslock(feeds_mutex);
		tmpfeeds = feeds;
	}
	return tmpfeeds;
}

void FeedContainer::clear_feeds_items()
{
	std::lock_guard<std::mutex> feedslock(feeds_mutex);
	for (const auto& feed : feeds) {
		std::lock_guard<std::mutex> lock(feed->item_mutex);
		feed->clear_items();
	}
}

unsigned int FeedContainer::unread_feed_count() const
{
	return std::count_if(feeds.begin(),
		feeds.end(),
		[](const std::shared_ptr<RssFeed> feed) {
			return feed->unread_item_count() > 0;
		});
}

unsigned int FeedContainer::unread_item_count() const
{
	return std::accumulate(feeds.begin(),
		feeds.end(),
		0,
		[](unsigned int sum, const std::shared_ptr<RssFeed> feed) {
			return sum += feed->unread_item_count();
		});
}

} // namespace newsboat