summaryrefslogtreecommitdiffstats
path: root/libkdepim/kscoring.h
blob: 3252a33eac5f819dace5e1c30f9dfe70c80b8b2a (plain)
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
    kscoring.h

    Copyright (c) 2001 Mathias Waack
    Copyright (C) 2005 by Volker Krause <volker.krause@rwth-aachen.de>

    Author: Mathias Waack <mathias@atoll-net.de>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
*/

#ifndef KSCORING_H
#define KSCORING_H

#include <unistd.h>

#include <qglobal.h>
#include <qptrlist.h>
#include <qptrstack.h>
#include <qregexp.h>

#include <qobject.h>
#include <qstring.h>
#include <qstringlist.h>
#include <qdatetime.h>
#include <qcolor.h>
#include <qtable.h>
#include <qmap.h>
#include <qdict.h>

#include <kdialogbase.h>
#include <klineedit.h>
#include <knuminput.h>

#include <kdepimmacros.h>

class QDomNode;
class QDomDocument;
class QDomElement;
class QTextStream;
class QLabel;


/**
  The following classes ScorableArticle, ScorableGroup define
  the interface for the scoring. Any application using this mechanism should
  create its own subclasses of this classes. The scoring itself will be handled
  by the ScoringManager class.
 */

//----------------------------------------------------------------------------
class KDE_EXPORT ScorableGroup
{
public:
  virtual ~ScorableGroup();
};

class KDE_EXPORT ScorableArticle
{
public:
  virtual ~ScorableArticle();

  virtual void addScore(short) {}
  virtual void displayMessage(const QString&);
  virtual void changeColor(const QColor&) {}
  virtual void markAsRead() {}
  virtual QString from() const = 0;
  virtual QString subject() const = 0;
  virtual QString getHeaderByType(const QString&) const = 0;
  //virtual ScorableGroup group() const =0;
};


//----------------------------------------------------------------------------
/**
  Base class for other Action classes.
 */
class KDE_EXPORT ActionBase {
 public:
  ActionBase();
  virtual ~ActionBase();
  virtual QString toString() const =0;
  virtual void apply(ScorableArticle&) const =0;
  virtual ActionBase* clone() const =0;
  virtual int getType() const =0;
  virtual QString getValueString() const { return QString(); }
  virtual void setValue(const QString&) {}
  static ActionBase* factory(int type, const QString &value);
  static QStringList userNames();
  static QString userName(int type);
  static int getTypeForName(const QString& name);
  static int getTypeForUserName(const QString& name);
  QString userName() { return userName(getType()); }
  enum ActionTypes { SETSCORE, NOTIFY, COLOR, MARKASREAD };
};

class KDE_EXPORT ActionColor : public ActionBase {
public:
  ActionColor(const QColor&);
  ActionColor(const QString&);
  ActionColor(const ActionColor&);
  virtual ~ActionColor();
  virtual QString toString() const;
  virtual int getType() const { return COLOR; }
  virtual QString getValueString() const { return color.name(); }
  virtual void setValue(const QString& s) { color.setNamedColor(s); }
  void setValue(const QColor& c) { color = c; }
  QColor value() const { return color; }
  virtual void apply(ScorableArticle&) const;
  virtual ActionColor* clone() const;
private:
  QColor color;
};

class KDE_EXPORT ActionSetScore : public ActionBase {
 public:
  ActionSetScore(short);
  ActionSetScore(const ActionSetScore&);
  ActionSetScore(const QString&);
  virtual ~ActionSetScore();
  virtual QString toString() const;
  virtual int getType() const { return SETSCORE; }
  virtual QString getValueString() const { return QString::number(val); }
  virtual void setValue(const QString& s) { val = s.toShort(); }
  void setValue(short v) { val = v; }
  short value() const { return val; }
  virtual void apply(ScorableArticle&) const;
  virtual ActionSetScore* clone() const;
 private:
  short val;
};

class KDE_EXPORT ActionNotify : public ActionBase {
 public:
  ActionNotify(const QString&);
  ActionNotify(const ActionNotify&);
  virtual ~ActionNotify() {}
  virtual QString toString() const;
  virtual int getType() const { return NOTIFY; }
  virtual QString getValueString() const { return note; }
  virtual void setValue(const QString& s) { note = s; }
  virtual void apply(ScorableArticle&) const;
  virtual ActionNotify* clone() const;
 private:
  QString note;
};

