Skip to content

Commit

Permalink
Add context menu to the HIBP report
Browse files Browse the repository at this point in the history
to edit entries and toggle the "known bad" flag.

Fixes keepassxreboot#4168
  • Loading branch information
wolframroesler committed Apr 10, 2020
1 parent 24c6fae commit 3ec9c83
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/gui/reports/ReportsWidgetHibp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
#include "core/Global.h"
#include "core/Group.h"
#include "core/PasswordHealth.h"
#include "core/Resources.h"
#include "gui/MessageBox.h"

#include <QMenu>
#include <QStandardItemModel>

namespace {
Expand Down Expand Up @@ -54,6 +56,7 @@ ReportsWidgetHibp::ReportsWidgetHibp(QWidget* parent)
m_ui->hibpTableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);

connect(m_ui->hibpTableView, SIGNAL(doubleClicked(QModelIndex)), SLOT(emitEntryActivated(QModelIndex)));
connect(m_ui->hibpTableView, SIGNAL(customContextMenuRequested(QPoint)), SLOT(customMenuRequested(QPoint)));
connect(m_ui->showKnownBadCheckBox, SIGNAL(stateChanged(int)), this, SLOT(makeHibpTable()));
#ifdef WITH_XC_NETWORKING
connect(&m_downloader, SIGNAL(hibpResult(QString, int)), SLOT(addHibpResult(QString, int)));
Expand Down Expand Up @@ -330,6 +333,57 @@ void ReportsWidgetHibp::refreshAfterEdit()
m_editedEntry = nullptr;
}

void ReportsWidgetHibp::customMenuRequested(QPoint pos){

// Find which entry has been clicked
const auto index = m_ui->hibpTableView->indexAt(pos);
if (!index.isValid()) {
return;
}
m_contextmenuEntry = const_cast<Entry*>(m_rowToEntry[index.row()]);
if (!m_contextmenuEntry) {
return;
}

// Create the context menu
const auto menu = new QMenu(this);

// Create the "edit entry" menu item
const auto edit = new QAction(Resources::instance()->icon("entry-edit"), tr("Edit Entry..."), this);
menu->addAction(edit);
connect(edit, SIGNAL(triggered()), SLOT(editFromContextmenu()));

// Create the "exclude from reports" menu item
const auto knownbad = new QAction(Resources::instance()->icon("reports-exclude"), tr("Exclude from reports"), this);
knownbad->setCheckable(true);
knownbad->setChecked(m_contextmenuEntry->customData()->contains(PasswordHealth::OPTION_KNOWN_BAD)
&& m_contextmenuEntry->customData()->value(PasswordHealth::OPTION_KNOWN_BAD) == TRUE_STR);
menu->addAction(knownbad);
connect(knownbad, SIGNAL(toggled(bool)), SLOT(toggleKnownBad(bool)));

// Show the context menu
menu->popup(m_ui->hibpTableView->viewport()->mapToGlobal(pos));
}

void ReportsWidgetHibp::editFromContextmenu()
{
if (m_contextmenuEntry) {
emit entryActivated(m_contextmenuEntry);
}
}

void ReportsWidgetHibp::toggleKnownBad(bool isKnownBad)
{
if (!m_contextmenuEntry) {
return;
}

m_contextmenuEntry->customData()->set(PasswordHealth::OPTION_KNOWN_BAD,
isKnownBad ? TRUE_STR : FALSE_STR);

makeHibpTable();
}

void ReportsWidgetHibp::saveSettings()
{
// nothing to do - the tab is passive
Expand Down
4 changes: 4 additions & 0 deletions src/gui/reports/ReportsWidgetHibp.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public slots:
void addHibpResult(const QString&, int);
void fetchFailed(const QString& error);
void makeHibpTable();
void customMenuRequested(QPoint);
void editFromContextmenu();
void toggleKnownBad(bool);

private:
void startValidation();
Expand All @@ -74,6 +77,7 @@ public slots:
QPointer<const Entry> m_editedEntry; // The entry we're currently editing
QString m_editedPassword; // The old password of the entry we're editing
bool m_editedKnownBad; // The old "known bad" flag of the entry we're editing
Entry* m_contextmenuEntry = nullptr; // The entry that was right-clicked

#ifdef WITH_XC_NETWORKING
HibpDownloader m_downloader; // This performs the actual HIBP online query
Expand Down
3 changes: 3 additions & 0 deletions src/gui/reports/ReportsWidgetHibp.ui
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@
</property>
<item>
<widget class="QTableView" name="hibpTableView">
<property name="contextMenuPolicy">
<enum>Qt::CustomContextMenu</enum>
</property>
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
Expand Down

0 comments on commit 3ec9c83

Please sign in to comment.