full transition to lmdbal, DOESNT WORK, DONT TAKE!

This commit is contained in:
Blue 2023-11-02 19:55:11 -03:00
parent 23ec80ccba
commit 9d688e8596
Signed by: blue
GPG Key ID: 9B203B252A63EE38
18 changed files with 497 additions and 1752 deletions

View File

@ -60,6 +60,7 @@ if (WITH_OMEMO)
if (PKG_CONFIG_FOUND)
pkg_check_modules(OMEMO libomemo-c)
if (OMEMO_FOUND)
target_compile_definitions(squawk PRIVATE WITH_OMEMO)
message("Building with support of OMEMO")
else ()
message("libomemo-c package wasn't found, trying to build without OMEMO support")
@ -73,7 +74,11 @@ endif ()
## QXmpp
if (SYSTEM_QXMPP)
find_package(QXmpp CONFIG)
if (WITH_OMEMO)
find_package(QXmpp CONFIG COMPONENTS Omemo)
else ()
find_package(QXmpp CONFIG)
endif ()
if (NOT QXmpp_FOUND)
set(SYSTEM_QXMPP OFF)
@ -138,7 +143,6 @@ if (NOT SYSTEM_QXMPP)
target_include_directories(squawk PRIVATE ${CMAKE_SOURCE_DIR}/external/qxmpp/src/omemo)
target_include_directories(squawk PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/external/qxmpp/src/omemo)
set(BUILD_OMEMO ON)
target_compile_definitions(squawk PRIVATE WITH_OMEMO)
else ()
set(BUILD_OMEMO OFF)
endif ()
@ -150,6 +154,9 @@ if (NOT SYSTEM_QXMPP)
endif ()
else ()
target_link_libraries(squawk PRIVATE QXmpp::QXmpp)
if (WITH_OMEMO)
target_link_libraries(squawk PRIVATE QXmpp::Omemo)
endif ()
endif ()
## LMDBAL

View File

@ -63,7 +63,7 @@
#include "handlers/discoveryhandler.h"
#ifdef WITH_OMEMO
#include <QXmppOmemoManager.h>
#include <Omemo/QXmppOmemoManager.h>
#include <QXmppTrustManager.h>
#include "handlers/trusthandler.h"
#include "handlers/omemohandler.h"

View File

