summaryrefslogtreecommitdiffstats
path: root/akregator/src/articlefilter.cpp
blob: a8bf8a36502fd6d4661f04a7e12937d50454f75c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/*
 * articlefilter.cpp
 *
 * Copyright (c) 2004, 2005 Frerich Raabe <raabe@kde.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "articlefilter.h"
#include "article.h"
#include "shared.h"

#include <kapplication.h>
#include <kconfig.h>
#include <kdebug.h>
#include <kurl.h>

#include <tqregexp.h>

namespace Akregator {
namespace Filters {

TQString Criterion::subjectToString(Subject subj)
{
    switch (subj)
    {
        case Title:
            return TQString::fromLatin1("Title");
        case Link:
            return TQString::fromLatin1("Link");
        case Author:
            return TQString::fromLatin1("Author");
        case Description:
            return TQString::fromLatin1("Description");
        case Status:
            return TQString::fromLatin1("Status");
        case KeepFlag:
            return TQString::fromLatin1("KeepFlag");
        default: // should never happen (TM)
            return TQString::fromLatin1("Description");
    }
}

Criterion::Subject Criterion::stringToSubject(const TQString& subjStr)
{
    if (subjStr == TQString::fromLatin1("Title"))
        return Title;
    else if (subjStr == TQString::fromLatin1("Link"))
        return Link;
    else if (subjStr == TQString::fromLatin1("Description"))
        return Description;
    else if (subjStr == TQString::fromLatin1("Author"))
        return Author;
    else if (subjStr == TQString::fromLatin1("Status"))
        return Status;
    else if (subjStr == TQString::fromLatin1("KeepFlag"))
        return KeepFlag;

    // hopefully never reached
    return Description;
}

TQString Criterion::predicateToString(Predicate pred)
{
    switch (pred)
    {
        case Contains:
            return TQString::fromLatin1("Contains");
        case Equals:
            return TQString::fromLatin1("Equals");
        case Matches:
            return TQString::fromLatin1("Matches");
        case Negation:
            return TQString::fromLatin1("Negation");
        default:// hopefully never reached
            return TQString::fromLatin1("Contains");
    }
}

Criterion::Predicate Criterion::stringToPredicate(const TQString& predStr)
{
    if (predStr == TQString::fromLatin1("Contains"))
        return Contains;
    else if (predStr == TQString::fromLatin1("Equals"))
        return Equals;
    else if (predStr == TQString::fromLatin1("Matches"))
        return Matches;
    else if (predStr == TQString::fromLatin1("Negation"))
        return Negation;
    
    // hopefully never reached
    return Contains;
}

Criterion::Criterion()
{
}

Criterion::Criterion( Subject subject, Predicate predicate, const TQVariant &object )
    : m_subject( subject )
    , m_predicate( predicate )
    , m_object( object )
{

}

void Criterion::writeConfig(KConfig* config) const
{
    config->writeEntry(TQString::fromLatin1("subject"), subjectToString(m_subject));

    config->writeEntry(TQString::fromLatin1("predicate"), predicateToString(m_predicate));

    config->writeEntry(TQString::fromLatin1("objectType"), TQString(m_object.typeName()));

    config->writeEntry(TQString::fromLatin1("objectValue"), m_object);
}

void Criterion::readConfig(KConfig* config)
{
    m_subject = stringToSubject(config->readEntry(TQString::fromLatin1("subject")));
    m_predicate = stringToPredicate(config->readEntry(TQString::fromLatin1("predicate")));
    TQVariant::Type type = TQVariant::nameToType(config->readEntry(TQString::fromLatin1("objType")).ascii());

    if (type != TQVariant::Invalid)
    {
        m_object = config->readPropertyEntry(TQString::fromLatin1("objectValue"), type);
    }
}

bool Criterion::satisfiedBy( const Article &article ) const
{
    TQVariant concreteSubject;

    switch ( m_subject ) {
        case Title:
            concreteSubject = TQVariant(article.title());
            break;
        case Description:
            concreteSubject = TQVariant(article.description());
            break;
        case Author:
            concreteSubject = TQVariant(article.author());
            break;
        case Link:
            // ### Maybe use prettyURL here?
            concreteSubject = TQVariant(article.link().url());
            break;
        case Status:
            concreteSubject = TQVariant(article.status());
            break;
        case KeepFlag:
            concreteSubject = TQVariant(article.keep());   
        default:
            break;
    }

    bool satisfied = false;

    const Predicate predicateType = static_cast<Predicate>( m_predicate & ~Negation );
	TQString subjectType=concreteSubject.typeName();

    switch ( predicateType ) {
        case Contains:
            satisfied = concreteSubject.toString().find( m_object.toString(), 0, false ) != -1;
            break;
        case Equals:
            if (subjectType=="int")
                satisfied = concreteSubject.toInt() == m_object.toInt();
            else
                satisfied = concreteSubject.toString() == m_object.toString();
            break;
        case Matches:
            satisfied = TQRegExp( m_object.toString() ).search( concreteSubject.toString() ) != -1;
            break;
        default:
            kdDebug() << "Internal inconsistency; predicateType should never be Negation" << endl;
            break;
    }

    if ( m_predicate & Negation ) {
        satisfied = !satisfied;
    }

    return satisfied;
}

Criterion::Subject Criterion::subject() const
{
    return m_subject;
}

Criterion::Predicate Criterion::predicate() const
{
    return m_predicate;
}

TQVariant Criterion::object() const
{
    return m_object;
}

ArticleMatcher::ArticleMatcher()
    : m_association( None )
{
}

ArticleMatcher::~ArticleMatcher()
{
}

bool ArticleMatcher::matchesAll() const
{
    return m_criteria.isEmpty();
}

ArticleMatcher* ArticleMatcher::clone() const
{
    return new ArticleMatcher(*this);
}

ArticleMatcher::ArticleMatcher( const TQValueList<Criterion> &criteria, Association assoc)
    : m_criteria( criteria )
    , m_association( assoc )
{
}

ArticleMatcher& ArticleMatcher::operator=(const ArticleMatcher& other)
{
    m_association = other.m_association;
    m_criteria = other.m_criteria;
    return *this;
}

ArticleMatcher::ArticleMatcher(const ArticleMatcher& other) : AbstractMatcher(other)
{
    *this = other;
}

bool ArticleMatcher::matches( const Article &a ) const
{
    switch ( m_association ) {
        case LogicalOr:
            return anyCriterionMatches( a );
        case LogicalAnd:
            return allCriteriaMatch( a );
        default:
            break;
    }
    return true;
}

void ArticleMatcher::writeConfig(KConfig* config) const
{
    config->writeEntry(TQString::fromLatin1("matcherAssociation"), associationToString(m_association));
    
    config->writeEntry(TQString::fromLatin1("matcherCriteriaCount"), m_criteria.count());

    int index = 0;

    for (TQValueList<Criterion>::ConstIterator it = m_criteria.begin(); it != m_criteria.end(); ++it)
    {
        config->setGroup(config->group()+TQString::fromLatin1("_Criterion")+TQString::number(index));
        (*it).writeConfig(config);
        ++index;
    }
}

void ArticleMatcher::readConfig(KConfig* config)
{
    m_criteria.clear();
    m_association = stringToAssociation(config->readEntry(TQString::fromLatin1("matcherAssociation")));

    int count =  config->readNumEntry(TQString::fromLatin1("matcherCriteriaCount"), 0);
    
    for (int i = 0; i < count; ++i)
    {
        Criterion c;
        config->setGroup(config->group()+TQString::fromLatin1("_Criterion")+TQString::number(i));
        c.readConfig(config);
        m_criteria.append(c);
    }
}

bool ArticleMatcher::operator==(const AbstractMatcher& other) const
{
    AbstractMatcher* ptr = const_cast<AbstractMatcher*>(&other);
    ArticleMatcher* o = dynamic_cast<ArticleMatcher*>(ptr);
    if (!o)
        return false;
    else
        return m_association == o->m_association && m_criteria == o->m_criteria;
}
bool ArticleMatcher::operator!=(const AbstractMatcher& other) const
{
    return !(*this == other);
}

bool ArticleMatcher::anyCriterionMatches( const Article &a ) const
{
    if (m_criteria.count()==0)
        return true;
    TQValueList<Criterion>::ConstIterator it = m_criteria.begin();
    TQValueList<Criterion>::ConstIterator end = m_criteria.end();
    for ( ; it != end; ++it ) {
        if ( ( *it ).satisfiedBy( a ) ) {
            return true;
        }
    }
    return false;
}

bool ArticleMatcher::allCriteriaMatch( const Article &a ) const
{
    if (m_criteria.count()==0)
        return true;
    TQValueList<Criterion>::ConstIterator it = m_criteria.begin();
    TQValueList<Criterion>::ConstIterator end = m_criteria.end();
    for ( ; it != end; ++it ) {
        if ( !( *it ).satisfiedBy( a ) ) {
            return false;
        }
    }
    return true;
}

ArticleMatcher::Association ArticleMatcher::stringToAssociation(const TQString& assocStr)
{
    if (assocStr == TQString::fromLatin1("LogicalAnd"))
        return LogicalAnd;
    else if (assocStr == TQString::fromLatin1("LogicalOr"))
        return LogicalOr;
    else
        return None;
}

TQString ArticleMatcher::associationToString(Association association)
{
    switch (association)
    {
        case LogicalAnd:
            return TQString::fromLatin1("LogicalAnd");
        case LogicalOr:
            return TQString::fromLatin1("LogicalOr");
        default:
            return TQString::fromLatin1("None");
    }
}


class TagMatcher::TagMatcherPrivate
{
    public:
    TQString tagID;
    bool operator==(const TagMatcherPrivate& other) const
    {
        return tagID == other.tagID;
    }
};

TagMatcher::TagMatcher(const TQString& tagID) : d(new TagMatcherPrivate)
{
    d->tagID = tagID;
}

TagMatcher::TagMatcher() : d(new TagMatcherPrivate)
{
}

TagMatcher::~TagMatcher()
{
    delete d;
    d = 0;
}

bool TagMatcher::matches(const Article& article) const
{
    return article.hasTag(d->tagID);
}

TagMatcher* TagMatcher::clone() const
{
    return new TagMatcher(*this);
}


TagMatcher::TagMatcher(const TagMatcher& other) : AbstractMatcher(other), d(0)
{
    *this = other;
}

void TagMatcher::writeConfig(KConfig* config) const
{
    config->writeEntry(TQString::fromLatin1("matcherType"), TQString::fromLatin1("TagMatcher"));
    config->writeEntry(TQString::fromLatin1("matcherParams"), d->tagID);
}

void TagMatcher::readConfig(KConfig* config)
{
    d->tagID = config->readEntry(TQString::fromLatin1("matcherParams"));
}

bool TagMatcher::operator==(const AbstractMatcher& other) const
{
    AbstractMatcher* ptr = const_cast<AbstractMatcher*>(&other);
    TagMatcher* tagFilter = dynamic_cast<TagMatcher*>(ptr);
    return tagFilter ? *d == *(tagFilter->d) : false;
}

bool TagMatcher::operator!=(const AbstractMatcher &other) const
{
    return !(*this == other);
}

TagMatcher& TagMatcher::operator=(const TagMatcher& other)
{
    d = new TagMatcherPrivate;
    *d = *(other.d);
    return *this;
}

void DeleteAction::exec(Article& article)
{
    if (!article.isNull())
        article.setDeleted();
}

SetStatusAction::SetStatusAction(int status) : m_status(status)
{
}
        
void SetStatusAction::exec(Article& article)
{
    if (!article.isNull())
        article.setStatus(m_status);
}

int SetStatusAction::status() const
{
    return m_status;
}

void SetStatusAction::setStatus(int status)
{
    m_status = status;
}

void SetStatusAction::writeConfig(KConfig* config) const
{
    config->writeEntry(TQString::fromLatin1("actionType"), TQString::fromLatin1("SetStatusAction"));
    config->writeEntry(TQString::fromLatin1("actionParams"), m_status);
}

void SetStatusAction::readConfig(KConfig* config)
{
    m_status = config->readNumEntry(TQString::fromLatin1("actionParams"), Article::Read);
}

bool SetStatusAction::operator==(const AbstractAction& other)
{
    AbstractAction* ptr = const_cast<AbstractAction*>(&other);
    SetStatusAction* o = dynamic_cast<SetStatusAction*>(ptr);
    if (!o)
        return false;
    else
        return m_status == o->m_status;
}

 
AssignTagAction::AssignTagAction(const TQString& tagID) : m_tagID(tagID)
{
}

void AssignTagAction::exec(Article& article)
{
    if (!article.isNull())
        article.addTag(m_tagID);
}

class ArticleFilter::ArticleFilterPrivate : public Shared
{
    public:
    AbstractAction* action;
    AbstractMatcher* matcher;
    TQString name;
    int id;
    
};

ArticleFilter::ArticleFilter() : d(new ArticleFilterPrivate)
{
    d->id = KApplication::random();
    d->action = 0;
    d->matcher = 0;
}

ArticleFilter::ArticleFilter(const AbstractMatcher& matcher, const AbstractAction& action) : d(new ArticleFilterPrivate)
{
    d->id = KApplication::random();
    d->matcher = matcher.clone();
    d->action = action.clone();
}

ArticleFilter::ArticleFilter(const ArticleFilter& other)
{
    *this = other;
}

ArticleFilter::~ArticleFilter()
{
    if (d->deref())
    {
        delete d->action;
        delete d->matcher;
        delete d;
        d = 0;
    }
    
}

AbstractMatcher* ArticleFilter::matcher() const
{
    return d->matcher;
}

AbstractAction* ArticleFilter::action() const
{
    return d->action;
}

void ArticleFilter::setMatcher(const AbstractMatcher& matcher)
{
    delete d->matcher;
    d->matcher = matcher.clone();
}

void ArticleFilter::setAction(const AbstractAction& action)
{
    delete d->action;
    d->action = action.clone();
}

ArticleFilter& ArticleFilter::operator=(const ArticleFilter& other)
{
    if (this != &other)
    {
        other.d->ref();
        if (d && d->deref())
            delete d;
        d = other.d;
    }
    return *this;
}

int ArticleFilter::id() const 
{
    return d->id;
}

bool ArticleFilter::operator==(const ArticleFilter& other) const
{
    return *(d->matcher) == *(other.d->matcher) && *(d->action) == *(other.d->action) && d->name == other.d->name;
}

void ArticleFilterList::writeConfig(KConfig* config) const
{
    config->setGroup(TQString::fromLatin1("Filters"));
    config->writeEntry(TQString::fromLatin1("count"), count());
    int index = 0;
    for (ArticleFilterList::ConstIterator it = begin(); it != end(); ++it)
    {
        config->setGroup(TQString::fromLatin1("Filters_")+TQString::number(index));
        (*it).writeConfig(config);
        ++index;
    }
}

void ArticleFilterList::readConfig(KConfig* config)
{
    clear();
    config->setGroup(TQString::fromLatin1("Filters"));
    int count = config->readNumEntry(TQString::fromLatin1("count"), 0);
    for (int i = 0; i < count; ++i)
    {
        config->setGroup(TQString::fromLatin1("Filters_")+TQString::number(i));
        ArticleFilter filter;
        filter.readConfig(config);
        append(filter);
    }
}


void AssignTagAction::readConfig(KConfig* config)
{
    m_tagID = config->readEntry(TQString::fromLatin1("actionParams"));
}

void AssignTagAction::writeConfig(KConfig* config) const
{
    config->writeEntry(TQString::fromLatin1("actionType"), TQString::fromLatin1("AssignTagAction"));
    config->writeEntry(TQString::fromLatin1("actionParams"), m_tagID);
}

bool AssignTagAction::operator==(const AbstractAction& other)
{
    AbstractAction* ptr = const_cast<AbstractAction*>(&other);
    AssignTagAction* o = dynamic_cast<AssignTagAction*>(ptr);
    if (!o)
        return false;
    else
        return m_tagID == o->m_tagID;
}

const TQString& AssignTagAction::tagID() const
{
    return m_tagID;
}

void AssignTagAction::setTagID(const TQString& tagID)
{
    m_tagID = tagID;
}

void DeleteAction::readConfig(KConfig* /*config*/)
{
}

