qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
setpassworddialog.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2014-2019 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "setpassworddialog.h"
21 #include "ui_setpassworddialog.h"
22 
23 #include <QApplication>
24 #include <QPushButton>
25 
27 
28 SetPasswordDialog::SetPasswordDialog(QString body, QString extraButton, QWidget* parent)
29  : QDialog(parent)
30  , ui(new Ui::SetPasswordDialog)
31  , body(body + "\n\n")
32 {
33  ui->setupUi(this);
34 
35  connect(ui->passwordlineEdit, SIGNAL(textChanged(QString)), this, SLOT(onPasswordEdit()));
36  connect(ui->repasswordlineEdit, SIGNAL(textChanged(QString)), this, SLOT(onPasswordEdit()));
37 
38  ui->body->setText(body + "\n\n");
39  QPushButton* ok = ui->buttonBox->button(QDialogButtonBox::Ok);
40  ok->setEnabled(false);
41  ok->setText(QApplication::tr("Ok"));
42  QPushButton* cancel = ui->buttonBox->button(QDialogButtonBox::Cancel);
43  cancel->setText(QApplication::tr("Cancel"));
44 
45  if (!extraButton.isEmpty()) {
46  QPushButton* third = new QPushButton(extraButton);
47  ui->buttonBox->addButton(third, QDialogButtonBox::YesRole);
48  connect(third, &QPushButton::clicked, this, [&]() { this->done(Tertiary); });
49  }
50 }
51 
53 {
54  delete ui;
55 }
56 
58 {
59  QString pswd = ui->passwordlineEdit->text();
60 
61  if (pswd.isEmpty()) {
62  ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
63  ui->body->setText(body);
64  } else if (pswd.length() < 6) {
65  ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
66  ui->body->setText(body + tr("The password is too short."));
67  } else if (pswd != ui->repasswordlineEdit->text()) {
68  ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
69  ui->body->setText(body + tr("The password doesn't match."));
70  } else {
71  ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
72  ui->body->setText(body);
73  }
74 
75  ui->passStrengthMeter->setValue(getPasswordStrength(pswd));
76 }
77 
79 {
80  if (pass.size() < 6)
81  return 0;
82 
83  double fscore = 0;
84  QHash<QChar, int> charCounts;
85  for (QChar c : pass) {
86  charCounts[c]++;
87  fscore += 5. / charCounts[c];
88  }
89 
90  int variations = -1;
91  variations += pass.contains(QRegExp("[0-9]", Qt::CaseSensitive, QRegExp::RegExp)) ? 1 : 0;
92  variations += pass.contains(QRegExp("[a-z]", Qt::CaseSensitive, QRegExp::RegExp)) ? 1 : 0;
93  variations += pass.contains(QRegExp("[A-Z]", Qt::CaseSensitive, QRegExp::RegExp)) ? 1 : 0;
94  variations += pass.contains(QRegExp("[\\W]", Qt::CaseSensitive, QRegExp::RegExp)) ? 1 : 0;
95 
96  int score = fscore;
97  score += variations * 10;
98  score -= 20;
99  score = std::min(score, 100);
100  score = std::max(score, 0);
101 
102  return score;
103 }
104 
106 {
107  return ui->passwordlineEdit->text();
108 }
SetPasswordDialog::onPasswordEdit
void onPasswordEdit()
Definition: setpassworddialog.cpp:57
SetPasswordDialog
Definition: setpassworddialog.h:28
SetPasswordDialog::Tertiary
@ Tertiary
Definition: setpassworddialog.h:37
SetPasswordDialog::getPasswordStrength
static int getPasswordStrength(QString pass)
Definition: setpassworddialog.cpp:78
SetPasswordDialog::getPassword
QString getPassword()
Definition: setpassworddialog.cpp:105
setpassworddialog.h
Ui
Definition: filetransferwidget.h:30
QHash
Definition: friendlist.h:27
SetPasswordDialog::~SetPasswordDialog
~SetPasswordDialog()
Definition: setpassworddialog.cpp:52
SetPasswordDialog::reasonablePasswordLength
static const double reasonablePasswordLength
Definition: setpassworddialog.h:50
SetPasswordDialog::SetPasswordDialog
SetPasswordDialog(QString body, QString extraButton, QWidget *parent=nullptr)
Definition: setpassworddialog.cpp:28
SetPasswordDialog::ui
Ui::SetPasswordDialog * ui
Definition: setpassworddialog.h:48
SetPasswordDialog::body
QString body
Definition: setpassworddialog.h:49