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