summaryrefslogtreecommitdiffstats
path: root/kshowmail/kcmconfigs/accountsetupdialog.cpp
blob: dc800ee0221934632d1e68b91880c2d162376531 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//
// C++ Implementation: accountsetupdialog
//
// Description:
//
//
// Author: Ulrich Weigelt <ulrich.weigelt@gmx.de>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "accountsetupdialog.h"

AccountSetupDialog::AccountSetupDialog( TQWidget* parent, TDEListView* view, AccountSetupItem* item )
  : KDialogBase( parent, "AccountSetupDialog", true, TQString::null, Ok|Cancel, Ok, true )
{
  //save pointer to account and view
  account = item;
  ListView = view;

  //tab widget
  KTabWidget* tabs = new KTabWidget( this, "tabs" );
  tabs->setMargin( 10 );

  //pages
  TQWidget* pgGeneral = new TQWidget( this );
  TQWidget* pgSecurity = new TQWidget( this );
  setMainWidget( tabs );

  //layouts of general page
  TQVBoxLayout* layGeneral = new TQVBoxLayout( pgGeneral, 0, spacingHint() );
  TQGridLayout* layTop = new TQGridLayout( layGeneral, 5, 2 );

  //layouts of security page
  TQVBoxLayout* laySecurity = new TQVBoxLayout( pgSecurity, 0, spacingHint() );
  laySecurity->setAlignment( TQt::AlignTop );

  //upper items
  TQLabel* lblAccount = new TQLabel( i18n( "Account:" ), pgGeneral, "lblAccount" );
  txtAccount = new KLineEdit( pgGeneral, "txtAccount" );
  txtAccount->setFocus();
  TQToolTip::add( lblAccount, i18n( "Unique Account Name" ) );
  TQToolTip::add( txtAccount, i18n( "Unique Account Name" ) );
  layTop->addWidget( lblAccount, 0, 0 );
  layTop->addWidget( txtAccount, 0, 1 );

  TQLabel* lblServer = new TQLabel( i18n( "Server:" ), pgGeneral, "lblServer" );
  txtServer = new KLineEdit( pgGeneral, "txtServer" );
  TQToolTip::add( lblServer, i18n( "Server Name" ) );
  TQToolTip::add( txtServer, i18n( "Server Name" ) );
  layTop->addWidget( lblServer, 1, 0 );
  layTop->addWidget( txtServer, 1, 1 );

  TQLabel* lblProtocol = new TQLabel( i18n( "Protocol:" ), pgGeneral, "lblProtocol" );
  cboProtocol = new KComboBox( pgGeneral, "cboProtocol" );
  cboProtocol->insertItem( "POP3" );  //currently KShowmail just supports POP3
  TQToolTip::add( lblProtocol, i18n( "Protocol, which shall be used to get the mails from the server. Currently KShowmail just supports POP3.") );
  TQToolTip::add( cboProtocol, i18n( "Protocol, which shall be used to get the mails from the server. Currently KShowmail just supports POP3.") );
  layTop->addWidget( lblProtocol, 2, 0 );
  layTop->addWidget( cboProtocol, 2, 1 );

  TQLabel* lblPort = new TQLabel( i18n( "Port:" ), pgGeneral, "lblPort" );
  spbPort = new TQSpinBox( 0, 65535, 1, pgGeneral, "spbPort" );
  spbPort->setValue( DEFAULT_ACCOUNT_PORT_POP3 );
  TQToolTip::add( lblPort, i18n( "Port Number. Normally POP3 uses port 110." ) );
  TQToolTip::add( spbPort, i18n( "Port Number. Normally POP3 uses port 110." ) );
  layTop->addWidget( lblPort, 3, 0 );
  layTop->addWidget( spbPort, 3, 1 );

  TQLabel* lblUser = new TQLabel( i18n( "User:" ), pgGeneral, "lblUser" );
  txtUser = new KLineEdit( pgGeneral, "txtUser" );
  TQToolTip::add( lblUser, i18n( "To authenticate to the mail server you need an user name." ) );
  TQToolTip::add( txtUser, i18n( "To authenticate to the mail server you need an user name." ) );
  layTop->addWidget( lblUser, 4, 0 );
  layTop->addWidget( txtUser, 4, 1 );

  //password groupbox and layouts
  TQGroupBox* gboxPassword = new TQGroupBox( 0, TQt::Horizontal, i18n( "Password" ), pgGeneral, "gboxPassword" );
  layGeneral->addWidget( gboxPassword );

  TQVBoxLayout* layPassword = new TQVBoxLayout( gboxPassword->layout(), spacingHint() );
  TQGridLayout* layPasswordStorage = new TQGridLayout( layPassword, 2, 2, spacingHint() );


  //radio buttons to set storage of the password
  grpPasswordStorage = new TQButtonGroup( NULL, "grpPasswordStorage" );
  connect( grpPasswordStorage, SIGNAL( clicked( int ) ), this, SLOT( slotPasswordStorageChanged( int ) ) );

  TQRadioButton* btnPasswordDontSave = new TQRadioButton( i18n( "Don't save" ), gboxPassword, "btnPasswordDontSave" );
  TQRadioButton* btnPasswordSaveFile = new TQRadioButton( i18n( "Save password"), gboxPassword, "btnPasswordSaveFile" );
  TQRadioButton* btnPasswordSaveKWallet = new TQRadioButton( i18n( "Use KWallet" ), gboxPassword, "btnPasswordSaveKWallet" );
  grpPasswordStorage->insert( btnPasswordDontSave, ID_BUTTON_PASSWORD_DONT_SAVE );
  grpPasswordStorage->insert( btnPasswordSaveFile, ID_BUTTON_PASSWORD_SAVE_FILE );
  grpPasswordStorage->insert( btnPasswordSaveKWallet, ID_BUTTON_PASSWORD_SAVE_KWALLET );
  TQToolTip::add( btnPasswordDontSave, i18n( "Don't save password. KShowmail will ask you for it at first server connect." ) );
  TQToolTip::add( btnPasswordSaveFile, i18n( "Save password in the configuration file. Not recommended, because the password is just lightly encrypted" ) );
  TQToolTip::add( btnPasswordSaveKWallet, i18n( "Use KWallet to save the password. Maybe you have to type in the KWallet master password at first server connect." ) );
  layPasswordStorage->addWidget( btnPasswordDontSave, 0, 0 );
  layPasswordStorage->addWidget( btnPasswordSaveFile, 0, 1 );
  layPasswordStorage->addWidget( btnPasswordSaveKWallet, 1, 0 );

  //password edit line
  txtPassword = new KPasswordEdit( gboxPassword, "txtUser" );
  layPassword->addWidget( txtPassword );

  //set password defaults
  grpPasswordStorage->setButton( DEFAULT_ACCOUNT_PASSWORD_STORAGE );
  slotPasswordStorageChanged( DEFAULT_ACCOUNT_PASSWORD_STORAGE );

  //active check box
  TQGridLayout* layActive = new TQGridLayout( layGeneral, 1, 1 );
  layActive->setAlignment( TQt::AlignCenter );
  chkActive = new TQCheckBox( i18n( "Active"), pgGeneral, "chkActive" );
  TQToolTip::add( chkActive, i18n( "Select it to activate this account." ) );
  layActive->addWidget( chkActive, 0, 0 );
  chkActive->setChecked( DEFAULT_ACCOUNT_ACTIVE );

  //secure transfer groupbox and layouts
  TQGroupBox* gboxSecureTransfer = new TQGroupBox( 0, TQt::Horizontal, i18n( "Encryption" ), pgSecurity, "gboxSecureTransfer" );
  gboxSecureTransfer->setSizePolicy( TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Maximum ) );
  laySecurity->addWidget( gboxSecureTransfer );

  TQHBoxLayout* laySecureTransfer = new TQHBoxLayout( gboxSecureTransfer->layout(), spacingHint() );

  //radio buttons to set secure transfer
  grpSecureTransfer = new TQButtonGroup( NULL, "grpSecureTransfer" );
  connect( grpSecureTransfer, SIGNAL( clicked( int ) ), this, SLOT( slotSecureTransferChanged ( int ) ) );

  TQRadioButton* btnSecureTransferNone = new TQRadioButton( i18n( "None" ), gboxSecureTransfer, "btnSecureTransferNone" );
  TQRadioButton* btnSecureTransferSSL = new TQRadioButton( i18n( "SSL"), gboxSecureTransfer, "btnSecureTransferSSL" );
  TQRadioButton* btnSecureTransferTLS = new TQRadioButton( i18n( "TLS" ), gboxSecureTransfer, "btnSecureTransferTLS" );
  grpSecureTransfer->insert( btnSecureTransferNone, ID_BUTTON_SECTRANSFER_NONE );
  grpSecureTransfer->insert( btnSecureTransferSSL, ID_BUTTON_SECTRANSFER_SSL );
  grpSecureTransfer->insert( btnSecureTransferTLS, ID_BUTTON_SECTRANSFER_TLS );
  TQToolTip::add( btnSecureTransferNone, i18n( "The download of the mail header and body will not be encrypted. Use this, if your provider doesn't make a secure transfer available." ) );
  TQToolTip::add( btnSecureTransferSSL, i18n( "Secure Sockets Layer (SSL), is a cryptographic protocol that provides secure communications on the Internet." ) );
  TQToolTip::add( btnSecureTransferTLS, i18n( "Transport Layer Security (TLS) is a cryptographic protocol that provides secure communications on the Internet. It is the successor of SSL." ) );
  laySecureTransfer->addWidget( btnSecureTransferNone );
  laySecureTransfer->addWidget( btnSecureTransferSSL );
  laySecureTransfer->addWidget( btnSecureTransferTLS );

  grpSecureTransfer->setButton( DEFAULT_ACCOUNT_SECTRANSFER );

  //set pages to tab widget
  tabs->addTab( pgGeneral, i18n( "General" ) );
  tabs->addTab( pgSecurity, i18n( "Security" ) );

  //set caption
  if( item == NULL )
    setCaption( i18n( "New account" ) );
  else
    setCaption( i18n( "Edit account" ) );

  //write values of the given account into the dialog items
  if( account != NULL )
    fillDialog();


}


