00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <kstandarddirs.h>
00016 #include <kconfig.h>
00017 #include <kconfiggroup.h>
00018 #include <klocale.h>
00019 #include <kdebug.h>
00020
00021 #include "kngroup.h"
00022 #include "knfolder.h"
00023 #include "utilities.h"
00024 #include "knarticlefilter.h"
00025
00026
00027
00028
00029
00030 static const char *defFil[] = { "all","unread","new","watched","threads with unread",
00031 "threads with new","own articles","threads with own articles", 0 };
00032 void dummyFilter()
00033 {
00034 i18nc("default filter name","all");
00035 i18nc("default filter name","unread");
00036 i18nc("default filter name","new");
00037 i18nc("default filter name","watched");
00038 i18nc("default filter name","threads with unread");
00039 i18nc("default filter name","threads with new");
00040 i18nc("default filter name","own articles");
00041 i18nc("default filter name","threads with own articles");
00042 }
00043
00044
00045
00046
00047
00048 KNArticleFilter::KNArticleFilter(int id)
00049 : i_d(id), c_ount(0), l_oaded(false), e_nabled(true), translateName(true), s_earchFilter(false), apon(articles)
00050 {}
00051
00052
00053
00054
00055 KNArticleFilter::KNArticleFilter(const KNArticleFilter& org)
00056 : i_d(-1), c_ount(0), l_oaded(false), e_nabled(org.e_nabled), translateName(true), s_earchFilter(org.s_earchFilter), apon(org.apon)
00057 {
00058 status = org.status;
00059 score = org.score;
00060 age = org.age;
00061 lines = org.lines;
00062 subject = org.subject;
00063 from = org.from;
00064 messageId = org.messageId;
00065 references = org.messageId;
00066 }
00067
00068
00069
00070 KNArticleFilter::~KNArticleFilter()
00071 {}
00072
00073
00074
00075 bool KNArticleFilter::loadInfo()
00076 {
00077 if (i_d!=-1) {
00078 QString fname(KStandardDirs::locate("data",QString( "knode/filters/%1.fltr" ).arg(i_d) ) );
00079
00080 if (fname.isNull())
00081 return false;
00082 KConfig conf( fname, KConfig::SimpleConfig);
00083
00084 KConfigGroup group = conf.group("GENERAL");
00085 n_ame=group.readEntry("name");
00086 translateName = group.readEntry("Translate_Name",true);
00087 e_nabled=group.readEntry("enabled", true);
00088 apon=(ApOn) group.readEntry("applyOn", 0);
00089 return true;
00090 }
00091 return false;
00092 }
00093
00094
00095
00096 void KNArticleFilter::load()
00097 {
00098 QString fname(KStandardDirs::locate("data",QString( "knode/filters/%1.fltr").arg(i_d) ) );
00099
00100 if (fname.isNull())
00101 return;
00102 KConfig conf( fname, KConfig::SimpleConfig);
00103
00104 KConfigGroup group = conf.group("STATUS");
00105 status.load(group);
00106
00107 group = conf.group("SCORE");
00108 score.load(group);
00109
00110 group = conf.group("AGE");
00111 age.load(group);
00112
00113 group = conf.group("LINES");
00114 lines.load(group);
00115
00116 group = conf.group("SUBJECT");
00117 subject.load(group);
00118
00119 group = conf.group("FROM");
00120 from.load(group);
00121
00122 group = conf.group("MESSAGEID");
00123 messageId.load(group);
00124
00125 group = conf.group("REFERENCES");
00126 references.load(group);
00127
00128 l_oaded=true;
00129
00130 kDebug(5003) <<"KNMessageFilter: filter loaded \"" << n_ame <<"\"";
00131
00132 }
00133
00134
00135
00136 void KNArticleFilter::save()
00137 {
00138 if (i_d==-1)
00139 return;
00140 QString dir(KStandardDirs::locateLocal("data","knode/")+"filters/");
00141 if (dir.isNull()) {
00142 KNHelper::displayInternalFileError();
00143 return;
00144 }
00145 KConfig conf(dir+QString("%1.fltr").arg(i_d), KConfig::SimpleConfig);
00146
00147 KConfigGroup group = conf.group("GENERAL");
00148 group.writeEntry("name", QString(n_ame));
00149 group.writeEntry("Translate_Name",translateName);
00150 group.writeEntry("enabled", e_nabled);
00151 group.writeEntry("applyOn", (int) apon);
00152
00153 group = conf.group("STATUS");
00154 status.save(group);
00155
00156 group = conf.group("SCORE");
00157 score.save(group);
00158
00159 group = conf.group("AGE");
00160 age.save(group);
00161
00162 group = conf.group("LINES");
00163 lines.save(group);
00164
00165 group = conf.group("SUBJECT");
00166 subject.save(group);
00167
00168 group = conf.group("FROM");
00169 from.save(group);
00170
00171 group = conf.group("MESSAGEID");
00172 messageId.save(group);
00173
00174 group = conf.group("REFERENCES");
00175 references.save(group);
00176
00177 kDebug(5003) <<"KNMessageFilter: filter saved \"" << n_ame <<"\"";
00178 }
00179
00180
00181
00182 void KNArticleFilter::doFilter(KNGroup *g)
00183 {
00184 c_ount=0;
00185 KNRemoteArticle *art=0, *ref=0;
00186 KNRemoteArticle::List orphant_threads;
00187 int idRef;
00188 int mergeCnt=0;
00189 bool inThread=false;
00190
00191 if(!l_oaded) load();
00192
00193 subject.expand(g);
00194 from.expand(g);
00195 messageId.expand(g);
00196 references.expand(g);
00197
00198 for(int idx=0; idx<g->length(); idx++) {
00199 art=g->at(idx);
00200 art->setFiltered(false);
00201 art->setVisibleFollowUps(false);
00202 art->setDisplayedReference(0);
00203 }
00204
00205 for(int idx=0; idx<g->length(); idx++) {
00206
00207 art=g->at(idx);
00208
00209 if(!art->isFiltered() && applyFilter(art) && apon==threads) {
00210 idRef=art->idRef();
00211 while(idRef!=0) {
00212 ref=g->byId(idRef);
00213 ref->setFilterResult(true);
00214 ref->setFiltered(true);
00215 if ( idRef==ref->idRef() ) break;
00216 idRef=ref->idRef();
00217 }
00218 }
00219
00220 }
00221
00222 for(int idx=0; idx<g->length(); idx++) {
00223
00224 art=g->at(idx);
00225
00226 if( apon==threads && !art->filterResult() ) {
00227 inThread=false;
00228 idRef=art->idRef();
00229 while(idRef!=0 && !inThread) {
00230 ref=g->byId(idRef);
00231 inThread=ref->filterResult();
00232 idRef=ref->idRef();
00233 }
00234 art->setFilterResult(inThread);
00235 }
00236
00237 if(art->filterResult()) {
00238 c_ount++;
00239
00240 ref = (art->idRef()>0) ? g->byId(art->idRef()) : 0;
00241 while(ref && !ref->filterResult())
00242 ref = (ref->idRef()>0) ? g->byId(ref->idRef()) : 0;
00243
00244 art->setDisplayedReference(ref);
00245 if(ref)
00246 ref->setVisibleFollowUps(true);
00247 else if(art->idRef()>0) {
00248 orphant_threads.append(art);
00249 }
00250 }
00251
00252 }
00253
00254 if( orphant_threads.count() > 0 ) {
00255
00256 KNRemoteArticle::List same_subjects;
00257 QString s;
00258 for ( KNRemoteArticle::List::Iterator it = orphant_threads.begin(); it != orphant_threads.end(); ++it ) {
00259 if ( (*it)->displayedReference() )
00260 continue;
00261
00262 s = (*it)->subject()->asUnicodeString();
00263 same_subjects.clear();
00264 for ( KNRemoteArticle::List::Iterator it2 = orphant_threads.begin(); it2 != orphant_threads.end(); ++it2 ) {
00265 if ( (*it2) != (*it) && (*it2)->subject()->asUnicodeString() == s )
00266 same_subjects.append( (*it2) );
00267 }
00268
00269 (*it)->setVisibleFollowUps( (*it)->hasVisibleFollowUps() || same_subjects.count() > 0 );
00270 for ( KNRemoteArticle::List::Iterator it2 = same_subjects.begin(); it2 != same_subjects.end(); ++it2 ) {
00271 (*it2)->setDisplayedReference( (*it) );
00272 mergeCnt++;
00273 }
00274 }
00275 }
00276
00277 kDebug(5003) <<"KNArticleFilter::doFilter() : matched" << c_ount
00278 << "articles , merged" << mergeCnt
00279 << "threads by subject";
00280
00281 }
00282
00283
00284 void KNArticleFilter::doFilter(KNFolder *f)
00285 {
00286 c_ount=0;
00287 KNLocalArticle *art=0;
00288
00289 if(!l_oaded) load();
00290
00291 subject.expand(0);
00292 from.expand(0);
00293 messageId.expand(0);
00294 references.expand(0);
00295
00296 for(int idx=0; idx<f->length(); idx++) {
00297 art=f->at(idx);
00298 if (applyFilter(art))
00299 c_ount++;
00300 }
00301 }
00302
00303
00304
00305 QString KNArticleFilter::translatedName()
00306 {
00307 if (translateName) {
00308
00309 if (!n_ame.isEmpty()) {
00310 if (i18nc("default filter name",n_ame.toLocal8Bit())!=n_ame.toLocal8Bit().data())
00311 return i18nc("default filter name",n_ame.toLocal8Bit());
00312 else
00313 return n_ame;
00314 } else
00315 return QString();
00316 } else
00317 return n_ame;
00318 }
00319
00320
00321
00322
00323 void KNArticleFilter::setTranslatedName(const QString &s)
00324 {
00325 bool retranslated = false;
00326 for (const char **c=defFil;(*c)!=0;c++)
00327 if (s==i18nc("default filter name",*c)) {
00328 n_ame = QString::fromLatin1(*c);
00329 retranslated = true;
00330 break;
00331 }
00332
00333 if (!retranslated) {
00334 n_ame = s;
00335 translateName = false;
00336 } else
00337 translateName = true;
00338 }
00339
00340
00341
00342 bool KNArticleFilter::applyFilter(KNRemoteArticle *a)
00343 {
00344 bool result=true;
00345
00346 if(result) result=status.doFilter(a);
00347 if(result) result=score.doFilter(a->score());
00348 if(result) result=lines.doFilter(a->lines()->numberOfLines());
00349 if(result) result=age.doFilter(a->date()->ageInDays());
00350 if(result) result=subject.doFilter(a->subject()->asUnicodeString());
00351 if(result) {
00352 QString tmp;
00353 if ( !a->from()->isEmpty() )
00354 tmp = a->from()->displayNames().first() + QLatin1String("##")
00355 + QString::fromLatin1( a->from()->addresses().first() );
00356 result=from.doFilter(tmp);
00357 }
00358 if(result) result=messageId.doFilter(a->messageID()->asUnicodeString());
00359 if(result) result=references.doFilter(a->references()->asUnicodeString());
00360
00361 a->setFilterResult(result);
00362 a->setFiltered(true);
00363
00364 return result;
00365 }
00366
00367
00368 bool KNArticleFilter::applyFilter(KNLocalArticle *a)
00369 {
00370 bool result=true;
00371
00372 if (isSearchFilter()) {
00373 if(result) result=lines.doFilter(a->lines()->numberOfLines());
00374 if(result) result=age.doFilter(a->date()->ageInDays());
00375 if(result) result=subject.doFilter(a->subject()->asUnicodeString());
00376 if(result) {
00377 QString tmp;
00378 if ( !a->from()->isEmpty() )
00379 tmp = a->from()->displayNames().first() + QLatin1String("##")
00380 + QString::fromLatin1( a->from()->addresses().first() );
00381 result=from.doFilter(tmp);
00382 }
00383 if(result) result=messageId.doFilter(a->messageID()->asUnicodeString());
00384 if(result) result=references.doFilter(a->references()->asUnicodeString());
00385 }
00386
00387 a->setFilterResult(result);
00388
00389 return result;
00390 }