mirror of
https://github.com/feather-wallet/feather.git
synced 2025-04-24 05:08:12 +00:00
AddressBook: cleanup
This commit is contained in:
parent
055b898c85
commit
de37f9a8b7
7 changed files with 86 additions and 130 deletions
|
@ -147,21 +147,18 @@ void ContactsWidget::newContact(QString address, QString name)
|
|||
std::swap(address, name);
|
||||
}
|
||||
|
||||
int num_addresses = m_wallet->addressBook()->count();
|
||||
QString address_entry;
|
||||
QString name_entry;
|
||||
for (int i=0; i<num_addresses; i++) {
|
||||
m_wallet->addressBook()->getRow(i, [&address_entry, &name_entry](const ContactRow &entry){
|
||||
address_entry = entry.getAddress();
|
||||
name_entry = entry.getLabel();
|
||||
});
|
||||
auto& rows = m_wallet->addressBook()->getRows();
|
||||
for (int i = 0; i < rows.size(); i++) {
|
||||
const ContactRow& row = rows[i];
|
||||
|
||||
if (address == address_entry) {
|
||||
if (address == row.address) {
|
||||
Utils::showError(this, "Unable to add contact", "Address already exists in contacts", {}, "add_contact");
|
||||
ui->contacts->setCurrentIndex(m_model->index(i,0)); // Highlight duplicate address
|
||||
QModelIndex sourceIndex = m_model->index(i, 0);
|
||||
ui->contacts->setCurrentIndex(m_proxyModel->mapFromSource(sourceIndex)); // Highlight duplicate address
|
||||
return;
|
||||
}
|
||||
if (name == name_entry) {
|
||||
|
||||
if (name == row.label) {
|
||||
Utils::showError(this, "Unable to add contact", "Label already exists in contacts", {}, "add_contact");
|
||||
this->newContact(address, name);
|
||||
return;
|
||||
|
|
|
@ -5,10 +5,10 @@
|
|||
|
||||
#include <wallet/wallet2.h>
|
||||
|
||||
AddressBook::AddressBook(Wallet *wallet, tools::wallet2 *wallet2, QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_wallet(wallet)
|
||||
, m_wallet2(wallet2)
|
||||
AddressBook::AddressBook(tools::wallet2 *wallet2, QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_wallet2(wallet2)
|
||||
, m_errorCode(Status_Ok)
|
||||
{
|
||||
this->refresh();
|
||||
}
|
||||
|
@ -29,37 +29,30 @@ void AddressBook::refresh()
|
|||
|
||||
clearRows();
|
||||
|
||||
// Fetch from Wallet2 and create vector of AddressBookRow objects
|
||||
std::vector<tools::wallet2::address_book_row> rows = m_wallet2->get_address_book();
|
||||
for (qsizetype i = 0; i < rows.size(); ++i) {
|
||||
tools::wallet2::address_book_row *row = &rows.at(i);
|
||||
|
||||
for (const auto &row : m_wallet2->get_address_book()) {
|
||||
std::string address;
|
||||
if (row->m_has_payment_id)
|
||||
address = cryptonote::get_account_integrated_address_as_str(m_wallet2->nettype(), row->m_address, row->m_payment_id);
|
||||
if (row.m_has_payment_id)
|
||||
address = cryptonote::get_account_integrated_address_as_str(m_wallet2->nettype(), row.m_address, row.m_payment_id);
|
||||
else
|
||||
address = get_account_address_as_str(m_wallet2->nettype(), row->m_is_subaddress, row->m_address);
|
||||
address = get_account_address_as_str(m_wallet2->nettype(), row.m_is_subaddress, row.m_address);
|
||||
|
||||
auto* abr = new ContactRow{this,
|
||||
i,
|
||||
QString::fromStdString(address),
|
||||
QString::fromStdString(row->m_description)};
|
||||
m_rows.push_back(abr);
|
||||
m_rows.emplaceBack(QString::fromStdString(address), QString::fromStdString(row.m_description));
|
||||
}
|
||||
|
||||
|
||||
emit refreshFinished();
|
||||
}
|
||||
|
||||
bool AddressBook::getRow(int index, std::function<void (ContactRow &)> callback) const
|
||||
const QList<ContactRow>& AddressBook::getRows()
|
||||
{
|
||||
if (index < 0 || index >= m_rows.size())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return m_rows;
|
||||
}
|
||||
|
||||
callback(*m_rows.value(index));
|
||||
return true;
|
||||
const ContactRow& AddressBook::getRow(const qsizetype i)
|
||||
{
|
||||
if (i < 0 || i >= m_rows.size()) {
|
||||
throw std::out_of_range("Index out of range");
|
||||
}
|
||||
return m_rows[i];
|
||||
}
|
||||
|
||||
bool AddressBook::addRow(const QString &address, const QString &description)
|
||||
|
@ -73,7 +66,7 @@ bool AddressBook::addRow(const QString &address, const QString &description)
|
|||
return false;
|
||||
}
|
||||
|
||||
bool r = m_wallet2->add_address_book_row(info.address, info.has_payment_id ? &info.payment_id : nullptr, description.toStdString(), info.is_subaddress);
|
||||
bool r = m_wallet2->add_address_book_row(info.address, info.has_payment_id ? &info.payment_id : nullptr, description.toStdString(), info.is_subaddress);
|
||||
if (r)
|
||||
refresh();
|
||||
else
|
||||
|
@ -91,7 +84,7 @@ bool AddressBook::setDescription(int index, const QString &description) {
|
|||
|
||||
tools::wallet2::address_book_row entry = ab[index];
|
||||
entry.m_description = description.toStdString();
|
||||
bool r = m_wallet2->set_address_book_row(index, entry.m_address, entry.m_has_payment_id ? &entry.m_payment_id : nullptr, entry.m_description, entry.m_is_subaddress);
|
||||
bool r = m_wallet2->set_address_book_row(index, entry.m_address, entry.m_has_payment_id ? &entry.m_payment_id : nullptr, entry.m_description, entry.m_is_subaddress);
|
||||
if (r)
|
||||
refresh();
|
||||
else
|
||||
|
@ -114,6 +107,5 @@ qsizetype AddressBook::count() const
|
|||
|
||||
void AddressBook::clearRows()
|
||||
{
|
||||
qDeleteAll(m_rows);
|
||||
m_rows.clear();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,14 +4,10 @@
|
|||
#ifndef ADDRESSBOOK_H
|
||||
#define ADDRESSBOOK_H
|
||||
|
||||
#include <QMap>
|
||||
#include <QObject>
|
||||
#include <QReadWriteLock>
|
||||
#include <QList>
|
||||
#include <QDateTime>
|
||||
|
||||
#include "rows/ContactRow.h"
|
||||
#include "Wallet.h"
|
||||
|
||||
namespace Monero {
|
||||
struct AddressBook;
|
||||
|
@ -21,6 +17,7 @@ namespace tools{
|
|||
class wallet2;
|
||||
}
|
||||
|
||||
class Wallet;
|
||||
class AddressBook : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
@ -34,7 +31,9 @@ public:
|
|||
};
|
||||
Q_ENUM(ErrorCode);
|
||||
|
||||
bool getRow(int index, std::function<void (ContactRow &)> callback) const;
|
||||
const QList<ContactRow>& getRows();
|
||||
const ContactRow& getRow(qsizetype i);
|
||||
|
||||
bool addRow(const QString &address, const QString &description);
|
||||
bool deleteRow(int rowId);
|
||||
bool setDescription(int index, const QString &label);
|
||||
|
@ -45,19 +44,17 @@ public:
|
|||
void refresh();
|
||||
void clearRows();
|
||||
|
||||
|
||||
signals:
|
||||
void refreshStarted() const;
|
||||
void refreshFinished() const;
|
||||
void descriptionChanged() const;
|
||||
|
||||
private:
|
||||
explicit AddressBook(Wallet *wallet, tools::wallet2 *wallet2, QObject *parent);
|
||||
explicit AddressBook(tools::wallet2 *wallet2, QObject *parent);
|
||||
friend class Wallet;
|
||||
|
||||
Wallet *m_wallet;
|
||||
tools::wallet2 *m_wallet2;
|
||||
QList<ContactRow*> m_rows;
|
||||
QList<ContactRow> m_rows;
|
||||
|
||||
QString m_errorString;
|
||||
ErrorCode m_errorCode;
|
||||
|
|
|
@ -38,7 +38,7 @@ Wallet::Wallet(Monero::Wallet *wallet, QObject *parent)
|
|||
, m_wallet2(wallet->getWallet())
|
||||
, m_history(new TransactionHistory(this, wallet->getWallet(), this))
|
||||
, m_historyModel(nullptr)
|
||||
, m_addressBook(new AddressBook(this, wallet->getWallet(), this))
|
||||
, m_addressBook(new AddressBook(wallet->getWallet(), this))
|
||||
, m_addressBookModel(nullptr)
|
||||
, m_daemonBlockChainHeight(0)
|
||||
, m_daemonBlockChainTargetHeight(0)
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
// SPDX-FileCopyrightText: The Monero Project
|
||||
|
||||
#include "ContactRow.h"
|
||||
|
||||
qsizetype ContactRow::getRow() const {
|
||||
return m_row;
|
||||
}
|
||||
|
||||
const QString& ContactRow::getAddress() const {
|
||||
return m_address;
|
||||
}
|
||||
|
||||
const QString& ContactRow::getLabel() const {
|
||||
return m_label;
|
||||
}
|
|
@ -4,28 +4,17 @@
|
|||
#ifndef FEATHER_CONTACTROW_H
|
||||
#define FEATHER_CONTACTROW_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <utility>
|
||||
|
||||
class ContactRow : public QObject
|
||||
struct ContactRow
|
||||
{
|
||||
Q_OBJECT
|
||||
QString address;
|
||||
QString label;
|
||||
|
||||
public:
|
||||
ContactRow(QObject *parent, qsizetype row, const QString& address, const QString &label)
|
||||
: QObject(parent)
|
||||
, m_row(row)
|
||||
, m_address(address)
|
||||
, m_label(label) {}
|
||||
|
||||
qsizetype getRow() const;
|
||||
const QString& getAddress() const;
|
||||
const QString& getLabel() const;
|
||||
|
||||
private:
|
||||
qsizetype m_row;
|
||||
QString m_address;
|
||||
QString m_label;
|
||||
ContactRow(const QString address_, const QString &label_)
|
||||
: address(address_)
|
||||
, label(label_) {}
|
||||
};
|
||||
|
||||
|
||||
#endif //FEATHER_CONTACTROW_H
|
||||
|
|
|
@ -60,50 +60,47 @@ bool AddressBookModel::setData(const QModelIndex &index, const QVariant &value,
|
|||
|
||||
QVariant AddressBookModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
QVariant result;
|
||||
|
||||
bool found = m_addressBook->getRow(index.row(), [this, &result, &role, &index](const ContactRow &row) {
|
||||
if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole) {
|
||||
switch (index.column()) {
|
||||
case Address:
|
||||
{
|
||||
QString address = row.getAddress();
|
||||
if (!m_showFullAddresses && role != Qt::UserRole) {
|
||||
address = Utils::displayAddress(address);
|
||||
}
|
||||
result = address;
|
||||
break;
|
||||
}
|
||||
case Description:
|
||||
result = row.getLabel();
|
||||
break;
|
||||
default:
|
||||
qCritical() << "Invalid column" << index.column();
|
||||
}
|
||||
}
|
||||
else if (role == Qt::FontRole) {
|
||||
switch (index.column()) {
|
||||
case Address:
|
||||
result = Utils::getMonospaceFont();
|
||||
}
|
||||
}
|
||||
else if (role == Qt::DecorationRole) {
|
||||
switch (index.column()) {
|
||||
case Description: {
|
||||
return QVariant(m_contactIcon); // @TODO: does not actually work
|
||||
}
|
||||
default: {
|
||||
return QVariant();
|
||||
}
|
||||
}
|
||||
}
|
||||
return QVariant();
|
||||
});
|
||||
if (!found) {
|
||||
qCritical("%s: internal error: invalid index %d", __FUNCTION__, index.row());
|
||||
const QList<ContactRow>& rows = m_addressBook->getRows();
|
||||
if (index.row() < 0 || index.row() >= rows.size()) {
|
||||
return {};
|
||||
}
|
||||
const ContactRow& row = rows[index.row()];
|
||||
|
||||
return result;
|
||||
if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole) {
|
||||
switch (index.column()) {
|
||||
case Address:
|
||||
{
|
||||
QString address = row.address;
|
||||
if (!m_showFullAddresses && role != Qt::UserRole) {
|
||||
address = Utils::displayAddress(address);
|
||||
}
|
||||
return address;
|
||||
}
|
||||
case Description:
|
||||
return row.label;
|
||||
default:
|
||||
qCritical() << "Invalid column" << index.column();
|
||||
}
|
||||
}
|
||||
else if (role == Qt::FontRole) {
|
||||
switch (index.column()) {
|
||||
case Address:
|
||||
return Utils::getMonospaceFont();
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
}
|
||||
else if (role == Qt::DecorationRole) {
|
||||
switch (index.column()) {
|
||||
case Description: {
|
||||
return QVariant(m_contactIcon); // @TODO: does not actually work
|
||||
}
|
||||
default: {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Qt::ItemFlags AddressBookModel::flags(const QModelIndex &index) const
|
||||
|
@ -120,7 +117,7 @@ Qt::ItemFlags AddressBookModel::flags(const QModelIndex &index) const
|
|||
QVariant AddressBookModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
{
|
||||
if (role != Qt::DisplayRole) {
|
||||
return QVariant();
|
||||
return {};
|
||||
}
|
||||
if (orientation == Qt::Horizontal)
|
||||
{
|
||||
|
@ -130,10 +127,10 @@ QVariant AddressBookModel::headerData(int section, Qt::Orientation orientation,
|
|||
case Description:
|
||||
return QString("Name");
|
||||
default:
|
||||
return QVariant();
|
||||
return {};
|
||||
}
|
||||
}
|
||||
return QVariant();
|
||||
return {};
|
||||
}
|
||||
|
||||
bool AddressBookModel::deleteRow(int row)
|
||||
|
|
Loading…
Reference in a new issue