@ -41,18 +41,12 @@ void Core::UrlStorage::close() {
}
void Core::UrlStorage::writeInfo(const QString& key, const Core::UrlStorage::UrlInfo& info, bool overwrite) {
LMDBAL::TransactionID txn = base.beginTransaction();
try {
writeInfo(key, info, txn, overwrite);
base.commitTransaction(txn);
} catch (...) {
base.abortTransaction(txn);
throw;
}
LMDBAL::WriteTransaction txn = base.beginTransaction();
writeInfo(key, info, txn, overwrite);
txn.commit();
}
void Core::UrlStorage::writeInfo(const QString& key, const Core::UrlStorage::UrlInfo& info, MDB_txn* txn, bool overwrite) {
void Core::UrlStorage::writeInfo(const QString& key, const Core::UrlStorage::UrlInfo& info, const LMDBAL::WriteTransaction& txn, bool overwrite) {
if (overwrite)
urlToInfo->forceRecord(key, info, txn);
else
@ -95,15 +89,11 @@ Core::UrlStorage::UrlInfo Core::UrlStorage::addToInfo(
const QString& path
) {
UrlInfo info;
LMDBAL::TransactionID txn = base.beginTransaction();
LMDBAL::WriteTransaction txn = base.beginTransaction();
try {
urlToInfo->getRecord(url, info, txn);
} catch (const LMDBAL::NotFound& e) {
} catch (...) {
base.abortTransaction(txn);
throw;
}
} catch (const LMDBAL::NotFound& e) {}
bool pathChange = false;
bool listChange = false;
@ -118,15 +108,8 @@ Core::UrlStorage::UrlInfo Core::UrlStorage::addToInfo(
listChange = info.addMessage(account, jid, id);
if (pathChange || listChange) {
try {
writeInfo(url, info, txn, true);
base.commitTransaction(txn);
} catch (...) {
base.abortTransaction(txn);
throw;
}
} else {
base.abortTransaction(txn);
writeInfo(url, info, txn, true);
txn.commit();
}
return info;
@ -134,70 +117,51 @@ Core::UrlStorage::UrlInfo Core::UrlStorage::addToInfo(
std::list<Shared::MessageInfo> Core::UrlStorage::setPath(const QString& url, const QString& path) {
std::list<Shared::MessageInfo> list;
LMDBAL::TransactionID txn = base.beginTransaction();
LMDBAL::WriteTransaction txn = base.beginTransaction();
UrlInfo info;
try {
urlToInfo->getRecord(url, info, txn);
info.getMessages(list);
} catch (const LMDBAL::NotFound& e) {
} catch (...) {
base.abortTransaction(txn);
throw;
}
} catch (const LMDBAL::NotFound& e) {}
info.setPath(path);
try {
writeInfo(url, info, txn, true);
base.commitTransaction(txn);
} catch (...) {
base.abortTransaction(txn);
throw;
}
writeInfo(url, info, txn, true);
txn.commit();
return list;
}
std::list<Shared::MessageInfo> Core::UrlStorage::removeFile(const QString& url) {
std::list<Shared::MessageInfo> list;
LMDBAL::TransactionID txn = base.beginTransaction();
LMDBAL::WriteTransaction txn = base.beginTransaction();
UrlInfo info;
try {
urlToInfo->getRecord(url, info, txn);
urlToInfo->removeRecord(url);
info.getMessages(list);
if (info.hasPath())
pathToUrl->removeRecord(info.getPath());
urlToInfo->getRecord(url, info, txn);
urlToInfo->removeRecord(url, txn);
info.getMessages(list);
base.commitTransaction(txn);
} catch (...) {
base.abortTransaction(txn);
throw;
}
if (info.hasPath())
pathToUrl->removeRecord(info.getPath(), txn);
txn.commit();
return list;
}
std::list<Shared::MessageInfo> Core::UrlStorage::deletedFile(const QString& path) {
std::list<Shared::MessageInfo> list;
LMDBAL::TransactionID txn = base.beginTransaction();
LMDBAL::WriteTransaction txn = base.beginTransaction();
try {
QString url = pathToUrl->getRecord(path, txn);
pathToUrl->removeRecord(path);
UrlInfo info = urlToInfo->getRecord(url, txn);
info.getMessages(list);
info.setPath(QString());
urlToInfo->changeRecord(url, info, txn);
base.commitTransaction(txn);
} catch (...) {
base.abortTransaction(txn);
throw;
}
QString url = pathToUrl->getRecord(path, txn);
pathToUrl->removeRecord(path, txn);
UrlInfo info = urlToInfo->getRecord(url, txn);
info.getMessages(list);
info.setPath(QString());
urlToInfo->changeRecord(url, info, txn);
txn.commit();
return list;
}

View File

@ -60,7 +60,7 @@ private:
private:
void writeInfo(const QString& key, const UrlInfo& info, bool overwrite = false);
void writeInfo(const QString& key, const UrlInfo& info, MDB_txn* txn, bool overwrite = false);
void writeInfo(const QString& key, const UrlInfo& info, const LMDBAL::WriteTransaction& txn, bool overwrite = false);
UrlInfo addToInfo(const QString& url, const QString& account, const QString& jid, const QString& id, const QString& path = "-s");
public:

View File

@ -591,7 +591,7 @@ void Core::MessageHandler::resendMessage(const QString& jid, const QString& id)
} else {
qDebug() << "An attempt to resend a message to" << jid << "by account" << acc->getName() << ", but this message seems to have been normally sent, this method was made to retry sending failed to be sent messages, skipping";
}
} catch (const Archive::NotFound& err) {
} catch (const LMDBAL::NotFound& err) {
qDebug() << "An attempt to resend a message to" << jid << "by account" << acc->getName() << ", but this message wasn't found in history, skipping";
}
} else {

View File

@ -21,7 +21,7 @@
#include <list>
#include <functional>
#include <QXmppOmemoStorage.h>
#include <Omemo/QXmppOmemoStorage.h>
#include <cache.h>
#include <shared/keyinfo.h>

View File

@ -27,7 +27,7 @@ Core::RosterItem::RosterItem(const QString& pJid, const QString& pAccount, QObje
account(pAccount),
name(),
archiveState(empty),
archive(new Archive(jid)),
archive(new Archive(account, jid)),
syncronizing(false),
requestedCount(0),
requestedBefore(),
@ -38,7 +38,7 @@ Core::RosterItem::RosterItem(const QString& pJid, const QString& pAccount, QObje
toCorrect(),
muc(false)
{
archive->open(account);
archive->open();
if (archive->size() != 0) {
if (archive->isFromTheBeginning())
@ -126,7 +126,8 @@ void Core::RosterItem::nextRequest() {
last = true;
}
}
} catch (const Archive::Empty& e) {
//} catch (const Archive::Empty& e) {
} catch (const LMDBAL::NotFound& e) {
last = true;
}
} else if (archiveState == empty && responseCache.size() == 0) {
@ -168,7 +169,8 @@ void Core::RosterItem::performRequest(int count, const QString& before) {
try {
Shared::Message msg = archive->newest();
emit needHistory("", getId(msg), msg.getTime());
} catch (const Archive::Empty& e) { //this can happen when the only message in archive is not server stored (error, for example)
//} catch (const Archive::Empty& e) {
} catch (const LMDBAL::NotFound& e) { //this can happen when the only message in archive is not server stored (error, for example)
emit needHistory(before, "");
}
}
@ -186,14 +188,14 @@ void Core::RosterItem::performRequest(int count, const QString& before) {
std::list<Shared::Message> arc = archive->getBefore(requestedCount - responseCache.size(), lBefore);
responseCache.insert(responseCache.begin(), arc.begin(), arc.end());
found = true;
} catch (const Archive::NotFound& e) {
requestCache.emplace_back(requestedCount, before);
requestedCount = -1;
emit needHistory(getId(archive->oldest()), "");
} catch (const Archive::Empty& e) {
} catch (const LMDBAL::NotFound& e) {
requestCache.emplace_back(requestedCount, before);
requestedCount = -1;
emit needHistory(getId(archive->oldest()), "");
// } catch (const Archive::Empty& e) {
// requestCache.emplace_back(requestedCount, before);
// requestedCount = -1;
// emit needHistory(getId(archive->oldest()), "");
}
if (found) {
@ -226,10 +228,10 @@ void Core::RosterItem::performRequest(int count, const QString& before) {
try {
std::list<Shared::Message> arc = archive->getBefore(requestedCount - responseCache.size(), before);
responseCache.insert(responseCache.begin(), arc.begin(), arc.end());
} catch (const Archive::NotFound& e) {
qDebug("requesting id hasn't been found in archive, skipping");
} catch (const Archive::Empty& e) {
} catch (const LMDBAL::NotFound& e) {
qDebug("requesting id hasn't been found in archive, skipping");
// } catch (const Archive::Empty& e) {
// qDebug("requesting id hasn't been found in archive, skipping");
}
nextRequest();
break;
@ -311,7 +313,7 @@ bool Core::RosterItem::changeMessage(const QString& id, const QMap<QString, QVar
try {
archive->changeMessage(id, data);
found = true;
} catch (const Archive::NotFound& e) {
} catch (const LMDBAL::NotFound& e) {
qDebug() << "An attempt to change state to the message" << id << "but it couldn't be found";
}
}
@ -387,10 +389,8 @@ void Core::RosterItem::flushMessagesToArchive(bool finished, const QString& firs
std::list<Shared::Message> arc = archive->getBefore(requestedCount - responseCache.size(), before);
responseCache.insert(responseCache.begin(), arc.begin(), arc.end());
found = true;
} catch (const Archive::NotFound& e) {
} catch (const Archive::Empty& e) {
} catch (const LMDBAL::NotFound& e) {
// } catch (const Archive::Empty& e) {
}
if (!found || requestedCount > int(responseCache.size())) {
if (archiveState == complete) {

File diff suppressed because it is too large Load Diff

View File

@ -29,18 +29,21 @@
#include <lmdb.h>
#include <list>
#include <lmdbal/base.h>
#include <lmdbal/storage.h>
#include <lmdbal/cursor.h>
namespace Core {
class Archive : public QObject
{
class Archive : public QObject {
Q_OBJECT
public:
class AvatarInfo;
Archive(const QString& jid, QObject* parent = 0);
Archive(const QString& account, const QString& jid, QObject* parent = 0);
~Archive();
void open(const QString& account);
void open();
void close();
bool addElement(const Shared::Message& message);
@ -48,13 +51,13 @@ public:
Shared::Message getElement(const QString& id) const;
bool hasElement(const QString& id) const;
void changeMessage(const QString& id, const QMap<QString, QVariant>& data);
Shared::Message oldest();
QString oldestId();
Shared::Message newest();
QString newestId();
Shared::Message oldest() const;
QString oldestId() const;
Shared::Message newest() const;
QString newestId() const;
void clear();
long unsigned int size() const;
std::list<Shared::Message> getBefore(int count, const QString& id);
std::list<Shared::Message> getBefore(unsigned int count, const QString& id);
bool isFromTheBeginning() const;
void setFromTheBeginning(bool is);
bool isEncryptionEnabled() const;
@ -68,103 +71,14 @@ public:
public:
const QString jid;
const QString account;
public:
class Directory:
public Utils::Exception
{
public:
Directory(const std::string& p_path):Exception(), path(p_path){}
std::string getMessage() const{return "Can't create directory for database at " + path;}
private:
std::string path;
};
class Closed:
public Utils::Exception
{
public:
Closed(const std::string& op, const std::string& acc):Exception(), operation(op), account(acc){}
std::string getMessage() const{return "An attempt to perform operation " + operation + " on closed archive for " + account;}
private:
std::string operation;
std::string account;
};
class NotFound:
public Utils::Exception
{
public:
NotFound(const std::string& k, const std::string& acc):Exception(), key(k), account(acc){}
std::string getMessage() const{return "Element for id " + key + " wasn't found in database " + account;}
private:
std::string key;
std::string account;
};
class Empty:
public Utils::Exception
{
public:
Empty(const std::string& acc):Exception(), account(acc){}
std::string getMessage() const{return "An attempt to read ordered elements from database " + account + " but it's empty";}
private:
std::string account;
};
class Exist:
public Utils::Exception
{
public:
Exist(const std::string& acc, const std::string& p_key):Exception(), account(acc), key(p_key){}
std::string getMessage() const{return "An attempt to insert element " + key + " to database " + account + " but it already has an element with given id";}
private:
std::string account;
std::string key;
};
class NoAvatar:
public Utils::Exception
{
public:
NoAvatar(const std::string& el, const std::string& res):Exception(), element(el), resource(res){
if (resource.size() == 0) {
resource = "for himself";
}
}
std::string getMessage() const{return "Element " + element + " has no avatar for " + resource ;}
private:
std::string element;
std::string resource;
};
class Unknown:
public Utils::Exception
{
public:
Unknown(const std::string& acc, const std::string& message):Exception(), account(acc), msg(message){}
std::string getMessage() const{return "Unknown error on database " + account + ": " + msg;}
private:
std::string account;
std::string msg;
};
class AvatarInfo {
public:
AvatarInfo();
AvatarInfo(const QString& type, const QByteArray& hash, bool autogenerated);
void deserialize(char* pointer, uint32_t size);
void serialize(QByteArray* ba) const;
QString type;
QByteArray hash;
bool autogenerated;
@ -172,29 +86,18 @@ public:
private:
bool opened;
bool fromTheBeginning;
bool encryptionEnabled;
MDB_env* environment;
MDB_dbi main; //id to message
MDB_dbi order; //time to id
MDB_dbi stats;
MDB_dbi avatars;
MDB_dbi sid; //stanzaId to id
bool getStatBoolValue(const std::string& id, MDB_txn* txn);
std::string getStatStringValue(const std::string& id, MDB_txn* txn);
bool setStatValue(const std::string& id, bool value, MDB_txn* txn);
bool setStatValue(const std::string& id, const std::string& value, MDB_txn* txn);
bool readAvatarInfo(AvatarInfo& target, const std::string& res, MDB_txn* txn) const;
void printOrder();
void printKeys();
bool dropAvatar(const std::string& resource);
Shared::Message getMessage(const std::string& id, MDB_txn* txn) const;
Shared::Message getStoredMessage(MDB_txn *txn, MDB_cursor* cursor, MDB_cursor_op op, MDB_val* key, MDB_val* value, int& rc);
Shared::Message edge(bool end);
LMDBAL::Base db;
LMDBAL::Storage<QString, Shared::Message>* messages;
LMDBAL::Storage<uint64_t, QString>* order;
LMDBAL::Storage<QString, QVariant>* stats;
LMDBAL::Storage<QString, AvatarInfo>* avatars;
LMDBAL::Storage<QString, QString>* stanzaIdToId;
mutable LMDBAL::Cursor<uint64_t, QString> cursor;
};
}
QDataStream& operator << (QDataStream &out, const Core::Archive::AvatarInfo& info);
QDataStream& operator >> (QDataStream &in, Core::Archive::AvatarInfo& info);
#endif // CORE_ARCHIVE_H

View File

@ -1,55 +0,0 @@
// Squawk messenger.
// Copyright (C) 2019 Yury Gubich <blue@macaw.me>
//
// 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
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef CORE_CACHE_H
#define CORE_CACHE_H
#include <map>
#include <set>
#include <QString>
#include <core/storage/storage.h>
namespace Core {
template <class K, class V>
class Cache
{
public:
Cache(const QString& name);
~Cache();
void open();
void close();
void addRecord(const K& key, const V& value);
void changeRecord(const K& key, const V& value);
void removeRecord(const K& key);
V getRecord(const K& key) const;
bool checkRecord(const K& key) const;
private:
Core::Storage<K, V> storage;
std::map<K, V>* cache;
std::set<K>* abscent;
};
}
#include "cache.hpp"
#endif // CORE_CACHE_H

View File

@ -1,102 +0,0 @@
// Squawk messenger.
// Copyright (C) 2019 Yury Gubich <blue@macaw.me>
//
// 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
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef CORE_CACHE_HPP
#define CORE_CACHE_HPP
#include "cache.h"
template <class K, class V>
Core::Cache<K, V>::Cache(const QString& name):
storage(name),
cache(new std::map<K, V> ()),
abscent(new std::set<K> ()) {}
template <class K, class V>
Core::Cache<K, V>::~Cache() {
close();
delete cache;
delete abscent;
}
template <class K, class V>
void Core::Cache<K, V>::open() {
storage.open();}
template <class K, class V>
void Core::Cache<K, V>::close() {
storage.close();}
template <class K, class V>
void Core::Cache<K, V>::addRecord(const K& key, const V& value) {
storage.addRecord(key, value);
cache->insert(std::make_pair(key, value));
abscent->erase(key);
}
template <class K, class V>
V Core::Cache<K, V>::getRecord(const K& key) const {
typename std::map<K, V>::const_iterator itr = cache->find(key);
if (itr == cache->end()) {
if (abscent->count(key) > 0) {
throw Archive::NotFound(std::to_string(key), storage.getName().toStdString());
}
try {
V value = storage.getRecord(key);
itr = cache->insert(std::make_pair(key, value)).first;
} catch (const Archive::NotFound& error) {
abscent->insert(key);
throw error;
}
}
return itr->second;
}
template<class K, class V>
bool Core::Cache<K, V>::checkRecord(const K& key) const {
typename std::map<K, V>::const_iterator itr = cache->find(key);
if (itr != cache->end())
return true;
if (abscent->count(key) > 0)
return false;
try {
V value = storage.getRecord(key);
itr = cache->insert(std::make_pair(key, value)).first;
} catch (const Archive::NotFound& error) {
return false;
}
return true;
}
template<class K, class V>
void Core::Cache<K, V>::changeRecord(const K& key, const V& value) {
storage.changeRecord(key, value); //there is a non straightforward behaviour: if there was no element at the sorage it will be added
cache->at(key) = value;
abscent->erase(key); //so... this line here is to make it coherent with the storage
}
template<class K, class V>
void Core::Cache<K, V>::removeRecord(const K& key) {
storage.removeRecord(key);
cache->erase(key);
abscent->insert(key);
}
#endif //CORE_CACHE_HPP

View File

@ -1,70 +0,0 @@
/*
* Squawk messenger.
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CORE_STORAGE_H
#define CORE_STORAGE_H
#include <QString>
#include <lmdb.h>
#include "archive.h"
namespace Core {
/**
* @todo write docs
*/
template <class K, class V>
class Storage
{
public:
Storage(const QString& name);
~Storage();
void open();
void close();
void addRecord(const K& key, const V& value);
void changeRecord(const K& key, const V& value);
void removeRecord(const K& key);
V getRecord(const K& key) const;
QString getName() const;
private:
QString name;
bool opened;
MDB_env* environment;
MDB_dbi base;
};
}
MDB_val& operator << (MDB_val& data, QString& value);
MDB_val& operator >> (MDB_val& data, QString& value);
MDB_val& operator << (MDB_val& data, uint32_t& value);
MDB_val& operator >> (MDB_val& data, uint32_t& value);
namespace std {
std::string to_string(const QString& str);
}
#include "storage.hpp"
#endif // CORE_STORAGE_H

View File

@ -1,226 +0,0 @@
/*
* Squawk messenger.
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CORE_STORAGE_HPP
#define CORE_STORAGE_HPP
#include <QStandardPaths>
#include <QDir>
#include "storage.h"
#include <cstring>
template <class K, class V>
Core::Storage<K, V>::Storage(const QString& p_name):
name(p_name),
opened(false),
environment(),
base()
{
}
template <class K, class V>
Core::Storage<K, V>::~Storage()
{
close();
}
template <class K, class V>
void Core::Storage<K, V>::open()
{
if (!opened) {
mdb_env_create(&environment);
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
path += "/" + name;
QDir cache(path);
if (!cache.exists()) {
bool res = cache.mkpath(path);
if (!res) {
throw Archive::Directory(path.toStdString());
}
}
mdb_env_set_maxdbs(environment, 1);
mdb_env_set_mapsize(environment, 10UL * 1024UL * 1024UL);
mdb_env_open(environment, path.toStdString().c_str(), 0, 0664);
MDB_txn *txn;
mdb_txn_begin(environment, NULL, 0, &txn);
mdb_dbi_open(txn, "base", MDB_CREATE, &base);
mdb_txn_commit(txn);
opened = true;
}
}
template <class K, class V>
void Core::Storage<K, V>::close()
{
if (opened) {
mdb_dbi_close(environment, base);
mdb_env_close(environment);
opened = false;
}
}
template <class K, class V>
void Core::Storage<K, V>::addRecord(const K& key, const V& value)
{
if (!opened) {
throw Archive::Closed("addRecord", name.toStdString());
}
QByteArray ba;
QDataStream ds(&ba, QIODevice::WriteOnly);
ds << value;
MDB_val lmdbKey, lmdbData;
lmdbKey << key;
lmdbData.mv_size = ba.size();
lmdbData.mv_data = (uint8_t*)ba.data();
MDB_txn *txn;
mdb_txn_begin(environment, NULL, 0, &txn);
int rc;
rc = mdb_put(txn, base, &lmdbKey, &lmdbData, MDB_NOOVERWRITE);
if (rc != 0) {
mdb_txn_abort(txn);
if (rc == MDB_KEYEXIST) {
throw Archive::Exist(name.toStdString(), std::to_string(key));
} else {
throw Archive::Unknown(name.toStdString(), mdb_strerror(rc));
}
} else {
mdb_txn_commit(txn);
}
}
template <class K, class V>
void Core::Storage<K, V>::changeRecord(const K& key, const V& value)
{
if (!opened) {
throw Archive::Closed("changeRecord", name.toStdString());
}
QByteArray ba;
QDataStream ds(&ba, QIODevice::WriteOnly);
ds << value;
MDB_val lmdbKey, lmdbData;
lmdbKey << key;
lmdbData.mv_size = ba.size();
lmdbData.mv_data = (uint8_t*)ba.data();
MDB_txn *txn;
mdb_txn_begin(environment, NULL, 0, &txn);
int rc;
rc = mdb_put(txn, base, &lmdbKey, &lmdbData, 0);
if (rc != 0) {
mdb_txn_abort(txn);
if (rc) {
throw Archive::Unknown(name.toStdString(), mdb_strerror(rc));
}
} else {
mdb_txn_commit(txn);
}
}
template <class K, class V>
V Core::Storage<K, V>::getRecord(const K& key) const
{
if (!opened) {
throw Archive::Closed("addElement", name.toStdString());
}
MDB_val lmdbKey, lmdbData;
lmdbKey << key;
MDB_txn *txn;
int rc;
mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
rc = mdb_get(txn, base, &lmdbKey, &lmdbData);
if (rc) {
mdb_txn_abort(txn);
if (rc == MDB_NOTFOUND) {
throw Archive::NotFound(std::to_string(key), name.toStdString());
} else {
throw Archive::Unknown(name.toStdString(), mdb_strerror(rc));
}
} else {
QByteArray ba((char*)lmdbData.mv_data, lmdbData.mv_size);
QDataStream ds(&ba, QIODevice::ReadOnly);
V value;
ds >> value;
mdb_txn_abort(txn);
return value;
}
}
template <class K, class V>
void Core::Storage<K, V>::removeRecord(const K& key)
{
if (!opened) {
throw Archive::Closed("addElement", name.toStdString());
}
MDB_val lmdbKey;
lmdbKey << key;
MDB_txn *txn;
int rc;
mdb_txn_begin(environment, NULL, 0, &txn);
rc = mdb_del(txn, base, &lmdbKey, NULL);
if (rc) {
mdb_txn_abort(txn);
if (rc == MDB_NOTFOUND) {
throw Archive::NotFound(std::to_string(key), name.toStdString());
} else {
throw Archive::Unknown(name.toStdString(), mdb_strerror(rc));
}
} else {
mdb_txn_commit(txn);
}
}
template <class K, class V>
QString Core::Storage<K, V>::getName() const {
return name;}
MDB_val& operator << (MDB_val& data, const QString& value) {
QByteArray ba = value.toUtf8();
data.mv_size = ba.size();
data.mv_data = ba.data();
return data;
}
MDB_val& operator >> (MDB_val& data, QString& value) {
value = QString::fromUtf8((const char*)data.mv_data, data.mv_size);
return data;
}
MDB_val& operator << (MDB_val& data, uint32_t& value) {
data.mv_size = 4;
data.mv_data = &value;
return data;
}
MDB_val& operator >> (MDB_val& data, uint32_t& value) {
std::memcpy(&value, data.mv_data, data.mv_size);
return data;
}
std::string std::to_string(const QString& str) {
return str.toStdString();
}
#endif //CORE_STORAGE_HPP

2
external/qxmpp vendored

@ -1 +1 @@
Subproject commit ab4bdf2da41a26f462fe3a333a34e32c999e2a6d
Subproject commit 9e9c22b16a39c7370fed31c6deea56d8abf72440

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.5)
project(simplecrypt LANGUAGES CXX)
set(CMAKE_AUTOMOC ON)

View File

@ -24,7 +24,7 @@
#include <QObject>
#ifdef WITH_OMEMO
#include <QXmppOmemoStorage.h>
#include <Omemo/QXmppOmemoStorage.h>
#endif
int main(int argc, char *argv[])

View File

@ -61,50 +61,42 @@ Shared::Message::Message():
attachPath()
{}
QString Shared::Message::getBody() const
{
QString Shared::Message::getBody() const {
return body;
}
QString Shared::Message::getFrom() const
{
QString Shared::Message::getFrom() const {
QString from = jFrom;
if (rFrom.size() > 0) {
if (rFrom.size() > 0)
from += "/" + rFrom;
}
return from;
}
QString Shared::Message::getTo() const
{
QString Shared::Message::getTo() const {
QString to = jTo;
if (rTo.size() > 0) {
if (rTo.size() > 0)
to += "/" + rTo;
}
return to;
}
QString Shared::Message::getId() const
{
if (id.size() > 0) {
QString Shared::Message::getId() const {
if (id.size() > 0)
return id;
} else {
else
return stanzaId;
}
}
QDateTime Shared::Message::getTime() const
{
QDateTime Shared::Message::getTime() const {
return time;
}
void Shared::Message::setBody(const QString& p_body)
{
void Shared::Message::setBody(const QString& p_body) {
body = p_body;
}
void Shared::Message::setFrom(const QString& from)
{
void Shared::Message::setFrom(const QString& from) {
QStringList list = from.split("/");
if (list.size() == 1) {
jFrom = from.toLower();
@ -114,8 +106,7 @@ void Shared::Message::setFrom(const QString& from)
}
}
void Shared::Message::setTo(const QString& to)
{
void Shared::Message::setTo(const QString& to) {
QStringList list = to.split("/");
if (list.size() == 1) {
jTo = to.toLower();
@ -125,153 +116,122 @@ void Shared::Message::setTo(const QString& to)
}
}
void Shared::Message::setId(const QString& p_id)
{
void Shared::Message::setId(const QString& p_id) {
id = p_id;
}
void Shared::Message::setTime(const QDateTime& p_time)
{
void Shared::Message::setTime(const QDateTime& p_time) {
time = p_time;
}
QString Shared::Message::getFromJid() const
{
QString Shared::Message::getFromJid() const {
return jFrom;
}
QString Shared::Message::getFromResource() const
{
QString Shared::Message::getFromResource() const {
return rFrom;
}
QString Shared::Message::getToJid() const
{
QString Shared::Message::getToJid() const {
return jTo;
}
QString Shared::Message::getToResource() const
{
QString Shared::Message::getToResource() const {
return rTo;
}
QString Shared::Message::getErrorText() const
{
QString Shared::Message::getErrorText() const {
return errorText;
}
QString Shared::Message::getPenPalJid() const
{
if (outgoing) {
QString Shared::Message::getPenPalJid() const {
if (outgoing)
return jTo;
} else {
else
return jFrom;
}
}
QString Shared::Message::getPenPalResource() const
{
if (outgoing) {
QString Shared::Message::getPenPalResource() const {
if (outgoing)
return rTo;
} else {
else
return rFrom;
}
}
Shared::Message::State Shared::Message::getState() const
{
Shared::Message::State Shared::Message::getState() const {
return state;
}
bool Shared::Message::getEdited() const
{
bool Shared::Message::getEdited() const {
return edited;
}
void Shared::Message::setFromJid(const QString& from)
{
void Shared::Message::setFromJid(const QString& from) {
jFrom = from.toLower();
}
void Shared::Message::setFromResource(const QString& from)
{
void Shared::Message::setFromResource(const QString& from) {
rFrom = from;
}
void Shared::Message::setToJid(const QString& to)
{
void Shared::Message::setToJid(const QString& to) {
jTo = to.toLower();
}
void Shared::Message::setToResource(const QString& to)
{
void Shared::Message::setToResource(const QString& to) {
rTo = to;
}
void Shared::Message::setErrorText(const QString& err)
{
if (state == State::error) {
void Shared::Message::setErrorText(const QString& err) {
if (state == State::error)
errorText = err;
}
}
bool Shared::Message::getOutgoing() const
{
bool Shared::Message::getOutgoing() const {
return outgoing;
}
void Shared::Message::setOutgoing(bool og)
{
void Shared::Message::setOutgoing(bool og) {
outgoing = og;
}
bool Shared::Message::getForwarded() const
{
bool Shared::Message::getForwarded() const {
return forwarded;
}
void Shared::Message::generateRandomId()
{
void Shared::Message::generateRandomId() {
id = generateUUID();
}
QString Shared::Message::getThread() const
{
QString Shared::Message::getThread() const {
return thread;
}
void Shared::Message::setForwarded(bool fwd)
{
void Shared::Message::setForwarded(bool fwd) {
forwarded = fwd;
}
void Shared::Message::setThread(const QString& p_body)
{
void Shared::Message::setThread(const QString& p_body) {
thread = p_body;
}
QDateTime Shared::Message::getLastModified() const
{
QDateTime Shared::Message::getLastModified() const {
return lastModified;
}
QString Shared::Message::getOriginalBody() const
{
QString Shared::Message::getOriginalBody() const {
return originalMessage;
}
Shared::Message::Type Shared::Message::getType() const
{
Shared::Message::Type Shared::Message::getType() const {
return type;
}
void Shared::Message::setType(Shared::Message::Type t)
{
void Shared::Message::setType(Shared::Message::Type t) {
type = t;
}
void Shared::Message::setState(Shared::Message::State p_state)
{
void Shared::Message::setState(Shared::Message::State p_state) {
state = p_state;
if (state != State::error) {
@ -279,96 +239,92 @@ void Shared::Message::setState(Shared::Message::State p_state)
}
}
bool Shared::Message::serverStored() const
{
bool Shared::Message::serverStored() const {
return state == State::delivered || state == State::sent;
}
void Shared::Message::setEdited(bool p_edited)
{
void Shared::Message::setEdited(bool p_edited) {
edited = p_edited;
}
void Shared::Message::serialize(QDataStream& data) const
{
data << jFrom;
data << rFrom;
data << jTo;
data << rTo;
data << id;
data << body;
data << time;
data << thread;
data << (quint8)type;
data << outgoing;
data << forwarded;
data << oob;
data << (quint8)state;
data << edited;
if (state == State::error) {
data << errorText;
QDataStream& operator<<(QDataStream& out, const Shared::Message& info) {
out << info.jFrom;
out << info.rFrom;
out << info.jTo;
out << info.rTo;
out << info.id;
out << info.body;
out << info.time;
out << info.thread;
out << (quint8)info.type;
out << info.outgoing;
out << info.forwarded;
out << info.oob;
out << (quint8)info.state;
out << info.edited;
if (info.state == Shared::Message::State::error)
out << info.errorText;
if (info.edited) {
out << info.originalMessage;
out << info.lastModified;
}
if (edited) {
data << originalMessage;
data << lastModified;
}
data << stanzaId;
data << attachPath;
out << info.stanzaId;
out << info.attachPath;
return out;
}
void Shared::Message::deserialize(QDataStream& data)
{
data >> jFrom;
data >> rFrom;
data >> jTo;
data >> rTo;
data >> id;
data >> body;
data >> time;
data >> thread;
QDataStream & operator>>(QDataStream& in, Shared::Message& info) {
in >> info.jFrom;
in >> info.rFrom;
in >> info.jTo;
in >> info.rTo;
in >> info.id;
in >> info.body;
in >> info.time;
in >> info.thread;
quint8 t;
data >> t;
type = static_cast<Type>(t);
data >> outgoing;
data >> forwarded;
data >> oob;
in >> t;
info.type = static_cast<Shared::Message::Type>(t);
in >> info.outgoing;
in >> info.forwarded;
in >> info.oob;
quint8 s;
data >> s;
state = static_cast<State>(s);
data >> edited;
if (state == State::error) {
data >> errorText;
in >> s;
info.state = static_cast<Shared::Message::State>(s);
in >> info.edited;
if (info.state == Shared::Message::State::error)
in >> info.errorText;
if (info.edited) {
in >> info.originalMessage;
in >> info.lastModified;
}
if (edited) {
data >> originalMessage;
data >> lastModified;
}
data >> stanzaId;
data >> attachPath;
in >> info.stanzaId;
in >> info.attachPath;
return in;
}
bool Shared::Message::change(const QMap<QString, QVariant>& data)
{
QMap<QString, QVariant>::const_iterator itr = data.find("state");
if (itr != data.end()) {
if (itr != data.end())
setState(static_cast<State>(itr.value().toUInt()));
}
itr = data.find("outOfBandUrl");
if (itr != data.end()) {
if (itr != data.end())
setOutOfBandUrl(itr.value().toString());
}
itr = data.find("attachPath");
if (itr != data.end()) {
if (itr != data.end())
setAttachPath(itr.value().toString());
}
if (state == State::error) {
itr = data.find("errorText");
if (itr != data.end()) {
if (itr != data.end())
setErrorText(itr.value().toString());
}
}
bool idChanged = false;
@ -386,9 +342,8 @@ bool Shared::Message::change(const QMap<QString, QVariant>& data)
QString newId = itr.value().toString();
if (stanzaId != newId) {
setStanzaId(newId);
if (id.size() == 0) {
if (id.size() == 0)
idChanged = true;
}
}
}
@ -398,15 +353,15 @@ bool Shared::Message::change(const QMap<QString, QVariant>& data)
if (body != b) {
QMap<QString, QVariant>::const_iterator dItr = data.find("stamp");
QDateTime correctionDate;
if (dItr != data.end()) {
if (dItr != data.end())
correctionDate = dItr.value().toDateTime();
} else {
else
correctionDate = QDateTime::currentDateTimeUtc(); //in case there is no information about time of this correction it's applied
}
if (!edited || lastModified < correctionDate) {
if (!edited) {
if (!edited)
originalMessage = body;
}
lastModified = correctionDate;
setBody(b);
setEdited(true);
@ -416,57 +371,47 @@ bool Shared::Message::change(const QMap<QString, QVariant>& data)
QMap<QString, QVariant>::const_iterator dItr = data.find("stamp");
if (dItr != data.end()) {
QDateTime ntime = dItr.value().toDateTime();
if (time != ntime) {
if (time != ntime)
setTime(ntime);
}
}
}
return idChanged;
}
void Shared::Message::setCurrentTime()
{
void Shared::Message::setCurrentTime() {
time = QDateTime::currentDateTimeUtc();
}
QString Shared::Message::getOutOfBandUrl() const
{
QString Shared::Message::getOutOfBandUrl() const {
return oob;
}
bool Shared::Message::hasOutOfBandUrl() const
{
bool Shared::Message::hasOutOfBandUrl() const {
return oob.size() > 0;
}
void Shared::Message::setOutOfBandUrl(const QString& url)
{
void Shared::Message::setOutOfBandUrl(const QString& url) {
oob = url;
}
bool Shared::Message::storable() const
{
bool Shared::Message::storable() const {
return id.size() > 0 && (body.size() > 0 || oob.size() > 0 || attachPath.size() > 0);
}
void Shared::Message::setStanzaId(const QString& sid)
{
void Shared::Message::setStanzaId(const QString& sid) {
stanzaId = sid;
}
QString Shared::Message::getStanzaId() const
{
QString Shared::Message::getStanzaId() const {
return stanzaId;
}
QString Shared::Message::getAttachPath() const
{
QString Shared::Message::getAttachPath() const {
return attachPath;
}
void Shared::Message::setAttachPath(const QString& path)
{
void Shared::Message::setAttachPath(const QString& path) {
attachPath = path;
}
@ -474,18 +419,15 @@ Shared::Message::Change::Change(const QMap<QString, QVariant>& _data):
data(_data),
idModified(false) {}
void Shared::Message::Change::operator()(Shared::Message& msg)
{
void Shared::Message::Change::operator()(Shared::Message& msg) {
idModified = msg.change(data);
}
void Shared::Message::Change::operator()(Shared::Message* msg)
{
void Shared::Message::Change::operator()(Shared::Message* msg) {
idModified = msg->change(data);
}
bool Shared::Message::Change::hasIdBeenModified() const
{
bool Shared::Message::Change::hasIdBeenModified() const {
return idModified;
}

View File

@ -25,12 +25,20 @@
#include <QMap>
#include <QDataStream>
namespace Shared {
class Message;
}
QDataStream& operator << (QDataStream& out, const Shared::Message& info);
QDataStream& operator >> (QDataStream& in, Shared::Message& info);
namespace Shared {
/**
* @todo write docs
*/
class Message {
friend QDataStream& ::operator << (QDataStream& out, const Shared::Message& info);
friend QDataStream& ::operator >> (QDataStream& in, Shared::Message& info);
public:
enum Type {
error,
@ -116,9 +124,6 @@ public:
QString getStanzaId() const;
QString getAttachPath() const;
void serialize(QDataStream& data) const;
void deserialize(QDataStream& data);
private:
QString jFrom;
QString rFrom;