libkdepim
kscoring.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef KDEPIM_KSCORING_H
00019 #define KDEPIM_KSCORING_H
00020
00021 #include "kdepim_export.h"
00022
00023 #include <KDialog>
00024
00025 #include <QColor>
00026 #include <QMap>
00027 #include <QObject>
00028 #include <QRegExp>
00029 #include <QString>
00030 #include <QStringList>
00031 #include <QTextStream>
00032 #include <Q3Dict>
00033 #include <Q3PtrList>
00034 #include <Q3PtrStack>
00035
00036 #include <unistd.h>
00037
00038 class QDomNode;
00039 class QDomDocument;
00040 class QDomElement;
00041 class QLabel;
00042
00043 namespace KPIM {
00044
00052
00053 class KDEPIM_EXPORT ScorableGroup
00054 {
00055 public:
00056 virtual ~ScorableGroup();
00057 };
00058
00059 class KDEPIM_EXPORT ScorableArticle
00060 {
00061 public:
00062 virtual ~ScorableArticle();
00063
00064 virtual void addScore( short ) {}
00065 virtual void displayMessage( const QString & );
00066 virtual void changeColor( const QColor & ) {}
00067 virtual void markAsRead() {}
00068 virtual QString from() const = 0;
00069 virtual QString subject() const = 0;
00070 virtual QString getHeaderByType( const QString & ) const = 0;
00071 };
00072
00073
00077 class KDEPIM_EXPORT ActionBase {
00078 public:
00079 ActionBase();
00080 virtual ~ActionBase();
00081 virtual QString toString() const = 0;
00082 virtual void apply( ScorableArticle & ) const = 0;
00083 virtual ActionBase *clone() const = 0;
00084 virtual int getType() const = 0;
00085 virtual QString getValueString() const { return QString(); }
00086 virtual void setValue( const QString & ) {}
00087 static ActionBase *factory( int type, const QString &value );
00088 static QStringList userNames();
00089 static QString userName( int type );
00090 static int getTypeForName( const QString &name );
00091 static int getTypeForUserName( const QString &name );
00092 QString userName() { return userName( getType() ); }
00093 enum ActionTypes {
00094 SETSCORE,
00095 NOTIFY,
00096 COLOR,
00097 MARKASREAD
00098 };
00099 };
00100
00101 class KDEPIM_EXPORT ActionColor : public ActionBase {
00102 public:
00103 ActionColor( const QColor & );
00104 ActionColor( const QString & );
00105 ActionColor( const ActionColor & );
00106 virtual ~ActionColor();
00107 virtual QString toString() const;
00108 virtual int getType() const { return COLOR; }
00109 virtual QString getValueString() const { return color.name(); }
00110 virtual void setValue( const QString &s ) { color.setNamedColor(s); }
00111 void setValue( const QColor &c ) { color = c; }
00112 QColor value() const { return color; }
00113 virtual void apply( ScorableArticle & ) const;
00114 virtual ActionColor *clone() const;
00115
00116 private:
00117 QColor color;
00118 };
00119
00120 class KDEPIM_EXPORT ActionSetScore : public ActionBase {
00121 public:
00122 ActionSetScore( short );
00123 ActionSetScore( const ActionSetScore & );
00124 ActionSetScore( const QString & );
00125 virtual ~ActionSetScore();
00126 virtual QString toString() const;
00127 virtual int getType() const { return SETSCORE; }
00128 virtual QString getValueString() const { return QString::number(val); }
00129 virtual void setValue( const QString &s ) { val = s.toShort(); }
00130 void setValue( short v ) { val = v; }
00131 short value() const { return val; }
00132 virtual void apply( ScorableArticle & ) const;
00133 virtual ActionSetScore *clone() const;
00134
00135 private:
00136 short val;
00137 };
00138
00139 class KDEPIM_EXPORT ActionNotify : public ActionBase {
00140 public:
00141 ActionNotify( const QString & );
00142 ActionNotify( const ActionNotify & );
00143 virtual ~ActionNotify() {}
00144 virtual QString toString() const;
00145 virtual int getType() const { return NOTIFY; }
00146 virtual QString getValueString() const { return note; }
00147 virtual void setValue( const QString &s ) { note = s; }
00148 virtual void apply( ScorableArticle & ) const;
00149 virtual ActionNotify *clone() const;
00150
00151 private:
00152 QString note;
00153 };
00154
00155 class KDEPIM_EXPORT ActionMarkAsRead : public ActionBase {
00156 public:
00157 ActionMarkAsRead();
00158 ActionMarkAsRead( const ActionMarkAsRead & );
00159 virtual ~ActionMarkAsRead() {}
00160 virtual QString toString() const;
00161 virtual int getType() const { return MARKASREAD; }
00162 virtual void apply( ScorableArticle &article ) const;
00163 virtual ActionMarkAsRead *clone() const;
00164 };
00165
00166 class KDEPIM_EXPORT NotifyCollection
00167 {
00168 public:
00169 NotifyCollection();
00170 ~NotifyCollection();
00171 void addNote( const ScorableArticle &, const QString & );
00172 QString collection() const;
00173 void displayCollection( QWidget *p=0 ) const;
00174 private:
00175 struct article_info {
00176 QString from;
00177 QString subject;
00178 };
00179 typedef QList<article_info> article_list;
00180 typedef Q3Dict<article_list> note_list;
00181 note_list notifyList;
00182 };
00183
00184
00185 class KDEPIM_EXPORT KScoringExpression
00186 {
00187 friend class KScoringRule;
00188 public:
00189 enum Condition {
00190 CONTAINS,
00191 MATCH,
00192 EQUALS,
00193 SMALLER,
00194 GREATER,
00195 MATCHCS
00196 };
00197
00198 KScoringExpression( const QString &, const QString &, const QString &, const QString & );
00199 ~KScoringExpression();
00200
00201 bool match( ScorableArticle &a ) const ;
00202 QString getTypeString() const;
00203 static QString getTypeString( int );
00204 int getType() const;
00205 QString toString() const;
00206 void write( QTextStream & ) const;
00207
00208 bool isNeg() const { return neg; }
00209 Condition getCondition() const { return cond; }
00210 QString getExpression() const { return expr_str; }
00211 QString getHeader() const { return header; }
00212 static QStringList conditionNames();
00213 static QStringList headerNames();
00214 static int getConditionForName( const QString & );
00215 static QString getNameForCondition( int );
00216
00217 private:
00218 bool neg;
00219 QString header;
00220 const char *c_header;
00221 Condition cond;
00222 QRegExp expr;
00223 QString expr_str;
00224 int expr_int;
00225 };
00226
00227
00228 class KDEPIM_EXPORT KScoringRule
00229 {
00230 friend class KScoringManager;
00231 public:
00232 KScoringRule( const QString &name );
00233 KScoringRule( const KScoringRule &r );
00234 ~KScoringRule();
00235
00236 typedef Q3PtrList<KScoringExpression> ScoreExprList;
00237 typedef Q3PtrList<ActionBase> ActionList;
00238 typedef QStringList GroupList;
00239 enum LinkMode {
00240 AND,
00241 OR
00242 };
00243
00244 QString getName() const { return name; }
00245 QStringList getGroups() const { return groups; }
00246 void setGroups( const QStringList &l ) { groups = l; }
00247 LinkMode getLinkMode() const { return link; }
00248 QString getLinkModeName() const;
00249 QString getExpireDateString() const;
00250 QDate getExpireDate() const { return expires; }
00251 void setExpireDate( const QDate &d ) { expires = d; }
00252 bool isExpired() const;
00253 ScoreExprList getExpressions() const { return expressions; }
00254 ActionList getActions() const { return actions; }
00255 void cleanExpressions();
00256 void cleanActions();
00257
00258 bool matchGroup( const QString &group ) const ;
00259 void applyRule( ScorableArticle &a ) const;
00260 void applyRule( ScorableArticle &a, const QString &group ) const;
00261 void applyAction( ScorableArticle &a ) const;
00262
00263 void setLinkMode( const QString &link );
00264 void setLinkMode( LinkMode m ) { link = m; }
00265 void setExpire( const QString &exp );
00266 void addExpression( KScoringExpression * );
00267 void addGroup( const QString &group ) { groups.append(group); }
00268 void addAction( int, const QString & );
00269 void addAction(ActionBase*);
00270
00271 void updateXML( QDomElement &e, QDomDocument &d );
00272 QString toString() const;
00273
00274
00275 void write( QTextStream & ) const;
00276
00277 protected:
00279 void setName( const QString &n ) { name = n; }
00280
00281 private:
00282 QString name;
00283 GroupList groups;
00284
00285 LinkMode link;
00286 ScoreExprList expressions;
00287 ActionList actions;
00288 QDate expires;
00289 };
00290
00295 class KDEPIM_EXPORT RuleStack
00296 {
00297 public:
00298 RuleStack();
00299 ~RuleStack();
00301 void push( Q3PtrList<KScoringRule>& );
00304 void pop( Q3PtrList<KScoringRule>& );
00306 void top( Q3PtrList<KScoringRule>& );
00308 void drop();
00309
00310 private:
00311 Q3PtrStack< Q3PtrList<KScoringRule> > stack;
00312 };
00313
00314
00315
00316 class KDEPIM_EXPORT KScoringManager : public QObject
00317 {
00318 Q_OBJECT
00319 public:
00320
00321 typedef Q3PtrList<KScoringRule> ScoringRuleList;
00322
00323 KScoringManager( const QString &appName = QString() );
00324 virtual ~KScoringManager();
00325
00326
00327 virtual QStringList getGroups() const = 0;
00328
00331 virtual QStringList getDefaultHeaders() const;
00332
00333
00334 void applyRules( ScorableArticle &article, const QString &group );
00335
00336 void applyRules( ScorableArticle & );
00337
00338 void applyRules( ScorableGroup *group );
00339
00340
00341 void pushRuleList();
00342
00343
00344
00345 void popRuleList();
00346
00347 void removeTOS();
00348
00349 KScoringRule *addRule( KScoringRule * );
00350 KScoringRule *addRule( const ScorableArticle &a, const QString &group, short score=0 );
00351 KScoringRule *addRule();
00352 void cancelNewRule( KScoringRule * );
00353 void deleteRule( KScoringRule * );
00354 void editRule( KScoringRule *e, QWidget *w=0 );
00355 KScoringRule *copyRule( KScoringRule * );
00356 void moveRuleAbove( KScoringRule *above, KScoringRule *below );
00357 void moveRuleBelow( KScoringRule *below, KScoringRule *above );
00358 void setGroup( const QString &g );
00359
00360 bool hasRulesForCurrentGroup();
00361 QString findUniqueName() const;
00362
00365 void editorReady();
00366
00367 ScoringRuleList getAllRules() const { return allRules; }
00368 KScoringRule *findRule( const QString & );
00369 QStringList getRuleNames();
00370 void setRuleName( KScoringRule *, const QString & );
00371 int getRuleCount() const { return allRules.count(); }
00372 QString toString() const;
00373
00374 bool setCacheValid( bool v );
00375 bool isCacheValid() { return cacheValid; }
00376 void initCache( const QString &group );
00377
00378 void load();
00379 void save();
00380
00381
00382 virtual bool canScores() const { return true; }
00383 virtual bool canNotes() const { return true; }
00384 virtual bool canColors() const { return false; }
00385 virtual bool canMarkAsRead() const { return false; }
00386 virtual bool hasFeature( int );
00387
00388 Q_SIGNALS:
00389 void changedRules();
00390 void changedRuleName( const QString &oldName, const QString &newName );
00391 void finishedEditing();
00392
00393 private:
00394 void addRuleInternal( KScoringRule *e );
00395 void expireRules();
00396
00397 QDomDocument createXMLfromInternal();
00398 void createInternalFromXML(QDomNode);
00399
00400
00401 ScoringRuleList allRules;
00402
00403
00404 RuleStack stack;
00405
00406
00407 bool cacheValid;
00408
00409 ScoringRuleList ruleList;
00410
00411 QString group;
00412
00413
00414
00415
00416 QString mFilename;
00417 };
00418
00419
00420 class KDEPIM_EXPORT NotifyDialog : public KDialog
00421 {
00422 Q_OBJECT
00423 public:
00424 static void display( ScorableArticle &, const QString & );
00425
00426 protected Q_SLOTS:
00427 void slotShowAgainToggled( bool );
00428
00429 private:
00430 NotifyDialog( QWidget *p=0 );
00431 static NotifyDialog *me;
00432
00433 QLabel *note;
00434 QString msg;
00435 typedef QMap<QString,bool> NotesMap;
00436 static NotesMap dict;
00437 };
00438
00439 }
00440
00441 #endif