diff --git a/src/gui/dbsettings/DatabaseSettingsPageStatistics.cpp b/src/gui/dbsettings/DatabaseSettingsPageStatistics.cpp index 6fdb7c5a93..6fe24ff0f3 100644 --- a/src/gui/dbsettings/DatabaseSettingsPageStatistics.cpp +++ b/src/gui/dbsettings/DatabaseSettingsPageStatistics.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 KeePassXC Team + * Copyright (C) 2019 KeePassXC Team * * 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 diff --git a/src/gui/dbsettings/DatabaseSettingsPageStatistics.h b/src/gui/dbsettings/DatabaseSettingsPageStatistics.h index feb70308a9..c890f3b81c 100644 --- a/src/gui/dbsettings/DatabaseSettingsPageStatistics.h +++ b/src/gui/dbsettings/DatabaseSettingsPageStatistics.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 KeePassXC Team + * Copyright (C) 2019 KeePassXC Team * * 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 @@ -18,8 +18,6 @@ #ifndef KEEPASSXC_DATABASESETTINGSPAGESTATISTICS_H #define KEEPASSXC_DATABASESETTINGSPAGESTATISTICS_H -#include -#include #include #include "DatabaseSettingsDialog.h" diff --git a/src/gui/dbsettings/DatabaseSettingsWidgetStatistics.cpp b/src/gui/dbsettings/DatabaseSettingsWidgetStatistics.cpp index 93351bf19d..d83363390e 100644 --- a/src/gui/dbsettings/DatabaseSettingsWidgetStatistics.cpp +++ b/src/gui/dbsettings/DatabaseSettingsWidgetStatistics.cpp @@ -1,4 +1,3 @@ - /* * Copyright (C) 2019 KeePassXC Team * @@ -25,9 +24,9 @@ #include "core/Metadata.h" #include +#include #include #include -#include namespace { @@ -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 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(); } @@ -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 pwds_; + QMap pwds_; void countGroup(const Group& group) { @@ -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); } @@ -146,56 +146,55 @@ DatabaseSettingsWidgetStatistics::~DatabaseSettingsWidgetStatistics() { } +void DatabaseSettingsWidgetStatistics::addStatsRow(QString name, QString value, bool bad, QString badMsg) +{ + auto row = QList(); + 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 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(); - 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); diff --git a/src/gui/dbsettings/DatabaseSettingsWidgetStatistics.h b/src/gui/dbsettings/DatabaseSettingsWidgetStatistics.h index f359afc178..9c34d7417f 100644 --- a/src/gui/dbsettings/DatabaseSettingsWidgetStatistics.h +++ b/src/gui/dbsettings/DatabaseSettingsWidgetStatistics.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 KeePassXC Team + * Copyright (C) 2019 KeePassXC Team * * 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 @@ -18,6 +18,7 @@ #ifndef KEEPASSXC_DATABASESETTINGSWIDGETSTATISTICS_H #define KEEPASSXC_DATABASESETTINGSWIDGETSTATISTICS_H +#include #include #include #include @@ -43,8 +44,10 @@ class DatabaseSettingsWidgetStatistics : public QWidget private: QScopedPointer m_ui; - + QIcon m_errIcon; QScopedPointer m_referencesModel; + + void addStatsRow(QString name, QString value, bool bad = false, QString badMsg = ""); }; #endif // KEEPASSXC_DATABASESETTINGSWIDGETSTATISTICS_H