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 <tqglobal.h> 00024 #include <tqptrlist.h> 00025 #include <tqptrstack.h> 00026 #include <tqregexp.h> 00027 00028 #include <tqobject.h> 00029 #include <tqstring.h> 00030 #include <tqstringlist.h> 00031 #include <tqdatetime.h> 00032 #include <tqcolor.h> 00033 #include <tqtable.h> 00034 #include <tqmap.h> 00035 #include <tqdict.h> 00036 00037 #include <kdialogbase.h> 00038 #include <klineedit.h> 00039 #include <knuminput.h> 00040 00041 #include <tdepimmacros.h> 00042 00043 class TQDomNode; 00044 class TQDomDocument; 00045 class TQDomElement; 00046 class TQTextStream; 00047 class TQLabel; 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 TQString&); 00071 virtual void changeColor(const TQColor&) {} 00072 virtual void markAsRead() {} 00073 virtual TQString from() const = 0; 00074 virtual TQString subject() const = 0; 00075 virtual TQString getHeaderByType(const TQString&) 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 TQString toString() const =0; 00089 virtual void apply(ScorableArticle&) const =0; 00090 virtual ActionBase* clone() const =0; 00091 virtual int getType() const =0; 00092 virtual TQString getValueString() const { return TQString(); } 00093 virtual void setValue(const TQString&) {} 00094 static ActionBase* factory(int type, const TQString &value); 00095 static TQStringList userNames(); 00096 static TQString userName(int type); 00097 static int getTypeForName(const TQString& name); 00098 static int getTypeForUserName(const TQString& name); 00099 TQString 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 TQColor&); 00106 ActionColor(const TQString&); 00107 ActionColor(const ActionColor&); 00108 virtual ~ActionColor(); 00109 virtual TQString toString() const; 00110 virtual int getType() const { return COLOR; } 00111 virtual TQString getValueString() const { return color.name(); } 00112 virtual void setValue(const TQString& s) { color.setNamedColor(s); } 00113 void setValue(const TQColor& c) { color = c; } 00114 TQColor value() const { return color; } 00115 virtual void apply(ScorableArticle&) const; 00116 virtual ActionColor* clone() const; 00117 private: 00118 TQColor color; 00119 }; 00120 00121 class KDE_EXPORT ActionSetScore : public ActionBase { 00122 public: 00123 ActionSetScore(short); 00124 ActionSetScore(const ActionSetScore&); 00125 ActionSetScore(const TQString&); 00126 virtual ~ActionSetScore(); 00127 virtual TQString toString() const; 00128 virtual int getType() const { return SETSCORE; } 00129 virtual TQString getValueString() const { return TQString::number(val); } 00130 virtual void setValue(const TQString& 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 TQString&); 00142 ActionNotify(const ActionNotify&); 00143 virtual ~ActionNotify() {} 00144 virtual TQString toString() const; 00145 virtual int getType() const { return NOTIFY; } 00146 virtual TQString getValueString() const { return note; } 00147 virtual void setValue(const TQString& s) { note = s; } 00148 virtual void apply(ScorableArticle&) const; 00149 virtual ActionNotify* clone() const; 00150 private: 00151 TQString note; 00152 }; 00153 00154 class KDE_EXPORT ActionMarkAsRead : public ActionBase { 00155 public: 00156 ActionMarkAsRead(); 00157 ActionMarkAsRead( const ActionMarkAsRead& ); 00158 virtual ~ActionMarkAsRead() {} 00159 virtual TQString 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 TQString&); 00171 TQString collection() const; 00172 void displayCollection(TQWidget *p=0) const; 00173 private: 00174 struct article_info { 00175 TQString from; 00176 TQString subject; 00177 }; 00178 typedef TQValueList<article_info> article_list; 00179 typedef TQDict<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 TQString&,const TQString&,const TQString&, const TQString&); 00192 ~KScoringExpression(); 00193 00194 bool match(ScorableArticle& a) const ; 00195 TQString getTypeString() const; 00196 static TQString getTypeString(int); 00197 int getType() const; 00198 TQString toString() const; 00199 void write(TQTextStream& ) const; 00200 00201 bool isNeg() const { return neg; } 00202 Condition getCondition() const { return cond; } 00203 TQString getExpression() const { return expr_str; } 00204 TQString getHeader() const { return header; } 00205 static TQStringList conditionNames(); 00206 static TQStringList headerNames(); 00207 static int getConditionForName(const TQString&); 00208 static TQString getNameForCondition(int); 00209 private: 00210 bool neg; 00211 TQString header; 00212 const char* c_header; 00213 Condition cond; 00214 TQRegExp expr; 00215 TQString expr_str; 00216 int expr_int; 00217 }; 00218 00219 //---------------------------------------------------------------------------- 00220 class KDE_EXPORT KScoringRule 00221 { 00222 friend class KScoringManager; 00223 public: 00224 KScoringRule(const TQString& name); 00225 KScoringRule(const KScoringRule& r); 00226 ~KScoringRule(); 00227 00228 typedef TQPtrList<KScoringExpression> ScoreExprList; 00229 typedef TQPtrList<ActionBase> ActionList; 00230 typedef TQStringList GroupList; 00231 enum LinkMode { AND, OR }; 00232 00233 TQString getName() const { return name; } 00234 TQStringList getGroups() const { return groups; } 00235 void setGroups(const TQStringList &l) { groups = l; } 00236 LinkMode getLinkMode() const { return link; } 00237 TQString getLinkModeName() const; 00238 TQString getExpireDateString() const; 00239 TQDate getExpireDate() const { return expires; } 00240 void setExpireDate(const TQDate &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 TQString& group) const ; 00248 void applyRule(ScorableArticle& a) const; 00249 void applyRule(ScorableArticle& a, const TQString& group) const; 00250 void applyAction(ScorableArticle& a) const; 00251 00252 void setLinkMode(const TQString& link); 00253 void setLinkMode(LinkMode m) { link = m; } 00254 void setExpire(const TQString& exp); 00255 void addExpression( KScoringExpression* ); 00256 void addGroup( const TQString& group) { groups.append(group); } 00257 //void addServer(const TQString& server) { servers.append(server); } 00258 void addAction(int, const TQString& ); 00259 void addAction(ActionBase*); 00260 00261 void updateXML(TQDomElement& e, TQDomDocument& d); 00262 TQString toString() const; 00263 00264 // writes the rule in XML format into the textstream 00265 void write(TQTextStream& ) const; 00266 protected: 00268 void setName(const TQString &n) { name = n; } 00269 private: 00270 TQString name; 00271 GroupList groups; 00272 //ServerList servers; 00273 LinkMode link; 00274 ScoreExprList expressions; 00275 ActionList actions; 00276 TQDate expires; 00277 }; 00278 00283 class KDE_EXPORT RuleStack 00284 { 00285 public: 00286 RuleStack(); 00287 ~RuleStack(); 00289 void push(TQPtrList<KScoringRule>&); 00292 void pop(TQPtrList<KScoringRule>&); 00294 void top(TQPtrList<KScoringRule>&); 00296 void drop(); 00297 private: 00298 TQPtrStack< TQPtrList<KScoringRule> > stack; 00299 }; 00300 00301 //---------------------------------------------------------------------------- 00302 // Manages the score rules. 00303 class KDE_EXPORT KScoringManager : public TQObject 00304 { 00305 Q_OBJECT 00306 00307 00308 public: 00309 //* this is the container for all rules 00310 typedef TQPtrList<KScoringRule> ScoringRuleList; 00311 00312 KScoringManager(const TQString& appName = TQString()); 00313 virtual ~KScoringManager(); 00314 00315 //* returns a list of all available groups, must be overridden 00316 virtual TQStringList getGroups() const =0; 00317 00320 virtual TQStringList getDefaultHeaders() const; 00321 00322 //* setting current server and group and calling applyRules(ScorableArticle&) 00323 void applyRules(ScorableArticle& article, const TQString& group/*, const TQString& server*/); 00324 //* assuming a properly set group 00325 void applyRules(ScorableArticle&); 00326 //* same as above 00327 void applyRules(ScorableGroup* group); 00328 00329 //* pushes the current rule list onto a stack 00330 void pushRuleList(); 00331 //* restores the current rule list from list stored on a stack 00332 //* by a previous call to pushRuleList (this implicitly deletes the 00333 //* current rule list) 00334 void popRuleList(); 00335 //* removes the TOS from the stack of rule lists 00336 void removeTOS(); 00337 00338 KScoringRule* addRule(KScoringRule *); 00339 KScoringRule* addRule(const ScorableArticle&, TQString group, short =0); 00340 KScoringRule* addRule(); 00341 void cancelNewRule(KScoringRule *); 00342 void deleteRule(KScoringRule *); 00343 void editRule(KScoringRule *e, TQWidget *w=0); 00344 KScoringRule* copyRule(KScoringRule *); 00345 void moveRuleAbove( KScoringRule *above, KScoringRule *below ); 00346 void moveRuleBelow( KScoringRule *below, KScoringRule *above ); 00347 void setGroup(const TQString& g); 00348 // has to be called after setGroup() or initCache() 00349 bool hasRulesForCurrentGroup(); 00350 TQString findUniqueName() const; 00351 00354 void editorReady(); 00355 00356 ScoringRuleList getAllRules() const { return allRules; } 00357 KScoringRule *findRule(const TQString&); 00358 TQStringList getRuleNames(); 00359 void setRuleName(KScoringRule *, const TQString&); 00360 int getRuleCount() const { return allRules.count(); } 00361 TQString toString() const; 00362 00363 bool setCacheValid(bool v); 00364 bool isCacheValid() { return cacheValid; } 00365 void initCache(const TQString& group/*, const TQString& server*/); 00366 00367 void load(); 00368 void save(); 00369 00370 //--------------- Properties 00371 virtual bool canScores() const { return true; } 00372 virtual bool canNotes() const { return true; } 00373 virtual bool canColors() const { return false; } 00374 virtual bool canMarkAsRead() const { return false; } 00375 virtual bool hasFeature(int); 00376 00377 signals: 00378 void changedRules(); 00379 void changedRuleName(const TQString& oldName, const TQString& newName); 00380 void finishedEditing(); 00381 00382 private: 00383 void addRuleInternal(KScoringRule *e); 00384 void expireRules(); 00385 00386 TQDomDocument createXMLfromInternal(); 00387 void createInternalFromXML(TQDomNode); 00388 00389 // list of all Rules 00390 ScoringRuleList allRules; 00391 00392 // the stack for temporary storing rule lists 00393 RuleStack stack; 00394 00395 // for the cache 00396 bool cacheValid; 00397 // current rule set, ie the cache 00398 ScoringRuleList ruleList; 00399 //TQString server; 00400 TQString group; 00401 00402 //ScorableServer* _s; 00403 00404 // filename of the scorefile 00405 TQString mFilename; 00406 }; 00407 00408 00409 //---------------------------------------------------------------------------- 00410 class KDE_EXPORT NotifyDialog : public KDialogBase 00411 { 00412 Q_OBJECT 00413 00414 public: 00415 static void display(ScorableArticle&,const TQString&); 00416 protected slots: 00417 void slotShowAgainToggled(bool); 00418 private: 00419 NotifyDialog(TQWidget* p =0); 00420 static NotifyDialog *me; 00421 00422 TQLabel *note; 00423 TQString msg; 00424 typedef TQMap<TQString,bool> NotesMap; 00425 static NotesMap dict; 00426 }; 00427 00428 00429 #endif