update// added new writer method to filesystem to convert old format to new
This commit is contained in:
15
.gdb_history
15
.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
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
break ContactItem::toJson()
|
||||
break FileSystem::writeContacts
|
||||
|
||||
|
||||
@@ -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<QString, ChatItem> ChatDataStore::getAllRawChatItems()
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -20,6 +20,51 @@ FileSystem* FileSystem::getInstance()
|
||||
}
|
||||
|
||||
QList<ContactItem> 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<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);
|
||||
@@ -51,26 +96,6 @@ QList<ContactItem> FileSystem::readContacts(QString file)
|
||||
return contacts;
|
||||
}
|
||||
|
||||
void FileSystem::writeContacts(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();
|
||||
}
|
||||
|
||||
FileSystem::~FileSystem()
|
||||
{
|
||||
this->instance = nullptr;
|
||||
|
||||
@@ -5,7 +5,8 @@
|
||||
#include <QList>
|
||||
#include "../Model/ContactItem.h"
|
||||
#include "../Crypto/FileEncryption.h"
|
||||
|
||||
#include <fstream>
|
||||
using json = nlohmann::json;
|
||||
class FileSystem
|
||||
{
|
||||
private:
|
||||
@@ -16,7 +17,11 @@ class FileSystem
|
||||
public:
|
||||
static FileSystem* getInstance();
|
||||
QList<ContactItem> readContacts(QString file);
|
||||
void writeContacts(QString file, QList<ContactItem> contacts);
|
||||
void writeContacts(QString file, QString data);
|
||||
|
||||
//converter
|
||||
QList<ContactItem> readContactsOldFormat(QString file);
|
||||
void writeContactsOldFormat(QString file, QList<ContactItem> contacts);
|
||||
~FileSystem();
|
||||
|
||||
};
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user