From 10786de3d0230f422b45c59c4f06ecf610a074bf Mon Sep 17 00:00:00 2001 From: varjolintu Date: Sat, 12 Oct 2024 22:56:23 +0300 Subject: [PATCH 1/2] Fix renaming extension key name in Database Settings --- src/browser/BrowserService.cpp | 2 +- src/core/CustomData.cpp | 4 +-- .../DatabaseSettingsWidgetBrowser.cpp | 35 +++++++++++++------ .../DatabaseSettingsWidgetBrowser.h | 4 ++- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/browser/BrowserService.cpp b/src/browser/BrowserService.cpp index d60a55109f..91d85aca28 100644 --- a/src/browser/BrowserService.cpp +++ b/src/browser/BrowserService.cpp @@ -611,7 +611,7 @@ QString BrowserService::storeKey(const QString& key) hideWindow(); db->metadata()->customData()->set(CustomData::BrowserKeyPrefix + id, key); - db->metadata()->customData()->set(QString("%1_%2").arg(CustomData::Created, id), + db->metadata()->customData()->set(QString("%1%2").arg(CustomData::Created, id), QLocale::system().toString(Clock::currentDateTime(), QLocale::ShortFormat)); return id; } diff --git a/src/core/CustomData.cpp b/src/core/CustomData.cpp index 1772cd62bc..bbd1d3b5e2 100644 --- a/src/core/CustomData.cpp +++ b/src/core/CustomData.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 KeePassXC Team + * Copyright (C) 2024 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 @@ -21,7 +21,7 @@ #include "core/Global.h" 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::BrowserLegacyKeyPrefix = QStringLiteral("Public Key: "); const QString CustomData::ExcludeFromReportsLegacy = QStringLiteral("KnownBad"); diff --git a/src/gui/dbsettings/DatabaseSettingsWidgetBrowser.cpp b/src/gui/dbsettings/DatabaseSettingsWidgetBrowser.cpp index 7658475b3d..524d2a6401 100644 --- a/src/gui/dbsettings/DatabaseSettingsWidgetBrowser.cpp +++ b/src/gui/dbsettings/DatabaseSettingsWidgetBrowser.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 KeePassXC Team + * Copyright (C) 2024 KeePassXC Team * Copyright (C) 2018 Sami Vänttinen * * This program is free software: you can redistribute it and/or modify @@ -124,7 +124,7 @@ void DatabaseSettingsWidgetBrowser::updateModel() if (key.startsWith(CustomData::BrowserKeyPrefix)) { QString strippedKey = key; strippedKey.remove(CustomData::BrowserKeyPrefix); - auto created = customData()->value(QString("%1_%2").arg(CustomData::Created, strippedKey)); + auto created = customData()->value(getKeyWithPrefix(CustomData::Created, strippedKey)); auto createdItem = new QStandardItem(created); createdItem->setEditable(false); m_customDataModel->appendRow(QList() @@ -267,18 +267,16 @@ void DatabaseSettingsWidgetBrowser::editFinished(QStandardItem* item) if (itemSelectionModel) { auto indexList = itemSelectionModel->selectedRows(item->column()); - if (indexList.length() > 0) { - QString newValue = item->index().data().toString(); + if (!indexList.isEmpty()) { + auto newValue = item->index().data().toString(); // The key is edited if (item->column() == 0) { - // Get the old key/value pair, remove it and replace it - m_valueInEdit.insert(0, CustomData::BrowserKeyPrefix); - auto tempValue = customData()->value(m_valueInEdit); - newValue.insert(0, CustomData::BrowserKeyPrefix); + // Update created timestamp with the new key + replaceKey(CustomData::Created, m_valueInEdit, newValue); - m_db->metadata()->customData()->remove(m_valueInEdit); - m_db->metadata()->customData()->set(newValue, tempValue); + // Get the old key/value pair, remove it and replace it + replaceKey(CustomData::BrowserKeyPrefix, m_valueInEdit, newValue); } else { // Replace just the value for (const QString& key : m_db->metadata()->customData()->keys()) { @@ -301,3 +299,20 @@ void DatabaseSettingsWidgetBrowser::updateSharedKeyList() { updateModel(); } + +// Replaces a key and the created timestamp for it +void DatabaseSettingsWidgetBrowser::replaceKey(const QString& prefix, + const QString& oldName, + const QString& newName) const +{ + const auto oldKey = getKeyWithPrefix(prefix, oldName); + const auto newKey = getKeyWithPrefix(prefix, newName); + const auto tempValue = customData()->value(oldKey); + m_db->metadata()->customData()->remove(oldKey); + m_db->metadata()->customData()->set(newKey, tempValue); +} + +QString DatabaseSettingsWidgetBrowser::getKeyWithPrefix(const QString& prefix, const QString& key) const +{ + return QString("%1%2").arg(prefix, key); +} diff --git a/src/gui/dbsettings/DatabaseSettingsWidgetBrowser.h b/src/gui/dbsettings/DatabaseSettingsWidgetBrowser.h index e032e414f7..1f20bf46b4 100644 --- a/src/gui/dbsettings/DatabaseSettingsWidgetBrowser.h +++ b/src/gui/dbsettings/DatabaseSettingsWidgetBrowser.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023 KeePassXC Team + * Copyright (C) 2024 KeePassXC Team * Copyright (C) 2018 Sami Vänttinen * * This program is free software: you can redistribute it and/or modify @@ -62,6 +62,8 @@ private slots: private: void updateModel(); void settingsWarning(); + void replaceKey(const QString& prefix, const QString& oldName, const QString& newName) const; + QString getKeyWithPrefix(const QString& prefix, const QString& key) const; protected: void showEvent(QShowEvent* event) override; From 84a0e0e8c5d3cf9bd74659c4ac91d17ef17cf024 Mon Sep 17 00:00:00 2001 From: varjolintu Date: Sun, 13 Oct 2024 09:27:19 +0300 Subject: [PATCH 2/2] Add CustomData::getKeyWithPrefix() --- src/browser/BrowserService.cpp | 12 +++++++----- src/core/CustomData.cpp | 10 +++++++++- src/core/CustomData.h | 5 +++-- .../dbsettings/DatabaseSettingsWidgetBrowser.cpp | 16 ++++++---------- .../dbsettings/DatabaseSettingsWidgetBrowser.h | 1 - 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/browser/BrowserService.cpp b/src/browser/BrowserService.cpp index 91d85aca28..11bc444b72 100644 --- a/src/browser/BrowserService.cpp +++ b/src/browser/BrowserService.cpp @@ -597,7 +597,8 @@ QString BrowserService::storeKey(const QString& key) return {}; } - contains = db->metadata()->customData()->contains(CustomData::BrowserKeyPrefix + id); + contains = + db->metadata()->customData()->contains(CustomData::getKeyWithPrefix(CustomData::BrowserKeyPrefix, id)); if (contains) { dialogResult = MessageBox::warning(m_currentDatabaseWidget, tr("KeePassXC - Overwrite existing key?"), @@ -610,8 +611,8 @@ QString BrowserService::storeKey(const QString& key) } while (contains && dialogResult == MessageBox::Cancel); hideWindow(); - db->metadata()->customData()->set(CustomData::BrowserKeyPrefix + id, key); - db->metadata()->customData()->set(QString("%1%2").arg(CustomData::Created, id), + db->metadata()->customData()->set(CustomData::getKeyWithPrefix(CustomData::BrowserKeyPrefix, id), key); + db->metadata()->customData()->set(CustomData::getKeyWithPrefix(CustomData::Created, id), QLocale::system().toString(Clock::currentDateTime(), QLocale::ShortFormat)); return id; } @@ -623,7 +624,7 @@ QString BrowserService::getKey(const QString& id) return {}; } - return db->metadata()->customData()->value(CustomData::BrowserKeyPrefix + id); + return db->metadata()->customData()->value(CustomData::getKeyWithPrefix(CustomData::BrowserKeyPrefix, id)); } #ifdef WITH_XC_BROWSER_PASSKEYS @@ -1065,7 +1066,8 @@ QList BrowserService::searchEntries(const QString& siteUrl, // Check if database is connected with KeePassXC-Browser. If so, return browser key (otherwise empty) auto databaseConnected = [&](const QSharedPointer& db) { 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) { return keyPair.first; } diff --git a/src/core/CustomData.cpp b/src/core/CustomData.cpp index bbd1d3b5e2..3c734e65f1 100644 --- a/src/core/CustomData.cpp +++ b/src/core/CustomData.cpp @@ -23,7 +23,6 @@ const QString CustomData::LastModified = QStringLiteral("_LAST_MODIFIED"); const QString CustomData::Created = QStringLiteral("_CREATED_"); const QString CustomData::BrowserKeyPrefix = QStringLiteral("KPXC_BROWSER_"); -const QString CustomData::BrowserLegacyKeyPrefix = QStringLiteral("Public Key: "); const QString CustomData::ExcludeFromReportsLegacy = QStringLiteral("KnownBad"); const QString CustomData::FdoSecretsExposedGroup = QStringLiteral("FDO_SECRETS_EXPOSED_GROUP"); 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; } +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 { auto item = m_data.find(key); diff --git a/src/core/CustomData.h b/src/core/CustomData.h index 49e8a33ee3..3ee4d05efe 100644 --- a/src/core/CustomData.h +++ b/src/core/CustomData.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 KeePassXC Team + * Copyright (C) 2024 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 @@ -64,11 +64,12 @@ class CustomData : public ModifiableObject bool operator==(const CustomData& other) const; bool operator!=(const CustomData& other) const; + static QString getKeyWithPrefix(const QString& prefix, const QString& key); + // Pre-defined keys static const QString LastModified; static const QString Created; static const QString BrowserKeyPrefix; - static const QString BrowserLegacyKeyPrefix; static const QString FdoSecretsExposedGroup; static const QString RandomSlug; static const QString RemoteProgramSettings; diff --git a/src/gui/dbsettings/DatabaseSettingsWidgetBrowser.cpp b/src/gui/dbsettings/DatabaseSettingsWidgetBrowser.cpp index 524d2a6401..fa52c8b8fb 100644 --- a/src/gui/dbsettings/DatabaseSettingsWidgetBrowser.cpp +++ b/src/gui/dbsettings/DatabaseSettingsWidgetBrowser.cpp @@ -102,9 +102,10 @@ void DatabaseSettingsWidgetBrowser::removeSelectedKey() const QItemSelectionModel* itemSelectionModel = m_ui->customDataTable->selectionModel(); if (itemSelectionModel) { for (const QModelIndex& index : itemSelectionModel->selectedRows(0)) { - QString key = index.data().toString(); - key.insert(0, CustomData::BrowserKeyPrefix); + const auto key = CustomData::getKeyWithPrefix(CustomData::BrowserKeyPrefix, index.data().toString()); + const auto createdKey = CustomData::getKeyWithPrefix(CustomData::Created, index.data().toString()); customData()->remove(key); + customData()->remove(createdKey); } updateModel(); } @@ -124,7 +125,7 @@ void DatabaseSettingsWidgetBrowser::updateModel() if (key.startsWith(CustomData::BrowserKeyPrefix)) { QString strippedKey = key; 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); createdItem->setEditable(false); m_customDataModel->appendRow(QList() @@ -305,14 +306,9 @@ void DatabaseSettingsWidgetBrowser::replaceKey(const QString& prefix, const QString& oldName, const QString& newName) const { - const auto oldKey = getKeyWithPrefix(prefix, oldName); - const auto newKey = getKeyWithPrefix(prefix, newName); + const auto oldKey = CustomData::getKeyWithPrefix(prefix, oldName); + const auto newKey = CustomData::getKeyWithPrefix(prefix, newName); const auto tempValue = customData()->value(oldKey); m_db->metadata()->customData()->remove(oldKey); m_db->metadata()->customData()->set(newKey, tempValue); } - -QString DatabaseSettingsWidgetBrowser::getKeyWithPrefix(const QString& prefix, const QString& key) const -{ - return QString("%1%2").arg(prefix, key); -} diff --git a/src/gui/dbsettings/DatabaseSettingsWidgetBrowser.h b/src/gui/dbsettings/DatabaseSettingsWidgetBrowser.h index 1f20bf46b4..25664a71fd 100644 --- a/src/gui/dbsettings/DatabaseSettingsWidgetBrowser.h +++ b/src/gui/dbsettings/DatabaseSettingsWidgetBrowser.h @@ -63,7 +63,6 @@ private slots: void updateModel(); void settingsWarning(); void replaceKey(const QString& prefix, const QString& oldName, const QString& newName) const; - QString getKeyWithPrefix(const QString& prefix, const QString& key) const; protected: void showEvent(QShowEvent* event) override;