• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.10.4 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 
40 class KPasswordDialog::KPasswordDialogPrivate
41 {
42 public:
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 
65 KPasswordDialog::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 
78 KPasswordDialog::~KPasswordDialog()
79 {
80  delete d;
81 }
82 
83 void KPasswordDialog::KPasswordDialogPrivate::updateFields()
84 {
85  if (q->anonymousMode())
86  {
87  ui.userEdit->setEnabled( false );
88  ui.domainEdit->setEnabled( false );
89  ui.passEdit->setEnabled( false );
90  ui.keepCheckBox->setEnabled( false );
91  }
92  else
93  {
94  ui.userEdit->setEnabled(!( m_flags & KPasswordDialog::UsernameReadOnly ));
95  ui.domainEdit->setEnabled(!( m_flags & KPasswordDialog::DomainReadOnly ));
96  ui.passEdit->setEnabled( true );
97  ui.keepCheckBox->setEnabled( true );
98  }
99 }
100 
101 void KPasswordDialog::KPasswordDialogPrivate::init()
102 {
103  ui.setupUi( q->mainWidget() );
104  ui.errorMessage->setHidden(true);
105 
106  // Row 4: Username field
107  if ( m_flags & KPasswordDialog::ShowUsernameLine ) {
108  ui.userEdit->setFocus();
109  QObject::connect( ui.userEdit, SIGNAL(returnPressed()), ui.passEdit, SLOT(setFocus()) );
110  } else {
111  ui.userNameLabel->hide();
112  ui.userEdit->hide();
113  ui.domainLabel->hide();
114  ui.domainEdit->hide();
115  ui.passEdit->setFocus();
116  }
117 
118  if ( !( m_flags & KPasswordDialog::ShowAnonymousLoginCheckBox ) )
119  {
120  ui.anonymousCheckBox->hide();
121  }
122  else
123  {
124  QObject::connect( ui.anonymousCheckBox, SIGNAL(stateChanged(int)), q, SLOT(updateFields()) );
125  }
126 
127  if ( !( m_flags & KPasswordDialog::ShowDomainLine ) )
128  {
129  ui.domainLabel->hide();
130  ui.domainEdit->hide();
131  }
132 
133  if ( !( m_flags & KPasswordDialog::ShowKeepPassword ) )
134  {
135  ui.keepCheckBox->hide();
136  }
137 
138  updateFields();
139 
140  QRect desktop = KGlobalSettings::desktopGeometry(q->topLevelWidget());
141  q->setMinimumWidth(qMin(1000, qMax(q->sizeHint().width(), desktop.width() / 4)));
142  q->setPixmap(KIcon("dialog-password").pixmap(KIconLoader::SizeHuge));
143 }
144 
145 void KPasswordDialog::setPixmap(const QPixmap &pixmap)
146 {
147  if ( !d->pixmapLabel )
148  {
149  d->pixmapLabel = new QLabel( mainWidget() );
150  d->pixmapLabel->setAlignment( Qt::AlignLeft | Qt::AlignTop );
151  d->ui.hboxLayout->insertWidget( 0, d->pixmapLabel );
152  }
153 
154  d->pixmapLabel->setPixmap( pixmap );
155 }
156 
157 QPixmap KPasswordDialog::pixmap() const
158 {
159  if ( !d->pixmapLabel ) {
160  return QPixmap();
161  }
162 
163  return *d->pixmapLabel->pixmap();
164 }
165 
166 
167 void KPasswordDialog::setUsername(const QString& user)
168 {
169  d->ui.userEdit->setText(user);
170  if ( user.isEmpty() )
171  return;
172 
173  d->activated(user);
174  if ( d->ui.userEdit->isVisibleTo( this ) )
175  {
176  d->ui.passEdit->setFocus();
177  }
178 }
179 
180 
181 QString KPasswordDialog::username() const
182 {
183  return d->ui.userEdit->text();
184 }
185 
186 QString KPasswordDialog::password() const
187 {
188  return d->ui.passEdit->text();
189 }
190 
191 void KPasswordDialog::setDomain(const QString& domain)
192 {
193  d->ui.domainEdit->setText(domain);
194 }
195 
196 QString KPasswordDialog::domain() const
197 {
198  return d->ui.domainEdit->text();
199 }
200 
201 void KPasswordDialog::setAnonymousMode(bool anonymous)
202 {
203  d->ui.anonymousCheckBox->setChecked( anonymous );
204 }
205 
206 bool KPasswordDialog::anonymousMode() const
207 {
208  return d->ui.anonymousCheckBox->isChecked();
209 }
210 
211 
212 void KPasswordDialog::setKeepPassword( bool b )
213 {
214  d->ui.keepCheckBox->setChecked( b );
215 }
216 
217 bool KPasswordDialog::keepPassword() const
218 {
219  return d->ui.keepCheckBox->isChecked();
220 }
221 
222 void 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 
264 void 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 
304 void 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 
311 QString KPasswordDialog::prompt() const
312 {
313  return d->ui.prompt->text();
314 }
315 
316 void KPasswordDialog::setPassword(const QString &p)
317 {
318  d->ui.passEdit->setText(p);
319 }
320 
321 void 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 
330 void 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  d->ui.formLayout->removeWidget(d->ui.userEdit);
346  delete d->ui.userEdit;
347  d->userEditCombo = new KComboBox( true, mainWidget() );
348  d->ui.userEdit = d->userEditCombo->lineEdit();
349 // QSize s = d->userEditCombo->sizeHint();
350 // d->ui.userEditCombo->setFixedHeight( s.height() );
351 // d->ui.userEditCombo->setMinimumWidth( s.width() );
352  d->ui.userNameLabel->setBuddy( d->userEditCombo );
353  d->ui.formLayout->setWidget( d->commentRow, QFormLayout::FieldRole, d->userEditCombo );
354  setTabOrder( d->ui.userEdit, d->ui.anonymousCheckBox );
355  setTabOrder( d->ui.anonymousCheckBox, d->ui.domainEdit );
356  setTabOrder( d->ui.domainEdit, d->ui.passEdit );
357  setTabOrder( d->ui.passEdit, d->ui.keepCheckBox );
358  connect( d->ui.userEdit, SIGNAL(returnPressed()), d->ui.passEdit, SLOT(setFocus()) );
359  }
360 
361  d->knownLogins = knownLogins;
362  d->userEditCombo->addItems( knownLogins.keys() );
363  d->userEditCombo->setFocus();
364 
365  connect( d->userEditCombo, SIGNAL(activated(QString)),
366  this, SLOT(activated(QString)) );
367 }
368 
369 void KPasswordDialog::KPasswordDialogPrivate::activated( const QString& userName )
370 {
371  QMap<QString, QString>::ConstIterator it = knownLogins.constFind( userName );
372  if ( it != knownLogins.constEnd() ) {
373  q->setPassword( it.value() );
374  }
375 }
376 
377 void KPasswordDialog::accept()
378 {
379  if (!d->ui.errorMessage->isHidden()) d->ui.errorMessage->setText( QString() );
380 
381  // reset the font in case we had an error previously
382  if (!d->ui.passwordLabel->isHidden()) {
383  d->ui.passwordLabel->setFont( font() );
384  d->ui.userNameLabel->setFont( font() );
385  }
386 
387  // we do this to allow the error message, if any, to go away
388  // checkPassword() may block for a period of time
389  QTimer::singleShot( 0, this, SLOT(actuallyAccept()) );
390 }
391 
392 void KPasswordDialog::KPasswordDialogPrivate::actuallyAccept()
393 {
394  if ( !q->checkPassword() )
395  {
396  return;
397  }
398 
399  bool keep = ui.keepCheckBox->isVisibleTo( q ) && ui.keepCheckBox->isChecked();
400  emit q->gotPassword( q->password(), keep);
401 
402  if ( ui.userEdit->isVisibleTo( q ) ) {
403  emit q->gotUsernameAndPassword( q->username(), q->password() , keep);
404  }
405 
406  q->KDialog::accept();
407 }
408 
409 bool KPasswordDialog::checkPassword()
410 {
411  return true;
412 }
413 
414 #include "kpassworddialog.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Mon Jul 15 2013 05:10:35 by doxygen 1.8.3.1 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.10.4 API Reference

Skip menu "kdelibs-4.10.4 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