Browse Source

update// refactored addressbook with new storage format

pull/130/head
Strider 4 years ago
parent
commit
2efec07e42
  1. 5
      compile.log
  2. 98
      src/addressbook.cpp
  3. 17
      src/addressbook.h
  4. 4
      src/chatmodel.cpp
  5. 9
      src/contactmodel.cpp
  6. 68
      src/contactmodel.h
  7. 1
      src/controller.cpp
  8. 1
      src/controller.h
  9. 4
      src/mainwindow.cpp
  10. 2
      src/sendtab.cpp

5
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

98
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<QString, QString>(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<QString, QString> AddressBookModel::itemAt(int row)
ContactItem AddressBookModel::itemAt(int row)
{
if (row >= labels.size())
return QPair<QString, QString>();
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<QList<QString>> 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<QString> 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<QString, QString>("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<QList<QString>> contacts;
for(auto &item: allLabels)
{
QList<QString> 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<QString, QString>(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<QPair<QString, QString>>& AddressBook::getAllAddressLabels()
const QList<ContactItem>& AddressBook::getAllAddressLabels()
{
if (allLabels.isEmpty())
readFromStorage();
@ -402,8 +436,8 @@ const QList<QPair<QString, QString>>& 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 "";
}

17
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<QString, QString> itemAt(int row);
//QPair<QString, QString> 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<QPair<QString, QString>> labels;
//QList<QPair<QString, QString>> labels;
QList<ContactItem> 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<QPair<QString, QString>>& getAllAddressLabels();
const QList<ContactItem>& getAllAddressLabels();
// Get an address's first label
QString getLabelForAddress(QString address);
@ -61,7 +64,7 @@ private:
void writeToStorage();
QString writeableFile();
QList<QPair<QString, QString>> allLabels;
QList<ContactItem> allLabels;
static AddressBook* instance;
};

4
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 ="";
}

9
src/contactmodel.cpp

@ -0,0 +1,9 @@
#include "contactmodel.h"
void ContactModel::renderContactList(QListWidget* view)
{
for(auto &c : this->_contacts)
{
view->addItem(c.getName());
}
}

68
src/contactmodel.h

@ -0,0 +1,68 @@
#ifndef CONTACTMODEL_H
#define CONTACTMODEL_H
#include <vector>
#include <QString>
#include <QListWidget>
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

1
src/controller.cpp

@ -11,6 +11,7 @@ DataStore<QString>* DataStore<QString>::instance = nullptr;
template<>
bool DataStore<QString>::instanced = false;
ChatModel *chatModel = new ChatModel();
ContactModel *contactModel = new ContactModel();
using json = nlohmann::json;

1
src/controller.h

@ -12,6 +12,7 @@
#include "liteinterface.h"
#include "connection.h"
#include "chatmodel.h"
#include "contactmodel.h"
using json = nlohmann::json;

4
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<QString> 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.

2
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;

Loading…
Cancel
Save