Skip to content

Commit

Permalink
HIBP: Sort the table by how often a pwd is exposed
Browse files Browse the repository at this point in the history
  • Loading branch information
wolframroesler committed Mar 14, 2020
1 parent 1257cbf commit e6dc9d5
Showing 1 changed file with 38 additions and 22 deletions.
60 changes: 38 additions & 22 deletions src/gui/reports/ReportsWidgetHibp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,43 +80,59 @@ void ReportsWidgetHibp::makeHibpTable() {

// Reset the table
m_referencesModel->clear();
m_referencesModel->setHorizontalHeaderLabels(QStringList() << tr("Title") << tr("Path") << tr("Password has been exposed"));
m_referencesModel->setHorizontalHeaderLabels(QStringList() << tr("Title") << tr("Path") << tr("Password exposed"));
m_rowToEntry.clear();

// Search database for passwords that we've found so far
std::vector<std::tuple<const Entry*, const Group*, int>> items;
for (const auto* group : m_db->rootGroup()->groupsRecursive(true)) {
if (!group->isRecycled()) {
for (const auto* entry : group->entries()) {
if (!entry->isRecycled()) {
const auto found = m_pwPwned.find(entry->password());
if (found!=m_pwPwned.end()) {

// Found a pwned entry: Add it to the table
const auto count = *found;
auto row = QList<QStandardItem*>();
row << new QStandardItem(entry->iconPixmap(), entry->title());
row << new QStandardItem(group->iconPixmap(), group->hierarchy().join("/"));
row << new QStandardItem(
count==1 ? tr("once") :
count <= 10 ? tr("10 times") :
count <= 100 ? tr("100 times") :
count <= 1000 ? tr("1000 times") :
count <= 10*1000 ? tr("10,000 times") :
count <= 100*1000 ? tr("100,000 times") :
count <= 1000*1000 ? tr("a million times") :
tr("millions of times")
);
m_referencesModel->appendRow(row);
row[2]->setForeground(QBrush(QColor("red")));

// Store entry pointer per table row (used in double click handler)
m_rowToEntry.append({group, entry});
items.emplace_back(entry, group, *found);
}
}
}
}
}

// Sort decending by the number the password has been exposed
std::sort(
items.begin(),
items.end(),
[](const std::tuple<const Entry*, const Group*, int>& lhs, const std::tuple<const Entry*, const Group*, int>& rhs) {
return std::get<2>(lhs) > std::get<2>(rhs);
}
);

// Build the table
for(const auto& item : items) {
const auto& entry = std::get<0>(item);
const auto& group = std::get<1>(item);
const auto count = std::get<2>(item);

auto row = QList<QStandardItem*>();
row << new QStandardItem(entry->iconPixmap(), entry->title())
<< new QStandardItem(group->iconPixmap(), group->hierarchy().join("/"))
<< new QStandardItem(
count==1 ? tr("once") :
count <= 10 ? tr("10 times") :
count <= 100 ? tr("100 times") :
count <= 1000 ? tr("1000 times") :
count <= 10*1000 ? tr("10,000 times") :
count <= 100*1000 ? tr("100,000 times") :
count <= 1000*1000 ? tr("a million times") :
tr("millions of times")
);
m_referencesModel->appendRow(row);
row[2]->setForeground(QBrush(QColor("red")));

// Store entry pointer per table row (used in double click handler)
m_rowToEntry.append({group, entry});
}

// If there was an error, append the error message to the table
if (!m_error.isEmpty()) {
auto row = QList<QStandardItem*>();
Expand Down

0 comments on commit e6dc9d5

Please sign in to comment.