1 /*
2  *  This file is part of nzbget. See <http://nzbget.net>.
3  *
4  *  Copyright (C) 2013-2016 Andrey Prygunkov <hugbug@users.sourceforge.net>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 
21 #ifndef FEEDINFO_H
22 #define FEEDINFO_H
23 
24 #include "NString.h"
25 #include "Util.h"
26 #include "DownloadInfo.h"
27 
28 class FeedInfo
29 {
30 public:
31 	enum EStatus
32 	{
33 		fsUndefined,
34 		fsRunning,
35 		fsFinished,
36 		fsFailed
37 	};
38 
39 	FeedInfo(int id, const char* name, const char* url, bool backlog, int interval,
40 		const char* filter, bool pauseNzb, const char* category, int priority,
41 		const char* extensions);
GetId()42 	int GetId() { return m_id; }
GetName()43 	const char* GetName() { return m_name; }
GetUrl()44 	const char* GetUrl() { return m_url; }
GetInterval()45 	int GetInterval() { return m_interval; }
GetFilter()46 	const char* GetFilter() { return m_filter; }
GetFilterHash()47 	uint32 GetFilterHash() { return m_filterHash; }
GetPauseNzb()48 	bool GetPauseNzb() { return m_pauseNzb; }
GetCategory()49 	const char* GetCategory() { return m_category; }
GetPriority()50 	int GetPriority() { return m_priority; }
GetExtensions()51 	const char* GetExtensions() { return m_extensions; }
GetLastUpdate()52 	time_t GetLastUpdate() { return m_lastUpdate; }
SetLastUpdate(time_t lastUpdate)53 	void SetLastUpdate(time_t lastUpdate) { m_lastUpdate = lastUpdate; }
GetNextUpdate()54 	time_t GetNextUpdate() { return m_nextUpdate; }
SetNextUpdate(time_t nextUpdate)55 	void SetNextUpdate(time_t nextUpdate) { m_nextUpdate = nextUpdate; }
GetLastInterval()56 	int GetLastInterval() { return m_lastInterval; }
SetLastInterval(int lastInterval)57 	void SetLastInterval(int lastInterval) { m_lastInterval = lastInterval; }
GetPreview()58 	bool GetPreview() { return m_preview; }
SetPreview(bool preview)59 	void SetPreview(bool preview) { m_preview = preview; }
GetStatus()60 	EStatus GetStatus() { return m_status; }
SetStatus(EStatus Status)61 	void SetStatus(EStatus Status) { m_status = Status; }
GetOutputFilename()62 	const char* GetOutputFilename() { return m_outputFilename; }
SetOutputFilename(const char * outputFilename)63 	void SetOutputFilename(const char* outputFilename) { m_outputFilename = outputFilename; }
GetFetch()64 	bool GetFetch() { return m_fetch; }
SetFetch(bool fetch)65 	void SetFetch(bool fetch) { m_fetch = fetch; }
GetForce()66 	bool GetForce() { return m_force; }
SetForce(bool force)67 	void SetForce(bool force) { m_force = force; }
GetBacklog()68 	bool GetBacklog() { return m_backlog; }
SetBacklog(bool backlog)69 	void SetBacklog(bool backlog) { m_backlog = backlog; }
70 
71 private:
72 	int m_id;
73 	CString m_name;
74 	CString m_url;
75 	bool m_backlog;
76 	int m_interval;
77 	CString m_filter;
78 	uint32 m_filterHash;
79 	bool m_pauseNzb;
80 	CString m_category;
81 	CString m_extensions;
82 	int m_priority;
83 	time_t m_lastUpdate = 0;
84 	time_t m_nextUpdate = 0;
85 	int m_lastInterval = 0;
86 	bool m_preview = false;
87 	EStatus m_status = fsUndefined;
88 	CString m_outputFilename;
89 	bool m_fetch = false;
90 	bool m_force = false;
91 };
92 
93 typedef std::deque<std::unique_ptr<FeedInfo>> Feeds;
94 
95 class FeedFilterHelper
96 {
97 public:
98 	virtual std::unique_ptr<RegEx>& GetRegEx(int id) = 0;
99 	virtual void CalcDupeStatus(const char* title, const char* dupeKey, char* statusBuf, int bufLen) = 0;
100 };
101 
102 class FeedItemInfo
103 {
104 public:
105 	enum EStatus
106 	{
107 		isUnknown,
108 		isBacklog,
109 		isFetched,
110 		isNew
111 	};
112 
113 	enum EMatchStatus
114 	{
115 		msIgnored,
116 		msAccepted,
117 		msRejected
118 	};
119 
120 	class Attr
121 	{
122 	public:
Attr(const char * name,const char * value)123 		Attr(const char* name, const char* value) :
124 			m_name(name ? name : ""), m_value(value ? value : "") {}
GetName()125 		const char* GetName() { return m_name; }
GetValue()126 		const char* GetValue() { return m_value; }
127 	private:
128 		CString m_name;
129 		CString m_value;
130 	};
131 
132 	typedef std::deque<Attr> AttributesBase;
133 
134 	class Attributes: public AttributesBase
135 	{
136 	public:
137 		Attr* Find(const char* name);
138 	};
139 
FeedItemInfo()140 	FeedItemInfo() {}
141 	FeedItemInfo(FeedItemInfo&&) = delete; // catch performance issues
SetFeedFilterHelper(FeedFilterHelper * feedFilterHelper)142 	void SetFeedFilterHelper(FeedFilterHelper* feedFilterHelper) { m_feedFilterHelper = feedFilterHelper; }
GetTitle()143 	const char* GetTitle() { return m_title; }
SetTitle(const char * title)144 	void SetTitle(const char* title) { m_title = title; }
GetFilename()145 	const char* GetFilename() { return m_filename; }
SetFilename(const char * filename)146 	void SetFilename(const char* filename) { m_filename = filename; }
GetUrl()147 	const char* GetUrl() { return m_url; }
SetUrl(const char * url)148 	void SetUrl(const char* url) { m_url = url; }
GetSize()149 	int64 GetSize() { return m_size; }
SetSize(int64 size)150 	void SetSize(int64 size) { m_size = size; }
GetCategory()151 	const char* GetCategory() { return m_category; }
SetCategory(const char * category)152 	void SetCategory(const char* category) { m_category = category; }
GetImdbId()153 	int GetImdbId() { return m_imdbId; }
SetImdbId(int imdbId)154 	void SetImdbId(int imdbId) { m_imdbId = imdbId; }
GetRageId()155 	int GetRageId() { return m_rageId; }
SetRageId(int rageId)156 	void SetRageId(int rageId) { m_rageId = rageId; }
GetTvdbId()157 	int GetTvdbId() { return m_tvdbId; }
SetTvdbId(int tvdbId)158 	void SetTvdbId(int tvdbId) { m_tvdbId = tvdbId; }
GetTvmazeId()159 	int GetTvmazeId() { return m_tvmazeId; }
SetTvmazeId(int tvmazeId)160 	void SetTvmazeId(int tvmazeId) { m_tvmazeId = tvmazeId; }
GetDescription()161 	const char* GetDescription() { return m_description; }
SetDescription(const char * description)162 	void SetDescription(const char* description) { m_description = description ? description: ""; }
GetSeason()163 	const char* GetSeason() { return m_season; }
164 	void SetSeason(const char* season);
GetEpisode()165 	const char* GetEpisode() { return m_episode; }
166 	void SetEpisode(const char* episode);
167 	int GetSeasonNum();
168 	int GetEpisodeNum();
GetAddCategory()169 	const char* GetAddCategory() { return m_addCategory; }
SetAddCategory(const char * addCategory)170 	void SetAddCategory(const char* addCategory) { m_addCategory = addCategory ? addCategory : ""; }
GetPauseNzb()171 	bool GetPauseNzb() { return m_pauseNzb; }
SetPauseNzb(bool pauseNzb)172 	void SetPauseNzb(bool pauseNzb) { m_pauseNzb = pauseNzb; }
GetPriority()173 	int GetPriority() { return m_priority; }
SetPriority(int priority)174 	void SetPriority(int priority) { m_priority = priority; }
GetTime()175 	time_t GetTime() { return m_time; }
SetTime(time_t time)176 	void SetTime(time_t time) { m_time = time; }
GetStatus()177 	EStatus GetStatus() { return m_status; }
SetStatus(EStatus status)178 	void SetStatus(EStatus status) { m_status = status; }
GetMatchStatus()179 	EMatchStatus GetMatchStatus() { return m_matchStatus; }
SetMatchStatus(EMatchStatus matchStatus)180 	void SetMatchStatus(EMatchStatus matchStatus) { m_matchStatus = matchStatus; }
GetMatchRule()181 	int GetMatchRule() { return m_matchRule; }
SetMatchRule(int matchRule)182 	void SetMatchRule(int matchRule) { m_matchRule = matchRule; }
GetDupeKey()183 	const char* GetDupeKey() { return m_dupeKey; }
SetDupeKey(const char * dupeKey)184 	void SetDupeKey(const char* dupeKey) { m_dupeKey = dupeKey ? dupeKey : ""; }
185 	void AppendDupeKey(const char* extraDupeKey);
186 	void BuildDupeKey(const char* rageId, const char* tvdbId, const char* tvmazeId, const char* series);
GetDupeScore()187 	int GetDupeScore() { return m_dupeScore; }
SetDupeScore(int dupeScore)188 	void SetDupeScore(int dupeScore) { m_dupeScore = dupeScore; }
GetDupeMode()189 	EDupeMode GetDupeMode() { return m_dupeMode; }
SetDupeMode(EDupeMode dupeMode)190 	void SetDupeMode(EDupeMode dupeMode) { m_dupeMode = dupeMode; }
191 	const char* GetDupeStatus();
GetAttributes()192 	Attributes* GetAttributes() { return &m_attributes; }
193 
194 private:
195 	CString m_title;
196 	CString m_filename;
197 	CString m_url;
198 	time_t m_time = 0;
199 	int64 m_size = 0;
200 	CString m_category = "";
201 	int m_imdbId = 0;
202 	int m_rageId = 0;
203 	int m_tvdbId = 0;
204 	int m_tvmazeId = 0;
205 	CString m_description = "";
206 	CString m_season;
207 	CString m_episode;
208 	int m_seasonNum = 0;
209 	int m_episodeNum = 0;
210 	bool m_seasonEpisodeParsed = false;
211 	CString m_addCategory = "";
212 	bool m_pauseNzb = false;
213 	int m_priority = 0;
214 	EStatus m_status = isUnknown;
215 	EMatchStatus m_matchStatus = msIgnored;
216 	int m_matchRule = 0;
217 	CString m_dupeKey;
218 	int m_dupeScore = 0;
219 	EDupeMode m_dupeMode = dmScore;
220 	CString m_dupeStatus;
221 	FeedFilterHelper* m_feedFilterHelper = nullptr;
222 	Attributes m_attributes;
223 
224 	int ParsePrefixedInt(const char *value);
225 	void ParseSeasonEpisode();
226 };
227 
228 typedef std::deque<FeedItemInfo> FeedItemList;
229 
230 class FeedHistoryInfo
231 {
232 public:
233 	enum EStatus
234 	{
235 		hsUnknown,
236 		hsBacklog,
237 		hsFetched
238 	};
239 
FeedHistoryInfo(const char * url,EStatus status,time_t lastSeen)240 	FeedHistoryInfo(const char* url, EStatus status, time_t lastSeen) :
241 		m_url(url), m_status(status), m_lastSeen(lastSeen) {}
GetUrl()242 	const char* GetUrl() { return m_url; }
GetStatus()243 	EStatus GetStatus() { return m_status; }
SetStatus(EStatus Status)244 	void SetStatus(EStatus Status) { m_status = Status; }
GetLastSeen()245 	time_t GetLastSeen() { return m_lastSeen; }
SetLastSeen(time_t lastSeen)246 	void SetLastSeen(time_t lastSeen) { m_lastSeen = lastSeen; }
247 
248 private:
249 	CString m_url;
250 	EStatus m_status;
251 	time_t m_lastSeen;
252 };
253 
254 typedef std::deque<FeedHistoryInfo> FeedHistoryBase;
255 
256 class FeedHistory : public FeedHistoryBase
257 {
258 public:
259 	void Remove(const char* url);
260 	FeedHistoryInfo* Find(const char* url);
261 };
262 
263 #endif
264