kmail Library API Documentation

rulewidgethandlermanager.cpp

00001 /* -*- mode: C++; c-file-style: "gnu" -*- 00002 rulewidgethandlermanager.cpp 00003 00004 This file is part of KMail, the KDE mail client. 00005 Copyright (c) 2004 Ingo Kloecker <kloecker@kde.org> 00006 00007 KMail is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 2 of the License, or 00010 (at your option) any later version. 00011 00012 KMail is distributed in the hope that it will be useful, but 00013 WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program; if not, write to the Free Software 00019 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 00021 In addition, as a special exception, the copyright holders give 00022 permission to link the code of this program with any edition of 00023 the Qt library by Trolltech AS, Norway (or with modified versions 00024 of Qt that use the same license as Qt), and distribute linked 00025 combinations including the two. You must obey the GNU General 00026 Public License in all respects for all of the code used other than 00027 Qt. If you modify this file, you may extend this exception to 00028 your version of the file, but you are not obligated to do so. If 00029 you do not wish to do so, delete this exception statement from 00030 your version. 00031 */ 00032 00033 #ifdef HAVE_CONFIG_H 00034 #include <config.h> 00035 #endif 00036 00037 #include "rulewidgethandlermanager.h" 00038 00039 #include "interfaces/rulewidgethandler.h" 00040 00041 #include <kdebug.h> 00042 00043 #include <qwidgetstack.h> 00044 #include <qstring.h> 00045 #include <qcstring.h> 00046 #include <qobject.h> 00047 #include <qobjectlist.h> 00048 00049 #include <assert.h> 00050 00051 #include <algorithm> 00052 using std::for_each; 00053 using std::remove; 00054 00055 KMail::RuleWidgetHandlerManager * KMail::RuleWidgetHandlerManager::self = 0; 00056 00057 namespace { 00058 class TextRuleWidgetHandler : public KMail::RuleWidgetHandler { 00059 public: 00060 TextRuleWidgetHandler() : KMail::RuleWidgetHandler() {} 00061 ~TextRuleWidgetHandler() {} 00062 00063 QWidget * createFunctionWidget( int number, 00064 QWidgetStack *functionStack, 00065 const QObject *receiver ) const; 00066 QWidget * createValueWidget( int number, 00067 QWidgetStack *valueStack, 00068 const QObject *receiver ) const; 00069 KMSearchRule::Function function( const QCString & field, 00070 const QWidgetStack *functionStack ) const; 00071 QString value( const QCString & field, 00072 const QWidgetStack *functionStack, 00073 const QWidgetStack *valueStack ) const; 00074 QString prettyValue( const QCString & field, 00075 const QWidgetStack *functionStack, 00076 const QWidgetStack *valueStack ) const; 00077 bool handlesField( const QCString & field ) const; 00078 void reset( QWidgetStack *functionStack, 00079 QWidgetStack *valueStack ) const; 00080 bool setRule( QWidgetStack *functionStack, 00081 QWidgetStack *valueStack, 00082 const KMSearchRule *rule ) const; 00083 bool update( const QCString & field, 00084 QWidgetStack *functionStack, 00085 QWidgetStack *valueStack ) const; 00086 00087 private: 00088 KMSearchRule::Function currentFunction( const QWidgetStack *functionStack ) const; 00089 QString currentValue( const QWidgetStack *valueStack, 00090 KMSearchRule::Function func ) const; 00091 }; 00092 00093 class MessageRuleWidgetHandler : public KMail::RuleWidgetHandler { 00094 public: 00095 MessageRuleWidgetHandler() : KMail::RuleWidgetHandler() {} 00096 ~MessageRuleWidgetHandler() {} 00097 00098 QWidget * createFunctionWidget( int number, 00099 QWidgetStack *functionStack, 00100 const QObject *receiver ) const; 00101 QWidget * createValueWidget( int number, 00102 QWidgetStack *valueStack, 00103 const QObject *receiver ) const; 00104 KMSearchRule::Function function( const QCString & field, 00105 const QWidgetStack *functionStack ) const; 00106 QString value( const QCString & field, 00107 const QWidgetStack *functionStack, 00108 const QWidgetStack *valueStack ) const; 00109 QString prettyValue( const QCString & field, 00110 const QWidgetStack *functionStack, 00111 const QWidgetStack *valueStack ) const; 00112 bool handlesField( const QCString & field ) const; 00113 void reset( QWidgetStack *functionStack, 00114 QWidgetStack *valueStack ) const; 00115 bool setRule( QWidgetStack *functionStack, 00116 QWidgetStack *valueStack, 00117 const KMSearchRule *rule ) const; 00118 bool update( const QCString & field, 00119 QWidgetStack *functionStack, 00120 QWidgetStack *valueStack ) const; 00121 00122 private: 00123 KMSearchRule::Function currentFunction( const QWidgetStack *functionStack ) const; 00124 QString currentValue( const QWidgetStack *valueStack, 00125 KMSearchRule::Function func ) const; 00126 }; 00127 00128 00129 class StatusRuleWidgetHandler : public KMail::RuleWidgetHandler { 00130 public: 00131 StatusRuleWidgetHandler() : KMail::RuleWidgetHandler() {} 00132 ~StatusRuleWidgetHandler() {} 00133 00134 QWidget * createFunctionWidget( int number, 00135 QWidgetStack *functionStack, 00136 const QObject *receiver ) const; 00137 QWidget * createValueWidget( int number, 00138 QWidgetStack *valueStack, 00139 const QObject *receiver ) const; 00140 KMSearchRule::Function function( const QCString & field, 00141 const QWidgetStack *functionStack ) const; 00142 QString value( const QCString & field, 00143 const QWidgetStack *functionStack, 00144 const QWidgetStack *valueStack ) const; 00145 QString prettyValue( const QCString & field, 00146 const QWidgetStack *functionStack, 00147 const QWidgetStack *valueStack ) const; 00148 bool handlesField( const QCString & field ) const; 00149 void reset( QWidgetStack *functionStack, 00150 QWidgetStack *valueStack ) const; 00151 bool setRule( QWidgetStack *functionStack, 00152 QWidgetStack *valueStack, 00153 const KMSearchRule *rule ) const; 00154 bool update( const QCString & field, 00155 QWidgetStack *functionStack, 00156 QWidgetStack *valueStack ) const; 00157 00158 private: 00159 KMSearchRule::Function currentFunction( const QWidgetStack *functionStack ) const; 00160 int currentStatusValue( const QWidgetStack *valueStack ) const; 00161 }; 00162 00163 class NumericRuleWidgetHandler : public KMail::RuleWidgetHandler { 00164 public: 00165 NumericRuleWidgetHandler() : KMail::RuleWidgetHandler() {} 00166 ~NumericRuleWidgetHandler() {} 00167 00168 QWidget * createFunctionWidget( int number, 00169 QWidgetStack *functionStack, 00170 const QObject *receiver ) const; 00171 QWidget * createValueWidget( int number, 00172 QWidgetStack *valueStack, 00173 const QObject *receiver ) const; 00174 KMSearchRule::Function function( const QCString & field, 00175 const QWidgetStack *functionStack ) const; 00176 QString value( const QCString & field, 00177 const QWidgetStack *functionStack, 00178 const QWidgetStack *valueStack ) const; 00179 QString prettyValue( const QCString & field, 00180 const QWidgetStack *functionStack, 00181 const QWidgetStack *valueStack ) const; 00182 bool handlesField( const QCString & field ) const; 00183 void reset( QWidgetStack *functionStack, 00184 QWidgetStack *valueStack ) const; 00185 bool setRule( QWidgetStack *functionStack, 00186 QWidgetStack *valueStack, 00187 const KMSearchRule *rule ) const; 00188 bool update( const QCString & field, 00189 QWidgetStack *functionStack, 00190 QWidgetStack *valueStack ) const; 00191 00192 private: 00193 KMSearchRule::Function currentFunction( const QWidgetStack *functionStack ) const; 00194 QString currentValue( const QWidgetStack *valueStack ) const; 00195 }; 00196 } 00197 00198 KMail::RuleWidgetHandlerManager::RuleWidgetHandlerManager() 00199 { 00200 registerHandler( new NumericRuleWidgetHandler() ); 00201 registerHandler( new StatusRuleWidgetHandler() ); 00202 registerHandler( new MessageRuleWidgetHandler() ); 00203 // the TextRuleWidgetHandler is the fallback handler, so it has to be added 00204 // as last handler 00205 registerHandler( new TextRuleWidgetHandler() ); 00206 } 00207 00208 namespace { 00209 template <typename T> struct Delete { 00210 void operator()( const T * x ) { delete x; x = 0; } 00211 }; 00212 } 00213 00214 KMail::RuleWidgetHandlerManager::~RuleWidgetHandlerManager() 00215 { 00216 for_each( mHandlers.begin(), mHandlers.end(), Delete<RuleWidgetHandler>() ); 00217 } 00218 00219 void KMail::RuleWidgetHandlerManager::registerHandler( const RuleWidgetHandler * handler ) 00220 { 00221 if ( !handler ) 00222 return; 00223 unregisterHandler( handler ); // don't produce duplicates 00224 mHandlers.push_back( handler ); 00225 } 00226 00227 void KMail::RuleWidgetHandlerManager::unregisterHandler( const RuleWidgetHandler * handler ) 00228 { 00229 // don't delete them, only remove them from the list! 00230 mHandlers.erase( remove( mHandlers.begin(), mHandlers.end(), handler ), mHandlers.end() ); 00231 } 00232 00233 namespace { 00237 int childCount( const QObject *parent, const char *objName ) 00238 { 00239 QObjectList *list = parent->queryList( 0, objName, false, false ); 00240 if ( !list ) 00241 return 0; 00242 const int count = list->count(); 00243 delete list; list = 0; 00244 return count; 00245 } 00246 } 00247 00248 void KMail::RuleWidgetHandlerManager::createWidgets( QWidgetStack *functionStack, 00249 QWidgetStack *valueStack, 00250 const QObject *receiver ) const 00251 { 00252 for ( const_iterator it = mHandlers.begin(); it != mHandlers.end(); ++it ) { 00253 QWidget *w = 0; 00254 for ( int i = 0; 00255 ( w = (*it)->createFunctionWidget( i, functionStack, receiver ) ); 00256 ++i ) { 00257 if ( childCount( functionStack, w->name() ) < 2 ) { 00258 // there wasn't already a widget with this name, so add this widget 00259 functionStack->addWidget( w ); 00260 } 00261 else { 00262 // there was already a widget with this name, so discard this widget 00263 kdDebug(5006) << "RuleWidgetHandlerManager::createWidgets: " 00264 << w->name() << " already exists in functionStack" 00265 << endl; 00266 delete w; w = 0; 00267 } 00268 } 00269 for ( int i = 0; 00270 ( w = (*it)->createValueWidget( i, valueStack, receiver ) ); 00271 ++i ) { 00272 if ( childCount( valueStack, w->name() ) < 2 ) { 00273 // there wasn't already a widget with this name, so add this widget 00274 valueStack->addWidget( w ); 00275 } 00276 else { 00277 // there was already a widget with this name, so discard this widget 00278 kdDebug(5006) << "RuleWidgetHandlerManager::createWidgets: " 00279 << w->name() << " already exists in valueStack" 00280 << endl; 00281 delete w; w = 0; 00282 } 00283 } 00284 } 00285 } 00286 00287 KMSearchRule::Function KMail::RuleWidgetHandlerManager::function( const QCString& field, 00288 const QWidgetStack *functionStack ) const 00289 { 00290 for ( const_iterator it = mHandlers.begin(); it != mHandlers.end(); ++it ) { 00291 const KMSearchRule::Function func = (*it)->function( field, 00292 functionStack ); 00293 if ( func != KMSearchRule::FuncNone ) 00294 return func; 00295 } 00296 return KMSearchRule::FuncNone; 00297 } 00298 00299 QString KMail::RuleWidgetHandlerManager::value( const QCString& field, 00300 const QWidgetStack *functionStack, 00301 const QWidgetStack *valueStack ) const 00302 { 00303 for ( const_iterator it = mHandlers.begin(); it != mHandlers.end(); ++it ) { 00304 const QString val = (*it)->value( field, functionStack, valueStack ); 00305 if ( !val.isEmpty() ) 00306 return val; 00307 } 00308 return QString::null; 00309 } 00310 00311 QString KMail::RuleWidgetHandlerManager::prettyValue( const QCString& field, 00312 const QWidgetStack *functionStack, 00313 const QWidgetStack *valueStack ) const 00314 { 00315 for ( const_iterator it = mHandlers.begin(); it != mHandlers.end(); ++it ) { 00316 const QString val = (*it)->prettyValue( field, functionStack, valueStack ); 00317 if ( !val.isEmpty() ) 00318 return val; 00319 } 00320 return QString::null; 00321 } 00322 00323 void KMail::RuleWidgetHandlerManager::reset( QWidgetStack *functionStack, 00324 QWidgetStack *valueStack ) const 00325 { 00326 for ( const_iterator it = mHandlers.begin(); it != mHandlers.end(); ++it ) { 00327 (*it)->reset( functionStack, valueStack ); 00328 } 00329 update( "", functionStack, valueStack ); 00330 } 00331 00332 void KMail::RuleWidgetHandlerManager::setRule( QWidgetStack *functionStack, 00333 QWidgetStack *valueStack, 00334 const KMSearchRule *rule ) const 00335 { 00336 assert( rule ); 00337 reset( functionStack, valueStack ); 00338 for ( const_iterator it = mHandlers.begin(); it != mHandlers.end(); ++it ) { 00339 if ( (*it)->setRule( functionStack, valueStack, rule ) ) 00340 return; 00341 } 00342 } 00343 00344 void KMail::RuleWidgetHandlerManager::update( const QCString &field, 00345 QWidgetStack *functionStack, 00346 QWidgetStack *valueStack ) const 00347 { 00348 //kdDebug(5006) << "RuleWidgetHandlerManager::update( \"" << field 00349 // << "\", ... )" << endl; 00350 for ( const_iterator it = mHandlers.begin(); it != mHandlers.end(); ++it ) { 00351 if ( (*it)->update( field, functionStack, valueStack ) ) 00352 return; 00353 } 00354 } 00355 00356 //----------------------------------------------------------------------------- 00357 00358 namespace { 00359 // FIXME (Qt >= 4.0): 00360 // This is a simplified and constified copy of QObject::child(). According 00361 // to a comment in qobject.h QObject::child() will be made const in Qt 4.0. 00362 // So once we require Qt 4.0 this can be removed. 00363 QObject* QObject_child_const( const QObject *parent, 00364 const char *objName ) 00365 { 00366 const QObjectList *list = parent->children(); 00367 if ( !list ) 00368 return 0; 00369 00370 QObjectListIterator it( *list ); 00371 QObject *obj; 00372 while ( ( obj = it.current() ) ) { 00373 ++it; 00374 if ( !objName || qstrcmp( objName, obj->name() ) == 0 ) 00375 break; 00376 } 00377 return obj; 00378 } 00379 } 00380 00381 //----------------------------------------------------------------------------- 00382 00383 // these includes are temporary and should not be needed for the code 00384 // above this line, so they appear only here: 00385 #include "kmaddrbook.h" 00386 #include "kmsearchpattern.h" 00387 #include "regexplineedit.h" 00388 using KMail::RegExpLineEdit; 00389 00390 #include <klocale.h> 00391 #include <knuminput.h> 00392 00393 #include <qcombobox.h> 00394 #include <qlabel.h> 00395 00396 //============================================================================= 00397 // 00398 // class TextRuleWidgetHandler 00399 // 00400 //============================================================================= 00401 00402 namespace { 00403 // also see KMSearchRule::matches() and KMSearchRule::Function 00404 // if you change the following strings! 00405 static const struct { 00406 const KMSearchRule::Function id; 00407 const char *displayName; 00408 } TextFunctions[] = { 00409 { KMSearchRule::FuncContains, I18N_NOOP( "contains" ) }, 00410 { KMSearchRule::FuncContainsNot, I18N_NOOP( "does not contain" ) }, 00411 { KMSearchRule::FuncEquals, I18N_NOOP( "equals" ) }, 00412 { KMSearchRule::FuncNotEqual, I18N_NOOP( "does not equal" ) }, 00413 { KMSearchRule::FuncRegExp, I18N_NOOP( "matches regular expr." ) }, 00414 { KMSearchRule::FuncNotRegExp, I18N_NOOP( "does not match reg. expr." ) }, 00415 { KMSearchRule::FuncIsInAddressbook, I18N_NOOP( "is in address book" ) }, 00416 { KMSearchRule::FuncIsNotInAddressbook, I18N_NOOP( "is not in address book" ) }, 00417 { KMSearchRule::FuncIsInCategory, I18N_NOOP( "is in category" ) }, 00418 { KMSearchRule::FuncIsNotInCategory, I18N_NOOP( "is not in category" ) } 00419 }; 00420 static const int TextFunctionCount = 00421 sizeof( TextFunctions ) / sizeof( *TextFunctions ); 00422 00423 //--------------------------------------------------------------------------- 00424 00425 QWidget * TextRuleWidgetHandler::createFunctionWidget( int number, 00426 QWidgetStack *functionStack, 00427 const QObject *receiver ) const 00428 { 00429 if ( number != 0 ) 00430 return 0; 00431 00432 QComboBox *funcCombo = new QComboBox( functionStack, "textRuleFuncCombo" ); 00433 for ( int i = 0; i < TextFunctionCount; ++i ) { 00434 funcCombo->insertItem( i18n( TextFunctions[i].displayName ) ); 00435 } 00436 funcCombo->adjustSize(); 00437 QObject::connect( funcCombo, SIGNAL( activated( int ) ), 00438 receiver, SLOT( slotFunctionChanged() ) ); 00439 return funcCombo; 00440 } 00441 00442 //--------------------------------------------------------------------------- 00443 00444 QWidget * TextRuleWidgetHandler::createValueWidget( int number, 00445 QWidgetStack *valueStack, 00446 const QObject *receiver ) const 00447 { 00448 if ( number == 0 ) { 00449 RegExpLineEdit *lineEdit = 00450 new RegExpLineEdit( valueStack, "regExpLineEdit" ); 00451 QObject::connect( lineEdit, SIGNAL( textChanged( const QString & ) ), 00452 receiver, SLOT( slotValueChanged() ) ); 00453 return lineEdit; 00454 } 00455 00456 // blank QLabel to hide value widget for in-address-book rule 00457 if ( number == 1 ) { 00458 return new QLabel( valueStack, "textRuleValueHider" ); 00459 } 00460 00461 if ( number == 2 ) { 00462 QComboBox *combo = new QComboBox( valueStack, "categoryCombo" ); 00463 QStringList categories = KabcBridge::categories(); 00464 combo->insertStringList( categories ); 00465 QObject::connect( combo, SIGNAL( activated( int ) ), 00466 receiver, SLOT( slotValueChanged() ) ); 00467 return combo; 00468 } 00469 00470 return 0; 00471 } 00472 00473 //--------------------------------------------------------------------------- 00474 00475 KMSearchRule::Function TextRuleWidgetHandler::currentFunction( const QWidgetStack *functionStack ) const 00476 { 00477 const QComboBox *funcCombo = 00478 dynamic_cast<QComboBox*>( QObject_child_const( functionStack, 00479 "textRuleFuncCombo" ) ); 00480 // FIXME (Qt >= 4.0): Use the following when QObject::child() is const. 00481 // dynamic_cast<QComboBox*>( functionStack->child( "textRuleFuncCombo", 00482 // 0, false ) ); 00483 if ( funcCombo ) { 00484 return TextFunctions[funcCombo->currentItem()].id; 00485 } 00486 else 00487 kdDebug(5006) << "TextRuleWidgetHandler::currentFunction: " 00488 "textRuleFuncCombo not found." << endl; 00489 return KMSearchRule::FuncNone; 00490 } 00491 00492 //--------------------------------------------------------------------------- 00493 00494 KMSearchRule::Function TextRuleWidgetHandler::function( const QCString &, 00495 const QWidgetStack *functionStack ) const 00496 { 00497 return currentFunction( functionStack ); 00498 } 00499 00500 //--------------------------------------------------------------------------- 00501 00502 QString TextRuleWidgetHandler::currentValue( const QWidgetStack *valueStack, 00503 KMSearchRule::Function func ) const 00504 { 00505 // here we gotta check the combobox which contains the categories 00506 if ( func == KMSearchRule::FuncIsInCategory || 00507 func == KMSearchRule::FuncIsNotInCategory ) { 00508 const QComboBox *combo= 00509 dynamic_cast<QComboBox*>( QObject_child_const( valueStack, 00510 "categoryCombo" ) ); 00511 // FIXME (Qt >= 4.0): Use the following when QObject::child() is const. 00512 // dynamic_cast<QComboBox*>( valueStack->child( "categoryCombo", 00513 // 0, false ) ); 00514 if ( combo ) { 00515 return combo->currentText(); 00516 } 00517 else { 00518 kdDebug(5006) << "TextRuleWidgetHandler::currentValue: " 00519 "categoryCombo not found." << endl; 00520 return QString::null; 00521 } 00522 } 00523 00524 //in other cases of func it is a lineedit 00525 const RegExpLineEdit *lineEdit = 00526 dynamic_cast<RegExpLineEdit*>( QObject_child_const( valueStack, 00527 "regExpLineEdit" ) ); 00528 // FIXME (Qt >= 4.0): Use the following when QObject::child() is const. 00529 // dynamic_cast<RegExpLineEdit*>( valueStack->child( "regExpLineEdit", 00530 // 0, false ) ); 00531 if ( lineEdit ) { 00532 return lineEdit->text(); 00533 } 00534 else 00535 kdDebug(5006) << "TextRuleWidgetHandler::currentValue: " 00536 "regExpLineEdit not found." << endl; 00537 00538 // or anything else, like addressbook 00539 return QString::null; 00540 } 00541 00542 //--------------------------------------------------------------------------- 00543 00544 QString TextRuleWidgetHandler::value( const QCString &, 00545 const QWidgetStack *functionStack, 00546 const QWidgetStack *valueStack ) const 00547 { 00548 KMSearchRule::Function func = currentFunction( functionStack ); 00549 if ( func == KMSearchRule::FuncIsInAddressbook ) 00550 return "is in address book"; // just a non-empty dummy value 00551 else if ( func == KMSearchRule::FuncIsNotInAddressbook ) 00552 return "is not in address book"; // just a non-empty dummy value 00553 else 00554 return currentValue( valueStack, func ); 00555 } 00556 00557 //--------------------------------------------------------------------------- 00558 00559 QString TextRuleWidgetHandler::prettyValue( const QCString &, 00560 const QWidgetStack *functionStack, 00561 const QWidgetStack *valueStack ) const 00562 { 00563 KMSearchRule::Function func = currentFunction( functionStack ); 00564 if ( func == KMSearchRule::FuncIsInAddressbook ) 00565 return i18n( "is in address book" ); 00566 else if ( func == KMSearchRule::FuncIsNotInAddressbook ) 00567 return i18n( "is not in address book" ); 00568 else 00569 return currentValue( valueStack, func ); 00570 } 00571 00572 //--------------------------------------------------------------------------- 00573 00574 bool TextRuleWidgetHandler::handlesField( const QCString & ) const 00575 { 00576 return true; // we handle all fields (as fallback) 00577 } 00578 00579 //--------------------------------------------------------------------------- 00580 00581 void TextRuleWidgetHandler::reset( QWidgetStack *functionStack, 00582 QWidgetStack *valueStack ) const 00583 { 00584 // reset the function combo box 00585 QComboBox *funcCombo = 00586 dynamic_cast<QComboBox*>( functionStack->child( "textRuleFuncCombo", 00587 0, false ) ); 00588 if ( funcCombo ) { 00589 funcCombo->blockSignals( true ); 00590 funcCombo->setCurrentItem( 0 ); 00591 funcCombo->blockSignals( false ); 00592 } 00593 00594 // reset the value widget 00595 RegExpLineEdit *lineEdit = 00596 dynamic_cast<RegExpLineEdit*>( valueStack->child( "regExpLineEdit", 00597 0, false ) ); 00598 if ( lineEdit ) { 00599 lineEdit->blockSignals( true ); 00600 lineEdit->clear(); 00601 lineEdit->blockSignals( false ); 00602 lineEdit->showEditButton( false ); 00603 valueStack->raiseWidget( lineEdit ); 00604 } 00605 00606 QComboBox *combo = 00607 dynamic_cast<QComboBox*>( valueStack->child( "categoryCombo", 00608 0, false ) ); 00609 if (combo) { 00610 combo->blockSignals( true ); 00611 combo->setCurrentItem( 0 ); 00612 combo->blockSignals( false ); 00613 } 00614 } 00615 00616 //--------------------------------------------------------------------------- 00617 00618 bool TextRuleWidgetHandler::setRule( QWidgetStack *functionStack, 00619 QWidgetStack *valueStack, 00620 const KMSearchRule *rule ) const 00621 { 00622 if ( !rule ) { 00623 reset( functionStack, valueStack ); 00624 return false; 00625 } 00626 00627 const KMSearchRule::Function func = rule->function(); 00628 int i = 0; 00629 for ( ; i < TextFunctionCount; ++i ) 00630 if ( func == TextFunctions[i].id ) 00631 break; 00632 QComboBox *funcCombo = 00633 dynamic_cast<QComboBox*>( functionStack->child( "textRuleFuncCombo", 00634 0, false ) ); 00635 if ( funcCombo ) { 00636 funcCombo->blockSignals( true ); 00637 if ( i < TextFunctionCount ) 00638 funcCombo->setCurrentItem( i ); 00639 else { 00640 kdDebug(5006) << "TextRuleWidgetHandler::setRule( " 00641 << rule->asString() 00642 << " ): unhandled function" << endl; 00643 funcCombo->setCurrentItem( 0 ); 00644 } 00645 funcCombo->blockSignals( false ); 00646 functionStack->raiseWidget( funcCombo ); 00647 } 00648 00649 if ( func == KMSearchRule::FuncIsInAddressbook || 00650 func == KMSearchRule::FuncIsNotInAddressbook ) { 00651 QWidget *w = 00652 static_cast<QWidget*>( valueStack->child( "textRuleValueHider", 00653 0, false ) ); 00654 valueStack->raiseWidget( w ); 00655 } 00656 else if ( func == KMSearchRule::FuncIsInCategory || 00657 func == KMSearchRule::FuncIsNotInCategory) { 00658 QComboBox *combo = 00659 static_cast<QComboBox*>( valueStack->child( "categoryCombo", 00660 0, false ) ); 00661 combo->blockSignals( true ); 00662 for ( i = 0; i < combo->count(); ++i ) 00663 if ( rule->contents() == combo->text( i ) ) { 00664 combo->setCurrentItem( i ); 00665 break; 00666 } 00667 if ( i == combo->count() ) 00668 combo->setCurrentItem( 0 ); 00669 00670 combo->blockSignals( false ); 00671 valueStack->raiseWidget( combo ); 00672 } 00673 else { 00674 RegExpLineEdit *lineEdit = 00675 dynamic_cast<RegExpLineEdit*>( valueStack->child( "regExpLineEdit", 00676 0, false ) ); 00677 if ( lineEdit ) { 00678 lineEdit->blockSignals( true ); 00679 lineEdit->setText( rule->contents() ); 00680 lineEdit->blockSignals( false ); 00681 lineEdit->showEditButton( func == KMSearchRule::FuncRegExp || 00682 func == KMSearchRule::FuncNotRegExp ); 00683 valueStack->raiseWidget( lineEdit ); 00684 } 00685 } 00686 return true; 00687 } 00688 00689 00690 //--------------------------------------------------------------------------- 00691 00692 bool TextRuleWidgetHandler::update( const QCString &, 00693 QWidgetStack *functionStack, 00694 QWidgetStack *valueStack ) const 00695 { 00696 // raise the correct function widget 00697 functionStack->raiseWidget( 00698 static_cast<QWidget*>( functionStack->child( "textRuleFuncCombo", 00699 0, false ) ) ); 00700 00701 // raise the correct value widget 00702 KMSearchRule::Function func = currentFunction( functionStack ); 00703 if ( func == KMSearchRule::FuncIsInAddressbook || 00704 func == KMSearchRule::FuncIsNotInAddressbook ) { 00705 valueStack->raiseWidget( 00706 static_cast<QWidget*>( valueStack->child( "textRuleValueHider", 00707 0, false ) ) ); 00708 } 00709 else if ( func == KMSearchRule::FuncIsInCategory || 00710 func == KMSearchRule::FuncIsNotInCategory) { 00711 valueStack->raiseWidget( 00712 static_cast<QWidget*>( valueStack->child( "categoryCombo", 00713 0, false ) ) ); 00714 } 00715 else { 00716 RegExpLineEdit *lineEdit = 00717 dynamic_cast<RegExpLineEdit*>( valueStack->child( "regExpLineEdit", 00718 0, false ) ); 00719 if ( lineEdit ) { 00720 lineEdit->showEditButton( func == KMSearchRule::FuncRegExp || 00721 func == KMSearchRule::FuncNotRegExp ); 00722 valueStack->raiseWidget( lineEdit ); 00723 } 00724 } 00725 return true; 00726 } 00727 00728 } // anonymous namespace for TextRuleWidgetHandler 00729 00730 00731 //============================================================================= 00732 // 00733 // class MessageRuleWidgetHandler 00734 // 00735 //============================================================================= 00736 00737 namespace { 00738 // also see KMSearchRule::matches() and KMSearchRule::Function 00739 // if you change the following strings! 00740 static const struct { 00741 const KMSearchRule::Function id; 00742 const char *displayName; 00743 } MessageFunctions[] = { 00744 { KMSearchRule::FuncContains, I18N_NOOP( "contains" ) }, 00745 { KMSearchRule::FuncContainsNot, I18N_NOOP( "does not contain" ) }, 00746 { KMSearchRule::FuncRegExp, I18N_NOOP( "matches regular expr." ) }, 00747 { KMSearchRule::FuncNotRegExp, I18N_NOOP( "does not match reg. expr." ) }, 00748 { KMSearchRule::FuncHasAttachment, I18N_NOOP( "has an attachment" ) }, 00749 { KMSearchRule::FuncHasNoAttachment, I18N_NOOP( "has no attachment" ) }, 00750 }; 00751 static const int MessageFunctionCount = 00752 sizeof( MessageFunctions ) / sizeof( *MessageFunctions ); 00753 00754 //--------------------------------------------------------------------------- 00755 00756 QWidget * MessageRuleWidgetHandler::createFunctionWidget( int number, 00757 QWidgetStack *functionStack, 00758 const QObject *receiver ) const 00759 { 00760 if ( number != 0 ) 00761 return 0; 00762 00763 QComboBox *funcCombo = new QComboBox( functionStack, "messageRuleFuncCombo" ); 00764 for ( int i = 0; i < MessageFunctionCount; ++i ) { 00765 funcCombo->insertItem( i18n( MessageFunctions[i].displayName ) ); 00766 } 00767 funcCombo->adjustSize(); 00768 QObject::connect( funcCombo, SIGNAL( activated( int ) ), 00769 receiver, SLOT( slotFunctionChanged() ) ); 00770 return funcCombo; 00771 } 00772 00773 //--------------------------------------------------------------------------- 00774 00775 QWidget * MessageRuleWidgetHandler::createValueWidget( int number, 00776 QWidgetStack *valueStack, 00777 const QObject *receiver ) const 00778 { 00779 if ( number == 0 ) { 00780 RegExpLineEdit *lineEdit = 00781 new RegExpLineEdit( valueStack, "regExpLineEdit" ); 00782 QObject::connect( lineEdit, SIGNAL( textChanged( const QString & ) ), 00783 receiver, SLOT( slotValueChanged() ) ); 00784 return lineEdit; 00785 } 00786 00787 // blank QLabel to hide value widget for has-attachment rule 00788 if ( number == 1 ) { 00789 return new QLabel( valueStack, "textRuleValueHider" ); 00790 } 00791 00792 return 0; 00793 } 00794 00795 //--------------------------------------------------------------------------- 00796 00797 KMSearchRule::Function MessageRuleWidgetHandler::currentFunction( const QWidgetStack *functionStack ) const 00798 { 00799 const QComboBox *funcCombo = 00800 dynamic_cast<QComboBox*>( QObject_child_const( functionStack, 00801 "messageRuleFuncCombo" ) ); 00802 // FIXME (Qt >= 4.0): Use the following when QObject::child() is const. 00803 // dynamic_cast<QComboBox*>( functionStack->child( "messageRuleFuncCombo", 00804 // 0, false ) ); 00805 if ( funcCombo ) { 00806 return MessageFunctions[funcCombo->currentItem()].id; 00807 } 00808 else 00809 kdDebug(5006) << "MessageRuleWidgetHandler::currentFunction: " 00810 "messageRuleFuncCombo not found." << endl; 00811 return KMSearchRule::FuncNone; 00812 } 00813 00814 //--------------------------------------------------------------------------- 00815 00816 KMSearchRule::Function MessageRuleWidgetHandler::function( const QCString & field, 00817 const QWidgetStack *functionStack ) const 00818 { 00819 if ( !handlesField( field ) ) 00820 return KMSearchRule::FuncNone; 00821 00822 return currentFunction( functionStack ); 00823 } 00824 00825 //--------------------------------------------------------------------------- 00826 00827 QString MessageRuleWidgetHandler::currentValue( const QWidgetStack *valueStack, 00828 KMSearchRule::Function ) const 00829 { 00830 const RegExpLineEdit *lineEdit = 00831 dynamic_cast<RegExpLineEdit*>( QObject_child_const( valueStack, 00832 "regExpLineEdit" ) ); 00833 // FIXME (Qt >= 4.0): Use the following when QObject::child() is const. 00834 // dynamic_cast<RegExpLineEdit*>( valueStack->child( "regExpLineEdit", 00835 // 0, false ) ); 00836 if ( lineEdit ) { 00837 return lineEdit->text(); 00838 } 00839 else 00840 kdDebug(5006) << "MessageRuleWidgetHandler::currentValue: " 00841 "regExpLineEdit not found." << endl; 00842 00843 return QString::null; 00844 } 00845 00846 //--------------------------------------------------------------------------- 00847 00848 QString MessageRuleWidgetHandler::value( const QCString & field, 00849 const QWidgetStack *functionStack, 00850 const QWidgetStack *valueStack ) const 00851 { 00852 if ( !handlesField( field ) ) 00853 return QString::null; 00854 00855 KMSearchRule::Function func = currentFunction( functionStack ); 00856 if ( func == KMSearchRule::FuncHasAttachment ) 00857 return "has an attachment"; // just a non-empty dummy value 00858 else if ( func == KMSearchRule::FuncHasNoAttachment ) 00859 return "has no attachment"; // just a non-empty dummy value 00860 else 00861 return currentValue( valueStack, func ); 00862 } 00863 00864 //--------------------------------------------------------------------------- 00865 00866 QString MessageRuleWidgetHandler::prettyValue( const QCString & field, 00867 const QWidgetStack *functionStack, 00868 const QWidgetStack *valueStack ) const 00869 { 00870 if ( !handlesField( field ) ) 00871 return QString::null; 00872 00873 KMSearchRule::Function func = currentFunction( functionStack ); 00874 if ( func == KMSearchRule::FuncHasAttachment ) 00875 return i18n( "has an attachment" ); 00876 else if ( func == KMSearchRule::FuncHasNoAttachment ) 00877 return i18n( "has no attachment" ); 00878 else 00879 return currentValue( valueStack, func ); 00880 } 00881 00882 //--------------------------------------------------------------------------- 00883 00884 bool MessageRuleWidgetHandler::handlesField( const QCString & field ) const 00885 { 00886 return ( field == "<message>" ); 00887 } 00888 00889 //--------------------------------------------------------------------------- 00890 00891 void MessageRuleWidgetHandler::reset( QWidgetStack *functionStack, 00892 QWidgetStack *valueStack ) const 00893 { 00894 // reset the function combo box 00895 QComboBox *funcCombo = 00896 dynamic_cast<QComboBox*>( functionStack->child( "messageRuleFuncCombo", 00897 0, false ) ); 00898 if ( funcCombo ) { 00899 funcCombo->blockSignals( true ); 00900 funcCombo->setCurrentItem( 0 ); 00901 funcCombo->blockSignals( false ); 00902 } 00903 00904 // reset the value widget 00905 RegExpLineEdit *lineEdit = 00906 dynamic_cast<RegExpLineEdit*>( valueStack->child( "regExpLineEdit", 00907 0, false ) ); 00908 if ( lineEdit ) { 00909 lineEdit->blockSignals( true ); 00910 lineEdit->clear(); 00911 lineEdit->blockSignals( false ); 00912 lineEdit->showEditButton( false ); 00913 valueStack->raiseWidget( lineEdit ); 00914 } 00915 } 00916 00917 //--------------------------------------------------------------------------- 00918 00919 bool MessageRuleWidgetHandler::setRule( QWidgetStack *functionStack, 00920 QWidgetStack *valueStack, 00921 const KMSearchRule *rule ) const 00922 { 00923 if ( !rule || !handlesField( rule->field() ) ) { 00924 reset( functionStack, valueStack ); 00925 return false; 00926 } 00927 00928 const KMSearchRule::Function func = rule->function(); 00929 int i = 0; 00930 for ( ; i < MessageFunctionCount; ++i ) 00931 if ( func == MessageFunctions[i].id ) 00932 break; 00933 QComboBox *funcCombo = 00934 dynamic_cast<QComboBox*>( functionStack->child( "messageRuleFuncCombo", 00935 0, false ) ); 00936 if ( funcCombo ) { 00937 funcCombo->blockSignals( true ); 00938 if ( i < MessageFunctionCount ) 00939 funcCombo->setCurrentItem( i ); 00940 else { 00941 kdDebug(5006) << "MessageRuleWidgetHandler::setRule( " 00942 << rule->asString() 00943 << " ): unhandled function" << endl; 00944 funcCombo->setCurrentItem( 0 ); 00945 } 00946 funcCombo->blockSignals( false ); 00947 functionStack->raiseWidget( funcCombo ); 00948 } 00949 00950 if ( func == KMSearchRule::FuncHasAttachment || 00951 func == KMSearchRule::FuncHasNoAttachment ) { 00952 QWidget *w = 00953 static_cast<QWidget*>( valueStack->child( "textRuleValueHider", 00954 0, false ) ); 00955 valueStack->raiseWidget( w ); 00956 } 00957 else { 00958 RegExpLineEdit *lineEdit = 00959 dynamic_cast<RegExpLineEdit*>( valueStack->child( "regExpLineEdit", 00960 0, false ) ); 00961 if ( lineEdit ) { 00962 lineEdit->blockSignals( true ); 00963 lineEdit->setText( rule->contents() ); 00964 lineEdit->blockSignals( false ); 00965 lineEdit->showEditButton( func == KMSearchRule::FuncRegExp || 00966 func == KMSearchRule::FuncNotRegExp ); 00967 valueStack->raiseWidget( lineEdit ); 00968 } 00969 } 00970 return true; 00971 } 00972 00973 00974 //--------------------------------------------------------------------------- 00975 00976 bool MessageRuleWidgetHandler::update( const QCString & field, 00977 QWidgetStack *functionStack, 00978 QWidgetStack *valueStack ) const 00979 { 00980 if ( !handlesField( field ) ) 00981 return false; 00982 // raise the correct function widget 00983 functionStack->raiseWidget( 00984 static_cast<QWidget*>( functionStack->child( "messageRuleFuncCombo", 00985 0, false ) ) ); 00986 00987 // raise the correct value widget 00988 KMSearchRule::Function func = currentFunction( functionStack ); 00989 if ( func == KMSearchRule::FuncHasAttachment || 00990 func == KMSearchRule::FuncHasNoAttachment ) { 00991 QWidget *w = 00992 static_cast<QWidget*>( valueStack->child( "textRuleValueHider", 00993 0, false ) ); 00994 valueStack->raiseWidget( w ); 00995 } 00996 else { 00997 RegExpLineEdit *lineEdit = 00998 dynamic_cast<RegExpLineEdit*>( valueStack->child( "regExpLineEdit", 00999 0, false ) ); 01000 if ( lineEdit ) { 01001 lineEdit->showEditButton( func == KMSearchRule::FuncRegExp || 01002 func == KMSearchRule::FuncNotRegExp ); 01003 valueStack->raiseWidget( lineEdit ); 01004 } 01005 } 01006 return true; 01007 } 01008 01009 } // anonymous namespace for MessageRuleWidgetHandler 01010 01011 01012 //============================================================================= 01013 // 01014 // class StatusRuleWidgetHandler 01015 // 01016 //============================================================================= 01017 01018 namespace { 01019 static const struct { 01020 const KMSearchRule::Function id; 01021 const char *displayName; 01022 } StatusFunctions[] = { 01023 { KMSearchRule::FuncContains, I18N_NOOP( "is" ) }, 01024 { KMSearchRule::FuncContainsNot, I18N_NOOP( "is not" ) } 01025 }; 01026 static const int StatusFunctionCount = 01027 sizeof( StatusFunctions ) / sizeof( *StatusFunctions ); 01028 01029 //--------------------------------------------------------------------------- 01030 01031 QWidget * StatusRuleWidgetHandler::createFunctionWidget( int number, 01032 QWidgetStack *functionStack, 01033 const QObject *receiver ) const 01034 { 01035 if ( number != 0 ) 01036 return 0; 01037 01038 QComboBox *funcCombo = new QComboBox( functionStack, 01039 "statusRuleFuncCombo" ); 01040 for ( int i = 0; i < StatusFunctionCount; ++i ) { 01041 funcCombo->insertItem( i18n( StatusFunctions[i].displayName ) ); 01042 } 01043 funcCombo->adjustSize(); 01044 QObject::connect( funcCombo, SIGNAL( activated( int ) ), 01045 receiver, SLOT( slotFunctionChanged() ) ); 01046 return funcCombo; 01047 } 01048 01049 //--------------------------------------------------------------------------- 01050 01051 QWidget * StatusRuleWidgetHandler::createValueWidget( int number, 01052 QWidgetStack *valueStack, 01053 const QObject *receiver ) const 01054 { 01055 if ( number != 0 ) 01056 return 0; 01057 01058 QComboBox *statusCombo = new QComboBox( valueStack, 01059 "statusRuleValueCombo" ); 01060 for ( int i = 0; i < KMail::StatusValueCountWithoutHidden; ++i ) { 01061 statusCombo->insertItem( i18n( KMail::StatusValues[i] ) ); 01062 } 01063 statusCombo->adjustSize(); 01064 QObject::connect( statusCombo, SIGNAL( activated( int ) ), 01065 receiver, SLOT( slotValueChanged() ) ); 01066 return statusCombo; 01067 } 01068 01069 //--------------------------------------------------------------------------- 01070 01071 KMSearchRule::Function StatusRuleWidgetHandler::currentFunction( const QWidgetStack *functionStack ) const 01072 { 01073 const QComboBox *funcCombo = 01074 dynamic_cast<QComboBox*>( QObject_child_const( functionStack, 01075 "statusRuleFuncCombo" ) ); 01076 // FIXME (Qt >= 4.0): Use the following when QObject::child() is const. 01077 // dynamic_cast<QComboBox*>( functionStack->child( "statusRuleFuncCombo", 01078 // 0, false ) ); 01079 if ( funcCombo ) { 01080 return StatusFunctions[funcCombo->currentItem()].id; 01081 } 01082 else 01083 kdDebug(5006) << "StatusRuleWidgetHandler::currentFunction: " 01084 "statusRuleFuncCombo not found." << endl; 01085 return KMSearchRule::FuncNone; 01086 } 01087 01088 //--------------------------------------------------------------------------- 01089 01090 KMSearchRule::Function StatusRuleWidgetHandler::function( const QCString & field, 01091 const QWidgetStack *functionStack ) const 01092 { 01093 if ( !handlesField( field ) ) 01094 return KMSearchRule::FuncNone; 01095 01096 return currentFunction( functionStack ); 01097 } 01098 01099 //--------------------------------------------------------------------------- 01100 01101 int StatusRuleWidgetHandler::currentStatusValue( const QWidgetStack *valueStack ) const 01102 { 01103 const QComboBox *statusCombo = 01104 dynamic_cast<QComboBox*>( QObject_child_const( valueStack, 01105 "statusRuleValueCombo" ) ); 01106 // FIXME (Qt >= 4.0): Use the following when QObject::child() is const. 01107 // dynamic_cast<QComboBox*>( valueStack->child( "statusRuleValueCombo", 01108 // 0, false ) ); 01109 if ( statusCombo ) { 01110 return statusCombo->currentItem(); 01111 } 01112 else 01113 kdDebug(5006) << "StatusRuleWidgetHandler::currentStatusValue: " 01114 "statusRuleValueCombo not found." << endl; 01115 return -1; 01116 } 01117 01118 //--------------------------------------------------------------------------- 01119 01120 QString StatusRuleWidgetHandler::value( const QCString & field, 01121 const QWidgetStack *, 01122 const QWidgetStack *valueStack ) const 01123 { 01124 if ( !handlesField( field ) ) 01125 return QString::null; 01126 01127 const int status = currentStatusValue( valueStack ); 01128 if ( status != -1 ) 01129 return QString::fromLatin1( KMail::StatusValues[status] ); 01130 else 01131 return QString::null; 01132 } 01133 01134 //--------------------------------------------------------------------------- 01135 01136 QString StatusRuleWidgetHandler::prettyValue( const QCString & field, 01137 const QWidgetStack *, 01138 const QWidgetStack *valueStack ) const 01139 { 01140 if ( !handlesField( field ) ) 01141 return QString::null; 01142 01143 const int status = currentStatusValue( valueStack ); 01144 if ( status != -1 ) 01145 return i18n( KMail::StatusValues[status] ); 01146 else 01147 return QString::null; 01148 } 01149 01150 //--------------------------------------------------------------------------- 01151 01152 bool StatusRuleWidgetHandler::handlesField( const QCString & field ) const 01153 { 01154 return ( field == "<status>" ); 01155 } 01156 01157 //--------------------------------------------------------------------------- 01158 01159 void StatusRuleWidgetHandler::reset( QWidgetStack *functionStack, 01160 QWidgetStack *valueStack ) const 01161 { 01162 // reset the function combo box 01163 QComboBox *funcCombo = 01164 dynamic_cast<QComboBox*>( functionStack->child( "statusRuleFuncCombo", 01165 0, false ) ); 01166 if ( funcCombo ) { 01167 funcCombo->blockSignals( true ); 01168 funcCombo->setCurrentItem( 0 ); 01169 funcCombo->blockSignals( false ); 01170 } 01171 01172 // reset the status value combo box 01173 QComboBox *statusCombo = 01174 dynamic_cast<QComboBox*>( valueStack->child( "statusRuleValueCombo", 01175 0, false ) ); 01176 if ( statusCombo ) { 01177 statusCombo->blockSignals( true ); 01178 statusCombo->setCurrentItem( 0 ); 01179 statusCombo->blockSignals( false ); 01180 } 01181 } 01182 01183 //--------------------------------------------------------------------------- 01184 01185 bool StatusRuleWidgetHandler::setRule( QWidgetStack *functionStack, 01186 QWidgetStack *valueStack, 01187 const KMSearchRule *rule ) const 01188 { 01189 if ( !rule || !handlesField( rule->field() ) ) { 01190 reset( functionStack, valueStack ); 01191 return false; 01192 } 01193 01194 // set the function 01195 const KMSearchRule::Function func = rule->function(); 01196 int funcIndex = 0; 01197 for ( ; funcIndex < StatusFunctionCount; ++funcIndex ) 01198 if ( func == StatusFunctions[funcIndex].id ) 01199 break; 01200 QComboBox *funcCombo = 01201 dynamic_cast<QComboBox*>( functionStack->child( "statusRuleFuncCombo", 01202 0, false ) ); 01203 if ( funcCombo ) { 01204 funcCombo->blockSignals( true ); 01205 if ( funcIndex < StatusFunctionCount ) 01206 funcCombo->setCurrentItem( funcIndex ); 01207 else { 01208 kdDebug(5006) << "StatusRuleWidgetHandler::setRule( " 01209 << rule->asString() 01210 << " ): unhandled function" << endl; 01211 funcCombo->setCurrentItem( 0 ); 01212 } 01213 funcCombo->blockSignals( false ); 01214 functionStack->raiseWidget( funcCombo ); 01215 } 01216 01217 // set the value 01218 const QString value = rule->contents(); 01219 int valueIndex = 0; 01220 for ( ; valueIndex < KMail::StatusValueCountWithoutHidden; ++valueIndex ) 01221 if ( value == QString::fromLatin1( 01222 KMail::StatusValues[valueIndex] ) ) 01223 break; 01224 QComboBox *statusCombo = 01225 dynamic_cast<QComboBox*>( valueStack->child( "statusRuleValueCombo", 01226 0, false ) ); 01227 if ( statusCombo ) { 01228 statusCombo->blockSignals( true ); 01229 if ( valueIndex < KMail::StatusValueCountWithoutHidden ) 01230 statusCombo->setCurrentItem( valueIndex ); 01231 else { 01232 kdDebug(5006) << "StatusRuleWidgetHandler::setRule( " 01233 << rule->asString() 01234 << " ): unhandled value" << endl; 01235 statusCombo->setCurrentItem( 0 ); 01236 } 01237 statusCombo->blockSignals( false ); 01238 valueStack->raiseWidget( statusCombo ); 01239 } 01240 return true; 01241 } 01242 01243 01244 //--------------------------------------------------------------------------- 01245 01246 bool StatusRuleWidgetHandler::update( const QCString &field, 01247 QWidgetStack *functionStack, 01248 QWidgetStack *valueStack ) const 01249 { 01250 if ( !handlesField( field ) ) 01251 return false; 01252 01253 // raise the correct function widget 01254 functionStack->raiseWidget( 01255 static_cast<QWidget*>( functionStack->child( "statusRuleFuncCombo", 01256 0, false ) ) ); 01257 01258 // raise the correct value widget 01259 valueStack->raiseWidget( 01260 static_cast<QWidget*>( valueStack->child( "statusRuleValueCombo", 01261 0, false ) ) ); 01262 return true; 01263 } 01264 01265 } // anonymous namespace for StatusRuleWidgetHandler 01266 01267 01268 //============================================================================= 01269 // 01270 // class NumericRuleWidgetHandler 01271 // 01272 //============================================================================= 01273 01274 namespace { 01275 static const struct { 01276 const KMSearchRule::Function id; 01277 const char *displayName; 01278 } NumericFunctions[] = { 01279 { KMSearchRule::FuncEquals, I18N_NOOP( "is equal to" ) }, 01280 { KMSearchRule::FuncNotEqual, I18N_NOOP( "is not equal to" ) }, 01281 { KMSearchRule::FuncIsGreater, I18N_NOOP( "is greater than" ) }, 01282 { KMSearchRule::FuncIsLessOrEqual, I18N_NOOP( "is less than or equal to" ) }, 01283 { KMSearchRule::FuncIsLess, I18N_NOOP( "is less than" ) }, 01284 { KMSearchRule::FuncIsGreaterOrEqual, I18N_NOOP( "is greater than or equal to" ) } 01285 }; 01286 static const int NumericFunctionCount = 01287 sizeof( NumericFunctions ) / sizeof( *NumericFunctions ); 01288 01289 //--------------------------------------------------------------------------- 01290 01291 QWidget * NumericRuleWidgetHandler::createFunctionWidget( int number, 01292 QWidgetStack *functionStack, 01293 const QObject *receiver ) const 01294 { 01295 if ( number != 0 ) 01296 return 0; 01297 01298 QComboBox *funcCombo = new QComboBox( functionStack, 01299 "numericRuleFuncCombo" ); 01300 for ( int i = 0; i < NumericFunctionCount; ++i ) { 01301 funcCombo->insertItem( i18n( NumericFunctions[i].displayName ) ); 01302 } 01303 funcCombo->adjustSize(); 01304 QObject::connect( funcCombo, SIGNAL( activated( int ) ), 01305 receiver, SLOT( slotFunctionChanged() ) ); 01306 return funcCombo; 01307 } 01308 01309 //--------------------------------------------------------------------------- 01310 01311 QWidget * NumericRuleWidgetHandler::createValueWidget( int number, 01312 QWidgetStack *valueStack, 01313 const QObject *receiver ) const 01314 { 01315 if ( number != 0 ) 01316 return 0; 01317 01318 KIntNumInput *numInput = new KIntNumInput( valueStack, "KIntNumInput" ); 01319 QObject::connect( numInput, SIGNAL( valueChanged( int ) ), 01320 receiver, SLOT( slotValueChanged() ) ); 01321 return numInput; 01322 } 01323 01324 //--------------------------------------------------------------------------- 01325 01326 KMSearchRule::Function NumericRuleWidgetHandler::currentFunction( const QWidgetStack *functionStack ) const 01327 { 01328 const QComboBox *funcCombo = 01329 dynamic_cast<QComboBox*>( QObject_child_const( functionStack, 01330 "numericRuleFuncCombo" ) ); 01331 // FIXME (Qt >= 4.0): Use the following when QObject::child() is const. 01332 // dynamic_cast<QComboBox*>( functionStack->child( "numericRuleFuncCombo", 01333 // 0, false ) ); 01334 if ( funcCombo ) { 01335 return NumericFunctions[funcCombo->currentItem()].id; 01336 } 01337 else 01338 kdDebug(5006) << "NumericRuleWidgetHandler::currentFunction: " 01339 "numericRuleFuncCombo not found." << endl; 01340 return KMSearchRule::FuncNone; 01341 } 01342 01343 //--------------------------------------------------------------------------- 01344 01345 KMSearchRule::Function NumericRuleWidgetHandler::function( const QCString & field, 01346 const QWidgetStack *functionStack ) const 01347 { 01348 if ( !handlesField( field ) ) 01349 return KMSearchRule::FuncNone; 01350 01351 return currentFunction( functionStack ); 01352 } 01353 01354 //--------------------------------------------------------------------------- 01355 01356 QString NumericRuleWidgetHandler::currentValue( const QWidgetStack *valueStack ) const 01357 { 01358 const KIntNumInput *numInput = 01359 dynamic_cast<KIntNumInput*>( QObject_child_const( valueStack, 01360 "KIntNumInput" ) ); 01361 // FIXME (Qt >= 4.0): Use the following when QObject::child() is const. 01362 // dynamic_cast<KIntNumInput*>( valueStack->child( "KIntNumInput", 01363 // 0, false ) ); 01364 if ( numInput ) { 01365 return QString::number( numInput->value() ); 01366 } 01367 else 01368 kdDebug(5006) << "NumericRuleWidgetHandler::currentValue: " 01369 "KIntNumInput not found." << endl; 01370 return QString::null; 01371 } 01372 01373 //--------------------------------------------------------------------------- 01374 01375 QString NumericRuleWidgetHandler::value( const QCString & field, 01376 const QWidgetStack *, 01377 const QWidgetStack *valueStack ) const 01378 { 01379 if ( !handlesField( field ) ) 01380 return QString::null; 01381 01382 return currentValue( valueStack ); 01383 } 01384 01385 //--------------------------------------------------------------------------- 01386 01387 QString NumericRuleWidgetHandler::prettyValue( const QCString & field, 01388 const QWidgetStack *, 01389 const QWidgetStack *valueStack ) const 01390 { 01391 if ( !handlesField( field ) ) 01392 return QString::null; 01393 01394 return currentValue( valueStack ); 01395 } 01396 01397 //--------------------------------------------------------------------------- 01398 01399 bool NumericRuleWidgetHandler::handlesField( const QCString & field ) const 01400 { 01401 return ( field == "<size>" || field == "<age in days>" ); 01402 } 01403 01404 //--------------------------------------------------------------------------- 01405 01406 void NumericRuleWidgetHandler::reset( QWidgetStack *functionStack, 01407 QWidgetStack *valueStack ) const 01408 { 01409 // reset the function combo box 01410 QComboBox *funcCombo = 01411 dynamic_cast<QComboBox*>( functionStack->child( "numericRuleFuncCombo", 01412 0, false ) ); 01413 if ( funcCombo ) { 01414 funcCombo->blockSignals( true ); 01415 funcCombo->setCurrentItem( 0 ); 01416 funcCombo->blockSignals( false ); 01417 } 01418 01419 // reset the value widget 01420 KIntNumInput *numInput = 01421 dynamic_cast<KIntNumInput*>( valueStack->child( "KIntNumInput", 01422 0, false ) ); 01423 if ( numInput ) { 01424 numInput->blockSignals( true ); 01425 numInput->setValue( 0 ); 01426 numInput->blockSignals( false ); 01427 } 01428 } 01429 01430 //--------------------------------------------------------------------------- 01431 01432 void initNumInput( KIntNumInput *numInput, const QCString &field ) 01433 { 01434 if ( field == "<size>" ) { 01435 numInput->setMinValue( 0 ); 01436 numInput->setSuffix( i18n( " bytes" ) ); 01437 } 01438 else { 01439 numInput->setMinValue( -10000 ); 01440 numInput->setSuffix( i18n( " days" ) ); 01441 } 01442 } 01443 01444 //--------------------------------------------------------------------------- 01445 01446 bool NumericRuleWidgetHandler::setRule( QWidgetStack *functionStack, 01447 QWidgetStack *valueStack, 01448 const KMSearchRule *rule ) const 01449 { 01450 if ( !rule || !handlesField( rule->field() ) ) { 01451 reset( functionStack, valueStack ); 01452 return false; 01453 } 01454 01455 // set the function 01456 const KMSearchRule::Function func = rule->function(); 01457 int funcIndex = 0; 01458 for ( ; funcIndex < NumericFunctionCount; ++funcIndex ) 01459 if ( func == NumericFunctions[funcIndex].id ) 01460 break; 01461 QComboBox *funcCombo = 01462 dynamic_cast<QComboBox*>( functionStack->child( "numericRuleFuncCombo", 01463 0, false ) ); 01464 if ( funcCombo ) { 01465 funcCombo->blockSignals( true ); 01466 if ( funcIndex < NumericFunctionCount ) 01467 funcCombo->setCurrentItem( funcIndex ); 01468 else { 01469 kdDebug(5006) << "NumericRuleWidgetHandler::setRule( " 01470 << rule->asString() 01471 << " ): unhandled function" << endl; 01472 funcCombo->setCurrentItem( 0 ); 01473 } 01474 funcCombo->blockSignals( false ); 01475 functionStack->raiseWidget( funcCombo ); 01476 } 01477 01478 // set the value 01479 bool ok; 01480 int value = rule->contents().toInt( &ok ); 01481 if ( !ok ) 01482 value = 0; 01483 KIntNumInput *numInput = 01484 dynamic_cast<KIntNumInput*>( valueStack->child( "KIntNumInput", 01485 0, false ) ); 01486 if ( numInput ) { 01487 initNumInput( numInput, rule->field() ); 01488 numInput->blockSignals( true ); 01489 numInput->setValue( value ); 01490 numInput->blockSignals( false ); 01491 valueStack->raiseWidget( numInput ); 01492 } 01493 return true; 01494 } 01495 01496 01497 //--------------------------------------------------------------------------- 01498 01499 bool NumericRuleWidgetHandler::update( const QCString &field, 01500 QWidgetStack *functionStack, 01501 QWidgetStack *valueStack ) const 01502 { 01503 if ( !handlesField( field ) ) 01504 return false; 01505 01506 // raise the correct function widget 01507 functionStack->raiseWidget( 01508 static_cast<QWidget*>( functionStack->child( "numericRuleFuncCombo", 01509 0, false ) ) ); 01510 01511 // raise the correct value widget 01512 KIntNumInput *numInput = 01513 dynamic_cast<KIntNumInput*>( valueStack->child( "KIntNumInput", 01514 0, false ) ); 01515 if ( numInput ) { 01516 initNumInput( numInput, field ); 01517 valueStack->raiseWidget( numInput ); 01518 } 01519 return true; 01520 } 01521 01522 } // anonymous namespace for NumericRuleWidgetHandler 01523
KDE Logo
This file is part of the documentation for kmail Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Oct 21 19:46:51 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003