kio Library API Documentation

passdlg.cpp

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2000 David Faure <faure@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00016    Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #include <qlineedit.h>
00020 #include <qpushbutton.h>
00021 #include <qcheckbox.h>
00022 #include <qlabel.h>
00023 #include <qlayout.h>
00024 #include <qaccel.h>
00025 #include <qhbox.h>
00026 
00027 #include <kapplication.h>
00028 #include <kconfig.h>
00029 #include <klocale.h>
00030 #include <kbuttonbox.h>
00031 #include <kstandarddirs.h>
00032 #include <kseparator.h>
00033 
00034 #include "passdlg.h"
00035 
00036 using namespace KIO;
00037 
00038 struct PasswordDialog::PasswordDialogPrivate
00039 {
00040     QGridLayout *layout;
00041     QLineEdit* userEdit;
00042     QLineEdit* passEdit;
00043     QLabel* prompt;
00044 
00045     bool keep;
00046     short unsigned int nRow;
00047 };
00048 
00049 PasswordDialog::PasswordDialog( const QString& prompt, const QString& user,
00050                                 bool enableKeep, bool modal, QWidget* parent,
00051                                 const char* name )
00052                :KDialogBase( parent, name, modal, i18n("Password"), Ok|Cancel, Ok, true)
00053 {
00054     init ( prompt, user, enableKeep );
00055 }
00056 
00057 PasswordDialog::~PasswordDialog()
00058 {
00059     delete d;
00060 }
00061 
00062 void PasswordDialog::init( const QString& prompt, const QString& user,
00063                            bool enableKeep  )
00064 {
00065     QWidget *main = makeMainWidget();
00066 
00067     d = new PasswordDialogPrivate;
00068     d->keep = false;
00069     d->nRow = 0;
00070 
00071     KConfig* cfg = KGlobal::config();
00072     KConfigGroupSaver saver( cfg, "Passwords" );
00073 
00074     d->layout = new QGridLayout( main, 9, 3, spacingHint(), marginHint());
00075     d->layout->addColSpacing(1, 5);
00076 
00077     // Row 0: pixmap  prompt
00078     QLabel* lbl;
00079     QPixmap pix(locate("data", QString::fromLatin1("kdeui/pics/keys.png")));
00080     if ( !pix.isNull() )
00081     {
00082         lbl = new QLabel( main );
00083         lbl->setPixmap( pix );
00084         lbl->setAlignment( Qt::AlignLeft|Qt::AlignVCenter );
00085         lbl->setFixedSize( lbl->sizeHint() );
00086         d->layout->addWidget( lbl, 0, 0, Qt::AlignLeft );
00087     }
00088     d->prompt = new QLabel( main );
00089     d->prompt->setAlignment( Qt::AlignLeft|Qt::AlignVCenter|Qt::WordBreak );
00090     d->layout->addWidget( d->prompt, 0, 2, Qt::AlignLeft );
00091     if ( prompt.isEmpty() )
00092         setPrompt( i18n( "You need to supply a username and a password" ) );
00093     else
00094         setPrompt( prompt );
00095 
00096     // Row 1: Row Spacer
00097     d->layout->addRowSpacing( 1, 7 );
00098 
00099     // Row 2-3: Reserved for an additional comment
00100 
00101     // Row 4: Username field
00102     lbl = new QLabel( i18n("&Username:"), main );
00103     lbl->setAlignment( Qt::AlignVCenter | Qt::AlignLeft );
00104     lbl->setFixedSize( lbl->sizeHint() );
00105     QHBox* hbox = new QHBox( main );
00106     d->userEdit = new QLineEdit( hbox );
00107     lbl->setBuddy( d->userEdit );
00108     QSize s = d->userEdit->sizeHint();
00109     d->userEdit->setFixedHeight( s.height() );
00110     d->userEdit->setMinimumWidth( s.width() );
00111     lbl->setBuddy( d->userEdit );
00112     d->layout->addWidget( lbl, 4, 0 );
00113     d->layout->addWidget( hbox, 4, 2 );
00114 
00115     // Row 5: Row spacer
00116     d->layout->addRowSpacing( 5, 4 );
00117 
00118     // Row 6: Password field
00119     lbl = new QLabel( i18n("&Password:"), main );
00120     lbl->setAlignment( Qt::AlignVCenter | Qt::AlignLeft );
00121     lbl->setFixedSize( lbl->sizeHint() );
00122     hbox = new QHBox( main );
00123     d->passEdit = new QLineEdit( hbox );
00124     if ( cfg->readEntry("EchoMode", "OneStar") == "NoEcho" )
00125         d->passEdit->setEchoMode( QLineEdit::NoEcho );
00126     else
00127         d->passEdit->setEchoMode( QLineEdit::Password );
00128     lbl->setBuddy( d->passEdit );
00129     s = d->passEdit->sizeHint();
00130     d->passEdit->setFixedHeight( s.height() );
00131     d->passEdit->setMinimumWidth( s.width() );
00132     lbl->setBuddy( d->passEdit );
00133     d->layout->addWidget( lbl, 6, 0 );
00134     d->layout->addWidget( hbox, 6, 2 );
00135 
00136     if ( enableKeep )
00137     {
00138         // Row 7: Add spacer
00139         d->layout->addRowSpacing( 7, 4 );
00140         // Row 8: Keep Password
00141         hbox = new QHBox( main );
00142         QCheckBox *cb = new QCheckBox( i18n("&Keep password"), hbox );
00143         cb->setFixedSize( cb->sizeHint() );
00144         d->keep = cfg->readBoolEntry("Keep", false );
00145         cb->setChecked( d->keep );
00146         connect(cb, SIGNAL(toggled( bool )), SLOT(slotKeep( bool )));
00147         d->layout->addWidget( hbox, 8, 2 );
00148     }
00149 
00150     // Configure necessary key-bindings and connect necessar slots and signals
00151     connect( d->userEdit, SIGNAL(returnPressed()), d->passEdit, SLOT(setFocus()) );
00152     connect( d->passEdit, SIGNAL(returnPressed()), SLOT(slotOk()) );
00153 
00154     if ( !user.isEmpty() )
00155     {
00156         d->userEdit->setText( user );
00157         d->passEdit->setFocus();
00158     }
00159     else
00160         d->userEdit->setFocus();
00161 
00162 //    setFixedSize( sizeHint() );
00163 }
00164 
00165 QString PasswordDialog::username() const
00166 {
00167     return d->userEdit->text();
00168 }
00169 
00170 QString PasswordDialog::password() const
00171 {
00172     return d->passEdit->text();
00173 }
00174 
00175 bool PasswordDialog::keepPassword() const
00176 {
00177     return d->keep;
00178 }
00179 
00180 void PasswordDialog::addCommentLine( const QString& label,
00181                                      const QString comment )
00182 {
00183     if (d->nRow > 0)
00184         return;
00185 
00186     QWidget *main = mainWidget();
00187 
00188     QLabel* lbl = new QLabel( label, main);
00189     lbl->setAlignment( Qt::AlignVCenter|Qt::AlignRight );
00190     lbl->setFixedSize( lbl->sizeHint() );
00191     d->layout->addWidget( lbl, d->nRow+2, 0, Qt::AlignLeft );
00192     lbl = new QLabel( comment, main);
00193     lbl->setAlignment( Qt::AlignVCenter|Qt::AlignLeft|Qt::WordBreak );
00194     int w = QMIN( d->prompt->sizeHint().width(), 250 );
00195     lbl->setFixedSize( w, lbl->heightForWidth(w) );
00196     d->layout->addWidget( lbl, d->nRow+2, 2, Qt::AlignLeft );
00197     d->layout->addRowSpacing( 3, 10 ); // Add a spacer
00198     d->nRow++;
00199 }
00200 
00201 void PasswordDialog::slotKeep( bool keep )
00202 {
00203     d->keep = keep;
00204 }
00205 
00206 void PasswordDialog::setPrompt(const QString& prompt)
00207 {
00208     d->prompt->setText(prompt);
00209     int w = QMIN( d->prompt->sizeHint().width(), 250 );
00210     d->prompt->setFixedSize( w, d->prompt->heightForWidth( w ) );
00211 }
00212 
00213 void PasswordDialog::setPassword(const QString &p)
00214 {
00215     d->passEdit->setText(p);
00216 }
00217 
00218 void PasswordDialog::setUserReadOnly( bool readOnly )
00219 {
00220     d->userEdit->setReadOnly( readOnly );
00221     if ( readOnly && d->userEdit->hasFocus() )
00222         d->passEdit->setFocus();
00223 }
00224 
00225 int PasswordDialog::getNameAndPassword( QString& user, QString& pass, bool* keep,
00226                                         const QString& prompt, bool readOnly,
00227                                         const QString& caption,
00228                                         const QString& comment,
00229                                         const QString& label )
00230 {
00231     PasswordDialog* dlg;
00232     if( keep )
00233         dlg = new PasswordDialog( prompt, user, (*keep) );
00234     else
00235         dlg = new PasswordDialog( prompt, user );
00236 
00237     if ( !caption.isEmpty() )
00238         dlg->setPlainCaption( caption );
00239     else
00240         dlg->setPlainCaption( i18n("Authorization Dialog") );
00241 
00242     if ( !comment.isEmpty() )
00243         dlg->addCommentLine( label, comment );
00244 
00245     if ( readOnly )
00246         dlg->setUserReadOnly( readOnly );
00247 
00248     int ret = dlg->exec();
00249     if ( ret == Accepted )
00250     {
00251         user = dlg->username();
00252         pass = dlg->password();
00253         if ( keep ) { (*keep) = dlg->keepPassword(); }
00254     }
00255     delete dlg;
00256     return ret;
00257  }
00258 
00259 void PasswordDialog::virtual_hook( int id, void* data )
00260 { KDialogBase::virtual_hook( id, data ); }
00261 
00262 
00263 #include "passdlg.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:21:33 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001