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 ui.keepCheckBox->setEnabled( false );
00092 }
00093 else
00094 {
00095 ui.userEdit->setEnabled(!( m_flags & KPasswordDialog::UsernameReadOnly ));
00096 ui.domainEdit->setEnabled(!( m_flags & KPasswordDialog::DomainReadOnly ));
00097 ui.passEdit->setEnabled( true );
00098 ui.keepCheckBox->setEnabled( true );
00099 }
00100 }
00101
00102 void KPasswordDialog::KPasswordDialogPrivate::init()
00103 {
00104 ui.setupUi( q->mainWidget() );
00105 ui.errorMessage->setHidden(true);
00106
00107
00108 if ( m_flags & KPasswordDialog::ShowUsernameLine ) {
00109 ui.userEdit->setFocus();
00110 QObject::connect( ui.userEdit, SIGNAL(returnPressed()), ui.passEdit, SLOT(setFocus()) );
00111 } else {
00112 ui.userNameLabel->hide();
00113 ui.userEdit->hide();
00114 ui.domainLabel->hide();
00115 ui.domainEdit->hide();
00116 ui.passEdit->setFocus();
00117 }
00118
00119 if ( !( m_flags & KPasswordDialog::ShowAnonymousLoginCheckBox ) )
00120 {
00121 ui.anonymousCheckBox->hide();
00122 }
00123 else
00124 {
00125 QObject::connect( ui.anonymousCheckBox, SIGNAL(stateChanged (int)), q, SLOT(updateFields()) );
00126 }
00127
00128 if ( !( m_flags & KPasswordDialog::ShowDomainLine ) )
00129 {
00130 ui.domainLabel->hide();
00131 ui.domainEdit->hide();
00132 }
00133
00134 if ( !( m_flags & KPasswordDialog::ShowKeepPassword ) )
00135 {
00136 ui.keepCheckBox->hide();
00137 }
00138
00139 updateFields();
00140
00141 QRect desktop = KGlobalSettings::desktopGeometry(q->topLevelWidget());
00142 q->setMinimumWidth(qMin(1000, qMax(400, desktop.width() / 4)));
00143 q->setPixmap(KIcon("dialog-password").pixmap(KIconLoader::SizeHuge));
00144 }
00145
00146 void KPasswordDialog::setPixmap(const QPixmap &pixmap)
00147 {
00148 if ( !d->pixmapLabel )
00149 {
00150 d->pixmapLabel = new QLabel( mainWidget() );
00151 d->pixmapLabel->setAlignment( Qt::AlignLeft | Qt::AlignTop );
00152 d->ui.hboxLayout->insertWidget( 0, d->pixmapLabel );
00153 }
00154
00155 d->pixmapLabel->setPixmap( pixmap );
00156 }
00157
00158 QPixmap KPasswordDialog::pixmap() const
00159 {
00160 if ( !d->pixmapLabel ) {
00161 return QPixmap();
00162 }
00163
00164 return *d->pixmapLabel->pixmap();
00165 }
00166
00167
00168 void KPasswordDialog::setUsername(const QString& user)
00169 {
00170 d->ui.userEdit->setText(user);
00171 if ( user.isEmpty() )
00172 return;
00173
00174 d->activated(user);
00175 if ( d->ui.userEdit->isVisibleTo( this ) )
00176 {
00177 d->ui.passEdit->setFocus();
00178 }
00179 }
00180
00181
00182 QString KPasswordDialog::username() const
00183 {
00184 return d->ui.userEdit->text();
00185 }
00186
00187 QString KPasswordDialog::password() const
00188 {
00189 return d->ui.passEdit->text();
00190 }
00191
00192 void KPasswordDialog::setDomain(const QString& domain)
00193 {
00194 d->ui.domainEdit->setText(domain);
00195 }
00196
00197 QString KPasswordDialog::domain() const
00198 {
00199 return d->ui.domainEdit->text();
00200 }
00201
00202 void KPasswordDialog::setAnonymousMode(bool anonymous)
00203 {
00204 d->ui.anonymousCheckBox->setChecked( anonymous );
00205 }
00206
00207 bool KPasswordDialog::anonymousMode() const
00208 {
00209 return d->ui.anonymousCheckBox->isChecked();
00210 }
00211
00212
00213 void KPasswordDialog::setKeepPassword( bool b )
00214 {
00215 d->ui.keepCheckBox->setChecked( b );
00216 }
00217
00218 bool KPasswordDialog::keepPassword() const
00219 {
00220 return d->ui.keepCheckBox->isChecked();
00221 }
00222
00223 void KPasswordDialog::addCommentLine( const QString& label,
00224 const QString& comment )
00225 {
00226 int gridMarginLeft, gridMarginTop, gridMarginRight, gridMarginBottom;
00227 d->ui.gridLayout->getContentsMargins(&gridMarginLeft, &gridMarginTop, &gridMarginRight, &gridMarginBottom);
00228
00229 QLabel* l = new QLabel(label, mainWidget());
00230 QLabel* c = new QLabel(comment, mainWidget());
00231 c->setWordWrap(true);
00232
00233 d->ui.gridLayout->addWidget(l, d->commentRow, 0);
00234 d->ui.gridLayout->addWidget(c, d->commentRow, 1);
00235 ++d->commentRow;
00236 d->ui.gridLayout->addWidget(d->ui.userNameLabel, d->commentRow, 0);
00237 d->ui.gridLayout->addWidget(d->ui.userEdit, d->commentRow, 1);
00238 d->ui.gridLayout->addWidget(d->ui.anonymousCheckBox, d->commentRow + 1, 1);
00239 d->ui.gridLayout->addWidget(d->ui.domainLabel, d->commentRow + 2, 0);
00240 d->ui.gridLayout->addWidget(d->ui.domainEdit, d->commentRow + 2, 1);
00241 d->ui.gridLayout->addWidget(d->ui.passwordLabel, d->commentRow + 3, 0);
00242 d->ui.gridLayout->addWidget(d->ui.passEdit, d->commentRow + 3, 1);
00243 d->ui.gridLayout->addWidget(d->ui.keepCheckBox, d->commentRow + 4, 1);
00244
00245
00246
00247 int firstColumnWidth = 0;
00248 for (int i = 0; i < d->ui.gridLayout->rowCount(); ++i) {
00249 QLayoutItem *li = d->ui.gridLayout->itemAtPosition(i, 0);
00250 if (li) {
00251 QWidget *w = li->widget();
00252 if (w) firstColumnWidth = qMax(firstColumnWidth, w->sizeHint().width());
00253 }
00254 }
00255 for (int i = 0; i < d->ui.gridLayout->rowCount(); ++i) {
00256 QLayoutItem *li = d->ui.gridLayout->itemAtPosition(i, 1);
00257 if (li) {
00258 QLabel *l = qobject_cast<QLabel*>(li->widget());
00259 if (l && l->wordWrap()) l->setMinimumHeight( l->heightForWidth( width() - firstColumnWidth - ( 2 * marginHint() ) - gridMarginLeft - gridMarginRight - d->ui.gridLayout->spacing() ) );
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 delete d->ui.userEdit;
00346 d->userEditCombo = new KComboBox( true, mainWidget() );
00347 d->ui.userEdit = d->userEditCombo->lineEdit();
00348
00349
00350
00351 d->ui.userNameLabel->setBuddy( d->userEditCombo );
00352 d->ui.gridLayout->addWidget( d->userEditCombo, d->commentRow, 1 );
00353 connect( d->ui.userEdit, SIGNAL(returnPressed()), d->ui.passEdit, SLOT(setFocus()) );
00354 }
00355
00356 d->knownLogins = knownLogins;
00357 d->userEditCombo->addItems( knownLogins.keys() );
00358 d->userEditCombo->setFocus();
00359
00360 connect( d->userEditCombo, SIGNAL( activated( const QString& ) ),
00361 this, SLOT( activated( const QString& ) ) );
00362 }
00363
00364 void KPasswordDialog::KPasswordDialogPrivate::activated( const QString& userName )
00365 {
00366 QMap<QString, QString>::ConstIterator it = knownLogins.constFind( userName );
00367 if ( it != knownLogins.constEnd() ) {
00368 q->setPassword( it.value() );
00369 }
00370 }
00371
00372 void KPasswordDialog::accept()
00373 {
00374 if (!d->ui.errorMessage->isHidden()) d->ui.errorMessage->setText( QString() );
00375
00376
00377 if (!d->ui.passwordLabel->isHidden()) d->ui.passwordLabel->setFont( font() );
00378 if (!d->ui.passwordLabel->isHidden()) d->ui.userNameLabel->setFont( font() );
00379
00380
00381
00382 QTimer::singleShot( 0, this, SLOT(actuallyAccept()) );
00383 }
00384
00385 void KPasswordDialog::KPasswordDialogPrivate::actuallyAccept()
00386 {
00387 if ( !q->checkPassword() )
00388 {
00389 return;
00390 }
00391
00392 bool keep = ui.keepCheckBox->isVisibleTo( q ) && ui.keepCheckBox->isChecked();
00393 emit q->gotPassword( q->password(), keep);
00394
00395 if ( ui.userEdit->isVisibleTo( q ) ) {
00396 emit q->gotUsernameAndPassword( q->username(), q->password() , keep);
00397 }
00398
00399 q->KDialog::accept();
00400 }
00401
00402 bool KPasswordDialog::checkPassword()
00403 {
00404 return true;
00405 }
00406
00407 #include "kpassworddialog.moc"