00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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 showButtonSeparator( true );
00074 setDefaultButton( Ok );
00075 d->m_flags = flags;
00076 d->init ();
00077 }
00078
00079 KPasswordDialog::~KPasswordDialog()
00080 {
00081 delete d;
00082 }
00083
00084 void KPasswordDialog::KPasswordDialogPrivate::updateFields()
00085 {
00086 if (q->anonymousMode())
00087 {
00088 ui.userEdit->setEnabled( false );
00089 ui.domainEdit->setEnabled( false );
00090 ui.passEdit->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 }
00098 }
00099
00100 void KPasswordDialog::KPasswordDialogPrivate::init()
00101 {
00102 ui.setupUi( q->mainWidget() );
00103 ui.errorMessage->setHidden(true);
00104
00105
00106 if ( m_flags & KPasswordDialog::ShowUsernameLine ) {
00107 ui.userEdit->setFocus();
00108 QObject::connect( ui.userEdit, SIGNAL(returnPressed()), ui.passEdit, SLOT(setFocus()) );
00109 } else {
00110 ui.userNameLabel->hide();
00111 ui.userEdit->hide();
00112 ui.domainLabel->hide();
00113 ui.domainEdit->hide();
00114 ui.passEdit->setFocus();
00115 }
00116
00117 if ( !( m_flags & KPasswordDialog::ShowAnonymousLoginCheckBox ) )
00118 {
00119 ui.anonymousCheckBox->hide();
00120 }
00121 else
00122 {
00123 QObject::connect( ui.anonymousCheckBox, SIGNAL(stateChanged (int)), q, SLOT(updateFields()) );
00124 }
00125
00126 if ( !( m_flags & KPasswordDialog::ShowDomainLine ) )
00127 {
00128 ui.domainLabel->hide();
00129 ui.domainEdit->hide();
00130 }
00131
00132 if ( !( m_flags & KPasswordDialog::ShowKeepPassword ) )
00133 {
00134 ui.keepCheckBox->hide();
00135 }
00136
00137 updateFields();
00138
00139 QRect desktop = KGlobalSettings::desktopGeometry(q->topLevelWidget());
00140 q->setFixedWidth(qMin(1000, qMax(400, desktop.width() / 4)));
00141 q->setPixmap(KIcon("dialog-password").pixmap(KIconLoader::SizeHuge));
00142 }
00143
00144 void KPasswordDialog::setPixmap(const QPixmap &pixmap)
00145 {
00146 if ( !d->pixmapLabel )
00147 {
00148 d->pixmapLabel = new QLabel( mainWidget() );
00149 d->pixmapLabel->setAlignment( Qt::AlignLeft | Qt::AlignTop );
00150 d->ui.hboxLayout->insertWidget( 0, d->pixmapLabel );
00151 }
00152
00153 d->pixmapLabel->setPixmap( pixmap );
00154 }
00155
00156 QPixmap KPasswordDialog::pixmap() const
00157 {
00158 if ( !d->pixmapLabel ) {
00159 return QPixmap();
00160 }
00161
00162 return *d->pixmapLabel->pixmap();
00163 }
00164
00165
00166 void KPasswordDialog::setUsername(const QString& user)
00167 {
00168 d->ui.userEdit->setText(user);
00169 if ( user.isEmpty() )
00170 return;
00171
00172 d->activated(user);
00173 if ( d->ui.userEdit->isVisibleTo( this ) )
00174 {
00175 d->ui.passEdit->setFocus();
00176 }
00177 }
00178
00179
00180 QString KPasswordDialog::username() const
00181 {
00182 return d->ui.userEdit->text();
00183 }
00184
00185 QString KPasswordDialog::password() const
00186 {
00187 return d->ui.passEdit->text();
00188 }
00189
00190 void KPasswordDialog::setDomain(const QString& domain)
00191 {
00192 d->ui.domainEdit->setText(domain);
00193 }
00194
00195 QString KPasswordDialog::domain() const
00196 {
00197 return d->ui.domainEdit->text();
00198 }
00199
00200 void KPasswordDialog::setAnonymousMode(bool anonymous)
00201 {
00202 d->ui.anonymousCheckBox->setChecked( anonymous );
00203 }
00204
00205 bool KPasswordDialog::anonymousMode() const
00206 {
00207 return d->ui.anonymousCheckBox->isChecked();
00208 }
00209
00210
00211 void KPasswordDialog::setKeepPassword( bool b )
00212 {
00213 d->ui.keepCheckBox->setChecked( b );
00214 }
00215
00216 bool KPasswordDialog::keepPassword() const
00217 {
00218 return d->ui.keepCheckBox->isChecked();
00219 }
00220
00221 void KPasswordDialog::addCommentLine( const QString& label,
00222 const QString& comment )
00223 {
00224 int gridMarginLeft, gridMarginTop, gridMarginRight, gridMarginBottom;
00225 d->ui.gridLayout->getContentsMargins(&gridMarginLeft, &gridMarginTop, &gridMarginRight, &gridMarginBottom);
00226
00227 QLabel* l = new QLabel(label, mainWidget());
00228 QLabel* c = new QLabel(comment, mainWidget());
00229 c->setWordWrap(true);
00230
00231 d->ui.gridLayout->addWidget(l, d->commentRow, 0);
00232 d->ui.gridLayout->addWidget(c, d->commentRow, 1);
00233 ++d->commentRow;
00234 d->ui.gridLayout->addWidget(d->ui.userNameLabel, d->commentRow, 0);
00235 d->ui.gridLayout->addWidget(d->ui.userEdit, d->commentRow, 1);
00236 d->ui.gridLayout->addWidget(d->ui.anonymousCheckBox, d->commentRow + 1, 1);
00237 d->ui.gridLayout->addWidget(d->ui.domainLabel, d->commentRow + 2, 0);
00238 d->ui.gridLayout->addWidget(d->ui.domainEdit, d->commentRow + 2, 1);
00239 d->ui.gridLayout->addWidget(d->ui.passwordLabel, d->commentRow + 3, 0);
00240 d->ui.gridLayout->addWidget(d->ui.passEdit, d->commentRow + 3, 1);
00241 d->ui.gridLayout->addWidget(d->ui.keepCheckBox, d->commentRow + 4, 1);
00242
00243
00244
00245 int firstColumnWidth = 0;
00246 for (int i = 0; i < d->ui.gridLayout->rowCount(); ++i) {
00247 QLayoutItem *li = d->ui.gridLayout->itemAtPosition(i, 0);
00248 if (li) {
00249 QWidget *w = li->widget();
00250 if (w) firstColumnWidth = qMax(firstColumnWidth, w->sizeHint().width());
00251 }
00252 }
00253 for (int i = 0; i < d->ui.gridLayout->rowCount(); ++i) {
00254 QLayoutItem *li = d->ui.gridLayout->itemAtPosition(i, 1);
00255 if (li) {
00256 QLabel *l = qobject_cast<QLabel*>(li->widget());
00257 if (l && l->wordWrap()) l->setMinimumHeight( l->heightForWidth( width() - firstColumnWidth - ( 2 * marginHint() ) - gridMarginLeft - gridMarginRight - d->ui.gridLayout->spacing() ) );
00258 }
00259 }
00260 }
00261
00262 void KPasswordDialog::showErrorMessage( const QString& message, const ErrorType type )
00263 {
00264 d->ui.errorMessage->setText( message, KTitleWidget::ErrorMessage );
00265
00266 QFont bold = font();
00267 bold.setBold( true );
00268 switch ( type ) {
00269 case PasswordError:
00270 d->ui.passwordLabel->setFont( bold );
00271 d->ui.passEdit->clear();
00272 d->ui.passEdit->setFocus();
00273 break;
00274 case UsernameError:
00275 if ( d->ui.userEdit->isVisibleTo( this ) )
00276 {
00277 d->ui.userNameLabel->setFont( bold );
00278 d->ui.userEdit->setFocus();
00279 }
00280 break;
00281 case DomainError:
00282 if ( d->ui.domainEdit->isVisibleTo( this ) )
00283 {
00284 d->ui.domainLabel->setFont( bold );
00285 d->ui.domainEdit->setFocus();
00286 }
00287 break;
00288 case FatalError:
00289 d->ui.userNameLabel->setEnabled( false );
00290 d->ui.userEdit->setEnabled( false );
00291 d->ui.passwordLabel->setEnabled( false );
00292 d->ui.passEdit->setEnabled( false );
00293 d->ui.keepCheckBox->setEnabled( false );
00294 enableButton( Ok, false );
00295 break;
00296 default:
00297 break;
00298 }
00299 adjustSize();
00300 }
00301
00302 void KPasswordDialog::setPrompt(const QString& prompt)
00303 {
00304 d->ui.prompt->setText( prompt );
00305 d->ui.prompt->setWordWrap( true );
00306 d->ui.prompt->setMinimumHeight( d->ui.prompt->heightForWidth( width() - ( 2 * marginHint() ) ) );
00307 }
00308
00309 QString KPasswordDialog::prompt() const
00310 {
00311 return d->ui.prompt->text();
00312 }
00313
00314 void KPasswordDialog::setPassword(const QString &p)
00315 {
00316 d->ui.passEdit->setText(p);
00317 }
00318
00319 void KPasswordDialog::setUsernameReadOnly( bool readOnly )
00320 {
00321 d->ui.userEdit->setReadOnly( readOnly );
00322
00323 if ( readOnly && d->ui.userEdit->hasFocus() ) {
00324 d->ui.passEdit->setFocus();
00325 }
00326 }
00327
00328 void KPasswordDialog::setKnownLogins( const QMap<QString, QString>& knownLogins )
00329 {
00330 const int nr = knownLogins.count();
00331 if ( nr == 0 ) {
00332 return;
00333 }
00334
00335 if ( nr == 1 ) {
00336 d->ui.userEdit->setText( knownLogins.begin().key() );
00337 setPassword( knownLogins.begin().value() );
00338 return;
00339 }
00340
00341 Q_ASSERT( !d->ui.userEdit->isReadOnly() );
00342 if ( !d->userEditCombo ) {
00343 delete d->ui.userEdit;
00344 d->userEditCombo = new KComboBox( true, mainWidget() );
00345 d->ui.userEdit = d->userEditCombo->lineEdit();
00346
00347
00348
00349 d->ui.userNameLabel->setBuddy( d->userEditCombo );
00350 d->ui.gridLayout->addWidget( d->userEditCombo, d->commentRow, 1 );
00351 connect( d->ui.userEdit, SIGNAL(returnPressed()), d->ui.passEdit, SLOT(setFocus()) );
00352 }
00353
00354 d->knownLogins = knownLogins;
00355 d->userEditCombo->addItems( knownLogins.keys() );
00356 d->userEditCombo->setFocus();
00357
00358 connect( d->userEditCombo, SIGNAL( activated( const QString& ) ),
00359 this, SLOT( activated( const QString& ) ) );
00360 }
00361
00362 void KPasswordDialog::KPasswordDialogPrivate::activated( const QString& userName )
00363 {
00364 QMap<QString, QString>::ConstIterator it = knownLogins.constFind( userName );
00365 if ( it != knownLogins.constEnd() ) {
00366 q->setPassword( it.value() );
00367 }
00368 }
00369
00370 void KPasswordDialog::accept()
00371 {
00372 if (!d->ui.errorMessage->isHidden()) d->ui.errorMessage->setText( QString() );
00373
00374
00375 if (!d->ui.passwordLabel->isHidden()) d->ui.passwordLabel->setFont( font() );
00376 if (!d->ui.passwordLabel->isHidden()) d->ui.userNameLabel->setFont( font() );
00377
00378
00379
00380 QTimer::singleShot( 0, this, SLOT(actuallyAccept()) );
00381 }
00382
00383 void KPasswordDialog::KPasswordDialogPrivate::actuallyAccept()
00384 {
00385 if ( !q->checkPassword() )
00386 {
00387 return;
00388 }
00389
00390 bool keep = ui.keepCheckBox->isVisibleTo( q ) && ui.keepCheckBox->isChecked();
00391 emit q->gotPassword( q->password(), keep);
00392
00393 if ( ui.userEdit->isVisibleTo( q ) ) {
00394 emit q->gotUsernameAndPassword( q->username(), q->password() , keep);
00395 }
00396
00397 q->KDialog::accept();
00398 }
00399
00400 bool KPasswordDialog::checkPassword()
00401 {
00402 return true;
00403 }
00404
00405 #include "kpassworddialog.moc"