some refactoring, some improvements

This commit is contained in:
Blue 2023-11-10 19:26:16 -03:00
parent be466fbad1
commit e31ef78e71
Signed by untrusted user: blue
GPG key ID: 9B203B252A63EE38
13 changed files with 188 additions and 147 deletions

View file

@ -28,9 +28,8 @@ Core::TrustHandler::TrustHandler(Account* account):
ownKeys(db.addCache<QString, QByteArray>("ownKeys")),
keysByProtocol()
{
if (!protocols.open(QIODevice::ReadWrite | QIODevice::Text)) { //never supposed to happen since I have just created a directory;
if (!protocols.open(QIODevice::ReadWrite | QIODevice::Text)) //never supposed to happen since I have just created a directory;
throw LMDBAL::Directory(protocols.fileName().toStdString());
}
QTextStream in(&protocols);
while(!in.atEnd()) {
@ -53,11 +52,10 @@ Core::TrustHandler::~TrustHandler() {
Core::TrustHandler::KeyCache * Core::TrustHandler::getCache(const QString& encryption) {
std::map<QString, KeyCache*>::iterator itr = keysByProtocol.find(encryption);
if (itr == keysByProtocol.end()) {
if (itr == keysByProtocol.end())
return createNewCache(encryption);
} else {
else
return itr->second;
}
}
Core::TrustHandler::KeyCache * Core::TrustHandler::createNewCache(const QString& encryption) {
@ -65,9 +63,9 @@ Core::TrustHandler::KeyCache * Core::TrustHandler::createNewCache(const QString&
KeyCache* cache = db.addCache<QString, Keys>(encryption.toStdString());
keysByProtocol.insert(std::make_pair(encryption, cache));
if(!protocols.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
if (!protocols.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
throw LMDBAL::Directory(protocols.fileName().toStdString());
}
QTextStream out(&protocols);
out << encryption + "\n";
protocols.close();
@ -83,10 +81,12 @@ QXmppTask<void> Core::TrustHandler::resetAll(const QString& encryption) {
if (itr == keysByProtocol.end())
return Core::makeReadyTask();
LMDBAL::WriteTransaction txn = db.beginTransaction();
KeyCache* cache = itr->second;
std::map<QString, Keys> keys = cache->readAll();
cache->drop();
std::map<QString, Keys> keys = cache->readAll(txn);
cache->drop(txn);
txn.commit();
for (const std::pair<const QString, Keys>& pair : keys) {
bool empty = true;
for (const std::pair<const QByteArray, Shared::TrustLevel>& trust : pair.second) {
@ -105,8 +105,8 @@ QXmppTask<void> Core::TrustHandler::resetAll(const QString& encryption) {
QXmppTask<QXmpp::TrustLevel> Core::TrustHandler::trustLevel(
const QString& encryption,
const QString& keyOwnerJid,
const QByteArray& keyId)
{
const QByteArray& keyId
) {
KeyCache* cache = getCache(encryption);
Shared::TrustLevel level = Shared::TrustLevel::undecided;
try {
@ -122,15 +122,17 @@ QXmppTask<QHash<QString, QMultiHash<QString, QByteArray>>> Core::TrustHandler::s
const QString& encryption,
const QList<QString>& keyOwnerJids,
QXmpp::TrustLevel oldTrustLevel,
QXmpp::TrustLevel newTrustLevel)
{
QXmpp::TrustLevel newTrustLevel
) {
QHash<QString, QMultiHash<QString, QByteArray>> modifiedKeys;
Shared::TrustLevel oldLevel = convert(oldTrustLevel);
Shared::TrustLevel newLevel = convert(newTrustLevel);
std::set<QString> modifiedJids;
KeyCache* cache = getCache(encryption);
LMDBAL::WriteTransaction txn = db.beginTransaction();
for (const QString& keyOwnerJid : keyOwnerJids) {
Keys map = cache->getRecord(keyOwnerJid);
Keys map = cache->getRecord(keyOwnerJid, txn);
uint count = 0;
for (std::pair<const QByteArray, Shared::TrustLevel>& pair : map) {
Shared::TrustLevel& current = pair.second;
@ -142,8 +144,9 @@ QXmppTask<QHash<QString, QMultiHash<QString, QByteArray>>> Core::TrustHandler::s
}
}
if (count > 0)
cache->changeRecord(keyOwnerJid, map);
cache->changeRecord(keyOwnerJid, map, txn);
}
txn.commit();
for (const QString& jid : modifiedJids)
emit trustLevelsChanged(jid, getSummary(jid));
@ -153,18 +156,19 @@ QXmppTask<QHash<QString, QMultiHash<QString, QByteArray>>> Core::TrustHandler::s
QXmppTask<QHash<QString, QMultiHash<QString, QByteArray>>> Core::TrustHandler::setTrustLevel(
const QString& encryption,
const QMultiHash<QString, QByteArray>& keyIds,
QXmpp::TrustLevel trustLevel)
{
QXmpp::TrustLevel trustLevel
) {
QHash<QString, QMultiHash<QString, QByteArray>> modifiedKeys;
Shared::TrustLevel level = convert(trustLevel);
std::set<QString> modifiedJids;
KeyCache* cache = getCache(encryption);
LMDBAL::WriteTransaction txn = db.beginTransaction();
for (MultySB::const_iterator itr = keyIds.begin(), end = keyIds.end(); itr != end; ++itr) {
const QString& keyOwnerJid = itr.key();
const QByteArray& keyId = itr.value();
try {
Keys map = cache->getRecord(keyOwnerJid);
Keys map = cache->getRecord(keyOwnerJid, txn);
std::pair<Keys::iterator, bool> result = map.insert(std::make_pair(keyId, level));
bool changed = result.second;
if (!changed && result.first->second != level) {
@ -174,15 +178,16 @@ QXmppTask<QHash<QString, QMultiHash<QString, QByteArray>>> Core::TrustHandler::s
if (changed) {
modifiedKeys[encryption].insert(keyOwnerJid, keyId);
modifiedJids.insert(keyOwnerJid);
cache->changeRecord(keyOwnerJid, map);
cache->changeRecord(keyOwnerJid, map, txn);
}
} catch (const LMDBAL::NotFound& e) {
Keys map({{keyId, level}});
modifiedKeys[encryption].insert(keyOwnerJid, keyId);
modifiedJids.insert(keyOwnerJid);
cache->addRecord(keyOwnerJid, map);
cache->addRecord(keyOwnerJid, map, txn);
}
}
txn.commit();
for (const QString& jid : modifiedJids)
emit trustLevelsChanged(jid, getSummary(jid));
@ -190,10 +195,11 @@ QXmppTask<QHash<QString, QMultiHash<QString, QByteArray>>> Core::TrustHandler::s
return Core::makeReadyTask(std::move(modifiedKeys));
}
QXmppTask<bool> Core::TrustHandler::hasKey(const QString& encryption,
const QString& keyOwnerJid,
QXmpp::TrustLevels trustLevels)
{
QXmppTask<bool> Core::TrustHandler::hasKey(
const QString& encryption,
const QString& keyOwnerJid,
QXmpp::TrustLevels trustLevels
) {
KeyCache* cache = getCache(encryption);
bool found = false;
try {
@ -211,8 +217,8 @@ QXmppTask<bool> Core::TrustHandler::hasKey(const QString& encryption,
QXmppTask<QHash<QString, QHash<QByteArray, QXmpp::TrustLevel>>> Core::TrustHandler::keys(
const QString& encryption,
const QList<QString>& keyOwnerJids,
QXmpp::TrustLevels trustLevels)
{
QXmpp::TrustLevels trustLevels
) {
HSHBTL res;
KeyCache* cache = getCache(encryption);
@ -233,8 +239,8 @@ QXmppTask<QHash<QString, QHash<QByteArray, QXmpp::TrustLevel>>> Core::TrustHandl
QXmppTask<QHash<QXmpp::TrustLevel, QMultiHash<QString, QByteArray>>> Core::TrustHandler::keys(
const QString& encryption,
QXmpp::TrustLevels trustLevels)
{
QXmpp::TrustLevels trustLevels
) {
QHash<TL, MultySB> res;
KeyCache* cache = getCache(encryption);
std::map<QString, Keys> storage = cache->readAll();
@ -253,9 +259,11 @@ QXmppTask<void> Core::TrustHandler::removeKeys(const QString& encryption) {
if (itr == keysByProtocol.end())
return Core::makeReadyTask();
LMDBAL::WriteTransaction txn = db.beginTransaction();
KeyCache* cache = itr->second;
std::map<QString, Keys> keys = cache->readAll();
cache->drop();
std::map<QString, Keys> keys = cache->readAll(txn);
cache->drop(txn);
txn.commit();
for (const std::pair<const QString, Keys>& pair : keys) {
bool empty = true;
@ -292,8 +300,9 @@ QXmppTask<void> Core::TrustHandler::removeKeys(const QString& encryption, const
for (const QByteArray& keyId : keyIds)
set.insert(keyId);
LMDBAL::WriteTransaction txn = db.beginTransaction();
KeyCache* cache = getCache(encryption);
std::map<QString, Keys> data = cache->readAll();
std::map<QString, Keys> data = cache->readAll(txn);
std::set<QString> modifiedJids;
for (std::map<QString, Keys>::iterator cItr = data.begin(), cEnd = data.end(); cItr != cEnd; /*no increment*/) {
@ -311,10 +320,10 @@ QXmppTask<void> Core::TrustHandler::removeKeys(const QString& encryption, const
else
++cItr;
}
if (modifiedJids.size() > 0) {
cache->replaceAll(data);
}
if (modifiedJids.size() > 0)
cache->replaceAll(data, txn);
txn.commit();
for (const QString& jid : modifiedJids)
emit trustLevelsChanged(jid, getSummary(jid));
@ -325,14 +334,15 @@ QXmppTask<void> Core::TrustHandler::addKeys(
const QString& encryption,
const QString& keyOwnerJid,
const QList<QByteArray>& keyIds,
QXmpp::TrustLevel trustLevel)
{
QXmpp::TrustLevel trustLevel
) {
KeyCache* cache = getCache(encryption);
Shared::TrustLevel level = convert(trustLevel);
Keys data;
bool had = false;
LMDBAL::WriteTransaction txn = db.beginTransaction();
try {
data = cache->getRecord(keyOwnerJid);
data = cache->getRecord(keyOwnerJid, txn);
had = true;
} catch (const LMDBAL::NotFound& e) {}
for (const QByteArray& keyId : keyIds) {
@ -342,10 +352,11 @@ QXmppTask<void> Core::TrustHandler::addKeys(
}
if (had)
cache->changeRecord(keyOwnerJid, data);
cache->changeRecord(keyOwnerJid, data, txn);
else
cache->addRecord(keyOwnerJid, data);
cache->addRecord(keyOwnerJid, data, txn);
txn.commit();
emit trustLevelsChanged(keyOwnerJid, getSummary(keyOwnerJid));
return Core::makeReadyTask();
@ -418,9 +429,9 @@ Shared::TrustSummary Core::TrustHandler::getSummary(const QString& jid) const {
try {
Keys keys = pair.second->getRecord(jid);
Shared::EncryptionProtocol protocol = Shared::TrustSummary::protocolValues.at(pair.first);
for (const std::pair<const QByteArray, Shared::TrustLevel>& trust : keys) {
for (const std::pair<const QByteArray, Shared::TrustLevel>& trust : keys)
result.increment(protocol, trust.second);
}
} catch (const LMDBAL::NotFound& e) {}
}