21 #include <tqapplication.h>
22 #include <tqcheckbox.h>
26 #include <tqsimplerichtext.h>
27 #include <tqstylesheet.h>
29 #include <kcombobox.h>
30 #include <tdeconfig.h>
31 #include <kiconloader.h>
32 #include <klineedit.h>
33 #include <tdelocale.h>
34 #include <kstandarddirs.h>
36 using namespace TDEIO;
38 struct PasswordDialog::PasswordDialogPrivate
43 TQLabel* userNameLabel;
45 TQCheckBox* keepCheckBox;
46 TQMap<TQString,TQString> knownLogins;
47 KComboBox* userEditCombo;
51 short unsigned int nRow;
55 bool enableKeep,
bool modal, TQWidget* parent,
57 :KDialogBase( parent, name, modal, i18n(
"Password"), Ok|Cancel, Ok, true)
59 init ( prompt, user, enableKeep );
67 void PasswordDialog::init(
const TQString& prompt,
const TQString& user,
70 TQWidget *main = makeMainWidget();
72 d =
new PasswordDialogPrivate;
77 TDEConfig* cfg = TDEGlobal::config();
78 TDEConfigGroupSaver saver( cfg,
"Passwords" );
80 d->layout =
new TQGridLayout( main, 9, 3, spacingHint(), marginHint());
81 d->layout->addColSpacing(1, 5);
85 TQPixmap pix( TDEGlobal::iconLoader()->loadIcon(
"password", TDEIcon::NoGroup, TDEIcon::SizeHuge, 0, 0,
true));
88 lbl =
new TQLabel( main );
89 lbl->setPixmap( pix );
90 lbl->setAlignment( Qt::AlignLeft|Qt::AlignVCenter );
91 lbl->setFixedSize( lbl->sizeHint() );
92 d->layout->addWidget( lbl, 0, 0, Qt::AlignLeft );
94 d->prompt =
new TQLabel( main );
95 d->prompt->setAlignment( Qt::AlignLeft|Qt::AlignVCenter|TQt::WordBreak );
96 d->layout->addWidget( d->prompt, 0, 2, Qt::AlignLeft );
97 if ( prompt.isEmpty() )
98 setPrompt( i18n(
"You need to supply a username and a password" ) );
103 d->layout->addRowSpacing( 1, 7 );
108 d->userNameLabel =
new TQLabel( i18n(
"&Username:"), main );
109 d->userNameLabel->setAlignment( Qt::AlignVCenter | Qt::AlignLeft );
110 d->userNameLabel->setFixedSize( d->userNameLabel->sizeHint() );
111 d->userNameHBox =
new TQHBox( main );
113 d->userEdit =
new KLineEdit( d->userNameHBox );
114 TQSize s = d->userEdit->sizeHint();
115 d->userEdit->setFixedHeight( s.height() );
116 d->userEdit->setMinimumWidth( s.width() );
117 d->userNameLabel->setBuddy( d->userEdit );
118 d->layout->addWidget( d->userNameLabel, 4, 0 );
119 d->layout->addWidget( d->userNameHBox, 4, 2 );
122 d->layout->addRowSpacing( 5, 4 );
125 lbl =
new TQLabel( i18n(
"&Password:"), main );
126 lbl->setAlignment( Qt::AlignVCenter | Qt::AlignLeft );
127 lbl->setFixedSize( lbl->sizeHint() );
128 TQHBox* hbox =
new TQHBox( main );
129 d->passEdit =
new KLineEdit( hbox );
130 if ( cfg->readEntry(
"EchoMode",
"OneStar") ==
"NoEcho" )
131 d->passEdit->setEchoMode( TQLineEdit::NoEcho );
133 d->passEdit->setEchoMode( TQLineEdit::Password );
134 s = d->passEdit->sizeHint();
135 d->passEdit->setFixedHeight( s.height() );
136 d->passEdit->setMinimumWidth( s.width() );
137 lbl->setBuddy( d->passEdit );
138 d->layout->addWidget( lbl, 6, 0 );
139 d->layout->addWidget( hbox, 6, 2 );
144 d->layout->addRowSpacing( 7, 4 );
146 hbox =
new TQHBox( main );
147 d->keepCheckBox =
new TQCheckBox( i18n(
"&Keep password"), hbox );
148 d->keepCheckBox->setFixedSize( d->keepCheckBox->sizeHint() );
149 d->keep = cfg->readBoolEntry(
"Keep",
false );
150 d->keepCheckBox->setChecked( d->keep );
151 connect(d->keepCheckBox, TQT_SIGNAL(toggled(
bool )), TQT_SLOT(slotKeep(
bool )));
152 d->layout->addWidget( hbox, 8, 2 );
156 connect( d->userEdit, TQT_SIGNAL(returnPressed()), d->passEdit, TQT_SLOT(setFocus()) );
157 connect( d->passEdit, TQT_SIGNAL(returnPressed()), TQT_SLOT(slotOk()) );
159 if ( !user.isEmpty() )
161 d->userEdit->setText( user );
162 d->passEdit->setFocus();
165 d->userEdit->setFocus();
167 d->userEditCombo = 0;
173 return d->userEdit->text();
178 return d->passEdit->text();
183 if ( d->keepCheckBox )
184 d->keepCheckBox->setChecked( b );
192 static void calculateLabelSize(TQLabel *label)
194 TQString qt_text = label->text();
200 TQSimpleRichText rt(qt_text, label->font());
201 TQRect d = TDEGlobalSettings::desktopGeometry(label->topLevelWidget());
203 pref_width = d.width() / 4;
204 rt.setWidth(pref_width-10);
205 int used_width = rt.widthUsed();
206 pref_height = rt.height();
207 if (used_width <= pref_width)
211 int new_width = (used_width * 9) / 10;
212 rt.setWidth(new_width-10);
213 int new_height = rt.height();
214 if (new_height > pref_height)
216 used_width = rt.widthUsed();
217 if (used_width > new_width)
220 pref_width = used_width;
224 if (used_width > (pref_width *2))
225 pref_width = pref_width *2;
227 pref_width = used_width;
230 label->setFixedSize(TQSize(pref_width+10, pref_height));
234 const TQString comment )
239 TQWidget *main = mainWidget();
241 TQLabel* lbl =
new TQLabel( label, main);
242 lbl->setAlignment( Qt::AlignVCenter|Qt::AlignRight );
243 lbl->setFixedSize( lbl->sizeHint() );
244 d->layout->addWidget( lbl, d->nRow+2, 0, Qt::AlignLeft );
245 lbl =
new TQLabel( comment, main);
246 lbl->setAlignment( Qt::AlignVCenter|Qt::AlignLeft|TQt::WordBreak );
247 calculateLabelSize(lbl);
248 d->layout->addWidget( lbl, d->nRow+2, 2, Qt::AlignLeft );
249 d->layout->addRowSpacing( 3, 10 );
253 void PasswordDialog::slotKeep(
bool keep )
258 static TQString qrichtextify(
const TQString& text )
260 if ( text.isEmpty() || text[0] ==
'<' )
263 TQStringList lines = TQStringList::split(
'\n', text);
264 for(TQStringList::Iterator it = lines.begin(); it != lines.end(); ++it)
266 *it = TQStyleSheet::convertFromPlainText( *it, TQStyleSheetItem::WhiteSpaceNormal );
269 return lines.join(TQString::null);
274 TQString text = qrichtextify(prompt);
275 d->prompt->setText(text);
276 calculateLabelSize(d->prompt);
281 d->passEdit->setText(p);
286 d->userEdit->setReadOnly( readOnly );
287 if ( readOnly && d->userEdit->hasFocus() )
288 d->passEdit->setFocus();
293 const int nr = knownLogins.count();
297 d->userEdit->setText( knownLogins.begin().key() );
302 Q_ASSERT( !d->userEdit->isReadOnly() );
303 if ( !d->userEditCombo ) {
305 d->userEditCombo =
new KComboBox(
true, d->userNameHBox );
306 d->userEdit = d->userEditCombo->lineEdit();
307 TQSize s = d->userEditCombo->sizeHint();
308 d->userEditCombo->setFixedHeight( s.height() );
309 d->userEditCombo->setMinimumWidth( s.width() );
310 d->userNameLabel->setBuddy( d->userEditCombo );
311 d->layout->addWidget( d->userNameHBox, 4, 2 );
314 d->knownLogins = knownLogins;
315 d->userEditCombo->insertStringList( knownLogins.keys() );
316 d->userEditCombo->setFocus();
318 connect( d->userEditCombo, TQT_SIGNAL( activated(
const TQString& ) ),
319 this, TQT_SLOT( slotActivated(
const TQString& ) ) );
322 void PasswordDialog::slotActivated(
const TQString& userName )
324 TQMap<TQString, TQString>::ConstIterator it = d->knownLogins.find( userName );
325 if ( it != d->knownLogins.end() )
331 const TQString& prompt,
bool readOnly,
332 const TQString& caption,
333 const TQString& comment,
334 const TQString& label )
342 if ( !caption.isEmpty() )
343 dlg->setPlainCaption( caption );
345 dlg->setPlainCaption( i18n(
"Authorization Dialog") );
347 if ( !comment.isEmpty() )
353 int ret = dlg->exec();
354 if ( ret == Accepted )
364 void PasswordDialog::virtual_hook(
int id,
void* data )
365 { KDialogBase::virtual_hook(
id, data ); }
367 #include "passdlg.moc"
void setPrompt(const TQString &prompt)
Sets the prompt to show to the user.
void setKeepPassword(bool b)
Check or uncheck the "keep password" checkbox.
TQString username() const
Returns the username entered by the user.
void setPassword(const TQString &password)
Presets the password.
A namespace for TDEIO globals.
void setKnownLogins(const TQMap< TQString, TQString > &knownLogins)
Presets a number of login+password pairs that the user can choose from.
static int getNameAndPassword(TQString &user, TQString &pass, bool *keep, const TQString &prompt=TQString::null, bool readOnly=false, const TQString &caption=TQString::null, const TQString &comment=TQString::null, const TQString &label=TQString::null)
A convienence static method for obtaining authorization information from the end user.
void addCommentLine(const TQString &label, const TQString comment)
Adds a comment line to the dialog.
bool keepPassword() const
Determines whether supplied authorization should persist even after the application has been closed...
void setUserReadOnly(bool readOnly)
Sets the username field read-only and sets the focus to the password field.
PasswordDialog(const TQString &prompt, const TQString &user, bool enableKeep=false, bool modal=true, TQWidget *parent=0, const char *name=0)
Create a password dialog.
A dialog for requesting a login and a password from the end user.
TQString password() const
Returns the password entered by the user.
~PasswordDialog()
Destructor.