akregator/src

articlefilter.h
1 /*
2  * articlefilter.h
3  *
4  * Copyright (c) 2004, 2005 Frerich Raabe <raabe@kde.org>
5  * 2005 Frank Osterfeld <frank.osterfeld@kdemail.net>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 #ifndef ARTICLEFILTER_H
29 #define ARTICLEFILTER_H
30 
31 #include <tqstring.h>
32 #include <tqvaluelist.h>
33 #include <tqvariant.h>
34 
35 class KConfig;
36 
37 namespace Akregator {
38 
39 class Article;
40 
41 namespace Filters {
42 
43 class AbstractAction;
44 class AbstractMatcher;
45 class Criterion;
46 
51 {
52  public:
53 
54  ArticleFilter();
55  ArticleFilter(const AbstractMatcher& matcher, const AbstractAction& action);
56  ArticleFilter(const ArticleFilter& other);
57 
58  virtual ~ArticleFilter();
59 
61  void applyTo(Article& article) const;
62 
63 
64 
66  const TQString& name() const;
67  void setName(const TQString& name);
68 
69  int id() const;
70 
71  AbstractMatcher* matcher() const;
72  void setMatcher(const AbstractMatcher& matcher);
73 
74  AbstractAction* action() const;
75  void setAction(const AbstractAction& action);
76 
77  ArticleFilter& operator=(const ArticleFilter& other);
78  bool operator==(const ArticleFilter& other) const;
79 
80  void writeConfig(KConfig* config) const;
81  void readConfig(KConfig* config);
82 
83  private:
84  class ArticleFilterPrivate;
85  ArticleFilterPrivate* d;
86 
87 };
88 
89 class ArticleFilterList : public TQValueList<ArticleFilter>
90 {
91 public:
92 
93  void writeConfig(KConfig* config) const;
94  void readConfig(KConfig* config);
95 };
96 
101 {
102  public:
103 
104  virtual ~AbstractMatcher() {}
106  virtual AbstractMatcher* clone() const = 0;
107 
108  virtual bool matches(const Article& article) const = 0;
109 
110  virtual void writeConfig(KConfig* config) const = 0;
111  virtual void readConfig(KConfig* config) = 0;
112 
113  virtual bool operator==(const AbstractMatcher&) const = 0;
114  virtual bool operator!=(const AbstractMatcher &other) const = 0;
115 };
116 
117 class TagMatcher : public AbstractMatcher
118 {
119  public:
120 
121  TagMatcher();
122  TagMatcher(const TQString& tagID);
123  TagMatcher(const TagMatcher& other);
124 
125  virtual ~TagMatcher();
126 
127 
128  virtual bool matches(const Article& article) const;
129 
130  virtual TagMatcher* clone() const;
131 
132  virtual void writeConfig(KConfig* config) const;
133  virtual void readConfig(KConfig* config);
134 
135  TagMatcher& operator=(const TagMatcher& other);
136  virtual bool operator==(const AbstractMatcher&) const;
137  virtual bool operator!=(const AbstractMatcher &other) const;
138 
139  private:
140 
141  class TagMatcherPrivate;
142  TagMatcherPrivate* d;
143 };
144 
145 class AbstractAction
146 {
147  public:
148  virtual void exec(Article& article) = 0;
149 
150  virtual void writeConfig(KConfig* config) const = 0;
151  virtual void readConfig(KConfig* config) = 0;
152 
153  virtual AbstractAction* clone() const = 0;
154  virtual bool operator==(const AbstractAction& other) = 0;
155 };
156 
157 class DeleteAction : public AbstractAction
158 {
159  public:
160  virtual void exec(Article& article);
161 
162  virtual void writeConfig(KConfig* config) const;
163  virtual void readConfig(KConfig* config);
164 
165  virtual DeleteAction* clone() const { return new DeleteAction; }
166  virtual bool operator==(const AbstractAction& other);
167 };
168 
169 class SetStatusAction : public AbstractAction
170 {
171  public:
172  SetStatusAction(int status=0);
173 
174  virtual void exec(Article& article);
175 
176  int status() const;
177  void setStatus(int status);
178 
179  virtual void writeConfig(KConfig* config) const;
180  virtual void readConfig(KConfig* config);
181 
182  virtual SetStatusAction* clone() const { return new SetStatusAction(*this); }
183  virtual bool operator==(const AbstractAction& other);
184 
185  private:
186  int m_status;
187 };
188 
189 class AssignTagAction : public AbstractAction
190 {
191  public:
192 
193  AssignTagAction(const TQString& tagID=TQString());
194  virtual void exec(Article& article);
195 
196  const TQString& tagID() const;
197  void setTagID(const TQString& tagID);
198 
199  virtual void writeConfig(KConfig* config) const;
200  virtual void readConfig(KConfig* config);
201 
202  virtual AssignTagAction* clone() const { return new AssignTagAction(*this); }
203  virtual bool operator==(const AbstractAction& other);
204 
205  private:
206  TQString m_tagID;
207 };
208 
213 {
214  public:
215 
216  enum Association {
217  None, LogicalAnd, LogicalOr
218  };
219 
220  ArticleMatcher();
221  ArticleMatcher( const TQValueList<Criterion> &criteria, Association assoc);
222 
223  ArticleMatcher(const ArticleMatcher& other);
224  virtual ~ArticleMatcher();
225 
230  virtual bool matchesAll() const;
231 
232  ArticleMatcher& operator=(const ArticleMatcher& other);
233  virtual ArticleMatcher* clone() const;
234  virtual bool matches(const Article &article) const;
235  virtual bool operator==(const AbstractMatcher &other) const;
236  virtual bool operator!=(const AbstractMatcher &other) const;
237 
238 
239  virtual void writeConfig(KConfig* config) const;
240  virtual void readConfig(KConfig* config);
241 
242  private:
243 
244  static Association stringToAssociation(const TQString& assocStr);
245  static TQString associationToString(Association association);
246 
247  bool anyCriterionMatches( const Article &a ) const;
248  bool allCriteriaMatch( const Article &a ) const;
249 
250  TQValueList<Criterion> m_criteria;
251  Association m_association;
252 };
253 
258 {
259  public:
260 
261  enum Subject {
262  Title, Description, Author, Link, Status, KeepFlag
263  };
264 
265  static TQString subjectToString(Subject subj);
266  static Subject stringToSubject(const TQString& subjStr);
267 
268  enum Predicate {
269  Contains = 0x01,
270  Equals = 0x02,
271  Matches = 0x03,
272  Negation = 0x80
273  };
274 
275  static TQString predicateToString(Predicate pred);
276  static Predicate stringToPredicate(const TQString& predStr);
277 
278  Criterion();
279  Criterion( Subject subject, Predicate predicate, const TQVariant &object );
280 
281  bool satisfiedBy( const Article &article ) const;
282 
283  virtual void writeConfig(KConfig* config) const;
284  virtual void readConfig(KConfig* config);
285 
286  Subject subject() const;
287  Predicate predicate() const;
288  TQVariant object() const;
289  bool operator==(const Criterion& other) const
290  { return m_subject == other.m_subject && m_predicate == other.m_predicate && m_object == other.m_object; }
291 
292  private:
293  Subject m_subject;
294  Predicate m_predicate;
295  TQVariant m_object;
296 };
297 
298 } // namespace Filters
299 } // namespace Akregator
300 
301 #endif