forked from Public/monero-gui
AddressBook: basic functions
This commit is contained in:
70
src/libwalletqt/AddressBook.cpp
Normal file
70
src/libwalletqt/AddressBook.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
#include "AddressBook.h"
|
||||
#include <QDebug>
|
||||
|
||||
AddressBook::AddressBook(Bitmonero::AddressBook *abImpl,QObject *parent)
|
||||
: QObject(parent), m_addressBookImpl(abImpl)
|
||||
{
|
||||
qDebug(__FUNCTION__);
|
||||
getAll();
|
||||
}
|
||||
|
||||
QString AddressBook::errorString() const
|
||||
{
|
||||
return QString::fromStdString(m_addressBookImpl->errorString());
|
||||
}
|
||||
|
||||
int AddressBook::errorCode() const
|
||||
{
|
||||
return m_addressBookImpl->errorCode();
|
||||
}
|
||||
|
||||
QList<Bitmonero::AddressBookRow*> AddressBook::getAll(bool update) const
|
||||
{
|
||||
qDebug(__FUNCTION__);
|
||||
|
||||
emit refreshStarted();
|
||||
|
||||
if(update)
|
||||
m_rows.clear();
|
||||
|
||||
if (m_rows.empty()){
|
||||
for (auto &abr: m_addressBookImpl->getAll()) {
|
||||
m_rows.append(abr);
|
||||
}
|
||||
}
|
||||
|
||||
emit refreshFinished();
|
||||
return m_rows;
|
||||
|
||||
}
|
||||
|
||||
Bitmonero::AddressBookRow * AddressBook::getRow(int index) const
|
||||
{
|
||||
return m_rows.at(index);
|
||||
}
|
||||
|
||||
bool AddressBook::addRow(const QString &address, const QString &payment_id, const QString &description) const
|
||||
{
|
||||
// virtual bool addRow(const std::string &dst_addr , const std::string &payment_id, const std::string &description) = 0;
|
||||
bool r = m_addressBookImpl->addRow(address.toStdString(), payment_id.toStdString(), description.toStdString());
|
||||
|
||||
if(r)
|
||||
getAll(true);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
bool AddressBook::deleteRow(int rowId) const
|
||||
{
|
||||
bool r = m_addressBookImpl->deleteRow(rowId);
|
||||
|
||||
// Fetch new data from wallet2.
|
||||
getAll(true);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
quint64 AddressBook::count() const
|
||||
{
|
||||
return m_rows.size();
|
||||
}
|
||||
50
src/libwalletqt/AddressBook.h
Normal file
50
src/libwalletqt/AddressBook.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#ifndef ADDRESSBOOK_H
|
||||
#define ADDRESSBOOK_H
|
||||
|
||||
#include <wallet/wallet2_api.h>
|
||||
#include <QObject>
|
||||
#include <QList>
|
||||
#include <QDateTime>
|
||||
|
||||
namespace Bitmonero {
|
||||
class AddressBook;
|
||||
}
|
||||
class AddressBookRow;
|
||||
|
||||
class AddressBook : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
Q_INVOKABLE QList<Bitmonero::AddressBookRow*> getAll(bool update = false) const;
|
||||
Q_INVOKABLE Bitmonero::AddressBookRow * getRow(int index) const;
|
||||
Q_INVOKABLE bool addRow(const QString &address, const QString &payment_id, const QString &description) const;
|
||||
Q_INVOKABLE bool deleteRow(int rowId) const;
|
||||
quint64 count() const;
|
||||
Q_INVOKABLE QString errorString() const;
|
||||
Q_INVOKABLE int errorCode() const;
|
||||
|
||||
enum ErrorCode {
|
||||
Status_Ok,
|
||||
General_Error,
|
||||
Invalid_Address,
|
||||
Invalid_Payment_Id
|
||||
};
|
||||
|
||||
Q_ENUM(ErrorCode);
|
||||
|
||||
|
||||
signals:
|
||||
void refreshStarted() const;
|
||||
void refreshFinished() const;
|
||||
|
||||
|
||||
public slots:
|
||||
|
||||
private:
|
||||
explicit AddressBook(Bitmonero::AddressBook * abImpl, QObject *parent);
|
||||
friend class Wallet;
|
||||
Bitmonero::AddressBook * m_addressBookImpl;
|
||||
mutable QList<Bitmonero::AddressBookRow*> m_rows;
|
||||
};
|
||||
|
||||
#endif // ADDRESSBOOK_H
|
||||
@@ -23,7 +23,8 @@ class PendingTransaction : public QObject
|
||||
public:
|
||||
enum Status {
|
||||
Status_Ok = Monero::PendingTransaction::Status_Ok,
|
||||
Status_Error = Monero::PendingTransaction::Status_Error
|
||||
Status_Error = Monero::PendingTransaction::Status_Error,
|
||||
Status_Critical = Monero::PendingTransaction::Status_Critical
|
||||
};
|
||||
Q_ENUM(Status)
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
#include "Wallet.h"
|
||||
#include "PendingTransaction.h"
|
||||
#include "TransactionHistory.h"
|
||||
#include "AddressBook.h"
|
||||
#include "model/TransactionHistoryModel.h"
|
||||
#include "model/TransactionHistorySortFilterModel.h"
|
||||
#include "model/AddressBookModel.h"
|
||||
#include "wallet/wallet2_api.h"
|
||||
|
||||
#include <QFile>
|
||||
@@ -11,6 +13,8 @@
|
||||
#include <QUrl>
|
||||
#include <QTimer>
|
||||
#include <QtConcurrent/QtConcurrent>
|
||||
#include <QList>
|
||||
#include <QVector>
|
||||
|
||||
namespace {
|
||||
static const int DAEMON_BLOCKCHAIN_HEIGHT_CACHE_TTL_SECONDS = 10;
|
||||
@@ -48,7 +52,6 @@ public:
|
||||
|
||||
virtual void updated()
|
||||
{
|
||||
qDebug() << __FUNCTION__;
|
||||
emit m_wallet->updated();
|
||||
}
|
||||
|
||||
@@ -324,6 +327,22 @@ TransactionHistorySortFilterModel *Wallet::historyModel() const
|
||||
return m_historySortFilterModel;
|
||||
}
|
||||
|
||||
AddressBook *Wallet::addressBook() const
|
||||
{
|
||||
return m_addressBook;
|
||||
}
|
||||
|
||||
AddressBookModel *Wallet::addressBookModel() const
|
||||
{
|
||||
|
||||
if (!m_addressBookModel) {
|
||||
Wallet * w = const_cast<Wallet*>(this);
|
||||
m_addressBookModel = new AddressBookModel(w,m_addressBook);
|
||||
}
|
||||
|
||||
return m_addressBookModel;
|
||||
}
|
||||
|
||||
|
||||
QString Wallet::generatePaymentId() const
|
||||
{
|
||||
@@ -437,6 +456,8 @@ Wallet::Wallet(Monero::Wallet *w, QObject *parent)
|
||||
, m_walletImpl(w)
|
||||
, m_history(nullptr)
|
||||
, m_historyModel(nullptr)
|
||||
, m_addressBook(nullptr)
|
||||
, m_addressBookModel(nullptr)
|
||||
, m_daemonBlockChainHeight(0)
|
||||
, m_daemonBlockChainHeightTtl(DAEMON_BLOCKCHAIN_HEIGHT_CACHE_TTL_SECONDS)
|
||||
, m_daemonBlockChainTargetHeight(0)
|
||||
@@ -444,6 +465,7 @@ Wallet::Wallet(Monero::Wallet *w, QObject *parent)
|
||||
, m_connectionStatusTtl(WALLET_CONNECTION_STATUS_CACHE_TTL_SECONDS)
|
||||
{
|
||||
m_history = new TransactionHistory(m_walletImpl->history(), this);
|
||||
m_addressBook = new AddressBook(m_walletImpl->addressBook(), this);
|
||||
m_walletImpl->setListener(new WalletListenerImpl(this));
|
||||
m_connectionStatus = Wallet::ConnectionStatus_Disconnected;
|
||||
// start cache timers
|
||||
@@ -456,6 +478,9 @@ Wallet::Wallet(Monero::Wallet *w, QObject *parent)
|
||||
Wallet::~Wallet()
|
||||
{
|
||||
qDebug("~Wallet: Closing wallet");
|
||||
|
||||
delete m_history;
|
||||
|
||||
Monero::WalletManagerFactory::getWalletManager()->closeWallet(m_walletImpl);
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@ namespace Monero {
|
||||
class TransactionHistory;
|
||||
class TransactionHistoryModel;
|
||||
class TransactionHistorySortFilterModel;
|
||||
class AddressBook;
|
||||
class AddressBookModel;
|
||||
|
||||
class Wallet : public QObject
|
||||
{
|
||||
@@ -32,13 +34,17 @@ class Wallet : public QObject
|
||||
Q_PROPERTY(QString paymentId READ paymentId WRITE setPaymentId)
|
||||
Q_PROPERTY(TransactionHistorySortFilterModel * historyModel READ historyModel NOTIFY historyModelChanged)
|
||||
Q_PROPERTY(QString path READ path)
|
||||
Q_PROPERTY(AddressBookModel * addressBookModel READ addressBookModel)
|
||||
Q_PROPERTY(AddressBook * addressBook READ addressBook)
|
||||
|
||||
|
||||
public:
|
||||
|
||||
|
||||
enum Status {
|
||||
Status_Ok = Monero::Wallet::Status_Ok,
|
||||
Status_Error = Monero::Wallet::Status_Error
|
||||
Status_Error = Monero::Wallet::Status_Error,
|
||||
Status_Critical = Monero::Wallet::Status_Critical
|
||||
};
|
||||
|
||||
Q_ENUM(Status)
|
||||
@@ -159,6 +165,12 @@ public:
|
||||
//! returns transaction history model
|
||||
TransactionHistorySortFilterModel *historyModel() const;
|
||||
|
||||
//! returns Address book
|
||||
AddressBook *addressBook() const;
|
||||
|
||||
//! returns adress book model
|
||||
AddressBookModel *addressBookModel() const;
|
||||
|
||||
//! generate payment id
|
||||
Q_INVOKABLE QString generatePaymentId() const;
|
||||
|
||||
@@ -227,6 +239,8 @@ private:
|
||||
int m_connectionStatusTtl;
|
||||
mutable QTime m_connectionStatusTime;
|
||||
mutable bool m_initialized;
|
||||
AddressBook * m_addressBook;
|
||||
mutable AddressBookModel * m_addressBookModel;
|
||||
};
|
||||
|
||||
|
||||
|
||||
75
src/model/AddressBookModel.cpp
Normal file
75
src/model/AddressBookModel.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
#include "AddressBookModel.h"
|
||||
#include "AddressBook.h"
|
||||
#include <QDebug>
|
||||
#include <QHash>
|
||||
#include <wallet/wallet2_api.h>
|
||||
|
||||
AddressBookModel::AddressBookModel(QObject *parent, AddressBook *addressBook)
|
||||
: QAbstractListModel(parent) , m_addressBook(addressBook)
|
||||
{
|
||||
qDebug(__FUNCTION__);
|
||||
connect(m_addressBook,SIGNAL(refreshStarted()),this,SLOT(startReset()));
|
||||
connect(m_addressBook,SIGNAL(refreshFinished()),this,SLOT(endReset()));
|
||||
|
||||
}
|
||||
|
||||
void AddressBookModel::startReset(){
|
||||
qDebug(__FUNCTION__);
|
||||
beginResetModel();
|
||||
}
|
||||
void AddressBookModel::endReset(){
|
||||
qDebug(__FUNCTION__);
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
int AddressBookModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
return m_addressBook->count();
|
||||
}
|
||||
|
||||
QVariant AddressBookModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid())
|
||||
return QVariant();
|
||||
|
||||
if (index.row() < 0 || (unsigned)index.row() >= m_addressBook->count()) {
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
Bitmonero::AddressBookRow * ar = m_addressBook->getRow(index.row());
|
||||
|
||||
QVariant result = "";
|
||||
switch (role) {
|
||||
case AddressBookAddressRole:
|
||||
result = QString::fromStdString(ar->getAddress());
|
||||
break;
|
||||
case AddressBookDescriptionRole:
|
||||
result = QString::fromStdString(ar->getDescription());
|
||||
break;
|
||||
case AddressBookPaymentIdRole:
|
||||
result = QString::fromStdString(ar->getPaymentId());
|
||||
break;
|
||||
case AddressBookRowIdRole:
|
||||
result = ar->getRowId();
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool AddressBookModel::deleteRow(int row)
|
||||
{
|
||||
m_addressBook->deleteRow(row);
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> AddressBookModel::roleNames() const
|
||||
{
|
||||
QHash<int, QByteArray> roleNames = QAbstractListModel::roleNames();
|
||||
roleNames.insert(AddressBookAddressRole, "address");
|
||||
roleNames.insert(AddressBookPaymentIdRole, "paymentId");
|
||||
roleNames.insert(AddressBookDescriptionRole, "description");
|
||||
roleNames.insert(AddressBookRowIdRole, "rowId");
|
||||
|
||||
|
||||
return roleNames;
|
||||
}
|
||||
38
src/model/AddressBookModel.h
Normal file
38
src/model/AddressBookModel.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef ADDRESSBOOKMODEL_H
|
||||
#define ADDRESSBOOKMODEL_H
|
||||
|
||||
#include <QAbstractListModel>
|
||||
//#include "wallet/wallet2_api.h" // we need to have an access to the Bitmonero::Wallet::AddressBook
|
||||
|
||||
class AddressBook;
|
||||
|
||||
class AddressBookModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum AddressBookRowRole {
|
||||
AddressBookRole = Qt::UserRole + 1, // for the AddressBookRow object;
|
||||
AddressBookAddressRole,
|
||||
AddressBookDescriptionRole,
|
||||
AddressBookPaymentIdRole,
|
||||
AddressBookRowIdRole,
|
||||
};
|
||||
Q_ENUM(AddressBookRowRole)
|
||||
|
||||
AddressBookModel(QObject *parent, AddressBook * addressBook);
|
||||
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
Q_INVOKABLE bool deleteRow(int row);
|
||||
virtual QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
public slots:
|
||||
void startReset();
|
||||
void endReset();
|
||||
|
||||
private:
|
||||
AddressBook * m_addressBook;
|
||||
};
|
||||
|
||||
#endif // ADDRESSBOOKMODEL_H
|
||||
Reference in New Issue
Block a user