libkdepim

kscoring.h

00001 /*
00002     kscoring.h
00003 
00004     Copyright (c) 2001 Mathias Waack
00005     Copyright (C) 2005 by Volker Krause <volker.krause@rwth-aachen.de>
00006 
00007     Author: Mathias Waack <mathias@atoll-net.de>
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013     You should have received a copy of the GNU General Public License
00014     along with this program; if not, write to the Free Software Foundation,
00015     Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
00016 */
00017 
00018 #ifndef KSCORING_H
00019 #define KSCORING_H
00020 
00021 #include <unistd.h>
00022 
00023 #include <qglobal.h>
00024 #include <qptrlist.h>
00025 #include <qptrstack.h>
00026 #include <qregexp.h>
00027 
00028 #include <qobject.h>
00029 #include <qstring.h>
00030 #include <qstringlist.h>
00031 #include <qdatetime.h>
00032 #include <qcolor.h>
00033 #include <qtable.h>
00034 #include <qmap.h>
00035 #include <qdict.h>
00036 
00037 #include <kdialogbase.h>
00038 #include <klineedit.h>
00039 #include <knuminput.h>
00040 
00041 #include <kdepimmacros.h>
00042 
00043 class QDomNode;
00044 class QDomDocument;
00045 class QDomElement;
00046 class QTextStream;
00047 class QLabel;
00048 
00049 
00057 //----------------------------------------------------------------------------
00058 class KDE_EXPORT ScorableGroup
00059 {
00060 public:
00061   virtual ~ScorableGroup();
00062 };
00063 
00064 class KDE_EXPORT ScorableArticle
00065 {
00066 public:
00067   virtual ~ScorableArticle();
00068 
00069   virtual void addScore(short) {}
00070   virtual void displayMessage(const QString&);
00071   virtual void changeColor(const QColor&) {}
00072   virtual void markAsRead() {}
00073   virtual QString from() const = 0;
00074   virtual QString subject() const = 0;
00075   virtual QString getHeaderByType(const QString&) const = 0;
00076   //virtual ScorableGroup group() const =0;
00077 };
00078 
00079 
00080 //----------------------------------------------------------------------------
00084 class KDE_EXPORT ActionBase {
00085  public:
00086   ActionBase();
00087   virtual ~ActionBase();
00088   virtual QString toString() const =0;
00089   virtual void apply(ScorableArticle&) const =0;
00090   virtual ActionBase* clone() const =0;
00091   virtual int getType() const =0;
00092   virtual QString getValueString() const { return QString(); }
00093   virtual void setValue(const QString&) {};
00094   static ActionBase* factory(int type, const QString &value);
00095   static QStringList userNames();
00096   static QString userName(int type);
00097   static int getTypeForName(const QString& name);
00098   static int getTypeForUserName(const QString& name);
00099   QString userName() { return userName(getType()); }
00100   enum ActionTypes { SETSCORE, NOTIFY, COLOR, MARKASREAD };
00101 };
00102 
00103 class KDE_EXPORT ActionColor : public ActionBase {
00104 public:
00105   ActionColor(const QColor&);
00106   ActionColor(const QString&);
00107   ActionColor(const ActionColor&);
00108   virtual ~ActionColor();
00109   virtual QString toString() const;
00110   virtual int getType() const { return COLOR; }
00111   virtual QString getValueString() const { return color.name(); }
00112   virtual void setValue(const QString& s) { color.setNamedColor(s); }
00113   void setValue(const QColor& c) { color = c; }
00114   QColor value() const { return color; }
00115   virtual void apply(ScorableArticle&) const;
00116   virtual ActionColor* clone() const;
00117 private:
00118   QColor color;
00119 };
00120 
00121 class KDE_EXPORT ActionSetScore : public ActionBase {
00122  public:
00123   ActionSetScore(short);
00124   ActionSetScore(const ActionSetScore&);
00125   ActionSetScore(const QString&);
00126   virtual ~ActionSetScore();
00127   virtual QString toString() const;
00128   virtual int getType() const { return SETSCORE; }
00129   virtual QString getValueString() const { return QString::number(val); }
00130   virtual void setValue(const QString& s) { val = s.toShort(); }
00131   void setValue(short v) { val = v; }
00132   short value() const { return val; }
00133   virtual void apply(ScorableArticle&) const;
00134   virtual ActionSetScore* clone() const;
00135  private:
00136   short val;
00137 };
00138 
00139 class KDE_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  private:
00151   QString note;
00152 };
00153 
00154 class KDE_EXPORT ActionMarkAsRead : public ActionBase {
00155   public:
00156     ActionMarkAsRead();
00157     ActionMarkAsRead( const ActionMarkAsRead& );
00158     virtual ~ActionMarkAsRead() {}
00159     virtual QString toString() const;
00160     virtual int getType() const { return MARKASREAD; }
00161     virtual void apply( ScorableArticle &article ) const;
00162     virtual ActionMarkAsRead* clone() const;
00163 };
00164 
00165 class KDE_EXPORT NotifyCollection
00166 {
00167 public:
00168   NotifyCollection();
00169   ~NotifyCollection();
00170   void addNote(const ScorableArticle&, const QString&);
00171   QString collection() const;
00172   void displayCollection(QWidget *p=0) const;
00173 private:
00174   struct article_info {
00175     QString from;
00176     QString subject;
00177   };
00178   typedef QValueList<article_info> article_list;
00179   typedef QDict<article_list> note_list;
00180   note_list notifyList;
00181 };
00182 
00183 
00184 //----------------------------------------------------------------------------
00185 class KDE_EXPORT KScoringExpression
00186 {
00187   friend class KScoringRule;
00188  public:
00189   enum Condition { CONTAINS, MATCH, EQUALS, SMALLER, GREATER, MATCHCS };
00190 
00191   KScoringExpression(const QString&,const QString&,const QString&, const QString&);
00192   ~KScoringExpression();
00193 
00194   bool match(ScorableArticle& a) const ;
00195   QString getTypeString() const;
00196   static QString getTypeString(int);
00197   int getType() const;
00198   QString toString() const;
00199   void write(QTextStream& ) const;
00200 
00201   bool isNeg() const { return neg; }
00202   Condition getCondition() const { return cond; }
00203   QString getExpression() const { return expr_str; }
00204   QString getHeader() const { return header; }
00205   static QStringList conditionNames();
00206   static QStringList headerNames();
00207   static int getConditionForName(const QString&);
00208   static QString getNameForCondition(int);
00209  private:
00210   bool neg;
00211   QString header;
00212   const char* c_header;
00213   Condition cond;
00214   QRegExp expr;
00215   QString expr_str;
00216   int expr_int;
00217 };
00218 
00219 //----------------------------------------------------------------------------
00220 class KDE_EXPORT KScoringRule
00221 {
00222   friend class KScoringManager;
00223  public:
00224   KScoringRule(const QString& name);
00225   KScoringRule(const KScoringRule& r);
00226   ~KScoringRule();
00227 
00228   typedef QPtrList<KScoringExpression> ScoreExprList;
00229   typedef QPtrList<ActionBase> ActionList;
00230   typedef QStringList GroupList;
00231   enum LinkMode { AND, OR };
00232 
00233   QString getName() const { return name; }
00234   QStringList getGroups() const { return groups; }
00235   void setGroups(const QStringList &l) { groups = l; }
00236   LinkMode getLinkMode() const { return link; }
00237   QString getLinkModeName() const;
00238   QString getExpireDateString() const;
00239   QDate getExpireDate() const { return expires; }
00240   void setExpireDate(const QDate &d) { expires = d; }
00241   bool isExpired() const;
00242   ScoreExprList getExpressions() const { return expressions; }
00243   ActionList getActions() const { return actions; }
00244   void cleanExpressions();
00245   void cleanActions();
00246 
00247   bool matchGroup(const QString& group) const ;
00248   void applyRule(ScorableArticle& a) const;
00249   void applyRule(ScorableArticle& a, const QString& group) const;
00250   void applyAction(ScorableArticle& a) const;
00251 
00252   void setLinkMode(const QString& link);
00253   void setLinkMode(LinkMode m) { link = m; }
00254   void setExpire(const QString& exp);
00255   void addExpression( KScoringExpression* );
00256   void addGroup( const QString& group) { groups.append(group); }
00257   //void addServer(const QString& server) { servers.append(server); }
00258   void addAction(int, const QString& );
00259   void addAction(ActionBase*);
00260 
00261   void updateXML(QDomElement& e, QDomDocument& d);
00262   QString toString() const;
00263 
00264   // writes the rule in XML format into the textstream
00265   void write(QTextStream& ) const;
00266 protected:
00268   void setName(const QString &n) { name = n; }
00269 private:
00270   QString name;
00271   GroupList groups;
00272   //ServerList servers;
00273   LinkMode link;
00274   ScoreExprList expressions;
00275   ActionList actions;
00276   QDate expires;
00277 };
00278 
00283 class KDE_EXPORT RuleStack
00284 {
00285 public:
00286   RuleStack();
00287   ~RuleStack();
00289   void push(QPtrList<KScoringRule>&);
00292   void pop(QPtrList<KScoringRule>&);
00294   void top(QPtrList<KScoringRule>&);
00296   void drop();
00297 private:
00298   QPtrStack< QPtrList<KScoringRule> > stack;
00299 };
00300 
00301 //----------------------------------------------------------------------------
00302 // Manages the score rules.
00303 class KDE_EXPORT KScoringManager : public QObject
00304 {
00305   Q_OBJECT
00306 
00307  public:
00308   //* this is the container for all rules
00309   typedef QPtrList<KScoringRule> ScoringRuleList;
00310 
00311   KScoringManager(const QString& appName = QString::null);
00312   virtual ~KScoringManager();
00313 
00314   //* returns a list of all available groups, must be overridden
00315   virtual QStringList getGroups() const =0;
00316 
00319   virtual QStringList getDefaultHeaders() const;
00320 
00321   //* setting current server and group and calling applyRules(ScorableArticle&)
00322   void applyRules(ScorableArticle& article, const QString& group/*, const QString& server*/);
00323   //* assuming a properly set group
00324   void applyRules(ScorableArticle&);
00325   //* same as above
00326   void applyRules(ScorableGroup* group);
00327 
00328   //* pushes the current rule list onto a stack
00329   void pushRuleList();
00330   //* restores the current rule list from list stored on a stack
00331   //* by a previous call to pushRuleList (this implicitly deletes the
00332   //* current rule list)
00333   void popRuleList();
00334   //* removes the TOS from the stack of rule lists
00335   void removeTOS();
00336 
00337   KScoringRule* addRule(KScoringRule *);
00338   KScoringRule* addRule(const ScorableArticle&, QString group, short =0);
00339   KScoringRule* addRule();
00340   void cancelNewRule(KScoringRule *);
00341   void deleteRule(KScoringRule *);
00342   void editRule(KScoringRule *e, QWidget *w=0);
00343   KScoringRule* copyRule(KScoringRule *);
00344   void moveRuleAbove( KScoringRule *above, KScoringRule *below );
00345   void moveRuleBelow( KScoringRule *below, KScoringRule *above );
00346   void setGroup(const QString& g);
00347   // has to be called after setGroup() or initCache()
00348   bool hasRulesForCurrentGroup();
00349   QString findUniqueName() const;
00350 
00353   void editorReady();
00354 
00355   ScoringRuleList getAllRules() const { return allRules; }
00356   KScoringRule *findRule(const QString&);
00357   QStringList getRuleNames();
00358   void setRuleName(KScoringRule *, const QString&);
00359   int getRuleCount() const { return allRules.count(); }
00360   QString toString() const;
00361 
00362   bool setCacheValid(bool v);
00363   bool isCacheValid() { return cacheValid; }
00364   void initCache(const QString& group/*, const QString& server*/);
00365 
00366   void load();
00367   void save();
00368 
00369   //--------------- Properties
00370   virtual bool canScores() const { return true; }
00371   virtual bool canNotes() const { return true; }
00372   virtual bool canColors() const { return false; }
00373   virtual bool canMarkAsRead() const { return false; }
00374   virtual bool hasFeature(int);
00375 
00376  signals:
00377   void changedRules();
00378   void changedRuleName(const QString& oldName, const QString& newName);
00379   void finishedEditing();
00380 
00381  private:
00382   void addRuleInternal(KScoringRule *e);
00383   void expireRules();
00384 
00385   QDomDocument createXMLfromInternal();
00386   void createInternalFromXML(QDomNode);
00387 
00388   // list of all Rules
00389   ScoringRuleList allRules;
00390 
00391   // the stack for temporary storing rule lists
00392   RuleStack stack;
00393 
00394   // for the cache
00395   bool cacheValid;
00396   // current rule set, ie the cache
00397   ScoringRuleList ruleList;
00398   //QString server;
00399   QString group;
00400 
00401   //ScorableServer* _s;
00402 
00403   // filename of the scorefile
00404   QString mFilename;
00405 };
00406 
00407 
00408 //----------------------------------------------------------------------------
00409 class KDE_EXPORT NotifyDialog : public KDialogBase
00410 {
00411   Q_OBJECT
00412 public:
00413   static void display(ScorableArticle&,const QString&);
00414 protected slots:
00415   void slotShowAgainToggled(bool);
00416 private:
00417   NotifyDialog(QWidget* p =0);
00418   static NotifyDialog *me;
00419 
00420   QLabel *note;
00421   QString msg;
00422   typedef QMap<QString,bool> NotesMap;
00423   static NotesMap dict;
00424 };
00425 
00426 
00427 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys