• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • dialogs
kpassworddialog.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 2000 David Faure <faure@kde.org>
3 Copyright (C) 2007 Olivier Goffart <ogoffart at kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19#include "kpassworddialog.h"
20
21#include <QCheckBox>
22#include <QLabel>
23#include <QLayout>
24#include <QTextDocument>
25#include <QTimer>
26
27#include <kcombobox.h>
28#include <kconfig.h>
29#include <kiconloader.h>
30#include <klineedit.h>
31#include <klocale.h>
32#include <khbox.h>
33#include <kdebug.h>
34#include <kconfiggroup.h>
35#include <ktitlewidget.h>
36
37#include "ui_kpassworddialog.h"
38
40class KPasswordDialog::KPasswordDialogPrivate
41{
42public:
43 KPasswordDialogPrivate(KPasswordDialog *q)
44 : q(q),
45 userEditCombo(0),
46 pixmapLabel(0),
47 commentRow(0)
48 {}
49
50 void actuallyAccept();
51 void activated( const QString& userName );
52
53 void updateFields();
54 void init();
55
56 KPasswordDialog *q;
57 KPasswordDialogFlags m_flags;
58 Ui_KPasswordDialog ui;
59 QMap<QString,QString> knownLogins;
60 KComboBox* userEditCombo;
61 QLabel* pixmapLabel;
62 unsigned int commentRow;
63};
64
65KPasswordDialog::KPasswordDialog( QWidget* parent ,
66 const KPasswordDialogFlags& flags,
67 const KDialog::ButtonCodes otherButtons )
68 : KDialog( parent ), d(new KPasswordDialogPrivate(this))
69{
70 setCaption( i18n("Password") );
71 setWindowIcon(KIcon("dialog-password"));
72 setButtons( Ok | Cancel | otherButtons );
73 setDefaultButton( Ok );
74 d->m_flags = flags;
75 d->init ();
76}
77
78KPasswordDialog::~KPasswordDialog()
79{
80 delete d;
81}
82
83void KPasswordDialog::KPasswordDialogPrivate::updateFields()
84{
85 if (m_flags & KPasswordDialog::UsernameReadOnly) {
86 ui.userEdit->setReadOnly(true);
87 ui.credentialsGroup->setFocusProxy(ui.passEdit);
88 }
89 ui.domainEdit->setReadOnly(( m_flags & KPasswordDialog::DomainReadOnly ));
90 ui.credentialsGroup->setEnabled( !q->anonymousMode() );
91}
92
93void KPasswordDialog::KPasswordDialogPrivate::init()
94{
95 ui.setupUi( q->mainWidget() );
96 ui.errorMessage->setHidden(true);
97
98 // Row 4: Username field
99 if ( m_flags & KPasswordDialog::ShowUsernameLine ) {
100 ui.userEdit->setFocus();
101 ui.credentialsGroup->setFocusProxy( ui.userEdit );
102 QObject::connect( ui.userEdit, SIGNAL(returnPressed()), ui.passEdit, SLOT(setFocus()) );
103 } else {
104 ui.userNameLabel->hide();
105 ui.userEdit->hide();
106 ui.domainLabel->hide();
107 ui.domainEdit->hide();
108 ui.passEdit->setFocus();
109 ui.credentialsGroup->setFocusProxy( ui.passEdit );
110 }
111
112 if ( !( m_flags & KPasswordDialog::ShowAnonymousLoginCheckBox ) )
113 {
114 ui.anonymousRadioButton->hide();
115 ui.usePasswordButton->hide();
116 }
117
118 if ( !( m_flags & KPasswordDialog::ShowDomainLine ) )
119 {
120 ui.domainLabel->hide();
121 ui.domainEdit->hide();
122 }
123
124 if ( !( m_flags & KPasswordDialog::ShowKeepPassword ) )
125 {
126 ui.keepCheckBox->hide();
127 }
128
129 updateFields();
130
131 QRect desktop = KGlobalSettings::desktopGeometry(q->topLevelWidget());
132 q->setMinimumWidth(qMin(1000, qMax(q->sizeHint().width(), desktop.width() / 4)));
133 q->setPixmap(KIcon("dialog-password").pixmap(KIconLoader::SizeHuge));
134}
135
136void KPasswordDialog::setPixmap(const QPixmap &pixmap)
137{
138 if ( !d->pixmapLabel )
139 {
140 d->pixmapLabel = new QLabel( mainWidget() );
141 d->pixmapLabel->setAlignment( Qt::AlignLeft | Qt::AlignTop );
142 d->ui.hboxLayout->insertWidget( 0, d->pixmapLabel );
143 }
144
145 d->pixmapLabel->setPixmap( pixmap );
146}
147
148QPixmap KPasswordDialog::pixmap() const
149{
150 if ( !d->pixmapLabel ) {
151 return QPixmap();
152 }
153
154 return *d->pixmapLabel->pixmap();
155}
156
157
158void KPasswordDialog::setUsername(const QString& user)
159{
160 d->ui.userEdit->setText(user);
161 if ( user.isEmpty() )
162 return;
163
164 d->activated(user);
165 if ( d->ui.userEdit->isVisibleTo( this ) )
166 {
167 d->ui.passEdit->setFocus();
168 }
169}
170
171
172QString KPasswordDialog::username() const
173{
174 return d->ui.userEdit->text();
175}
176
177QString KPasswordDialog::password() const
178{
179 return d->ui.passEdit->text();
180}
181
182void KPasswordDialog::setDomain(const QString& domain)
183{
184 d->ui.domainEdit->setText(domain);
185}
186
187QString KPasswordDialog::domain() const
188{
189 return d->ui.domainEdit->text();
190}
191
192void KPasswordDialog::setAnonymousMode(bool anonymous)
193{
194 if (anonymous && !(d->m_flags & KPasswordDialog::ShowAnonymousLoginCheckBox)) {
195 // This is an error case, but we can at least let user see what's about
196 // to happen if they proceed.
197 d->ui.anonymousRadioButton->setVisible( true );
198
199 d->ui.usePasswordButton->setVisible( true );
200 d->ui.usePasswordButton->setEnabled( false );
201 }
202
203 d->ui.anonymousRadioButton->setChecked( anonymous );
204}
205
206bool KPasswordDialog::anonymousMode() const
207{
208 return d->ui.anonymousRadioButton->isChecked();
209}
210
211
212void KPasswordDialog::setKeepPassword( bool b )
213{
214 d->ui.keepCheckBox->setChecked( b );
215}
216
217bool KPasswordDialog::keepPassword() const
218{
219 return d->ui.keepCheckBox->isChecked();
220}
221
222void KPasswordDialog::addCommentLine( const QString& label,
223 const QString& comment )
224{
225 int gridMarginLeft, gridMarginTop, gridMarginRight, gridMarginBottom;
226 d->ui.formLayout->getContentsMargins(&gridMarginLeft, &gridMarginTop, &gridMarginRight, &gridMarginBottom);
227
228 int spacing = d->ui.formLayout->horizontalSpacing();
229 if (spacing < 0) {
230 // same inter-column spacing for all rows, see comment in qformlayout.cpp
231 spacing = style()->combinedLayoutSpacing(QSizePolicy::Label, QSizePolicy::LineEdit, Qt::Horizontal, 0, this);
232 }
233
234 QLabel* c = new QLabel(comment, mainWidget());
235 c->setWordWrap(true);
236
237 d->ui.formLayout->insertRow(d->commentRow, label, c);
238 ++d->commentRow;
239
240 // cycle through column 0 widgets and see the max width so we can set the minimum height of
241 // column 2 wordwrapable labels
242 int firstColumnWidth = 0;
243 for (int i = 0; i < d->ui.formLayout->rowCount(); ++i) {
244 QLayoutItem *li = d->ui.formLayout->itemAt(i, QFormLayout::LabelRole);
245 if (li) {
246 QWidget *w = li->widget();
247 if (w && !w->isHidden()) {
248 firstColumnWidth = qMax(firstColumnWidth, w->sizeHint().width());
249 }
250 }
251 }
252 for (int i = 0; i < d->ui.formLayout->rowCount(); ++i) {
253 QLayoutItem *li = d->ui.formLayout->itemAt(i, QFormLayout::FieldRole);
254 if (li) {
255 QLabel *l = qobject_cast<QLabel*>(li->widget());
256 if (l && l->wordWrap()) {
257 int w = sizeHint().width() - firstColumnWidth - ( 2 * marginHint() ) - gridMarginLeft - gridMarginRight - spacing;
258 l->setMinimumSize( w, l->heightForWidth(w) );
259 }
260 }
261 }
262}
263
264void KPasswordDialog::showErrorMessage( const QString& message, const ErrorType type )
265{
266 d->ui.errorMessage->setText( message, KTitleWidget::ErrorMessage );
267
268 QFont bold = font();
269 bold.setBold( true );
270 switch ( type ) {
271 case PasswordError:
272 d->ui.passwordLabel->setFont( bold );
273 d->ui.passEdit->clear();
274 d->ui.passEdit->setFocus();
275 break;
276 case UsernameError:
277 if ( d->ui.userEdit->isVisibleTo( this ) )
278 {
279 d->ui.userNameLabel->setFont( bold );
280 d->ui.userEdit->setFocus();
281 }
282 break;
283 case DomainError:
284 if ( d->ui.domainEdit->isVisibleTo( this ) )
285 {
286 d->ui.domainLabel->setFont( bold );
287 d->ui.domainEdit->setFocus();
288 }
289 break;
290 case FatalError:
291 d->ui.userNameLabel->setEnabled( false );
292 d->ui.userEdit->setEnabled( false );
293 d->ui.passwordLabel->setEnabled( false );
294 d->ui.passEdit->setEnabled( false );
295 d->ui.keepCheckBox->setEnabled( false );
296 enableButton( Ok, false );
297 break;
298 default:
299 break;
300 }
301 adjustSize();
302}
303
304void KPasswordDialog::setPrompt(const QString& prompt)
305{
306 d->ui.prompt->setText( prompt );
307 d->ui.prompt->setWordWrap( true );
308 d->ui.prompt->setMinimumHeight( d->ui.prompt->heightForWidth( width() - ( 2 * marginHint() ) ) );
309}
310
311QString KPasswordDialog::prompt() const
312{
313 return d->ui.prompt->text();
314}
315
316void KPasswordDialog::setPassword(const QString &p)
317{
318 d->ui.passEdit->setText(p);
319}
320
321void KPasswordDialog::setUsernameReadOnly( bool readOnly )
322{
323 d->ui.userEdit->setReadOnly( readOnly );
324
325 if ( readOnly && d->ui.userEdit->hasFocus() ) {
326 d->ui.passEdit->setFocus();
327 }
328}
329
330void KPasswordDialog::setKnownLogins( const QMap<QString, QString>& knownLogins )
331{
332 const int nr = knownLogins.count();
333 if ( nr == 0 ) {
334 return;
335 }
336
337 if ( nr == 1 ) {
338 d->ui.userEdit->setText( knownLogins.begin().key() );
339 setPassword( knownLogins.begin().value() );
340 return;
341 }
342
343 Q_ASSERT( !d->ui.userEdit->isReadOnly() );
344 if ( !d->userEditCombo ) {
345 int row = -1;
346 QFormLayout::ItemRole userEditRole = QFormLayout::FieldRole;
347
348 d->ui.formLayout->getWidgetPosition(d->ui.userEdit, &row, &userEditRole);
349 d->ui.formLayout->removeWidget(d->ui.userEdit);
350 delete d->ui.userEdit;
351 d->userEditCombo = new KComboBox( true, d->ui.credentialsGroup );
352 d->ui.userEdit = d->userEditCombo->lineEdit();
353 d->ui.userNameLabel->setBuddy( d->userEditCombo );
354 d->ui.formLayout->setWidget( row > -1 ? row : 0, userEditRole, d->userEditCombo );
355
356 setTabOrder( d->ui.userEdit, d->ui.anonymousRadioButton );
357 setTabOrder( d->ui.anonymousRadioButton, d->ui.domainEdit );
358 setTabOrder( d->ui.domainEdit, d->ui.passEdit );
359 setTabOrder( d->ui.passEdit, d->ui.keepCheckBox );
360 connect( d->ui.userEdit, SIGNAL(returnPressed()), d->ui.passEdit, SLOT(setFocus()) );
361 }
362
363 d->knownLogins = knownLogins;
364 d->userEditCombo->addItems( knownLogins.keys() );
365 d->userEditCombo->setFocus();
366
367 connect( d->userEditCombo, SIGNAL(activated(QString)),
368 this, SLOT(activated(QString)) );
369}
370
371void KPasswordDialog::KPasswordDialogPrivate::activated( const QString& userName )
372{
373 QMap<QString, QString>::ConstIterator it = knownLogins.constFind( userName );
374 if ( it != knownLogins.constEnd() ) {
375 q->setPassword( it.value() );
376 }
377}
378
379void KPasswordDialog::accept()
380{
381 if (!d->ui.errorMessage->isHidden()) d->ui.errorMessage->setText( QString() );
382
383 // reset the font in case we had an error previously
384 if (!d->ui.passwordLabel->isHidden()) {
385 d->ui.passwordLabel->setFont( font() );
386 d->ui.userNameLabel->setFont( font() );
387 }
388
389 // we do this to allow the error message, if any, to go away
390 // checkPassword() may block for a period of time
391 QTimer::singleShot( 0, this, SLOT(actuallyAccept()) );
392}
393
394void KPasswordDialog::KPasswordDialogPrivate::actuallyAccept()
395{
396 if ( !q->checkPassword() )
397 {
398 return;
399 }
400
401 bool keep = ui.keepCheckBox->isVisibleTo( q ) && ui.keepCheckBox->isChecked();
402 emit q->gotPassword( q->password(), keep);
403
404 if ( ui.userEdit->isVisibleTo( q ) ) {
405 emit q->gotUsernameAndPassword( q->username(), q->password() , keep);
406 }
407
408 q->KDialog::accept();
409}
410
411bool KPasswordDialog::checkPassword()
412{
413 return true;
414}
415
416#include "kpassworddialog.moc"
KComboBox
An enhanced combo box.
Definition: kcombobox.h:149
KDialog
A dialog base class with standard buttons and predefined layouts.
Definition: kdialog.h:129
KDialog::marginHint
static int marginHint()
Returns the number of pixels that should be used between a dialog edge and the outermost widget(s) ac...
Definition: kdialog.cpp:427
KDialog::mainWidget
QWidget * mainWidget()
Definition: kdialog.cpp:351
KDialog::sizeHint
virtual QSize sizeHint() const
Reimplemented from QDialog.
Definition: kdialog.cpp:359
KDialog::enableButton
void enableButton(ButtonCode id, bool state)
Enable or disable (gray out) a general action button.
Definition: kdialog.cpp:661
KDialog::setButtons
void setButtons(ButtonCodes buttonMask)
Creates (or recreates) the button box and all the buttons in it.
Definition: kdialog.cpp:206
KDialog::Ok
@ Ok
Show Ok button. (this button accept()s the dialog; result set to QDialog::Accepted)
Definition: kdialog.h:141
KDialog::Cancel
@ Cancel
Show Cancel-button. (this button reject()s the dialog; result set to QDialog::Rejected)
Definition: kdialog.h:144
KDialog::setDefaultButton
void setDefaultButton(ButtonCode id)
Sets the button that will be activated when the Enter key is pressed.
Definition: kdialog.cpp:287
KDialog::setCaption
virtual void setCaption(const QString &caption)
Make a KDE compliant caption.
Definition: kdialog.cpp:469
KGlobalSettings::desktopGeometry
static QRect desktopGeometry(const QPoint &point)
This function returns the desktop geometry for an application that needs to set the geometry of a wid...
Definition: kglobalsettings.cpp:732
KIconLoader::SizeHuge
@ SizeHuge
huge sized icons for iconviews
Definition: kiconloader.h:163
KIcon
A wrapper around QIcon that provides KDE icon features.
Definition: kicon.h:41
KPasswordDialog
A dialog for requesting a password and optionaly a login from the end user.
Definition: kpassworddialog.h:57
KPasswordDialog::prompt
QString prompt() const
Returns the prompt.
Definition: kpassworddialog.cpp:311
KPasswordDialog::setDomain
void setDomain(const QString &)
set the default domain.
Definition: kpassworddialog.cpp:182
KPasswordDialog::setKeepPassword
void setKeepPassword(bool b)
Check or uncheck the "keep password" checkbox.
Definition: kpassworddialog.cpp:212
KPasswordDialog::anonymousMode
bool anonymousMode() const
Definition: kpassworddialog.cpp:206
KPasswordDialog::setAnonymousMode
void setAnonymousMode(bool anonymous)
set anonymous mode (all other fields will be grayed out)
Definition: kpassworddialog.cpp:192
KPasswordDialog::ShowAnonymousLoginCheckBox
@ ShowAnonymousLoginCheckBox
If this flag is set, the Anonymous Login checkbox will be displayed.
Definition: kpassworddialog.h:83
KPasswordDialog::ShowKeepPassword
@ ShowKeepPassword
If this flag is set, the "keep this password" checkbox will been shown, otherwise,...
Definition: kpassworddialog.h:69
KPasswordDialog::UsernameReadOnly
@ UsernameReadOnly
If this flag is set, the login lineedit will be in read only mode.
Definition: kpassworddialog.h:78
KPasswordDialog::DomainReadOnly
@ DomainReadOnly
If this flag is set, the domain lineedit will be in read only mode.
Definition: kpassworddialog.h:93
KPasswordDialog::ShowDomainLine
@ ShowDomainLine
If this flag is set, there will be an additional line to let the user enter the domain.
Definition: kpassworddialog.h:88
KPasswordDialog::ShowUsernameLine
@ ShowUsernameLine
If this flag is set, there will be an additional line to let the user enter his login.
Definition: kpassworddialog.h:74
KPasswordDialog::password
QString password() const
Returns the password entered by the user.
Definition: kpassworddialog.cpp:177
KPasswordDialog::domain
QString domain() const
Returns the domain entered by the user.
Definition: kpassworddialog.cpp:187
KPasswordDialog::setUsername
void setUsername(const QString &)
set the default username.
Definition: kpassworddialog.cpp:158
KPasswordDialog::ErrorType
ErrorType
Definition: kpassworddialog.h:98
KPasswordDialog::PasswordError
@ PasswordError
Incorrect password.
Definition: kpassworddialog.h:109
KPasswordDialog::UsernameError
@ UsernameError
A problem with the user name as entered.
Definition: kpassworddialog.h:104
KPasswordDialog::DomainError
@ DomainError
A problem with the domain as entered.
Definition: kpassworddialog.h:120
KPasswordDialog::FatalError
@ FatalError
Error preventing further attempts, will result in disabling most of the interface.
Definition: kpassworddialog.h:114
KPasswordDialog::setPrompt
void setPrompt(const QString &prompt)
Sets the prompt to show to the user.
Definition: kpassworddialog.cpp:304
KPasswordDialog::checkPassword
virtual bool checkPassword()
Virtual function that can be overridden to provide password checking in derived classes.
Definition: kpassworddialog.cpp:411
KPasswordDialog::setPassword
void setPassword(const QString &password)
Presets the password.
Definition: kpassworddialog.cpp:316
KPasswordDialog::addCommentLine
void addCommentLine(const QString &label, const QString &comment)
Adds a comment line to the dialog.
Definition: kpassworddialog.cpp:222
KPasswordDialog::setUsernameReadOnly
void setUsernameReadOnly(bool readOnly)
Sets the username field read-only and sets the focus to the password field.
Definition: kpassworddialog.cpp:321
KPasswordDialog::setKnownLogins
void setKnownLogins(const QMap< QString, QString > &knownLogins)
Presets a number of login+password pairs that the user can choose from.
Definition: kpassworddialog.cpp:330
KPasswordDialog::keepPassword
bool keepPassword() const
Determines whether supplied authorization should persist even after the application has been closed.
Definition: kpassworddialog.cpp:217
KPasswordDialog::accept
void accept()
Definition: kpassworddialog.cpp:379
KPasswordDialog::username
QString username() const
Returns the username entered by the user.
Definition: kpassworddialog.cpp:172
KPasswordDialog::KPasswordDialog
KPasswordDialog(QWidget *parent=0L, const KPasswordDialogFlags &flags=0, const KDialog::ButtonCodes otherButtons=0)
create a password dialog
Definition: kpassworddialog.cpp:65
KPasswordDialog::pixmap
QPixmap pixmap() const
Definition: kpassworddialog.cpp:148
KPasswordDialog::~KPasswordDialog
~KPasswordDialog()
Destructor.
Definition: kpassworddialog.cpp:78
KPasswordDialog::showErrorMessage
void showErrorMessage(const QString &message, const ErrorType type=PasswordError)
Shows an error message in the dialog box.
Definition: kpassworddialog.cpp:264
KPasswordDialog::setPixmap
void setPixmap(const QPixmap &)
set an image that appears next to the prompt.
Definition: kpassworddialog.cpp:136
KTitleWidget::ErrorMessage
@ ErrorMessage
An error message.
Definition: ktitlewidget.h:89
QLabel
QMap
QWidget
kcombobox.h
kconfig.h
kconfiggroup.h
kdebug.h
khbox.h
kiconloader.h
klineedit.h
klocale.h
i18n
QString i18n(const char *text)
kpassworddialog.h
ktitlewidget.h
message
void message(KMessage::MessageType messageType, const QString &text, const QString &caption=QString())
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal