Skip to content

Commit

Permalink
Buttons for adding/deleting custom wordlists
Browse files Browse the repository at this point in the history
  • Loading branch information
snipfoo committed Aug 3, 2021
1 parent da5742d commit 25853c0
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 16 deletions.
46 changes: 46 additions & 0 deletions share/translations/keepassx_en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5367,6 +5367,52 @@ Expect some bugs and minor issues, this version is not meant for production use.
<source>(CUSTOM)</source>
<translation>(CUSTOM)</translation>
</message>
<message>
<source>Delete selected wordlist</source>
<translation>Delete selected wordlist</translation>
</message>
<message>
<source>Delete the selected wordlist?</source>
<translation>Delete the selected wordlist?</translation>
</message>
<message>
<source>Do you really want to delete the wordlist &quot;%1&quot;?</source>
<translation>Do you really want to delete the wordlist &quot;%1&quot;?</translation>
</message>
<message>
<source>Failed to delete wordlist</source>
<translation>Failed to delete wordlist</translation>
</message>
<message>
<source>Cannot delete the system wordlist &quot;%1&quot;</source>
<translation>Cannot delete the system wordlist &quot;%1&quot;</translation>
</message>
<message>
<source>Add custom wordlist</source>
<translation>Add custom wordlist</translation>
</message>
<message>
<source>Wordlists</source>
<translation>Wordlists</translation>
</message>
<message>
<source>All files</source>
<translation>All files</translation>
</message>
<message>
<source>Wordlist already exists</source>
<translation>Wordlist already exists</translation>
</message>
<message>
<source>Wordlist &quot;%1&quot; already exists.
Do you want to overwrite it?</source>
<translation>Wordlist &quot;%1&quot; already exists.
Do you want to overwrite it?</translation>
</message>
<message>
<source>Failed to add wordlist</source>
<translation>Failed to add wordlist</translation>
</message>
<message>
<source>Word Separator:</source>
<translation>Word Separator:</translation>
Expand Down
46 changes: 46 additions & 0 deletions share/translations/keepassx_fr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5316,6 +5316,52 @@ Attendez-vous à des bogues et des problèmes mineurs. Cette version n’est pas
<source>(CUSTOM)</source>
<translation>(PERSONNALISÉE)</translation>
</message>
<message>
<source>Delete selected wordlist</source>
<translation>Supprimer la liste de mots sélectionnée</translation>
</message>
<message>
<source>Delete the selected wordlist?</source>
<translation>Supprimer la liste de mots sélectionnée ?</translation>
</message>
<message>
<source>Do you really want to delete the wordlist &quot;%1&quot;?</source>
<translation>Souhaitez-vous vraiment supprimer la liste de mots « %1 » ?</translation>
</message>
<message>
<source>Failed to delete wordlist</source>
<translation>Impossible de supprimer la liste de mots</translation>
</message>
<message>
<source>Cannot delete the system wordlist &quot;%1&quot;</source>
<translation>Impossible de supprimer la liste de mots système « %1 »</translation>
</message>
<message>
<source>Add custom wordlist</source>
<translation>Ajouter une liste de mots personnalisée</translation>
</message>
<message>
<source>Wordlists</source>
<translation>Listes de mots</translation>
</message>
<message>
<source>All files</source>
<translation>Tous les fichiers</translation>
</message>
<message>
<source>Wordlist already exists</source>
<translation>La liste de mots existe déjà</translation>
</message>
<message>
<source>Wordlist &quot;%1&quot; already exists.
Do you want to overwrite it?</source>
<translation>La liste de mots « %1 » existe déjà.
Souhaitez-vous la remplacer ?</translation>
</message>
<message>
<source>Failed to add wordlist</source>
<translation>Impossible d’ajouter la liste de mots</translation>
</message>
<message>
<source>Word Separator:</source>
<translation>Séparateur de mot :</translation>
Expand Down
133 changes: 125 additions & 8 deletions src/gui/PasswordGeneratorWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "core/Resources.h"
#include "gui/Clipboard.h"
#include "gui/Icons.h"
#include "gui/FileDialog.h"
#include "gui/MessageBox.h"
#include "gui/styles/StateColorPalette.h"

