50
.gdb_history
Normal file
50
.gdb_history
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
b ContactDataStore::dump()
|
||||||
|
r
|
||||||
|
n
|
||||||
|
q
|
||||||
|
b ContactDataStore::dump()
|
||||||
|
r
|
||||||
|
n
|
||||||
|
c
|
||||||
|
./build.sh
|
||||||
|
$(./build.sh)
|
||||||
|
$(./build.sh)
|
||||||
|
q
|
||||||
|
r
|
||||||
|
q
|
||||||
|
r
|
||||||
|
b ContactDataStore::dump()
|
||||||
|
r
|
||||||
|
n
|
||||||
|
b ContactItem::toJson()
|
||||||
|
r
|
||||||
|
c
|
||||||
|
n
|
||||||
|
q
|
||||||
|
b ContactItem::toJson()
|
||||||
|
r
|
||||||
|
n
|
||||||
|
c
|
||||||
|
c
|
||||||
|
c
|
||||||
|
c
|
||||||
|
c
|
||||||
|
c
|
||||||
|
c
|
||||||
|
c
|
||||||
|
q
|
||||||
|
b FileSystem::writeContacts(QString file, json j)
|
||||||
|
b FileSystem::writeContacts
|
||||||
|
r
|
||||||
|
q
|
||||||
|
b FileSystem::writeContacts
|
||||||
|
r
|
||||||
|
b ContactDataStore::dump()
|
||||||
|
r
|
||||||
|
c
|
||||||
|
n
|
||||||
|
q
|
||||||
|
b FileSystem::writeContacts
|
||||||
|
r
|
||||||
|
n
|
||||||
|
q
|
||||||
2
peda-session-SilentDragonLite.txt
Normal file
2
peda-session-SilentDragonLite.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
break FileSystem::writeContacts
|
||||||
|
|
||||||
@@ -75,11 +75,14 @@ SOURCES += \
|
|||||||
src/DataStore/DataStore.cpp \
|
src/DataStore/DataStore.cpp \
|
||||||
src/DataStore/ChatDataStore.cpp \
|
src/DataStore/ChatDataStore.cpp \
|
||||||
src/DataStore/SietchDataStore.cpp \
|
src/DataStore/SietchDataStore.cpp \
|
||||||
|
src/DataStore/ContactDataStore.cpp \
|
||||||
src/Model/ChatItem.cpp \
|
src/Model/ChatItem.cpp \
|
||||||
src/Model/ContactRequestChatItem.cpp \
|
src/Model/ContactRequestChatItem.cpp \
|
||||||
src/Model/ContactItem.cpp \
|
src/Model/ContactItem.cpp \
|
||||||
src/Chat/Helper/ChatIDGenerator.cpp \
|
src/Chat/Helper/ChatIDGenerator.cpp \
|
||||||
src/Chat/Chat.cpp
|
src/Chat/Chat.cpp \
|
||||||
|
src/FileSystem/FileSystem.cpp \
|
||||||
|
src/Crypto/FileEncryption.cpp
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
src/firsttimewizard.h \
|
src/firsttimewizard.h \
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ void Chat::renderChatBox(Ui::MainWindow *ui, QListView *view)
|
|||||||
// ui->lcdNumber->setStyleSheet("background-color: red");
|
// ui->lcdNumber->setStyleSheet("background-color: red");
|
||||||
// ui->lcdNumber->setPalette(Qt::red);
|
// ui->lcdNumber->setPalette(Qt::red);
|
||||||
// ui->lcdNumber->display("1");
|
// ui->lcdNumber->display("1");
|
||||||
|
DataStore::getChatDataStore()->dump(); // test to see if the chat items in datastore are correctly dumped to json
|
||||||
for (auto &p : AddressBook::getInstance()->getAllAddressLabels())
|
for (auto &p : AddressBook::getInstance()->getAllAddressLabels())
|
||||||
{
|
{
|
||||||
for (auto &c : DataStore::getChatDataStore()->getAllMemos())
|
for (auto &c : DataStore::getChatDataStore()->getAllMemos())
|
||||||
|
|||||||
102
src/Crypto/FileEncryption.cpp
Normal file
102
src/Crypto/FileEncryption.cpp
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
#include "FileEncryption.h"
|
||||||
|
|
||||||
|
void FileEncryption::showConfig()
|
||||||
|
{
|
||||||
|
qInfo() << FILEENCRYPTION_CHUNK_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int FileEncryption::encrypt(QString target_file, QString source_file, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES])
|
||||||
|
{
|
||||||
|
unsigned char buf_in[FILEENCRYPTION_CHUNK_SIZE];
|
||||||
|
unsigned char buf_out[FILEENCRYPTION_CHUNK_SIZE + crypto_secretstream_xchacha20poly1305_ABYTES];
|
||||||
|
unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES];
|
||||||
|
crypto_secretstream_xchacha20poly1305_state st;
|
||||||
|
FILE *fp_t, *fp_s;
|
||||||
|
unsigned long long out_len;
|
||||||
|
size_t rlen;
|
||||||
|
int eof;
|
||||||
|
unsigned char tag;
|
||||||
|
|
||||||
|
fp_s = fopen(source_file.toStdString().c_str(), "rb");
|
||||||
|
fp_t = fopen(target_file.toStdString().c_str(), "wb");
|
||||||
|
crypto_secretstream_xchacha20poly1305_init_push(&st, header, key);
|
||||||
|
fwrite(header, 1, sizeof header, fp_t);
|
||||||
|
do
|
||||||
|
{
|
||||||
|
rlen = fread(buf_in, 1, sizeof buf_in, fp_s);
|
||||||
|
eof = feof(fp_s);
|
||||||
|
tag = eof ? crypto_secretstream_xchacha20poly1305_TAG_FINAL : 0;
|
||||||
|
crypto_secretstream_xchacha20poly1305_push(
|
||||||
|
&st,
|
||||||
|
buf_out,
|
||||||
|
&out_len,
|
||||||
|
buf_in,
|
||||||
|
rlen,
|
||||||
|
NULL,
|
||||||
|
0,
|
||||||
|
tag
|
||||||
|
);
|
||||||
|
|
||||||
|
fwrite(buf_out, 1, (size_t) out_len, fp_t);
|
||||||
|
}
|
||||||
|
while (! eof);
|
||||||
|
|
||||||
|
fclose(fp_t);
|
||||||
|
fclose(fp_s);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int FileEncryption::decrypt(QString target_file, QString source_file, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES])
|
||||||
|
{
|
||||||
|
unsigned char buf_in[FILEENCRYPTION_CHUNK_SIZE + crypto_secretstream_xchacha20poly1305_ABYTES];
|
||||||
|
unsigned char buf_out[FILEENCRYPTION_CHUNK_SIZE];
|
||||||
|
unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES];
|
||||||
|
crypto_secretstream_xchacha20poly1305_state st;
|
||||||
|
FILE *fp_t, *fp_s;
|
||||||
|
unsigned long long out_len;
|
||||||
|
size_t rlen;
|
||||||
|
int eof;
|
||||||
|
int ret = -1;
|
||||||
|
unsigned char tag;
|
||||||
|
|
||||||
|
fp_s = fopen(source_file.toStdString().c_str(), "rb");
|
||||||
|
fp_t = fopen(target_file.toStdString().c_str(), "wb");
|
||||||
|
fread(header, 1, sizeof header, fp_s);
|
||||||
|
if (crypto_secretstream_xchacha20poly1305_init_pull(&st, header, key) != 0)
|
||||||
|
{
|
||||||
|
goto ret; /* incomplete header */
|
||||||
|
}
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
rlen = fread(buf_in, 1, sizeof buf_in, fp_s);
|
||||||
|
eof = feof(fp_s);
|
||||||
|
if (crypto_secretstream_xchacha20poly1305_pull(
|
||||||
|
&st,
|
||||||
|
buf_out,
|
||||||
|
&out_len,
|
||||||
|
&tag,
|
||||||
|
buf_in,
|
||||||
|
rlen,
|
||||||
|
NULL,
|
||||||
|
0
|
||||||
|
) != 0)
|
||||||
|
{
|
||||||
|
goto ret; /* corrupted chunk */
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tag == crypto_secretstream_xchacha20poly1305_TAG_FINAL && ! eof)
|
||||||
|
{
|
||||||
|
goto ret; /* premature end (end of file reached before the end of the stream) */
|
||||||
|
}
|
||||||
|
|
||||||
|
fwrite(buf_out, 1, (size_t) out_len, fp_t);
|
||||||
|
}
|
||||||
|
while (! eof);
|
||||||
|
ret = 0;
|
||||||
|
|
||||||
|
ret:
|
||||||
|
fclose(fp_t);
|
||||||
|
fclose(fp_s);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
18
src/Crypto/FileEncryption.h
Normal file
18
src/Crypto/FileEncryption.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef FILEENCRYPTION_H
|
||||||
|
#define FILEENCRYPTION_H
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sodium.h>
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
|
#define FILEENCRYPTION_CHUNK_SIZE 4096
|
||||||
|
|
||||||
|
class FileEncryption
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static void showConfig();
|
||||||
|
static int encrypt(QString target_file, QString source_file, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]);
|
||||||
|
static int decrypt(QString target_file, QString source_file, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -32,7 +32,15 @@ ChatItem ChatDataStore::getData(QString key)
|
|||||||
|
|
||||||
QString ChatDataStore::dump()
|
QString ChatDataStore::dump()
|
||||||
{
|
{
|
||||||
return "";
|
json chats;
|
||||||
|
chats["count"] = this->data.size();
|
||||||
|
json j = {};
|
||||||
|
for (auto &c: this->data)
|
||||||
|
{
|
||||||
|
j.push_back(c.second.toJson());
|
||||||
|
}
|
||||||
|
chats["chatitems"] = j;
|
||||||
|
return QString::fromStdString(chats.dump());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<QString, ChatItem> ChatDataStore::getAllRawChatItems()
|
std::map<QString, ChatItem> ChatDataStore::getAllRawChatItems()
|
||||||
|
|||||||
50
src/DataStore/ContactDataStore.cpp
Normal file
50
src/DataStore/ContactDataStore.cpp
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
// Copyright 2019-2020 The Hush developers
|
||||||
|
// GPLv3
|
||||||
|
|
||||||
|
#include "ContactDataStore.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
ContactDataStore* ContactDataStore::getInstance()
|
||||||
|
{
|
||||||
|
if(!ContactDataStore::instanced)
|
||||||
|
{
|
||||||
|
ContactDataStore::instanced = true;
|
||||||
|
ContactDataStore::instance = new ContactDataStore();
|
||||||
|
}
|
||||||
|
|
||||||
|
return ContactDataStore::instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ContactDataStore::clear()
|
||||||
|
{
|
||||||
|
this->data.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void ContactDataStore::setData(QString key, ContactItem value)
|
||||||
|
{
|
||||||
|
this->data[key] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
ContactItem ContactDataStore::getData(QString key)
|
||||||
|
{
|
||||||
|
return this->data[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
QString ContactDataStore::dump()
|
||||||
|
{
|
||||||
|
json contacts;
|
||||||
|
contacts["count"] = this->data.size();
|
||||||
|
json j = {};
|
||||||
|
for (auto &c: this->data)
|
||||||
|
{
|
||||||
|
qDebug() << c.second.toQTString();
|
||||||
|
c.second.toJson();
|
||||||
|
j.push_back(c.second.toJson());
|
||||||
|
}
|
||||||
|
contacts["contacts"] = j;
|
||||||
|
return QString::fromStdString(contacts.dump(4));
|
||||||
|
}
|
||||||
|
|
||||||
|
ContactDataStore* ContactDataStore::instance = nullptr;
|
||||||
|
bool ContactDataStore::instanced = false;
|
||||||
34
src/DataStore/ContactDataStore.h
Normal file
34
src/DataStore/ContactDataStore.h
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#ifndef CONTACTDATASTORE_H
|
||||||
|
#define CONTACTDATASTORE_H
|
||||||
|
#include "../Model/ContactItem.h"
|
||||||
|
#include <string>
|
||||||
|
using json = nlohmann::json;
|
||||||
|
|
||||||
|
class ContactDataStore
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
static bool instanced;
|
||||||
|
static ContactDataStore* instance;
|
||||||
|
std::map<QString, ContactItem> data;
|
||||||
|
ContactDataStore()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
static ContactDataStore* getInstance();
|
||||||
|
void clear();
|
||||||
|
void setData(QString key, ContactItem value);
|
||||||
|
ContactItem getData(QString key);
|
||||||
|
QString dump();
|
||||||
|
|
||||||
|
~ContactDataStore()
|
||||||
|
{
|
||||||
|
ContactDataStore::instanced = false;
|
||||||
|
ContactDataStore::instance = nullptr;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -9,3 +9,8 @@ ChatDataStore* DataStore::getChatDataStore()
|
|||||||
{
|
{
|
||||||
return ChatDataStore::getInstance();
|
return ChatDataStore::getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ContactDataStore* DataStore::getContactDataStore()
|
||||||
|
{
|
||||||
|
return ContactDataStore::getInstance();
|
||||||
|
}
|
||||||
@@ -3,12 +3,14 @@
|
|||||||
|
|
||||||
#include "SietchDataStore.h"
|
#include "SietchDataStore.h"
|
||||||
#include "ChatDataStore.h"
|
#include "ChatDataStore.h"
|
||||||
|
#include "ContactDataStore.h"
|
||||||
|
|
||||||
class DataStore
|
class DataStore
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static SietchDataStore* getSietchDataStore();
|
static SietchDataStore* getSietchDataStore();
|
||||||
static ChatDataStore* getChatDataStore();
|
static ChatDataStore* getChatDataStore();
|
||||||
|
static ContactDataStore* getContactDataStore();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
126
src/FileSystem/FileSystem.cpp
Normal file
126
src/FileSystem/FileSystem.cpp
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
#include "FileSystem.h"
|
||||||
|
|
||||||
|
#include <QString>
|
||||||
|
#include <QList>
|
||||||
|
|
||||||
|
FileSystem::FileSystem()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
FileSystem* FileSystem::getInstance()
|
||||||
|
{
|
||||||
|
if(!FileSystem::instanced)
|
||||||
|
{
|
||||||
|
FileSystem::instanced = true;
|
||||||
|
FileSystem::instance = new FileSystem();
|
||||||
|
FileEncryption::showConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
return FileSystem::instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<ContactItem> FileSystem::readContacts(QString file)
|
||||||
|
{
|
||||||
|
return this->readContactsOldFormat(file); //will be called if addresses are in the old dat-format
|
||||||
|
|
||||||
|
QFile _file(file);
|
||||||
|
if (_file.exists())
|
||||||
|
{
|
||||||
|
std::ifstream f(file.toStdString().c_str(), std::ios::binary);
|
||||||
|
if(f.is_open())
|
||||||
|
{
|
||||||
|
std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(f), {});
|
||||||
|
//todo covert to string to use is as json to feed the data store in addressbook
|
||||||
|
}
|
||||||
|
|
||||||
|
f.close();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
qInfo() << file << "not exist";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FileSystem::writeContacts(QString file, QString data)
|
||||||
|
{
|
||||||
|
qDebug() << data;
|
||||||
|
QFile _file(file);
|
||||||
|
if (_file.exists())
|
||||||
|
{
|
||||||
|
std::ofstream f(file.toStdString().c_str());
|
||||||
|
if(f.is_open())
|
||||||
|
{
|
||||||
|
//ENCRYPT HERE
|
||||||
|
|
||||||
|
f << data.toStdString();
|
||||||
|
}
|
||||||
|
|
||||||
|
f.close();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
qInfo() << file << "not exist";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FileSystem::writeContactsOldFormat(QString file, QList<ContactItem> contacts)
|
||||||
|
{
|
||||||
|
QFile _file(file);
|
||||||
|
_file.open(QIODevice::ReadWrite | QIODevice::Truncate);
|
||||||
|
QDataStream out(&_file); // we will serialize the data into the file
|
||||||
|
QList<QList<QString>> _contacts;
|
||||||
|
for(auto &item: contacts)
|
||||||
|
{
|
||||||
|
QList<QString> c;
|
||||||
|
c.push_back(item.getName());
|
||||||
|
c.push_back(item.getPartnerAddress());
|
||||||
|
c.push_back(item.getMyAddress());
|
||||||
|
c.push_back(item.getCid());
|
||||||
|
c.push_back(item.getAvatar());
|
||||||
|
_contacts.push_back(c);
|
||||||
|
}
|
||||||
|
out << QString("v1") << _contacts;
|
||||||
|
_file.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<ContactItem> FileSystem::readContactsOldFormat(QString file)
|
||||||
|
{
|
||||||
|
QList<ContactItem> contacts;
|
||||||
|
QFile _file(file);
|
||||||
|
if (_file.exists())
|
||||||
|
{
|
||||||
|
contacts.clear();
|
||||||
|
_file.open(QIODevice::ReadOnly);
|
||||||
|
QDataStream in(&_file); // read the data serialized from the file
|
||||||
|
QString version;
|
||||||
|
in >> version;
|
||||||
|
qDebug() << "Read " << version << " Hush contacts from disk...";
|
||||||
|
qDebug() << "Detected old addressbook format";
|
||||||
|
QList<QList<QString>> stuff;
|
||||||
|
in >> stuff;
|
||||||
|
//qDebug() << "Stuff: " << stuff;
|
||||||
|
for (int i=0; i < stuff.size(); i++)
|
||||||
|
{
|
||||||
|
ContactItem contact = ContactItem(stuff[i][0],stuff[i][1], stuff[i][2], stuff[i][3],stuff[i][4]);
|
||||||
|
contacts.push_back(contact);
|
||||||
|
}
|
||||||
|
|
||||||
|
_file.close();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
qDebug() << "No Hush contacts found on disk!";
|
||||||
|
}
|
||||||
|
|
||||||
|
return contacts;
|
||||||
|
}
|
||||||
|
|
||||||
|
FileSystem::~FileSystem()
|
||||||
|
{
|
||||||
|
this->instance = nullptr;
|
||||||
|
this->instanced = false;
|
||||||
|
delete this->instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
FileSystem *FileSystem::instance = nullptr;
|
||||||
|
bool FileSystem::instanced = false;
|
||||||
29
src/FileSystem/FileSystem.h
Normal file
29
src/FileSystem/FileSystem.h
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#ifndef FILESYSTEM_H
|
||||||
|
#define FILESYSTEM_H
|
||||||
|
|
||||||
|
#include <QString>
|
||||||
|
#include <QList>
|
||||||
|
#include "../Model/ContactItem.h"
|
||||||
|
#include "../Crypto/FileEncryption.h"
|
||||||
|
#include <fstream>
|
||||||
|
using json = nlohmann::json;
|
||||||
|
class FileSystem
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
static bool instanced;
|
||||||
|
static FileSystem* instance;
|
||||||
|
FileSystem();
|
||||||
|
|
||||||
|
public:
|
||||||
|
static FileSystem* getInstance();
|
||||||
|
QList<ContactItem> readContacts(QString file);
|
||||||
|
void writeContacts(QString file, QString data);
|
||||||
|
|
||||||
|
//converter
|
||||||
|
QList<ContactItem> readContactsOldFormat(QString file);
|
||||||
|
void writeContactsOldFormat(QString file, QList<ContactItem> contacts);
|
||||||
|
~FileSystem();
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
11
src/Logger/LogContext.h
Normal file
11
src/Logger/LogContext.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef LOGCONTEXT_H
|
||||||
|
#define LOGCONTEXT_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
class LogContext
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual void log(std::string message) {};
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
18
src/Logger/LogCrtitical.h
Normal file
18
src/Logger/LogCrtitical.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef LOGCRITICAL_H
|
||||||
|
#define LOGCRITICAL_H
|
||||||
|
|
||||||
|
#include "LogType.h"
|
||||||
|
#include "LogStrategy.h"
|
||||||
|
#include "LogWriter.h"
|
||||||
|
|
||||||
|
class LogCritical : public LogStrategy
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void log(std::string message)
|
||||||
|
{
|
||||||
|
LogWriter* lw = LogWriter::getInstance();
|
||||||
|
lw->write(LogType::CRITICAL, message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
18
src/Logger/LogDebug.h
Normal file
18
src/Logger/LogDebug.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef LOGDEBUG_H
|
||||||
|
#define LOGDEBUG_H
|
||||||
|
|
||||||
|
#include "LogType.h"
|
||||||
|
#include "LogStrategy.h"
|
||||||
|
#include "LogWriter.h"
|
||||||
|
|
||||||
|
class LogDebug : public LogStrategy
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void log(std::string message)
|
||||||
|
{
|
||||||
|
LogWriter* lw = LogWriter::getInstance();
|
||||||
|
lw->write(LogType::DEBUG, message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
18
src/Logger/LogError.h
Normal file
18
src/Logger/LogError.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef LOGERROR_H
|
||||||
|
#define LOGERROR_H
|
||||||
|
|
||||||
|
#include "LogType.h"
|
||||||
|
#include "LogStrategy.h"
|
||||||
|
#include "LogWriter.h"
|
||||||
|
|
||||||
|
class LogError : public LogStrategy
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void log(std::string message)
|
||||||
|
{
|
||||||
|
LogWriter* lw = LogWriter::getInstance();
|
||||||
|
lw->write(LogType::ERROR, message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
18
src/Logger/LogFatal.h
Normal file
18
src/Logger/LogFatal.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef LOGFATAL_H
|
||||||
|
#define LOGFATAL_H
|
||||||
|
|
||||||
|
#include "LogType.h"
|
||||||
|
#include "LogStrategy.h"
|
||||||
|
#include "LogWriter.h"
|
||||||
|
|
||||||
|
class LogFatal : public LogStrategy
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void log(std::string message)
|
||||||
|
{
|
||||||
|
LogWriter* lw = LogWriter::getInstance();
|
||||||
|
lw->write(LogType::FATAL, message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
18
src/Logger/LogInfo.h
Normal file
18
src/Logger/LogInfo.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef LOGINFO_H
|
||||||
|
#define LOGINFO_H
|
||||||
|
|
||||||
|
#include "LogType.h"
|
||||||
|
#include "LogStrategy.h"
|
||||||
|
#include "LogWriter.h"
|
||||||
|
|
||||||
|
class LogInfo : public LogStrategy
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void log(std::string message)
|
||||||
|
{
|
||||||
|
LogWriter* lw = LogWriter::getInstance();
|
||||||
|
lw->write(LogType::INFO, message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
11
src/Logger/LogStrategy.h
Normal file
11
src/Logger/LogStrategy.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef LOGSTRATEGY_H
|
||||||
|
#define LOGSTRATEGY_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
class LogStrategy
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual void log(std::string message) {};
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
18
src/Logger/LogSuccess.h
Normal file
18
src/Logger/LogSuccess.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef LOGSUCCESS_H
|
||||||
|
#define LOGSUCCESS_H
|
||||||
|
|
||||||
|
#include "LogType.h"
|
||||||
|
#include "LogStrategy.h"
|
||||||
|
#include "LogWriter.h"
|
||||||
|
|
||||||
|
class LogSuccess : public LogStrategy
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void log(std::string message)
|
||||||
|
{
|
||||||
|
LogWriter* lw = LogWriter::getInstance();
|
||||||
|
lw->write(LogType::SUCCESS, message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
47
src/Logger/LogType.h
Normal file
47
src/Logger/LogType.h
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#ifndef LOGTYPE_H
|
||||||
|
#define LOGTYPE_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class LogType
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
enum TYPE {
|
||||||
|
INFO = 0,
|
||||||
|
DEBUG = 1,
|
||||||
|
SUCCESS = 2,
|
||||||
|
WARNING = 3,
|
||||||
|
ERROR = 4,
|
||||||
|
FATAL = 5,
|
||||||
|
CRITICAL = 6
|
||||||
|
};
|
||||||
|
|
||||||
|
static std::string enum2String(int type)
|
||||||
|
{
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
default:
|
||||||
|
case 0:
|
||||||
|
return "INFO";
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
return "DEBUG";
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
return "SUCCESS";
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
return "WARNING";
|
||||||
|
|
||||||
|
case 4:
|
||||||
|
return "ERROR";
|
||||||
|
|
||||||
|
case 5:
|
||||||
|
return "FATAL";
|
||||||
|
|
||||||
|
case 6:
|
||||||
|
return "CRITICAL";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
#endif
|
||||||
18
src/Logger/LogWarning.h
Normal file
18
src/Logger/LogWarning.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef LOGWARNING_H
|
||||||
|
#define LOGWARNING_H
|
||||||
|
|
||||||
|
#include "LogType.h"
|
||||||
|
#include "LogStrategy.h"
|
||||||
|
#include "LogWriter.h"
|
||||||
|
|
||||||
|
class LogWarning : public LogStrategy
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void log(std::string message)
|
||||||
|
{
|
||||||
|
LogWriter* lw = LogWriter::getInstance();
|
||||||
|
lw->write(LogType::WARNING, message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
35
src/Logger/LogWriter.cpp
Normal file
35
src/Logger/LogWriter.cpp
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#include "LogWriter.h"
|
||||||
|
|
||||||
|
LogWriter* LogWriter::getInstance()
|
||||||
|
{
|
||||||
|
if(instance == nullptr)
|
||||||
|
instance = new LogWriter();
|
||||||
|
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LogWriter::setLogFile(std::string file)
|
||||||
|
{
|
||||||
|
this->logfile = file;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LogWriter::write(LogType::TYPE type, std::string message)
|
||||||
|
{
|
||||||
|
std::ofstream writer(this->logfile, std::ios::out | std::ios::app);
|
||||||
|
if(writer.good())
|
||||||
|
{
|
||||||
|
time_t now = time(0);
|
||||||
|
tm *ltm = localtime(&now);
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "[" << LogType::enum2String(type) << "] " <<
|
||||||
|
ltm->tm_mon << "-" <<
|
||||||
|
ltm->tm_mday << "-" <<
|
||||||
|
(1900 + ltm->tm_year) << " " <<
|
||||||
|
ltm->tm_hour << ":" <<
|
||||||
|
ltm->tm_min << ":" <<
|
||||||
|
ltm->tm_sec << " > " << message;
|
||||||
|
writer << ss.str() << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
22
src/Logger/LogWriter.h
Normal file
22
src/Logger/LogWriter.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#ifndef LOGWRITER_H
|
||||||
|
#define LOGWRITER_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <ctime>
|
||||||
|
#include "LogType.h"
|
||||||
|
|
||||||
|
class LogWriter
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static LogWriter* getInstance();
|
||||||
|
std::string logfile = "";
|
||||||
|
void setLogFile(std::string file);
|
||||||
|
void write(LogType::TYPE t, std::string message);
|
||||||
|
|
||||||
|
private:
|
||||||
|
static LogWriter* instance;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
25
src/Logger/Logger.h
Normal file
25
src/Logger/Logger.h
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#ifndef LOGGER_H
|
||||||
|
#define LOGGER_H
|
||||||
|
|
||||||
|
#include "LogContext.h"
|
||||||
|
#include "LogStrategy.h"
|
||||||
|
#include "LogWriter.h"
|
||||||
|
|
||||||
|
class Logger : LogContext
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
LogStrategy * strategy = nullptr;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Logger(LogStrategy * strategy)
|
||||||
|
{
|
||||||
|
this->strategy = strategy;
|
||||||
|
}
|
||||||
|
|
||||||
|
void log(std::string message)
|
||||||
|
{
|
||||||
|
this->strategy->log(message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
LogWriter* LogWriter::instance = nullptr;
|
||||||
|
#endif
|
||||||
84
src/Logger/SimpleLogger.h
Normal file
84
src/Logger/SimpleLogger.h
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
#ifndef SIMPLELOGGER_H
|
||||||
|
#define SIMPLELOGGER_H
|
||||||
|
|
||||||
|
#include "Logger.h"
|
||||||
|
#include "LogInfo.h"
|
||||||
|
#include "LogDebug.h"
|
||||||
|
#include "LogSuccess.h"
|
||||||
|
#include "LogWarning.h"
|
||||||
|
#include "LogError.h"
|
||||||
|
#include "LogFatal.h"
|
||||||
|
#include "LogCrtitical.h"
|
||||||
|
#include "LogWriter.h"
|
||||||
|
|
||||||
|
class SimpleLogger
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
SimpleLogger()
|
||||||
|
{
|
||||||
|
LogWriter::getInstance()->setLogFile("log.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
SimpleLogger(std::string logFile)
|
||||||
|
{
|
||||||
|
LogWriter::getInstance()->setLogFile(logFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
void logInfo(std::string message)
|
||||||
|
{
|
||||||
|
Logger* logger = nullptr;
|
||||||
|
LogStrategy* li = new LogInfo();
|
||||||
|
logger = new Logger(li);
|
||||||
|
logger->log(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
void logDebug(std::string message)
|
||||||
|
{
|
||||||
|
Logger* logger = nullptr;
|
||||||
|
LogStrategy* li = new LogDebug();
|
||||||
|
logger = new Logger(li);
|
||||||
|
logger->log(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
void logSuccess(std::string message)
|
||||||
|
{
|
||||||
|
Logger* logger = nullptr;
|
||||||
|
LogStrategy* li = new LogSuccess();
|
||||||
|
logger = new Logger(li);
|
||||||
|
logger->log(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
void logWarning(std::string message)
|
||||||
|
{
|
||||||
|
Logger* logger = nullptr;
|
||||||
|
LogStrategy* li = new LogWarning();
|
||||||
|
logger = new Logger(li);
|
||||||
|
logger->log(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
void logError(std::string message)
|
||||||
|
{
|
||||||
|
Logger* logger = nullptr;
|
||||||
|
LogStrategy* li = new LogError();
|
||||||
|
logger = new Logger(li);
|
||||||
|
logger->log(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
void logFatal(std::string message)
|
||||||
|
{
|
||||||
|
Logger* logger = nullptr;
|
||||||
|
LogStrategy* li = new LogFatal();
|
||||||
|
logger = new Logger(li);
|
||||||
|
logger->log(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
void logCritical(std::string message)
|
||||||
|
{
|
||||||
|
Logger* logger = nullptr;
|
||||||
|
LogStrategy* li = new LogCritical();
|
||||||
|
logger = new Logger(li);
|
||||||
|
logger->log(message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
14
src/Logger/test.cpp
Normal file
14
src/Logger/test.cpp
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#include "SimpleLogger.h"
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
SimpleLogger sl = SimpleLogger("/tmp/simplelog.log");
|
||||||
|
sl.logInfo("test info");
|
||||||
|
sl.logDebug("test debug");
|
||||||
|
sl.logSuccess("test success");
|
||||||
|
sl.logWarning("test warning");
|
||||||
|
sl.logError("test error");
|
||||||
|
sl.logFatal("test fatal");
|
||||||
|
sl.logCritical("test critical");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -150,6 +150,22 @@ QString ChatItem::toChatLine()
|
|||||||
return line;
|
return line;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
json ChatItem::toJson()
|
||||||
|
{
|
||||||
|
json j;
|
||||||
|
j["_timestamp"] = _timestamp;
|
||||||
|
j["_address"] = _address.toStdString();
|
||||||
|
j["_contact"] = _contact.toStdString();
|
||||||
|
j["_memo"] = _memo.toStdString();
|
||||||
|
j["_requestZaddr"] = _requestZaddr.toStdString();
|
||||||
|
j["_type"] = _type.toStdString();
|
||||||
|
j["_cid"] = _cid.toStdString();
|
||||||
|
j["_txid"] = _txid.toStdString();
|
||||||
|
j["_confirmations"] = _confirmations;
|
||||||
|
j["_outgoing"] = _outgoing;
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
|
||||||
ChatItem::~ChatItem()
|
ChatItem::~ChatItem()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#define CHATITEM_H
|
#define CHATITEM_H
|
||||||
|
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
using json = nlohmann::json;
|
||||||
|
|
||||||
class ChatItem
|
class ChatItem
|
||||||
{
|
{
|
||||||
@@ -45,6 +46,7 @@ class ChatItem
|
|||||||
void setConfirmations(int confirmations);
|
void setConfirmations(int confirmations);
|
||||||
void toggleOutgo();
|
void toggleOutgo();
|
||||||
QString toChatLine();
|
QString toChatLine();
|
||||||
|
json toJson();
|
||||||
~ChatItem();
|
~ChatItem();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -64,3 +64,14 @@ QString ContactItem::toQTString()
|
|||||||
{
|
{
|
||||||
return _name + "|" + _partnerAddress + "|" + _myAddress + "|" + _cid + "|" + _avatar;
|
return _name + "|" + _partnerAddress + "|" + _myAddress + "|" + _cid + "|" + _avatar;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
json ContactItem::toJson()
|
||||||
|
{
|
||||||
|
json j;
|
||||||
|
j["_myAddress"] = _myAddress.toStdString();
|
||||||
|
j["_partnerAddress"] = _partnerAddress.toStdString();
|
||||||
|
j["_name"] = _name.toStdString();
|
||||||
|
j["_cid"] = _cid.toStdString();
|
||||||
|
j["_avatar"] = _avatar.toStdString();
|
||||||
|
return j;
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
using json = nlohmann::json;
|
||||||
|
|
||||||
class ContactItem
|
class ContactItem
|
||||||
{
|
{
|
||||||
@@ -27,6 +28,7 @@ public:
|
|||||||
void setcid(QString cid);
|
void setcid(QString cid);
|
||||||
void setAvatar(QString avatar);
|
void setAvatar(QString avatar);
|
||||||
QString toQTString();
|
QString toQTString();
|
||||||
|
json toJson();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -7,6 +7,8 @@
|
|||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
#include "mainwindow.h"
|
#include "mainwindow.h"
|
||||||
#include "controller.h"
|
#include "controller.h"
|
||||||
|
#include "DataStore/DataStore.h"
|
||||||
|
#include "FileSystem/FileSystem.h"
|
||||||
|
|
||||||
|
|
||||||
AddressBookModel::AddressBookModel(QTableView *parent) : QAbstractTableModel(parent)
|
AddressBookModel::AddressBookModel(QTableView *parent) : QAbstractTableModel(parent)
|
||||||
@@ -362,7 +364,7 @@ AddressBook::AddressBook()
|
|||||||
|
|
||||||
void AddressBook::readFromStorage()
|
void AddressBook::readFromStorage()
|
||||||
{
|
{
|
||||||
QFile file(AddressBook::writeableFile());
|
/*QFile file(AddressBook::writeableFile());
|
||||||
|
|
||||||
if (file.exists())
|
if (file.exists())
|
||||||
{
|
{
|
||||||
@@ -392,15 +394,29 @@ void AddressBook::readFromStorage()
|
|||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
|
{
|
||||||
|
qDebug() << "No Hush contacts found on disk!";
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
allLabels = FileSystem::getInstance()->readContacts(AddressBook::writeableFile());
|
||||||
|
|
||||||
|
// test to see if the contact items in datastore are correctly dumped to json
|
||||||
|
for(ContactItem item: allLabels)
|
||||||
{
|
{
|
||||||
qDebug() << "No Hush contacts found on disk!";
|
DataStore::getContactDataStore()->setData(item.getCid(), item);
|
||||||
}
|
}
|
||||||
}
|
DataStore::getContactDataStore()->dump();
|
||||||
|
AddressBook::writeToStorage();
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddressBook::writeToStorage()
|
void AddressBook::writeToStorage()
|
||||||
{
|
{
|
||||||
QFile file(AddressBook::writeableFile());
|
//FileSystem::getInstance()->writeContacts(AddressBook::writeableFile(), DataStore::getContactDataStore()->dump());
|
||||||
|
|
||||||
|
FileSystem::getInstance()->writeContactsOldFormat(AddressBook::writeableFile(), allLabels);
|
||||||
|
|
||||||
|
|
||||||
|
/*QFile file(AddressBook::writeableFile());
|
||||||
file.open(QIODevice::ReadWrite | QIODevice::Truncate);
|
file.open(QIODevice::ReadWrite | QIODevice::Truncate);
|
||||||
QDataStream out(&file); // we will serialize the data into the file
|
QDataStream out(&file); // we will serialize the data into the file
|
||||||
QList<QList<QString>> contacts;
|
QList<QList<QString>> contacts;
|
||||||
@@ -415,7 +431,7 @@ void AddressBook::writeToStorage()
|
|||||||
contacts.push_back(c);
|
contacts.push_back(c);
|
||||||
}
|
}
|
||||||
out << QString("v1") << contacts;
|
out << QString("v1") << contacts;
|
||||||
file.close();
|
file.close();*/
|
||||||
}
|
}
|
||||||
|
|
||||||
QString AddressBook::writeableFile()
|
QString AddressBook::writeableFile()
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include "precompiled.h"
|
#include "precompiled.h"
|
||||||
#include "contactmodel.h"
|
#include "contactmodel.h"
|
||||||
|
#include "FileSystem/FileSystem.h"
|
||||||
|
|
||||||
class MainWindow;
|
class MainWindow;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user