KDEUI
kpassworddialog.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2000 David Faure <faure@kde.org> 00003 Copyright (C) 2007 Olivier Goffart <ogoffart at kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License version 2 as published by the Free Software Foundation. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 #include "kpassworddialog.h" 00020 00021 #include <QCheckBox> 00022 #include <QLabel> 00023 #include <QLayout> 00024 #include <QTextDocument> 00025 #include <QTimer> 00026 00027 #include <kcombobox.h> 00028 #include <kconfig.h> 00029 #include <kiconloader.h> 00030 #include <klineedit.h> 00031 #include <klocale.h> 00032 #include <khbox.h> 00033 #include <kdebug.h> 00034 #include <kconfiggroup.h> 00035 #include <ktitlewidget.h> 00036 00037 #include "ui_kpassworddialog.h" 00038 00040 class KPasswordDialog::KPasswordDialogPrivate 00041 { 00042 public: 00043 KPasswordDialogPrivate(KPasswordDialog *q) 00044 : q(q), 00045 userEditCombo(0), 00046 pixmapLabel(0), 00047 commentRow(0) 00048 {} 00049 00050 void actuallyAccept(); 00051 void activated( const QString& userName ); 00052 00053 void updateFields(); 00054 void init(); 00055 00056 KPasswordDialog *q; 00057 KPasswordDialogFlags m_flags; 00058 Ui_KPasswordDialog ui; 00059 QMap<QString,QString> knownLogins; 00060 KComboBox* userEditCombo; 00061 QLabel* pixmapLabel; 00062 unsigned int commentRow; 00063 }; 00064 00065 KPasswordDialog::KPasswordDialog( QWidget* parent , 00066 const KPasswordDialogFlags& flags, 00067 const KDialog::ButtonCodes otherButtons ) 00068 : KDialog( parent ), d(new KPasswordDialogPrivate(this)) 00069 { 00070 setCaption( i18n("Password") ); 00071 setWindowIcon(KIcon("dialog-password")); 00072 setButtons( Ok | Cancel | otherButtons ); 00073 setDefaultButton( Ok ); 00074 d->m_flags = flags; 00075 d->init (); 00076 } 00077 00078 KPasswordDialog::~KPasswordDialog() 00079 { 00080 delete d; 00081 } 00082 00083 void KPasswordDialog::KPasswordDialogPrivate::updateFields() 00084 { 00085 if (q->anonymousMode()) 00086 { 00087 ui.userEdit->setEnabled( false ); 00088 ui.domainEdit->setEnabled( false ); 00089 ui.passEdit->setEnabled( false ); 00090 ui.keepCheckBox->setEnabled( false ); 00091 } 00092 else 00093 { 00094 ui.userEdit->setEnabled(!( m_flags & KPasswordDialog::UsernameReadOnly )); 00095 ui.domainEdit->setEnabled(!( m_flags & KPasswordDialog::DomainReadOnly )); 00096 ui.passEdit->setEnabled( true ); 00097 ui.keepCheckBox->setEnabled( true ); 00098 } 00099 } 00100 00101 void KPasswordDialog::KPasswordDialogPrivate::init() 00102 { 00103 ui.setupUi( q->mainWidget() ); 00104 ui.errorMessage->setHidden(true); 00105 00106 // Row 4: Username field 00107 if ( m_flags & KPasswordDialog::ShowUsernameLine ) { 00108 ui.userEdit->setFocus(); 00109 QObject::connect( ui.userEdit, SIGNAL(returnPressed()), ui.passEdit, SLOT(setFocus()) ); 00110 } else { 00111 ui.userNameLabel->hide(); 00112 ui.userEdit->hide(); 00113 ui.domainLabel->hide(); 00114 ui.domainEdit->hide(); 00115 ui.passEdit->setFocus(); 00116 } 00117 00118 if ( !( m_flags & KPasswordDialog::ShowAnonymousLoginCheckBox ) ) 00119 { 00120 ui.anonymousCheckBox->hide(); 00121 } 00122 else 00123 { 00124 QObject::connect( ui.anonymousCheckBox, SIGNAL(stateChanged (int)), q, SLOT(updateFields()) ); 00125 } 00126 00127 if ( !( m_flags & KPasswordDialog::ShowDomainLine ) ) 00128 { 00129 ui.domainLabel->hide(); 00130 ui.domainEdit->hide(); 00131 } 00132 00133 if ( !( m_flags & KPasswordDialog::ShowKeepPassword ) ) 00134 { 00135 ui.keepCheckBox->hide(); 00136 } 00137 00138 updateFields(); 00139 00140 QRect desktop = KGlobalSettings::desktopGeometry(q->topLevelWidget()); 00141 q->setMinimumWidth(qMin(1000, qMax(q->sizeHint().width(), desktop.width() / 4))); 00142 q->setPixmap(KIcon("dialog-password").pixmap(KIconLoader::SizeHuge)); 00143 } 00144 00145 void KPasswordDialog::setPixmap(const QPixmap &pixmap) 00146 { 00147 if ( !d->pixmapLabel ) 00148 { 00149 d->pixmapLabel = new QLabel( mainWidget() ); 00150 d->pixmapLabel->setAlignment( Qt::AlignLeft | Qt::AlignTop ); 00151 d->ui.hboxLayout->insertWidget( 0, d->pixmapLabel ); 00152 } 00153 00154 d->pixmapLabel->setPixmap( pixmap ); 00155 } 00156 00157 QPixmap KPasswordDialog::pixmap() const 00158 { 00159 if ( !d->pixmapLabel ) { 00160 return QPixmap(); 00161 } 00162 00163 return *d->pixmapLabel->pixmap(); 00164 } 00165 00166 00167 void KPasswordDialog::setUsername(const QString& user) 00168 { 00169 d->ui.userEdit->setText(user); 00170 if ( user.isEmpty() ) 00171 return; 00172 00173 d->activated(user); 00174 if ( d->ui.userEdit->isVisibleTo( this ) ) 00175 { 00176 d->ui.passEdit->setFocus(); 00177 } 00178 } 00179 00180 00181 QString KPasswordDialog::username() const 00182 { 00183 return d->ui.userEdit->text(); 00184 } 00185 00186 QString KPasswordDialog::password() const 00187 { 00188 return d->ui.passEdit->text(); 00189 } 00190 00191 void KPasswordDialog::setDomain(const QString& domain) 00192 { 00193 d->ui.domainEdit->setText(domain); 00194 } 00195 00196 QString KPasswordDialog::domain() const 00197 { 00198 return d->ui.domainEdit->text(); 00199 } 00200 00201 void KPasswordDialog::setAnonymousMode(bool anonymous) 00202 { 00203 d->ui.anonymousCheckBox->setChecked( anonymous ); 00204 } 00205 00206 bool KPasswordDialog::anonymousMode() const 00207 { 00208 return d->ui.anonymousCheckBox->isChecked(); 00209 } 00210 00211 00212 void KPasswordDialog::setKeepPassword( bool b ) 00213 { 00214 d->ui.keepCheckBox->setChecked( b ); 00215 } 00216 00217 bool KPasswordDialog::keepPassword() const 00218 { 00219 return d->ui.keepCheckBox->isChecked(); 00220 } 00221 00222 void KPasswordDialog::addCommentLine( const QString& label, 00223 const QString& comment ) 00224 { 00225 int gridMarginLeft, gridMarginTop, gridMarginRight, gridMarginBottom; 00226 d->ui.formLayout->getContentsMargins(&gridMarginLeft, &gridMarginTop, &gridMarginRight, &gridMarginBottom); 00227 00228 int spacing = d->ui.formLayout->horizontalSpacing(); 00229 if (spacing < 0) { 00230 // same inter-column spacing for all rows, see comment in qformlayout.cpp 00231 spacing = style()->combinedLayoutSpacing(QSizePolicy::Label, QSizePolicy::LineEdit, Qt::Horizontal, 0, this); 00232 } 00233 00234 QLabel* c = new QLabel(comment, mainWidget()); 00235 c->setWordWrap(true); 00236 00237 d->ui.formLayout->insertRow(d->commentRow, label, c); 00238 ++d->commentRow; 00239 00240 // cycle through column 0 widgets and see the max width so we can set the minimum height of 00241 // column 2 wordwrapable labels 00242 int firstColumnWidth = 0; 00243 for (int i = 0; i < d->ui.formLayout->rowCount(); ++i) { 00244 QLayoutItem *li = d->ui.formLayout->itemAt(i, QFormLayout::LabelRole); 00245 if (li) { 00246 QWidget *w = li->widget(); 00247 if (w && !w->isHidden()) { 00248 firstColumnWidth = qMax(firstColumnWidth, w->sizeHint().width()); 00249 } 00250 } 00251 } 00252 for (int i = 0; i < d->ui.formLayout->rowCount(); ++i) { 00253 QLayoutItem *li = d->ui.formLayout->itemAt(i, QFormLayout::FieldRole); 00254 if (li) { 00255 QLabel *l = qobject_cast<QLabel*>(li->widget()); 00256 if (l && l->wordWrap()) { 00257 int w = sizeHint().width() - firstColumnWidth - ( 2 * marginHint() ) - gridMarginLeft - gridMarginRight - spacing; 00258 l->setMinimumSize( w, l->heightForWidth(w) ); 00259 } 00260 } 00261 } 00262 } 00263 00264 void KPasswordDialog::showErrorMessage( const QString& message, const ErrorType type ) 00265 { 00266 d->ui.errorMessage->setText( message, KTitleWidget::ErrorMessage ); 00267 00268 QFont bold = font(); 00269 bold.setBold( true ); 00270 switch ( type ) { 00271 case PasswordError: 00272 d->ui.passwordLabel->setFont( bold ); 00273 d->ui.passEdit->clear(); 00274 d->ui.passEdit->setFocus(); 00275 break; 00276 case UsernameError: 00277 if ( d->ui.userEdit->isVisibleTo( this ) ) 00278 { 00279 d->ui.userNameLabel->setFont( bold ); 00280 d->ui.userEdit->setFocus(); 00281 } 00282 break; 00283 case DomainError: 00284 if ( d->ui.domainEdit->isVisibleTo( this ) ) 00285 { 00286 d->ui.domainLabel->setFont( bold ); 00287 d->ui.domainEdit->setFocus(); 00288 } 00289 break; 00290 case FatalError: 00291 d->ui.userNameLabel->setEnabled( false ); 00292 d->ui.userEdit->setEnabled( false ); 00293 d->ui.passwordLabel->setEnabled( false ); 00294 d->ui.passEdit->setEnabled( false ); 00295 d->ui.keepCheckBox->setEnabled( false ); 00296 enableButton( Ok, false ); 00297 break; 00298 default: 00299 break; 00300 } 00301 adjustSize(); 00302 } 00303 00304 void KPasswordDialog::setPrompt(const QString& prompt) 00305 { 00306 d->ui.prompt->setText( prompt ); 00307 d->ui.prompt->setWordWrap( true ); 00308 d->ui.prompt->setMinimumHeight( d->ui.prompt->heightForWidth( width() - ( 2 * marginHint() ) ) ); 00309 } 00310 00311 QString KPasswordDialog::prompt() const 00312 { 00313 return d->ui.prompt->text(); 00314 } 00315 00316 void KPasswordDialog::setPassword(const QString &p) 00317 { 00318 d->ui.passEdit->setText(p); 00319 } 00320 00321 void KPasswordDialog::setUsernameReadOnly( bool readOnly ) 00322 { 00323 d->ui.userEdit->setReadOnly( readOnly ); 00324 00325 if ( readOnly && d->ui.userEdit->hasFocus() ) { 00326 d->ui.passEdit->setFocus(); 00327 } 00328 } 00329 00330 void KPasswordDialog::setKnownLogins( const QMap<QString, QString>& knownLogins ) 00331 { 00332 const int nr = knownLogins.count(); 00333 if ( nr == 0 ) { 00334 return; 00335 } 00336 00337 if ( nr == 1 ) { 00338 d->ui.userEdit->setText( knownLogins.begin().key() ); 00339 setPassword( knownLogins.begin().value() ); 00340 return; 00341 } 00342 00343 Q_ASSERT( !d->ui.userEdit->isReadOnly() ); 00344 if ( !d->userEditCombo ) { 00345 d->ui.formLayout->removeWidget(d->ui.userEdit); 00346 delete d->ui.userEdit; 00347 d->userEditCombo = new KComboBox( true, mainWidget() ); 00348 d->ui.userEdit = d->userEditCombo->lineEdit(); 00349 // QSize s = d->userEditCombo->sizeHint(); 00350 // d->ui.userEditCombo->setFixedHeight( s.height() ); 00351 // d->ui.userEditCombo->setMinimumWidth( s.width() ); 00352 d->ui.userNameLabel->setBuddy( d->userEditCombo ); 00353 d->ui.formLayout->setWidget( d->commentRow, QFormLayout::FieldRole, d->userEditCombo ); 00354 setTabOrder( d->ui.userEdit, d->ui.anonymousCheckBox ); 00355 setTabOrder( d->ui.anonymousCheckBox, d->ui.domainEdit ); 00356 setTabOrder( d->ui.domainEdit, d->ui.passEdit ); 00357 setTabOrder( d->ui.passEdit, d->ui.keepCheckBox ); 00358 connect( d->ui.userEdit, SIGNAL(returnPressed()), d->ui.passEdit, SLOT(setFocus()) ); 00359 } 00360 00361 d->knownLogins = knownLogins; 00362 d->userEditCombo->addItems( knownLogins.keys() ); 00363 d->userEditCombo->setFocus(); 00364 00365 connect( d->userEditCombo, SIGNAL( activated( const QString& ) ), 00366 this, SLOT( activated( const QString& ) ) ); 00367 } 00368 00369 void KPasswordDialog::KPasswordDialogPrivate::activated( const QString& userName ) 00370 { 00371 QMap<QString, QString>::ConstIterator it = knownLogins.constFind( userName ); 00372 if ( it != knownLogins.constEnd() ) { 00373 q->setPassword( it.value() ); 00374 } 00375 } 00376 00377 void KPasswordDialog::accept() 00378 { 00379 if (!d->ui.errorMessage->isHidden()) d->ui.errorMessage->setText( QString() ); 00380 00381 // reset the font in case we had an error previously 00382 if (!d->ui.passwordLabel->isHidden()) d->ui.passwordLabel->setFont( font() ); 00383 if (!d->ui.passwordLabel->isHidden()) d->ui.userNameLabel->setFont( font() ); 00384 00385 // we do this to allow the error message, if any, to go away 00386 // checkPassword() may block for a period of time 00387 QTimer::singleShot( 0, this, SLOT(actuallyAccept()) ); 00388 } 00389 00390 void KPasswordDialog::KPasswordDialogPrivate::actuallyAccept() 00391 { 00392 if ( !q->checkPassword() ) 00393 { 00394 return; 00395 } 00396 00397 bool keep = ui.keepCheckBox->isVisibleTo( q ) && ui.keepCheckBox->isChecked(); 00398 emit q->gotPassword( q->password(), keep); 00399 00400 if ( ui.userEdit->isVisibleTo( q ) ) { 00401 emit q->gotUsernameAndPassword( q->username(), q->password() , keep); 00402 } 00403 00404 q->KDialog::accept(); 00405 } 00406 00407 bool KPasswordDialog::checkPassword() 00408 { 00409 return true; 00410 } 00411 00412 #include "kpassworddialog.moc"
KDE 4.6 API Reference