• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

knode

  • sources
  • kde-4.12
  • kdepim
  • knode
knarticlefilter.cpp
Go to the documentation of this file.
1 /*
2  KNode, the KDE newsreader
3  Copyright (c) 1999-2006 the KNode authors.
4  See file AUTHORS for details
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  You should have received a copy of the GNU General Public License
11  along with this program; if not, write to the Free Software Foundation,
12  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
13 */
14 
15 #include <kstandarddirs.h>
16 #include <kconfig.h>
17 #include <kconfiggroup.h>
18 #include <klocale.h>
19 #include <kdebug.h>
20 
21 #include "kngroup.h"
22 #include "knfolder.h"
23 #include "utilities.h"
24 #include "knarticlefilter.h"
25 
26 //=============================================================================================================
27 
28 
29 // the names of our default filters
30 static const char *defFil[] = { "all","unread","new","watched","threads with unread",
31  "threads with new","own articles","threads with own articles", 0 };
32 void dummyFilter()
33 {
34  i18nc("default filter name","all");
35  i18nc("default filter name","unread");
36  i18nc("default filter name","new");
37  i18nc("default filter name","watched");
38  i18nc("default filter name","threads with unread");
39  i18nc("default filter name","threads with new");
40  i18nc("default filter name","own articles");
41  i18nc("default filter name","threads with own articles");
42 }
43 
44 
45 //=============================================================================================================
46 
47 
48 KNArticleFilter::KNArticleFilter(int id)
49 : i_d(id), c_ount(0), l_oaded(false), e_nabled(true), translateName(true), s_earchFilter(false), apon(articles)
50 {}
51 
52 
53 
54 // constructs a copy of org
55 KNArticleFilter::KNArticleFilter(const KNArticleFilter& org)
56 : i_d(-1), c_ount(0), l_oaded(false), e_nabled(org.e_nabled), translateName(true), s_earchFilter(org.s_earchFilter), apon(org.apon)
57 {
58  status = org.status;
59  score = org.score;
60  age = org.age;
61  lines = org.lines;
62  subject = org.subject;
63  from = org.from;
64  messageId = org.messageId;
65  references = org.messageId;
66 }
67 
68 
69 
70 KNArticleFilter::~KNArticleFilter()
71 {}
72 
73 
74 
75 bool KNArticleFilter::loadInfo()
76 {
77  if (i_d!=-1) {
78  QString fname( KStandardDirs::locate( "data",
79  QString( "knode/filters/%1.fltr" ).arg( i_d ) ) );
80 
81  if (fname.isNull())
82  return false;
83  KConfig conf( fname, KConfig::SimpleConfig);
84 
85  KConfigGroup group = conf.group("GENERAL");
86  n_ame=group.readEntry("name");
87  translateName = group.readEntry("Translate_Name",true);
88  e_nabled=group.readEntry("enabled", true);
89  apon=(ApOn) group.readEntry("applyOn", 0);
90  return true;
91  }
92  return false;
93 }
94 
95 
96 
97 void KNArticleFilter::load()
98 {
99  QString fname(KStandardDirs::locate( "data", QString( "knode/filters/%1.fltr" ).arg( i_d ) ) );
100 
101  if (fname.isNull())
102  return;
103  KConfig conf( fname, KConfig::SimpleConfig);
104 
105  KConfigGroup group = conf.group("STATUS");
106  status.load(group);
107 
108  group = conf.group("SCORE");
109  score.load(group);
110 
111  group = conf.group("AGE");
112  age.load(group);
113 
114  group = conf.group("LINES");
115  lines.load(group);
116 
117  group = conf.group("SUBJECT");
118  subject.load(group);
119 
120  group = conf.group("FROM");
121  from.load(group);
122 
123  group = conf.group("MESSAGEID");
124  messageId.load(group);
125 
126  group = conf.group("REFERENCES");
127  references.load(group);
128 
129  l_oaded=true;
130 
131  kDebug(5003) <<"KNMessageFilter: filter loaded \"" << n_ame <<"\"";
132 
133 }
134 
135 
136 
137 void KNArticleFilter::save()
138 {
139  if (i_d==-1)
140  return;
141  QString dir( KStandardDirs::locateLocal( "data", "knode/filters/" ) );
142  if (dir.isNull()) {
143  KNHelper::displayInternalFileError();
144  return;
145  }
146  KConfig conf(dir+QString("%1.fltr").arg(i_d), KConfig::SimpleConfig);
147 
148  KConfigGroup group = conf.group("GENERAL");
149  group.writeEntry("name", QString(n_ame));
150  group.writeEntry("Translate_Name",translateName);
151  group.writeEntry("enabled", e_nabled);
152  group.writeEntry("applyOn", (int) apon);
153 
154  group = conf.group("STATUS");
155  status.save(group);
156 
157  group = conf.group("SCORE");
158  score.save(group);
159 
160  group = conf.group("AGE");
161  age.save(group);
162 
163  group = conf.group("LINES");
164  lines.save(group);
165 
166  group = conf.group("SUBJECT");
167  subject.save(group);
168 
169  group = conf.group("FROM");
170  from.save(group);
171 
172  group = conf.group("MESSAGEID");
173  messageId.save(group);
174 
175  group = conf.group("REFERENCES");
176  references.save(group);
177 
178  kDebug(5003) <<"KNMessageFilter: filter saved \"" << n_ame <<"\"";
179 }
180 
181 
182 
183 void KNArticleFilter::doFilter( KNGroup::Ptr g )
184 {
185  c_ount=0;
186  KNRemoteArticle::Ptr art, ref;
187  KNRemoteArticle::List orphant_threads;
188  int idRef;
189  int mergeCnt=0;
190  bool inThread=false;
191 
192  if(!l_oaded) load();
193 
194  subject.expand( g.get() ); // replace placeholders
195  from.expand( g.get() );
196  messageId.expand( g.get() );
197  references.expand( g.get() );
198 
199  for(int idx=0; idx<g->length(); idx++) {
200  art=g->at(idx);
201  art->setFiltered(false);
202  art->setVisibleFollowUps(false);
203  art->setDisplayedReference( KNRemoteArticle::Ptr() );
204  }
205 
206  for(int idx=0; idx<g->length(); idx++) {
207 
208  art=g->at(idx);
209 
210  if(!art->isFiltered() && applyFilter(art) && apon==threads) {
211  idRef=art->idRef();
212  while(idRef!=0) {
213  ref=g->byId(idRef);
214  ref->setFilterResult(true);
215  ref->setFiltered(true);
216  if ( idRef==ref->idRef() ) break;
217  idRef=ref->idRef();
218  }
219  }
220 
221  }
222 
223  for(int idx=0; idx<g->length(); idx++) {
224 
225  art=g->at(idx);
226 
227  if( apon==threads && !art->filterResult() ) {
228  inThread=false;
229  idRef=art->idRef();
230  while(idRef!=0 && !inThread) {
231  ref=g->byId(idRef);
232  inThread=ref->filterResult();
233  idRef=ref->idRef();
234  }
235  art->setFilterResult(inThread);
236  }
237 
238  if(art->filterResult()) {
239  c_ount++;
240 
241  ref = ( art->idRef() > 0 ) ? g->byId( art->idRef() ) : KNRemoteArticle::Ptr();
242  while(ref && !ref->filterResult())
243  ref = ( ref->idRef() > 0 ) ? g->byId( ref->idRef() ) : KNRemoteArticle::Ptr();
244 
245  art->setDisplayedReference(ref);
246  if(ref)
247  ref->setVisibleFollowUps(true);
248  else if(art->idRef()>0) {
249  orphant_threads.append(art);
250  }
251  }
252 
253  }
254 
255  if( orphant_threads.count() > 0 ) {
256  // try to merge orphant threads by subject
257  KNRemoteArticle::List same_subjects;
258  QString s;
259  for ( KNRemoteArticle::List::Iterator it = orphant_threads.begin(); it != orphant_threads.end(); ++it ) {
260  if ( (*it)->displayedReference() ) // already processed
261  continue;
262 
263  s = (*it)->subject()->asUnicodeString();
264  same_subjects.clear();
265  for ( KNRemoteArticle::List::Iterator it2 = orphant_threads.begin(); it2 != orphant_threads.end(); ++it2 ) {
266  if ( (*it2) != (*it) && (*it2)->subject()->asUnicodeString() == s )
267  same_subjects.append( (*it2) );
268  }
269 
270  (*it)->setVisibleFollowUps( (*it)->hasVisibleFollowUps() || same_subjects.count() > 0 );
271  for ( KNRemoteArticle::List::Iterator it2 = same_subjects.begin(); it2 != same_subjects.end(); ++it2 ) {
272  (*it2)->setDisplayedReference( (*it) );
273  mergeCnt++;
274  }
275  }
276  }
277 
278  kDebug(5003) <<"KNArticleFilter::doFilter() : matched" << c_ount
279  << "articles , merged" << mergeCnt
280  << "threads by subject";
281 
282 }
283 
284 
285 void KNArticleFilter::doFilter( KNFolder::Ptr f )
286 {
287  c_ount=0;
288  KNLocalArticle::Ptr art;
289 
290  if(!l_oaded) load();
291 
292  subject.expand(0); // replace placeholders
293  from.expand(0);
294  messageId.expand(0);
295  references.expand(0);
296 
297  for(int idx=0; idx<f->length(); idx++) {
298  art=f->at(idx);
299  if (applyFilter(art))
300  c_ount++;
301  }
302 }
303 
304 
305 // *tries* to translate the name
306 QString KNArticleFilter::translatedName()
307 {
308  if (translateName) {
309  // major hack alert !!!
310  if (!n_ame.isEmpty()) {
311  if (i18nc("default filter name",n_ame.toLocal8Bit())!=n_ame.toLocal8Bit().data()) // try to guess if this english or not
312  return i18nc("default filter name",n_ame.toLocal8Bit());
313  else
314  return n_ame;
315  } else
316  return QString();
317  } else
318  return n_ame;
319 }
320 
321 
322 
323 // *tries* to retranslate the name to english
324 void KNArticleFilter::setTranslatedName(const QString &s)
325 {
326  bool retranslated = false;
327  for (const char **c=defFil;(*c)!=0;c++) // ok, try if it matches any of the standard filter names
328  if (s==i18nc("default filter name",*c)) {
329  n_ame = QString::fromLatin1(*c);
330  retranslated = true;
331  break;
332  }
333 
334  if (!retranslated) { // ok, we give up and store the maybe non-english string
335  n_ame = s;
336  translateName = false; // and don't try to translate it, so a german user *can* use the original english name
337  } else
338  translateName = true;
339 }
340 
341 
342 
343 bool KNArticleFilter::applyFilter( KNRemoteArticle::Ptr a )
344 {
345  bool result=true;
346 
347  if(result) result=status.doFilter(a);
348  if(result) result=score.doFilter(a->score());
349  if(result) result=lines.doFilter(a->lines()->numberOfLines());
350  if(result) result=age.doFilter(a->date()->ageInDays());
351  if(result) result=subject.doFilter(a->subject()->asUnicodeString());
352  if(result) {
353  QString tmp;
354  if ( !a->from()->isEmpty() )
355  tmp = a->from()->displayNames().first() + QLatin1String("##")
356  + QString::fromLatin1( a->from()->addresses().first() );
357  result=from.doFilter(tmp);
358  }
359  if(result) result=messageId.doFilter(a->messageID()->asUnicodeString());
360  if(result) result=references.doFilter(a->references()->asUnicodeString());
361 
362  a->setFilterResult(result);
363  a->setFiltered(true);
364 
365  return result;
366 }
367 
368 
369 bool KNArticleFilter::applyFilter( KNLocalArticle::Ptr a )
370 {
371  bool result=true;
372 
373  if (isSearchFilter()) {
374  if(result) result=lines.doFilter(a->lines()->numberOfLines());
375  if(result) result=age.doFilter(a->date()->ageInDays());
376  if(result) result=subject.doFilter(a->subject()->asUnicodeString());
377  if(result) {
378  QString tmp;
379  if ( !a->from()->isEmpty() )
380  tmp = a->from()->displayNames().first() + QLatin1String("##")
381  + QString::fromLatin1( a->from()->addresses().first() );
382  result=from.doFilter(tmp);
383  }
384  if(result) result=messageId.doFilter(a->messageID()->asUnicodeString());
385  if(result) result=references.doFilter(a->references()->asUnicodeString());
386  }
387 
388  a->setFilterResult(result);
389 
390  return result;
391 }
KNArticleFilter::score
KNode::RangeFilter score
Definition: knarticlefilter.h:82
KNLocalArticle::Ptr
boost::shared_ptr< KNLocalArticle > Ptr
Shared pointer to a KNLocalArticle. To be used instead of raw KNLocalArticle*.
Definition: knarticle.h:214
defFil
static const char * defFil[]
Definition: knarticlefilter.cpp:30
KNArticleFilter::n_ame
QString n_ame
Definition: knarticlefilter.h:76
KNArticleFilter::subject
KNode::StringFilter subject
Definition: knarticlefilter.h:83
KNode::RangeFilter::load
void load(const KConfigGroup &conf)
Definition: knrangefilter.cpp:73
utilities.h
knfolder.h
KNArticleFilter::doFilter
void doFilter(KNGroup::Ptr g)
Definition: knarticlefilter.cpp:183
KNArticleFilter::ApOn
ApOn
Definition: knarticlefilter.h:72
KNArticleFilter::references
KNode::StringFilter references
Definition: knarticlefilter.h:83
dummyFilter
void dummyFilter()
Definition: knarticlefilter.cpp:32
KNArticleFilter::setTranslatedName
void setTranslatedName(const QString &s)
tries to retranslate the name to english
Definition: knarticlefilter.cpp:324
KNArticleFilter::threads
Definition: knarticlefilter.h:72
KNode::StatusFilter::save
void save(KConfigGroup &conf)
Definition: knstatusfilter.cpp:66
KNFolder::Ptr
boost::shared_ptr< KNFolder > Ptr
Shared pointer to a KNFolder.
Definition: knfolder.h:38
kngroup.h
KNode::RangeFilter::doFilter
bool doFilter(int a)
Definition: knrangefilter.cpp:28
KNArticleFilter::~KNArticleFilter
~KNArticleFilter()
Definition: knarticlefilter.cpp:70
KNArticleFilter::translatedName
QString translatedName()
tries to translate the name
Definition: knarticlefilter.cpp:306
KNode::StringFilter::load
void load(const KConfigGroup &group)
Definition: knstringfilter.cpp:86
KNArticleFilter::isSearchFilter
bool isSearchFilter() const
Definition: knarticlefilter.h:59
KNArticleFilter::i_d
int i_d
Definition: knarticlefilter.h:77
KNArticleFilter::translateName
bool translateName
Definition: knarticlefilter.h:78
KNArticleFilter::age
KNode::RangeFilter age
Definition: knarticlefilter.h:82
KNArticleFilter::c_ount
int c_ount
Definition: knarticlefilter.h:77
KNHelper::displayInternalFileError
static void displayInternalFileError(QWidget *w=0)
use this for all internal files
Definition: utilities.cpp:344
KNArticleFilter
Article filter.
Definition: knarticlefilter.h:33
KNode::StringFilter::expand
void expand(KNGroup *g)
replace placeholders
Definition: knstringfilter.cpp:65
KNode::RangeFilter::save
void save(KConfigGroup &conf)
Definition: knrangefilter.cpp:84
KNArticleFilter::save
void save()
Definition: knarticlefilter.cpp:137
KNArticleFilter::messageId
KNode::StringFilter messageId
Definition: knarticlefilter.h:83
KNode::StatusFilter::load
void load(const KConfigGroup &conf)
Definition: knstatusfilter.cpp:48
KNArticleFilter::load
void load()
Definition: knarticlefilter.cpp:97
KNode::StringFilter::doFilter
bool doFilter(const QString &s)
Definition: knstringfilter.cpp:45
KNArticleFilter::loadInfo
bool loadInfo()
Definition: knarticlefilter.cpp:75
KNArticleFilter::apon
ApOn apon
Definition: knarticlefilter.h:79
KNArticleFilter::KNArticleFilter
KNArticleFilter(int id=-1)
Definition: knarticlefilter.cpp:48
KNRemoteArticle::Ptr
boost::shared_ptr< KNRemoteArticle > Ptr
Shared pointer to a KNRemoteArticle. To be used instead of raw KNRemoteArticle*.
Definition: knarticle.h:107
KNArticleFilter::status
KNode::StatusFilter status
Definition: knarticlefilter.h:81
KNArticleFilter::e_nabled
bool e_nabled
Definition: knarticlefilter.h:78
KNArticleFilter::l_oaded
bool l_oaded
Definition: knarticlefilter.h:78
KNode::StringFilter::save
void save(KConfigGroup &conf)
Definition: knstringfilter.cpp:95
KNArticleFilter::from
KNode::StringFilter from
Definition: knarticlefilter.h:83
KNArticleFilter::lines
KNode::RangeFilter lines
Definition: knarticlefilter.h:82
KNode::StatusFilter::doFilter
bool doFilter(KNRemoteArticle::Ptr a)
Definition: knstatusfilter.cpp:83
QList
KNGroup::Ptr
boost::shared_ptr< KNGroup > Ptr
Shared pointer to a KNGroup.
Definition: kngroup.h:47
knarticlefilter.h
KNArticleFilter::applyFilter
bool applyFilter(KNRemoteArticle::Ptr a)
Definition: knarticlefilter.cpp:343
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:36 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

knode

Skip menu "knode"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal