Merge pull request #25 from strider-paff-shell/chat

c
This commit is contained in:
Denio
2020-05-08 20:22:21 +02:00
committed by GitHub
5 changed files with 202 additions and 49 deletions

View File

@@ -0,0 +1,65 @@
#ifndef CHATDATASTORE_H
#define CHATDATASTORE_H
using json = nlohmann::json;
class ChatDataStore
{
private:
static bool instanced;
static ChatDataStore* instance;
std::map<QString, ChatItem> data;
ChatDataStore()
{
}
public:
static ChatDataStore* getInstance()
{
if(!ChatDataStore::instanced)
{
ChatDataStore::instanced = true;
ChatDataStore::instance = new ChatDataStore();
}
return ChatDataStore::instance;
}
void clear();
void setData(QString key, ChatItem value);
ChatItem getData(QString key);
QString dump();
~ChatDataStore()
{
ChatDataStore::instanced = false;
ChatDataStore::instance = nullptr;
}
};
void ChatDataStore::clear()
{
this->data.clear();
}
void ChatDataStore::setData(QString key, ChatItem value)
{
this->data[key] = value;
}
ChatItem ChatDataStore::getData(QString key)
{
return this->data[key];
}
QString ChatDataStore::dump()
{
return "";
}
ChatDataStore* ChatDataStore::instance = nullptr;
bool ChatDataStore::instanced = false;
#endif

View File

@@ -0,0 +1,58 @@
#ifndef DATASTORE_H
#define DATASTORE_H
#include <QString>
#include <map>
template <class T>
class DataStore
{
private:
static bool instanced;
static DataStore<T>* instance;
std::map<QString, T> data;
DataStore()
{
}
public:
static DataStore<T>* getInstance()
{
if(!DataStore<T>::instanced)
{
DataStore<T>::instanced = true;
DataStore<T>::instance = new DataStore<T>();
}
return DataStore<T>::instance;
}
void clear();
void setData(QString key, T value);
QString getData(QString key);
~DataStore()
{
DataStore<T>::instanced = false;
}
};
template <class T>
void DataStore<T>::clear()
{
this->data.clear();
}
template <class T>
void DataStore<T>::setData(QString key, T value)
{
this->data[key] = value;
}
template <class T>
QString DataStore<T>::getData(QString key)
{
return this->data[key];
}
#endif

View File

@@ -1,58 +1,24 @@
#ifndef DATASTORE_H #ifndef DATASTORE_H
#define DATASTORE_H #define DATASTORE_H
#include <QString> #include "SietchDataStore.h"
#include <map> #include "ChatDataStore.h"
template <class T>
class DataStore class DataStore
{ {
private: public:
static bool instanced; static SietchDataStore* getSietchDataStore();
static DataStore<T>* instance; static ChatDataStore* getChatDataStore();
std::map<QString, T> data;
DataStore()
{
}
public:
static DataStore<T>* getInstance()
{
if(!DataStore<T>::instanced)
{
DataStore<T>::instanced = true;
DataStore<T>::instance = new DataStore<T>();
}
return DataStore<T>::instance;
}
void clear();
void setData(QString key, T value);
QString getData(QString key);
~DataStore()
{
DataStore<T>::instanced = false;
}
}; };
template <class T> SietchDataStore* DataStore::getSietchDataStore()
void DataStore<T>::clear()
{ {
this->data.clear(); return SietchDataStore::getInstance();
} }
template <class T> ChatDataStore* DataStore::getChatDataStore()
void DataStore<T>::setData(QString key, T value)
{ {
this->data[key] = value; return ChatDataStore::getInstance();
} }
template <class T>
QString DataStore<T>::getData(QString key)
{
return this->data[key];
}
#endif #endif

View File

@@ -0,0 +1,64 @@
#ifndef SIETCHDATASTORE_H
#define SIETCHDATASTORE_H
using json = nlohmann::json;
class SietchDataStore
{
private:
static bool instanced;
static SietchDataStore* instance;
std::map<QString, QString> data;
SietchDataStore()
{
}
public:
static SietchDataStore* getInstance()
{
if(!SietchDataStore::instanced)
{
SietchDataStore::instanced = true;
SietchDataStore::instance = new SietchDataStore();
}
return SietchDataStore::instance;
}
void clear();
void setData(QString key, QString value);
QString getData(QString key);
QString dump();
~SietchDataStore()
{
SietchDataStore::instanced = false;
SietchDataStore::instance = nullptr;
}
};
void SietchDataStore::clear()
{
this->data.clear();
}
void SietchDataStore::setData(QString key, QString value)
{
this->data[key] = value;
}
QString SietchDataStore::getData(QString key)
{
return this->data[key];
}
QString SietchDataStore::dump()
{
return "";
}
SietchDataStore* SietchDataStore::instance = nullptr;
bool SietchDataStore::instanced = false;
#endif

View File

@@ -10,10 +10,10 @@
#include "websockets.h" #include "websockets.h"
#include "DataStore/DataStore.h" #include "DataStore/DataStore.h"
template<> /*template<>
DataStore<QString>* DataStore<QString>::instance = nullptr; DataStore<QString>* DataStore<QString>::instance = nullptr;
template<> template<>
bool DataStore<QString>::instanced = false; bool DataStore<QString>::instanced = false;*/
ChatModel *chatModel = new ChatModel(); ChatModel *chatModel = new ChatModel();
ContactModel *contactModel = new ContactModel(); ContactModel *contactModel = new ContactModel();
@@ -97,7 +97,7 @@ void Controller::setConnection(Connection* c)
{ {
zrpc->createNewSietchZaddr( [=] (json reply) { zrpc->createNewSietchZaddr( [=] (json reply) {
QString zdust = QString::fromStdString(reply.get<json::array_t>()[0]); QString zdust = QString::fromStdString(reply.get<json::array_t>()[0]);
DataStore<QString>::getInstance()->setData("Sietch" + QString(i), zdust.toUtf8()); DataStore::getSietchDataStore()->setData("Sietch" + QString(i), zdust.toUtf8());
}); });
} }
} }
@@ -120,7 +120,7 @@ void Controller::fillTxJsonParams(json& allRecepients, Tx tx)
{ {
zrpc->createNewSietchZaddr( [=] (json reply) { zrpc->createNewSietchZaddr( [=] (json reply) {
QString zdust = QString::fromStdString(reply.get<json::array_t>()[0]); QString zdust = QString::fromStdString(reply.get<json::array_t>()[0]);
DataStore<QString>::getInstance()->setData(QString("Sietch") + QString(i), zdust.toUtf8()); DataStore::getSietchDataStore()->setData(QString("Sietch") + QString(i), zdust.toUtf8());
} ); } );
} }
@@ -128,10 +128,10 @@ void Controller::fillTxJsonParams(json& allRecepients, Tx tx)
// Using DataStore singelton, to store the data into the dusts, bing bada boom :D // Using DataStore singelton, to store the data into the dusts, bing bada boom :D
for(uint8_t i = 0; i < 10; i++) for(uint8_t i = 0; i < 10; i++)
{ {
dust.at(i)["address"] = DataStore<QString>::getInstance()->getData(QString("Sietch" + QString(i))).toStdString(); dust.at(i)["address"] = DataStore::getSietchDataStore()->getData(QString("Sietch" + QString(i))).toStdString();
} }
DataStore<QString>::getInstance()->clear(); // clears the datastore DataStore::getSietchDataStore()->clear(); // clears the datastore
// Dust amt/memo, construct the JSON // Dust amt/memo, construct the JSON
for(uint8_t i = 0; i < 10; i++) for(uint8_t i = 0; i < 10; i++)