Refactor Addressbook
This commit is contained in:
@@ -18,22 +18,23 @@ AddressBookModel::~AddressBookModel() {
|
||||
}
|
||||
|
||||
void AddressBookModel::saveData() {
|
||||
AddressBook::writeToStorage(labels);
|
||||
|
||||
// Save column positions
|
||||
QSettings().setValue("addresstablegeometry", parent->horizontalHeader()->saveState());
|
||||
}
|
||||
|
||||
|
||||
void AddressBookModel::loadData() {
|
||||
labels = AddressBook::readFromStorage();
|
||||
labels = AddressBook::getInstance()->getAllAddressLabels();
|
||||
|
||||
parent->horizontalHeader()->restoreState(QSettings().value("addresstablegeometry").toByteArray());
|
||||
}
|
||||
|
||||
void AddressBookModel::addNewLabel(QString label, QString addr) {
|
||||
labels.push_back(QPair<QString, QString>(label, addr));
|
||||
AddressBook::writeToStorage(labels);
|
||||
//labels.push_back(QPair<QString, QString>(label, addr));
|
||||
AddressBook::getInstance()->addAddressLabel(label, addr);
|
||||
|
||||
labels.clear();
|
||||
labels = AddressBook::getInstance()->getAllAddressLabels();
|
||||
|
||||
dataChanged(index(0, 0), index(labels.size()-1, columnCount(index(0,0))-1));
|
||||
layoutChanged();
|
||||
@@ -43,9 +44,10 @@ void AddressBookModel::removeItemAt(int row) {
|
||||
if (row >= labels.size())
|
||||
return;
|
||||
|
||||
labels.removeAt(row);
|
||||
AddressBook::writeToStorage(labels);
|
||||
|
||||
AddressBook::getInstance()->removeAddressLabel(labels[row].first, labels[row].second);
|
||||
|
||||
labels.clear();
|
||||
labels = AddressBook::getInstance()->getAllAddressLabels();
|
||||
|
||||
dataChanged(index(0, 0), index(labels.size()-1, columnCount(index(0,0))-1));
|
||||
layoutChanged();
|
||||
@@ -86,6 +88,10 @@ QVariant AddressBookModel::headerData(int section, Qt::Orientation orientation,
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
|
||||
//===============
|
||||
// AddressBook
|
||||
//===============
|
||||
void AddressBook::open(MainWindow* parent, QLineEdit* target) {
|
||||
QDialog d(parent);
|
||||
Ui_addressBook ab;
|
||||
@@ -180,31 +186,41 @@ void AddressBook::open(MainWindow* parent, QLineEdit* target) {
|
||||
};
|
||||
}
|
||||
|
||||
QList<QPair<QString, QString>> AddressBook::readFromStorage() {
|
||||
//=============
|
||||
// AddressBook singleton class
|
||||
//=============
|
||||
AddressBook* AddressBook::getInstance() {
|
||||
if (!instance)
|
||||
instance = new AddressBook();
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
AddressBook::AddressBook() {
|
||||
readFromStorage();
|
||||
}
|
||||
|
||||
void AddressBook::readFromStorage() {
|
||||
QFile file(AddressBook::writeableFile());
|
||||
|
||||
QList<QPair<QString, QString>> labels;
|
||||
|
||||
if (!file.exists()) {
|
||||
return labels;
|
||||
return;
|
||||
}
|
||||
|
||||
allLabels.clear();
|
||||
file.open(QIODevice::ReadOnly);
|
||||
QDataStream in(&file); // read the data serialized from the file
|
||||
QString version;
|
||||
in >> version >> labels;
|
||||
in >> version >> allLabels;
|
||||
|
||||
file.close();
|
||||
|
||||
return labels;
|
||||
}
|
||||
|
||||
|
||||
void AddressBook::writeToStorage(QList<QPair<QString, QString>> labels) {
|
||||
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") << labels;
|
||||
out << QString("v1") << allLabels;
|
||||
file.close();
|
||||
}
|
||||
|
||||
@@ -221,3 +237,40 @@ QString AddressBook::writeableFile() {
|
||||
return dir.filePath(filename);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Add a new address/label to the database
|
||||
void AddressBook::addAddressLabel(QString label, QString address) {
|
||||
Q_ASSERT(Settings::isValidAddress(address));
|
||||
|
||||
allLabels.push_back(QPair<QString, QString>(label, address));
|
||||
writeToStorage();
|
||||
}
|
||||
|
||||
// Remove a new address/label from the database
|
||||
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)
|
||||
allLabels.removeAt(i);
|
||||
writeToStorage();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Read all addresses
|
||||
const QList<QPair<QString, QString>>& AddressBook::getAllAddressLabels() {
|
||||
return allLabels;
|
||||
}
|
||||
|
||||
// Get the label for an address
|
||||
QString AddressBook::getLabelForAddress(QString addr) {
|
||||
for (auto i : allLabels) {
|
||||
if (i.second == addr)
|
||||
return i.first;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
AddressBook* AddressBook::instance = nullptr;
|
||||
@@ -31,12 +31,32 @@ private:
|
||||
|
||||
class AddressBook {
|
||||
public:
|
||||
// Method that opens the AddressBook dialog window.
|
||||
static void open(MainWindow* parent, QLineEdit* target = nullptr);
|
||||
|
||||
static QList<QPair<QString, QString>> readFromStorage();
|
||||
static void writeToStorage(QList<QPair<QString, QString>> labels);
|
||||
static AddressBook* getInstance();
|
||||
|
||||
static QString writeableFile();
|
||||
// Add a new address/label to the database
|
||||
void addAddressLabel(QString label, QString address);
|
||||
|
||||
// Remove a new address/label from the database
|
||||
void removeAddressLabel(QString label, QString address);
|
||||
|
||||
// Read all addresses
|
||||
const QList<QPair<QString, QString>>& getAllAddressLabels();
|
||||
|
||||
// Get an address's first label
|
||||
QString getLabelForAddress(QString address);
|
||||
private:
|
||||
AddressBook();
|
||||
|
||||
void readFromStorage();
|
||||
void writeToStorage();
|
||||
|
||||
QString writeableFile();
|
||||
QList<QPair<QString, QString>> allLabels;
|
||||
|
||||
static AddressBook* instance;
|
||||
};
|
||||
|
||||
#endif // ADDRESSBOOK_H
|
||||
@@ -97,8 +97,6 @@ private:
|
||||
|
||||
void restoreSavedStates();
|
||||
|
||||
QString addressFromAddressField(const QString& lblAddr) { return lblAddr.trimmed().split("/").last(); }
|
||||
|
||||
RPC* rpc = nullptr;
|
||||
QCompleter* labelCompleter = nullptr;
|
||||
|
||||
|
||||
@@ -92,7 +92,7 @@ void MainWindow::setupSendTab() {
|
||||
|
||||
void MainWindow::updateLabelsAutoComplete() {
|
||||
QList<QString> list;
|
||||
auto labels = AddressBook::readFromStorage();
|
||||
auto labels = AddressBook::getInstance()->getAllAddressLabels();
|
||||
|
||||
std::transform(labels.begin(), labels.end(), std::back_inserter(list), [=] (auto la) -> QString {
|
||||
return la.first % "/" % la.second;
|
||||
@@ -244,7 +244,7 @@ void MainWindow::addAddressSection() {
|
||||
}
|
||||
|
||||
void MainWindow::addressChanged(int itemNumber, const QString& text) {
|
||||
auto addr = addressFromAddressField(text);
|
||||
auto addr = Settings::addressFromAddressLabel(text);
|
||||
setMemoEnabled(itemNumber, addr.startsWith("z"));
|
||||
}
|
||||
|
||||
@@ -267,7 +267,7 @@ void MainWindow::setMemoEnabled(int number, bool enabled) {
|
||||
void MainWindow::memoButtonClicked(int number) {
|
||||
// Memos can only be used with zAddrs. So check that first
|
||||
auto addr = ui->sendToWidgets->findChild<QLineEdit*>(QString("Address") + QString::number(number));
|
||||
if (!addressFromAddressField(addr->text()).startsWith("z")) {
|
||||
if (!Settings::addressFromAddressLabel(addr->text()).startsWith("z")) {
|
||||
QMessageBox msg(QMessageBox::Critical, "Memos can only be used with z-addresses",
|
||||
"The memo field can only be used with a z-address.\n" + addr->text() + "\ndoesn't look like a z-address",
|
||||
QMessageBox::Ok, this);
|
||||
@@ -357,7 +357,7 @@ void MainWindow::maxAmountChecked(int checked) {
|
||||
}
|
||||
sumAllAmounts += Settings::getTotalFee();
|
||||
|
||||
auto addr = ui->inputsCombo->currentText().split("(")[0];
|
||||
auto addr = Settings::addressFromAddressLabel(ui->inputsCombo->currentText().split("(")[0]);
|
||||
|
||||
auto maxamount = rpc->getAllBalances()->value(addr) - sumAllAmounts;
|
||||
maxamount = (maxamount < 0) ? 0 : maxamount;
|
||||
@@ -380,7 +380,7 @@ Tx MainWindow::createTxFromSendPage() {
|
||||
for (int i=0; i < totalItems; i++) {
|
||||
QString addr = ui->sendToWidgets->findChild<QLineEdit*>(QString("Address") % QString::number(i+1))->text().trimmed();
|
||||
// Remove label if it exists
|
||||
addr = addressFromAddressField(addr);
|
||||
addr = Settings::addressFromAddressLabel(addr);
|
||||
|
||||
double amt = ui->sendToWidgets->findChild<QLineEdit*>(QString("Amount") % QString::number(i+1))->text().trimmed().toDouble();
|
||||
QString memo = ui->sendToWidgets->findChild<QLabel*>(QString("MemoTxt") % QString::number(i+1))->text().trimmed();
|
||||
|
||||
@@ -205,3 +205,7 @@ bool Settings::isValidAddress(QString addr) {
|
||||
return zcexp.exactMatch(addr) || texp.exactMatch(addr) ||
|
||||
ztsexp.exactMatch(addr) || zsexp.exactMatch(addr);
|
||||
}
|
||||
|
||||
QString Settings::addressFromAddressLabel(const QString& lblAddr) {
|
||||
return lblAddr.trimmed().split("/").last();
|
||||
}
|
||||
@@ -72,6 +72,8 @@ public:
|
||||
static double getTotalFee();
|
||||
|
||||
static bool isValidAddress(QString addr);
|
||||
static QString addressFromAddressLabel(const QString& lblAddr);
|
||||
static QString addressLabelFromAddress(const QString& addr);
|
||||
|
||||
static const int updateSpeed = 20 * 1000; // 20 sec
|
||||
static const int quickUpdateSpeed = 5 * 1000; // 5 sec
|
||||
|
||||
Reference in New Issue
Block a user