2019-04-14 20:37:02 +00:00
|
|
|
/*
|
2019-08-14 14:54:46 +00:00
|
|
|
* Squawk messenger.
|
2019-04-14 20:37:02 +00:00
|
|
|
* 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_ARCHIVE_H
|
|
|
|
#define CORE_ARCHIVE_H
|
|
|
|
|
|
|
|
#include <QObject>
|
2019-10-15 19:25:40 +00:00
|
|
|
#include <QCryptographicHash>
|
|
|
|
#include <QMimeDatabase>
|
|
|
|
#include <QMimeType>
|
|
|
|
|
2020-03-27 20:59:30 +00:00
|
|
|
#include "global.h"
|
|
|
|
#include "exception.h"
|
2019-04-16 16:29:24 +00:00
|
|
|
#include <lmdb.h>
|
2019-04-21 19:17:04 +00:00
|
|
|
#include <list>
|
2019-04-14 20:37:02 +00:00
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
class Archive : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2019-12-30 20:22:04 +00:00
|
|
|
class AvatarInfo;
|
|
|
|
|
2019-04-15 22:35:09 +00:00
|
|
|
Archive(const QString& jid, QObject* parent = 0);
|
2019-04-14 20:37:02 +00:00
|
|
|
~Archive();
|
|
|
|
|
|
|
|
void open(const QString& account);
|
2019-04-17 20:08:56 +00:00
|
|
|
void close();
|
2019-04-14 20:37:02 +00:00
|
|
|
|
2019-04-21 19:17:04 +00:00
|
|
|
bool addElement(const Shared::Message& message);
|
2019-04-26 12:52:34 +00:00
|
|
|
unsigned int addElements(const std::list<Shared::Message>& messages);
|
2019-04-15 22:35:09 +00:00
|
|
|
Shared::Message getElement(const QString& id);
|
2020-03-27 20:59:30 +00:00
|
|
|
void changeMessage(const QString& id, const QMap<QString, QVariant>& data);
|
2019-04-15 22:35:09 +00:00
|
|
|
Shared::Message oldest();
|
|
|
|
QString oldestId();
|
|
|
|
Shared::Message newest();
|
|
|
|
QString newestId();
|
2019-04-14 20:37:02 +00:00
|
|
|
void clear();
|
2019-04-15 22:35:09 +00:00
|
|
|
long unsigned int size() const;
|
2019-04-21 19:17:04 +00:00
|
|
|
std::list<Shared::Message> getBefore(int count, const QString& id);
|
|
|
|
bool isFromTheBeginning();
|
2019-05-15 17:36:37 +00:00
|
|
|
void setFromTheBeginning(bool is);
|
2019-12-30 20:22:04 +00:00
|
|
|
bool setAvatar(const QByteArray& data, bool generated = false, const QString& resource = "");
|
|
|
|
AvatarInfo getAvatarInfo(const QString& resource = "") const;
|
|
|
|
bool readAvatarInfo(AvatarInfo& target, const QString& resource = "") const;
|
2019-04-14 20:37:02 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
const QString jid;
|
|
|
|
|
2019-04-15 22:35:09 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2019-09-11 15:03:52 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2019-12-30 20:22:04 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2019-09-11 15:03:52 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2019-12-30 20:22:04 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2019-04-14 20:37:02 +00:00
|
|
|
private:
|
|
|
|
bool opened;
|
2019-04-21 19:17:04 +00:00
|
|
|
bool fromTheBeginning;
|
2019-04-16 16:29:24 +00:00
|
|
|
MDB_env* environment;
|
|
|
|
MDB_dbi main;
|
|
|
|
MDB_dbi order;
|
2019-04-21 19:17:04 +00:00
|
|
|
MDB_dbi stats;
|
2019-12-30 20:22:04 +00:00
|
|
|
MDB_dbi avatars;
|
2019-04-21 19:17:04 +00:00
|
|
|
|
2019-10-14 20:18:51 +00:00
|
|
|
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);
|
2019-12-30 20:22:04 +00:00
|
|
|
bool readAvatarInfo(AvatarInfo& target, const std::string& res, MDB_txn* txn) const;
|
2019-05-15 17:36:37 +00:00
|
|
|
void printOrder();
|
|
|
|
void printKeys();
|
2019-12-30 20:22:04 +00:00
|
|
|
bool dropAvatar(const std::string& resource);
|
2020-01-07 09:26:07 +00:00
|
|
|
Shared::Message getMessage(const std::string& id, MDB_txn* txn);
|
2020-03-28 14:05:49 +00:00
|
|
|
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);
|
2019-04-14 20:37:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CORE_ARCHIVE_H
|