update// reformated code in file addressbook.cpp

This commit is contained in:
Strider
2020-04-08 13:21:52 +02:00
parent 20cf745a1b
commit 683c62b054
2 changed files with 139 additions and 90 deletions

BIN
SilentDragonLite Executable file

Binary file not shown.

View File

@@ -6,85 +6,99 @@
#include "controller.h" #include "controller.h"
AddressBookModel::AddressBookModel(QTableView *parent) AddressBookModel::AddressBookModel(QTableView *parent) : QAbstractTableModel(parent)
: QAbstractTableModel(parent) { {
headers << tr("Label") << tr("Address"); headers << tr("Label") << tr("Address");
this->parent = parent; this->parent = parent;
loadData(); loadData();
} }
AddressBookModel::~AddressBookModel() { AddressBookModel::~AddressBookModel()
{
saveData(); saveData();
} }
void AddressBookModel::saveData() { void AddressBookModel::saveData()
{
// Save column positions // Save column positions
QSettings().setValue("addresstablegeometry", parent->horizontalHeader()->saveState()); QSettings().setValue(
"addresstablegeometry",
parent->horizontalHeader()->saveState()
);
} }
void AddressBookModel::loadData() { void AddressBookModel::loadData()
{
labels = AddressBook::getInstance()->getAllAddressLabels(); labels = AddressBook::getInstance()->getAllAddressLabels();
parent->horizontalHeader()->restoreState(
parent->horizontalHeader()->restoreState(QSettings().value("addresstablegeometry").toByteArray()); QSettings().value(
"addresstablegeometry"
).toByteArray()
);
} }
void AddressBookModel::addNewLabel(QString label, QString addr) { void AddressBookModel::addNewLabel(QString label, QString addr)
{
//labels.push_back(QPair<QString, QString>(label, addr)); //labels.push_back(QPair<QString, QString>(label, addr));
AddressBook::getInstance()->addAddressLabel(label, addr); AddressBook::getInstance()->addAddressLabel(label, addr);
labels.clear(); labels.clear();
labels = AddressBook::getInstance()->getAllAddressLabels(); labels = AddressBook::getInstance()->getAllAddressLabels();
dataChanged(index(0, 0), index(labels.size()-1, columnCount(index(0,0))-1)); dataChanged(index(0, 0), index(labels.size()-1, columnCount(index(0,0))-1));
layoutChanged(); layoutChanged();
} }
void AddressBookModel::removeItemAt(int row) { void AddressBookModel::removeItemAt(int row)
{
if (row >= labels.size()) if (row >= labels.size())
return; return;
AddressBook::getInstance()->removeAddressLabel(labels[row].first, labels[row].second); AddressBook::getInstance()->removeAddressLabel(labels[row].first, labels[row].second);
labels.clear(); labels.clear();
labels = AddressBook::getInstance()->getAllAddressLabels(); labels = AddressBook::getInstance()->getAllAddressLabels();
dataChanged(index(0, 0), index(labels.size()-1, columnCount(index(0,0))-1)); dataChanged(index(0, 0), index(labels.size()-1, columnCount(index(0,0))-1));
layoutChanged(); layoutChanged();
} }
QPair<QString, QString> AddressBookModel::itemAt(int row) { QPair<QString, QString> AddressBookModel::itemAt(int row)
if (row >= labels.size()) return QPair<QString, QString>(); {
if (row >= labels.size())
return QPair<QString, QString>();
return labels.at(row); return labels.at(row);
} }
int AddressBookModel::rowCount(const QModelIndex&) const { int AddressBookModel::rowCount(const QModelIndex&) const
{
return labels.size(); return labels.size();
} }
int AddressBookModel::columnCount(const QModelIndex&) const { int AddressBookModel::columnCount(const QModelIndex&) const
{
return headers.size(); return headers.size();
} }
QVariant AddressBookModel::data(const QModelIndex &index, int role) const { QVariant AddressBookModel::data(const QModelIndex &index, int role) const
if (role == Qt::DisplayRole) { {
switch(index.column()) { if (role == Qt::DisplayRole)
{
switch(index.column())
{
case 0: return labels.at(index.row()).first; case 0: return labels.at(index.row()).first;
case 1: return labels.at(index.row()).second; case 1: return labels.at(index.row()).second;
} }
} }
return QVariant(); return QVariant();
} }
QVariant AddressBookModel::headerData(int section, Qt::Orientation orientation, int role) const { QVariant AddressBookModel::headerData(int section, Qt::Orientation orientation, int role) const
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) { {
if (role == Qt::DisplayRole && orientation == Qt::Horizontal)
return headers.at(section); return headers.at(section);
}
return QVariant(); return QVariant();
} }
@@ -93,28 +107,27 @@ QVariant AddressBookModel::headerData(int section, Qt::Orientation orientation,
//=============== //===============
// AddressBook // AddressBook
//=============== //===============
void AddressBook::open(MainWindow* parent, QLineEdit* target) { void AddressBook::open(MainWindow* parent, QLineEdit* target)
{
QDialog d(parent); QDialog d(parent);
Ui_addressBook ab; Ui_addressBook ab;
ab.setupUi(&d); ab.setupUi(&d);
Settings::saveRestore(&d); Settings::saveRestore(&d);
QRegExpValidator v(QRegExp(Settings::labelRegExp), ab.label); QRegExpValidator v(QRegExp(Settings::labelRegExp), ab.label);
ab.label->setValidator(&v); ab.label->setValidator(&v);
AddressBookModel model(ab.addresses); AddressBookModel model(ab.addresses);
ab.addresses->setModel(&model); ab.addresses->setModel(&model);
// If there is no target, the we'll call the button "Ok", else "Pick" // If there is no target, the we'll call the button "Ok", else "Pick"
if (target != nullptr) { if (target != nullptr)
ab.buttonBox->button(QDialogButtonBox::Ok)->setText(QObject::tr("Pick")); ab.buttonBox->button(QDialogButtonBox::Ok)->setText(QObject::tr("Pick"));
}
// Connect the dialog's closing to updating the label address completor // Connect the dialog's closing to updating the label address completor
QObject::connect(&d, &QDialog::finished, [=] (auto) { parent->updateLabels(); }); QObject::connect(&d, &QDialog::finished, [=] (auto) { parent->updateLabels(); });
// If there is a target then make it the addr for the "Add to" button // If there is a target then make it the addr for the "Add to" button
if (target != nullptr && Settings::isValidAddress(target->text())) { if (target != nullptr && Settings::isValidAddress(target->text()))
{
ab.addr->setText(target->text()); ab.addr->setText(target->text());
ab.label->setFocus(); ab.label->setFocus();
} }
@@ -124,26 +137,37 @@ void AddressBook::open(MainWindow* parent, QLineEdit* target) {
auto addr = ab.addr->text().trimmed(); auto addr = ab.addr->text().trimmed();
QString newLabel = ab.label->text(); QString newLabel = ab.label->text();
if (addr.isEmpty() || newLabel.isEmpty()) { if (addr.isEmpty() || newLabel.isEmpty())
QMessageBox::critical(parent, QObject::tr("Address or Label Error"), {
QObject::tr("Address or Label cannot be empty"), QMessageBox::Ok); QMessageBox::critical(
parent,
QObject::tr("Address or Label Error"),
QObject::tr("Address or Label cannot be empty"),
QMessageBox::Ok
);
return; return;
} }
// Test if address is valid. // Test if address is valid.
if (!Settings::isValidAddress(addr)) { if (!Settings::isValidAddress(addr))
QMessageBox::critical(parent, QObject::tr("Address Format Error"), {
QObject::tr("%1 doesn't seem to be a valid hush address.") QMessageBox::critical(
.arg(addr), parent,
QMessageBox::Ok); QObject::tr("Address Format Error"),
QObject::tr("%1 doesn't seem to be a valid hush address.").arg(addr),
QMessageBox::Ok
);
return; return;
} }
// Don't allow duplicate address labels. // Don't allow duplicate address labels.
if (!getInstance()->getAddressForLabel(newLabel).isEmpty()) { if (!getInstance()->getAddressForLabel(newLabel).isEmpty())
QMessageBox::critical(parent, QObject::tr("Label Error"), {
QObject::tr("The label '%1' already exists. Please remove the existing label.") QMessageBox::critical(
.arg(newLabel), parent,
QMessageBox::Ok); QObject::tr("Label Error"),
QObject::tr("The label '%1' already exists. Please remove the existing label.").arg(newLabel),
QMessageBox::Ok
);
return; return;
} }
@@ -153,21 +177,30 @@ void AddressBook::open(MainWindow* parent, QLineEdit* target) {
// Import Button // Import Button
QObject::connect(ab.btnImport, &QPushButton::clicked, [&] () { QObject::connect(ab.btnImport, &QPushButton::clicked, [&] () {
// Get the import file name. // Get the import file name.
auto fileName = QFileDialog::getOpenFileUrl(&d, QObject::tr("Import Address Book"), QUrl(), auto fileName = QFileDialog::getOpenFileUrl(
"CSV file (*.csv)"); &d, QObject::tr("Import Address Book"),
QUrl(),
"CSV file (*.csv)"
);
if (fileName.isEmpty()) if (fileName.isEmpty())
return; return;
QFile file(fileName.toLocalFile()); QFile file(fileName.toLocalFile());
if (!file.open(QIODevice::ReadOnly)) { if (!file.open(QIODevice::ReadOnly))
QMessageBox::information(&d, QObject::tr("Unable to open file"), file.errorString()); {
QMessageBox::information(
&d,
QObject::tr("Unable to open file"),
file.errorString()
);
return; return;
} }
QTextStream in(&file); QTextStream in(&file);
QString line; QString line;
int numImported = 0; int numImported = 0;
while (in.readLineInto(&line)) { while (in.readLineInto(&line))
{
QStringList items = line.split(","); QStringList items = line.split(",");
if (items.size() != 2) if (items.size() != 2)
continue; continue;
@@ -180,8 +213,11 @@ void AddressBook::open(MainWindow* parent, QLineEdit* target) {
numImported++; numImported++;
} }
QMessageBox::information(&d, QObject::tr("Address Book Import Done"), QMessageBox::information(
QObject::tr("Imported %1 new Address book entries").arg(numImported)); &d,
QObject::tr("Address Book Import Done"),
QObject::tr("Imported %1 new Address book entries").arg(numImported)
);
}); });
auto fnSetTargetLabelAddr = [=] (QLineEdit* target, QString label, QString addr) { auto fnSetTargetLabelAddr = [=] (QLineEdit* target, QString label, QString addr) {
@@ -194,7 +230,8 @@ void AddressBook::open(MainWindow* parent, QLineEdit* target) {
if (!target) if (!target)
return; return;
if (index.row() < 0) return; if (index.row() < 0)
return;
QString lbl = model.itemAt(index.row()).first; QString lbl = model.itemAt(index.row()).first;
QString addr = model.itemAt(index.row()).second; QString addr = model.itemAt(index.row()).second;
@@ -206,20 +243,19 @@ void AddressBook::open(MainWindow* parent, QLineEdit* target) {
ab.addresses->setContextMenuPolicy(Qt::CustomContextMenu); ab.addresses->setContextMenuPolicy(Qt::CustomContextMenu);
QObject::connect(ab.addresses, &QTableView::customContextMenuRequested, [&] (QPoint pos) { QObject::connect(ab.addresses, &QTableView::customContextMenuRequested, [&] (QPoint pos) {
QModelIndex index = ab.addresses->indexAt(pos); QModelIndex index = ab.addresses->indexAt(pos);
if (index.row() < 0)
if (index.row() < 0) return; return;
QString lbl = model.itemAt(index.row()).first; QString lbl = model.itemAt(index.row()).first;
QString addr = model.itemAt(index.row()).second; QString addr = model.itemAt(index.row()).second;
QMenu menu(parent); QMenu menu(parent);
if (target != nullptr) { if (target != nullptr)
menu.addAction("Pick", [&] () { menu.addAction("Pick", [&] () {
d.accept(); d.accept();
fnSetTargetLabelAddr(target, lbl, addr); fnSetTargetLabelAddr(target, lbl, addr);
}); });
}
menu.addAction(QObject::tr("Copy address"), [&] () { menu.addAction(QObject::tr("Copy address"), [&] () {
QGuiApplication::clipboard()->setText(addr); QGuiApplication::clipboard()->setText(addr);
@@ -248,27 +284,30 @@ void AddressBook::open(MainWindow* parent, QLineEdit* target) {
//============= //=============
// AddressBook singleton class // AddressBook singleton class
//============= //=============
AddressBook* AddressBook::getInstance() { AddressBook* AddressBook::getInstance()
{
if (!instance) if (!instance)
instance = new AddressBook(); instance = new AddressBook();
return instance; return instance;
} }
AddressBook::AddressBook() { AddressBook::AddressBook()
{
readFromStorage(); readFromStorage();
} }
void AddressBook::readFromStorage() { void AddressBook::readFromStorage()
{
QFile file(AddressBook::writeableFile()); QFile file(AddressBook::writeableFile());
if (file.exists()) { if (file.exists())
{
allLabels.clear(); allLabels.clear();
file.open(QIODevice::ReadOnly); file.open(QIODevice::ReadOnly);
QDataStream in(&file); // read the data serialized from the file QDataStream in(&file); // read the data serialized from the file
QString version; QString version;
in >> version >> allLabels; in >> version >> allLabels;
file.close(); file.close();
} }
@@ -282,7 +321,8 @@ void AddressBook::readFromStorage() {
// } // }
} }
void AddressBook::writeToStorage() { void AddressBook::writeToStorage()
{
QFile file(AddressBook::writeableFile()); QFile file(AddressBook::writeableFile());
file.open(QIODevice::ReadWrite | QIODevice::Truncate); file.open(QIODevice::ReadWrite | QIODevice::Truncate);
QDataStream out(&file); // we will serialize the data into the file QDataStream out(&file); // we will serialize the data into the file
@@ -290,42 +330,44 @@ void AddressBook::writeToStorage() {
file.close(); file.close();
} }
QString AddressBook::writeableFile() { QString AddressBook::writeableFile()
{
auto filename = QStringLiteral("addresslabels.dat"); auto filename = QStringLiteral("addresslabels.dat");
auto dir = QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)); auto dir = QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
if (!dir.exists()) if (!dir.exists())
QDir().mkpath(dir.absolutePath()); QDir().mkpath(dir.absolutePath());
if (Settings::getInstance()->isTestnet()) { if (Settings::getInstance()->isTestnet())
return dir.filePath("testnet-" % filename); return dir.filePath("testnet-" % filename);
} else {
else
return dir.filePath(filename); return dir.filePath(filename);
}
} }
// Add a new address/label to the database // Add a new address/label to the database
void AddressBook::addAddressLabel(QString label, QString address) { void AddressBook::addAddressLabel(QString label, QString address)
{
Q_ASSERT(Settings::isValidAddress(address)); Q_ASSERT(Settings::isValidAddress(address));
// First, remove any existing label // First, remove any existing label
// Iterate over the list and remove the label/address // Iterate over the list and remove the label/address
for (int i=0; i < allLabels.size(); i++) { for (int i=0; i < allLabels.size(); i++)
if (allLabels[i].first == label) { if (allLabels[i].first == label)
removeAddressLabel(allLabels[i].first, allLabels[i].second); removeAddressLabel(allLabels[i].first, allLabels[i].second);
}
}
allLabels.push_back(QPair<QString, QString>(label, address)); allLabels.push_back(QPair<QString, QString>(label, address));
writeToStorage(); writeToStorage();
} }
// Remove a new address/label from the database // Remove a new address/label from the database
void AddressBook::removeAddressLabel(QString label, QString address) { void AddressBook::removeAddressLabel(QString label, QString address)
{
// Iterate over the list and remove the label/address // Iterate over the list and remove the label/address
for (int i=0; i < allLabels.size(); i++) { for (int i=0; i < allLabels.size(); i++)
if (allLabels[i].first == label && allLabels[i].second == address) { {
if (allLabels[i].first == label && allLabels[i].second == address)
{
allLabels.removeAt(i); allLabels.removeAt(i);
writeToStorage(); writeToStorage();
return; return;
@@ -333,10 +375,13 @@ void AddressBook::removeAddressLabel(QString label, QString address) {
} }
} }
void AddressBook::updateLabel(QString oldlabel, QString address, QString newlabel) { void AddressBook::updateLabel(QString oldlabel, QString address, QString newlabel)
{
// Iterate over the list and update the label/address // Iterate over the list and update the label/address
for (int i = 0; i < allLabels.size(); i++) { for (int i = 0; i < allLabels.size(); i++)
if (allLabels[i].first == oldlabel && allLabels[i].second == address) { {
if (allLabels[i].first == oldlabel && allLabels[i].second == address)
{
allLabels[i].first = newlabel; allLabels[i].first = newlabel;
writeToStorage(); writeToStorage();
return; return;
@@ -345,42 +390,46 @@ void AddressBook::updateLabel(QString oldlabel, QString address, QString newlabe
} }
// Read all addresses // Read all addresses
const QList<QPair<QString, QString>>& AddressBook::getAllAddressLabels() { const QList<QPair<QString, QString>>& AddressBook::getAllAddressLabels()
if (allLabels.isEmpty()) { {
if (allLabels.isEmpty())
readFromStorage(); readFromStorage();
}
return allLabels; return allLabels;
} }
// Get the label for an address // Get the label for an address
QString AddressBook::getLabelForAddress(QString addr) { QString AddressBook::getLabelForAddress(QString addr)
for (auto i : allLabels) { {
for (auto i : allLabels)
if (i.second == addr) if (i.second == addr)
return i.first; return i.first;
}
return ""; return "";
} }
// Get the address for a label // Get the address for a label
QString AddressBook::getAddressForLabel(QString label) { QString AddressBook::getAddressForLabel(QString label)
for (auto i: allLabels) { {
for (auto i: allLabels)
if (i.first == label) if (i.first == label)
return i.second; return i.second;
}
return ""; return "";
} }
QString AddressBook::addLabelToAddress(QString addr) { QString AddressBook::addLabelToAddress(QString addr)
{
QString label = AddressBook::getInstance()->getLabelForAddress(addr); QString label = AddressBook::getInstance()->getLabelForAddress(addr);
if (!label.isEmpty()) if (!label.isEmpty())
return label + "/" + addr; return label + "/" + addr;
else else
return addr; return addr;
} }
QString AddressBook::addressFromAddressLabel(const QString& lblAddr) { QString AddressBook::addressFromAddressLabel(const QString& lblAddr)
{
return lblAddr.trimmed().split("/").last(); return lblAddr.trimmed().split("/").last();
} }