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

KDEUI

  • sources
  • kde-4.14
  • kdelibs
  • kdeui
  • dialogs
knewpassworddialog.cpp
Go to the documentation of this file.
1 // vi: ts=8 sts=4 sw=4
2 /* This file is part of the KDE libraries
3  Copyright (C) 1998 Pietro Iglio <iglio@fub.it>
4  Copyright (C) 1999,2000 Geert Jansen <jansen@kde.org>
5  Copyright (C) 2004,2005 Andrew Coles <andrew_coles@yahoo.co.uk>
6  Copyright (C) 2007 MichaĆ«l Larouche <larouche@kde.org>
7  Copyright (C) 2009 Christoph Feck <christoph@maxiom.de>
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Library General Public
11  License version 2 as published by the Free Software Foundation.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Library General Public License for more details.
17 
18  You should have received a copy of the GNU Library General Public License
19  along with this library; see the file COPYING.LIB. If not, write to
20  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  Boston, MA 02110-1301, USA.
22 */
23 #include "knewpassworddialog.h"
24 
25 #include <QtGui/QApplication>
26 #include <QtGui/QProgressBar>
27 #include <QtCore/QRegExp>
28 #include <QtCore/QSize>
29 #include <QtCore/QString>
30 
31 #include <kapplication.h>
32 #include <kglobal.h>
33 #include <kicon.h>
34 #include <klocale.h>
35 #include <kmessagebox.h>
36 #include <klineedit.h>
37 #include <ktitlewidget.h>
38 
39 #include "ui_knewpassworddialog.h"
40 
41 class KNewPasswordDialog::KNewPasswordDialogPrivate
42 {
43 public:
44  KNewPasswordDialogPrivate( KNewPasswordDialog *parent )
45  : q( parent ),
46  minimumPasswordLength(0), passwordStrengthWarningLevel(1),reasonablePasswordLength(8)
47  {}
48 
49  void init();
50  void _k_textChanged();
51 
52  KNewPasswordDialog *q;
53 
54  int minimumPasswordLength;
55  int passwordStrengthWarningLevel;
56  int reasonablePasswordLength;
57 
58  int effectivePasswordLength(const QString &password);
59 
60  QString pass;
61 
62  Ui::KNewPasswordDialog ui;
63 };
64 
65 
66 void KNewPasswordDialog::KNewPasswordDialogPrivate::init()
67 {
68  q->setButtons( Ok | Cancel );
69  q->setDefaultButton( Ok );
70 
71  ui.setupUi( q->mainWidget() );
72 
73  ui.labelIcon->setPixmap( KIcon("dialog-password").pixmap(96, 96) );
74  ui.labelMatch->setHidden(true);
75 
76  const QString strengthBarWhatsThis(i18n("The password strength meter gives an indication of the security "
77  "of the password you have entered. To improve the strength of "
78  "the password, try:\n"
79  " - using a longer password;\n"
80  " - using a mixture of upper- and lower-case letters;\n"
81  " - using numbers or symbols, such as #, as well as letters."));
82  ui.labelStrengthMeter->setWhatsThis(strengthBarWhatsThis);
83  ui.strengthBar->setWhatsThis(strengthBarWhatsThis);
84 
85  connect( ui.linePassword, SIGNAL(textChanged(QString)), q, SLOT(_k_textChanged()) );
86  connect( ui.lineVerifyPassword, SIGNAL(textChanged(QString)), q, SLOT(_k_textChanged()) );
87 
88  _k_textChanged();
89 }
90 
91 
92 int KNewPasswordDialog::KNewPasswordDialogPrivate::effectivePasswordLength(const QString &password)
93 {
94  enum Category {
95  Digit,
96  Upper,
97  Vowel,
98  Consonant,
99  Special
100  };
101 
102  Category previousCategory = Vowel;
103  QString vowels("aeiou");
104  int count = 0;
105 
106  for (int i = 0; i < password.length(); ++i) {
107  QChar currentChar = password.at(i);
108  if (!password.left(i).contains(currentChar)) {
109  Category currentCategory;
110  switch (currentChar.category()) {
111  case QChar::Letter_Uppercase:
112  currentCategory = Upper;
113  break;
114  case QChar::Letter_Lowercase:
115  if (vowels.contains(currentChar)) {
116  currentCategory = Vowel;
117  } else {
118  currentCategory = Consonant;
119  }
120  break;
121  case QChar::Number_DecimalDigit:
122  currentCategory = Digit;
123  break;
124  default:
125  currentCategory = Special;
126  break;
127  }
128  switch (currentCategory) {
129  case Vowel:
130  if (previousCategory != Consonant) {
131  ++count;
132  }
133  break;
134  case Consonant:
135  if (previousCategory != Vowel) {
136  ++count;
137  }
138  break;
139  default:
140  if (previousCategory != currentCategory) {
141  ++count;
142  }
143  break;
144  }
145  previousCategory = currentCategory;
146  }
147  }
148  return count;
149 }
150 
151 
152 void KNewPasswordDialog::KNewPasswordDialogPrivate::_k_textChanged()
153 {
154  const bool match = ui.linePassword->text() == ui.lineVerifyPassword->text();
155 
156  const int minPasswordLength = q->minimumPasswordLength();
157 
158  if ( ui.linePassword->text().length() < minPasswordLength) {
159  q->enableButtonOk(false);
160  } else {
161  q->enableButtonOk( match );
162  }
163 
164  if ( match && !q->allowEmptyPasswords() && ui.linePassword->text().isEmpty()) {
165  ui.labelMatch->setPixmap( KIcon("dialog-error") );
166  ui.labelMatch->setText( i18n("Password is empty") );
167  }
168  else {
169  if ( ui.linePassword->text().length() < minPasswordLength ) {
170  ui.labelMatch->setPixmap( KIcon("dialog-error") );
171  ui.labelMatch->setText(i18np("Password must be at least 1 character long", "Password must be at least %1 characters long", minPasswordLength));
172  } else {
173  ui.labelMatch->setPixmap( match ? KIcon("dialog-ok") : KIcon("dialog-error") );
174  // "ok" icon should probably be "dialog-success", but we don't have that icon in KDE 4.0
175  ui.labelMatch->setText( match? i18n("Passwords match")
176  :i18n("Passwords do not match") );
177  }
178  }
179 
180  // Password strength calculator
181  int pwstrength = (20 * ui.linePassword->text().length() + 80 * effectivePasswordLength(ui.linePassword->text())) / qMax(reasonablePasswordLength, 2);
182  if (pwstrength < 0) {
183  pwstrength = 0;
184  } else if (pwstrength > 100) {
185  pwstrength = 100;
186  }
187  ui.strengthBar->setValue(pwstrength);
188 }
189 
190 /*
191  * Password dialog.
192  */
193 
194 KNewPasswordDialog::KNewPasswordDialog( QWidget *parent)
195  : KDialog(parent), d(new KNewPasswordDialogPrivate(this))
196 {
197  d->init();
198 }
199 
200 
201 KNewPasswordDialog::~KNewPasswordDialog()
202 {
203  delete d;
204 }
205 
206 
207 void KNewPasswordDialog::setPrompt(const QString &prompt)
208 {
209  d->ui.labelPrompt->setText(prompt);
210 }
211 
212 
213 QString KNewPasswordDialog::prompt() const
214 {
215  return d->ui.labelPrompt->text();
216 }
217 
218 
219 void KNewPasswordDialog::setPixmap(const QPixmap &pixmap)
220 {
221  d->ui.labelIcon->setPixmap(pixmap);
222  d->ui.labelIcon->setFixedSize( d->ui.labelIcon->sizeHint() );
223 }
224 
225 
226 QPixmap KNewPasswordDialog::pixmap() const
227 {
228  return *d->ui.labelIcon->pixmap();
229 }
230 
231 bool KNewPasswordDialog::checkAndGetPassword(QString *pwd)
232 {
233  pwd->clear();
234  if ( d->ui.linePassword->text() != d->ui.lineVerifyPassword->text() ) {
235  d->ui.labelMatch->setPixmap( KTitleWidget::ErrorMessage );
236  d->ui.labelMatch->setText( i18n("You entered two different "
237  "passwords. Please try again.") );
238 
239  d->ui.linePassword->clear();
240  d->ui.lineVerifyPassword->clear();
241  return false;
242  }
243  if (d->ui.strengthBar && d->ui.strengthBar->value() < d->passwordStrengthWarningLevel) {
244  int retVal = KMessageBox::warningYesNo(this,
245  i18n( "The password you have entered has a low strength. "
246  "To improve the strength of "
247  "the password, try:\n"
248  " - using a longer password;\n"
249  " - using a mixture of upper- and lower-case letters;\n"
250  " - using numbers or symbols as well as letters.\n"
251  "\n"
252  "Would you like to use this password anyway?"),
253  i18n("Low Password Strength"));
254  if (retVal == KMessageBox::No) return false;
255  }
256  if ( !checkPassword(d->ui.linePassword->text()) ) {
257  return false;
258  }
259 
260  *pwd = d->ui.linePassword->text();
261  return true;
262 }
263 
264 void KNewPasswordDialog::accept()
265 {
266  QString pwd;
267  if (!checkAndGetPassword(&pwd)) {
268  return;
269  }
270  d->pass = pwd;
271  emit newPassword( d->pass );
272  KDialog::accept();
273 }
274 
275 
276 void KNewPasswordDialog::setAllowEmptyPasswords(bool allowed)
277 {
278  setMinimumPasswordLength( allowed ? 0 : 1 );
279  d->_k_textChanged();
280 }
281 
282 
283 bool KNewPasswordDialog::allowEmptyPasswords() const
284 {
285  return d->minimumPasswordLength == 0;
286 }
287 
288 void KNewPasswordDialog::setMinimumPasswordLength(int minLength)
289 {
290  d->minimumPasswordLength = minLength;
291  d->_k_textChanged();
292 }
293 
294 int KNewPasswordDialog::minimumPasswordLength() const
295 {
296  return d->minimumPasswordLength;
297 }
298 
299 void KNewPasswordDialog::setMaximumPasswordLength(int maxLength)
300 {
301  d->ui.linePassword->setMaxLength(maxLength);
302  d->ui.lineVerifyPassword->setMaxLength(maxLength);
303 }
304 
305 int KNewPasswordDialog::maximumPasswordLength() const
306 {
307  return d->ui.linePassword->maxLength();
308 }
309 
310 // reasonable password length code contributed by Steffen Mthing
311 
312 void KNewPasswordDialog::setReasonablePasswordLength(int reasonableLength)
313 {
314 
315  if (reasonableLength < 1) {
316  reasonableLength = 1;
317  }
318  if (reasonableLength >= maximumPasswordLength()) {
319  reasonableLength = maximumPasswordLength();
320  }
321 
322  d->reasonablePasswordLength = reasonableLength;
323 
324 }
325 
326 int KNewPasswordDialog::reasonablePasswordLength() const
327 {
328  return d->reasonablePasswordLength;
329 }
330 
331 
332 void KNewPasswordDialog::setPasswordStrengthWarningLevel(int warningLevel)
333 {
334  if (warningLevel < 0) {
335  warningLevel = 0;
336  }
337  if (warningLevel > 99) {
338  warningLevel = 99;
339  }
340  d->passwordStrengthWarningLevel = warningLevel;
341 }
342 
343 int KNewPasswordDialog::passwordStrengthWarningLevel() const
344 {
345  return d->passwordStrengthWarningLevel;
346 }
347 
348 QString KNewPasswordDialog::password() const
349 {
350  return d->pass;
351 }
352 
353 bool KNewPasswordDialog::checkPassword(const QString &)
354 {
355  return true;
356 }
357 
358 #include "knewpassworddialog.moc"
359 
360 // kate: space-indent on; indent-width 4; encoding utf-8; replace-tabs on;
i18n
QString i18n(const char *text)
KMessageBox::No
Definition: kmessagebox.h:73
QWidget
KNewPasswordDialog::setMaximumPasswordLength
void setMaximumPasswordLength(int maxLength)
Maximum acceptable password length.
Definition: knewpassworddialog.cpp:299
KNewPasswordDialog::checkPassword
virtual bool checkPassword(const QString &)
Virtual function that can be overridden to provide password checking in derived classes.
Definition: knewpassworddialog.cpp:353
ktitlewidget.h
kapplication.h
KMessageBox::warningYesNo
static int warningYesNo(QWidget *parent, const QString &text, const QString &caption=QString(), const KGuiItem &buttonYes=KStandardGuiItem::yes(), const KGuiItem &buttonNo=KStandardGuiItem::no(), const QString &dontAskAgainName=QString(), Options options=Options(Notify|Dangerous))
Display a "warning" dialog.
Definition: kmessagebox.cpp:564
QChar
i18np
QString i18np(const char *sing, const char *plur, const A1 &a1)
KNewPasswordDialog
A password input dialog.
Definition: knewpassworddialog.h:68
KNewPasswordDialog::prompt
QString prompt() const
Returns the password prompt.
Definition: knewpassworddialog.cpp:213
KNewPasswordDialog::passwordStrengthWarningLevel
int passwordStrengthWarningLevel() const
Password strength level below which a warning is given.
Definition: knewpassworddialog.cpp:343
KDialog
A dialog base class with standard buttons and predefined layouts.
Definition: kdialog.h:128
QChar::category
Category category() const
klocale.h
KNewPasswordDialog::setAllowEmptyPasswords
void setAllowEmptyPasswords(bool allowed)
Allow empty passwords? - Default: true.
Definition: knewpassworddialog.cpp:276
KNewPasswordDialog::allowEmptyPasswords
bool allowEmptyPasswords() const
Allow empty passwords?
Definition: knewpassworddialog.cpp:283
QString::clear
void clear()
KStandardGuiItem::Ok
Definition: kstandardguiitem.h:50
kglobal.h
KNewPasswordDialog::minimumPasswordLength
int minimumPasswordLength() const
Minimum acceptable password length.
Definition: knewpassworddialog.cpp:294
KNewPasswordDialog::setPixmap
void setPixmap(const QPixmap &)
Sets the pixmap that appears next to the prompt in the dialog.
Definition: knewpassworddialog.cpp:219
KNewPasswordDialog::accept
virtual void accept()
Definition: knewpassworddialog.cpp:264
KIcon
A wrapper around QIcon that provides KDE icon features.
Definition: kicon.h:40
QString
KNewPasswordDialog::password
QString password() const
Returns the password entered.
Definition: knewpassworddialog.cpp:348
QDialog::accept
virtual void accept()
QPixmap
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
KStandardGuiItem::Cancel
Definition: kstandardguiitem.h:50
KNewPasswordDialog::setPasswordStrengthWarningLevel
void setPasswordStrengthWarningLevel(int warningLevel)
Set the password strength level below which a warning is given Value is in the range 0 to 99...
Definition: knewpassworddialog.cpp:332
QString::at
const QChar at(int position) const
KNewPasswordDialog::pixmap
QPixmap pixmap() const
Returns the pixmap that appears next to the prompt in the dialog.
Definition: knewpassworddialog.cpp:226
KNewPasswordDialog::KNewPasswordDialog
KNewPasswordDialog(QWidget *parent=0)
Constructs a password dialog.
Definition: knewpassworddialog.cpp:194
KNewPasswordDialog::maximumPasswordLength
int maximumPasswordLength() const
Maximum acceptable password length.
Definition: knewpassworddialog.cpp:305
QString::length
int length() const
KNewPasswordDialog::setMinimumPasswordLength
void setMinimumPasswordLength(int minLength)
Minimum acceptable password length.
Definition: knewpassworddialog.cpp:288
QString::left
QString left(int n) const
KTitleWidget::ErrorMessage
An error message.
Definition: ktitlewidget.h:89
KNewPasswordDialog::newPassword
void newPassword(const QString &password)
The dialog has been accepted, and the new password is password.
KNewPasswordDialog::~KNewPasswordDialog
virtual ~KNewPasswordDialog()
Destructs the password dialog.
Definition: knewpassworddialog.cpp:201
klineedit.h
KNewPasswordDialog::checkAndGetPassword
bool checkAndGetPassword(QString *pwd)
Checks input password.
Definition: knewpassworddialog.cpp:231
kicon.h
kmessagebox.h
QObject::parent
QObject * parent() const
KNewPasswordDialog::reasonablePasswordLength
int reasonablePasswordLength() const
Password length that is expected to be reasonably safe.
Definition: knewpassworddialog.cpp:326
KNewPasswordDialog::setPrompt
void setPrompt(const QString &prompt)
Sets the password prompt.
Definition: knewpassworddialog.cpp:207
knewpassworddialog.h
KNewPasswordDialog::setReasonablePasswordLength
void setReasonablePasswordLength(int reasonableLength)
Password length that is expected to be reasonably safe.
Definition: knewpassworddialog.cpp:312
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:23:59 by doxygen 1.8.7 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 API Reference

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

Search



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