6 #include "kmsearchpatternedit.h"
8 #include "kmsearchpattern.h"
9 #include "rulewidgethandlermanager.h"
12 #include <tdelocale.h>
16 #include <tqradiobutton.h>
17 #include <tqcombobox.h>
18 #include <tqbuttongroup.h>
19 #include <tqwidgetstack.h>
30 const char *internalName;
31 const char *displayName;
32 } SpecialRuleFields[] = {
33 {
"<message>", I18N_NOOP(
"Complete Message" ) },
34 {
"<body>", I18N_NOOP(
"Body of Message" ) },
35 {
"<any header>", I18N_NOOP(
"Anywhere in Headers" ) },
36 {
"<recipients>", I18N_NOOP(
"All Recipients" ) },
37 {
"<size>", I18N_NOOP(
"Size in Bytes" ) },
38 {
"<age in days>", I18N_NOOP(
"Age in Days" ) },
39 {
"<status>", I18N_NOOP(
"Message Status" ) },
40 {
"Subject", I18N_NOOP(
"Subject" ) },
41 {
"From", I18N_NOOP(
"From" ) },
42 {
"To", I18N_NOOP(
"To" ) },
43 {
"CC", I18N_NOOP(
"CC" ) },
44 {
"Reply-To", I18N_NOOP(
"Reply To" ) },
45 {
"Organization", I18N_NOOP(
"Organization" ) }
47 static const int SpecialRuleFieldsCount =
48 sizeof( SpecialRuleFields ) /
sizeof( *SpecialRuleFields );
57 const char *name,
bool headersOnly,
59 : TQWidget( parent, name ),
63 mAbsoluteDates( absoluteDates )
65 initFieldList( headersOnly, absoluteDates );
77 TQCString currentText = srule->
field();
79 initFieldList( headersOnly, mAbsoluteDates );
82 mRuleField->insertStringList( mFilterFieldList );
83 mRuleField->setSizeLimit( mRuleField->count() );
84 mRuleField->adjustSize();
86 if (( currentText !=
"<message>") &&
87 ( currentText !=
"<body>"))
88 mRuleField->changeItem( TQString(TQString::fromAscii( currentText )), 0 );
90 mRuleField->changeItem( TQString(), 0 );
93 void KMSearchRuleWidget::initWidget()
95 TQHBoxLayout * hlay =
new TQHBoxLayout(
this, 0, KDialog::spacingHint() );
98 mRuleField =
new TQComboBox(
true,
this,
"mRuleField" );
99 mRuleField->insertStringList( mFilterFieldList );
101 mRuleField->setSizeLimit( mRuleField->count() );
102 mRuleField->adjustSize();
103 hlay->addWidget( mRuleField );
106 mFunctionStack =
new TQWidgetStack(
this,
"mFunctionStack" );
108 mFunctionStack->setSizePolicy( TQSizePolicy::Preferred,TQSizePolicy::Fixed );
110 hlay->addWidget( mFunctionStack );
112 mValueStack =
new TQWidgetStack(
this,
"mValueStack" );
113 mValueStack->setSizePolicy( TQSizePolicy::Preferred,TQSizePolicy::Fixed );
114 hlay->addWidget( mValueStack );
115 hlay->setStretchFactor( mValueStack, 10 );
117 RuleWidgetHandlerManager::instance()->createWidgets( mFunctionStack,
122 setFocusProxy( mRuleField );
124 connect( mRuleField, TQ_SIGNAL( activated(
const TQString & ) ),
125 this, TQ_SLOT( slotRuleFieldChanged(
const TQString & ) ) );
126 connect( mRuleField, TQ_SIGNAL( textChanged(
const TQString & ) ),
127 this, TQ_SLOT( slotRuleFieldChanged(
const TQString & ) ) );
128 connect( mRuleField, TQ_SIGNAL( textChanged(
const TQString & ) ),
142 mRuleField->blockSignals(
true );
145 mRuleField->changeItem( TQString::fromLatin1( aRule->
field() ), 0 );
148 mRuleField->changeItem( TQString(), 0 );
151 mRuleField->setCurrentItem( i );
152 mRuleField->blockSignals(
false );
154 RuleWidgetHandlerManager::instance()->setRule( mFunctionStack, mValueStack,
161 RuleWidgetHandlerManager::instance()->function( ruleField,
163 const TQString value =
164 RuleWidgetHandlerManager::instance()->value( ruleField, mFunctionStack,
172 mRuleField->blockSignals(
true );
173 mRuleField->changeItem(
"", 0 );
174 mRuleField->setCurrentItem( 0 );
175 mRuleField->blockSignals(
false );
177 RuleWidgetHandlerManager::instance()->reset( mFunctionStack, mValueStack );
180 void KMSearchRuleWidget::slotFunctionChanged()
183 RuleWidgetHandlerManager::instance()->update( ruleField,
188 void KMSearchRuleWidget::slotValueChanged()
191 const TQString prettyValue =
192 RuleWidgetHandlerManager::instance()->prettyValue( ruleField,
200 for (
int i = 0; i < SpecialRuleFieldsCount; ++i ) {
201 if ( i18nVal == i18n( SpecialRuleFields[i].displayName ) )
202 return SpecialRuleFields[i].internalName;
204 return i18nVal.latin1();
207 int KMSearchRuleWidget::ruleFieldToId(
const TQString & i18nVal )
209 for (
int i = 0; i < SpecialRuleFieldsCount; ++i ) {
210 if ( i18nVal == i18n( SpecialRuleFields[i].displayName ) )
216 static TQString displayNameFromInternalName(
const TQString &
internal )
218 for (
int i = 0; i < SpecialRuleFieldsCount; ++i ) {
219 if (
internal == SpecialRuleFields[i].internalName )
220 return i18n(SpecialRuleFields[i].displayName);
222 return internal.latin1();
229 if ( aName.isEmpty() )
232 TQString i18n_aName = displayNameFromInternalName( aName );
234 for (
int i = 1; i < mRuleField->count(); ++i ) {
235 if ( mRuleField->text( i ) == i18n_aName )
242 void KMSearchRuleWidget::initFieldList(
bool headersOnly,
bool absoluteDates )
244 mFilterFieldList.clear();
245 mFilterFieldList.append(
"");
247 mFilterFieldList.append( i18n( SpecialRuleFields[Message].displayName ) );
248 mFilterFieldList.append( i18n( SpecialRuleFields[Body].displayName ) );
250 mFilterFieldList.append( i18n( SpecialRuleFields[AnyHeader].displayName ) );
251 mFilterFieldList.append( i18n( SpecialRuleFields[Recipients].displayName ) );
252 mFilterFieldList.append( i18n( SpecialRuleFields[Size].displayName ) );
253 if ( !absoluteDates )
254 mFilterFieldList.append( i18n( SpecialRuleFields[AgeInDays].displayName ) );
255 mFilterFieldList.append( i18n( SpecialRuleFields[Subject].displayName ) );
256 mFilterFieldList.append( i18n( SpecialRuleFields[From].displayName ) );
257 mFilterFieldList.append( i18n( SpecialRuleFields[To].displayName ) );
258 mFilterFieldList.append( i18n( SpecialRuleFields[CC].displayName ) );
259 mFilterFieldList.append( i18n( SpecialRuleFields[ReplyTo].displayName ) );
260 mFilterFieldList.append( i18n( SpecialRuleFields[Organization].displayName ) );
264 mFilterFieldList.append(
"List-Id");
265 mFilterFieldList.append(
"Resent-From");
266 mFilterFieldList.append(
"X-Loop");
267 mFilterFieldList.append(
"X-Mailing-List");
268 mFilterFieldList.append(
"X-Spam-Flag");
271 void KMSearchRuleWidget::slotRuleFieldChanged(
const TQString & field )
284 KMSearchRuleWidgetLister::KMSearchRuleWidgetLister( TQWidget *parent,
const char* name,
bool headersOnly,
bool absoluteDates )
285 : KWidgetLister( 2, FILTER_MAX_RULES, parent, name )
288 mHeadersOnly = headersOnly;
289 mAbsoluteDates = absoluteDates;
292 KMSearchRuleWidgetLister::~KMSearchRuleWidgetLister()
296 void KMSearchRuleWidgetLister::setRuleList( TQPtrList<KMSearchRule> *aList )
300 if ( mRuleList && mRuleList != aList )
301 regenerateRuleListFromWidgets();
305 if ( mWidgetList.first() )
306 mWidgetList.first()->blockSignals(
true);
308 if ( aList->count() == 0 ) {
310 mWidgetList.first()->blockSignals(
false);
314 int superfluousItems = (int)mRuleList->count() - mMaxWidgets ;
315 if ( superfluousItems > 0 ) {
316 kdDebug(5006) <<
"KMSearchRuleWidgetLister: Clipping rule list to "
317 << mMaxWidgets <<
" items!" << endl;
319 for ( ; superfluousItems ; superfluousItems-- )
320 mRuleList->removeLast();
324 setNumberOfShownWidgetsTo( TQMAX((
int)mRuleList->count(),mMinWidgets)+1 );
326 setNumberOfShownWidgetsTo( TQMAX((
int)mRuleList->count(),mMinWidgets) );
329 TQPtrListIterator<KMSearchRule> rIt( *mRuleList );
330 TQPtrListIterator<TQWidget> wIt( mWidgetList );
331 for ( rIt.toFirst(), wIt.toFirst() ;
332 rIt.current() && wIt.current() ; ++rIt, ++wIt ) {
335 for ( ; wIt.current() ; ++wIt )
338 assert( mWidgetList.first() );
339 mWidgetList.first()->blockSignals(
false);
342 void KMSearchRuleWidgetLister::setHeadersOnly(
bool headersOnly )
344 TQPtrListIterator<TQWidget> wIt( mWidgetList );
345 for ( wIt.toFirst() ; wIt.current() ; ++wIt ) {
350 void KMSearchRuleWidgetLister::reset()
353 regenerateRuleListFromWidgets();
359 TQWidget* KMSearchRuleWidgetLister::createWidget( TQWidget *parent )
364 void KMSearchRuleWidgetLister::clearWidget( TQWidget *aWidget )
370 void KMSearchRuleWidgetLister::regenerateRuleListFromWidgets()
372 if ( !mRuleList )
return;
376 TQPtrListIterator<TQWidget> it( mWidgetList );
377 for ( it.toFirst() ; it.current() ; ++it ) {
380 mRuleList->append( r );
394 : TQGroupBox( 1, TQt::Horizontal, parent, name )
396 setTitle( i18n(
"Search Criteria") );
397 initLayout( headersOnly, absoluteDates );
401 : TQGroupBox( 1, TQt::Horizontal, title, parent, name )
403 initLayout( headersOnly, absoluteDates );
406 KMSearchPatternEdit::~KMSearchPatternEdit()
410 void KMSearchPatternEdit::initLayout(
bool headersOnly,
bool absoluteDates)
413 mAllRBtn =
new TQRadioButton( i18n(
"Match a&ll of the following"),
this,
"mAllRBtn" );
414 mAnyRBtn =
new TQRadioButton( i18n(
"Match an&y of the following"),
this,
"mAnyRBtn" );
416 mAllRBtn->setChecked(
true);
417 mAnyRBtn->setChecked(
false);
419 TQButtonGroup *bg =
new TQButtonGroup(
this );
421 bg->insert( mAllRBtn, (
int)KMSearchPattern::OpAnd );
422 bg->insert( mAnyRBtn, (
int)KMSearchPattern::OpOr );
425 mRuleLister =
new KMSearchRuleWidgetLister(
this,
"swl", headersOnly, absoluteDates );
426 mRuleLister->slotClear();
429 connect( bg, TQ_SIGNAL(clicked(
int)),
430 this, TQ_SLOT(slotRadioClicked(
int)) );
434 connect( srw, TQ_SIGNAL(fieldChanged(
const TQString &)),
435 this, TQ_SLOT(slotAutoNameHack()) );
436 connect( srw, TQ_SIGNAL(contentsChanged(
const TQString &)),
437 this, TQ_SLOT(slotAutoNameHack()) );
439 kdDebug(5006) <<
"KMSearchPatternEdit: no first KMSearchRuleWidget, though slotClear() has been called!" << endl;
446 mRuleLister->setRuleList( aPattern );
451 if ( mPattern->
op() == KMSearchPattern::OpOr )
452 mAnyRBtn->setChecked(
true);
454 mAllRBtn->setChecked(
true);
462 mRuleLister->setHeadersOnly( headersOnly );
467 mRuleLister->reset();
470 mAllRBtn->setChecked(
true );
476 void KMSearchPatternEdit::slotRadioClicked(
int aIdx)
482 void KMSearchPatternEdit::slotAutoNameHack()
484 mRuleLister->regenerateRuleListFromWidgets();
488 #include "kmsearchpatternedit.moc"
void maybeNameChanged()
This signal is emitted whenever the name of the processed search pattern may have changed.
KMSearchPatternEdit(TQWidget *parent=0, const char *name=0, bool headersOnly=false, bool absoluteDates=false)
Constructor.
void setHeadersOnly(bool headersOnly)
Set whether only header fields can be searched.
void reset()
Called when the widget should let go of the currently referenced filter and disable itself.
void setSearchPattern(KMSearchPattern *aPattern)
Set the search pattern.
This class is an abstraction of a search over messages.
KMSearchPattern::Operator op() const
Get the filter operator.
void setOp(KMSearchPattern::Operator aOp)
Set the filter operator.
Operator
Boolean operators that connect the return values of the individual rules.
Incoming mail is sent through the list of mail filter rules before it is placed in the associated mai...
static KMSearchRule * createInstance(const TQCString &field=0, Function function=FuncContains, const TQString &contents=TQString())
Create a search rule of a certain type by instantiating the appro- priate subclass depending on the f...
TQCString field() const
Return message header field name (without the trailing ':').
Function
Operators for comparison of field and contents.