From e6b8f000c94ff14f5f7f6dfe5c839334bc6246ad Mon Sep 17 00:00:00 2001 From: Strider <127.0.0.1@404.com> Date: Sun, 17 May 2020 14:57:38 +0200 Subject: [PATCH] update// added new writer method to filesystem to convert old format to new --- .gdb_history | 15 +++++++ peda-session-SilentDragonLite.txt | 2 +- src/DataStore/ChatDataStore.cpp | 5 +-- src/DataStore/ContactDataStore.cpp | 5 +-- src/FileSystem/FileSystem.cpp | 65 +++++++++++++++++++++--------- src/FileSystem/FileSystem.h | 9 ++++- src/addressbook.cpp | 7 +++- 7 files changed, 76 insertions(+), 32 deletions(-) diff --git a/.gdb_history b/.gdb_history index 2eb34c2..4e303b8 100644 --- a/.gdb_history +++ b/.gdb_history @@ -33,3 +33,18 @@ 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 diff --git a/peda-session-SilentDragonLite.txt b/peda-session-SilentDragonLite.txt index 87f58c8..f57fa7c 100644 --- a/peda-session-SilentDragonLite.txt +++ b/peda-session-SilentDragonLite.txt @@ -1,2 +1,2 @@ -break ContactItem::toJson() +break FileSystem::writeContacts diff --git a/src/DataStore/ChatDataStore.cpp b/src/DataStore/ChatDataStore.cpp index ff56c79..22c10f5 100644 --- a/src/DataStore/ChatDataStore.cpp +++ b/src/DataStore/ChatDataStore.cpp @@ -40,10 +40,7 @@ QString ChatDataStore::dump() j.push_back(c.second.toJson()); } chats["chatitems"] = j; - - std::string dump = chats.dump(4); - qDebug() << dump.c_str(); - return ""; + return QString::fromStdString(chats.dump()); } std::map ChatDataStore::getAllRawChatItems() diff --git a/src/DataStore/ContactDataStore.cpp b/src/DataStore/ContactDataStore.cpp index 4a1f691..e853770 100644 --- a/src/DataStore/ContactDataStore.cpp +++ b/src/DataStore/ContactDataStore.cpp @@ -43,10 +43,7 @@ QString ContactDataStore::dump() j.push_back(c.second.toJson()); } contacts["contacts"] = j; - - std::string dump = contacts.dump(4); - qDebug() << dump.c_str(); - return ""; + return QString::fromStdString(contacts.dump(4)); } ContactDataStore* ContactDataStore::instance = nullptr; diff --git a/src/FileSystem/FileSystem.cpp b/src/FileSystem/FileSystem.cpp index 5ee2fef..49c8178 100644 --- a/src/FileSystem/FileSystem.cpp +++ b/src/FileSystem/FileSystem.cpp @@ -20,6 +20,51 @@ FileSystem* FileSystem::getInstance() } QList FileSystem::readContacts(QString file) +{ + return this->readContactsOldFormat(file); //will be called if addresses are in the old dat-format +} + +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()) + { + f << data.toStdString(); + } + + f.close(); + } + else + { + qInfo() << file << "not exist"; + } +} + +void FileSystem::writeContactsOldFormat(QString file, QList contacts) +{ + QFile _file(file); + _file.open(QIODevice::ReadWrite | QIODevice::Truncate); + QDataStream out(&_file); // we will serialize the data into the file + QList> _contacts; + for(auto &item: contacts) + { + QList 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 FileSystem::readContactsOldFormat(QString file) { QList contacts; QFile _file(file); @@ -51,26 +96,6 @@ QList FileSystem::readContacts(QString file) return contacts; } -void FileSystem::writeContacts(QString file, QList contacts) -{ - QFile _file(file); - _file.open(QIODevice::ReadWrite | QIODevice::Truncate); - QDataStream out(&_file); // we will serialize the data into the file - QList> _contacts; - for(auto &item: contacts) - { - QList 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(); -} - FileSystem::~FileSystem() { this->instance = nullptr; diff --git a/src/FileSystem/FileSystem.h b/src/FileSystem/FileSystem.h index 995c586..30dd5fc 100644 --- a/src/FileSystem/FileSystem.h +++ b/src/FileSystem/FileSystem.h @@ -5,7 +5,8 @@ #include #include "../Model/ContactItem.h" #include "../Crypto/FileEncryption.h" - +#include +using json = nlohmann::json; class FileSystem { private: @@ -16,7 +17,11 @@ class FileSystem public: static FileSystem* getInstance(); QList readContacts(QString file); - void writeContacts(QString file, QList contacts); + void writeContacts(QString file, QString data); + + //converter + QList readContactsOldFormat(QString file); + void writeContactsOldFormat(QString file, QList contacts); ~FileSystem(); }; diff --git a/src/addressbook.cpp b/src/addressbook.cpp index 58c8695..b557dd9 100644 --- a/src/addressbook.cpp +++ b/src/addressbook.cpp @@ -406,11 +406,16 @@ void AddressBook::readFromStorage() DataStore::getContactDataStore()->setData(item.getCid(), item); } DataStore::getContactDataStore()->dump(); + AddressBook::writeToStorage(); } void AddressBook::writeToStorage() { - FileSystem::getInstance()->writeContacts(AddressBook::writeableFile(), allLabels); + //FileSystem::getInstance()->writeContacts(AddressBook::writeableFile(), DataStore::getContactDataStore()->dump()); + + FileSystem::getInstance()->writeContactsOldFormat(AddressBook::writeableFile(), allLabels); + + /*QFile file(AddressBook::writeableFile()); file.open(QIODevice::ReadWrite | QIODevice::Truncate); QDataStream out(&file); // we will serialize the data into the file