PasswordGeneratorWidget::PasswordGeneratorWidget(QWidget* parent)
Expand All @@ -42,6 +44,8 @@ PasswordGeneratorWidget::PasswordGeneratorWidget(QWidget* parent)
m_ui->buttonGenerate->setToolTip(
tr("Regenerate password (%1)").arg(m_ui->buttonGenerate->shortcut().toString(QKeySequence::NativeText)));
m_ui->buttonCopy->setIcon(icons()->icon("clipboard-text"));
m_ui->buttonDeleteWordList->setIcon(icons()->icon("entry-delete"));
m_ui->buttonAddWordList->setIcon(icons()->icon("document-new"));
m_ui->buttonClose->setShortcut(Qt::Key_Escape);

// Add two shortcuts to save the form CTRL+Enter and CTRL+S
Expand All @@ -59,6 +63,8 @@ PasswordGeneratorWidget::PasswordGeneratorWidget(QWidget* parent)
connect(m_ui->buttonApply, SIGNAL(clicked()), SLOT(applyPassword()));
connect(m_ui->buttonCopy, SIGNAL(clicked()), SLOT(copyPassword()));
connect(m_ui->buttonGenerate, SIGNAL(clicked()), SLOT(regeneratePassword()));
connect(m_ui->buttonDeleteWordList, SIGNAL(clicked()), SLOT(deleteWordList()));
connect(m_ui->buttonAddWordList, SIGNAL(clicked()), SLOT(addWordList()));
connect(m_ui->buttonClose, SIGNAL(clicked()), SIGNAL(closed()));

connect(m_ui->sliderLength, SIGNAL(valueChanged(int)), SLOT(passwordLengthChanged(int)));
Expand Down Expand Up @@ -109,14 +115,6 @@ PasswordGeneratorWidget::PasswordGeneratorWidget(QWidget* parent)
}
}

if (m_ui->comboBoxWordList->count() > 1) {
m_ui->comboBoxWordList->setVisible(true);
m_ui->labelWordList->setVisible(true);
} else {
m_ui->comboBoxWordList->setVisible(false);
m_ui->labelWordList->setVisible(false);
}

loadSettings();
}

Expand Down Expand Up @@ -339,6 +337,115 @@ bool PasswordGeneratorWidget::isPasswordVisible() const
return m_ui->editNewPassword->isPasswordVisible();
}

void PasswordGeneratorWidget::deleteWordList()
{
const QVariant itemData = m_ui->comboBoxWordList->currentData();
if (static_cast<QMetaType::Type>(itemData.type()) != QMetaType::QString || itemData.toString().isEmpty()) {
return;
}
QString path = itemData.toString();
QString name = QFileInfo(path).fileName();

auto result =
MessageBox::question(this,
tr("Delete the selected wordlist?"),
tr("Do you really want to delete the wordlist \"%1\"?").arg(name),
MessageBox::Delete | MessageBox::Cancel,
MessageBox::Cancel);
if (result != MessageBox::Delete) {
return;
}

if (!path.startsWith("/")) {
MessageBox::critical(this,
tr("Failed to delete wordlist"),
tr("Cannot delete the system wordlist \"%1\"").arg(path));
return;
}

QFile file(path);
if (!file.remove()) {
MessageBox::critical(this, tr("Failed to delete wordlist"), file.errorString());
return;
}

m_ui->comboBoxWordList->removeItem(m_ui->comboBoxWordList->currentIndex());
updateGenerator();
}

void PasswordGeneratorWidget::addWordList()
{
QString filter = QString("%1 (*.txt *.asc *.wordlist);;%2 (*)").arg(tr("Wordlists"), tr("All files"));
QString filePath = fileDialog()->getOpenFileName(this, tr("Add custom wordlist"), "", filter);
if (filePath.isEmpty()) {
return;
}

// create directory for user-specified wordlists, if necessary
QDir destDir(resources()->userWordlistPath(""));
if (!destDir.exists()) {
destDir.mkpath(".");
}

// check if destination wordlist already exists
QString fileName = QFileInfo(filePath).fileName();
QString destPath = destDir.absolutePath() + "/" + fileName;
QFile dest(destPath);
if (dest.exists()) {
auto response =
MessageBox::warning(this,
tr("Wordlist already exists"),
tr("Wordlist \"%1\" already exists.\n"
"Do you want to overwrite it?").arg(fileName),
MessageBox::Overwrite | MessageBox::Cancel,
MessageBox::Cancel);
if (response != MessageBox::Overwrite) {
return;
}
if (!dest.remove()) {
MessageBox::critical(this, tr("Failed to delete wordlist"), dest.errorString());
return;
}
}

// indicate if we need to update the password generator
bool needsUpdate = false;

// remove existing wordlist (if any) from the combo box
int i = m_ui->comboBoxWordList->findData(destPath);
if (i > -1) {
if (m_ui->comboBoxWordList->currentIndex() == i) {
needsUpdate = true;
}
m_ui->comboBoxWordList->removeItem(i);
}

// copy wordlist to destination path and add corresponding item to the combo box
QFile file(filePath);
if (!file.copy(destPath)) {
MessageBox::critical(this, tr("Failed to add wordlist"), file.errorString());
} else {
int i;
for (i = 0; i < m_ui->comboBoxWordList->count(); ++i) {
const QVariant itemData = m_ui->comboBoxWordList->itemData(i);
if (static_cast<QMetaType::Type>(itemData.type()) == QMetaType::QString) {
const QString itemPath = itemData.toString();
if (itemPath.startsWith("/") && destPath < itemPath) {
break;
}
}
}
m_ui->comboBoxWordList->insertItem(i, tr("(CUSTOM)") + " " + fileName, destPath);
m_ui->comboBoxWordList->setCurrentIndex(i);
needsUpdate = true;
}

// update the password generator, if needed
if (needsUpdate) {
updateGenerator();
}
}

