File: rss.h

package info (click to toggle)
newsbeuter 2.9-5+deb9u2
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 4,692 kB
  • ctags: 3,693
  • sloc: cpp: 18,663; ruby: 1,797; xml: 350; sh: 199; makefile: 171; perl: 101
file content (363 lines) | stat: -rw-r--r-- 8,078 bytes parent folder | download | duplicates (2)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#ifndef NEWSBEUTER_RSS__H
#define NEWSBEUTER_RSS__H

#include <string>
#include <vector>

#include <configcontainer.h>
#include <matcher.h>
#include <mutex>
#include <utils.h>

#include <memory>
#include <unordered_map>

namespace newsbeuter {

typedef std::pair<std::string, matcher *> feedurl_expr_pair;

enum dl_status { SUCCESS, TO_BE_DOWNLOADED, DURING_DOWNLOAD, DL_ERROR };

class cache;
class rss_feed;

class rss_item : public matchable {
	public:
		rss_item(cache * c);
		~rss_item();

		std::string title() const;
		std::string title_raw() const {
			return title_;
		}
		void set_title(const std::string& t);

		inline const std::string& link() const {
			return link_;
		}
		void set_link(const std::string& l);

		std::string author() const;
		std::string author_raw() const {
			return author_;
		}
		void set_author(const std::string& a);

		std::string description() const;
		std::string description_raw() const {
			return description_;
		}
		void set_description(const std::string& d);
		void set_size(unsigned int size);

		std::string length() const;
		std::string pubDate() const;

		inline time_t pubDate_timestamp() const {
			return pubDate_;
		}
		void set_pubDate(time_t t);

		bool operator<(const rss_item& item) const {
			return item.pubDate_ < this->pubDate_;    // new items come first
		}

		inline const std::string& guid() const {
			return guid_;
		}
		void set_guid(const std::string& g);

		inline bool unread() const {
			return unread_;
		}
		void set_unread(bool u);
		void set_unread_nowrite(bool u);
		void set_unread_nowrite_notify(bool u, bool notify);

		inline void set_cache(cache * c) {
			ch = c;
		}
		inline void set_feedurl(const std::string& f) {
			feedurl_ = f;
		}

		inline const std::string& feedurl() const {
			return feedurl_;
		}

		inline const std::string& enclosure_url() const {
			return enclosure_url_;
		}
		inline const std::string& enclosure_type() const {
			return enclosure_type_;
		}

		void set_enclosure_url(const std::string& url);
		void set_enclosure_type(const std::string& type);

		inline bool enqueued() {
			return enqueued_;
		}
		inline void set_enqueued(bool v) {
			enqueued_ = v;
		}

		inline const std::string& flags() const {
			return flags_;
		}
		inline const std::string& oldflags() const {
			return oldflags_;
		}
		void set_flags(const std::string& ff);
		void update_flags();
		void sort_flags();

		virtual bool has_attribute(const std::string& attribname);
		virtual std::string get_attribute(const std::string& attribname);

		void set_feedptr(std::shared_ptr<rss_feed> ptr);
		inline std::shared_ptr<rss_feed> get_feedptr() {
			return feedptr_.lock();
		}

		inline bool deleted() const {
			return deleted_;
		}
		inline void set_deleted(bool b) {
			deleted_ = b;
		}

		inline void set_index(unsigned int i) {
			idx = i;
		}
		inline unsigned int get_index() {
			return idx;
		}

		inline void set_base(const std::string& b) {
			base = b;
		}
		inline const std::string& get_base() {
			return base;
		}

		inline void set_override_unread(bool b) {
			override_unread_ = b;
		}
		inline bool override_unread() {
			return override_unread_;
		}

		inline void unload() {
			description_.clear();
		}

	private:
		std::string title_;
		std::string link_;
		std::string author_;
		std::string description_;
		time_t pubDate_;
		std::string guid_;
		std::string feedurl_;
		bool unread_;
		cache * ch;
		std::string enclosure_url_;
		std::string enclosure_type_;
		bool enqueued_;
		std::string flags_;
		std::string oldflags_;
		std::weak_ptr<rss_feed> feedptr_;
		bool deleted_;
		unsigned int idx;
		std::string base;
		bool override_unread_;
		unsigned int size_;
};

class rss_feed : public matchable {
	public:
		rss_feed(cache * c);
		rss_feed();
		~rss_feed();
		std::string title_raw() const {
			return title_;
		}
		std::string title() const;
		inline void set_title(const std::string& t) {
			title_ = t;
			utils::trim(title_);
		}

