parent
160a042165
commit
a344f17cd1
@ -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();
|
||||||
|
}
|
@ -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
|
@ -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;
|
||||||
|
}
|
@ -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