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 <qpushbutton.h>
00020 #include <qcheckbox.h>
00021 #include <qlabel.h>
00022 #include <qlayout.h>
00023 #include <qaccel.h>
00024 #include <qhbox.h>
00025 #include <qsimplerichtext.h>
00026 #include <qstylesheet.h>
00027 
00028 #include <kapplication.h>
00029 #include <klineedit.h>
00030 #include <kconfig.h>
00031 #include <klocale.h>
00032 #include <kbuttonbox.h>
00033 #include <kstandarddirs.h>
00034 #include <kseparator.h>
00035 
00036 #include "passdlg.h"
00037 
00038 using namespace KIO;
00039 
00040 struct PasswordDialog::PasswordDialogPrivate
00041 {
00042     QGridLayout *layout;
00043     KLineEdit* userEdit;
00044     KLineEdit* passEdit;
00045     QLabel* prompt;
00046     QCheckBox* keepCheckBox;
00047 
00048     bool keep;
00049     short unsigned int nRow;
00050 };
00051 
00052 PasswordDialog::PasswordDialog( const QString& prompt, const QString& user,
00053                                 bool enableKeep, bool modal, QWidget* parent,
00054                                 const char* name )
00055                :KDialogBase( parent, name, modal, i18n("Password"), Ok|Cancel, Ok, true)
00056 {
00057     init ( prompt, user, enableKeep );
00058 }
00059 
00060 PasswordDialog::~PasswordDialog()
00061 {
00062     delete d;
00063 }
00064 
00065 void PasswordDialog::init( const QString& prompt, const QString& user,
00066                            bool enableKeep  )
00067 {
00068     QWidget *main = makeMainWidget();
00069 
00070     d = new PasswordDialogPrivate;
00071     d->keep = false;
00072     d->nRow = 0;
00073     d->keepCheckBox = 0;
00074 
00075     KConfig* cfg = KGlobal::config();
00076     KConfigGroupSaver saver( cfg, "Passwords" );
00077 
00078     d->layout = new QGridLayout( main, 9, 3, spacingHint(), marginHint());
00079     d->layout->addColSpacing(1, 5);
00080 
00081     // Row 0: pixmap  prompt
00082     QLabel* lbl;
00083     QPixmap pix(locate("data", QString::fromLatin1("kdeui/pics/keys.png")));
00084     if ( !pix.isNull() )
00085     {
00086         lbl = new QLabel( main );
00087         lbl->setPixmap( pix );
00088         lbl->setAlignment( Qt::AlignLeft|Qt::AlignVCenter );
00089         lbl->setFixedSize( lbl->sizeHint() );
00090         d->layout->addWidget( lbl, 0, 0, Qt::AlignLeft );
00091     }
00092     d->prompt = new QLabel( main );
00093     d->prompt->setAlignment( Qt::AlignLeft|Qt::AlignVCenter|Qt::WordBreak );
00094     d->layout->addWidget( d->prompt, 0, 2, Qt::AlignLeft );
00095     if ( prompt.isEmpty() )
00096         setPrompt( i18n( "You need to supply a username and a password" ) );
00097     else
00098         setPrompt( prompt );
00099 
00100     // Row 1: Row Spacer
00101     d->layout->addRowSpacing( 1, 7 );
00102 
00103     // Row 2-3: Reserved for an additional comment
00104 
00105     // Row 4: Username field
00106     lbl = new QLabel( i18n("&Username:"), main );
00107     lbl->setAlignment( Qt::AlignVCenter | Qt::AlignLeft );
00108     lbl->setFixedSize( lbl->sizeHint() );
00109     QHBox* hbox = new QHBox( main );
00110     d->userEdit = new KLineEdit( hbox );
00111     lbl->setBuddy( d->userEdit );
00112     QSize s = d->userEdit->sizeHint();
00113     d->userEdit->setFixedHeight( s.height() );
00114     d->userEdit->setMinimumWidth( s.width() );
00115     lbl->setBuddy( d->userEdit );
00116     d->layout->addWidget( lbl, 4, 0 );
00117     d->layout->addWidget( hbox, 4, 2 );
00118 
00119     // Row 5: Row spacer
00120     d->layout->addRowSpacing( 5, 4 );
00121 
00122     // Row 6: Password field
00123     lbl = new QLabel( i18n("&Password:"), main );
00124     lbl->setAlignment( Qt::AlignVCenter | Qt::AlignLeft );
00125     lbl->setFixedSize( lbl->sizeHint() );
00126     hbox = new QHBox( main );
00127     d->passEdit = new KLineEdit( hbox );
00128     if ( cfg->readEntry("EchoMode", "OneStar") == "NoEcho" )
00129         d->passEdit->setEchoMode( QLineEdit::NoEcho );
00130     else
00131         d->passEdit->setEchoMode( QLineEdit::Password );
00132     lbl->setBuddy( d->passEdit );
00133     s = d->passEdit->sizeHint();
00134     d->passEdit->setFixedHeight( s.height() );
00135     d->passEdit->setMinimumWidth( s.width() );
00136     lbl->setBuddy( d->passEdit );
00137     d->layout->addWidget( lbl, 6, 0 );
00138     d->layout->addWidget( hbox, 6, 2 );
00139 
00140     if ( enableKeep )
00141     {
00142         // Row 7: Add spacer
00143         d->layout->addRowSpacing( 7, 4 );
00144         // Row 8: Keep Password
00145         hbox = new QHBox( main );
00146         d->keepCheckBox = new QCheckBox( i18n("&Keep password"), hbox );
00147         d->keepCheckBox->setFixedSize( d->keepCheckBox->sizeHint() );
00148         d->keep = cfg->readBoolEntry("Keep", false );
00149         d->keepCheckBox->setChecked( d->keep );
00150         connect(d->keepCheckBox, SIGNAL(toggled( bool )), SLOT(slotKeep( bool )));
00151         d->layout->addWidget( hbox, 8, 2 );
00152     }
00153 
00154     // Configure necessary key-bindings and connect necessar slots and signals
00155     connect( d->userEdit, SIGNAL(returnPressed()), d->passEdit, SLOT(setFocus()) );
00156     connect( d->passEdit, SIGNAL(returnPressed()), SLOT(slotOk()) );
00157 
00158     if ( !user.isEmpty() )
00159     {
00160         d->userEdit->setText( user );
00161         d->passEdit->setFocus();
00162     }
00163     else
00164         d->userEdit->setFocus();
00165 
00166 //    setFixedSize( sizeHint() );
00167 }
00168 
00169 QString PasswordDialog::username() const
00170 {
00171     return d->userEdit->text();
00172 }
00173 
00174 QString PasswordDialog::password() const
00175 {
00176     return d->passEdit->text();
00177 }
00178 
00179 void PasswordDialog::setKeepPassword( bool b )
00180 {
00181     if ( d->keepCheckBox )
00182         d->keepCheckBox->setChecked( b );
00183 }
00184 
00185 bool PasswordDialog::keepPassword() const
00186 {
00187     return d->keep;
00188 }
00189 
00190 static void calculateLabelSize(QLabel *label)
00191 {
00192    QString qt_text = label->text();
00193 
00194    int pref_width = 0;
00195    int pref_height = 0;
00196    // Calculate a proper size for the text.
00197    {
00198        QSimpleRichText rt(qt_text, label->font());
00199        QRect d = KGlobalSettings::desktopGeometry(label->topLevelWidget());
00200 
00201        pref_width = d.width() / 4;
00202        rt.setWidth(pref_width-10);
00203        int used_width = rt.widthUsed();
00204        pref_height = rt.height();
00205        if (used_width <= pref_width)
00206        {
00207           while(true)
00208           {
00209              int new_width = (used_width * 9) / 10;
00210              rt.setWidth(new_width-10);
00211              int new_height = rt.height();
00212              if (new_height > pref_height)
00213                 break;
00214              used_width = rt.widthUsed();
00215              if (used_width > new_width)
00216                 break;
00217           }
00218           pref_width = used_width;
00219        }
00220        else
00221        {
00222           if (used_width > (pref_width *2))
00223              pref_width = pref_width *2;
00224           else
00225              pref_width = used_width;
00226        }
00227     }
00228     label->setFixedSize(QSize(pref_width+10, pref_height));
00229 }
00230 
00231 void PasswordDialog::addCommentLine( const QString& label,
00232                                      const QString comment )
00233 {
00234     if (d->nRow > 0)
00235         return;
00236 
00237     QWidget *main = mainWidget();
00238 
00239     QLabel* lbl = new QLabel( label, main);
00240     lbl->setAlignment( Qt::AlignVCenter|Qt::AlignRight );
00241     lbl->setFixedSize( lbl->sizeHint() );
00242     d->layout->addWidget( lbl, d->nRow+2, 0, Qt::AlignLeft );
00243     lbl = new QLabel( comment, main);
00244     lbl->setAlignment( Qt::AlignVCenter|Qt::AlignLeft|Qt::WordBreak );
00245     calculateLabelSize(lbl);
00246     d->layout->addWidget( lbl, d->nRow+2, 2, Qt::AlignLeft );
00247     d->layout->addRowSpacing( 3, 10 ); // Add a spacer
00248     d->nRow++;
00249 }
00250 
00251 void PasswordDialog::slotKeep( bool keep )
00252 {
00253     d->keep = keep;
00254 }
00255 
00256 static QString qrichtextify( const QString& text )
00257 {
00258   if ( text.isEmpty() || text[0] == '<' )
00259     return text;
00260 
00261   QStringList lines = QStringList::split('\n', text);
00262   for(QStringList::Iterator it = lines.begin(); it != lines.end(); ++it)
00263   {
00264     *it = QStyleSheet::convertFromPlainText( *it, QStyleSheetItem::WhiteSpaceNormal );
00265   }
00266 
00267   return lines.join(QString::null);
00268 }
00269 
00270 void PasswordDialog::setPrompt(const QString& prompt)
00271 {
00272     QString text = qrichtextify(prompt);
00273     d->prompt->setText(text);
00274     calculateLabelSize(d->prompt);
00275 }
00276 
00277 void PasswordDialog::setPassword(const QString &p)
00278 {
00279     d->passEdit->setText(p);
00280 }
00281 
00282 void PasswordDialog::setUserReadOnly( bool readOnly )
00283 {
00284     d->userEdit->setReadOnly( readOnly );
00285     if ( readOnly && d->userEdit->hasFocus() )
00286         d->passEdit->setFocus();
00287 }
00288 
00289 int PasswordDialog::getNameAndPassword( QString& user, QString& pass, bool* keep,
00290                                         const QString& prompt, bool readOnly,
00291                                         const QString& caption,
00292                                         const QString& comment,
00293                                         const QString& label )
00294 {
00295     PasswordDialog* dlg;
00296     if( keep )
00297         dlg = new PasswordDialog( prompt, user, (*keep) );
00298     else
00299         dlg = new PasswordDialog( prompt, user );
00300 
00301     if ( !caption.isEmpty() )
00302         dlg->setPlainCaption( caption );
00303     else
00304         dlg->setPlainCaption( i18n("Authorization Dialog") );
00305 
00306     if ( !comment.isEmpty() )
00307         dlg->addCommentLine( label, comment );
00308 
00309     if ( readOnly )
00310         dlg->setUserReadOnly( readOnly );
00311 
00312     int ret = dlg->exec();
00313     if ( ret == Accepted )
00314     {
00315         user = dlg->username();
00316         pass = dlg->password();
00317         if ( keep ) { (*keep) = dlg->keepPassword(); }
00318     }
00319     delete dlg;
00320     return ret;
00321  }
00322 
00323 void PasswordDialog::virtual_hook( int id, void* data )
00324 { KDialogBase::virtual_hook( id, data ); }
00325 
00326 
00327 #include "passdlg.moc"
KDE Logo
This file is part of the documentation for kio Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Apr 22 14:24:13 2004 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2003