summaryrefslogtreecommitdiffstats
path: root/knode/knstringfilter.cpp
blob: 4f69f7eaf7bfd2c9359d7ffd09845b0a614883e0 (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
/*
    KNode, the KDE newsreader
    Copyright (c) 1999-2005 the KNode authors.
    See file AUTHORS for details

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
*/

#include <tqcombobox.h>
#include <tqlayout.h>
#include <tqcheckbox.h>

#include <klocale.h>
#include <ksimpleconfig.h>
#include <klineedit.h>

#include "kngroup.h"
#include "knnntpaccount.h"
#include "knglobals.h"
#include "knconfigmanager.h"
#include "knstringfilter.h"


KNStringFilter& KNStringFilter::operator=(const KNStringFilter &sf)
{
  con=sf.con;
  data=sf.data.copy();
  regExp=sf.regExp;

  return (*this);
}



bool KNStringFilter::doFilter(const TQString &s)
{
  bool ret=true;

  if(!expanded.isEmpty()) {
    if(regExp) {
      TQRegExp matcher(expanded);
      ret = ( matcher.search(s) >= 0 );
    } else
      ret=(s.find(expanded,0,false)!=-1);

    if(!con) ret=!ret;
  }

  return ret;
}



// replace placeholders
void KNStringFilter::expand(KNGroup *g)
{
  KNConfig::Identity *id = (g) ? g->identity() : 0;

  if (!id) {
    id = (g) ? g->account()->identity() : 0;
    if (!id)
      id = knGlobals.configManager()->identity();
  }

  expanded = data;
  expanded.replace(TQRegExp("%MYNAME"), id->name());
  expanded.replace(TQRegExp("%MYEMAIL"), id->email());
}



void KNStringFilter::load(KSimpleConfig *conf)
{
  con=conf->readBoolEntry("contains", true);
  data=conf->readEntry("Data");
  regExp=conf->readBoolEntry("regX", false);
}



void KNStringFilter::save(KSimpleConfig *conf)
{
  conf->writeEntry("contains", con);
  conf->writeEntry("Data", data);
  conf->writeEntry("regX", regExp);
}


//===============================================================================

KNStringFilterWidget::KNStringFilterWidget(const TQString& title, TQWidget *parent)
  : TQGroupBox(title, parent)
{
  fType=new TQComboBox(this);
  fType->insertItem(i18n("Does Contain"));
  fType->insertItem(i18n("Does NOT Contain"));

  fString=new KLineEdit(this);

  regExp=new TQCheckBox(i18n("Regular expression"), this);

  TQGridLayout *topL=new TQGridLayout(this, 3,3, 8,5 );
  topL->addRowSpacing(0, fontMetrics().lineSpacing()-4);
  topL->addWidget(fType, 1,0);
  topL->addColSpacing(1, 10);
  topL->addWidget(regExp, 1,1);
  topL->addMultiCellWidget(fString, 2,2, 0,2);
  topL->setColStretch(2,1);
}



KNStringFilterWidget::~KNStringFilterWidget()
{
}



KNStringFilter KNStringFilterWidget::filter()
{
  KNStringFilter ret;
  ret.con=(fType->currentItem()==0);
  ret.data=fString->text();
  ret.regExp=regExp->isChecked();

  return ret;
}



void KNStringFilterWidget::setFilter(KNStringFilter &f)
{
  if(f.con) fType->setCurrentItem(0);
  else fType->setCurrentItem(1);
  fString->setText(f.data);
  regExp->setChecked(f.regExp);
}



void KNStringFilterWidget::clear()
{
  fString->clear();
  fType->setCurrentItem(0);
  regExp->setChecked(false);
}


void KNStringFilterWidget::setStartFocus()
{
  fString->setFocus();
}


// -----------------------------------------------------------------------------+

#include "knstringfilter.moc"

// kate: space-indent on; indent-width 2;