From 863675470360ef396b450f5d283fff1cc928c7ea Mon Sep 17 00:00:00 2001 From: Duke Leto Date: Sun, 16 Feb 2020 06:00:12 -0500 Subject: [PATCH] Large addressbook refactor to use QList, to support contacts having more than 2 data fields We still need deal with converting v1 addressbooks to v2 --- src/addressbook.cpp | 62 +++++++++++++++++++++++++-------------------- src/addressbook.h | 17 ++++++++----- src/mainwindow.cpp | 26 +++++++++---------- src/mainwindow.h | 4 +-- src/sendtab.cpp | 3 ++- 5 files changed, 60 insertions(+), 52 deletions(-) diff --git a/src/addressbook.cpp b/src/addressbook.cpp index 4e0694a..c8723b2 100644 --- a/src/addressbook.cpp +++ b/src/addressbook.cpp @@ -32,9 +32,9 @@ void AddressBookModel::loadData() { parent->horizontalHeader()->restoreState(QSettings().value("addresstablegeometry").toByteArray()); } -void AddressBookModel::addNewLabel(QString label, QString addr) { +void AddressBookModel::addNewLabel(QString label, QString addr, QString myZaddr) { //labels.push_back(QPair(label, addr)); - AddressBook::getInstance()->addAddressLabel(label, addr); + AddressBook::getInstance()->addAddressLabel(label, addr, myZaddr); labels.clear(); labels = AddressBook::getInstance()->getAllAddressLabels(); @@ -47,7 +47,7 @@ void AddressBookModel::removeItemAt(int row) { if (row >= labels.size()) return; - AddressBook::getInstance()->removeAddressLabel(labels[row].first, labels[row].second); + AddressBook::getInstance()->removeAddressLabel(labels[row][0], labels[row][1]); labels.clear(); labels = AddressBook::getInstance()->getAllAddressLabels(); @@ -56,8 +56,8 @@ void AddressBookModel::removeItemAt(int row) { layoutChanged(); } -QPair AddressBookModel::itemAt(int row) { - if (row >= labels.size()) return QPair(); +QList AddressBookModel::itemAt(int row) { + if (row >= labels.size()) return QList(); return labels.at(row); } @@ -75,8 +75,8 @@ int AddressBookModel::columnCount(const QModelIndex&) const { QVariant AddressBookModel::data(const QModelIndex &index, int role) const { if (role == Qt::DisplayRole) { 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())[0]; + case 1: return labels.at(index.row())[1]; } } return QVariant(); @@ -149,7 +149,7 @@ void AddressBook::open(MainWindow* parent, QLineEdit* target) { return; } - model.addNewLabel(newLabel, ab.addr->text()); + model.addNewLabel(newLabel, ab.addr->text(),""); }); // Import Button @@ -178,7 +178,8 @@ void AddressBook::open(MainWindow* parent, QLineEdit* target) { continue; // Add label, address. - model.addNewLabel(items.at(1), items.at(0)); + //TODO: myzaddr + model.addNewLabel(items.at(1), items.at(0), ""); numImported++; } @@ -198,8 +199,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())[0]; + QString addr = model.itemAt(index.row())[1]; d.accept(); fnSetTargetLabelAddr(target, lbl, addr); }); @@ -211,8 +212,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())[0]; + QString addr = model.itemAt(index.row())[1]; QMenu menu(parent); @@ -239,7 +240,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[0], item[1]); } }; @@ -269,16 +270,20 @@ void AddressBook::readFromStorage() { file.open(QIODevice::ReadOnly); QDataStream in(&file); // read the data serialized from the file QString version; + //TODO: Do we need to convert v1 address file to v2, or is QT smart enough? in >> version >> allLabels; + qDebug() << "Read " << version << " Hush contacts from disk..."; file.close(); + } else { + qDebug() << "No Hush contacts found on disk!"; } // Special. // Add the default ZecWallet 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[1]; }); // if (!allAddresses.contains(Settings::getDonationAddr())) { // allLabels.append(QPair("ZecWallet donation", Settings::getDonationAddr())); // } @@ -288,7 +293,7 @@ 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; + out << QString("v2") << allLabels; file.close(); } @@ -308,18 +313,19 @@ 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 myzaddr) { Q_ASSERT(Settings::isValidAddress(address)); // First, 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][0] == label) { + removeAddressLabel(allLabels[i][0], allLabels[i][1]); } } - allLabels.push_back(QPair(label, address)); + QList contact = { label, address, myzaddr }; + allLabels.push_back(contact); writeToStorage(); } @@ -327,7 +333,7 @@ void AddressBook::addAddressLabel(QString label, QString address) { 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][0] == label && allLabels[i][1] == address) { allLabels.removeAt(i); writeToStorage(); return; @@ -338,8 +344,8 @@ void AddressBook::removeAddressLabel(QString label, QString address) { void AddressBook::updateLabel(QString oldlabel, QString address, QString newlabel) { // 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) { - allLabels[i].first = newlabel; + if (allLabels[i][0] == oldlabel && allLabels[i][1] == address) { + allLabels[i][0] = newlabel; writeToStorage(); return; } @@ -347,7 +353,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(); } @@ -357,8 +363,8 @@ const QList>& AddressBook::getAllAddressLabels() { // Get the label for an address QString AddressBook::getLabelForAddress(QString addr) { for (auto i : allLabels) { - if (i.second == addr) - return i.first; + if (i[1] == addr) + return i[0]; } return ""; @@ -367,8 +373,8 @@ QString AddressBook::getLabelForAddress(QString addr) { // Get the address for a label QString AddressBook::getAddressForLabel(QString label) { for (auto i: allLabels) { - if (i.first == label) - return i.second; + if (i[0] == label) + return i[1]; } return ""; diff --git a/src/addressbook.h b/src/addressbook.h index af64c07..72e0336 100644 --- a/src/addressbook.h +++ b/src/addressbook.h @@ -1,3 +1,5 @@ +// Copyright 2019-2020 The Hush Developers +// Released under the GPLv3 #ifndef ADDRESSBOOK_H #define ADDRESSBOOK_H @@ -11,9 +13,9 @@ public: AddressBookModel(QTableView* parent); ~AddressBookModel(); - void addNewLabel(QString label, QString addr); + void addNewLabel(QString label, QString addr, QString myzaddr); void removeItemAt(int row); - QPair itemAt(int row); + QList itemAt(int row); int rowCount(const QModelIndex &parent) const; int columnCount(const QModelIndex &parent) const; @@ -25,7 +27,7 @@ private: void saveData(); QTableView* parent; - QList> labels; + QList> labels; QStringList headers; }; @@ -39,7 +41,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 myZaddr); // Remove a new address/label from the database void removeAddressLabel(QString label, QString address); @@ -48,7 +50,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,9 +63,10 @@ private: void writeToStorage(); QString writeableFile(); - QList> allLabels; + // contact name, contact address, myzaddr (the zaddr we use to receive from this contact) + QList> allLabels; static AddressBook* instance; }; -#endif // ADDRESSBOOK_H \ No newline at end of file +#endif // ADDRESSBOOK_H diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index c9ef303..21387e0 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -30,12 +30,12 @@ void QListView::selectionChanged(const QItemSelection& selected, const QItemSele } QString MainWindow::getZaddrForContact(QString contact) { - QList> addressLabels = AddressBook::getInstance()->getAllAddressLabels(); + QList> addressLabels = AddressBook::getInstance()->getAllAddressLabels(); for (int i = 0; i < addressLabels.size(); ++i) { - QPair pair = addressLabels.at(i); - if (pair.first == contact) { - qDebug() << "Found contact " << pair.first << " " << pair.second; - return pair.second; + QList thisContact = addressLabels.at(i); + if (thisContact[0] == contact) { + qDebug() << "Found contact " << thisContact[0] << " " << thisContact[1]; + return thisContact[1]; } } return ""; @@ -1181,14 +1181,14 @@ void MainWindow::setupHushTab() { void MainWindow::setupChatTab() { qDebug() << __FUNCTION__; - QList> addressLabels = AddressBook::getInstance()->getAllAddressLabels(); + QList> addressLabels = AddressBook::getInstance()->getAllAddressLabels(); QStringListModel *chatModel = new QStringListModel(); QStringList contacts; //contacts << "Alice" << "Bob" << "Charlie" << "Eve"; for (int i = 0; i < addressLabels.size(); ++i) { - QPair pair = addressLabels.at(i); - qDebug() << "Found contact " << pair.first << " " << pair.second; - contacts << pair.first; + QList thisContact = addressLabels.at(i); + qDebug() << "Found contact " << thisContact[0] << " " << thisContact[1]; + contacts << thisContact[0]; } chatModel->setStringList(contacts); @@ -1534,14 +1534,12 @@ void MainWindow::setupReceiveTab() { if (!curLabel.isEmpty() && label.isEmpty()) { info = "Removed Label '" % curLabel % "'"; AddressBook::getInstance()->removeAddressLabel(curLabel, addr); - } - else if (!curLabel.isEmpty() && !label.isEmpty()) { + } else if (!curLabel.isEmpty() && !label.isEmpty()) { info = "Updated Label '" % curLabel % "' to '" % label % "'"; AddressBook::getInstance()->updateLabel(curLabel, addr, label); - } - else if (curLabel.isEmpty() && !label.isEmpty()) { + } 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 diff --git a/src/mainwindow.h b/src/mainwindow.h index 63c334c..7109c4f 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -46,8 +46,8 @@ private: QString nickname; QString zaddr; QString myZaddr; - int64_t lastSentTime; - int64_t lastReceivedTime; + //int64_t lastSentTime; + //int64_t lastReceivedTime; }; diff --git a/src/sendtab.cpp b/src/sendtab.cpp index 0567149..0b56f17 100644 --- a/src/sendtab.cpp +++ b/src/sendtab.cpp @@ -1,4 +1,5 @@ // Copyright 2019-2020 Hush developers +// Released under the GPLv3 #include "mainwindow.h" #include "ui_mainwindow.h" #include "addressbook.h" @@ -124,7 +125,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[0] % "/" % la[1]; }); delete labelCompleter;