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->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Maximum );
00038 layMain->addWidget( lblDescription );
00039
00040
00041 btnTest = new KPushButton( KStandardGuiItem::test(), this );
00042 btnTest->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );
00043 layMain->addWidget( btnTest );
00044 connect( btnTest, SIGNAL( clicked() ), this, SLOT( slotTestSpamAssassin() ) );
00045
00046
00047 gboxAction = new QGroupBox( i18n( "Action for Spam" ), this );
00048 QHBoxLayout* layAction = new QHBoxLayout();
00049 gboxAction->setLayout( layAction );
00050 layMain->addWidget( gboxAction );
00051
00052 cmbAction = new KComboBox( gboxAction );
00053 layAction->addWidget( cmbAction );
00054 cmbAction->setToolTip( i18n( "Choose the action for spam mails." ) );
00055 connect( cmbAction, SIGNAL( activated( int ) ), this, SLOT( slotActionChanged( int ) ) );
00056 connect( cmbAction, SIGNAL( activated( int ) ), this, SLOT( slotChanged() ) );
00057
00058
00059 cmbAction->insertItem( ID_SPAM_ACTION_BUTTONS_DELETE, i18n( "Delete" ) );
00060 cmbAction->insertItem( ID_SPAM_ACTION_BUTTONS_MARK, i18n( "Mark" ) );
00061 cmbAction->insertItem( ID_SPAM_ACTION_BUTTONS_MOVE, i18n( "Move" ) );
00062
00063
00064 txtMailbox = new KLineEdit( gboxAction );
00065 layAction->addWidget( txtMailbox );
00066 connect( txtMailbox, SIGNAL( textChanged( const QString & ) ), this, SLOT( slotChanged() ) );
00067
00068
00069 btnMailboxWizard= new KPushButton( KGuiItem( QString(), "tools-wizard" ), gboxAction );
00070 layAction->addWidget( btnMailboxWizard );
00071 btnMailboxWizard->setToolTip( i18n( "Choose the mailbox" ) );
00072 connect( btnMailboxWizard, SIGNAL( clicked() ), this, SLOT( slotOpenMailBoxWizard() ) );
00073
00074
00075 switch( DEFAULT_SPAMCHECK_ACTION )
00076 {
00077 case CONFIG_VALUE_SPAMCHECK_ACTION_DELETE : cmbAction->setCurrentIndex( ID_SPAM_ACTION_BUTTONS_DELETE ); break;
00078 case CONFIG_VALUE_SPAMCHECK_ACTION_MARK : cmbAction->setCurrentIndex( ID_SPAM_ACTION_BUTTONS_MARK ); break;
00079 case CONFIG_VALUE_SPAMCHECK_ACTION_MOVE : cmbAction->setCurrentIndex( ID_SPAM_ACTION_BUTTONS_MOVE ); break;
00080 }
00081
00082 txtMailbox->setText( DEFAULT_SPAMCHECK_ACTION_MOVE_MAILBOX );
00083
00084
00085 slotActionChanged( cmbAction->currentIndex() );
00086 gboxAction->setEnabled( isSpamAssassinRunning() );
00087
00088
00089 config = KGlobal::config();
00090
00091
00092 load();
00093 }
00094
00095 ConfigSpamcheck::~ConfigSpamcheck()
00096 {
00097 }
00098
00099 void ConfigSpamcheck::load()
00100 {
00101 KConfigGroup* configSpam = new KConfigGroup( config, CONFIG_GROUP_SPAMCHECK );
00102
00103
00104 switch( configSpam->readEntry( CONFIG_ENTRY_SPAMCHECK_ACTION, DEFAULT_SPAMCHECK_ACTION ) )
00105 {
00106 case CONFIG_VALUE_SPAMCHECK_ACTION_DELETE : cmbAction->setCurrentIndex( ID_SPAM_ACTION_BUTTONS_DELETE ); break;
00107 case CONFIG_VALUE_SPAMCHECK_ACTION_MARK : cmbAction->setCurrentIndex( ID_SPAM_ACTION_BUTTONS_MARK ); break;
00108 case CONFIG_VALUE_SPAMCHECK_ACTION_MOVE : cmbAction->setCurrentIndex( ID_SPAM_ACTION_BUTTONS_MOVE ); break;
00109 }
00110
00111
00112 if( configSpam->readEntry( CONFIG_ENTRY_SPAMCHECK_ACTION, DEFAULT_SPAMCHECK_ACTION ) == CONFIG_VALUE_SPAMCHECK_ACTION_MOVE )
00113 txtMailbox->setText( configSpam->readEntry( CONFIG_ENTRY_SPAMCHECK_MOVE_MAILBOX, DEFAULT_SPAMCHECK_ACTION_MOVE_MAILBOX ) );
00114 else
00115 txtMailbox->clear();
00116
00117
00118 slotActionChanged( cmbAction->currentIndex() );
00119 }
00120
00121 void ConfigSpamcheck::defaults()
00122 {
00123 switch( DEFAULT_SPAMCHECK_ACTION )
00124 {
00125 case CONFIG_VALUE_SPAMCHECK_ACTION_DELETE : cmbAction->setCurrentIndex( ID_SPAM_ACTION_BUTTONS_DELETE ); break;
00126 case CONFIG_VALUE_SPAMCHECK_ACTION_MARK : cmbAction->setCurrentIndex( ID_SPAM_ACTION_BUTTONS_MARK ); break;
00127 case CONFIG_VALUE_SPAMCHECK_ACTION_MOVE : cmbAction->setCurrentIndex( ID_SPAM_ACTION_BUTTONS_MOVE ); break;
00128 }
00129
00130 if( DEFAULT_SPAMCHECK_ACTION == CONFIG_VALUE_SPAMCHECK_ACTION_MOVE )
00131 txtMailbox->setText( QString( DEFAULT_SPAMCHECK_ACTION_MOVE_MAILBOX ) );
00132 else
00133 txtMailbox->clear();
00134
00135
00136 slotActionChanged( cmbAction->currentIndex() );
00137
00138 slotChanged();
00139 }
00140
00141 void ConfigSpamcheck::save()
00142 {
00143 KConfigGroup* configSpam = new KConfigGroup( config, CONFIG_GROUP_SPAMCHECK );
00144
00145
00146 switch( cmbAction->currentIndex() )
00147 {
00148 case ID_SPAM_ACTION_BUTTONS_DELETE : configSpam->writeEntry( CONFIG_ENTRY_SPAMCHECK_ACTION, CONFIG_VALUE_SPAMCHECK_ACTION_DELETE ); break;
00149 case ID_SPAM_ACTION_BUTTONS_MARK : configSpam->writeEntry( CONFIG_ENTRY_SPAMCHECK_ACTION, CONFIG_VALUE_SPAMCHECK_ACTION_MARK ); break;
00150 case ID_SPAM_ACTION_BUTTONS_MOVE : configSpam->writeEntry( CONFIG_ENTRY_SPAMCHECK_ACTION, CONFIG_VALUE_SPAMCHECK_ACTION_MOVE ); break;
00151 default : configSpam->writeEntry( CONFIG_ENTRY_SPAMCHECK_ACTION, DEFAULT_SPAMCHECK_ACTION ); break;
00152 }
00153
00154
00155 if( cmbAction->currentIndex() == ID_SPAM_ACTION_BUTTONS_MOVE )
00156 {
00157 configSpam->writeEntry( CONFIG_ENTRY_SPAMCHECK_MOVE_MAILBOX, txtMailbox->text() );
00158 }
00159 else
00160 {
00161 configSpam->deleteEntry( CONFIG_ENTRY_SPAMCHECK_MOVE_MAILBOX );
00162 }
00163
00164 config->sync();
00165 }
00166
00167 void ConfigSpamcheck::slotChanged()
00168 {
00169 KCModule::changed();
00170 }
00171
00172 void ConfigSpamcheck::slotActionChanged( int index )
00173 {
00174 switch( index )
00175 {
00176 case ID_SPAM_ACTION_BUTTONS_MOVE : txtMailbox->setEnabled( true );
00177 if( txtMailbox->text().isEmpty() )
00178 txtMailbox->setText( DEFAULT_FILTER_ACTION_MOVE_MAILBOX );
00179 btnMailboxWizard->setHidden( false );
00180 break;
00181 default : txtMailbox->setEnabled( false );
00182 btnMailboxWizard->setHidden( true );
00183 break;
00184 }
00185 }
00186
00187 void ConfigSpamcheck::slotOpenMailBoxWizard( )
00188 {
00189 QPointer<MailBoxWizard> wizard = new MailBoxWizard( this );
00190 wizard->setWindowTitle( i18n( "Mailbox Select" ) );
00191 int res = wizard->exec();
00192
00193 if( res == QDialog::Accepted )
00194 txtMailbox->setText( wizard->getPath() );
00195
00196 delete wizard;
00197 }
00198
00199 bool ConfigSpamcheck::isSpamAssassinRunning( ) const
00200 {
00201 FILE *read_fp;
00202 char buffer[ BUFSIZ + 1 ];
00203 int chars_read;
00204 bool found = false;
00205
00206 memset( buffer, '\0', sizeof( buffer ) );
00207 read_fp = popen( "ps -eo comm", "r" );
00208 if( read_fp != NULL )
00209 {
00210 chars_read = fread( buffer, sizeof( char ), BUFSIZ, read_fp );
00211 while( chars_read > 0 )
00212 {
00213 buffer[ chars_read - 1 ] = '\0';
00214 QString output( buffer );
00215 found = output.contains( NAME_SPAMASSASSIN_DAEMON ) > 0;
00216 chars_read = fread( buffer, sizeof( char ), BUFSIZ, read_fp );
00217 }
00218 pclose( read_fp );
00219 }
00220
00221 return found;
00222 }
00223
00224 void ConfigSpamcheck::slotTestSpamAssassin( )
00225 {
00226 if( isSpamAssassinRunning() )
00227 {
00228 KMessageBox::information( this, i18n( "SpamAssassin is running." ), i18n( "Check for SpamAssassin" ) );
00229 gboxAction->setEnabled( true );
00230 }
00231 else
00232 {
00233 KMessageBox::information( this, i18n( "SpamAssassin is not running." ), i18n( "Check for SpamAssassin" ) );
00234 gboxAction->setEnabled( false );
00235 }
00236 }
00237
00238 #include "configspamcheck.moc"