kmail

kmfilterdlg.cpp

00001 // -*- mode: C++; c-file-style: "gnu" -*-
00002 // kmfilterdlg.cpp
00003 // Author: Marc Mutz <Marc@Mutz.com>
00004 // based on work by Stefan Taferner <taferner@kde.org>
00005 // This code is under the GPL
00006 
00007 #include <config.h>
00008 #include "kmfilterdlg.h"
00009 
00010 // other KMail headers:
00011 #include "kmsearchpatternedit.h"
00012 #include "kmfiltermgr.h"
00013 #include "kmmainwidget.h"
00014 #include "accountmanager.h"
00015 using KMail::AccountManager;
00016 
00017 // other KDE headers:
00018 #include <kmessagebox.h>
00019 #include <kdebug.h>
00020 #include <klocale.h>
00021 #include <kinputdialog.h>
00022 #include <kiconloader.h>
00023 #include <kapplication.h>
00024 #include <kwin.h>
00025 #include <kconfig.h>
00026 #include <kicondialog.h>
00027 #include <kkeybutton.h>
00028 #include <klistview.h>
00029 #include <kpushbutton.h>
00030 
00031 // other Qt headers:
00032 #include <qlayout.h>
00033 #include <qlabel.h>
00034 #include <qcombobox.h>
00035 #include <qwidgetstack.h>
00036 #include <qtooltip.h>
00037 #include <qwhatsthis.h>
00038 #include <qcheckbox.h>
00039 #include <qhbox.h>
00040 #include <qvalidator.h>
00041 #include <qtabwidget.h>
00042 
00043 // other headers:
00044 #include <assert.h>
00045 
00046 
00047 // What's this help texts
00048 const char * _wt_filterlist =
00049 I18N_NOOP( "<qt><p>This is the list of defined filters. "
00050        "They are processed top-to-bottom.</p>"
00051        "<p>Click on any filter to edit it "
00052        "using the controls in the right-hand half "
00053        "of the dialog.</p></qt>" );
00054 const char * _wt_filterlist_new =
00055 I18N_NOOP( "<qt><p>Click this button to create a new filter.</p>"
00056        "<p>The filter will be inserted just before the currently-"
00057        "selected one, but you can always change that "
00058        "later on.</p>"
00059        "<p>If you have clicked this button accidentally, you can undo this "
00060        "by clicking on the <em>Delete</em> button.</p></qt>" );
00061 const char * _wt_filterlist_copy =
00062 I18N_NOOP( "<qt><p>Click this button to copy a filter.</p>"
00063        "<p>If you have clicked this button accidentally, you can undo this "
00064        "by clicking on the <em>Delete</em> button.</p></qt>" );
00065 const char * _wt_filterlist_delete =
00066 I18N_NOOP( "<qt><p>Click this button to <em>delete</em> the currently-"
00067        "selected filter from the list above.</p>"
00068        "<p>There is no way to get the filter back once "
00069        "it is deleted, but you can always leave the "
00070        "dialog by clicking <em>Cancel</em> to discard the "
00071        "changes made.</p></qt>" );
00072 const char * _wt_filterlist_top =
00073 I18N_NOOP( "<qt><p>Click this button to move the currently-"
00074        "selected filter to the <em>top</em> of the list above.</p>"
00075        "<p>This is useful since the order of the filters in the list "
00076        "determines the order in which they are tried on messages: "
00077        "The topmost filter gets tried first.</p></qt>" );
00078 const char * _wt_filterlist_up =
00079 I18N_NOOP( "<qt><p>Click this button to move the currently-"
00080        "selected filter <em>up</em> one in the list above.</p>"
00081        "<p>This is useful since the order of the filters in the list "
00082        "determines the order in which they are tried on messages: "
00083        "The topmost filter gets tried first.</p>"
00084        "<p>If you have clicked this button accidentally, you can undo this "
00085        "by clicking on the <em>Down</em> button.</p></qt>" );
00086 const char * _wt_filterlist_down =
00087 I18N_NOOP( "<qt><p>Click this button to move the currently-"
00088        "selected filter <em>down</em> one in the list above.</p>"
00089        "<p>This is useful since the order of the filters in the list "
00090        "determines the order in which they are tried on messages: "
00091        "The topmost filter gets tried first.</p>"
00092        "<p>If you have clicked this button accidentally, you can undo this "
00093        "by clicking on the <em>Up</em> button.</p></qt>" );
00094 const char * _wt_filterlist_bot =
00095 I18N_NOOP( "<qt><p>Click this button to move the currently-"
00096        "selected filter to the <em>bottom</em> of the list above.</p>"
00097        "<p>This is useful since the order of the filters in the list "
00098        "determines the order in which they are tried on messages: "
00099        "The topmost filter gets tried first.</p></qt>" );
00100 const char * _wt_filterlist_rename =
00101 I18N_NOOP( "<qt><p>Click this button to rename the currently-selected filter.</p>"
00102        "<p>Filters are named automatically, as long as they start with "
00103        "\"&lt;\".</p>"
00104        "<p>If you have renamed a filter accidentally and want automatic "
00105        "naming back, click this button and select <em>Clear</em> followed "
00106        "by <em>OK</em> in the appearing dialog.</p></qt>" );
00107 const char * _wt_filterdlg_showLater =
00108 I18N_NOOP( "<qt><p>Check this button to force the confirmation dialog to be "
00109        "displayed.</p><p>This is useful if you have defined a ruleset that tags "
00110            "messages to be downloaded later. Without the possibility to force "
00111            "the dialog popup, these messages could never be downloaded if no "
00112            "other large messages were waiting on the server, or if you wanted to "
00113            "change the ruleset to tag the messages differently.</p></qt>" );
00114 
00115 // The anchor of the filter dialog's help.
00116 const char * KMFilterDlgHelpAnchor =  "filters-id" ;
00117 const char * KMPopFilterDlgHelpAnchor =  "popfilters-id" ;
00118 
00119 //=============================================================================
00120 //
00121 // class KMFilterDlg (the filter dialog)
00122 //
00123 //=============================================================================
00124 
00125 KMFilterDlg::KMFilterDlg(QWidget* parent, const char* name, bool popFilter, bool createDummyFilter )
00126   : KDialogBase( parent, name, FALSE /* modality */,
00127          (popFilter)? i18n("POP3 Filter Rules"): i18n("Filter Rules") /* caption*/,
00128          Help|Ok|Apply|Cancel /* button mask */,
00129          Ok /* default btn */, FALSE /* separator */),
00130   bPopFilter(popFilter)
00131 {
00132   KWin::setIcons( winId(), kapp->icon(), kapp->miniIcon() );
00133   setHelp( (bPopFilter)? KMPopFilterDlgHelpAnchor: KMFilterDlgHelpAnchor );
00134 
00135   QWidget *w = new QWidget( this );
00136   setMainWidget( w );
00137   QHBoxLayout *topLayout = new QHBoxLayout( w, 0, spacingHint(), "topLayout" );
00138   QHBoxLayout *hbl = topLayout;
00139   QVBoxLayout *vbl2 = 0;
00140   QWidget *page1 = 0;
00141   QWidget *page2 = 0;
00142 
00143   mFilterList = new KMFilterListBox( i18n("Available Filters"), w, 0, bPopFilter);
00144   topLayout->addWidget( mFilterList, 1 /*stretch*/ );
00145 
00146   if(!bPopFilter) {
00147     QTabWidget *tabWidget = new QTabWidget( w, "kmfd_tab" );
00148     tabWidget->setMargin( KDialog::marginHint() );
00149     topLayout->addWidget( tabWidget );
00150 
00151     page1 = new QWidget( tabWidget );
00152     tabWidget->addTab( page1, i18n("&General") );
00153     hbl = new QHBoxLayout( page1, 0, spacingHint(), "kmfd_hbl" );
00154 
00155     page2 = new QWidget( tabWidget );
00156     tabWidget->addTab( page2, i18n("A&dvanced") );
00157     vbl2 = new QVBoxLayout( page2, 0, spacingHint(), "kmfd_vbl2" );
00158   }
00159 
00160   QVBoxLayout *vbl = new QVBoxLayout( hbl, spacingHint(), "kmfd_vbl" );
00161   hbl->setStretchFactor( vbl, 2 );
00162 
00163   mPatternEdit = new KMSearchPatternEdit( i18n("Filter Criteria"), bPopFilter ? w : page1 , "spe", bPopFilter);
00164   vbl->addWidget( mPatternEdit, 0, Qt::AlignTop );
00165 
00166   if(bPopFilter){
00167     mActionGroup = new KMPopFilterActionWidget( i18n("Filter Action"), w );
00168     vbl->addWidget( mActionGroup, 0, Qt::AlignTop );
00169 
00170     mGlobalsBox = new QVGroupBox(i18n("Global Options"), w);
00171     mShowLaterBtn = new QCheckBox(i18n("Always &show matched 'Download Later' messages in confirmation dialog"), mGlobalsBox);
00172     QWhatsThis::add( mShowLaterBtn, i18n(_wt_filterdlg_showLater) );
00173     vbl->addWidget( mGlobalsBox, 0, Qt::AlignTop );
00174   }
00175   else {
00176     QGroupBox *agb = new QGroupBox( 1 /*column*/, Vertical, i18n("Filter Actions"), page1 );
00177     mActionLister = new KMFilterActionWidgetLister( agb );
00178     vbl->addWidget( agb, 0, Qt::AlignTop );
00179 
00180     mAdvOptsGroup = new QGroupBox ( 1 /*columns*/, Vertical,
00181                     i18n("Advanced Options"), page2);
00182     {
00183       QWidget *adv_w = new QWidget( mAdvOptsGroup );
00184       QGridLayout *gl = new QGridLayout( adv_w, 8 /*rows*/, 3 /*cols*/,
00185                              0 /*border*/, spacingHint() );
00186 
00187       QVBoxLayout *vbl3 = new QVBoxLayout( gl, spacingHint(), "vbl3" );
00188       vbl3->addStretch( 1 );
00189       mApplyOnIn = new QCheckBox( i18n("Apply this filter to incoming messages:"), adv_w );
00190       vbl3->addWidget( mApplyOnIn );
00191       QButtonGroup *bg = new QButtonGroup( 0, "bg" );
00192       bg->setExclusive( true );
00193       mApplyOnForAll = new QRadioButton( i18n("from all accounts"), adv_w );
00194       bg->insert( mApplyOnForAll );
00195       vbl3->addWidget( mApplyOnForAll );
00196       mApplyOnForTraditional = new QRadioButton( i18n("from all but online IMAP accounts"), adv_w );
00197       bg->insert( mApplyOnForTraditional );
00198       vbl3->addWidget( mApplyOnForTraditional );
00199       mApplyOnForChecked = new QRadioButton( i18n("from checked accounts only"), adv_w );
00200       bg->insert( mApplyOnForChecked );
00201       vbl3->addWidget( mApplyOnForChecked );
00202       vbl3->addStretch( 2 );
00203 
00204       mAccountList = new KListView( adv_w, "accountList" );
00205       mAccountList->addColumn( i18n("Account Name") );
00206       mAccountList->addColumn( i18n("Type") );
00207       mAccountList->setAllColumnsShowFocus( true );
00208       mAccountList->setFrameStyle( QFrame::WinPanel + QFrame::Sunken );
00209       mAccountList->setSorting( -1 );
00210       gl->addMultiCellWidget( mAccountList, 0, 3, 1, 3 );
00211 
00212       mApplyOnOut = new QCheckBox( i18n("Apply this filter to &sent messages"), adv_w );
00213       gl->addMultiCellWidget( mApplyOnOut, 4, 4, 0, 3 );
00214 
00215       mApplyOnCtrlJ = new QCheckBox( i18n("Apply this filter on manual &filtering"), adv_w );
00216       gl->addMultiCellWidget( mApplyOnCtrlJ, 5, 5, 0, 3 );
00217 
00218       mStopProcessingHere = new QCheckBox( i18n("If this filter &matches, stop processing here"), adv_w );
00219       gl->addMultiCellWidget( mStopProcessingHere,
00220                   6, 6, /*from to row*/
00221                   0, 3 /*from to col*/ );
00222       mConfigureShortcut = new QCheckBox( i18n("Add this filter to the Apply Filter menu"), adv_w );
00223       gl->addMultiCellWidget( mConfigureShortcut, 7, 7, 0, 1 );
00224       QLabel *keyButtonLabel = new QLabel( i18n( "Shortcut:" ), adv_w );
00225       keyButtonLabel->setAlignment( AlignVCenter | AlignRight );
00226       gl->addMultiCellWidget( keyButtonLabel, 7, 7, 2, 2 );
00227       mKeyButton = new KKeyButton( adv_w, "FilterShortcutSelector" );
00228       gl->addMultiCellWidget( mKeyButton, 7, 7, 3, 3 );
00229       mConfigureToolbar = new QCheckBox( i18n("Additionally add this filter to the toolbar"), adv_w );
00230       gl->addMultiCellWidget( mConfigureToolbar, 8, 8, 0, 3 );
00231       mConfigureToolbar->setEnabled( false );
00232 
00233       QHBox *hbox = new QHBox( adv_w );
00234       mFilterActionLabel = new QLabel( i18n( "Icon for this filter:" ),
00235                                        hbox );
00236       mFilterActionLabel->setEnabled( false );
00237 
00238       mFilterActionIconButton = new KIconButton( hbox );
00239       mFilterActionLabel->setBuddy( mFilterActionIconButton );
00240       mFilterActionIconButton->setIconType( KIcon::NoGroup, KIcon::Any, true );
00241       mFilterActionIconButton->setIconSize( 16 );
00242       mFilterActionIconButton->setIcon( "gear" );
00243       mFilterActionIconButton->setEnabled( false );
00244 
00245       gl->addMultiCellWidget( hbox, 9, 9, 0, 3 );
00246     }
00247     vbl2->addWidget( mAdvOptsGroup, 0, Qt::AlignTop );
00248   }
00249   // spacer:
00250   vbl->addStretch( 1 );
00251 
00252   // load the filter parts into the edit widgets
00253   connect( mFilterList, SIGNAL(filterSelected(KMFilter*)),
00254        this, SLOT(slotFilterSelected(KMFilter*)) );
00255 
00256   if (bPopFilter){
00257     // set the state of the global setting 'show later msgs'
00258     connect( mShowLaterBtn, SIGNAL(toggled(bool)),
00259              mFilterList, SLOT(slotShowLaterToggled(bool)));
00260 
00261     // set the action in the filter when changed
00262     connect( mActionGroup, SIGNAL(actionChanged(const KMPopFilterAction)),
00263          this, SLOT(slotActionChanged(const KMPopFilterAction)) );
00264   } else {
00265     // transfer changes from the 'Apply this filter on...'
00266     // combo box to the filter
00267     connect( mApplyOnIn, SIGNAL(clicked()),
00268          this, SLOT(slotApplicabilityChanged()) );
00269     connect( mApplyOnForAll, SIGNAL(clicked()),
00270          this, SLOT(slotApplicabilityChanged()) );
00271     connect( mApplyOnForTraditional, SIGNAL(clicked()),
00272          this, SLOT(slotApplicabilityChanged()) );
00273     connect( mApplyOnForChecked, SIGNAL(clicked()),
00274          this, SLOT(slotApplicabilityChanged()) );
00275     connect( mApplyOnOut, SIGNAL(clicked()),
00276          this, SLOT(slotApplicabilityChanged()) );
00277     connect( mApplyOnCtrlJ, SIGNAL(clicked()),
00278          this, SLOT(slotApplicabilityChanged()) );
00279     connect( mAccountList, SIGNAL(clicked(QListViewItem*)),
00280          this, SLOT(slotApplicableAccountsChanged()) );
00281     connect( mAccountList, SIGNAL(spacePressed(QListViewItem*)),
00282          this, SLOT(slotApplicableAccountsChanged()) );
00283 
00284     // transfer changes from the 'stop processing here'
00285     // check box to the filter
00286     connect( mStopProcessingHere, SIGNAL(toggled(bool)),
00287          this, SLOT(slotStopProcessingButtonToggled(bool)) );
00288 
00289     connect( mConfigureShortcut, SIGNAL(toggled(bool)),
00290          this, SLOT(slotConfigureShortcutButtonToggled(bool)) );
00291 
00292     connect( mKeyButton, SIGNAL( capturedShortcut( const KShortcut& ) ),
00293              this, SLOT( slotCapturedShortcutChanged( const KShortcut& ) ) );
00294 
00295     connect( mConfigureToolbar, SIGNAL(toggled(bool)),
00296          this, SLOT(slotConfigureToolbarButtonToggled(bool)) );
00297 
00298     connect( mFilterActionIconButton, SIGNAL( iconChanged( QString ) ),
00299              this, SLOT( slotFilterActionIconChanged( QString ) ) );
00300   }
00301 
00302   // reset all widgets here
00303   connect( mFilterList, SIGNAL(resetWidgets()),
00304        this, SLOT(slotReset()) );
00305 
00306   connect( mFilterList, SIGNAL( applyWidgets() ),
00307            this, SLOT( slotUpdateFilter() ) );
00308 
00309   // support auto-naming the filter
00310   connect( mPatternEdit, SIGNAL(maybeNameChanged()),
00311        mFilterList, SLOT(slotUpdateFilterName()) );
00312 
00313   // apply changes on 'Apply'
00314   connect( this, SIGNAL(applyClicked()),
00315        mFilterList, SLOT(slotApplyFilterChanges()) );
00316 
00317   // apply changes on 'OK'
00318   connect( this, SIGNAL(okClicked()),
00319        mFilterList, SLOT(slotApplyFilterChanges()) );
00320 
00321   // save dialog size on 'OK'
00322   connect( this, SIGNAL(okClicked()),
00323        this, SLOT(slotSaveSize()) );
00324 
00325   // destruct the dialog on OK, close and Cancel
00326   connect( this, SIGNAL(finished()),
00327        this, SLOT(slotFinished()) );
00328 
00329   KConfigGroup geometry( KMKernel::config(), "Geometry");
00330   const char * configKey
00331     = bPopFilter ? "popFilterDialogSize" : "filterDialogSize";
00332   if ( geometry.hasKey( configKey ) )
00333     resize( geometry.readSizeEntry( configKey ) );
00334   else
00335     adjustSize();
00336 
00337   // load the filter list (emits filterSelected())
00338   mFilterList->loadFilterList( createDummyFilter );
00339 }
00340 
00341 void KMFilterDlg::slotFinished() {
00342     delayedDestruct();
00343 }
00344 
00345 void KMFilterDlg::slotSaveSize() {
00346   KConfigGroup geometry( KMKernel::config(), "Geometry" );
00347   geometry.writeEntry( bPopFilter ? "popFilterDialogSize" : "filterDialogSize", size() );
00348 }
00349 
00351 void KMFilterDlg::slotActionChanged(const KMPopFilterAction aAction)
00352 {
00353   mFilter->setAction(aAction);
00354 }
00355 
00356 void KMFilterDlg::slotFilterSelected( KMFilter* aFilter )
00357 {
00358   assert( aFilter );
00359 
00360   if (bPopFilter){
00361     mActionGroup->setAction( aFilter->action() );
00362     mGlobalsBox->setEnabled(true);
00363     mShowLaterBtn->setChecked(mFilterList->showLaterMsgs());
00364   } else {
00365     mActionLister->setActionList( aFilter->actions() );
00366 
00367     mAdvOptsGroup->setEnabled( true );
00368   }
00369 
00370   mPatternEdit->setSearchPattern( aFilter->pattern() );
00371   mFilter = aFilter;
00372 
00373   if (!bPopFilter) {
00374     kdDebug(5006) << "apply on inbound == "
00375           << aFilter->applyOnInbound() << endl;
00376     kdDebug(5006) << "apply on outbound == "
00377           << aFilter->applyOnOutbound() << endl;
00378     kdDebug(5006) << "apply on explicit == "
00379           << aFilter->applyOnExplicit() << endl;
00380 
00381     // NOTE: setting these values activates the slot that sets them in
00382     // the filter! So make sure we have the correct values _before_ we
00383     // set the first one:
00384     const bool applyOnIn = aFilter->applyOnInbound();
00385     const bool applyOnForAll = aFilter->applicability() == KMFilter::All;
00386     const bool applyOnTraditional = aFilter->applicability() == KMFilter::ButImap;
00387     const bool applyOnOut = aFilter->applyOnOutbound();
00388     const bool applyOnExplicit = aFilter->applyOnExplicit();
00389     const bool stopHere = aFilter->stopProcessingHere();
00390     const bool configureShortcut = aFilter->configureShortcut();
00391     const bool configureToolbar = aFilter->configureToolbar();
00392     const QString icon = aFilter->icon();
00393     const KShortcut shortcut( aFilter->shortcut() );
00394 
00395     mApplyOnIn->setChecked( applyOnIn );
00396     mApplyOnForAll->setEnabled( applyOnIn );
00397     mApplyOnForTraditional->setEnabled( applyOnIn );
00398     mApplyOnForChecked->setEnabled( applyOnIn );
00399     mApplyOnForAll->setChecked( applyOnForAll );
00400     mApplyOnForTraditional->setChecked( applyOnTraditional );
00401     mApplyOnForChecked->setChecked( !applyOnForAll && !applyOnTraditional );
00402     mAccountList->setEnabled( mApplyOnForChecked->isEnabled() && mApplyOnForChecked->isChecked() );
00403     slotUpdateAccountList();
00404     mApplyOnOut->setChecked( applyOnOut );
00405     mApplyOnCtrlJ->setChecked( applyOnExplicit );
00406     mStopProcessingHere->setChecked( stopHere );
00407     mConfigureShortcut->setChecked( configureShortcut );
00408     mKeyButton->setShortcut( shortcut, false );
00409     mConfigureToolbar->setChecked( configureToolbar );
00410     mFilterActionIconButton->setIcon( icon );
00411   }
00412 }
00413 
00414 void KMFilterDlg::slotReset()
00415 {
00416   mFilter = 0;
00417   mPatternEdit->reset();
00418 
00419   if(bPopFilter) {
00420     mActionGroup->reset();
00421     mGlobalsBox->setEnabled( false );
00422   } else {
00423     mActionLister->reset();
00424     mAdvOptsGroup->setEnabled( false );
00425     slotUpdateAccountList();
00426   }
00427 }
00428 
00429 void KMFilterDlg::slotUpdateFilter()
00430 {
00431   mPatternEdit->updateSearchPattern();
00432   if ( !bPopFilter ) {
00433     mActionLister->updateActionList();
00434   }
00435 }
00436 
00437 void KMFilterDlg::slotApplicabilityChanged()
00438 {
00439   if ( mFilter ) {
00440     mFilter->setApplyOnInbound( mApplyOnIn->isChecked() );
00441     mFilter->setApplyOnOutbound( mApplyOnOut->isChecked() );
00442     mFilter->setApplyOnExplicit( mApplyOnCtrlJ->isChecked() );
00443     if ( mApplyOnForAll->isChecked() )
00444       mFilter->setApplicability( KMFilter::All );
00445     else if ( mApplyOnForTraditional->isChecked() )
00446       mFilter->setApplicability( KMFilter::ButImap );
00447     else if ( mApplyOnForChecked->isChecked() )
00448       mFilter->setApplicability( KMFilter::Checked );
00449 
00450     mApplyOnForAll->setEnabled( mApplyOnIn->isChecked() );
00451     mApplyOnForTraditional->setEnabled(  mApplyOnIn->isChecked() );
00452     mApplyOnForChecked->setEnabled( mApplyOnIn->isChecked() );
00453     mAccountList->setEnabled( mApplyOnForChecked->isEnabled() && mApplyOnForChecked->isChecked() );
00454 
00455     // Advanced tab functionality - Update list of accounts this filter applies to
00456     QListViewItemIterator it( mAccountList );
00457     while ( it.current() ) {
00458       QCheckListItem *item = dynamic_cast<QCheckListItem*>( it.current() );
00459       if (item) {
00460     int id = item->text( 2 ).toInt();
00461       item->setOn( mFilter->applyOnAccount( id ) );
00462       }
00463       ++it;
00464     }
00465 
00466     kdDebug(5006) << "KMFilterDlg: setting filter to be applied at "
00467                   << ( mFilter->applyOnInbound() ? "incoming " : "" )
00468                   << ( mFilter->applyOnOutbound() ? "outgoing " : "" )
00469                   << ( mFilter->applyOnExplicit() ? "explicit CTRL-J" : "" )
00470                   << endl;
00471   }
00472 }
00473 
00474 void KMFilterDlg::slotApplicableAccountsChanged()
00475 {
00476   if ( mFilter && mApplyOnForChecked->isEnabled() && mApplyOnForChecked->isChecked() ) {
00477     // Advanced tab functionality - Update list of accounts this filter applies to
00478     QListViewItemIterator it( mAccountList );
00479     while ( it.current() ) {
00480       QCheckListItem *item = dynamic_cast<QCheckListItem*>( it.current() );
00481       if (item) {
00482     int id = item->text( 2 ).toInt();
00483     mFilter->setApplyOnAccount( id, item->isOn() );
00484       }
00485       ++it;
00486     }
00487   }
00488 }
00489 
00490 void KMFilterDlg::slotStopProcessingButtonToggled( bool aChecked )
00491 {
00492   if ( mFilter )
00493     mFilter->setStopProcessingHere( aChecked );
00494 }
00495 
00496 void KMFilterDlg::slotConfigureShortcutButtonToggled( bool aChecked )
00497 {
00498   if ( mFilter ) {
00499     mFilter->setConfigureShortcut( aChecked );
00500     mConfigureToolbar->setEnabled( aChecked );
00501     mFilterActionIconButton->setEnabled( aChecked );
00502     mFilterActionLabel->setEnabled( aChecked );
00503   }
00504 }
00505 
00506 void KMFilterDlg::slotCapturedShortcutChanged( const KShortcut& sc )
00507 {
00508   KShortcut mySc(sc);
00509   if ( mySc == mKeyButton->shortcut() ) return;
00510   // FIXME work around a problem when reseting the shortcut via the shortcut dialog
00511   // somehow the returned shortcut does not evaluate to true in KShortcut::isNull(),
00512   // so we additionally have to check for an empty string
00513   if ( mySc.isNull() || mySc.toString().isEmpty() )
00514     mySc.clear();
00515   if ( !mySc.isNull() && !( kmkernel->getKMMainWidget()->shortcutIsValid( mySc ) ) ) {
00516     QString msg( i18n( "The selected shortcut is already used, "
00517           "please select a different one." ) );
00518     KMessageBox::sorry( this, msg );
00519   } else {
00520     mKeyButton->setShortcut( mySc, false );
00521     if ( mFilter )
00522       mFilter->setShortcut( mKeyButton->shortcut() );
00523   }
00524 }
00525 
00526 void KMFilterDlg::slotConfigureToolbarButtonToggled( bool aChecked )
00527 {
00528   if ( mFilter )
00529     mFilter->setConfigureToolbar( aChecked );
00530 }
00531 
00532 void KMFilterDlg::slotFilterActionIconChanged( QString icon )
00533 {
00534   if ( mFilter )
00535     mFilter->setIcon( icon );
00536 }
00537 
00538 void KMFilterDlg::slotUpdateAccountList()
00539 {
00540   mAccountList->clear();
00541   QListViewItem *top = 0;
00542   for( KMAccount *a = kmkernel->acctMgr()->first(); a!=0;
00543        a = kmkernel->acctMgr()->next() ) {
00544     QCheckListItem *listItem =
00545       new QCheckListItem( mAccountList, top, a->name(), QCheckListItem::CheckBox );
00546     listItem->setText( 1, a->type() );
00547     listItem->setText( 2, QString( "%1" ).arg( a->id() ) );
00548     if ( mFilter )
00549       listItem->setOn( mFilter->applyOnAccount( a->id() ) );
00550     top = listItem;
00551   }
00552 
00553   QListViewItem *listItem = mAccountList->firstChild();
00554   if ( listItem ) {
00555     mAccountList->setCurrentItem( listItem );
00556     mAccountList->setSelected( listItem, true );
00557   }
00558 }
00559 
00560 //=============================================================================
00561 //
00562 // class KMFilterListBox (the filter list manipulator)
00563 //
00564 //=============================================================================
00565 
00566 KMFilterListBox::KMFilterListBox( const QString & title, QWidget *parent, const char* name, bool popFilter )
00567   : QGroupBox( 1, Horizontal, title, parent, name ),
00568     bPopFilter(popFilter)
00569 {
00570   mFilterList.setAutoDelete(TRUE);
00571   mIdxSelItem = -1;
00572 
00573   //----------- the list box
00574   mListBox = new QListBox(this);
00575   mListBox->setMinimumWidth(150);
00576   QWhatsThis::add( mListBox, i18n(_wt_filterlist) );
00577 
00578   //----------- the first row of buttons
00579   QHBox *hb = new QHBox(this);
00580   hb->setSpacing(4);
00581   mBtnTop = new KPushButton( QString::null, hb );
00582   mBtnTop->setAutoRepeat( true );
00583   mBtnTop->setIconSet( BarIconSet( "top", KIcon::SizeSmall ) );
00584   mBtnTop->setMinimumSize( mBtnTop->sizeHint() * 1.2 );
00585   mBtnUp = new KPushButton( QString::null, hb );
00586   mBtnUp->setAutoRepeat( true );
00587   mBtnUp->setIconSet( BarIconSet( "up", KIcon::SizeSmall ) );
00588   mBtnUp->setMinimumSize( mBtnUp->sizeHint() * 1.2 );
00589   mBtnDown = new KPushButton( QString::null, hb );
00590   mBtnDown->setAutoRepeat( true );
00591   mBtnDown->setIconSet( BarIconSet( "down", KIcon::SizeSmall ) );
00592   mBtnDown->setMinimumSize( mBtnDown->sizeHint() * 1.2 );
00593   mBtnBot = new KPushButton( QString::null, hb );
00594   mBtnBot->setAutoRepeat( true );
00595   mBtnBot->setIconSet( BarIconSet( "bottom", KIcon::SizeSmall ) );
00596   mBtnBot->setMinimumSize( mBtnBot->sizeHint() * 1.2 );
00597   QToolTip::add( mBtnTop, i18n("Top") );
00598   QToolTip::add( mBtnUp, i18n("Up") );
00599   QToolTip::add( mBtnDown, i18n("Down") );
00600   QToolTip::add( mBtnBot, i18n("Bottom") );
00601   QWhatsThis::add( mBtnTop, i18n(_wt_filterlist_top) );
00602   QWhatsThis::add( mBtnUp, i18n(_wt_filterlist_up) );
00603   QWhatsThis::add( mBtnDown, i18n(_wt_filterlist_down) );
00604   QWhatsThis::add( mBtnBot, i18n(_wt_filterlist_bot) );
00605 
00606   //----------- the second row of buttons
00607   hb = new QHBox(this);
00608   hb->setSpacing(4);
00609   mBtnNew = new QPushButton( QString::null, hb );
00610   mBtnNew->setPixmap( BarIcon( "filenew", KIcon::SizeSmall ) );
00611   mBtnNew->setMinimumSize( mBtnNew->sizeHint() * 1.2 );
00612   mBtnCopy = new QPushButton( QString::null, hb );
00613   mBtnCopy->setPixmap( BarIcon( "editcopy", KIcon::SizeSmall ) );
00614   mBtnCopy->setMinimumSize( mBtnCopy->sizeHint() * 1.2 );
00615   mBtnDelete = new QPushButton( QString::null, hb );
00616   mBtnDelete->setPixmap( BarIcon( "editdelete", KIcon::SizeSmall ) );
00617   mBtnDelete->setMinimumSize( mBtnDelete->sizeHint() * 1.2 );
00618   mBtnRename = new QPushButton( i18n("Rename..."), hb );
00619   QToolTip::add( mBtnNew, i18n("New") );
00620   QToolTip::add( mBtnCopy, i18n("Copy") );
00621   QToolTip::add( mBtnDelete, i18n("Delete"));
00622   QWhatsThis::add( mBtnNew, i18n(_wt_filterlist_new) );
00623   QWhatsThis::add( mBtnCopy, i18n(_wt_filterlist_copy) );
00624   QWhatsThis::add( mBtnDelete, i18n(_wt_filterlist_delete) );
00625   QWhatsThis::add( mBtnRename, i18n(_wt_filterlist_rename) );
00626 
00627 
00628   //----------- now connect everything
00629   connect( mListBox, SIGNAL(highlighted(int)),
00630        this, SLOT(slotSelected(int)) );
00631   connect( mListBox, SIGNAL( doubleClicked ( QListBoxItem * )),
00632            this, SLOT( slotRename()) );
00633   connect( mBtnTop, SIGNAL(clicked()),
00634        this, SLOT(slotTop()) );
00635   connect( mBtnUp, SIGNAL(clicked()),
00636        this, SLOT(slotUp()) );
00637   connect( mBtnDown, SIGNAL(clicked()),
00638        this, SLOT(slotDown()) );
00639   connect( mBtnBot, SIGNAL(clicked()),
00640        this, SLOT(slotBottom()) );
00641   connect( mBtnNew, SIGNAL(clicked()),
00642        this, SLOT(slotNew()) );
00643   connect( mBtnCopy, SIGNAL(clicked()),
00644        this, SLOT(slotCopy()) );
00645   connect( mBtnDelete, SIGNAL(clicked()),
00646        this, SLOT(slotDelete()) );
00647   connect( mBtnRename, SIGNAL(clicked()),
00648        this, SLOT(slotRename()) );
00649 
00650   // the dialog should call loadFilterList()
00651   // when all signals are connected.
00652   enableControls();
00653 }
00654 
00655 
00656 void KMFilterListBox::createFilter( const QCString & field,
00657                     const QString & value )
00658 {
00659   KMSearchRule *newRule = KMSearchRule::createInstance( field, KMSearchRule::FuncContains, value );
00660 
00661   KMFilter *newFilter = new KMFilter(0, bPopFilter);
00662   newFilter->pattern()->append( newRule );
00663   newFilter->pattern()->setName( QString("<%1>:%2").arg( field ).arg( value) );
00664 
00665   KMFilterActionDesc *desc = (*kmkernel->filterActionDict())["transfer"];
00666   if ( desc )
00667     newFilter->actions()->append( desc->create() );
00668 
00669   insertFilter( newFilter );
00670   enableControls();
00671 }
00672 
00673 bool KMFilterListBox::showLaterMsgs()
00674 {
00675     return mShowLater;
00676 }
00677 
00678 void KMFilterListBox::slotUpdateFilterName()
00679 {
00680   KMSearchPattern *p = mFilterList.at(mIdxSelItem)->pattern();
00681   if ( !p ) return;
00682 
00683   QString shouldBeName = p->name();
00684   QString displayedName = mListBox->text( mIdxSelItem );
00685 
00686   if ( shouldBeName.stripWhiteSpace().isEmpty() ) {
00687     mFilterList.at(mIdxSelItem)->setAutoNaming( true );
00688   }
00689 
00690   if ( mFilterList.at(mIdxSelItem)->isAutoNaming() ) {
00691     // auto-naming of patterns
00692     if ( p->first() && !p->first()->field().stripWhiteSpace().isEmpty() )
00693       shouldBeName = QString( "<%1>: %2" ).arg( p->first()->field() ).arg( p->first()->contents() );
00694     else
00695       shouldBeName = "<" + i18n("unnamed") + ">";
00696     p->setName( shouldBeName );
00697   }
00698 
00699   if ( displayedName == shouldBeName ) return;
00700 
00701   mListBox->blockSignals(TRUE);
00702   mListBox->changeItem( shouldBeName, mIdxSelItem );
00703   mListBox->blockSignals(FALSE);
00704 }
00705 
00706 void KMFilterListBox::slotShowLaterToggled(bool aOn)
00707 {
00708   mShowLater = aOn;
00709 }
00710 
00711 void KMFilterListBox::slotApplyFilterChanges()
00712 {
00713   if ( mIdxSelItem >= 0 )
00714     slotSelected( mListBox->currentItem() );
00715 
00716   // by now all edit widgets should have written back
00717   // their widget's data into our filter list.
00718 
00719   KMFilterMgr *fm;
00720   if (bPopFilter)
00721     fm = kmkernel->popFilterMgr();
00722   else
00723     fm = kmkernel->filterMgr();
00724 
00725   QValueList<KMFilter*> newFilters;
00726   QStringList emptyFilters;
00727   QPtrListIterator<KMFilter> it( mFilterList );
00728   for ( it.toFirst() ; it.current() ; ++it ) {
00729     KMFilter *f = new KMFilter( **it ); // deep copy
00730     f->purify();
00731     if ( !f->isEmpty() )
00732       // the filter is valid:
00733       newFilters.append( f );
00734     else {
00735       // the filter is invalid:
00736       emptyFilters << f->name();
00737       delete f;
00738     }
00739   }
00740   if (bPopFilter)
00741     fm->setShowLaterMsgs(mShowLater);
00742 
00743   // block attemts to use filters (currently a no-op)
00744   fm->beginUpdate();
00745   fm->setFilters( newFilters );
00746   if (fm->atLeastOneOnlineImapFolderTarget()) {
00747     QString str = i18n("At least one filter targets a folder on an online "
00748                "IMAP account. Such filters will only be applied "
00749                "when manually filtering and when filtering "
00750                "incoming online IMAP mail.");
00751     KMessageBox::information( this, str, QString::null,
00752                   "filterDlgOnlineImapCheck" );
00753   }
00754   // allow usage of the filters again.
00755   fm->endUpdate();
00756   fm->writeConfig();
00757 
00758   // report on invalid filters:
00759   if ( !emptyFilters.empty() ) {
00760     QString msg = i18n("The following filters have not been saved because they "
00761                "were invalid (e.g. containing no actions or no search "
00762                "rules).");
00763     KMessageBox::informationList( 0, msg, emptyFilters, QString::null,
00764                   "ShowInvalidFilterWarning" );
00765   }
00766 }
00767 
00768 void KMFilterListBox::slotSelected( int aIdx )
00769 {
00770   mIdxSelItem = aIdx;
00771   // QPtrList::at(i) will return 0 if i is out of range.
00772   KMFilter *f = mFilterList.at(aIdx);
00773   if ( f )
00774     emit filterSelected( f );
00775   else
00776     emit resetWidgets();
00777   enableControls();
00778 }
00779 
00780 void KMFilterListBox::slotNew()
00781 {
00782   // just insert a new filter.
00783   insertFilter( new KMFilter(0, bPopFilter) );
00784   enableControls();
00785 }
00786 
00787 void KMFilterListBox::slotCopy()
00788 {
00789   if ( mIdxSelItem < 0 ) {
00790     kdDebug(5006) << "KMFilterListBox::slotCopy called while no filter is selected, ignoring." << endl;
00791     return;
00792   }
00793 
00794   // make sure that all changes are written to the filter before we copy it
00795   emit applyWidgets();
00796 
00797   KMFilter *filter = mFilterList.at( mIdxSelItem );
00798 
00799   // enableControls should make sure this method is
00800   // never called when no filter is selected.
00801   assert( filter );
00802 
00803   // inserts a copy of the current filter.
00804   insertFilter( new KMFilter( *filter ) );
00805   enableControls();
00806 }
00807 
00808 void KMFilterListBox::slotDelete()
00809 {
00810   if ( mIdxSelItem < 0 ) {
00811     kdDebug(5006) << "KMFilterListBox::slotDelete called while no filter is selected, ignoring." << endl;
00812     return;
00813   }
00814 
00815   int oIdxSelItem = mIdxSelItem;
00816   mIdxSelItem = -1;
00817   // unselect all
00818   mListBox->selectAll(FALSE);
00819   // broadcast that all widgets let go
00820   // of the filter
00821   emit resetWidgets();
00822 
00823   // remove the filter from both the filter list...
00824   mFilterList.remove( oIdxSelItem );
00825   // and the listbox
00826   mListBox->removeItem( oIdxSelItem );
00827 
00828   int count = (int)mListBox->count();
00829   // and set the new current item.
00830   if ( count > oIdxSelItem )
00831     // oIdxItem is still a valid index
00832     mListBox->setSelected( oIdxSelItem, TRUE );
00833   else if ( count )
00834     // oIdxSelIdx is no longer valid, but the
00835     // list box isn't empty
00836     mListBox->setSelected( count - 1, TRUE );
00837   // the list is empty - keep index -1
00838 
00839   enableControls();
00840 }
00841 
00842 void KMFilterListBox::slotTop()
00843 {
00844   if ( mIdxSelItem < 0 ) {
00845     kdDebug(5006) << "KMFilterListBox::slotTop called while no filter is selected, ignoring." << endl;
00846     return;
00847   }
00848   if ( mIdxSelItem == 0 ) {
00849     kdDebug(5006) << "KMFilterListBox::slotTop called while the _topmost_ filter is selected, ignoring." << endl;
00850     return;
00851   }
00852 
00853   swapFilters( mIdxSelItem, 0 );
00854   enableControls();
00855 }
00856 
00857 void KMFilterListBox::slotUp()
00858 {
00859   if ( mIdxSelItem < 0 ) {
00860     kdDebug(5006) << "KMFilterListBox::slotUp called while no filter is selected, ignoring." << endl;
00861     return;
00862   }
00863   if ( mIdxSelItem == 0 ) {
00864     kdDebug(5006) << "KMFilterListBox::slotUp called while the _topmost_ filter is selected, ignoring." << endl;
00865     return;
00866   }
00867 
00868   swapNeighbouringFilters( mIdxSelItem, mIdxSelItem - 1 );
00869   enableControls();
00870 }
00871 
00872 void KMFilterListBox::slotDown()
00873 {
00874   if ( mIdxSelItem < 0 ) {
00875     kdDebug(5006) << "KMFilterListBox::slotDown called while no filter is selected, ignoring." << endl;
00876     return;
00877   }
00878   if ( mIdxSelItem == (int)mListBox->count() - 1 ) {
00879     kdDebug(5006) << "KMFilterListBox::slotDown called while the _last_ filter is selected, ignoring." << endl;
00880     return;
00881   }
00882 
00883   swapNeighbouringFilters( mIdxSelItem, mIdxSelItem + 1);
00884   enableControls();
00885 }
00886 
00887 void KMFilterListBox::slotBottom()
00888 {
00889   if ( mIdxSelItem < 0 ) {
00890     kdDebug(5006) << "KMFilterListBox::slotBottom called while no filter is selected, ignoring." << endl;
00891     return;
00892   }
00893   if ( mIdxSelItem == (int)mListBox->count() - 1 ) {
00894     kdDebug(5006) << "KMFilterListBox::slotBottom called while the _last_ filter is selected, ignoring." << endl;
00895     return;
00896   }
00897 
00898   swapFilters( mIdxSelItem, mListBox->count()-1 );
00899   enableControls();
00900 }
00901 
00902 void KMFilterListBox::slotRename()
00903 {
00904   if ( mIdxSelItem < 0 ) {
00905     kdDebug(5006) << "KMFilterListBox::slotRename called while no filter is selected, ignoring." << endl;
00906     return;
00907   }
00908 
00909   bool okPressed = FALSE;
00910   KMFilter *filter = mFilterList.at( mIdxSelItem );
00911 
00912   // enableControls should make sure this method is
00913   // never called when no filter is selected.
00914   assert( filter );
00915 
00916   // allow empty names - those will turn auto-naming on again
00917   QValidator *validator = new QRegExpValidator( QRegExp( ".*" ), 0 );
00918   QString newName = KInputDialog::getText
00919     (
00920      i18n("Rename Filter"),
00921      i18n("Rename filter \"%1\" to:\n(leave the field empty for automatic naming)")
00922         .arg( filter->pattern()->name() ) /*label*/,
00923      filter->pattern()->name() /* initial value */,
00924      &okPressed, topLevelWidget(), 0, validator
00925      );
00926   delete validator;
00927 
00928   if ( !okPressed ) return;
00929 
00930   if ( newName.isEmpty() ) {
00931     // bait for slotUpdateFilterName to
00932     // use automatic naming again.
00933     filter->pattern()->setName( "<>" );
00934     filter->setAutoNaming( true );
00935   } else {
00936     filter->pattern()->setName( newName );
00937     filter->setAutoNaming( false );
00938   }
00939 
00940   slotUpdateFilterName();
00941 }
00942 
00943 void KMFilterListBox::enableControls()
00944 {
00945   bool theFirst = ( mIdxSelItem == 0 );
00946   bool theLast = ( mIdxSelItem >= (int)mFilterList.count() - 1 );
00947   bool aFilterIsSelected = ( mIdxSelItem >= 0 );
00948 
00949   mBtnTop->setEnabled( aFilterIsSelected && !theFirst );
00950   mBtnUp->setEnabled( aFilterIsSelected && !theFirst );
00951   mBtnDown->setEnabled( aFilterIsSelected && !theLast );
00952   mBtnBot->setEnabled( aFilterIsSelected && !theLast );
00953   mBtnCopy->setEnabled( aFilterIsSelected );
00954   mBtnDelete->setEnabled( aFilterIsSelected );
00955   mBtnRename->setEnabled( aFilterIsSelected );
00956 
00957   if ( aFilterIsSelected )
00958     mListBox->ensureCurrentVisible();
00959 }
00960 
00961 void KMFilterListBox::loadFilterList( bool createDummyFilter )
00962 {
00963   assert(mListBox);
00964   setEnabled(FALSE);
00965   // we don't want the insertion to
00966   // cause flicker in the edit widgets.
00967   blockSignals(TRUE);
00968 
00969   // clear both lists
00970   mFilterList.clear();
00971   mListBox->clear();
00972 
00973   const KMFilterMgr *manager = 0;
00974   if(bPopFilter)
00975   {
00976     mShowLater = kmkernel->popFilterMgr()->showLaterMsgs();
00977     manager = kmkernel->popFilterMgr();
00978   }
00979   else
00980   {
00981     manager = kmkernel->filterMgr();
00982   }
00983   Q_ASSERT( manager );
00984 
00985   QValueListConstIterator<KMFilter*> it;
00986   for ( it = manager->filters().constBegin() ; it != manager->filters().constEnd() ; ++it ) {
00987     mFilterList.append( new KMFilter( **it ) ); // deep copy
00988     mListBox->insertItem( (*it)->pattern()->name() );
00989   }
00990 
00991   blockSignals(FALSE);
00992   setEnabled(TRUE);
00993 
00994   // create an empty filter when there's none, to avoid a completely
00995   // disabled dialog (usability tests indicated that the new-filter
00996   // button is too hard to find that way):
00997   if ( !mListBox->count() && createDummyFilter )
00998     slotNew();
00999 
01000   if ( mListBox->count() > 0 )
01001     mListBox->setSelected( 0, true );
01002 
01003   enableControls();
01004 }
01005 
01006 void KMFilterListBox::insertFilter( KMFilter* aFilter )
01007 {
01008   // must be really a filter...
01009   assert( aFilter );
01010 
01011   // if mIdxSelItem < 0, QListBox::insertItem will append.
01012   mListBox->insertItem( aFilter->pattern()->name(), mIdxSelItem );
01013   if ( mIdxSelItem < 0 ) {
01014     // none selected -> append
01015     mFilterList.append( aFilter );
01016     mListBox->setSelected( mListBox->count() - 1, TRUE );
01017     //    slotSelected( mListBox->count() - 1 );
01018   } else {
01019     // insert just before selected
01020     mFilterList.insert( mIdxSelItem, aFilter );
01021     mListBox->setSelected( mIdxSelItem, TRUE );
01022     //    slotSelected( mIdxSelItem );
01023   }
01024 
01025 }
01026 
01027 void KMFilterListBox::swapNeighbouringFilters( int untouchedOne, int movedOne )
01028 {
01029   // must be neighbours...
01030   assert( untouchedOne - movedOne == 1 || movedOne - untouchedOne == 1 );
01031 
01032   // untouchedOne is at idx. to move it down(up),
01033   // remove item at idx+(-)1 w/o deleting it.
01034   QListBoxItem *item = mListBox->item( movedOne );
01035   mListBox->takeItem( item );
01036   // now selected item is at idx(idx-1), so
01037   // insert the other item at idx, ie. above(below).
01038   mListBox->insertItem( item, untouchedOne );
01039 
01040   KMFilter* filter = mFilterList.take( movedOne );
01041   mFilterList.insert( untouchedOne, filter );
01042 
01043   mIdxSelItem += movedOne - untouchedOne;
01044 }
01045 
01046 void KMFilterListBox::swapFilters( int from, int to )
01047 {
01048   QListBoxItem *item = mListBox->item( from );
01049   mListBox->takeItem( item );
01050   mListBox->insertItem( item, to );
01051 
01052   KMFilter* filter = mFilterList.take( from );
01053   mFilterList.insert( to, filter );
01054 
01055   mIdxSelItem = to;
01056   mListBox->setCurrentItem( mIdxSelItem );
01057   mListBox->setSelected( mIdxSelItem, true );
01058 }
01059 
01060 //=============================================================================
01061 //
01062 // class KMFilterActionWidget
01063 //
01064 //=============================================================================
01065 
01066 KMFilterActionWidget::KMFilterActionWidget( QWidget *parent, const char* name )
01067   : QHBox( parent, name )
01068 {
01069   int i;
01070   mActionList.setAutoDelete(TRUE);
01071 
01072   mComboBox = new QComboBox( FALSE, this );
01073   assert( mComboBox );
01074   mWidgetStack = new QWidgetStack(this);
01075   assert( mWidgetStack );
01076 
01077   setSpacing( 4 );
01078 
01079   QPtrListIterator<KMFilterActionDesc> it ( kmkernel->filterActionDict()->list() );
01080   for ( i=0, it.toFirst() ; it.current() ; ++it, ++i ) {
01081     //create an instance:
01082     KMFilterAction *a = (*it)->create();
01083     // append to the list of actions:
01084     mActionList.append( a );
01085     // add parameter widget to widget stack:
01086     mWidgetStack->addWidget( a->createParamWidget( mWidgetStack ), i );
01087     // add (i18n-ized) name to combo box
01088     mComboBox->insertItem( (*it)->label );
01089   }
01090   // widget for the case where no action is selected.
01091   mWidgetStack->addWidget( new QLabel( i18n("Please select an action."), mWidgetStack ), i );
01092   mWidgetStack->raiseWidget(i);
01093   mComboBox->insertItem( " " );
01094   mComboBox->setCurrentItem(i);
01095 
01096   // don't show scroll bars.
01097   mComboBox->setSizeLimit( mComboBox->count() );
01098   // layout management:
01099   // o the combo box is not to be made larger than it's sizeHint(),
01100   //   the parameter widget should grow instead.
01101   // o the whole widget takes all space horizontally, but is fixed vertically.
01102   mComboBox->adjustSize();
01103   mComboBox->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
01104   setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed ) );
01105   updateGeometry();
01106 
01107   // redirect focus to the filter action combo box
01108   setFocusProxy( mComboBox );
01109 
01110   // now connect the combo box and the widget stack
01111   connect( mComboBox, SIGNAL(activated(int)),
01112        mWidgetStack, SLOT(raiseWidget(int)) );
01113 }
01114 
01115 void KMFilterActionWidget::setAction( const KMFilterAction* aAction )
01116 {
01117   int i=0;
01118   bool found = FALSE;
01119   int count = mComboBox->count() - 1 ; // last entry is the empty one
01120   QString label = ( aAction ) ? aAction->label() : QString::null ;
01121 
01122   // find the index of typeOf(aAction) in mComboBox
01123   // and clear the other widgets on the way.
01124   for ( ; i < count ; i++ )
01125     if ( aAction && mComboBox->text(i) == label ) {
01126       //...set the parameter widget to the settings
01127       // of aAction...
01128       aAction->setParamWidgetValue( mWidgetStack->widget(i) );
01129       //...and show the correct entry of
01130       // the combo box
01131       mComboBox->setCurrentItem(i); // (mm) also raise the widget, but doesn't
01132       mWidgetStack->raiseWidget(i);
01133       found = TRUE;
01134     } else // clear the parameter widget
01135       mActionList.at(i)->clearParamWidget( mWidgetStack->widget(i) );
01136   if ( found ) return;
01137 
01138   // not found, so set the empty widget
01139   mComboBox->setCurrentItem( count ); // last item
01140   mWidgetStack->raiseWidget( count) ;
01141 }
01142 
01143 KMFilterAction * KMFilterActionWidget::action()
01144 {
01145   // look up the action description via the label
01146   // returned by QComboBox::currentText()...
01147   KMFilterActionDesc *desc = (*kmkernel->filterActionDict())[ mComboBox->currentText() ];
01148   if ( desc ) {
01149     // ...create an instance...
01150     KMFilterAction *fa = desc->create();
01151     if ( fa ) {
01152       // ...and apply the setting of the parameter widget.
01153       fa->applyParamWidgetValue( mWidgetStack->visibleWidget() );
01154       return fa;
01155     }
01156   }
01157 
01158   return 0;
01159 }
01160 
01161 //=============================================================================
01162 //
01163 // class KMFilterActionWidgetLister (the filter action editor)
01164 //
01165 //=============================================================================
01166 
01167 KMFilterActionWidgetLister::KMFilterActionWidgetLister( QWidget *parent, const char* name )
01168   : KWidgetLister( 1, FILTER_MAX_ACTIONS, parent, name )
01169 {
01170   mActionList = 0;
01171 }
01172 
01173 KMFilterActionWidgetLister::~KMFilterActionWidgetLister()
01174 {
01175 }
01176 
01177 void KMFilterActionWidgetLister::setActionList( QPtrList<KMFilterAction> *aList )
01178 {
01179   assert ( aList );
01180 
01181   if ( mActionList )
01182     regenerateActionListFromWidgets();
01183 
01184   mActionList = aList;
01185 
01186   ((QWidget*)parent())->setEnabled( TRUE );
01187 
01188   if ( aList->count() == 0 ) {
01189     slotClear();
01190     return;
01191   }
01192 
01193   int superfluousItems = (int)mActionList->count() - mMaxWidgets ;
01194   if ( superfluousItems > 0 ) {
01195     kdDebug(5006) << "KMFilterActionWidgetLister: Clipping action list to "
01196           << mMaxWidgets << " items!" << endl;
01197 
01198     for ( ; superfluousItems ; superfluousItems-- )
01199       mActionList->removeLast();
01200   }
01201 
01202   // set the right number of widgets
01203   setNumberOfShownWidgetsTo( mActionList->count() );
01204 
01205   // load the actions into the widgets
01206   QPtrListIterator<KMFilterAction> aIt( *mActionList );
01207   QPtrListIterator<QWidget> wIt( mWidgetList );
01208   for ( aIt.toFirst(), wIt.toFirst() ;
01209     aIt.current() && wIt.current() ; ++aIt, ++wIt )
01210     ((KMFilterActionWidget*)(*wIt))->setAction( (*aIt) );
01211 }
01212 
01213 void KMFilterActionWidgetLister::reset()
01214 {
01215   if ( mActionList )
01216     regenerateActionListFromWidgets();
01217 
01218   mActionList = 0;
01219   slotClear();
01220   ((QWidget*)parent())->setEnabled( FALSE );
01221 }
01222 
01223 QWidget* KMFilterActionWidgetLister::createWidget( QWidget *parent )
01224 {
01225   return new KMFilterActionWidget(parent);
01226 }
01227 
01228 void KMFilterActionWidgetLister::clearWidget( QWidget *aWidget )
01229 {
01230   if ( aWidget )
01231     ((KMFilterActionWidget*)aWidget)->setAction(0);
01232 }
01233 
01234 void KMFilterActionWidgetLister::regenerateActionListFromWidgets()
01235 {
01236   if ( !mActionList ) return;
01237 
01238   mActionList->clear();
01239 
01240   QPtrListIterator<QWidget> it( mWidgetList );
01241   for ( it.toFirst() ; it.current() ; ++it ) {
01242     KMFilterAction *a = ((KMFilterActionWidget*)(*it))->action();
01243     if ( a )
01244       mActionList->append( a );
01245   }
01246 
01247 }
01248 
01249 //=============================================================================
01250 //
01251 // class KMPopFilterActionWidget
01252 //
01253 //=============================================================================
01254 
01255 KMPopFilterActionWidget::KMPopFilterActionWidget( const QString& title, QWidget *parent, const char* name )
01256   : QVButtonGroup( title, parent, name )
01257 {
01258   mActionMap[Down] = new QRadioButton( i18n("&Download mail"), this );
01259   mActionMap[Later] = new QRadioButton( i18n("Download mail la&ter"), this );
01260   mActionMap[Delete] = new QRadioButton( i18n("D&elete mail from server"), this );
01261   mIdMap[id(mActionMap[Later])] = Later;
01262   mIdMap[id(mActionMap[Down])] = Down;
01263   mIdMap[id(mActionMap[Delete])] = Delete;
01264 
01265   connect( this, SIGNAL(clicked(int)),
01266        this, SLOT( slotActionClicked(int)) );
01267 }
01268 
01269 void KMPopFilterActionWidget::setAction( KMPopFilterAction aAction )
01270 {
01271   if( aAction == NoAction)
01272   {
01273     aAction = Later;
01274   }
01275 
01276   mAction = aAction;
01277 
01278   blockSignals( true );
01279   if(!mActionMap[aAction]->isChecked())
01280   {
01281     mActionMap[aAction]->setChecked(true);
01282   }
01283   blockSignals( false );
01284 
01285   setEnabled(true);
01286 }
01287 
01288 KMPopFilterAction  KMPopFilterActionWidget::action()
01289 {
01290   return mAction;
01291 }
01292 
01293 void KMPopFilterActionWidget::slotActionClicked(int aId)
01294 {
01295   emit actionChanged(mIdMap[aId]);
01296   setAction(mIdMap[aId]);
01297 }
01298 
01299 void KMPopFilterActionWidget::reset()
01300 {
01301   blockSignals(TRUE);
01302   mActionMap[Down]->setChecked( TRUE );
01303   blockSignals(FALSE);
01304 
01305   setEnabled( FALSE );
01306 }
01307 
01308 #include "kmfilterdlg.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys