• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 API Reference
  • KDE Home
  • Contact Us
 

mailtransport

  • mailtransport
smtpconfigwidget.cpp
1 /*
2  Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com>
3 
4  Based on MailTransport code by:
5  Copyright (c) 2006 - 2007 Volker Krause <vkrause@kde.org>
6  Copyright (c) 2007 KovoKs <kovoks@kovoks.nl>
7 
8  Based on KMail code by:
9  Copyright (c) 2001-2002 Michael Haeckel <haeckel@kde.org>
10 
11  This library is free software; you can redistribute it and/or modify it
12  under the terms of the GNU Library General Public License as published by
13  the Free Software Foundation; either version 2 of the License, or (at your
14  option) any later version.
15 
16  This library is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
19  License for more details.
20 
21  You should have received a copy of the GNU Library General Public License
22  along with this library; see the file COPYING.LIB. If not, write to the
23  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24  02110-1301, USA.
25 */
26 
27 #include "smtpconfigwidget.h"
28 #include "transportconfigwidget_p.h"
29 #include "transport.h"
30 #include "transportmanager.h"
31 #include "servertest.h"
32 #include "mailtransport_defs.h"
33 
34 #ifndef KDEPIM_MOBILE_UI
35 #include "ui_smtpsettings_desktop.h"
36 #else
37 #include "ui_smtpsettings_mobile.h"
38 #endif
39 
40 #include <QAbstractButton>
41 #include <QButtonGroup>
42 
43 #include <KProtocolInfo>
44 
45 namespace {
46 
47 // TODO: is this really necessary?
48 class BusyCursorHelper : public QObject
49 {
50  public:
51  inline BusyCursorHelper( QObject *parent ) : QObject( parent )
52  {
53 #ifndef QT_NO_CURSOR
54  qApp->setOverrideCursor( Qt::BusyCursor );
55 #endif
56  }
57 
58  inline ~BusyCursorHelper()
59  {
60 #ifndef QT_NO_CURSOR
61  qApp->restoreOverrideCursor();
62 #endif
63  }
64 };
65 
66 }
67 
68 using namespace MailTransport;
69 
70 class MailTransport::SMTPConfigWidgetPrivate : public TransportConfigWidgetPrivate
71 {
72  public:
73  ::Ui::SMTPSettings ui;
74 
75  ServerTest *serverTest;
76  QButtonGroup *encryptionGroup;
77 
78  // detected authentication capabilities
79  QList<int> noEncCapa, sslCapa, tlsCapa;
80 
81  bool serverTestFailed;
82 
83  static void addAuthenticationItem( KComboBox *combo,
84  int authenticationType )
85  {
86  combo->addItem( Transport::authenticationTypeString( authenticationType ),
87  QVariant( authenticationType ) );
88  }
89 
90  void resetAuthCapabilities()
91  {
92  noEncCapa.clear();
93  noEncCapa << Transport::EnumAuthenticationType::LOGIN
94  << Transport::EnumAuthenticationType::PLAIN
95  << Transport::EnumAuthenticationType::CRAM_MD5
96  << Transport::EnumAuthenticationType::DIGEST_MD5
97  << Transport::EnumAuthenticationType::NTLM
98  << Transport::EnumAuthenticationType::GSSAPI;
99  sslCapa = tlsCapa = noEncCapa;
100  updateAuthCapbilities();
101 
102  }
103 
104  void updateAuthCapbilities()
105  {
106  if ( serverTestFailed ) {
107  return;
108  }
109 
110  QList<int> capa = noEncCapa;
111  if ( ui.ssl->isChecked() ) {
112  capa = sslCapa;
113  } else if ( ui.tls->isChecked() ) {
114  capa = tlsCapa;
115  }
116 
117  ui.authCombo->clear();
118  foreach ( int authType, capa ) {
119  addAuthenticationItem( ui.authCombo, authType );
120  }
121 
122  if ( transport->isValid() ) {
123  const int idx = ui.authCombo->findData( transport->authenticationType() );
124 
125  if ( idx != -1 ) {
126  ui.authCombo->setCurrentIndex( idx );
127  }
128  }
129 
130  if ( capa.count() == 0 ) {
131  ui.noAuthPossible->setVisible( true );
132  ui.kcfg_requiresAuthentication->setChecked( false );
133  ui.kcfg_requiresAuthentication->setEnabled( false );
134  ui.kcfg_requiresAuthentication->setVisible( false );
135  ui.authCombo->setEnabled( false );
136  ui.authLabel->setEnabled( false );
137  } else {
138  ui.noAuthPossible->setVisible( false );
139  ui.kcfg_requiresAuthentication->setEnabled( true );
140  ui.kcfg_requiresAuthentication->setVisible( true );
141  ui.authCombo->setEnabled( true );
142  ui.authLabel->setEnabled( true );
143  }
144  }
145 };
146 
147 SMTPConfigWidget::SMTPConfigWidget( Transport *transport, QWidget *parent )
148  : TransportConfigWidget( *new SMTPConfigWidgetPrivate, transport, parent )
149 {
150  init();
151 }
152 
153 SMTPConfigWidget::SMTPConfigWidget( SMTPConfigWidgetPrivate &dd,
154  Transport *transport, QWidget *parent )
155  : TransportConfigWidget( dd, transport, parent )
156 {
157  init();
158 }
159 
160 static void checkHighestEnabledButton( QButtonGroup *group )
161 {
162  Q_ASSERT( group );
163 
164  for ( int i = group->buttons().count() - 1; i >= 0; --i ) {
165  QAbstractButton *b = group->buttons().at( i );
166  if ( b && b->isEnabled() ) {
167  b->animateClick();
168  return;
169  }
170  }
171 }
172 
173 void SMTPConfigWidget::init()
174 {
175  Q_D( SMTPConfigWidget );
176  d->serverTest = 0;
177 
178  connect( TransportManager::self(), SIGNAL(passwordsChanged()),
179  SLOT(passwordsLoaded()) );
180 
181  d->serverTestFailed = false;
182 
183  d->ui.setupUi( this );
184  d->manager->addWidget( this ); // otherwise it doesn't find out about these widgets
185  d->manager->updateWidgets();
186 
187  d->encryptionGroup = new QButtonGroup( this );
188  d->encryptionGroup->addButton( d->ui.none, Transport::EnumEncryption::None );
189  d->encryptionGroup->addButton( d->ui.ssl, Transport::EnumEncryption::SSL );
190  d->encryptionGroup->addButton( d->ui.tls, Transport::EnumEncryption::TLS );
191 
192  d->resetAuthCapabilities();
193 
194  if ( KProtocolInfo::capabilities( SMTP_PROTOCOL ).contains( QLatin1String( "SASL" ) ) == 0 ) {
195  d->ui.authCombo->removeItem( d->ui.authCombo->findData(
196  Transport::EnumAuthenticationType::NTLM ) );
197  d->ui.authCombo->removeItem( d->ui.authCombo->findData(
198  Transport::EnumAuthenticationType::GSSAPI ) );
199  }
200 
201  connect( d->ui.checkCapabilities, SIGNAL(clicked()),
202  SLOT(checkSmtpCapabilities()) );
203  connect( d->ui.kcfg_host, SIGNAL(textChanged(QString)),
204  SLOT(hostNameChanged(QString)) );
205  connect( d->encryptionGroup, SIGNAL(buttonClicked(int)),
206  SLOT(encryptionChanged(int)) );
207  connect( d->ui.kcfg_requiresAuthentication, SIGNAL(toggled(bool)),
208  SLOT(ensureValidAuthSelection()) );
209 
210  if ( !d->transport->isValid() ) {
211  checkHighestEnabledButton( d->encryptionGroup );
212  }
213 
214  // load the password
215  d->transport->updatePasswordState();
216  if ( d->transport->isComplete() ) {
217  d->ui.password->setText( d->transport->password() );
218  } else {
219  if ( d->transport->requiresAuthentication() ) {
220  TransportManager::self()->loadPasswordsAsync();
221  }
222  }
223 
224  hostNameChanged( d->transport->host() );
225 
226 #ifdef KDEPIM_MOBILE_UI
227  d->ui.smtpSettingsGroupBox->hide();
228 #endif
229 }
230 
231 void SMTPConfigWidget::checkSmtpCapabilities()
232 {
233  Q_D( SMTPConfigWidget );
234 
235  d->serverTest = new ServerTest( this );
236  d->serverTest->setProtocol( SMTP_PROTOCOL );
237  d->serverTest->setServer( d->ui.kcfg_host->text().trimmed() );
238  if ( d->ui.kcfg_specifyHostname->isChecked() ) {
239  d->serverTest->setFakeHostname( d->ui.kcfg_localHostname->text() );
240  }
241  d->serverTest->setProgressBar( d->ui.checkCapabilitiesProgress );
242  d->ui.checkCapabilitiesStack->setCurrentIndex( 1 );
243  BusyCursorHelper *busyCursorHelper = new BusyCursorHelper( d->serverTest );
244 
245  connect( d->serverTest, SIGNAL(finished(QList<int>)),
246  SLOT(slotFinished(QList<int>)));
247  connect( d->serverTest, SIGNAL(finished(QList<int>)),
248  busyCursorHelper, SLOT(deleteLater()) );
249  d->ui.checkCapabilities->setEnabled( false );
250  d->serverTest->start();
251  d->serverTestFailed = false;
252 }
253 
254 void SMTPConfigWidget::apply()
255 {
256  Q_D( SMTPConfigWidget );
257  Q_ASSERT( d->manager );
258  d->manager->updateSettings();
259  d->transport->setPassword( d->ui.password->text() );
260 
261  KConfigGroup group( d->transport->config(), d->transport->currentGroup() );
262  const int index = d->ui.authCombo->currentIndex();
263  if ( index >= 0 ) {
264  group.writeEntry( "authtype", d->ui.authCombo->itemData( index ).toInt() );
265  }
266 
267  TransportConfigWidget::apply();
268 }
269 
270 void SMTPConfigWidget::passwordsLoaded()
271 {
272  Q_D( SMTPConfigWidget );
273 
274  // Load the password from the original to our cloned copy
275  d->transport->updatePasswordState();
276 
277  if ( d->ui.password->text().isEmpty() ) {
278  d->ui.password->setText( d->transport->password() );
279  }
280 }
281 
282 // TODO rename
283 void SMTPConfigWidget::slotFinished( QList<int> results )
284 {
285  Q_D( SMTPConfigWidget );
286 
287  d->ui.checkCapabilitiesStack->setCurrentIndex( 0 );
288 
289  d->ui.checkCapabilities->setEnabled( true );
290  d->serverTest->deleteLater();
291 
292  // If the servertest did not find any useable authentication modes, assume the
293  // connection failed and don't disable any of the radioboxes.
294  if ( results.isEmpty() ) {
295  d->serverTestFailed = true;
296  return;
297  }
298 
299  // encryption method
300  d->ui.none->setEnabled( results.contains( Transport::EnumEncryption::None ) );
301  d->ui.ssl->setEnabled( results.contains( Transport::EnumEncryption::SSL ) );
302  d->ui.tls->setEnabled( results.contains( Transport::EnumEncryption::TLS ) );
303  checkHighestEnabledButton( d->encryptionGroup );
304 
305  d->noEncCapa = d->serverTest->normalProtocols();
306  if ( d->ui.tls->isEnabled() ) {
307  d->tlsCapa = d->serverTest->tlsProtocols();
308  } else {
309  d->tlsCapa.clear();
310  }
311  d->sslCapa = d->serverTest->secureProtocols();
312  d->updateAuthCapbilities();
313 }
314 
315 void SMTPConfigWidget::hostNameChanged( const QString &text )
316 {
317  // TODO: really? is this done at every change? wtf
318 
319  Q_D( SMTPConfigWidget );
320 
321  // sanitize hostname...
322  int pos = d->ui.kcfg_host->cursorPosition();
323  d->ui.kcfg_host->blockSignals( true );
324  d->ui.kcfg_host->setText( text.trimmed() );
325  d->ui.kcfg_host->blockSignals( false );
326  d->ui.kcfg_host->setCursorPosition( pos );
327 
328  d->resetAuthCapabilities();
329  for ( int i = 0; d->encryptionGroup && i < d->encryptionGroup->buttons().count(); i++ ) {
330  d->encryptionGroup->buttons().at( i )->setEnabled( true );
331  }
332 }
333 
334 void SMTPConfigWidget::ensureValidAuthSelection()
335 {
336  Q_D( SMTPConfigWidget );
337 
338  // adjust available authentication methods
339  d->updateAuthCapbilities();
340 }
341 
342 void SMTPConfigWidget::encryptionChanged( int enc )
343 {
344  Q_D( SMTPConfigWidget );
345  kDebug() << enc;
346 
347  // adjust port
348  if ( enc == Transport::EnumEncryption::SSL ) {
349  if ( d->ui.kcfg_port->value() == SMTP_PORT ) {
350  d->ui.kcfg_port->setValue( SMTPS_PORT );
351  }
352  } else {
353  if ( d->ui.kcfg_port->value() == SMTPS_PORT ) {
354  d->ui.kcfg_port->setValue( SMTP_PORT );
355  }
356  }
357 
358  ensureValidAuthSelection();
359 }
360 
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:26:52 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

mailtransport

Skip menu "mailtransport"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal