• Skip to content
  • Skip to link menu
KDE 4.5 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

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     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     // Row 4: Username field
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.formLayout->getContentsMargins(&gridMarginLeft, &gridMarginTop, &gridMarginRight, &gridMarginBottom);
00228 
00229     int spacing = d->ui.formLayout->horizontalSpacing();
00230     if (spacing < 0) {
00231         // same inter-column spacing for all rows, see comment in qformlayout.cpp
00232         spacing = style()->combinedLayoutSpacing(QSizePolicy::Label, QSizePolicy::LineEdit, Qt::Horizontal, 0, this);
00233     }
00234     
00235     QLabel* c = new QLabel(comment, mainWidget());
00236     c->setWordWrap(true);
00237 
00238     d->ui.formLayout->insertRow(d->commentRow, label, c);
00239     ++d->commentRow;
00240 
00241     // cycle through column 0 widgets and see the max width so we can set the minimum height of
00242     // column 2 wordwrapable labels
00243     int firstColumnWidth = 0;
00244     for (int i = 0; i < d->ui.formLayout->rowCount(); ++i) {
00245         QLayoutItem *li = d->ui.formLayout->itemAt(i, QFormLayout::LabelRole);
00246         if (li) {
00247             QWidget *w = li->widget();
00248             if (w) firstColumnWidth = qMax(firstColumnWidth, w->sizeHint().width());
00249         }
00250     }
00251     for (int i = 0; i < d->ui.formLayout->rowCount(); ++i) {
00252         QLayoutItem *li = d->ui.formLayout->itemAt(i, QFormLayout::FieldRole);
00253         if (li) {
00254             QLabel *l = qobject_cast<QLabel*>(li->widget());
00255             if (l && l->wordWrap()) l->setMinimumHeight( l->heightForWidth( width() - firstColumnWidth - ( 2 * marginHint() ) - gridMarginLeft - gridMarginRight - spacing ) );
00256         }
00257     }
00258 }
00259 
00260 void KPasswordDialog::showErrorMessage( const QString& message, const ErrorType type )
00261 {
00262     d->ui.errorMessage->setText( message, KTitleWidget::ErrorMessage );
00263 
00264     QFont bold = font();
00265     bold.setBold( true );
00266     switch ( type ) {
00267         case PasswordError:
00268             d->ui.passwordLabel->setFont( bold );
00269             d->ui.passEdit->clear();
00270             d->ui.passEdit->setFocus();
00271             break;
00272         case UsernameError:
00273             if ( d->ui.userEdit->isVisibleTo( this ) )
00274             {
00275                 d->ui.userNameLabel->setFont( bold );
00276                 d->ui.userEdit->setFocus();
00277             }
00278             break;
00279         case DomainError:
00280             if ( d->ui.domainEdit->isVisibleTo( this ) )
00281             {
00282                 d->ui.domainLabel->setFont( bold );
00283                 d->ui.domainEdit->setFocus();
00284             }            
00285             break;
00286         case FatalError:
00287             d->ui.userNameLabel->setEnabled( false );
00288             d->ui.userEdit->setEnabled( false );
00289             d->ui.passwordLabel->setEnabled( false );
00290             d->ui.passEdit->setEnabled( false );
00291             d->ui.keepCheckBox->setEnabled( false );
00292             enableButton( Ok, false );
00293             break;
00294         default:
00295             break;
00296     }
00297     adjustSize();
00298 }
00299 
00300 void KPasswordDialog::setPrompt(const QString& prompt)
00301 {
00302     d->ui.prompt->setText( prompt );
00303     d->ui.prompt->setWordWrap( true );
00304     d->ui.prompt->setMinimumHeight( d->ui.prompt->heightForWidth( width() -  ( 2 * marginHint() ) ) );
00305 }
00306 
00307 QString KPasswordDialog::prompt() const
00308 {
00309     return d->ui.prompt->text();
00310 }
00311 
00312 void KPasswordDialog::setPassword(const QString &p)
00313 {
00314     d->ui.passEdit->setText(p);
00315 }
00316 
00317 void KPasswordDialog::setUsernameReadOnly( bool readOnly )
00318 {
00319     d->ui.userEdit->setReadOnly( readOnly );
00320 
00321     if ( readOnly && d->ui.userEdit->hasFocus() ) {
00322         d->ui.passEdit->setFocus();
00323     }
00324 }
00325 
00326 void KPasswordDialog::setKnownLogins( const QMap<QString, QString>& knownLogins )
00327 {
00328     const int nr = knownLogins.count();
00329     if ( nr == 0 ) {
00330         return;
00331     }
00332 
00333     if ( nr == 1 ) {
00334         d->ui.userEdit->setText( knownLogins.begin().key() );
00335         setPassword( knownLogins.begin().value() );
00336         return;
00337     }
00338 
00339     Q_ASSERT( !d->ui.userEdit->isReadOnly() );
00340     if ( !d->userEditCombo ) {
00341         d->ui.formLayout->removeWidget(d->ui.userEdit);
00342         delete d->ui.userEdit;
00343         d->userEditCombo = new KComboBox( true, mainWidget() );
00344         d->ui.userEdit = d->userEditCombo->lineEdit();
00345 //        QSize s = d->userEditCombo->sizeHint();
00346 //        d->ui.userEditCombo->setFixedHeight( s.height() );
00347 //        d->ui.userEditCombo->setMinimumWidth( s.width() );
00348         d->ui.userNameLabel->setBuddy( d->userEditCombo );
00349         d->ui.formLayout->setWidget( d->commentRow, QFormLayout::FieldRole, d->userEditCombo );
00350         setTabOrder( d->ui.userEdit, d->ui.anonymousCheckBox );
00351         setTabOrder( d->ui.anonymousCheckBox, d->ui.domainEdit );
00352         setTabOrder( d->ui.domainEdit, d->ui.passEdit );
00353         setTabOrder( d->ui.passEdit, d->ui.keepCheckBox );
00354         connect( d->ui.userEdit, SIGNAL(returnPressed()), d->ui.passEdit, SLOT(setFocus()) );
00355     }
00356 
00357     d->knownLogins = knownLogins;
00358     d->userEditCombo->addItems( knownLogins.keys() );
00359     d->userEditCombo->setFocus();
00360 
00361     connect( d->userEditCombo, SIGNAL( activated( const QString& ) ),
00362              this, SLOT( activated( const QString& ) ) );
00363 }
00364 
00365 void KPasswordDialog::KPasswordDialogPrivate::activated( const QString& userName )
00366 {
00367     QMap<QString, QString>::ConstIterator it = knownLogins.constFind( userName );
00368     if ( it != knownLogins.constEnd() ) {
00369         q->setPassword( it.value() );
00370     }
00371 }
00372 
00373 void KPasswordDialog::accept()
00374 {
00375     if (!d->ui.errorMessage->isHidden()) d->ui.errorMessage->setText( QString() );
00376 
00377     // reset the font in case we had an error previously
00378     if (!d->ui.passwordLabel->isHidden()) d->ui.passwordLabel->setFont( font() );
00379     if (!d->ui.passwordLabel->isHidden()) d->ui.userNameLabel->setFont( font() );
00380 
00381     // we do this to allow the error message, if any, to go away
00382     // checkPassword() may block for a period of time
00383     QTimer::singleShot( 0, this, SLOT(actuallyAccept()) );
00384 }
00385 
00386 void KPasswordDialog::KPasswordDialogPrivate::actuallyAccept()
00387 {
00388     if ( !q->checkPassword() )
00389     {
00390         return;
00391     }
00392 
00393     bool keep = ui.keepCheckBox->isVisibleTo( q ) && ui.keepCheckBox->isChecked();
00394     emit q->gotPassword( q->password(), keep);
00395 
00396     if ( ui.userEdit->isVisibleTo( q ) ) {
00397         emit q->gotUsernameAndPassword( q->username(), q->password() , keep);
00398     }
00399 
00400     q->KDialog::accept();
00401 }
00402 
00403 bool KPasswordDialog::checkPassword()
00404 {
00405     return true;
00406 }
00407 
00408 #include "kpassworddialog.moc"

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  •     Sodep
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.9-20090814
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal