diff --git a/.gdb_history b/.gdb_history new file mode 100644 index 0000000..2eb34c2 --- /dev/null +++ b/.gdb_history @@ -0,0 +1,35 @@ +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 diff --git a/peda-session-SilentDragonLite.txt b/peda-session-SilentDragonLite.txt new file mode 100644 index 0000000..87f58c8 --- /dev/null +++ b/peda-session-SilentDragonLite.txt @@ -0,0 +1,2 @@ +break ContactItem::toJson() + diff --git a/silentdragon-lite.pro b/silentdragon-lite.pro index 56c9e4c..bb10655 100644 --- a/silentdragon-lite.pro +++ b/silentdragon-lite.pro @@ -75,6 +75,7 @@ SOURCES += \ src/DataStore/DataStore.cpp \ src/DataStore/ChatDataStore.cpp \ src/DataStore/SietchDataStore.cpp \ + src/DataStore/ContactDataStore.cpp \ src/Model/ChatItem.cpp \ src/Model/ContactRequestChatItem.cpp \ src/Model/ContactItem.cpp \ diff --git a/src/Chat/Chat.cpp b/src/Chat/Chat.cpp index 5b34929..c7d6637 100644 --- a/src/Chat/Chat.cpp +++ b/src/Chat/Chat.cpp @@ -12,7 +12,7 @@ void Chat::renderChatBox(Ui::MainWindow *ui, QListView *view) // ui->lcdNumber->setStyleSheet("background-color: red"); // ui->lcdNumber->setPalette(Qt::red); // 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 &c : DataStore::getChatDataStore()->getAllMemos()) diff --git a/src/DataStore/ChatDataStore.cpp b/src/DataStore/ChatDataStore.cpp index 5223d04..ff56c79 100644 --- a/src/DataStore/ChatDataStore.cpp +++ b/src/DataStore/ChatDataStore.cpp @@ -32,6 +32,17 @@ ChatItem ChatDataStore::getData(QString key) QString ChatDataStore::dump() { + json chats; + chats["count"] = this->data.size(); + json j = {}; + for (auto &c: this->data) + { + j.push_back(c.second.toJson()); + } + chats["chatitems"] = j; + + std::string dump = chats.dump(4); + qDebug() << dump.c_str(); return ""; } diff --git a/src/DataStore/ContactDataStore.cpp b/src/DataStore/ContactDataStore.cpp new file mode 100644 index 0000000..4a1f691 --- /dev/null +++ b/src/DataStore/ContactDataStore.cpp @@ -0,0 +1,53 @@ +// Copyright 2019-2020 The Hush developers +// GPLv3 + +#include "ContactDataStore.h" +#include + +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; + + std::string dump = contacts.dump(4); + qDebug() << dump.c_str(); + return ""; +} + +ContactDataStore* ContactDataStore::instance = nullptr; +bool ContactDataStore::instanced = false; \ No newline at end of file diff --git a/src/DataStore/ContactDataStore.h b/src/DataStore/ContactDataStore.h new file mode 100644 index 0000000..009657e --- /dev/null +++ b/src/DataStore/ContactDataStore.h @@ -0,0 +1,34 @@ +#ifndef CONTACTDATASTORE_H +#define CONTACTDATASTORE_H +#include "../Model/ContactItem.h" +#include +using json = nlohmann::json; + +class ContactDataStore +{ + private: + static bool instanced; + static ContactDataStore* instance; + std::map 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 \ No newline at end of file diff --git a/src/DataStore/DataStore.cpp b/src/DataStore/DataStore.cpp index d16c255..ae85ed6 100644 --- a/src/DataStore/DataStore.cpp +++ b/src/DataStore/DataStore.cpp @@ -8,4 +8,9 @@ SietchDataStore* DataStore::getSietchDataStore() ChatDataStore* DataStore::getChatDataStore() { return ChatDataStore::getInstance(); +} + +ContactDataStore* DataStore::getContactDataStore() +{ + return ContactDataStore::getInstance(); } \ No newline at end of file diff --git a/src/DataStore/DataStore.h b/src/DataStore/DataStore.h index 6715c02..9fa42d3 100644 --- a/src/DataStore/DataStore.h +++ b/src/DataStore/DataStore.h @@ -3,12 +3,14 @@ #include "SietchDataStore.h" #include "ChatDataStore.h" +#include "ContactDataStore.h" class DataStore { public: static SietchDataStore* getSietchDataStore(); static ChatDataStore* getChatDataStore(); + static ContactDataStore* getContactDataStore(); }; #endif \ No newline at end of file diff --git a/src/Model/ChatItem.cpp b/src/Model/ChatItem.cpp index afdde53..dd43785 100644 --- a/src/Model/ChatItem.cpp +++ b/src/Model/ChatItem.cpp @@ -150,6 +150,22 @@ QString ChatItem::toChatLine() 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() { diff --git a/src/Model/ChatItem.h b/src/Model/ChatItem.h index 6f27609..be8a807 100644 --- a/src/Model/ChatItem.h +++ b/src/Model/ChatItem.h @@ -5,6 +5,7 @@ #define CHATITEM_H #include +using json = nlohmann::json; class ChatItem { @@ -45,6 +46,7 @@ class ChatItem void setConfirmations(int confirmations); void toggleOutgo(); QString toChatLine(); + json toJson(); ~ChatItem(); }; diff --git a/src/Model/ContactItem.cpp b/src/Model/ContactItem.cpp index f338552..d38c1c5 100644 --- a/src/Model/ContactItem.cpp +++ b/src/Model/ContactItem.cpp @@ -63,4 +63,15 @@ void ContactItem::setAvatar(QString avatar) QString ContactItem::toQTString() { 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; } \ No newline at end of file diff --git a/src/Model/ContactItem.h b/src/Model/ContactItem.h index a18d0f7..f066740 100644 --- a/src/Model/ContactItem.h +++ b/src/Model/ContactItem.h @@ -3,6 +3,7 @@ #include #include +using json = nlohmann::json; class ContactItem { @@ -27,6 +28,7 @@ public: void setcid(QString cid); void setAvatar(QString avatar); QString toQTString(); + json toJson(); }; #endif \ No newline at end of file diff --git a/src/addressbook.cpp b/src/addressbook.cpp index 55a3b5e..58c8695 100644 --- a/src/addressbook.cpp +++ b/src/addressbook.cpp @@ -7,6 +7,7 @@ #include "settings.h" #include "mainwindow.h" #include "controller.h" +#include "DataStore/DataStore.h" #include "FileSystem/FileSystem.h" @@ -398,6 +399,13 @@ void AddressBook::readFromStorage() } }*/ allLabels = FileSystem::getInstance()->readContacts(AddressBook::writeableFile()); + + // test to see if the contact items in datastore are correctly dumped to json + for(ContactItem item: allLabels) + { + DataStore::getContactDataStore()->setData(item.getCid(), item); + } + DataStore::getContactDataStore()->dump(); } void AddressBook::writeToStorage()