From 6a258adc315f78f7bd2f25db25248865690ce27f Mon Sep 17 00:00:00 2001 From: Juraj Oravec Date: Sat, 28 Jan 2023 20:37:42 +0100 Subject: [PATCH] KWallet: Store passwords entries in map fortmat BUG: 391298 FIXED-IN: 23.04.0 Signed-off-by: Juraj Oravec --- .../kwalletpasswordbackend.cpp | 110 +++++++++++------- 1 file changed, 70 insertions(+), 40 deletions(-) diff --git a/src/plugins/KDEFrameworksIntegration/kwalletpasswordbackend.cpp b/src/plugins/KDEFrameworksIntegration/kwalletpasswordbackend.cpp index c4b4c888b..5981a9d53 100644 --- a/src/plugins/KDEFrameworksIntegration/kwalletpasswordbackend.cpp +++ b/src/plugins/KDEFrameworksIntegration/kwalletpasswordbackend.cpp @@ -34,11 +34,14 @@ static PasswordEntry decodeEntry(const QByteArray &data) return entry; } -static QByteArray encodeEntry(const PasswordEntry &entry) +static QMap encodeEntry(const PasswordEntry &entry) { - QByteArray data; - QDataStream stream(&data, QIODevice::WriteOnly); - stream << entry; + QMap data = { + {"host", entry.host}, + {"username", entry.username}, + {"password", entry.password}, + {"data", QString::fromUtf8(entry.data)} + }; return data; } @@ -93,7 +96,7 @@ void KWalletPasswordBackend::addEntry(const PasswordEntry &entry) stored.id = QString("%1/%2").arg(entry.host, entry.username); stored.updated = QDateTime::currentDateTime().toTime_t(); - m_wallet->writeEntry(stored.id.toString(), encodeEntry(stored)); + m_wallet->writeMap(stored.id.toString(), encodeEntry(stored)); m_allEntries.append(stored); } @@ -107,7 +110,7 @@ bool KWalletPasswordBackend::updateEntry(const PasswordEntry &entry) } m_wallet->removeEntry(entry.id.toString()); - m_wallet->writeEntry(entry.id.toString(), encodeEntry(entry)); + m_wallet->writeMap(entry.id.toString(), encodeEntry(entry)); int index = m_allEntries.indexOf(entry); @@ -131,7 +134,7 @@ void KWalletPasswordBackend::updateLastUsed(PasswordEntry &entry) entry.updated = QDateTime::currentDateTime().toTime_t(); - m_wallet->writeEntry(entry.id.toString(), encodeEntry(entry)); + m_wallet->writeMap(entry.id.toString(), encodeEntry(entry)); int index = m_allEntries.indexOf(entry); @@ -169,14 +172,14 @@ void KWalletPasswordBackend::removeAll() m_allEntries.clear(); - m_wallet->removeFolder("Falkon"); - m_wallet->createFolder("Falkon"); + m_wallet->removeFolder("FalkonPasswords"); + m_wallet->createFolder("FalkonPasswords"); } void KWalletPasswordBackend::showErrorNotification() { static bool initialized; - + if (!initialized) { initialized = true; mApp->desktopNotifications()->showNotification(KDEFrameworksIntegrationPlugin::tr("KWallet disabled"), KDEFrameworksIntegrationPlugin::tr("Please enable KWallet to save password.")); @@ -201,56 +204,83 @@ void KWalletPasswordBackend::initialize() return; } - bool migrate = !m_wallet->hasFolder("Falkon") && m_wallet->hasFolder("QupZilla"); + bool migrationFalkon = !m_wallet->hasFolder("FalkonPasswords") && m_wallet->hasFolder("Falkon"); + bool migrateQupzilla = !m_wallet->hasFolder("FalkonPasswords") && !m_wallet->hasFolder("Falkon") && m_wallet->hasFolder("QupZilla"); + bool migration = false; - if (!m_wallet->hasFolder("Falkon") && !m_wallet->createFolder("Falkon")) { - qWarning() << "KWalletPasswordBackend::initialize Cannot create folder \"Falkon\"!"; + if (!m_wallet->hasFolder("FalkonPasswords") && !m_wallet->createFolder("FalkonPasswords")) { + qWarning() << "KWalletPasswordBackend::initialize Cannot create folder \"FalkonPasswords\"!"; return; } - if (migrate) { + if (migrationFalkon) { + if (!m_wallet->setFolder("Falkon")) { + qWarning() << "KWalletPasswordBackend::initialize Cannot set folder \"Falkon\"!"; + return; + } + migration = true; + } + else if (migrateQupzilla) { if (!m_wallet->setFolder("QupZilla")) { qWarning() << "KWalletPasswordBackend::initialize Cannot set folder \"QupZilla\"!"; return; } - } else { - if (!m_wallet->setFolder("Falkon")) { - qWarning() << "KWalletPasswordBackend::initialize Cannot set folder \"Falkon\"!"; + migration = true; + } + else { + if (!m_wallet->setFolder("FalkonPasswords")) { + qWarning() << "KWalletPasswordBackend::initialize Cannot set folder \"FalkonPasswords\"!"; return; } } - QMap entries; - bool ok = false; -#if KWALLET_VERSION < QT_VERSION_CHECK(5, 72, 0) - ok = m_wallet->readEntryList("*", entries) == 0; -#else - entries = m_wallet->entriesList(&ok); -#endif - if (!ok) { - qWarning() << "KWalletPasswordBackend::initialize Cannot read entries!"; - return; - } - - QMap::const_iterator i = entries.constBegin(); - while (i != entries.constEnd()) { - PasswordEntry entry = decodeEntry(i.value()); - if (entry.isValid()) { - m_allEntries.append(entry); + if (migration) { + QMap entries; + bool ok = false; + entries = m_wallet->entriesList(&ok); + if (!ok) { + qWarning() << "KWalletPasswordBackend::initialize Cannot read entries!"; + return; } - ++i; - } - if (migrate) { - if (!m_wallet->setFolder("Falkon")) { - qWarning() << "KWalletPasswordBackend::initialize Cannot set folder \"Falkon\"!"; + QMap::const_iterator i = entries.constBegin(); + while (i != entries.constEnd()) { + PasswordEntry entry = decodeEntry(i.value()); + if (entry.isValid()) { + m_allEntries.append(entry); + } + ++i; + } + + if (!m_wallet->setFolder("FalkonPasswords")) { + qWarning() << "KWalletPasswordBackend::initialize Cannot set folder \"FalkonPasswords\"!"; return; } for (const PasswordEntry &entry : qAsConst(m_allEntries)) { - m_wallet->writeEntry(entry.id.toString(), encodeEntry(entry)); + m_wallet->writeMap(entry.id.toString(), encodeEntry(entry)); } } + else { + QMap> entriesMap; + bool ok = false; + entriesMap = m_wallet->mapList(&ok); + QMap>::const_iterator j = entriesMap.constBegin(); + while (j != entriesMap.constEnd()) { + PasswordEntry entry; + entry.id = j.key(); + entry.host = j.value()["host"]; + entry.username = j.value()["username"]; + entry.password = j.value()["password"]; + entry.data = j.value()["data"].toUtf8(); + if (entry.isValid()) { + m_allEntries.append(entry); + } + ++j; + } + } + + return; } KWalletPasswordBackend::~KWalletPasswordBackend()