class KDE_EXPORT ActionMarkAsRead : public ActionBase {
  public:
    ActionMarkAsRead();
    ActionMarkAsRead( const ActionMarkAsRead& );
    virtual ~ActionMarkAsRead() {}
    virtual QString toString() const;
    virtual int getType() const { return MARKASREAD; }
    virtual void apply( ScorableArticle &article ) const;
    virtual ActionMarkAsRead* clone() const;
};

class KDE_EXPORT NotifyCollection
{
public:
  NotifyCollection();
  ~NotifyCollection();
  void addNote(const ScorableArticle&, const QString&);
  QString collection() const;
  void displayCollection(QWidget *p=0) const;
private:
  struct article_info {
    QString from;
    QString subject;
  };
  typedef QValueList<article_info> article_list;
  typedef QDict<article_list> note_list;
  note_list notifyList;
};


//----------------------------------------------------------------------------
class KDE_EXPORT KScoringExpression
{
  friend class KScoringRule;
 public:
  enum Condition { CONTAINS, MATCH, EQUALS, SMALLER, GREATER, MATCHCS };

  KScoringExpression(const QString&,const QString&,const QString&, const QString&);
  ~KScoringExpression();

  bool match(ScorableArticle& a) const ;
  QString getTypeString() const;
  static QString getTypeString(int);
  int getType() const;
  QString toString() const;
  void write(QTextStream& ) const;

  bool isNeg() const { return neg; }
  Condition getCondition() const { return cond; }
  QString getExpression() const { return expr_str; }
  QString getHeader() const { return header; }
  static QStringList conditionNames();
  static QStringList headerNames();
  static int getConditionForName(const QString&);
  static QString getNameForCondition(int);
 private:
  bool neg;
  QString header;
  const char* c_header;
  Condition cond;
  QRegExp expr;
  QString expr_str;
  int expr_int;
};

//----------------------------------------------------------------------------
class KDE_EXPORT KScoringRule
{
  friend class KScoringManager;
 public:
  KScoringRule(const QString& name);
  KScoringRule(const KScoringRule& r);
  ~KScoringRule();

  typedef QPtrList<KScoringExpression> ScoreExprList;
  typedef QPtrList<ActionBase> ActionList;
  typedef QStringList GroupList;
  enum LinkMode { AND, OR };

  QString getName() const { return name; }
  QStringList getGroups() const { return groups; }
  void setGroups(const QStringList &l) { groups = l; }
  LinkMode getLinkMode() const { return link; }
  QString getLinkModeName() const;
  QString getExpireDateString() const;
  QDate getExpireDate() const { return expires; }
  void setExpireDate(const QDate &d) { expires = d; }
  bool isExpired() const;
  ScoreExprList getExpressions() const { return expressions; }
  ActionList getActions() const { return actions; }
  void cleanExpressions();
  void cleanActions();

  bool matchGroup(const QString& group) const ;
  void applyRule(ScorableArticle& a) const;
  void applyRule(ScorableArticle& a, const QString& group) const;
  void applyAction(ScorableArticle& a) const;

  void setLinkMode(const QString& link);
  void setLinkMode(LinkMode m) { link = m; }
  void setExpire(const QString& exp);
  void addExpression( KScoringExpression* );
  void addGroup( const QString& group) { groups.append(group); }
  //void addServer(const QString& server) { servers.append(server); }
  void addAction(int, const QString& );
  void addAction(ActionBase*);

  void updateXML(QDomElement& e, QDomDocument& d);
  QString toString() const;

  // writes the rule in XML format into the textstream
  void write(QTextStream& ) const;
protected:
  //! assert that the name is unique
  void setName(const QString &n) { name = n; }
private:
  QString name;
  GroupList groups;
  //ServerList servers;
  LinkMode link;
  ScoreExprList expressions;
  ActionList actions;
  QDate expires;
};

