4 Copyright (c) 2001 Mathias Waack <mathias@atoll-net.de>
5 Copyright (C) 2005 by Volker Krause <vkrause@kde.org>
7 Author: Mathias Waack <mathias@atoll-net.de>
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
18 #ifndef KDEPIM_KSCORING_H
19 #define KDEPIM_KSCORING_H
21 #include "knode_export.h"
30 #include <QStringList>
31 #include <QTextStream>
46 The following classes ScorableArticle, ScorableGroup define
47 the interface for the scoring. Any application using this mechanism should
48 create its own subclasses of this classes. The scoring itself will be handled
49 by the ScoringManager class.
52 //----------------------------------------------------------------------------
53 class KNODE_EXPORT ScorableGroup
56 virtual ~ScorableGroup();
59 class KNODE_EXPORT ScorableArticle
62 virtual ~ScorableArticle();
64 virtual void addScore( short ) {}
65 virtual void displayMessage( const QString
& );
66 virtual void changeColor( const QColor
& ) {}
67 virtual void markAsRead() {}
68 virtual QString
from() const = 0;
69 virtual QString
subject() const = 0;
70 virtual QString
getHeaderByType( const QString
& ) const = 0;
73 //----------------------------------------------------------------------------
75 Base class for other Action classes.
77 class KNODE_EXPORT ActionBase
{
80 virtual ~ActionBase();
81 virtual QString
toString() const = 0;
82 virtual void apply( ScorableArticle
& ) const = 0;
83 virtual ActionBase
*clone() const = 0;
84 virtual int getType() const = 0;
85 virtual QString
getValueString() const { return QString(); }
86 virtual void setValue( const QString
& ) {}
87 static ActionBase
*factory( int type
, const QString
&value
);
88 static QStringList
userNames();
89 static QString
userName( int type
);
90 static int getTypeForName( const QString
&name
);
91 static int getTypeForUserName( const QString
&name
);
92 QString
userName() { return userName( getType() ); }
101 class KNODE_EXPORT ActionColor
: public ActionBase
{
103 ActionColor( const QColor
& );
104 ActionColor( const QString
& );
105 ActionColor( const ActionColor
& );
106 virtual ~ActionColor();
107 virtual QString
toString() const;
108 virtual int getType() const { return COLOR
; }
109 virtual QString
getValueString() const { return color
.name(); }
110 virtual void setValue( const QString
&s
) { color
.setNamedColor(s
); }
111 void setValue( const QColor
&c
) { color
= c
; }
112 QColor
value() const { return color
; }
113 virtual void apply( ScorableArticle
& ) const;
114 virtual ActionColor
*clone() const;
120 class KNODE_EXPORT ActionSetScore
: public ActionBase
{
122 ActionSetScore( short );
123 ActionSetScore( const ActionSetScore
& );
124 ActionSetScore( const QString
& );
125 virtual ~ActionSetScore();
126 virtual QString
toString() const;
127 virtual int getType() const { return SETSCORE
; }
128 virtual QString
getValueString() const { return QString::number(val
); }
129 virtual void setValue( const QString
&s
) { val
= s
.toShort(); }
130 void setValue( short v
) { val
= v
; }
131 short value() const { return val
; }
132 virtual void apply( ScorableArticle
& ) const;
133 virtual ActionSetScore
*clone() const;
139 class KNODE_EXPORT ActionNotify
: public ActionBase
{
141 ActionNotify( const QString
& );
142 ActionNotify( const ActionNotify
& );
143 virtual ~ActionNotify() {}
144 virtual QString
toString() const;
145 virtual int getType() const { return NOTIFY
; }
146 virtual QString
getValueString() const { return note
; }
147 virtual void setValue( const QString
&s
) { note
= s
; }
148 virtual void apply( ScorableArticle
& ) const;
149 virtual ActionNotify
*clone() const;
155 class KNODE_EXPORT ActionMarkAsRead
: public ActionBase
{
158 ActionMarkAsRead( const ActionMarkAsRead
& );
159 virtual ~ActionMarkAsRead() {}
160 virtual QString
toString() const;
161 virtual int getType() const { return MARKASREAD
; }
162 virtual void apply( ScorableArticle
&article
) const;
163 virtual ActionMarkAsRead
*clone() const;
166 class KNODE_EXPORT NotifyCollection
171 void addNote( const ScorableArticle
&, const QString
& );
172 QString
collection() const;
173 void displayCollection( QWidget
*p
=0 ) const;
175 struct article_info
{
179 typedef QList
<article_info
> article_list
;
180 typedef Q3Dict
<article_list
> note_list
;
181 note_list notifyList
;
184 //----------------------------------------------------------------------------
185 class KNODE_EXPORT KScoringExpression
187 friend class KScoringRule
;
198 KScoringExpression( const QString
&, const QString
&, const QString
&, const QString
& );
199 ~KScoringExpression();
201 bool match( ScorableArticle
&a
) const ;
202 QString
getTypeString() const;
203 static QString
getTypeString( int );
205 QString
toString() const;
206 void write( QTextStream
& ) const;
208 bool isNeg() const { return neg
; }
209 Condition
getCondition() const { return cond
; }
210 QString
getExpression() const { return expr_str
; }
211 QString
getHeader() const { return header
; }
212 static QStringList
conditionNames();
213 static QStringList
headerNames();
214 static int getConditionForName( const QString
& );
215 static QString
getNameForCondition( int );
226 //----------------------------------------------------------------------------
227 class KNODE_EXPORT KScoringRule
229 friend class KScoringManager
;
231 KScoringRule( const QString
&name
);
232 KScoringRule( const KScoringRule
&r
);
235 typedef Q3PtrList
<KScoringExpression
> ScoreExprList
;
236 typedef Q3PtrList
<ActionBase
> ActionList
;
237 typedef QStringList GroupList
;
243 QString
getName() const { return name
; }
244 QStringList
getGroups() const { return groups
; }
245 void setGroups( const QStringList
&l
) { groups
= l
; }
246 LinkMode
getLinkMode() const { return link
; }
247 QString
getLinkModeName() const;
248 QString
getExpireDateString() const;
249 QDate
getExpireDate() const { return expires
; }
250 void setExpireDate( const QDate
&d
) { expires
= d
; }
251 bool isExpired() const;
252 ScoreExprList
getExpressions() const { return expressions
; }
253 ActionList
getActions() const { return actions
; }
254 void cleanExpressions();
257 bool matchGroup( const QString
&group
) const ;
258 void applyRule( ScorableArticle
&a
) const;
259 void applyRule( ScorableArticle
&a
, const QString
&group
) const;
260 void applyAction( ScorableArticle
&a
) const;
262 void setLinkMode( const QString
&link
);
263 void setLinkMode( LinkMode m
) { link
= m
; }
264 void setExpire( const QString
&exp
);
265 void addExpression( KScoringExpression
* );
266 void addGroup( const QString
&group
) { groups
.append(group
); }
267 void addAction( int, const QString
& );
268 void addAction(ActionBase
*);
270 void updateXML( QDomElement
&e
, QDomDocument
&d
);
271 QString
toString() const;
273 // writes the rule in XML format into the textstream
274 void write( QTextStream
& ) const;
277 //! assert that the name is unique
278 void setName( const QString
&n
) { name
= n
; }
283 //ServerList servers;
285 ScoreExprList expressions
;
290 /** this helper class implements a stack for lists of lists of rules.
291 With the help of this class it is very easy for the KScoringManager
292 to temporary drop lists of rules and restore them afterwards
294 class KNODE_EXPORT RuleStack
299 //! puts the list on the stack, doesn't change the list
300 void push( Q3PtrList
<KScoringRule
>& );
301 //! clears the argument list and copy the content of the TOS into it
302 //! after that the TOS gets dropped
303 void pop( Q3PtrList
<KScoringRule
>& );
304 //! like pop but without dropping the TOS
305 void top( Q3PtrList
<KScoringRule
>& );
310 Q3PtrStack
< Q3PtrList
<KScoringRule
> > stack
;
313 //----------------------------------------------------------------------------
314 // Manages the score rules.
315 class KNODE_EXPORT KScoringManager
: public QObject
319 // this is the container for all rules
320 typedef Q3PtrList
<KScoringRule
> ScoringRuleList
;
322 KScoringManager( const QString
&appName
= QString() );
323 virtual ~KScoringManager();
325 // returns a list of all available groups, must be overridden
326 virtual QStringList
getGroups() const = 0;
328 //! returns a list of common (or available) headers
329 //! defaults to returning { Subject, From, Message-ID, Date }
330 virtual QStringList
getDefaultHeaders() const;
332 // setting current server and group and calling applyRules(ScorableArticle&)
333 void applyRules( ScorableArticle
&article
, const QString
&group
);
334 // assuming a properly set group
335 void applyRules( ScorableArticle
& );
337 void applyRules( ScorableGroup
*group
);
339 // pushes the current rule list onto a stack
341 // restores the current rule list from list stored on a stack
342 // by a previous call to pushRuleList (this implicitly deletes the
343 // current rule list)
345 // removes the TOS from the stack of rule lists
348 KScoringRule
*addRule( KScoringRule
* );
349 KScoringRule
*addRule( const ScorableArticle
&a
, const QString
&group
, short score
=0 );
350 KScoringRule
*addRule();
351 void cancelNewRule( KScoringRule
* );
352 void deleteRule( KScoringRule
* );
353 void editRule( KScoringRule
*e
, QWidget
*w
=0 );
354 KScoringRule
*copyRule( KScoringRule
* );
355 void moveRuleAbove( KScoringRule
*above
, KScoringRule
*below
);
356 void moveRuleBelow( KScoringRule
*below
, KScoringRule
*above
);
357 void setGroup( const QString
&g
);
358 // has to be called after setGroup() or initCache()
359 bool hasRulesForCurrentGroup();
360 QString
findUniqueName() const;
362 /** called from an editor whenever it finishes editing the rule base,
363 causes the finishedEditing signal to be emitted */
366 ScoringRuleList
getAllRules() const { return allRules
; }
367 KScoringRule
*findRule( const QString
& );
368 QStringList
getRuleNames();
369 void setRuleName( KScoringRule
*, const QString
& );
370 int getRuleCount() const { return allRules
.count(); }
371 QString
toString() const;
373 bool setCacheValid( bool v
);
374 bool isCacheValid() { return cacheValid
; }
375 void initCache( const QString
&group
);
380 //--------------- Properties
381 virtual bool canScores() const { return true; }
382 virtual bool canNotes() const { return true; }
383 virtual bool canColors() const { return false; }
384 virtual bool canMarkAsRead() const { return false; }
385 virtual bool hasFeature( int );
389 void changedRuleName( const QString
&oldName
, const QString
&newName
);
390 void finishedEditing();
393 void addRuleInternal( KScoringRule
*e
);
396 QDomDocument
createXMLfromInternal();
397 void createInternalFromXML(QDomNode
);
400 ScoringRuleList allRules
;
402 // the stack for temporary storing rule lists
407 // current rule set, ie the cache
408 ScoringRuleList ruleList
;
412 //ScorableServer* _s;
414 // filename of the scorefile
418 //----------------------------------------------------------------------------
419 class KNODE_EXPORT NotifyDialog
: public KDialog
423 static void display( ScorableArticle
&, const QString
& );
426 void slotShowAgainToggled( bool );
429 NotifyDialog( QWidget
*p
=0 );
430 static NotifyDialog
*me
;
434 typedef QMap
<QString
,bool> NotesMap
;
435 static NotesMap dict
;