AccountSetupDialog::~AccountSetupDialog()
{
}

void AccountSetupDialog::slotPasswordStorageChanged( int id )
{
  if( id == ID_BUTTON_PASSWORD_DONT_SAVE )
  {
    txtPassword->setEnabled( false );
    txtPassword->clear();
  }
  else
    txtPassword->setEnabled( true );
}

void AccountSetupDialog::slotOk( )
{
  //check for necessary values
  if( txtAccount->text() == "" )
  {
    KMessageBox::error( this, i18n( "Please enter an account name." ) );
    return;
  }
  if( txtServer->text() == "" )
  {
    KMessageBox::error( this, i18n( "Please enter an server." ) );
    return;
  }

  if( txtUser->text() == "" )
  {
    KMessageBox::error( this, i18n( "Please enter an user name." ) );
    return;
  }

  //test for unique account name
  //we will only test if the account is new or if the
  //name in the dialog is different from the name of the account object
  bool test = false;
  if( account == NULL )
    test = true;
  else if( account != NULL && account->getAccountName() != txtAccount->text() )
    test = true;
  else
    test = false;

  if( test )
  {
    //OK, we want to test
    AccountSetupItem* item = NULL;
    int index = 0;
    bool equality = false;

    do
    {
      item = (AccountSetupItem*)( ListView->itemAtIndex( index ) );
      if( item != NULL )
      {
        index++;
        equality = item->getAccountName() == txtAccount->text();
      }
    } while( item != NULL && !equality );

    //exit method if we have found an account with the same name
    if( equality )
    {
      KMessageBox::error( this, i18n( "There is already an account named %1. Please choose another name." ).arg( txtAccount->text() ) );
      return;
    }

  }


  //create a new account item if necessary
  if( account == NULL )
    account = new AccountSetupItem( ListView );

  //show a warning if the account name was changend
  if( account->getAccountName() != DEFAULT_ACCOUNT_NAME && account->getAccountName() != txtAccount->text() )
    KMessageBox::information( this, i18n( "You have changed the account name. The account will lose all downloaded mail headers. Please perform a refresh." ) );

  //set column text
  account->setText( 0, txtAccount->text() );

  //write values into account item
  account->setAccountName( txtAccount->text() );
  account->setServer( txtServer->text() );
  account->setProtocol( cboProtocol->currentText() );
  account->setPort( spbPort->value() );
  account->setUser( txtUser->text() );

  //get the password
  //the class KPasswordEdit doesn't have a method to set the password
  //therefore we use setText(). But if we use this method, KPasswordEdit::password()
  //will return an empty string. If the user has typed in a new password, KPasswordEdit::password()
  //will return the correct password
  TQString pass;
  if( txtPassword->password() == "" || txtPassword->password() == TQString::null )
    pass = txtPassword->text();
  else
    pass = txtPassword->password();

  switch( grpPasswordStorage->selectedId() )
  {
    case ID_BUTTON_PASSWORD_DONT_SAVE    : account->setPasswordStorageType( CONFIG_VALUE_ACCOUNT_PASSWORD_DONT_SAVE );
                                           account->setPassword( TQString::null );
                                           break;
    case ID_BUTTON_PASSWORD_SAVE_FILE    : account->setPasswordStorageType( CONFIG_VALUE_ACCOUNT_PASSWORD_SAVE_FILE );
                                           account->setPassword( pass );
                                           break;
    case ID_BUTTON_PASSWORD_SAVE_KWALLET : account->setPasswordStorageType( CONFIG_VALUE_ACCOUNT_PASSWORD_SAVE_KWALLET );
                                           account->setPassword( pass );
                                           break;
    default                              : account->setPasswordStorageType( -1 );
                                           account->setPassword( TQString::null );
  }

  account->setActive( chkActive->isChecked() );

  switch( grpSecureTransfer->selectedId() )
  {
    case ID_BUTTON_SECTRANSFER_NONE : account->setTransferSecurity( CONFIG_VALUE_ACCOUNT_SECTRANSFER_NONE ); break;
    case ID_BUTTON_SECTRANSFER_SSL  : account->setTransferSecurity( CONFIG_VALUE_ACCOUNT_SECTRANSFER_SSL ); break;
    case ID_BUTTON_SECTRANSFER_TLS  : account->setTransferSecurity( CONFIG_VALUE_ACCOUNT_SECTRANSFER_TLS ); break;
    default                         : account->setTransferSecurity( DEFAULT_ACCOUNT_SECTRANSFER ); break;
  }

  //call slot of super class to close the dialog
  KDialogBase::slotOk();
}

