mailtransport
transportconfigdialog.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "transportconfigdialog.h"
00025 #include "transport.h"
00026 #include "transportmanager.h"
00027 #include "servertest.h"
00028 #include "mailtransport_defs.h"
00029
00030 #include "ui_smtpsettings.h"
00031 #include "ui_sendmailsettings.h"
00032
00033 #include <kconfigdialogmanager.h>
00034 #include <kfiledialog.h>
00035 #include <kmessagebox.h>
00036 #include <kprotocolinfo.h>
00037
00038 #include <QButtonGroup>
00039
00040 using namespace MailTransport;
00041
00042 class MailTransport::TransportConfigDialog::Private
00043 {
00044 public:
00045 Transport *transport;
00046
00047 Ui::SMTPSettings smtp;
00048 Ui::SendmailSettings sendmail;
00049
00050 KConfigDialogManager *manager;
00051 KLineEdit *passwordEdit;
00052 ServerTest *serverTest;
00053 QButtonGroup *encryptionGroup;
00054 QButtonGroup *authGroup;
00055
00056
00057 QList<int> noEncCapa, sslCapa, tlsCapa;
00058
00059 bool serverTestFailed;
00060
00061 void resetAuthCapabilities()
00062 {
00063 noEncCapa.clear();
00064 noEncCapa << Transport::EnumAuthenticationType::LOGIN
00065 << Transport::EnumAuthenticationType::PLAIN
00066 << Transport::EnumAuthenticationType::CRAM_MD5
00067 << Transport::EnumAuthenticationType::DIGEST_MD5
00068 << Transport::EnumAuthenticationType::NTLM
00069 << Transport::EnumAuthenticationType::GSSAPI;
00070 sslCapa = tlsCapa = noEncCapa;
00071 if ( authGroup ) {
00072 updateAuthCapbilities();
00073 }
00074 }
00075
00076 void updateAuthCapbilities()
00077 {
00078 Q_ASSERT( transport->type() == Transport::EnumType::SMTP );
00079
00080 if ( serverTestFailed ) {
00081 return;
00082 }
00083
00084 QList<int> capa = noEncCapa;
00085 if ( smtp.ssl->isChecked() ) {
00086 capa = sslCapa;
00087 } else if ( smtp.tls->isChecked() ) {
00088 capa = tlsCapa;
00089 }
00090
00091 for ( int i = 0; i < authGroup->buttons().count(); ++i ) {
00092 authGroup->buttons().at( i )->setEnabled( capa.contains( i ) );
00093 }
00094
00095 if ( capa.count() == 0 ) {
00096 smtp.noAuthPossible->setVisible( true );
00097 smtp.kcfg_requiresAuthentication->setChecked( false );
00098 smtp.kcfg_requiresAuthentication->setEnabled( false );
00099 } else {
00100 smtp.noAuthPossible->setVisible( false );
00101 smtp.kcfg_requiresAuthentication->setEnabled( true );
00102 }
00103 }
00104 };
00105
00106 TransportConfigDialog::TransportConfigDialog( Transport *transport, QWidget *parent )
00107 : KDialog( parent ), d( new Private )
00108 {
00109 Q_ASSERT( transport );
00110
00111 d->transport = transport;
00112 d->passwordEdit = 0;
00113 d->serverTest = 0;
00114 d->encryptionGroup = 0;
00115 d->authGroup = 0;
00116 d->resetAuthCapabilities();
00117
00118 setButtons( Ok|Cancel|User3 );
00119 showButton( User3, false );
00120 setButtonText( User3, i18n( "Use Sendmail" ) );
00121 connect( this, SIGNAL( user3Clicked() ), SLOT( slotUser3() ) );
00122 connect( this, SIGNAL(okClicked()), SLOT(save()) );
00123 connect( TransportManager::self(), SIGNAL(passwordsChanged()),
00124 SLOT(passwordsLoaded()) );
00125
00126 switch ( transport->type() ) {
00127 case Transport::EnumType::SMTP:
00128 {
00129 showButton( User3, true );
00130
00131 d->smtp.setupUi( mainWidget() );
00132 d->passwordEdit = d->smtp.password;
00133
00134 d->encryptionGroup = new QButtonGroup( this );
00135 d->encryptionGroup->addButton( d->smtp.none );
00136 d->encryptionGroup->addButton( d->smtp.ssl );
00137 d->encryptionGroup->addButton( d->smtp.tls );
00138
00139 d->authGroup = new QButtonGroup( this );
00140 d->authGroup->addButton( d->smtp.login );
00141 d->authGroup->addButton( d->smtp.plain );
00142 d->authGroup->addButton( d->smtp.crammd5 );
00143 d->authGroup->addButton( d->smtp.digestmd5 );
00144 d->authGroup->addButton( d->smtp.ntlm );
00145 d->authGroup->addButton( d->smtp.gssapi );
00146
00147 if ( KProtocolInfo::capabilities( SMTP_PROTOCOL ).contains( QLatin1String( "SASL" ) ) == 0 ) {
00148 d->smtp.ntlm->hide();
00149 d->smtp.gssapi->hide();
00150 }
00151
00152 connect( d->smtp.checkCapabilities, SIGNAL(clicked()),
00153 SLOT(checkSmtpCapabilities()) );
00154 connect( d->smtp.kcfg_host, SIGNAL(textChanged(QString)),
00155 SLOT(hostNameChanged(QString)) );
00156 connect( d->smtp.kcfg_encryption, SIGNAL(clicked(int)),
00157 SLOT(encryptionChanged(int)) );
00158 connect( d->smtp.kcfg_requiresAuthentication, SIGNAL( toggled(bool) ),
00159 SLOT( ensureValidAuthSelection() ) );
00160 break;
00161 }
00162 case Transport::EnumType::Sendmail:
00163 {
00164 d->sendmail.setupUi( mainWidget() );
00165
00166 connect( d->sendmail.chooseButton, SIGNAL(clicked()),
00167 SLOT(chooseSendmail()) );
00168 connect( d->sendmail.kcfg_host, SIGNAL(textChanged(QString)),
00169 SLOT(hostNameChanged(QString)) );
00170 }
00171 }
00172
00173
00174 if ( d->passwordEdit ) {
00175 if ( d->transport->isComplete() ) {
00176 d->passwordEdit->setText( d->transport->password() );
00177 } else {
00178 if ( d->transport->requiresAuthentication() ) {
00179 TransportManager::self()->loadPasswordsAsync();
00180 }
00181 }
00182 }
00183
00184 d->manager = new KConfigDialogManager( this, transport );
00185 d->manager->updateWidgets();
00186 hostNameChanged( d->transport->host() );
00187 }
00188
00189 TransportConfigDialog::~ TransportConfigDialog()
00190 {
00191 delete d;
00192 }
00193
00194 void TransportConfigDialog::checkSmtpCapabilities()
00195 {
00196 Q_ASSERT( d->transport->type() == Transport::EnumType::SMTP );
00197
00198 d->serverTest = new ServerTest( this );
00199 d->serverTest->setProtocol( SMTP_PROTOCOL );
00200 d->serverTest->setServer( d->smtp.kcfg_host->text() );
00201 if ( d->smtp.kcfg_specifyHostname->isChecked() ) {
00202 d->serverTest->setFakeHostname( d->smtp.kcfg_localHostname->text() );
00203 }
00204 d->serverTest->setProgressBar( d->smtp.checkCapabilitiesProgress );
00205
00206 connect( d->serverTest, SIGNAL(finished( QList< int > )),
00207 SLOT(slotFinished( QList< int > )));
00208 d->smtp.checkCapabilities->setEnabled( false );
00209 d->serverTest->start();
00210 d->serverTestFailed = false;
00211 }
00212
00213 void TransportConfigDialog::save()
00214 {
00215 d->manager->updateSettings();
00216 if ( d->passwordEdit ) {
00217 d->transport->setPassword( d->passwordEdit->text() );
00218 }
00219
00220
00221 QStringList existingNames;
00222 foreach ( Transport *t, TransportManager::self()->transports() ) {
00223 if ( t->id() != d->transport->id() ) {
00224 existingNames << t->name();
00225 }
00226 }
00227 int suffix = 1;
00228 QString origName = d->transport->name();
00229 while ( existingNames.contains( d->transport->name() ) ) {
00230 d->transport->setName( i18nc( "%1: name; %2: number appended to it to make "
00231 "it unique among a list of names", "%1 %2", origName, suffix ) );
00232 ++suffix;
00233 }
00234
00235 d->transport->writeConfig();
00236 }
00237
00238 void TransportConfigDialog::slotUser3()
00239 {
00240 reject();
00241 emit sendmailClicked();
00242 }
00243
00244 void TransportConfigDialog::chooseSendmail()
00245 {
00246 Q_ASSERT( d->transport->type() == Transport::EnumType::Sendmail );
00247
00248 KFileDialog dialog( KUrl( "/" ), QString(), this );
00249 dialog.setCaption( i18n( "Choose sendmail Location" ) );
00250
00251 if ( dialog.exec() == QDialog::Accepted ) {
00252 KUrl url = dialog.selectedUrl();
00253 if ( url.isEmpty() == true ) {
00254 return;
00255 }
00256 if ( !url.isLocalFile() ) {
00257 KMessageBox::sorry( this, i18n( "Only local files allowed." ) );
00258 return;
00259 }
00260 d->sendmail.kcfg_host->setText( url.path() );
00261 }
00262 }
00263
00264 void TransportConfigDialog::passwordsLoaded()
00265 {
00266 Q_ASSERT( d->passwordEdit );
00267
00268 if ( d->passwordEdit->text().isEmpty() ) {
00269 d->passwordEdit->setText( d->transport->password() );
00270 }
00271 }
00272
00273 static void checkHighestEnabledButton( QButtonGroup *group )
00274 {
00275 Q_ASSERT( group );
00276
00277 for ( int i = group->buttons().count() - 1; i >= 0; --i ) {
00278 QAbstractButton *b = group->buttons().at( i );
00279 if ( b && b->isEnabled() ) {
00280 b->animateClick();
00281 return;
00282 }
00283 }
00284 }
00285
00286 void TransportConfigDialog::slotFinished( QList<int> results )
00287 {
00288 d->smtp.checkCapabilities->setEnabled( true );
00289 d->serverTest->deleteLater();
00290
00291
00292
00293 if ( results.isEmpty() ) {
00294 d->serverTestFailed = true;
00295 return;
00296 }
00297
00298
00299 d->smtp.none->setEnabled( results.contains( Transport::EnumEncryption::None ) );
00300 d->smtp.ssl->setEnabled( results.contains( Transport::EnumEncryption::SSL ) );
00301 d->smtp.tls->setEnabled( results.contains( Transport::EnumEncryption::TLS ) );
00302 checkHighestEnabledButton( d->encryptionGroup );
00303
00304 d->noEncCapa = d->serverTest->normalProtocols();
00305 if ( d->smtp.tls->isEnabled() ) {
00306 d->tlsCapa = d->serverTest->tlsProtocols();
00307 } else {
00308 d->tlsCapa.clear();
00309 }
00310 d->sslCapa = d->serverTest->secureProtocols();
00311 d->updateAuthCapbilities();
00312 checkHighestEnabledButton( d->authGroup );
00313 }
00314
00315 void TransportConfigDialog::hostNameChanged( const QString &text )
00316 {
00317 d->resetAuthCapabilities();
00318 enableButton( Ok, !text.isEmpty() );
00319 for ( int i = 0; d->encryptionGroup && i < d->encryptionGroup->buttons().count(); i++ ) {
00320 d->encryptionGroup->buttons().at( i )->setEnabled( true );
00321 }
00322 }
00323
00324 void TransportConfigDialog::ensureValidAuthSelection()
00325 {
00326
00327 d->updateAuthCapbilities();
00328 foreach ( QAbstractButton *b, d->authGroup->buttons() ) {
00329 if ( b->isChecked() && !b->isEnabled() ) {
00330 checkHighestEnabledButton( d->authGroup );
00331 break;
00332 }
00333 }
00334 }
00335
00336 void TransportConfigDialog::encryptionChanged( int enc )
00337 {
00338 Q_ASSERT( d->transport->type() == Transport::EnumType::SMTP );
00339 kDebug() << enc;
00340
00341
00342 if ( enc == Transport::EnumEncryption::SSL ) {
00343 if ( d->smtp.kcfg_port->value() == SMTP_PORT ) {
00344 d->smtp.kcfg_port->setValue( SMTPS_PORT );
00345 }
00346 } else {
00347 if ( d->smtp.kcfg_port->value() == SMTPS_PORT ) {
00348 d->smtp.kcfg_port->setValue( SMTP_PORT );
00349 }
00350 }
00351
00352 ensureValidAuthSelection();
00353 }
00354
00355 #include "transportconfigdialog.moc"