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