summaryrefslogtreecommitdiffstats
path: root/kshowmail/kcmconfigs/configspamcheck.cpp
blob: 7d2371928343e21bed70296de92f55329cb9e9c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//
// C++ Implementation: configspamcheck
//
// Description:
//
//
// Author: Ulrich Weigelt <ulrich.weigelt@gmx.de>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "configspamcheck.h"


typedef KGenericFactory<ConfigSpamcheck, TQWidget> ConfigSpamcheckFactory;

K_EXPORT_COMPONENT_FACTORY( kcm_kshowmailconfigspamcheck, ConfigSpamcheckFactory(
    "kcm_kshowmailconfigspamcheck" ) );


ConfigSpamcheck::ConfigSpamcheck(TQWidget * parent, const char * name, const TQStringList & args)
  : TDECModule( ConfigSpamcheckFactory::instance(), parent, args )
{
  //set the module name
  if ( !name )
    setName( "configfilter" );

  //build GUI
  //---------

  //main layout
  TQVBoxLayout* layMain = new TQVBoxLayout( this, 0, 10 );

  //description
  TQLabel* lblDescription = new TQLabel( this, "lblDescription" );
  lblDescription->setAlignment( TQt::WordBreak );
  lblDescription->setText( TQString( "<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." ) ) );
  lblDescription->setSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Maximum );
  layMain->addWidget( lblDescription );

  //Test button
  btnTest = new KPushButton( KStdGuiItem::test(), this, "btnTest" );
  btnTest->setSizePolicy( TQSizePolicy::Maximum, TQSizePolicy::Maximum );
  layMain->addWidget( btnTest );
  connect( btnTest, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotTestSpamAssassin() ) );

  //action
  gboxAction = new TQGroupBox( 0, TQt::Horizontal, i18n( "Action for Spam" ), this, "gboxAction" );
  TQHBoxLayout* layAction = new TQHBoxLayout( gboxAction->layout(), 10 );
  layMain->addWidget( gboxAction );

  cmbAction = new KComboBox( gboxAction, "cmbAction" );
  layAction->addWidget( cmbAction );
  TQToolTip::add( cmbAction, i18n( "Choose the action for spam mails." ) );
  connect( cmbAction, TQ_SIGNAL( activated( int ) ), this, TQ_SLOT( slotActionChanged( int ) ) );
  connect( cmbAction, TQ_SIGNAL( activated( int ) ), this, TQ_SLOT( slotChanged() ) );

  //insert items
  cmbAction->insertItem( i18n( "Delete" ), ID_SPAM_ACTION_BUTTONS_DELETE );
  cmbAction->insertItem( i18n( "Mark" ), ID_SPAM_ACTION_BUTTONS_MARK );
  cmbAction->insertItem( i18n( "Move" ), ID_SPAM_ACTION_BUTTONS_MOVE );

  //create edit line to defined the mailbox for move
  txtMailbox = new KLineEdit( gboxAction );
  layAction->addWidget( txtMailbox );
  connect( txtMailbox, TQ_SIGNAL( textChanged( const TQString & ) ), this, TQ_SLOT( slotChanged() ) );

  //create wizard button to configure mailbox
  btnMailboxWizard= new KPushButton( KGuiItem( TQString::null, "wizard" ), gboxAction );
  layAction->addWidget( btnMailboxWizard );
  TQToolTip::add( btnMailboxWizard, i18n( "Choose the mailbox" ) );
  connect( btnMailboxWizard, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotOpenMailBoxWizard() ) );

  //set defaults
  switch( DEFAULT_SPAMCHECK_ACTION )
  {
    case CONFIG_VALUE_SPAMCHECK_ACTION_DELETE     : cmbAction->setCurrentItem( ID_SPAM_ACTION_BUTTONS_DELETE ); break;
    case CONFIG_VALUE_SPAMCHECK_ACTION_MARK       : cmbAction->setCurrentItem( ID_SPAM_ACTION_BUTTONS_MARK ); break;
    case CONFIG_VALUE_SPAMCHECK_ACTION_MOVE       : cmbAction->setCurrentItem( ID_SPAM_ACTION_BUTTONS_MOVE ); break;
  }

  txtMailbox->setText( DEFAULT_SPAMCHECK_ACTION_MOVE_MAILBOX );

  //enable or disable widgets
  slotActionChanged( cmbAction->currentItem() );
  gboxAction->setEnabled( isSpamAssassinRunning() );

  //get application config object (kshowmailrc)
  config = TDEApplication::kApplication()->config();

  //load configured values
  load();
}

ConfigSpamcheck::~ConfigSpamcheck()
{
}

void ConfigSpamcheck::load()
{
  config->setGroup( CONFIG_GROUP_SPAMCHECK );

  //load action
  switch( config->readNumEntry( CONFIG_ENTRY_SPAMCHECK_ACTION, DEFAULT_SPAMCHECK_ACTION ) )
  {
    case CONFIG_VALUE_SPAMCHECK_ACTION_DELETE     : cmbAction->setCurrentItem( ID_SPAM_ACTION_BUTTONS_DELETE ); break;
    case CONFIG_VALUE_SPAMCHECK_ACTION_MARK       : cmbAction->setCurrentItem( ID_SPAM_ACTION_BUTTONS_MARK ); break;
    case CONFIG_VALUE_SPAMCHECK_ACTION_MOVE       : cmbAction->setCurrentItem( ID_SPAM_ACTION_BUTTONS_MOVE ); break;
  }

  //get mailbox name
  if( config->readNumEntry( CONFIG_ENTRY_SPAMCHECK_ACTION, DEFAULT_SPAMCHECK_ACTION ) == CONFIG_VALUE_SPAMCHECK_ACTION_MOVE )
    txtMailbox->setText( config->readEntry( CONFIG_ENTRY_SPAMCHECK_MOVE_MAILBOX, DEFAULT_SPAMCHECK_ACTION_MOVE_MAILBOX ) );
  else
    txtMailbox->clear();

  //enable or disable widgets for other action
  slotActionChanged( cmbAction->currentItem() );
}

