Qt wrappers for libwallet API classes

This commit is contained in:
Ilya Kitaev 2016-06-08 13:53:24 +03:00
parent da1b74a707
commit 5c10be3251
11 changed files with 414 additions and 12 deletions

View file

@ -15,7 +15,10 @@ HEADERS += \
clipboardAdapter.h \
oscursor.h \
src/libwalletqt/WalletManager.h \
src/libwalletqt/Wallet.h
src/libwalletqt/Wallet.h \
src/libwalletqt/PendingTransaction.h \
src/libwalletqt/TransactionHistory.h \
src/libwalletqt/TransactionInfo.h
SOURCES += main.cpp \
@ -23,7 +26,10 @@ SOURCES += main.cpp \
clipboardAdapter.cpp \
oscursor.cpp \
src/libwalletqt/WalletManager.cpp \
src/libwalletqt/Wallet.cpp
src/libwalletqt/Wallet.cpp \
src/libwalletqt/PendingTransaction.cpp \
src/libwalletqt/TransactionHistory.cpp \
src/libwalletqt/TransactionInfo.cpp
lupdate_only {
SOURCES = *.qml \

View file

@ -0,0 +1,37 @@
#include "PendingTransaction.h"
PendingTransaction::Status PendingTransaction::status() const
{
return static_cast<Status>(m_pimpl->status());
}
QString PendingTransaction::errorString() const
{
return QString::fromStdString(m_pimpl->errorString());
}
bool PendingTransaction::commit()
{
return m_pimpl->commit();
}
quint64 PendingTransaction::amount() const
{
return m_pimpl->amount();
}
quint64 PendingTransaction::dust() const
{
return m_pimpl->dust();
}
quint64 PendingTransaction::fee() const
{
return m_pimpl->fee();
}
PendingTransaction::PendingTransaction(Bitmonero::PendingTransaction *pt, QObject *parent)
: QObject(parent), m_pimpl(pt)
{
}

View file

@ -0,0 +1,41 @@
#ifndef PENDINGTRANSACTION_H
#define PENDINGTRANSACTION_H
#include <QObject>
#include <wallet/wallet2_api.h>
//namespace Bitmonero {
//class PendingTransaction;
//}
class PendingTransaction : public QObject
{
Q_OBJECT
Q_PROPERTY(Status status READ status)
Q_PROPERTY(QString errorString READ errorString)
Q_PROPERTY(quint64 amount READ amount)
Q_PROPERTY(quint64 dust READ dust)
Q_PROPERTY(quint64 fee READ fee)
public:
enum Status {
Status_Ok = Bitmonero::PendingTransaction::Status_Ok,
Status_Error = Bitmonero::PendingTransaction::Status_Error
};
Status status() const;
QString errorString() const;
Q_INVOKABLE bool commit();
quint64 amount() const;
quint64 dust() const;
quint64 fee() const;
private:
explicit PendingTransaction(Bitmonero::PendingTransaction * pt, QObject *parent = 0);
private:
friend class Wallet;
Bitmonero::PendingTransaction * m_pimpl;
};
#endif // PENDINGTRANSACTION_H

View file

@ -0,0 +1,50 @@
#include "TransactionHistory.h"
#include "TransactionInfo.h"
#include <wallet/wallet2_api.h>
int TransactionHistory::count() const
{
return m_pimpl->count();
}
TransactionInfo *TransactionHistory::transaction(int index)
{
// box up Bitmonero::TransactionInfo
Bitmonero::TransactionInfo * impl = m_pimpl->transaction(index);
TransactionInfo * result = new TransactionInfo(impl, this);
return result;
}
TransactionInfo *TransactionHistory::transaction(const QString &id)
{
// box up Bitmonero::TransactionInfo
Bitmonero::TransactionInfo * impl = m_pimpl->transaction(id.toStdString());
TransactionInfo * result = new TransactionInfo(impl, this);
return result;
}
QList<TransactionInfo *> TransactionHistory::getAll() const
{
qDeleteAll(m_tinfo);
m_tinfo.clear();
TransactionHistory * parent = const_cast<TransactionHistory*>(this);
for (const auto i : m_pimpl->getAll()) {
TransactionInfo * ti = new TransactionInfo(i, parent);
m_tinfo.append(ti);
}
return m_tinfo;
}
void TransactionHistory::refresh()
{
// XXX this invalidates previously saved history that might be used by clients
m_pimpl->refresh();
emit invalidated();
}
TransactionHistory::TransactionHistory(Bitmonero::TransactionHistory *pimpl, QObject *parent)
: QObject(parent), m_pimpl(pimpl)
{
}

View file

@ -0,0 +1,42 @@
#ifndef TRANSACTIONHISTORY_H
#define TRANSACTIONHISTORY_H
#include <QObject>
#include <QList>
namespace Bitmonero {
class TransactionHistory;
}
class TransactionInfo;
class TransactionHistory : public QObject
{
Q_OBJECT
Q_PROPERTY(int count READ count)
public:
int count() const;
Q_INVOKABLE TransactionInfo *transaction(int index);
Q_INVOKABLE TransactionInfo * transaction(const QString &id);
Q_INVOKABLE QList<TransactionInfo*> getAll() const;
Q_INVOKABLE void refresh();
signals:
void invalidated();
public slots:
private:
explicit TransactionHistory(Bitmonero::TransactionHistory * pimpl, QObject *parent = 0);
private:
friend class Wallet;
Bitmonero::TransactionHistory * m_pimpl;
mutable QList<TransactionInfo*> m_tinfo;
};
#endif // TRANSACTIONHISTORY_H

View file

@ -0,0 +1,55 @@
#include "TransactionInfo.h"
#include <QDateTime>
TransactionInfo::Direction TransactionInfo::direction() const
{
return static_cast<Direction>(m_pimpl->direction());
}
bool TransactionInfo::isPending() const
{
return m_pimpl->isPending();
}
bool TransactionInfo::isFailed() const
{
return m_pimpl->isFailed();
}
quint64 TransactionInfo::amount() const
{
return m_pimpl->amount();
}
quint64 TransactionInfo::fee() const
{
return m_pimpl->fee();
}
quint64 TransactionInfo::blockHeight() const
{
return m_pimpl->blockHeight();
}
QString TransactionInfo::hash() const
{
return QString::fromStdString(m_pimpl->hash());
}
QString TransactionInfo::timestamp()
{
QString result = QDateTime::fromTime_t(m_pimpl->timestamp()).toString(Qt::ISODate);
return result;
}
QString TransactionInfo::paymentId()
{
return QString::fromStdString(m_pimpl->paymentId());
}
TransactionInfo::TransactionInfo(Bitmonero::TransactionInfo *pimpl, QObject *parent)
: QObject(parent), m_pimpl(pimpl)
{
}

View file

@ -0,0 +1,54 @@
#ifndef TRANSACTIONINFO_H
#define TRANSACTIONINFO_H
#include <QObject>
#include <wallet/wallet2_api.h>
class TransactionInfo : public QObject
{
Q_OBJECT
Q_PROPERTY(Direction direction READ direction)
Q_PROPERTY(bool isPending READ isPending)
Q_PROPERTY(bool isFailed READ isFailed)
Q_PROPERTY(quint64 amount READ amount)
Q_PROPERTY(quint64 fee READ fee)
Q_PROPERTY(quint64 blockHeight READ blockHeight)
Q_PROPERTY(QString hash READ hash)
Q_PROPERTY(QString timestamp READ timestamp)
Q_PROPERTY(QString paymentId READ paymentId)
public:
enum Direction {
Direction_In = Bitmonero::TransactionInfo::Direction_In,
Direction_Out = Bitmonero::TransactionInfo::Direction_Out
};
// TODO: implement as separate class;
// struct Transfer {
// Transfer(uint64_t _amount, const std::string &address);
// const uint64_t amount;
// const std::string address;
// };
Direction direction() const;
bool isPending() const;
bool isFailed() const;
quint64 amount() const;
quint64 fee() const;
quint64 blockHeight() const;
//! transaction_id
QString hash() const;
QString timestamp();
QString paymentId();
// TODO: implement it
//! only applicable for output transactions
// virtual const std::vector<Transfer> & transfers() const = 0;
private:
explicit TransactionInfo(Bitmonero::TransactionInfo * pimpl, QObject *parent = 0);
private:
friend class TransactionHistory;
Bitmonero::TransactionInfo * m_pimpl;
};
#endif // TRANSACTIONINFO_H

View file

@ -1,4 +1,6 @@
#include "Wallet.h"
#include "PendingTransaction.h"
#include "TransactionHistory.h"
#include "wallet/wallet2_api.h"
#include <QFile>
@ -22,9 +24,14 @@ QString Wallet::getSeedLanguage() const
return QString::fromStdString(m_walletImpl->getSeedLanguage());
}
int Wallet::status() const
void Wallet::setSeedLanguage(const QString &lang)
{
return m_walletImpl->status();
m_walletImpl->setSeedLanguage(lang.toStdString());
}
Wallet::Status Wallet::status() const
{
return static_cast<Status>(m_walletImpl->status());
}
QString Wallet::errorString() const
@ -47,10 +54,63 @@ bool Wallet::store(const QString &path)
return m_walletImpl->store(path.toStdString());
}
bool Wallet::init(const QString &daemonAddress, quint64 upperTransactionLimit)
{
return m_walletImpl->init(daemonAddress.toStdString(), upperTransactionLimit);
}
bool Wallet::connectToDaemon()
{
return m_walletImpl->connectToDaemon();
}
void Wallet::setTrustedDaemon(bool arg)
{
m_walletImpl->setTrustedDaemon(arg);
}
quint64 Wallet::balance() const
{
return m_walletImpl->balance();
}
quint64 Wallet::unlockedBalance() const
{
return m_walletImpl->unlockedBalance();
}
bool Wallet::refresh()
{
return m_walletImpl->refresh();
}
PendingTransaction *Wallet::createTransaction(const QString &dst_addr, quint64 amount)
{
Bitmonero::PendingTransaction * ptImpl = m_walletImpl->createTransaction(
dst_addr.toStdString(), amount);
PendingTransaction * result = new PendingTransaction(ptImpl, this);
return result;
}
void Wallet::disposeTransaction(PendingTransaction *t)
{
m_walletImpl->disposeTransaction(t->m_pimpl);
delete t;
}
TransactionHistory *Wallet::history()
{
if (!m_history) {
Bitmonero::TransactionHistory * impl = m_walletImpl->history();
m_history = new TransactionHistory(impl, this);
}
return m_history;
}
Wallet::Wallet(Bitmonero::Wallet *w, QObject *parent)
: QObject(parent), m_walletImpl(w)
: QObject(parent), m_walletImpl(w), m_history(nullptr)
{
}

