passdlg.cpp Source File (kio Library)

| # kio Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

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

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:14:35 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001