void ConfigSpamcheck::defaults()
{
  switch( DEFAULT_SPAMCHECK_ACTION )
  {
    case CONFIG_VALUE_SPAMCHECK_ACTION_DELETE     : cmbAction->setCurrentItem( ID_SPAM_ACTION_BUTTONS_DELETE ); break;
    case CONFIG_VALUE_SPAMCHECK_ACTION_MARK       : cmbAction->setCurrentItem( ID_SPAM_ACTION_BUTTONS_MARK ); break;
    case CONFIG_VALUE_SPAMCHECK_ACTION_MOVE       : cmbAction->setCurrentItem( ID_SPAM_ACTION_BUTTONS_MOVE ); break;
  }

  if( DEFAULT_SPAMCHECK_ACTION == CONFIG_VALUE_SPAMCHECK_ACTION_MOVE )
    txtMailbox->setText( TQString( DEFAULT_SPAMCHECK_ACTION_MOVE_MAILBOX ) );
  else
    txtMailbox->clear();

  //enable or disable widgets for other action
  slotActionChanged( cmbAction->currentItem() );

  slotChanged();
}

void ConfigSpamcheck::save()
{
  config->setGroup( CONFIG_GROUP_SPAMCHECK );

  //save action
  switch( cmbAction->currentItem() )
  {
    case ID_SPAM_ACTION_BUTTONS_DELETE    : config->writeEntry( CONFIG_ENTRY_SPAMCHECK_ACTION, CONFIG_VALUE_SPAMCHECK_ACTION_DELETE ); break;
    case ID_SPAM_ACTION_BUTTONS_MARK      : config->writeEntry( CONFIG_ENTRY_SPAMCHECK_ACTION, CONFIG_VALUE_SPAMCHECK_ACTION_MARK ); break;
    case ID_SPAM_ACTION_BUTTONS_MOVE      : config->writeEntry( CONFIG_ENTRY_SPAMCHECK_ACTION, CONFIG_VALUE_SPAMCHECK_ACTION_MOVE ); break;
    default                               : config->writeEntry( CONFIG_ENTRY_SPAMCHECK_ACTION, DEFAULT_SPAMCHECK_ACTION ); break;
  }

  //save mailbox name
  if( cmbAction->currentItem() == ID_SPAM_ACTION_BUTTONS_MOVE )
  {
    config->writeEntry( CONFIG_ENTRY_SPAMCHECK_MOVE_MAILBOX, txtMailbox->text() );
  }
  else
  {
    config->deleteEntry( CONFIG_ENTRY_SPAMCHECK_MOVE_MAILBOX );
  }

  config->sync();
}

void ConfigSpamcheck::slotChanged()
{
  TDECModule::changed();
}

void ConfigSpamcheck::slotActionChanged( int index )
{
  switch( index )
  {
    case ID_SPAM_ACTION_BUTTONS_MOVE    : txtMailbox->setEnabled( true );
                                          if( txtMailbox->text() == "" )
                                            txtMailbox->setText( DEFAULT_FILTER_ACTION_MOVE_MAILBOX );
                                          btnMailboxWizard->setHidden( false );
                                          break;
    default                             : txtMailbox->setEnabled( false );
                                          btnMailboxWizard->setHidden( true );
                                          break;
  }
}

void ConfigSpamcheck::slotOpenMailBoxWizard( )
{
  MailBoxWizard wizard( this, "malboxwizard" );
  wizard.setCaption( i18n( "Mailbox Select" ) );
  int res = wizard.exec();

  if( res == TQDialog::Accepted )
    txtMailbox->setText( wizard.getPath() );
}

bool ConfigSpamcheck::isSpamAssassinRunning( ) const
{
  FILE *read_fp;
  char buffer[ BUFSIZ + 1 ];
  int chars_read;
  bool found = false;

  memset( buffer, '\0', sizeof( buffer ) );
  read_fp = popen( "ps -eo comm", "r" );
  if( read_fp != NULL )
  {
    chars_read = fread( buffer, sizeof( char ), BUFSIZ, read_fp );
    while( chars_read > 0 )
    {
      buffer[ chars_read - 1 ] = '\0';
      TQString output( buffer );
      found = output.contains( NAME_SPAMASSASSIN_DAEMON ) > 0;
      chars_read = fread( buffer, sizeof( char ), BUFSIZ, read_fp );
    }
    pclose( read_fp );
  }

  return found;
}

void ConfigSpamcheck::slotTestSpamAssassin( )
{
  if( isSpamAssassinRunning() )
  {
    KMessageBox::information( this, i18n( "SpamAssassin is running." ), i18n( "Check for SpamAssassin" ) );
    gboxAction->setEnabled( true );
  }
  else
  {
    KMessageBox::information( this, i18n( "SpamAssassin is not running." ), i18n( "Check for SpamAssassin" ) );
    gboxAction->setEnabled( false );
  }
}

#include "configspamcheck.moc"