-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
120 lines (102 loc) · 3.44 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Copyright (C) 2013 Guy Rutenberg
* http://www.guyrutenberg.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <sstream>
#include <QDebug>
#include <QClipboard>
#include <cmath>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "audio_random.h"
#include "audio_random_alsa.h"
#include "audio_random_portaudio.h"
#include "audio_random_oss.h"
#include "spass_utils.h"
#include "config.h"
const int DEFAULT_MAX_STRENGTH = 128;
extern "C" char * Dicewds8k[1<<13];
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->strengthBar->setMaximum(DEFAULT_MAX_STRENGTH);
setWindowTitle(PACKAGE_STRING);
AudioRandom::getInstance()->setBackend(AUDIO_RANDOM_BACKEND::getInstance());
updateStrip();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_copyButton_clicked()
{
QApplication::clipboard()->setText(ui->outputEdit->text());
}
void MainWindow::on_generateButton_clicked()
{
int length = ui->lengthSpinBox->value();
string out;
strip->generatePassword(length, out);
ui->outputEdit->setText(QString::fromUtf8(out.c_str()));
}
void MainWindow::updateStrength()
{
double bits_per_symbol = max(log2(strip->strip.size()), 0.0);
//qDebug() << "Bits" << bits_per_symbol;
int total_bits = bits_per_symbol * ui->lengthSpinBox->value();
ui->strengthBar->setMaximum(std::max(DEFAULT_MAX_STRENGTH, total_bits));
ui->strengthBar->setValue(total_bits);
}
void MainWindow::updateStrip()
{
strip.reset(new SpassStrip);
if (ui->tabWidget->currentWidget() == ui->tabPassword) {
strip->separator = "";
for (const auto& widget : ui->tabPassword->findChildren<QCheckBox *>()) {
if (widget->isChecked()) {
const QByteArray strip_chars = widget->property("strip").toString().toLatin1();
strip->strip.reserve(strip->strip.size() + strip_chars.size());
const char *i = strip_chars.data();
for (const char *i = strip_chars.data(); *i; i++)
strip->strip.push_back(string(1, *i));
}
}
} else if (ui->tabWidget->currentWidget() == ui->tabPassphrase) {
strip->separator = " ";
strip->strip.assign(std::begin(Dicewds8k), std::end(Dicewds8k));
} else if (ui->tabWidget->currentWidget() == ui->tabCustom) {
// first copy the custom strip into the "strip" property of
// the corresponding radioButton.
ui->radioButtonCustom->setProperty("strip", ui->lineEditCustom->text());
strip->separator = "";
for (const auto& widget : ui->tabCustom->findChildren<QRadioButton *>()) {
if (!widget->isChecked())
continue;
const QByteArray strip_chars = widget->property("strip").toString().toLatin1();
strip->strip.clear();
for (const char *i = strip_chars.data(); *i; i++)
strip->strip.push_back(string(1, *i));
break;
}
} else {
qWarning() << "MainWindow::updateStrip: Unknown tab";
}
updateStrength();
}