serverdialog.cpp

00001 /***************************************************************************
00002                           serverdialog.cpp  -  description
00003                              -------------------
00004     begin                : Sun Oct 28 2001
00005     copyright            : (C) 2001 by Eggert Ehmke
00006     email                : eggert.ehmke@berlin.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "serverdialog.h"
00019 
00020 ServerDialog::ServerDialog( TQWidget* parent, ConfigElem* item )
00021   : KDialogBase( parent, "ServerDialog", true, TQString::null, Ok|Cancel, Ok, true )
00022 {
00023   //save pointer to account and view
00024   account = item;
00025 
00026   //main widget
00027   TQWidget* page = new TQWidget( this );
00028   setMainWidget( page );
00029 
00030   //layouts
00031   TQVBoxLayout* layMain = new TQVBoxLayout( page, 0, spacingHint() );
00032   TQGridLayout* layTop = new TQGridLayout( layMain, 5, 2 );
00033 
00034   //upper items
00035   TQLabel* lblAccount = new TQLabel( i18n( "Account:" ), page, "lblAccount" );
00036   txtAccount = new KLineEdit( page, "txtAccount" );
00037   txtAccount->setEnabled( false );
00038   TQToolTip::add( lblAccount, i18n( "Unique Account Name" ) );
00039   TQToolTip::add( txtAccount, i18n( "Unique Account Name" ) );
00040   layTop->addWidget( lblAccount, 0, 0 );
00041   layTop->addWidget( txtAccount, 0, 1 );
00042 
00043   TQLabel* lblServer = new TQLabel( i18n( "Server:" ), page, "lblServer" );
00044   txtServer = new KLineEdit( page, "txtServer" );
00045   txtServer->setFocus();
00046   TQToolTip::add( lblServer, i18n( "Server Name" ) );
00047   TQToolTip::add( txtAccount, i18n( "Server Name" ) );
00048   layTop->addWidget( lblServer, 1, 0 );
00049   layTop->addWidget( txtServer, 1, 1 );
00050 
00051   TQLabel* lblProtocol = new TQLabel( i18n( "Protocol:" ), page, "lblProtocol" );
00052   cboProtocol = new KComboBox( page, "cboProtocol" );
00053   cboProtocol->insertItem( "POP3" );  //currently KShowmail just supports POP3
00054   TQToolTip::add( lblProtocol, i18n( "Protocol, which shall be used to get the mails from the server. Currently KShowmail just supports POP3.") );
00055   TQToolTip::add( cboProtocol, i18n( "Protocol, which shall be used to get the mails from the server. Currently KShowmail just supports POP3.") );
00056   layTop->addWidget( lblProtocol, 2, 0 );
00057   layTop->addWidget( cboProtocol, 2, 1 );
00058 
00059   TQLabel* lblPort = new TQLabel( i18n( "Port:" ), page, "lblPort" );
00060   spbPort = new TQSpinBox( 0, 65535, 1, page, "spbPort" );
00061   spbPort->setValue( DEFAULT_PORT_POP3 );
00062   TQToolTip::add( lblPort, i18n( "Port Number. Normally POP3 uses port 110." ) );
00063   TQToolTip::add( spbPort, i18n( "Port Number. Normally POP3 uses port 110." ) );
00064   layTop->addWidget( lblPort, 3, 0 );
00065   layTop->addWidget( spbPort, 3, 1 );
00066 
00067   TQLabel* lblUser = new TQLabel( i18n( "User:" ), page, "lblUser" );
00068   txtUser = new KLineEdit( page, "txtUser" );
00069   TQToolTip::add( lblUser, i18n( "To authenticate to the mail server you need an user name." ) );
00070   TQToolTip::add( txtUser, i18n( "To authenticate to the mail server you need an user name." ) );
00071   layTop->addWidget( lblUser, 4, 0 );
00072   layTop->addWidget( txtUser, 4, 1 );
00073 
00074   //password groupbox and layouts
00075   TQGroupBox* gboxPassword = new TQGroupBox( 0, TQt::Horizontal, i18n( "Password" ), page, "gboxPassword" );
00076   layMain->addWidget( gboxPassword );
00077 
00078   TQVBoxLayout* layPassword = new TQVBoxLayout( gboxPassword->layout(), spacingHint() );
00079   TQGridLayout* layPasswordStorage = new TQGridLayout( layPassword, 2, 2, spacingHint() );
00080 
00081 
00082   //radio buttons to set storage of the password
00083   grpPasswordStorage = new TQButtonGroup( NULL, "grpPasswordStorage" );
00084   connect( grpPasswordStorage, SIGNAL( clicked( int ) ), this, SLOT( slotPasswordStorageChanged( int ) ) );
00085 
00086   TQRadioButton* btnPasswordDontSave = new TQRadioButton( i18n( "Don't save" ), gboxPassword, "btnPasswordDontSave" );
00087   TQRadioButton* btnPasswordSaveFile = new TQRadioButton( i18n( "Save password"), gboxPassword, "btnPasswordSaveFile" );
00088   TQRadioButton* btnPasswordSaveTDEWallet = new TQRadioButton( i18n( "Use TDEWallet" ), gboxPassword, "btnPasswordSaveTDEWallet" );
00089   grpPasswordStorage->insert( btnPasswordDontSave, ID_BUTTON_PASSWORD_DONT_SAVE );
00090   grpPasswordStorage->insert( btnPasswordSaveFile, ID_BUTTON_PASSWORD_SAVE_FILE );
00091   grpPasswordStorage->insert( btnPasswordSaveTDEWallet, ID_BUTTON_PASSWORD_SAVE_KWALLET );
00092   TQToolTip::add( btnPasswordDontSave, i18n( "Don't save password. KShowmail will ask you for it at first server connect." ) );
00093   TQToolTip::add( btnPasswordSaveFile, i18n( "Save password in the configuration file. Not recommended, because the password is just lightly encrypted" ) );
00094   TQToolTip::add( btnPasswordSaveTDEWallet, i18n( "Use TDEWallet to save the password. Maybe you have to type in the TDEWallet master password at first server connect." ) );
00095   layPasswordStorage->addWidget( btnPasswordDontSave, 0, 0 );
00096   layPasswordStorage->addWidget( btnPasswordSaveFile, 0, 1 );
00097   layPasswordStorage->addWidget( btnPasswordSaveTDEWallet, 1, 0 );
00098 
00099   //password edit line
00100   txtPassword = new KPasswordEdit( gboxPassword, "txtUser" );
00101   layPassword->addWidget( txtPassword );
00102 
00103   //set password defaults
00104   grpPasswordStorage->setButton( DEFAULT_ACCOUNT_PASSWORD_STORAGE );
00105   slotPasswordStorageChanged( DEFAULT_ACCOUNT_PASSWORD_STORAGE );
00106 
00107   //active check box
00108   TQGridLayout* layActive = new TQGridLayout( layMain, 1, 1 );
00109   layActive->setAlignment( TQt::AlignCenter );
00110   chkActive = new TQCheckBox( i18n( "Active"), page, "chkActive" );
00111   TQToolTip::add( chkActive, i18n( "Select it to activate this account." ) );
00112   layActive->addWidget( chkActive, 0, 0 );
00113   chkActive->setChecked( DEFAULT_ACCOUNT_ACTIVE );
00114 
00115   //set caption
00116   if( item == NULL )
00117     setCaption( i18n( "New account" ) );
00118   else
00119     setCaption( i18n( "Edit account" ) );
00120 
00121   //write values of the given account into the dialog items
00122   if( account != NULL )
00123     fillDialog();
00124 
00125 
00126 }
00127 
00128 
00129 ServerDialog::~ServerDialog()
00130 {
00131 }
00132 
00133 void ServerDialog::slotPasswordStorageChanged( int id )
00134 {
00135   if( id == ID_BUTTON_PASSWORD_DONT_SAVE )
00136   {
00137     txtPassword->setEnabled( false );
00138     txtPassword->clear();
00139   }
00140   else
00141     txtPassword->setEnabled( true );
00142 }
00143 
00144 void ServerDialog::slotOk( )
00145 {
00146   //check for necessary values
00147   if( account == NULL )
00148   {
00149     kdError() << "ServerDialog::slotOk: Given account pointer is Null." << endl;
00150     return;
00151   }
00152   if( txtServer->text() == "" )
00153   {
00154     KMessageBox::error( this, i18n( "Please enter an server." ) );
00155     return;
00156   }
00157 
00158   if( txtUser->text() == "" )
00159   {
00160     KMessageBox::error( this, i18n( "Please enter an user name." ) );
00161     return;
00162   }
00163 
00164   //get application config object (kshowmailrc)
00165   TDEConfig* config = TDEApplication::kApplication()->config();
00166 
00167   config->setGroup( txtAccount->text() );
00168 
00169   config->writeEntry( CONFIG_ENTRY_ACCOUNT_SERVER, txtServer->text() );
00170   config->writeEntry( CONFIG_ENTRY_ACCOUNT_PROTOCOL, cboProtocol->currentText().upper() );
00171   config->writeEntry( CONFIG_ENTRY_ACCOUNT_PORT, spbPort->value() );
00172   config->writeEntry( CONFIG_ENTRY_ACCOUNT_USER, txtUser->text() );
00173 
00174   //get the password
00175   //the class KPasswordEdit doesn't have a method to set the password
00176   //therefore we use setText(). But if we use this method, KPasswordEdit::password()
00177   //will return an empty string. If the user has typed in a new password, KPasswordEdit::password()
00178   //will return the correct password
00179   TQString pass;
00180   if( txtPassword->password() == "" || txtPassword->password() == TQString::null )
00181     pass = txtPassword->text();
00182   else
00183     pass = txtPassword->password();
00184 
00185   //used to encrypt password
00186   KURL url;
00187   url.setUser( txtUser->text() );
00188   url.setHost( txtServer->text() );
00189   url.setPass( pass );
00190 
00191   //save password (or not)
00192   switch( grpPasswordStorage->selectedId() )
00193   {
00194     case ID_BUTTON_PASSWORD_DONT_SAVE    : config->writeEntry( CONFIG_ENTRY_ACCOUNT_PASSWORD_STORAGE, CONFIG_VALUE_ACCOUNT_PASSWORD_DONT_SAVE );
00195                                            config->writeEntry( CONFIG_ENTRY_ACCOUNT_PASSWORD, TQString::null );
00196                                            break;
00197     case ID_BUTTON_PASSWORD_SAVE_FILE    : config->writeEntry( CONFIG_ENTRY_ACCOUNT_PASSWORD_STORAGE, CONFIG_VALUE_ACCOUNT_PASSWORD_SAVE_FILE );
00198                                            config->writeEntry( CONFIG_ENTRY_ACCOUNT_PASSWORD, crypt( url ) );
00199                                            break;
00200     case ID_BUTTON_PASSWORD_SAVE_KWALLET : config->writeEntry( CONFIG_ENTRY_ACCOUNT_PASSWORD_STORAGE, CONFIG_VALUE_ACCOUNT_PASSWORD_SAVE_KWALLET );
00201                                            config->writeEntry( CONFIG_ENTRY_ACCOUNT_PASSWORD, TQString::null );
00202                                            TDEWalletAccess::savePassword( txtAccount->text(), pass );
00203                                            break;
00204     default                              : config->writeEntry( CONFIG_ENTRY_ACCOUNT_PASSWORD_STORAGE, CONFIG_VALUE_ACCOUNT_PASSWORD_DONT_SAVE );
00205                                            config->writeEntry( CONFIG_ENTRY_ACCOUNT_PASSWORD, TQString::null );
00206   }
00207 
00208   config->writeEntry( CONFIG_ENTRY_ACCOUNT_ACTIVE, chkActive->isChecked() );
00209 
00210   //save config to file
00211   config->sync();
00212 
00213   //call slot of super class to close the dialog
00214   KDialogBase::slotOk();
00215 }
00216 
00217 void ServerDialog::fillDialog( )
00218 {
00219   //check for valid account pointer
00220   if( account == NULL )
00221   {
00222     kdError() << "ServerDialog::fillDialog: invalid pointer to account item." << endl;
00223     return;
00224   }
00225 
00226   txtAccount->setText( account->getAccountName() );
00227   txtServer->setText( account->getHost() );
00228   cboProtocol->setCurrentText( account->getProtocol( true ) );
00229   spbPort->setValue( account->getPort() );
00230   txtUser->setText( account->getUser() );
00231 
00232   int type = account->getPasswordStorage();
00233   if( type != CONFIG_VALUE_ACCOUNT_PASSWORD_DONT_SAVE && type != CONFIG_VALUE_ACCOUNT_PASSWORD_SAVE_FILE && type != CONFIG_VALUE_ACCOUNT_PASSWORD_SAVE_KWALLET )
00234     type = DEFAULT_ACCOUNT_PASSWORD_STORAGE;
00235 
00236   switch( type )
00237   {
00238     case CONFIG_VALUE_ACCOUNT_PASSWORD_DONT_SAVE    : grpPasswordStorage->setButton( ID_BUTTON_PASSWORD_DONT_SAVE );
00239                                                       txtPassword->setEnabled( false );
00240                                                       txtPassword->clear();
00241                                                       break;
00242     case CONFIG_VALUE_ACCOUNT_PASSWORD_SAVE_FILE    : grpPasswordStorage->setButton( ID_BUTTON_PASSWORD_SAVE_FILE );
00243                                                       txtPassword->setEnabled( true );
00244                                                       txtPassword->setText( account->getPassword() );
00245                                                       break;
00246     case CONFIG_VALUE_ACCOUNT_PASSWORD_SAVE_KWALLET : grpPasswordStorage->setButton( ID_BUTTON_PASSWORD_SAVE_KWALLET );
00247                                                       txtPassword->setEnabled( true );
00248                                                       txtPassword->setText( account->getPassword() );
00249                                                       break;
00250     default                                         : grpPasswordStorage->setButton( 1 );
00251                                                       txtPassword->clear();
00252   }
00253 
00254   chkActive->setChecked( account->isActive() );
00255 }
00256 
00257 #include "serverdialog.moc"

Generated on Thu Jul 5 19:36:07 2007 for kshowmail by  doxygen 1.5.0