/** this helper class implements a stack for lists of lists of rules.
    With the help of this class its very easy for the KScoringManager
    to temporary drop lists of rules and restore them afterwards
*/
class KDE_EXPORT RuleStack
{
public:
  RuleStack();
  ~RuleStack();
  //! puts the list on the stack, doesn't change the list
  void push(QPtrList<KScoringRule>&);
  //! clears the argument list and copy the content of the TOS into it
  //! after that the TOS gets dropped
  void pop(QPtrList<KScoringRule>&);
  //! like pop but without dropping the TOS
  void top(QPtrList<KScoringRule>&);
  //! drops the TOS
  void drop();
private:
  QPtrStack< QPtrList<KScoringRule> > stack;
};

//----------------------------------------------------------------------------
// Manages the score rules.
class KDE_EXPORT KScoringManager : public QObject
{
  Q_OBJECT

 public:
  //* this is the container for all rules
  typedef QPtrList<KScoringRule> ScoringRuleList;

  KScoringManager(const QString& appName = QString::null);
  virtual ~KScoringManager();

  //* returns a list of all available groups, must be overridden
  virtual QStringList getGroups() const =0;

  //! returns a list of common (or available) headers
  //! defaults to returning { Subject, From, Message-ID, Date }
  virtual QStringList getDefaultHeaders() const;

  //* setting current server and group and calling applyRules(ScorableArticle&)
  void applyRules(ScorableArticle& article, const QString& group/*, const QString& server*/);
  //* assuming a properly set group
  void applyRules(ScorableArticle&);
  //* same as above
  void applyRules(ScorableGroup* group);

  //* pushes the current rule list onto a stack
  void pushRuleList();
  //* restores the current rule list from list stored on a stack
  //* by a previous call to pushRuleList (this implicitly deletes the
  //* current rule list)
  void popRuleList();
  //* removes the TOS from the stack of rule lists
  void removeTOS();

  KScoringRule* addRule(KScoringRule *);
  KScoringRule* addRule(const ScorableArticle&, QString group, short =0);
  KScoringRule* addRule();
  void cancelNewRule(KScoringRule *);
  void deleteRule(KScoringRule *);
  void editRule(KScoringRule *e, QWidget *w=0);
  KScoringRule* copyRule(KScoringRule *);
  void moveRuleAbove( KScoringRule *above, KScoringRule *below );
  void moveRuleBelow( KScoringRule *below, KScoringRule *above );
  void setGroup(const QString& g);
  // has to be called after setGroup() or initCache()
  bool hasRulesForCurrentGroup();
  QString findUniqueName() const;

  /** called from an editor whenever it finishes editing the rule base,
      causes the finishedEditing signal to be emitted */
  void editorReady();

  ScoringRuleList getAllRules() const { return allRules; }
  KScoringRule *findRule(const QString&);
  QStringList getRuleNames();
  void setRuleName(KScoringRule *, const QString&);
  int getRuleCount() const { return allRules.count(); }
  QString toString() const;

  bool setCacheValid(bool v);
  bool isCacheValid() { return cacheValid; }
  void initCache(const QString& group/*, const QString& server*/);

  void load();
  void save();

  //--------------- Properties
  virtual bool canScores() const { return true; }
  virtual bool canNotes() const { return true; }
  virtual bool canColors() const { return false; }
  virtual bool canMarkAsRead() const { return false; }
  virtual bool hasFeature(int);

 signals:
  void changedRules();
  void changedRuleName(const QString& oldName, const QString& newName);
  void finishedEditing();

 private:
  void addRuleInternal(KScoringRule *e);
  void expireRules();

  QDomDocument createXMLfromInternal();
  void createInternalFromXML(QDomNode);

  // list of all Rules
  ScoringRuleList allRules;

  // the stack for temporary storing rule lists
  RuleStack stack;

  // for the cache
  bool cacheValid;
  // current rule set, ie the cache
  ScoringRuleList ruleList;
  //QString server;
  QString group;

  //ScorableServer* _s;

  // filename of the scorefile
  QString mFilename;
};


//----------------------------------------------------------------------------
class KDE_EXPORT NotifyDialog : public KDialogBase
{
  Q_OBJECT
public:
  static void display(ScorableArticle&,const QString&);
protected slots:
  void slotShowAgainToggled(bool);
private:
  NotifyDialog(QWidget* p =0);
  static NotifyDialog *me;

  QLabel *note;
  QString msg;
  typedef QMap<QString,bool> NotesMap;
  static NotesMap dict;
};


#endif