From 2efec07e42cf107a8e4cdc219ffe9e2c5922794c Mon Sep 17 00:00:00 2001 From: Strider Date: Tue, 28 Apr 2020 20:24:50 +0200 Subject: [PATCH] update// refactored addressbook with new storage format --- compile.log | 5 +++ src/addressbook.cpp | 98 +++++++++++++++++++++++++++++--------------- src/addressbook.h | 17 ++++---- src/chatmodel.cpp | 4 +- src/contactmodel.cpp | 9 ++++ src/contactmodel.h | 68 ++++++++++++++++++++++++++++++ src/controller.cpp | 1 + src/controller.h | 1 + src/mainwindow.cpp | 4 +- src/sendtab.cpp | 2 +- 10 files changed, 165 insertions(+), 44 deletions(-) create mode 100644 compile.log create mode 100644 src/contactmodel.cpp create mode 100644 src/contactmodel.h diff --git a/compile.log b/compile.log new file mode 100644 index 0000000..6c6584f --- /dev/null +++ b/compile.log @@ -0,0 +1,5 @@ +Compiling SilentDragonLite 1.2.2 with 4 threads... +g++ -c -include bin/SilentDragonLite -pipe -g -std=gnu++1y -Wall -W -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQAPPLICATION_CLASS=QApplication -D_FORTIFY_SOURCE=2 -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_WEBSOCKETS_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -I. -Isrc/3rdparty -Isrc -Isingleapplication -Ires -isystem /usr/include/x86_64-linux-gnu/qt5 -isystem /usr/include/x86_64-linux-gnu/qt5/QtWidgets -isystem /usr/include/x86_64-linux-gnu/qt5/QtGui -isystem /usr/include/x86_64-linux-gnu/qt5/QtWebSockets -isystem /usr/include/x86_64-linux-gnu/qt5/QtNetwork -isystem /usr/include/x86_64-linux-gnu/qt5/QtCore -Ibin -Isrc -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++ -o bin/mainwindow.o src/mainwindow.cpp +g++ -c -include bin/SilentDragonLite -pipe -g -std=gnu++1y -Wall -W -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQAPPLICATION_CLASS=QApplication -D_FORTIFY_SOURCE=2 -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_WEBSOCKETS_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -I. -Isrc/3rdparty -Isrc -Isingleapplication -Ires -isystem /usr/include/x86_64-linux-gnu/qt5 -isystem /usr/include/x86_64-linux-gnu/qt5/QtWidgets -isystem /usr/include/x86_64-linux-gnu/qt5/QtGui -isystem /usr/include/x86_64-linux-gnu/qt5/QtWebSockets -isystem /usr/include/x86_64-linux-gnu/qt5/QtNetwork -isystem /usr/include/x86_64-linux-gnu/qt5/QtCore -Ibin -Isrc -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++ -o bin/sendtab.o src/sendtab.cpp +g++ -c -include bin/SilentDragonLite -pipe -g -std=gnu++1y -Wall -W -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQAPPLICATION_CLASS=QApplication -D_FORTIFY_SOURCE=2 -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_WEBSOCKETS_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -I. -Isrc/3rdparty -Isrc -Isingleapplication -Ires -isystem /usr/include/x86_64-linux-gnu/qt5 -isystem /usr/include/x86_64-linux-gnu/qt5/QtWidgets -isystem /usr/include/x86_64-linux-gnu/qt5/QtGui -isystem /usr/include/x86_64-linux-gnu/qt5/QtWebSockets -isystem /usr/include/x86_64-linux-gnu/qt5/QtNetwork -isystem /usr/include/x86_64-linux-gnu/qt5/QtCore -Ibin -Isrc -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++ -o bin/addressbook.o src/addressbook.cpp +g++ -c -include bin/SilentDragonLite -pipe -g -std=gnu++1y -Wall -W -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQAPPLICATION_CLASS=QApplication -D_FORTIFY_SOURCE=2 -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_WEBSOCKETS_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -I. -Isrc/3rdparty -Isrc -Isingleapplication -Ires -isystem /usr/include/x86_64-linux-gnu/qt5 -isystem /usr/include/x86_64-linux-gnu/qt5/QtWidgets -isystem /usr/include/x86_64-linux-gnu/qt5/QtGui -isystem /usr/include/x86_64-linux-gnu/qt5/QtWebSockets -isystem /usr/include/x86_64-linux-gnu/qt5/QtNetwork -isystem /usr/include/x86_64-linux-gnu/qt5/QtCore -Ibin -Isrc -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++ -o bin/addresscombo.o src/addresscombo.cpp diff --git a/src/addressbook.cpp b/src/addressbook.cpp index a00a355..060b5ca 100644 --- a/src/addressbook.cpp +++ b/src/addressbook.cpp @@ -38,10 +38,10 @@ void AddressBookModel::loadData() ); } -void AddressBookModel::addNewLabel(QString label, QString addr) +void AddressBookModel::addNewLabel(QString label, QString addr, QString myAddr) { //labels.push_back(QPair(label, addr)); - AddressBook::getInstance()->addAddressLabel(label, addr); + AddressBook::getInstance()->addAddressLabel(label, addr, myAddr); labels.clear(); labels = AddressBook::getInstance()->getAllAddressLabels(); dataChanged(index(0, 0), index(labels.size()-1, columnCount(index(0,0))-1)); @@ -53,22 +53,27 @@ void AddressBookModel::removeItemAt(int row) if (row >= labels.size()) return; - AddressBook::getInstance()->removeAddressLabel(labels[row].first, labels[row].second); + AddressBook::getInstance()->removeAddressLabel(labels[row].getName(), labels[row].getPartnerAddress()); labels.clear(); labels = AddressBook::getInstance()->getAllAddressLabels(); dataChanged(index(0, 0), index(labels.size()-1, columnCount(index(0,0))-1)); layoutChanged(); } -QPair AddressBookModel::itemAt(int row) +ContactItem AddressBookModel::itemAt(int row) { - if (row >= labels.size()) - return QPair(); + if (row >= labels.size()) + { + ContactItem item = ContactItem("", "", ""); + return item; + } + return labels.at(row); } + int AddressBookModel::rowCount(const QModelIndex&) const { return labels.size(); @@ -86,8 +91,8 @@ QVariant AddressBookModel::data(const QModelIndex &index, int role) const { switch(index.column()) { - case 0: return labels.at(index.row()).first; - case 1: return labels.at(index.row()).second; + case 0: return labels.at(index.row()).getName(); + case 1: return labels.at(index.row()).getPartnerAddress(); } } @@ -171,7 +176,7 @@ void AddressBook::open(MainWindow* parent, QLineEdit* target) return; } - model.addNewLabel(newLabel, ab.addr->text()); + model.addNewLabel(newLabel, ab.addr->text(), ""); }); // Import Button @@ -209,7 +214,7 @@ void AddressBook::open(MainWindow* parent, QLineEdit* target) continue; // Add label, address. - model.addNewLabel(items.at(1), items.at(0)); + model.addNewLabel(items.at(1), items.at(0), ""); numImported++; } @@ -233,8 +238,8 @@ void AddressBook::open(MainWindow* parent, QLineEdit* target) if (index.row() < 0) return; - QString lbl = model.itemAt(index.row()).first; - QString addr = model.itemAt(index.row()).second; + QString lbl = model.itemAt(index.row()).getName(); + QString addr = model.itemAt(index.row()).getPartnerAddress(); d.accept(); fnSetTargetLabelAddr(target, lbl, addr); }); @@ -246,8 +251,8 @@ void AddressBook::open(MainWindow* parent, QLineEdit* target) if (index.row() < 0) return; - QString lbl = model.itemAt(index.row()).first; - QString addr = model.itemAt(index.row()).second; + QString lbl = model.itemAt(index.row()).getName(); + QString addr = model.itemAt(index.row()).getPartnerAddress(); QMenu menu(parent); @@ -273,7 +278,7 @@ void AddressBook::open(MainWindow* parent, QLineEdit* target) auto selection = ab.addresses->selectionModel(); if (selection && selection->hasSelection() && selection->selectedRows().size() > 0) { auto item = model.itemAt(selection->selectedRows().at(0).row()); - fnSetTargetLabelAddr(target, item.first, item.second); + fnSetTargetLabelAddr(target, item.getName(), item.getPartnerAddress()); } }; @@ -307,15 +312,35 @@ void AddressBook::readFromStorage() file.open(QIODevice::ReadOnly); QDataStream in(&file); // read the data serialized from the file QString version; - in >> version >> allLabels; + in >> version; + 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++) + { + //qDebug() << "0:" << stuff[i][0]; + //qDebug() << "1:" << stuff[i][1]; + //qDebug() << "2:" << stuff[i][2]; + ContactItem contact = ContactItem(stuff[i][2], stuff[i][1], stuff[i][0]); + qDebug() << "contact=" << contact.toQTString(); + allLabels.push_back(contact); + } + + qDebug() << "Read " << version << " Hush contacts from disk..."; file.close(); } + else + { + qDebug() << "No Hush contacts found on disk!"; + } // Special. // Add the default silentdragon donation address if it isn't already present // QList allAddresses; // std::transform(allLabels.begin(), allLabels.end(), - // std::back_inserter(allAddresses), [=] (auto i) { return i.second; }); + // std::back_inserter(allAddresses), [=] (auto i) { return i.getPartnerAddress(); }); // if (!allAddresses.contains(Settings::getDonationAddr(true))) { // allLabels.append(QPair("silentdragon donation", Settings::getDonationAddr(true))); // } @@ -326,7 +351,16 @@ void AddressBook::writeToStorage() QFile file(AddressBook::writeableFile()); file.open(QIODevice::ReadWrite | QIODevice::Truncate); QDataStream out(&file); // we will serialize the data into the file - out << QString("v1") << allLabels; + QList> contacts; + for(auto &item: allLabels) + { + QList c; + c.push_back(item.getName()); + c.push_back(item.getPartnerAddress()); + c.push_back(item.getMyAddress()); + contacts.push_back(c); + } + out << QString("v1") << contacts; file.close(); } @@ -346,17 +380,17 @@ QString AddressBook::writeableFile() // Add a new address/label to the database -void AddressBook::addAddressLabel(QString label, QString address) +void AddressBook::addAddressLabel(QString label, QString address, QString myAddr) { Q_ASSERT(Settings::isValidAddress(address)); - - // First, remove any existing label + // getName(), remove any existing label // Iterate over the list and remove the label/address for (int i=0; i < allLabels.size(); i++) - if (allLabels[i].first == label) - removeAddressLabel(allLabels[i].first, allLabels[i].second); + if (allLabels[i].getName() == label) + removeAddressLabel(allLabels[i].getName(), allLabels[i].getPartnerAddress()); - allLabels.push_back(QPair(label, address)); + ContactItem item = ContactItem(myAddr, address, label); + allLabels.push_back(item); writeToStorage(); } @@ -366,7 +400,7 @@ void AddressBook::removeAddressLabel(QString label, QString address) // Iterate over the list and remove the label/address for (int i=0; i < allLabels.size(); i++) { - if (allLabels[i].first == label && allLabels[i].second == address) + if (allLabels[i].getName() == label && allLabels[i].getPartnerAddress() == address) { allLabels.removeAt(i); writeToStorage(); @@ -380,9 +414,9 @@ void AddressBook::updateLabel(QString oldlabel, QString address, QString newlabe // Iterate over the list and update the label/address for (int i = 0; i < allLabels.size(); i++) { - if (allLabels[i].first == oldlabel && allLabels[i].second == address) + if (allLabels[i].getName() == oldlabel && allLabels[i].getPartnerAddress() == address) { - allLabels[i].first = newlabel; + allLabels[i].setName(newlabel); writeToStorage(); return; } @@ -390,7 +424,7 @@ void AddressBook::updateLabel(QString oldlabel, QString address, QString newlabe } // Read all addresses -const QList>& AddressBook::getAllAddressLabels() +const QList& AddressBook::getAllAddressLabels() { if (allLabels.isEmpty()) readFromStorage(); @@ -402,8 +436,8 @@ const QList>& AddressBook::getAllAddressLabels() QString AddressBook::getLabelForAddress(QString addr) { for (auto i : allLabels) - if (i.second == addr) - return i.first; + if (i.getPartnerAddress() == addr) + return i.getName(); return ""; } @@ -412,8 +446,8 @@ QString AddressBook::getLabelForAddress(QString addr) QString AddressBook::getAddressForLabel(QString label) { for (auto i: allLabels) - if (i.first == label) - return i.second; + if (i.getName() == label) + return i.getPartnerAddress(); return ""; } diff --git a/src/addressbook.h b/src/addressbook.h index af64c07..13ba606 100644 --- a/src/addressbook.h +++ b/src/addressbook.h @@ -2,6 +2,7 @@ #define ADDRESSBOOK_H #include "precompiled.h" +#include "contactmodel.h" class MainWindow; @@ -10,10 +11,11 @@ class AddressBookModel : public QAbstractTableModel { public: AddressBookModel(QTableView* parent); ~AddressBookModel(); - - void addNewLabel(QString label, QString addr); + + void addNewLabel(QString label, QString address, QString myAddr); void removeItemAt(int row); - QPair itemAt(int row); + //QPair itemAt(int row); + ContactItem itemAt(int row); int rowCount(const QModelIndex &parent) const; int columnCount(const QModelIndex &parent) const; @@ -25,7 +27,8 @@ private: void saveData(); QTableView* parent; - QList> labels; + //QList> labels; + QList labels; QStringList headers; }; @@ -39,7 +42,7 @@ public: static QString addressFromAddressLabel(const QString& lblAddr); // Add a new address/label to the database - void addAddressLabel(QString label, QString address); + void addAddressLabel(QString label, QString address, QString myAddr); // Remove a new address/label from the database void removeAddressLabel(QString label, QString address); @@ -48,7 +51,7 @@ public: void updateLabel(QString oldlabel, QString address, QString newlabel); // Read all addresses - const QList>& getAllAddressLabels(); + const QList& getAllAddressLabels(); // Get an address's first label QString getLabelForAddress(QString address); @@ -61,7 +64,7 @@ private: void writeToStorage(); QString writeableFile(); - QList> allLabels; + QList allLabels; static AddressBook* instance; }; diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index e3374f1..1561d4e 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -86,8 +86,8 @@ void ChatModel::renderChatBox(QListWidget *view) myDateTime.setTime_t(c.second.getTimestamp()); qDebug() << "[" << myDateTime.toString("dd.MM.yyyy hh:mm:ss ") << "] " << "<" << c.second.getAddress() << "> :" << c.second.getMemo(); line += QString("[") + myDateTime.toString("dd.MM.yyyy hh:mm:ss ") + QString("] "); - line += QString("<") + QString(c.second.getAddress()) + QString("> :\n"); - line += QString(c.second.getMemo()); + line += QString("<") + QString(c.second.getContact()) + QString("> :\n"); + line += QString(c.second.getMemo()) + QString("\n"); view->addItem(line); line =""; } diff --git a/src/contactmodel.cpp b/src/contactmodel.cpp new file mode 100644 index 0000000..60df97b --- /dev/null +++ b/src/contactmodel.cpp @@ -0,0 +1,9 @@ +#include "contactmodel.h" + +void ContactModel::renderContactList(QListWidget* view) +{ + for(auto &c : this->_contacts) + { + view->addItem(c.getName()); + } +} \ No newline at end of file diff --git a/src/contactmodel.h b/src/contactmodel.h new file mode 100644 index 0000000..d1ce239 --- /dev/null +++ b/src/contactmodel.h @@ -0,0 +1,68 @@ +#ifndef CONTACTMODEL_H +#define CONTACTMODEL_H + +#include +#include +#include + +class ContactItem +{ + private: + QString _myAddress; + QString _partnerAddress; + QString _name; + + public: + ContactItem(); + ContactItem(QString myAddress, QString partnerAddress, QString name) + { + _myAddress = myAddress; + _partnerAddress = partnerAddress; + _name = name; + } + + QString getName() const + { + return _name; + } + + QString getMyAddress() const + { + return _myAddress; + } + + QString getPartnerAddress() const + { + return _partnerAddress; + } + + void setName(QString name) + { + _name = name; + } + + void setMyAddress(QString myAddress) + { + _myAddress = myAddress; + } + + void setPartnerAddress(QString partnerAddress) + { + _partnerAddress = partnerAddress; + } + + QString toQTString() + { + return _name + "|" + _partnerAddress + "|" + _myAddress; + } + +}; + +class ContactModel +{ + public: + ContactModel() {} + void renderContactList(QListWidget* view); +}; + +#endif \ No newline at end of file diff --git a/src/controller.cpp b/src/controller.cpp index daace4e..24560e3 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -11,6 +11,7 @@ DataStore* DataStore::instance = nullptr; template<> bool DataStore::instanced = false; ChatModel *chatModel = new ChatModel(); +ContactModel *contactModel = new ContactModel(); using json = nlohmann::json; diff --git a/src/controller.h b/src/controller.h index 32d843b..7684863 100644 --- a/src/controller.h +++ b/src/controller.h @@ -12,6 +12,7 @@ #include "liteinterface.h" #include "connection.h" #include "chatmodel.h" +#include "contactmodel.h" using json = nlohmann::json; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index b17f086..b243a6f 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1241,7 +1241,7 @@ void MainWindow::setupReceiveTab() { } else if (curLabel.isEmpty() && !label.isEmpty()) { info = "Added Label '" % label % "'"; - AddressBook::getInstance()->addAddressLabel(label, addr); + AddressBook::getInstance()->addAddressLabel(label, addr, ""); } // Update labels everywhere on the UI @@ -1291,7 +1291,7 @@ void MainWindow::updateTAddrCombo(bool checked) { auto allTaddrs = this->rpc->getModel()->getAllTAddresses(); QSet labels; for (auto p : AddressBook::getInstance()->getAllAddressLabels()) { - labels.insert(p.second); + labels.insert(p.getPartnerAddress()); } std::for_each(allTaddrs.begin(), allTaddrs.end(), [=, &addrs] (auto& taddr) { // If the address is in the address book, add it. diff --git a/src/sendtab.cpp b/src/sendtab.cpp index 3fc5510..6d4ba38 100644 --- a/src/sendtab.cpp +++ b/src/sendtab.cpp @@ -207,7 +207,7 @@ void MainWindow::updateLabelsAutoComplete() { auto labels = AddressBook::getInstance()->getAllAddressLabels(); std::transform(labels.begin(), labels.end(), std::back_inserter(list), [=] (auto la) -> QString { - return la.first % "/" % la.second; + return la.getName() % "/" % la.getPartnerAddress(); }); delete labelCompleter;