void DeleteAction::writeConfig(KConfig* config) const
{
    config->writeEntry(TQString::fromLatin1("actionType"), TQString::fromLatin1("DeleteAction"));
}

bool DeleteAction::operator==(const AbstractAction& other)
{
    AbstractAction* ptr = const_cast<AbstractAction*>(&other);
    DeleteAction* o = dynamic_cast<DeleteAction*>(ptr);
    return o != 0;
}

void ArticleFilter::readConfig(KConfig* config)
{
    delete d->matcher;
    d->matcher = 0;
    delete d->action;
    d->action = 0;

    d->name = config->readEntry(TQString::fromLatin1("name"));
    d->id = config->readNumEntry(TQString::fromLatin1("id"), 0);

    TQString matcherType = config->readEntry(TQString::fromLatin1("matcherType"));

    if (matcherType == TQString::fromLatin1("TagMatcher"))
        d->matcher = new TagMatcher();
    else if (matcherType == TQString::fromLatin1("ArticleMatcher"))
        d->matcher = new ArticleMatcher();

    if (d->matcher)
        d->matcher->readConfig(config);


        TQString actionType = config->readEntry(TQString::fromLatin1("actionType"));

    if (actionType == TQString::fromLatin1("AssignTagAction"))
        d->action = new AssignTagAction();
    else if (actionType == TQString::fromLatin1("DeleteAction"))
        d->action = new DeleteAction();
    else if (actionType == TQString::fromLatin1("SetStatusAction"))
        d->action = new SetStatusAction();

    if (d->action)
        d->action->readConfig(config);
}

void ArticleFilter::writeConfig(KConfig* config) const
{
    config->writeEntry(TQString::fromLatin1("name"), d->name);
    config->writeEntry(TQString::fromLatin1("id"), d->id);
    d->matcher->writeConfig(config);
    d->action->writeConfig(config);
}

void ArticleFilter::setName(const TQString& name)
{
    d->name = name;
}

const TQString& ArticleFilter::name() const
{
    return d->name;
}

void ArticleFilter::applyTo(Article& article) const
{
    if (d->matcher && d->action && d->matcher->matches(article))
        d->action->exec(article);
}
} //namespace Filters
} //namespace Akregator