		std::string description_raw() const {
			return description_;
		}
		std::string description() const;
		inline void set_description(const std::string& d) {
			description_ = d;
		}

		inline const std::string& link() const {
			return link_;
		}
		inline void set_link(const std::string& l) {
			link_ = l;
		}

		inline std::string pubDate() const {
			return "TODO";
		}
		inline void set_pubDate(time_t t) {
			pubDate_ = t;
		}

		bool hidden() const;

		inline std::vector<std::shared_ptr<rss_item>>& items() {
			return items_;
		}
		inline void add_item(std::shared_ptr<rss_item> item) {
			items_.push_back(item);
			items_guid_map[item->guid()] = item;
		}
		inline void set_items(std::vector<std::shared_ptr<rss_item>>& items) {
			erase_items(items_.begin(), items_.end());
			for (auto item : items) {
				add_item(item);
			}
		}

		inline void clear_items() {
			LOG(LOG_DEBUG, "rss_feed: clearing items");
			items_.clear();
			items_guid_map.clear();
		}

		inline void erase_items(std::vector<std::shared_ptr<rss_item>>::iterator begin, std::vector<std::shared_ptr<rss_item>>::iterator end) {
			for (auto it=begin; it!=end; ++it) {
				items_guid_map.erase((*it)->guid());
			}
			items_.erase(begin, end);
		}
		inline void erase_item(std::vector<std::shared_ptr<rss_item>>::iterator pos) {
			items_guid_map.erase((*pos)->guid());
			items_.erase(pos);
		}

		std::shared_ptr<rss_item> get_item_by_guid(const std::string& guid);
		std::shared_ptr<rss_item> get_item_by_guid_unlocked(const std::string& guid);

		inline const std::string& rssurl() const {
			return rssurl_;
		}
		void set_rssurl(const std::string& u);

		unsigned int unread_item_count();
		inline unsigned int total_item_count() const {
			return items_.size();
		}

		void set_tags(const std::vector<std::string>& tags);
		bool matches_tag(const std::string& tag);
		std::string get_tags();
		std::string get_firsttag();

		virtual bool has_attribute(const std::string& attribname);
		virtual std::string get_attribute(const std::string& attribname);

		void update_items(std::vector<std::shared_ptr<rss_feed>> feeds);

		inline void set_query(const std::string& s) {
			query = s;
		}

		bool is_empty() {
			return empty;
		}
		void set_empty(bool t) {
			empty = t;
		}

		void sort(const std::string& method);
		void sort_unlocked(const std::string& method);

		void remove_old_deleted_items();

		void purge_deleted_items();

		inline void set_rtl(bool b) {
			is_rtl_ = b;
		}
		inline bool is_rtl() {
			return is_rtl_;
		}

		inline void set_index(unsigned int i) {
			idx = i;
		}
		inline unsigned int get_index() {
			return idx;
		}

		inline void set_order(unsigned int x) {
			order = x;
		}
		inline unsigned int get_order() {
			return order;
		}

		void set_feedptrs(std::shared_ptr<rss_feed> self);

		std::string get_status();

		inline void reset_status() {
			status_ = TO_BE_DOWNLOADED;
		}
		inline void set_status(dl_status st) {
			status_ = st;
		}

		void unload();
		void load();

		std::mutex item_mutex; // this is ugly, but makes it possible to lock items use e.g. from the cache class
	private:
		std::string title_;
		std::string description_;
		std::string link_;
		time_t pubDate_;
		std::string rssurl_;
		std::vector<std::shared_ptr<rss_item>> items_;
		std::unordered_map<std::string, std::shared_ptr<rss_item>> items_guid_map;
		std::vector<std::string> tags_;
		std::string query;

		cache * ch;

		bool empty;
		bool is_rtl_;
		unsigned int idx;
		unsigned int order;
		dl_status status_;
		std::mutex items_guid_map_mutex;
};

class rss_ignores : public config_action_handler {
	public:
		rss_ignores() { }
		virtual ~rss_ignores();
		virtual void handle_action(const std::string& action, const std::vector<std::string>& params);
		virtual void dump_config(std::vector<std::string>& config_output);
		bool matches(rss_item * item);
		bool matches_lastmodified(const std::string& url);
		bool matches_resetunread(const std::string& url);
	private:
		std::vector<feedurl_expr_pair> ignores;
		std::vector<std::string> ignores_lastmodified;
		std::vector<std::string> resetflag;
};

}


#endif