Add CustomData::getKeyWithPrefix()

This commit is contained in:
varjolintu 2024-10-13 09:27:19 +03:00 committed by Jonathan White
parent 132ca42ec5
commit 9670a5e74e
5 changed files with 25 additions and 19 deletions

View file

@ -602,7 +602,8 @@ QString BrowserService::storeKey(const QString& key)
return {}; return {};
} }
contains = db->metadata()->customData()->contains(CustomData::BrowserKeyPrefix + id); contains =
db->metadata()->customData()->contains(CustomData::getKeyWithPrefix(CustomData::BrowserKeyPrefix, id));
if (contains) { if (contains) {
dialogResult = MessageBox::warning(m_currentDatabaseWidget, dialogResult = MessageBox::warning(m_currentDatabaseWidget,
tr("KeePassXC - Overwrite existing key?"), tr("KeePassXC - Overwrite existing key?"),
@ -615,8 +616,8 @@ QString BrowserService::storeKey(const QString& key)
} while (contains && dialogResult == MessageBox::Cancel); } while (contains && dialogResult == MessageBox::Cancel);
hideWindow(); hideWindow();
db->metadata()->customData()->set(CustomData::BrowserKeyPrefix + id, key); db->metadata()->customData()->set(CustomData::getKeyWithPrefix(CustomData::BrowserKeyPrefix, id), key);
db->metadata()->customData()->set(QString("%1%2").arg(CustomData::Created, id), db->metadata()->customData()->set(CustomData::getKeyWithPrefix(CustomData::Created, id),
QLocale::system().toString(Clock::currentDateTime(), QLocale::ShortFormat)); QLocale::system().toString(Clock::currentDateTime(), QLocale::ShortFormat));
return id; return id;
} }
@ -628,7 +629,7 @@ QString BrowserService::getKey(const QString& id)
return {}; return {};
} }
return db->metadata()->customData()->value(CustomData::BrowserKeyPrefix + id); return db->metadata()->customData()->value(CustomData::getKeyWithPrefix(CustomData::BrowserKeyPrefix, id));
} }
#ifdef WITH_XC_BROWSER_PASSKEYS #ifdef WITH_XC_BROWSER_PASSKEYS
@ -1070,7 +1071,8 @@ QList<Entry*> BrowserService::searchEntries(const QString& siteUrl,
// Check if database is connected with KeePassXC-Browser. If so, return browser key (otherwise empty) // Check if database is connected with KeePassXC-Browser. If so, return browser key (otherwise empty)
auto databaseConnected = [&](const QSharedPointer<Database>& db) { auto databaseConnected = [&](const QSharedPointer<Database>& db) {
for (const StringPair& keyPair : keyList) { for (const StringPair& keyPair : keyList) {
QString key = db->metadata()->customData()->value(CustomData::BrowserKeyPrefix + keyPair.first); const auto key = db->metadata()->customData()->value(
CustomData::getKeyWithPrefix(CustomData::BrowserKeyPrefix, keyPair.first));
if (!key.isEmpty() && keyPair.second == key) { if (!key.isEmpty() && keyPair.second == key) {
return keyPair.first; return keyPair.first;
} }

View file

@ -23,7 +23,6 @@
const QString CustomData::LastModified = QStringLiteral("_LAST_MODIFIED"); const QString CustomData::LastModified = QStringLiteral("_LAST_MODIFIED");
const QString CustomData::Created = QStringLiteral("_CREATED_"); const QString CustomData::Created = QStringLiteral("_CREATED_");
const QString CustomData::BrowserKeyPrefix = QStringLiteral("KPXC_BROWSER_"); const QString CustomData::BrowserKeyPrefix = QStringLiteral("KPXC_BROWSER_");
const QString CustomData::BrowserLegacyKeyPrefix = QStringLiteral("Public Key: ");
const QString CustomData::ExcludeFromReportsLegacy = QStringLiteral("KnownBad"); const QString CustomData::ExcludeFromReportsLegacy = QStringLiteral("KnownBad");
const QString CustomData::FdoSecretsExposedGroup = QStringLiteral("FDO_SECRETS_EXPOSED_GROUP"); const QString CustomData::FdoSecretsExposedGroup = QStringLiteral("FDO_SECRETS_EXPOSED_GROUP");
const QString CustomData::RandomSlug = QStringLiteral("KPXC_RANDOM_SLUG"); const QString CustomData::RandomSlug = QStringLiteral("KPXC_RANDOM_SLUG");
@ -52,6 +51,15 @@ QString CustomData::value(const QString& key) const
return m_data.value(key).value; return m_data.value(key).value;
} }
QString CustomData::getKeyWithPrefix(const QString& prefix, const QString& key)
{
QString keyWithPrefix;
keyWithPrefix.reserve(prefix.length() + key.length());
keyWithPrefix.append(prefix);
keyWithPrefix.append(key);
return keyWithPrefix;
}
const CustomData::CustomDataItem& CustomData::item(const QString& key) const const CustomData::CustomDataItem& CustomData::item(const QString& key) const
{ {
auto item = m_data.find(key); auto item = m_data.find(key);

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2018 KeePassXC Team <team@keepassxc.org> * Copyright (C) 2024 KeePassXC Team <team@keepassxc.org>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -64,11 +64,12 @@ public:
bool operator==(const CustomData& other) const; bool operator==(const CustomData& other) const;
bool operator!=(const CustomData& other) const; bool operator!=(const CustomData& other) const;
static QString getKeyWithPrefix(const QString& prefix, const QString& key);
// Pre-defined keys // Pre-defined keys
static const QString LastModified; static const QString LastModified;
static const QString Created; static const QString Created;
static const QString BrowserKeyPrefix; static const QString BrowserKeyPrefix;
static const QString BrowserLegacyKeyPrefix;
static const QString FdoSecretsExposedGroup; static const QString FdoSecretsExposedGroup;
static const QString RandomSlug; static const QString RandomSlug;
static const QString RemoteProgramSettings; static const QString RemoteProgramSettings;

View file

@ -102,9 +102,10 @@ void DatabaseSettingsWidgetBrowser::removeSelectedKey()
const QItemSelectionModel* itemSelectionModel = m_ui->customDataTable->selectionModel(); const QItemSelectionModel* itemSelectionModel = m_ui->customDataTable->selectionModel();
if (itemSelectionModel) { if (itemSelectionModel) {
for (const QModelIndex& index : itemSelectionModel->selectedRows(0)) { for (const QModelIndex& index : itemSelectionModel->selectedRows(0)) {
QString key = index.data().toString(); const auto key = CustomData::getKeyWithPrefix(CustomData::BrowserKeyPrefix, index.data().toString());
key.insert(0, CustomData::BrowserKeyPrefix); const auto createdKey = CustomData::getKeyWithPrefix(CustomData::Created, index.data().toString());
customData()->remove(key); customData()->remove(key);
customData()->remove(createdKey);
} }
updateModel(); updateModel();
} }
@ -124,7 +125,7 @@ void DatabaseSettingsWidgetBrowser::updateModel()
if (key.startsWith(CustomData::BrowserKeyPrefix)) { if (key.startsWith(CustomData::BrowserKeyPrefix)) {
QString strippedKey = key; QString strippedKey = key;
strippedKey.remove(CustomData::BrowserKeyPrefix); strippedKey.remove(CustomData::BrowserKeyPrefix);
auto created = customData()->value(getKeyWithPrefix(CustomData::Created, strippedKey)); auto created = customData()->value(CustomData::getKeyWithPrefix(CustomData::Created, strippedKey));
auto createdItem = new QStandardItem(created); auto createdItem = new QStandardItem(created);
createdItem->setEditable(false); createdItem->setEditable(false);
m_customDataModel->appendRow(QList<QStandardItem*>() m_customDataModel->appendRow(QList<QStandardItem*>()
@ -305,14 +306,9 @@ void DatabaseSettingsWidgetBrowser::replaceKey(const QString& prefix,
const QString& oldName, const QString& oldName,
const QString& newName) const const QString& newName) const
{ {
const auto oldKey = getKeyWithPrefix(prefix, oldName); const auto oldKey = CustomData::getKeyWithPrefix(prefix, oldName);
const auto newKey = getKeyWithPrefix(prefix, newName); const auto newKey = CustomData::getKeyWithPrefix(prefix, newName);
const auto tempValue = customData()->value(oldKey); const auto tempValue = customData()->value(oldKey);
m_db->metadata()->customData()->remove(oldKey); m_db->metadata()->customData()->remove(oldKey);
m_db->metadata()->customData()->set(newKey, tempValue); m_db->metadata()->customData()->set(newKey, tempValue);
} }
QString DatabaseSettingsWidgetBrowser::getKeyWithPrefix(const QString& prefix, const QString& key) const
{
return QString("%1%2").arg(prefix, key);
}

View file

@ -63,7 +63,6 @@ private:
void updateModel(); void updateModel();
void settingsWarning(); void settingsWarning();
void replaceKey(const QString& prefix, const QString& oldName, const QString& newName) const; void replaceKey(const QString& prefix, const QString& oldName, const QString& newName) const;
QString getKeyWithPrefix(const QString& prefix, const QString& key) const;
protected: protected:
void showEvent(QShowEvent* event) override; void showEvent(QShowEvent* event) override;