From bf2081d1a3f4877dcc3db30f2f15ee2291a0e286 Mon Sep 17 00:00:00 2001 From: Danny Wensley Date: Mon, 27 May 2024 19:37:52 +0100 Subject: [PATCH] Add user-configurable summary to unlock screen. --- src/core/Database.cpp | 30 +++++++++++ src/core/Database.h | 5 ++ src/gui/DatabaseOpenWidget.cpp | 24 +++++++++ src/gui/DatabaseOpenWidget.ui | 18 ++++++- .../DatabaseSettingsWidgetGeneral.cpp | 6 +++ .../DatabaseSettingsWidgetGeneral.ui | 53 ++++++++++++++++++- src/gui/styles/base/basestyle.qss | 2 +- src/gui/styles/base/classicstyle.qss | 2 +- 8 files changed, 136 insertions(+), 4 deletions(-) diff --git a/src/core/Database.cpp b/src/core/Database.cpp index 14ddd87d92..03baa73015 100644 --- a/src/core/Database.cpp +++ b/src/core/Database.cpp @@ -1049,3 +1049,33 @@ QUuid Database::publicUuid() return QUuid::fromRfc4122(publicCustomData()["KPXC_PUBLIC_UUID"].toByteArray()); } + +QString Database::publicSummary() +{ + if (!publicCustomData().contains("KPXC_PUBLIC_SUMMARY")) { + return QString(); + } + + return publicCustomData()["KPXC_PUBLIC_SUMMARY"].toString(); +} + +void Database::setPublicSummary(const QString& newSummary) +{ + publicCustomData().insert("KPXC_PUBLIC_SUMMARY", newSummary); + markAsModified(); +} + +QString Database::publicColor() +{ + if (!publicCustomData().contains("KPXC_PUBLIC_COLOR")) { + return QString(); + } + + return publicCustomData()["KPXC_PUBLIC_COLOR"].toString(); +} + +void Database::setPublicColor(const QString& newSummary) +{ + publicCustomData().insert("KPXC_PUBLIC_COLOR", newSummary); + markAsModified(); +} diff --git a/src/core/Database.h b/src/core/Database.h index decceeecf5..94dfbd1aee 100644 --- a/src/core/Database.h +++ b/src/core/Database.h @@ -108,6 +108,11 @@ class Database : public ModifiableObject QString canonicalFilePath() const; void setFilePath(const QString& filePath); + QString publicSummary(); + void setPublicSummary(const QString& newSummary); + QString publicColor(); + void setPublicColor(const QString& newSummary); + Metadata* metadata(); const Metadata* metadata() const; Group* rootGroup(); diff --git a/src/gui/DatabaseOpenWidget.cpp b/src/gui/DatabaseOpenWidget.cpp index e27bbd93d1..7438b317a3 100644 --- a/src/gui/DatabaseOpenWidget.cpp +++ b/src/gui/DatabaseOpenWidget.cpp @@ -74,6 +74,8 @@ DatabaseOpenWidget::DatabaseOpenWidget(QWidget* parent) m_ui->labelHeadline->setFont(font); m_ui->labelHeadline->setText(tr("Unlock KeePassXC Database")); + m_ui->publicSummaryLabel->setVisible(false); + m_ui->quickUnlockButton->setFont(font); m_ui->quickUnlockButton->setIcon( icons()->icon("fingerprint", true, palette().color(QPalette::Active, QPalette::HighlightedText))); @@ -226,6 +228,28 @@ void DatabaseOpenWidget::load(const QString& filename) m_ui->fileNameLabel->setRawText(m_filename); + if (!m_db->publicSummary().isEmpty()) { + m_ui->publicSummaryLabel->setVisible(true); + m_ui->publicSummaryLabel->setText(m_db->publicSummary()); + } + + m_ui->publicSummaryLabel->setStyleSheet(""); + + if (!m_db->publicColor().isEmpty()) { + QColor userColor = QColor(m_db->publicColor()); + + if (userColor.isValid()) { + float luminance = (0.299 * userColor.redF() + 0.587 * userColor.greenF() + 0.114 * userColor.blueF()); + + QColor textColor = Qt::white; + if (luminance > 0.5) { + textColor = Qt::black; + } + + m_ui->publicSummaryLabel->setStyleSheet(QString("background-color:%1;color:%2;border-color:%1;").arg(userColor.name(), textColor.name())); + } + } + if (config()->get(Config::RememberLastKeyFiles).toBool()) { auto lastKeyFiles = config()->get(Config::LastKeyFiles).toHash(); if (lastKeyFiles.contains(m_filename)) { diff --git a/src/gui/DatabaseOpenWidget.ui b/src/gui/DatabaseOpenWidget.ui index 104ca19596..0b9850bbfd 100644 --- a/src/gui/DatabaseOpenWidget.ui +++ b/src/gui/DatabaseOpenWidget.ui @@ -40,7 +40,7 @@ - + @@ -90,6 +90,22 @@ + + + + QFrame::StyledPanel + + + publicSummary + + + Qt::AlignCenter + + + 10 + + + diff --git a/src/gui/dbsettings/DatabaseSettingsWidgetGeneral.cpp b/src/gui/dbsettings/DatabaseSettingsWidgetGeneral.cpp index 777c464e44..18be90acea 100644 --- a/src/gui/dbsettings/DatabaseSettingsWidgetGeneral.cpp +++ b/src/gui/dbsettings/DatabaseSettingsWidgetGeneral.cpp @@ -46,6 +46,9 @@ void DatabaseSettingsWidgetGeneral::initialize() m_ui->defaultUsernameEdit->setText(meta->defaultUserName()); m_ui->compressionCheckbox->setChecked(m_db->compressionAlgorithm() != Database::CompressionNone); + m_ui->dbPublicSummary->setText(m_db->publicSummary()); + m_ui->dbPublicColor->setText(m_db->publicColor()); + if (meta->historyMaxItems() > -1) { m_ui->historyMaxItemsSpinBox->setValue(meta->historyMaxItems()); m_ui->historyMaxItemsCheckBox->setChecked(true); @@ -116,6 +119,9 @@ bool DatabaseSettingsWidgetGeneral::save() meta->setRecycleBinEnabled(m_ui->recycleBinEnabledCheckBox->isChecked()); meta->setSettingsChanged(Clock::currentDateTimeUtc()); + m_db->setPublicSummary(m_ui->dbPublicSummary->text()); + m_db->setPublicColor(m_ui->dbPublicColor->text()); + bool truncate = false; int historyMaxItems; diff --git a/src/gui/dbsettings/DatabaseSettingsWidgetGeneral.ui b/src/gui/dbsettings/DatabaseSettingsWidgetGeneral.ui index 939b699631..7440f6047a 100644 --- a/src/gui/dbsettings/DatabaseSettingsWidgetGeneral.ui +++ b/src/gui/dbsettings/DatabaseSettingsWidgetGeneral.ui @@ -7,7 +7,7 @@ 0 0 453 - 394 + 598 @@ -92,6 +92,57 @@ + + + + Public Database Metadata + + + + + + Warning: the following settings are stored unencrypted. + + + + + + + 0 + + + + + Database summary: + + + + + + + Database summary field + + + + + + + Database color: + + + + + + + Database color field + + + + + + + + diff --git a/src/gui/styles/base/basestyle.qss b/src/gui/styles/base/basestyle.qss index ee0fa4e021..8d40281a38 100644 --- a/src/gui/styles/base/basestyle.qss +++ b/src/gui/styles/base/basestyle.qss @@ -38,7 +38,7 @@ EntryPreviewWidget TagsEdit padding-left: 0px; } -DatabaseOpenWidget #centralStack { +DatabaseOpenWidget #centralStack, DatabaseOpenWidget #publicSummaryLabel { border: 1px solid palette(mid); background: palette(light); } diff --git a/src/gui/styles/base/classicstyle.qss b/src/gui/styles/base/classicstyle.qss index d0ab2b88fc..72308f39e2 100644 --- a/src/gui/styles/base/classicstyle.qss +++ b/src/gui/styles/base/classicstyle.qss @@ -1,4 +1,4 @@ -DatabaseOpenWidget #centralStack { +DatabaseOpenWidget #centralStack, DatabaseOpenWidget #publicSummaryLabel { border: 2px groove palette(mid); background: palette(light); }