libtdepim

kscoring.h
1 /*
2  kscoring.h
3 
4  Copyright (c) 2001 Mathias Waack
5  Copyright (C) 2005 by Volker Krause <volker.krause@rwth-aachen.de>
6 
7  Author: Mathias Waack <mathias@atoll-net.de>
8 
9  This program is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software Foundation,
15  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
16 */
17 
18 #ifndef KSCORING_H
19 #define KSCORING_H
20 
21 #include <unistd.h>
22 
23 #include <tqglobal.h>
24 #include <tqptrlist.h>
25 #include <tqptrstack.h>
26 #include <tqregexp.h>
27 
28 #include <tqobject.h>
29 #include <tqstring.h>
30 #include <tqstringlist.h>
31 #include <tqdatetime.h>
32 #include <tqcolor.h>
33 #include <tqtable.h>
34 #include <tqmap.h>
35 #include <tqdict.h>
36 
37 #include <kdialogbase.h>
38 #include <klineedit.h>
39 #include <knuminput.h>
40 
41 #include <tdemacros.h>
42 
43 class TQDomNode;
44 class TQDomDocument;
45 class TQDomElement;
46 class TQTextStream;
47 class TQLabel;
48 
49 
57 //----------------------------------------------------------------------------
58 class TDE_EXPORT ScorableGroup
59 {
60 public:
61  virtual ~ScorableGroup();
62 };
63 
64 class TDE_EXPORT ScorableArticle
65 {
66 public:
67  virtual ~ScorableArticle();
68 
69  virtual void addScore(short) {}
70  virtual void displayMessage(const TQString&);
71  virtual void changeColor(const TQColor&) {}
72  virtual void markAsRead() {}
73  virtual TQString from() const = 0;
74  virtual TQString subject() const = 0;
75  virtual TQString getHeaderByType(const TQString&) const = 0;
76  //virtual ScorableGroup group() const =0;
77 };
78 
79 
80 //----------------------------------------------------------------------------
84 class TDE_EXPORT ActionBase {
85  public:
86  ActionBase();
87  virtual ~ActionBase();
88  virtual TQString toString() const =0;
89  virtual void apply(ScorableArticle&) const =0;
90  virtual ActionBase* clone() const =0;
91  virtual int getType() const =0;
92  virtual TQString getValueString() const { return TQString(); }
93  virtual void setValue(const TQString&) {}
94  static ActionBase* factory(int type, const TQString &value);
95  static TQStringList userNames();
96  static TQString userName(int type);
97  static int getTypeForName(const TQString& name);
98  static int getTypeForUserName(const TQString& name);
99  TQString userName() { return userName(getType()); }
100  enum ActionTypes { SETSCORE, NOTIFY, COLOR, MARKASREAD };
101 };
102 
103 class TDE_EXPORT ActionColor : public ActionBase {
104 public:
105  ActionColor(const TQColor&);
106  ActionColor(const TQString&);
107  ActionColor(const ActionColor&);
108  virtual ~ActionColor();
109  virtual TQString toString() const;
110  virtual int getType() const { return COLOR; }
111  virtual TQString getValueString() const { return color.name(); }
112  virtual void setValue(const TQString& s) { color.setNamedColor(s); }
113  void setValue(const TQColor& c) { color = c; }
114  TQColor value() const { return color; }
115  virtual void apply(ScorableArticle&) const;
116  virtual ActionColor* clone() const;
117 private:
118  TQColor color;
119 };
120 
121 class TDE_EXPORT ActionSetScore : public ActionBase {
122  public:
123  ActionSetScore(short);
124  ActionSetScore(const ActionSetScore&);
125  ActionSetScore(const TQString&);
126  virtual ~ActionSetScore();
127  virtual TQString toString() const;
128  virtual int getType() const { return SETSCORE; }
129  virtual TQString getValueString() const { return TQString::number(val); }
130  virtual void setValue(const TQString& s) { val = s.toShort(); }
131  void setValue(short v) { val = v; }
132  short value() const { return val; }
133  virtual void apply(ScorableArticle&) const;
134  virtual ActionSetScore* clone() const;
135  private:
136  short val;
137 };
138 
139 class TDE_EXPORT ActionNotify : public ActionBase {
140  public:
141  ActionNotify(const TQString&);
142  ActionNotify(const ActionNotify&);
143  virtual ~ActionNotify() {}
144  virtual TQString toString() const;
145  virtual int getType() const { return NOTIFY; }
146  virtual TQString getValueString() const { return note; }
147  virtual void setValue(const TQString& s) { note = s; }
148  virtual void apply(ScorableArticle&) const;
149  virtual ActionNotify* clone() const;
150  private:
151  TQString note;
152 };
153 
154 class TDE_EXPORT ActionMarkAsRead : public ActionBase {
155  public:
156  ActionMarkAsRead();
157  ActionMarkAsRead( const ActionMarkAsRead& );
158  virtual ~ActionMarkAsRead() {}
159  virtual TQString toString() const;
160  virtual int getType() const { return MARKASREAD; }
161  virtual void apply( ScorableArticle &article ) const;
162  virtual ActionMarkAsRead* clone() const;
163 };
164 
165 class TDE_EXPORT NotifyCollection
166 {
167 public:
168  NotifyCollection();
169  ~NotifyCollection();
170  void addNote(const ScorableArticle&, const TQString&);
171  TQString collection() const;
172  void displayCollection(TQWidget *p=0) const;
173 private:
174  struct article_info {
175  TQString from;
176  TQString subject;
177  };
178  typedef TQValueList<article_info> article_list;
179  typedef TQDict<article_list> note_list;
180  note_list notifyList;
181 };
182 
183 
184 //----------------------------------------------------------------------------
185 class TDE_EXPORT KScoringExpression
186 {
187  friend class KScoringRule;
188  public:
189  enum Condition { CONTAINS, MATCH, EQUALS, SMALLER, GREATER, MATCHCS };
190 
191  KScoringExpression(const TQString&,const TQString&,const TQString&, const TQString&);
192  ~KScoringExpression();
193 
194  bool match(ScorableArticle& a) const ;
195  TQString getTypeString() const;
196  static TQString getTypeString(int);
197  int getType() const;
198  TQString toString() const;
199  void write(TQTextStream& ) const;
200 
201  bool isNeg() const { return neg; }
202  Condition getCondition() const { return cond; }
203  TQString getExpression() const { return expr_str; }
204  TQString getHeader() const { return header; }
205  static TQStringList conditionNames();
206  static TQStringList headerNames();
207  static int getConditionForName(const TQString&);
208  static TQString getNameForCondition(int);
209  private:
210  bool neg;
211  TQString header;
212  const char* c_header;
213  Condition cond;
214  TQRegExp expr;
215  TQString expr_str;
216  int expr_int;
217 };
218 
219 //----------------------------------------------------------------------------
220 class TDE_EXPORT KScoringRule
221 {
222  friend class KScoringManager;
223  public:
224  KScoringRule(const TQString& name);
225  KScoringRule(const KScoringRule& r);
226  ~KScoringRule();
227 
228  typedef TQPtrList<KScoringExpression> ScoreExprList;
229  typedef TQPtrList<ActionBase> ActionList;
230  typedef TQStringList GroupList;
231  enum LinkMode { AND, OR };
232 
233  TQString getName() const { return name; }
234  TQStringList getGroups() const { return groups; }
235  void setGroups(const TQStringList &l) { groups = l; }
236  LinkMode getLinkMode() const { return link; }
237  TQString getLinkModeName() const;
238  TQString getExpireDateString() const;
239  TQDate getExpireDate() const { return expires; }
240  void setExpireDate(const TQDate &d) { expires = d; }
241  bool isExpired() const;
242  ScoreExprList getExpressions() const { return expressions; }
243  ActionList getActions() const { return actions; }
244  void cleanExpressions();
245  void cleanActions();
246 
247  bool matchGroup(const TQString& group) const ;
248  void applyRule(ScorableArticle& a) const;
249  void applyRule(ScorableArticle& a, const TQString& group) const;
250  void applyAction(ScorableArticle& a) const;
251 
252  void setLinkMode(const TQString& link);
253  void setLinkMode(LinkMode m) { link = m; }
254  void setExpire(const TQString& exp);
255  void addExpression( KScoringExpression* );
256  void addGroup( const TQString& group) { groups.append(group); }
257  //void addServer(const TQString& server) { servers.append(server); }
258  void addAction(int, const TQString& );
259  void addAction(ActionBase*);
260 
261  void updateXML(TQDomElement& e, TQDomDocument& d);
262  TQString toString() const;
263 
264  // writes the rule in XML format into the textstream
265  void write(TQTextStream& ) const;
266 protected:
268  void setName(const TQString &n) { name = n; }
269 private:
270  TQString name;
271  GroupList groups;
272  //ServerList servers;
273  LinkMode link;
274  ScoreExprList expressions;
275  ActionList actions;
276  TQDate expires;
277 };
278 
283 class TDE_EXPORT RuleStack
284 {
285 public:
286  RuleStack();
287  ~RuleStack();
289  void push(TQPtrList<KScoringRule>&);
292  void pop(TQPtrList<KScoringRule>&);
294  void top(TQPtrList<KScoringRule>&);
296  void drop();
297 private:
298  TQPtrStack< TQPtrList<KScoringRule> > stack;
299 };
300 
301 //----------------------------------------------------------------------------
302 // Manages the score rules.
303 class TDE_EXPORT KScoringManager : public TQObject
304 {
305  TQ_OBJECT
306 
307 
308  public:
309  //* this is the container for all rules
310  typedef TQPtrList<KScoringRule> ScoringRuleList;
311 
312  KScoringManager(const TQString& appName = TQString());
313  virtual ~KScoringManager();
314 
315  //* returns a list of all available groups, must be overridden
316  virtual TQStringList getGroups() const =0;
317 
320  virtual TQStringList getDefaultHeaders() const;
321 
322  //* setting current server and group and calling applyRules(ScorableArticle&)
323  void applyRules(ScorableArticle& article, const TQString& group/*, const TQString& server*/);
324  //* assuming a properly set group
325  void applyRules(ScorableArticle&);
326  //* same as above
327  void applyRules(ScorableGroup* group);
328 
329  //* pushes the current rule list onto a stack
330  void pushRuleList();
331  //* restores the current rule list from list stored on a stack
332  //* by a previous call to pushRuleList (this implicitly deletes the
333  //* current rule list)
334  void popRuleList();
335  //* removes the TOS from the stack of rule lists
336  void removeTOS();
337 
338  KScoringRule* addRule(KScoringRule *);
339  KScoringRule* addRule(const ScorableArticle&, TQString group, short =0);
340  KScoringRule* addRule();
341  void cancelNewRule(KScoringRule *);
342  void deleteRule(KScoringRule *);
343  void editRule(KScoringRule *e, TQWidget *w=0);
344  KScoringRule* copyRule(KScoringRule *);
345  void moveRuleAbove( KScoringRule *above, KScoringRule *below );
346  void moveRuleBelow( KScoringRule *below, KScoringRule *above );
347  void setGroup(const TQString& g);
348  // has to be called after setGroup() or initCache()
349  bool hasRulesForCurrentGroup();
350  TQString findUniqueName() const;
351 
354  void editorReady();
355 
356  ScoringRuleList getAllRules() const { return allRules; }
357  KScoringRule *findRule(const TQString&);
358  TQStringList getRuleNames();
359  void setRuleName(KScoringRule *, const TQString&);
360  int getRuleCount() const { return allRules.count(); }
361  TQString toString() const;
362 
363  bool setCacheValid(bool v);
364  bool isCacheValid() { return cacheValid; }
365  void initCache(const TQString& group/*, const TQString& server*/);
366 
367  void load();
368  void save();
369 
370  //--------------- Properties
371  virtual bool canScores() const { return true; }
372  virtual bool canNotes() const { return true; }
373  virtual bool canColors() const { return false; }
374  virtual bool canMarkAsRead() const { return false; }
375  virtual bool hasFeature(int);
376 
377  signals:
378  void changedRules();
379  void changedRuleName(const TQString& oldName, const TQString& newName);
380  void finishedEditing();
381 
382  private:
383  void addRuleInternal(KScoringRule *e);
384  void expireRules();
385 
386  TQDomDocument createXMLfromInternal();
387  void createInternalFromXML(TQDomNode);
388 
389  // list of all Rules
390  ScoringRuleList allRules;
391 
392  // the stack for temporary storing rule lists
393  RuleStack stack;
394 
395  // for the cache
396  bool cacheValid;
397  // current rule set, ie the cache
398  ScoringRuleList ruleList;
399  //TQString server;
400  TQString group;
401 
402  //ScorableServer* _s;
403 
404  // filename of the scorefile
405  TQString mFilename;
406 };
407 
408 
409 //----------------------------------------------------------------------------
410 class TDE_EXPORT NotifyDialog : public KDialogBase
411 {
412  TQ_OBJECT
413 
414 public:
415  static void display(ScorableArticle&,const TQString&);
416 protected slots:
417  void slotShowAgainToggled(bool);
418 private:
419  NotifyDialog(TQWidget* p =0);
420  static NotifyDialog *me;
421 
422  TQLabel *note;
423  TQString msg;
424  typedef TQMap<TQString,bool> NotesMap;
425  static NotesMap dict;
426 };
427 
428 
429 #endif
Base class for other Action classes.
Definition: kscoring.h:84
this helper class implements a stack for lists of lists of rules.
Definition: kscoring.h:284
The following classes ScorableArticle, ScorableGroup define the interface for the scoring.
Definition: kscoring.h:59