View file

@ -3,41 +3,88 @@
#include <QObject>
#include "wallet/wallet2_api.h" // we need to access Status enum here;
namespace Bitmonero {
class Wallet; // forward declaration
}
class PendingTransaction;
class TransactionHistory;
class Wallet : public QObject
{
Q_OBJECT
Q_PROPERTY(QString seed READ getSeed)
Q_PROPERTY(QString seedLanguage READ getSeedLanguage)
Q_PROPERTY(Status status READ status)
Q_PROPERTY(QString errorString READ errorString)
Q_PROPERTY(QString address READ address)
Q_PROPERTY(quint64 balance READ balance)
Q_PROPERTY(quint64 unlockedBalance READ unlockedBalance)
Q_PROPERTY(TransactionHistory * history READ history)
public:
enum Status {
Status_Ok = 0,
Status_Error = 1
Status_Ok = Bitmonero::Wallet::Status_Ok,
Status_Error = Bitmonero::Wallet::Status_Error
};
Q_ENUM(Status)
//! returns mnemonic seed
Q_INVOKABLE QString getSeed() const;
QString getSeed() const;
//! returns seed language
Q_INVOKABLE QString getSeedLanguage() const;
QString getSeedLanguage() const;
//! set seed language
Q_INVOKABLE void setSeedLanguage(const QString &lang);
//! returns last operation's status
Q_INVOKABLE int status() const;
Status status() const;
//! returns last operation's error message
Q_INVOKABLE QString errorString() const;
QString errorString() const;
//! changes the password using existing parameters (path, seed, seed lang)
Q_INVOKABLE bool setPassword(const QString &password);
//! returns wallet's public address
Q_INVOKABLE QString address() const;
QString address() const;
//! saves wallet to the file by given path
Q_INVOKABLE bool store(const QString &path);
//! initializes wallet
Q_INVOKABLE bool init(const QString &daemonAddress, quint64 upperTransactionLimit);
//! connects to daemon
Q_INVOKABLE bool connectToDaemon();
//! indicates id daemon is trusted
Q_INVOKABLE void setTrustedDaemon(bool arg);
//! returns balance
quint64 balance() const;
//! returns unlocked balance
quint64 unlockedBalance() const;
//! refreshes the wallet
Q_INVOKABLE bool refresh();
//! creates transaction
Q_INVOKABLE PendingTransaction * createTransaction(const QString &dst_addr,
quint64 amount);
//! deletes transaction and frees memory
Q_INVOKABLE void disposeTransaction(PendingTransaction * t);
//! returns transaction history
TransactionHistory * history();
// TODO: setListenter() when it implemented in API
private:
Wallet(Bitmonero::Wallet *w, QObject * parent = 0);
@ -47,6 +94,8 @@ private:
friend class WalletManager;
//! libwallet's
Bitmonero::Wallet * m_walletImpl;
// history lifetime managed by wallet;
TransactionHistory * m_history;
};
#endif // WALLET_H

View file

@ -87,6 +87,11 @@ QString WalletManager::walletLanguage(const QString &locale)
return "English";
}
QString WalletManager::displayAmount(quint64 amount)
{
return QString::fromStdString(Bitmonero::Wallet::displayAmount(amount));
}
WalletManager::WalletManager(QObject *parent) : QObject(parent)
{

View file

@ -42,6 +42,9 @@ public:
//! returns libwallet language name for given locale
Q_INVOKABLE QString walletLanguage(const QString &locale);
//! since we can't call static method from QML, move it to this class
Q_INVOKABLE QString displayAmount(quint64 amount);
signals:
public slots: