00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "configspamcheck.h"
00019
00020
00021 K_PLUGIN_FACTORY( ConfigSpamcheckFactory, registerPlugin<ConfigSpamcheck>(); )
00022 K_EXPORT_PLUGIN( ConfigSpamcheckFactory( "kcm_kshowmailconfigspamcheck" ) )
00023
00024 ConfigSpamcheck::ConfigSpamcheck( QWidget * parent, const QVariantList & args )
00025 : KCModule( ConfigSpamcheckFactory::componentData(), parent, args )
00026 {
00027
00028
00029
00030
00031 QVBoxLayout* layMain = new QVBoxLayout( this );
00032
00033
00034 QLabel* lblDescription = new QLabel( this );
00035 lblDescription->setAlignment( Qt::AlignJustify );
00036 lblDescription->setText( QString( "<i>%1</i>" ).arg( i18n( "KShowmail uses SpamAssassin to check the mails for spam. You have to install, configure and start the SpamAssassin daemon, before you can use this service." ) ) );
00037 lblDescription->setWordWrap( true );
00038 lblDescription->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Maximum );
00039 layMain->addWidget( lblDescription );
00040
00041
00042 btnTest = new KPushButton( KStandardGuiItem::test(), this );
00043 btnTest->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );
00044 layMain->addWidget( btnTest );
00045 connect( btnTest, SIGNAL( clicked() ), this, SLOT( slotTestSpamAssassin() ) );
00046
00047
00048 gboxAction = new QGroupBox( i18n( "Action for Spam" ), this );
00049 QHBoxLayout* layAction = new QHBoxLayout();
00050 gboxAction->setLayout( layAction );
00051 layMain->addWidget( gboxAction );
00052
00053 cmbAction = new KComboBox( gboxAction );
00054 layAction->addWidget( cmbAction );
00055 cmbAction->setToolTip( i18n( "Choose the action for spam mails." ) );
00056 connect( cmbAction, SIGNAL( activated( int ) ), this, SLOT( slotActionChanged( int ) ) );
00057 connect( cmbAction, SIGNAL( activated( int ) ), this, SLOT( slotChanged() ) );
00058
00059
00060 cmbAction->insertItem( ID_SPAM_ACTION_BUTTONS_DELETE, i18n( "Delete" ) );
00061 cmbAction->insertItem( ID_SPAM_ACTION_BUTTONS_MARK, i18n( "Mark" ) );
00062 cmbAction->insertItem( ID_SPAM_ACTION_BUTTONS_MOVE, i18n( "Move" ) );
00063
00064
00065 txtMailbox = new KLineEdit( gboxAction );
00066 layAction->addWidget( txtMailbox );
00067 connect( txtMailbox, SIGNAL( textChanged( const QString & ) ), this, SLOT( slotChanged() ) );
00068
00069
00070 btnMailboxWizard= new KPushButton( KGuiItem( QString(), "tools-wizard" ), gboxAction );
00071 layAction->addWidget( btnMailboxWizard );
00072 btnMailboxWizard->setToolTip( i18n( "Choose the mailbox" ) );
00073 connect( btnMailboxWizard, SIGNAL( clicked() ), this, SLOT( slotOpenMailBoxWizard() ) );
00074
00075
00076 switch( DEFAULT_SPAMCHECK_ACTION )
00077 {
00078 case CONFIG_VALUE_SPAMCHECK_ACTION_DELETE : cmbAction->setCurrentIndex( ID_SPAM_ACTION_BUTTONS_DELETE ); break;
00079 case CONFIG_VALUE_SPAMCHECK_ACTION_MARK : cmbAction->setCurrentIndex( ID_SPAM_ACTION_BUTTONS_MARK ); break;
00080 case CONFIG_VALUE_SPAMCHECK_ACTION_MOVE : cmbAction->setCurrentIndex( ID_SPAM_ACTION_BUTTONS_MOVE ); break;
00081 }
00082
00083 txtMailbox->setText( DEFAULT_SPAMCHECK_ACTION_MOVE_MAILBOX );
00084
00085
00086 slotActionChanged( cmbAction->currentIndex() );
00087 gboxAction->setEnabled( isSpamAssassinRunning() );
00088
00089 setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
00090
00091
00092 config = KGlobal::config();
00093
00094
00095 load();
00096 }
00097
00098 ConfigSpamcheck::~ConfigSpamcheck()
00099 {
00100 }
00101
00102 void ConfigSpamcheck::load()
00103 {
00104 KConfigGroup* configSpam = new KConfigGroup( config, CONFIG_GROUP_SPAMCHECK );
00105
00106
00107 switch( configSpam->readEntry( CONFIG_ENTRY_SPAMCHECK_ACTION, DEFAULT_SPAMCHECK_ACTION ) )
00108 {
00109 case CONFIG_VALUE_SPAMCHECK_ACTION_DELETE : cmbAction->setCurrentIndex( ID_SPAM_ACTION_BUTTONS_DELETE ); break;
00110 case CONFIG_VALUE_SPAMCHECK_ACTION_MARK : cmbAction->setCurrentIndex( ID_SPAM_ACTION_BUTTONS_MARK ); break;
00111 case CONFIG_VALUE_SPAMCHECK_ACTION_MOVE : cmbAction->setCurrentIndex( ID_SPAM_ACTION_BUTTONS_MOVE ); break;
00112 }
00113
00114
00115 if( configSpam->readEntry( CONFIG_ENTRY_SPAMCHECK_ACTION, DEFAULT_SPAMCHECK_ACTION ) == CONFIG_VALUE_SPAMCHECK_ACTION_MOVE )
00116 txtMailbox->setText( configSpam->readEntry( CONFIG_ENTRY_SPAMCHECK_MOVE_MAILBOX, DEFAULT_SPAMCHECK_ACTION_MOVE_MAILBOX ) );
00117 else
00118 txtMailbox->clear();
00119
00120
00121 slotActionChanged( cmbAction->currentIndex() );
00122 }
00123
00124 void ConfigSpamcheck::defaults()
00125 {
00126 switch( DEFAULT_SPAMCHECK_ACTION )
00127 {
00128 case CONFIG_VALUE_SPAMCHECK_ACTION_DELETE : cmbAction->setCurrentIndex( ID_SPAM_ACTION_BUTTONS_DELETE ); break;
00129 case CONFIG_VALUE_SPAMCHECK_ACTION_MARK : cmbAction->setCurrentIndex( ID_SPAM_ACTION_BUTTONS_MARK ); break;
00130 case CONFIG_VALUE_SPAMCHECK_ACTION_MOVE : cmbAction->setCurrentIndex( ID_SPAM_ACTION_BUTTONS_MOVE ); break;
00131 }
00132
00133 if( DEFAULT_SPAMCHECK_ACTION == CONFIG_VALUE_SPAMCHECK_ACTION_MOVE )
00134 txtMailbox->setText( QString( DEFAULT_SPAMCHECK_ACTION_MOVE_MAILBOX ) );
00135 else
00136 txtMailbox->clear();
00137
00138
00139 slotActionChanged( cmbAction->currentIndex() );
00140
00141 slotChanged();
00142 }
00143
00144 void ConfigSpamcheck::save()
00145 {
00146 KConfigGroup* configSpam = new KConfigGroup( config, CONFIG_GROUP_SPAMCHECK );
00147
00148
00149 switch( cmbAction->currentIndex() )
00150 {
00151 case ID_SPAM_ACTION_BUTTONS_DELETE : configSpam->writeEntry( CONFIG_ENTRY_SPAMCHECK_ACTION, CONFIG_VALUE_SPAMCHECK_ACTION_DELETE ); break;
00152 case ID_SPAM_ACTION_BUTTONS_MARK : configSpam->writeEntry( CONFIG_ENTRY_SPAMCHECK_ACTION, CONFIG_VALUE_SPAMCHECK_ACTION_MARK ); break;
00153 case ID_SPAM_ACTION_BUTTONS_MOVE : configSpam->writeEntry( CONFIG_ENTRY_SPAMCHECK_ACTION, CONFIG_VALUE_SPAMCHECK_ACTION_MOVE ); break;
00154 default : configSpam->writeEntry( CONFIG_ENTRY_SPAMCHECK_ACTION, DEFAULT_SPAMCHECK_ACTION ); break;
00155 }
00156
00157
00158 if( cmbAction->currentIndex() == ID_SPAM_ACTION_BUTTONS_MOVE )
00159 {
00160 configSpam->writeEntry( CONFIG_ENTRY_SPAMCHECK_MOVE_MAILBOX, txtMailbox->text() );
00161 }
00162 else
00163 {
00164 configSpam->deleteEntry( CONFIG_ENTRY_SPAMCHECK_MOVE_MAILBOX );
00165 }
00166
00167 config->sync();
00168 }
00169
00170 void ConfigSpamcheck::slotChanged()
00171 {
00172 KCModule::changed();
00173 }
00174
00175 void ConfigSpamcheck::slotActionChanged( int index )
00176 {
00177 switch( index )
00178 {
00179 case ID_SPAM_ACTION_BUTTONS_MOVE : txtMailbox->setEnabled( true );
00180 if( txtMailbox->text().isEmpty() )
00181 txtMailbox->setText( DEFAULT_FILTER_ACTION_MOVE_MAILBOX );
00182 btnMailboxWizard->setHidden( false );
00183 break;
00184 default : txtMailbox->setEnabled( false );
00185 btnMailboxWizard->setHidden( true );
00186 break;
00187 }
00188 }
00189
00190 void ConfigSpamcheck::slotOpenMailBoxWizard( )
00191 {
00192 QPointer<MailBoxWizard> wizard = new MailBoxWizard( this );
00193 wizard->setWindowTitle( i18n( "Mailbox Select" ) );
00194 int res = wizard->exec();
00195
00196 if( res == QDialog::Accepted )
00197 txtMailbox->setText( wizard->getPath() );
00198
00199 delete wizard;
00200 }
00201
00202 bool ConfigSpamcheck::isSpamAssassinRunning( ) const
00203 {
00204 FILE *read_fp;
00205 char buffer[ BUFSIZ + 1 ];
00206 int chars_read;
00207 bool found = false;
00208
00209 memset( buffer, '\0', sizeof( buffer ) );
00210 read_fp = popen( "sa-check_spamd", "r" );
00211 if( read_fp != NULL )
00212 {
00213 chars_read = fread( buffer, sizeof( char ), BUFSIZ, read_fp );
00214 while( chars_read > 0 )
00215 {
00216 buffer[ chars_read - 1 ] = '\0';
00217 QString output( buffer );
00218 found = output.contains( "SPAMD OK" ) > 0;
00219 if( found ) return found;
00220 chars_read = fread( buffer, sizeof( char ), BUFSIZ, read_fp );
00221 }
00222 pclose( read_fp );
00223 }
00224
00225 return found;
00226 }
00227
00228 void ConfigSpamcheck::slotTestSpamAssassin( )
00229 {
00230 if( isSpamAssassinRunning() )
00231 {
00232 KMessageBox::information( this, i18n( "SpamAssassin is running." ), i18n( "Check for SpamAssassin" ) );
00233 gboxAction->setEnabled( true );
00234 }
00235 else
00236 {
00237 KMessageBox::information( this, i18n( "SpamAssassin is not running." ), i18n( "Check for SpamAssassin" ) );
00238 gboxAction->setEnabled( false );
00239 }
00240 }
00241
00242 #include "configspamcheck.moc"