void AccountSetupDialog::fillDialog( )
{
  //check for valid account pointer
  if( account == NULL )
  {
    kdError() << "AccountSetupDialog::fillDialog: invalid pointer to account item." << endl;
    return;
  }

  txtAccount->setText( account->getAccountName() );
  txtServer->setText( account->getServer() );
  cboProtocol->setCurrentText( account->getProtocol().upper() );

  if( account->getPort() >= 0 && account->getPort() <= 65535 )
    spbPort->setValue( account->getPort() );
  else
    spbPort->setValue( DEFAULT_ACCOUNT_PORT_POP3 );

  txtUser->setText( account->getUser() );

  int type = account->getPasswordStorageType();
  if( type != CONFIG_VALUE_ACCOUNT_PASSWORD_DONT_SAVE && type != CONFIG_VALUE_ACCOUNT_PASSWORD_SAVE_FILE && type != CONFIG_VALUE_ACCOUNT_PASSWORD_SAVE_KWALLET )
    type = DEFAULT_ACCOUNT_PASSWORD_STORAGE;

  switch( type )
  {
    case CONFIG_VALUE_ACCOUNT_PASSWORD_DONT_SAVE    : grpPasswordStorage->setButton( ID_BUTTON_PASSWORD_DONT_SAVE );
                                                      txtPassword->setEnabled( false );
                                                      txtPassword->clear();
                                                      break;
    case CONFIG_VALUE_ACCOUNT_PASSWORD_SAVE_FILE    : grpPasswordStorage->setButton( ID_BUTTON_PASSWORD_SAVE_FILE );
                                                      txtPassword->setEnabled( true );
                                                      txtPassword->setText( account->getPassword() );
                                                      break;
    case CONFIG_VALUE_ACCOUNT_PASSWORD_SAVE_KWALLET : grpPasswordStorage->setButton( ID_BUTTON_PASSWORD_SAVE_KWALLET );
                                                      txtPassword->setEnabled( true );
                                                      txtPassword->setText( account->getPassword() );
                                                      break;
    default                                         : grpPasswordStorage->setButton( 1 );
                                                      txtPassword->clear();
  }

  chkActive->setChecked( account->getActive() );

  int transferSecurity = account->getTransferSecurity();
  if( transferSecurity != CONFIG_VALUE_ACCOUNT_SECTRANSFER_NONE && transferSecurity != CONFIG_VALUE_ACCOUNT_SECTRANSFER_SSL && transferSecurity != CONFIG_VALUE_ACCOUNT_SECTRANSFER_TLS )
    transferSecurity = DEFAULT_ACCOUNT_SECTRANSFER;

  switch( transferSecurity )
  {
    case CONFIG_VALUE_ACCOUNT_SECTRANSFER_NONE : grpSecureTransfer->setButton( ID_BUTTON_SECTRANSFER_NONE ); break;
    case CONFIG_VALUE_ACCOUNT_SECTRANSFER_SSL  : grpSecureTransfer->setButton( ID_BUTTON_SECTRANSFER_SSL ); break;
    case CONFIG_VALUE_ACCOUNT_SECTRANSFER_TLS  : grpSecureTransfer->setButton( ID_BUTTON_SECTRANSFER_TLS ); break;
    default                                    : grpSecureTransfer->setButton( ID_BUTTON_SECTRANSFER_NONE ); break;
  }
}

void AccountSetupDialog::slotSecureTransferChanged( int id )
{
  switch( id )
  {
    case ID_BUTTON_SECTRANSFER_NONE   : spbPort->setValue( DEFAULT_ACCOUNT_PORT_POP3 ); break;
    case ID_BUTTON_SECTRANSFER_SSL    : spbPort->setValue( DEFAULT_ACCOUNT_PORT_POP3SSL ); break;
    case ID_BUTTON_SECTRANSFER_TLS    : spbPort->setValue( DEFAULT_ACCOUNT_PORT_POP3 ); break;
  }
}



#include "accountsetupdialog.moc"