diff --git a/.gdb_history b/.gdb_history index 06f6849..4846c14 100644 --- a/.gdb_history +++ b/.gdb_history @@ -54,3 +54,8 @@ r s n q +b FileSystem::readContactsOldFormat +r +n +c +q diff --git a/peda-session-SilentDragonLite.txt b/peda-session-SilentDragonLite.txt index 6f620bf..12c5dfb 100644 --- a/peda-session-SilentDragonLite.txt +++ b/peda-session-SilentDragonLite.txt @@ -1,2 +1,2 @@ -break FileEncryption::encrypt +break FileSystem::readContactsOldFormat diff --git a/src/FileSystem/FileSystem.cpp b/src/FileSystem/FileSystem.cpp index 07d476c..f7d37be 100644 --- a/src/FileSystem/FileSystem.cpp +++ b/src/FileSystem/FileSystem.cpp @@ -83,7 +83,7 @@ void FileSystem::writeContactsOldFormat(QString file, QList contact c.push_back(item.getAvatar()); _contacts.push_back(c); } - out << QString("v1") << _contacts; + out << QString("v2") << _contacts; _file.close(); } @@ -96,35 +96,68 @@ QList FileSystem::readContactsOldFormat(QString file) 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"; if(in.status() == QDataStream::ReadCorruptData) { - qDebug() << "Error reading contacts! ---> Your hush contacts from disk maybe corrupted"; - QFile::rename(file, file + QString(".corrupted")); - QMessageBox::critical( - nullptr, - QObject::tr("Error reading contacts!"), - QObject::tr("Your hush contacts from disk maybe corrupted"), - QMessageBox::Ok - ); + qDebug() << "Error reading contacts! ---> Your hush contacts from disk maybe corrupted"; + QFile::rename(file, file + QString(".corrupted")); + QMessageBox::critical( + nullptr, + QObject::tr("Error reading contacts!"), + QObject::tr("Your hush contacts from disk maybe corrupted"), + QMessageBox::Ok + ); } - else + else { - qDebug() << "Read " << version << " Hush contacts from disk..."; - QList> stuff; - in >> stuff; - for (int i=0; i < stuff.size(); i++) + QString version; + in >> version; + if(version == "v1") { - ContactItem contact = ContactItem(stuff[i][0],stuff[i][1], stuff[i][2], stuff[i][3],stuff[i][4]); - contacts.push_back(contact); + qDebug() << "Detected old addressbook format"; + // Convert old addressbook format v1 to v2 + QList> stuff; + in >> stuff; + qDebug() << "Stuff: " << stuff; + for (int i=0; i < stuff.size(); i++) + { + ContactItem contact = ContactItem(stuff[i].first, stuff[i].second); + contacts.push_back(contact); + qDebug() << "contact=" << contact.toQTString(); + } + + } + else + { + qDebug() << "Read " << version << " Hush contacts from disk..."; + QList> stuff; + in >> stuff; + qDebug() << "Dataarray size: " << stuff.size(); + if(stuff.size() == 0) + return contacts; + + for (int i= 0; i < stuff.size(); i++) + { + qDebug() << stuff[i].size(); + ContactItem contact; + if(stuff[i].size() == 4) + { + contact = ContactItem(stuff[i][0],stuff[i][1], stuff[i][2], stuff[i][3]); + } + else + { + contact = ContactItem(stuff[i][0],stuff[i][1], stuff[i][2], stuff[i][3],stuff[i][4]); + } + + qDebug() << contact.toQTString(); + contacts.push_back(contact); + } + + } qDebug() << "Hush contacts readed from disk..."; } - + _file.close(); } else diff --git a/src/Model/ContactItem.cpp b/src/Model/ContactItem.cpp index d2321bd..589ca9f 100644 --- a/src/Model/ContactItem.cpp +++ b/src/Model/ContactItem.cpp @@ -7,6 +7,20 @@ ContactItem::ContactItem() {} +ContactItem::ContactItem(QString name, QString partnerAddress) +{ + _name = name; + _partnerAddress = partnerAddress; +} + +ContactItem::ContactItem(QString name, QString partnerAddress, QString myAddress, QString cid) +{ + _name = name; + _myAddress = myAddress; + _partnerAddress = partnerAddress; + _cid = cid; +} + ContactItem::ContactItem(QString name, QString partnerAddress, QString myAddress, QString cid, QString avatar) { _name = name; diff --git a/src/Model/ContactItem.h b/src/Model/ContactItem.h index 7eda17d..edb512d 100644 --- a/src/Model/ContactItem.h +++ b/src/Model/ContactItem.h @@ -11,14 +11,16 @@ using json = nlohmann::json; class ContactItem { private: - QString _myAddress; - QString _partnerAddress; - QString _name; - QString _cid; - QString _avatar; + QString _myAddress = ""; + QString _partnerAddress = ""; + QString _name = ""; + QString _cid = ""; + QString _avatar = ":/icons/res/sdlogo.png"; public: ContactItem(); + ContactItem(QString name, QString partnerAddress); + ContactItem(QString name, QString partnerAddress, QString myAddress, QString cid); ContactItem(QString name, QString partnerAddress, QString myAddress, QString cid, QString avatar); QString getName() const; QString getMyAddress() const; diff --git a/src/addressbook.cpp b/src/addressbook.cpp index 808ca40..6d7f913 100644 --- a/src/addressbook.cpp +++ b/src/addressbook.cpp @@ -417,12 +417,7 @@ 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(); - AddressBook::writeToStorage(); } void AddressBook::writeToStorage()