void PasswordGeneratorWidget::setAdvancedMode(bool advanced)
{
saveSettings();
Expand Down Expand Up @@ -550,14 +657,24 @@ void PasswordGeneratorWidget::updateGenerator()
static_cast<PassphraseGenerator::PassphraseWordCase>(m_ui->wordCaseComboBox->currentData().toInt()));

m_dicewareGenerator->setWordCount(m_ui->spinBoxWordCount->value());

if (m_ui->comboBoxWordList->count() > 1) {
m_ui->comboBoxWordList->setEnabled(true);
} else {
m_ui->comboBoxWordList->setEnabled(false);
}
const QVariant itemData = m_ui->comboBoxWordList->currentData();
if (static_cast<QMetaType::Type>(itemData.type()) == QMetaType::QString && !itemData.toString().isEmpty()) {
QString path = itemData.toString();
if (!path.startsWith("/")) {
path = resources()->wordlistPath(path);
m_ui->buttonDeleteWordList->setEnabled(false);
} else {
m_ui->buttonDeleteWordList->setEnabled(true);
}
m_dicewareGenerator->setWordList(path);
}

m_dicewareGenerator->setWordSeparator(m_ui->editWordSeparator->text());

if (m_dicewareGenerator->isValid()) {
Expand Down
2 changes: 2 additions & 0 deletions src/gui/PasswordGeneratorWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public slots:
void applyPassword();
void copyPassword();
void setPasswordVisible(bool visible);
void deleteWordList();
void addWordList();

signals:
void appliedPassword(const QString& password);
Expand Down
48 changes: 40 additions & 8 deletions src/gui/PasswordGeneratorWidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -862,14 +862,44 @@ QProgressBar::chunk {
</layout>
</item>
<item row="0" column="2">
<widget class="QComboBox" name="comboBoxWordList">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
<layout class="QHBoxLayout" name="horizontalLayout_10">
<item>
<widget class="QComboBox" name="comboBoxWordList">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="buttonDeleteWordList">
<property name="focusPolicy">
<enum>Qt::TabFocus</enum>
</property>
<property name="toolTip">
<string>Delete selected wordlist</string>
</property>
<property name="accessibleDescription">
<string>Delete selected wordlist</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="buttonAddWordList">
<property name="focusPolicy">
<enum>Qt::TabFocus</enum>
</property>
<property name="toolTip">
<string>Add custom wordlist</string>
</property>
<property name="accessibleDescription">
<string>Add custom wordlist</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="4" column="2" alignment="Qt::AlignLeft">
<widget class="QLabel" name="charactersInPassphraseLabel">
Expand Down Expand Up @@ -990,6 +1020,8 @@ QProgressBar::chunk {
<tabstop>checkBoxExcludeAlike</tabstop>
<tabstop>checkBoxEnsureEvery</tabstop>
<tabstop>comboBoxWordList</tabstop>
<tabstop>buttonDeleteWordList</tabstop>
<tabstop>buttonAddWordList</tabstop>
<tabstop>sliderWordCount</tabstop>
<tabstop>spinBoxWordCount</tabstop>
<tabstop>editWordSeparator</tabstop>
Expand Down

0 comments on commit 25853c0

Please sign in to comment.