Skip to content

Commit

Permalink
Minor rework of statistics panel after code view (keepassxreboot#2034)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolframroesler committed Oct 6, 2019
1 parent 47e2045 commit ea6519c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/gui/dbsettings/DatabaseSettingsPageStatistics.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 KeePassXC Team <[email protected]>
* Copyright (C) 2019 KeePassXC Team <[email protected]>
*
* 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
Expand Down
4 changes: 1 addition & 3 deletions src/gui/dbsettings/DatabaseSettingsPageStatistics.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 KeePassXC Team <[email protected]>
* Copyright (C) 2019 KeePassXC Team <[email protected]>
*
* 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
Expand All @@ -18,8 +18,6 @@
#ifndef KEEPASSXC_DATABASESETTINGSPAGESTATISTICS_H
#define KEEPASSXC_DATABASESETTINGSPAGESTATISTICS_H

#include <QObject>
#include <QPointer>
#include <QWidget>

#include "DatabaseSettingsDialog.h"
Expand Down
117 changes: 58 additions & 59 deletions src/gui/dbsettings/DatabaseSettingsWidgetStatistics.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/*
* Copyright (C) 2019 KeePassXC Team <[email protected]>
*
Expand All @@ -25,9 +24,9 @@
#include "core/Metadata.h"

#include <QFileInfo>
#include <QMap>
#include <QMessageBox>
#include <QStandardItemModel>
#include <map>

namespace
{
Expand All @@ -44,14 +43,14 @@ namespace
int pwdTotalLen = 0; // Total length of all passwords

// Ctor does all the work
Stats(const Database& db)
: modified(QFileInfo(db.filePath()).lastModified())
Stats(QSharedPointer<Database> db)
: modified(QFileInfo(db->filePath()).lastModified())
{
countGroup(*db.rootGroup());
countGroup(*db->rootGroup());
}

// Get average password length
int pwdAvgLen() const
int averagePwdLength() const
{
return pwds_.empty() ? 0 : pwdTotalLen / pwds_.size();
}
Expand All @@ -61,36 +60,36 @@ namespace
int maxPwdReuse() const
{
int ret = 0;
for (const auto& p : pwds_) {
ret = std::max(ret, p.second);
for (const auto& count : pwds_) {
ret = std::max(ret, count);
}
return ret;
}

// A warning sign is displayed if one of the
// following returns true.
bool expiredBad() const
bool isAnyExpired() const
{
return nExpired > 0;
}

bool reuseBad() const
bool areTooManyPwdsReused() const
{
return nPwdsReused > nPwdsUnique / 10;
}

bool maxReuseBad() const
bool arePwdsReusedTooOften() const
{
return maxPwdReuse() > 3;
}

bool avgLenBad() const
bool isAvgPwdTooShort() const
{
return pwdAvgLen() < 10;
return averagePwdLength() < 10;
}

private:
std::map<QString, int> pwds_;
QMap<QString, int> pwds_;

void countGroup(const Group& group)
{
Expand Down Expand Up @@ -138,6 +137,7 @@ namespace
DatabaseSettingsWidgetStatistics::DatabaseSettingsWidgetStatistics(QWidget* parent)
: QWidget(parent)
, m_ui(new Ui::DatabaseSettingsWidgetStatistics())
, m_errIcon(FilePath::instance()->icon("status", "dialog-error"))
{
m_ui->setupUi(this);
}
Expand All @@ -146,56 +146,55 @@ DatabaseSettingsWidgetStatistics::~DatabaseSettingsWidgetStatistics()
{
}

void DatabaseSettingsWidgetStatistics::addStatsRow(QString name, QString value, bool bad, QString badMsg)
{
auto row = QList<QStandardItem*>();
row << new QStandardItem(name);
row << new QStandardItem(value);
m_referencesModel->appendRow(row);

if (bad) {
m_referencesModel->item(m_referencesModel->rowCount() - 1, 1)->setIcon(m_errIcon);
if (!badMsg.isEmpty()) {
m_referencesModel->item(m_referencesModel->rowCount() - 1, 1)->setToolTip(badMsg);
}
}
};

void DatabaseSettingsWidgetStatistics::loadSettings(QSharedPointer<Database> db)
{
m_referencesModel.reset(new QStandardItemModel());
m_referencesModel->setHorizontalHeaderLabels(QStringList() << tr("Name") << tr("Value"));

auto errIcon = FilePath::instance()->icon("status", "dialog-error");

const auto show = [this, &errIcon](QString name, QString value, bool bad = false, QString badMsg = "") {
auto row = QList<QStandardItem*>();
row << new QStandardItem(name);
row << new QStandardItem(value);
m_referencesModel->appendRow(row);

if (bad) {
m_referencesModel->item(m_referencesModel->rowCount() - 1, 1)->setIcon(errIcon);
if (!badMsg.isEmpty()) {
m_referencesModel->item(m_referencesModel->rowCount() - 1, 1)->setToolTip(badMsg);
}
}
};

const auto stats = Stats(*db);
show(tr("Database name"), db->metadata()->name());
show(tr("Description"), db->metadata()->description());
show(tr("Location"), db->filePath());
show(tr("Last saved"), stats.modified.toString(Qt::DefaultLocaleShortDate));
show(tr("Unsaved changes"),
db->isModified() ? tr("yes") : tr("no"),
db->isModified(),
tr("The database was modified, but the changes have not yet been saved to disk."));
show(tr("Number of groups"), QString("%L1").arg(stats.nGroups));
show(tr("Number of entries"), QString("%L1").arg(stats.nEntries));
show(tr("Number of expired entries"),
QString("%L1").arg(stats.nExpired),
stats.expiredBad(),
tr("The database contains entries that have expired."));
show(tr("Unique passwords"), QString("%L1").arg(stats.nPwdsUnique));
show(tr("Non-unique passwords"),
QString("%L1").arg(stats.nPwdsReused),
stats.reuseBad(),
tr("More than 10 % of the passwords are used in more than one entry. If possible, use unique passwords "
"whereever possible."));
show(tr("Maximum password reuse"),
QString("%L1").arg(stats.maxPwdReuse()),
stats.maxReuseBad(),
tr("Some passwords are used more than three times. If possible, don't use a password more than once."));
show(tr("Average password length"),
QString("%L1").arg(stats.pwdAvgLen()),
stats.avgLenBad(),
tr("The average password length is less than ten characters. Longer passwords provide more security."));
const auto stats = Stats(db);
addStatsRow(tr("Database name"), db->metadata()->name());
addStatsRow(tr("Description"), db->metadata()->description());
addStatsRow(tr("Location"), db->filePath());
addStatsRow(tr("Last saved"), stats.modified.toString(Qt::DefaultLocaleShortDate));
addStatsRow(tr("Unsaved changes"),
db->isModified() ? tr("yes") : tr("no"),
db->isModified(),
tr("The database was modified, but the changes have not yet been saved to disk."));
addStatsRow(tr("Number of groups"), QString("%L1").arg(stats.nGroups));
addStatsRow(tr("Number of entries"), QString("%L1").arg(stats.nEntries));
addStatsRow(tr("Number of expired entries"),
QString("%L1").arg(stats.nExpired),
stats.isAnyExpired(),
tr("The database contains entries that have expired."));
addStatsRow(tr("Unique passwords"), QString("%L1").arg(stats.nPwdsUnique));
addStatsRow(tr("Non-unique passwords"),
QString("%L1").arg(stats.nPwdsReused),
stats.areTooManyPwdsReused(),
tr("More than 10 % of the passwords are used in more than one entry. If possible, use unique passwords "
"whereever possible."));
addStatsRow(tr("Maximum password reuse"),
QString("%L1").arg(stats.maxPwdReuse()),
stats.arePwdsReusedTooOften(),
tr("Some passwords are used more than three times. If possible, don't use a password more than once."));
addStatsRow(tr("Average password length"),
QString("%L1").arg(stats.averagePwdLength()),
stats.isAvgPwdTooShort(),
tr("The average password length is less than ten characters. Longer passwords provide more security."));

m_ui->sharedGroupsView->setModel(m_referencesModel.data());
m_ui->sharedGroupsView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
Expand Down
7 changes: 5 additions & 2 deletions src/gui/dbsettings/DatabaseSettingsWidgetStatistics.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 KeePassXC Team <[email protected]>
* Copyright (C) 2019 KeePassXC Team <[email protected]>
*
* 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
Expand All @@ -18,6 +18,7 @@
#ifndef KEEPASSXC_DATABASESETTINGSWIDGETSTATISTICS_H
#define KEEPASSXC_DATABASESETTINGSWIDGETSTATISTICS_H

#include <QIcon>
#include <QPointer>
#include <QScopedPointer>
#include <QWidget>
Expand All @@ -43,8 +44,10 @@ class DatabaseSettingsWidgetStatistics : public QWidget

private:
QScopedPointer<Ui::DatabaseSettingsWidgetStatistics> m_ui;

QIcon m_errIcon;
QScopedPointer<QStandardItemModel> m_referencesModel;

void addStatsRow(QString name, QString value, bool bad = false, QString badMsg = "");
};

#endif // KEEPASSXC_DATABASESETTINGSWIDGETSTATISTICS_H

0 comments on commit ea6519c

Please sign in to comment.