2011-06-30 18:05:29 +02:00
|
|
|
#ifndef WALLETMODEL_H
|
|
|
|
#define WALLETMODEL_H
|
|
|
|
|
|
|
|
#include <QObject>
|
2011-11-26 07:02:04 +01:00
|
|
|
|
2012-03-24 20:07:01 +01:00
|
|
|
#include "allocators.h" /* for SecureString */
|
2011-06-30 18:05:29 +02:00
|
|
|
|
|
|
|
class OptionsModel;
|
|
|
|
class AddressTableModel;
|
|
|
|
class TransactionTableModel;
|
|
|
|
class CWallet;
|
|
|
|
|
2012-07-05 17:43:28 +02:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
class QTimer;
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
2012-03-18 23:14:03 +01:00
|
|
|
class SendCoinsRecipient
|
2011-07-16 19:01:05 +02:00
|
|
|
{
|
2012-03-18 23:14:03 +01:00
|
|
|
public:
|
2011-07-16 19:01:05 +02:00
|
|
|
QString address;
|
|
|
|
QString label;
|
|
|
|
qint64 amount;
|
|
|
|
};
|
|
|
|
|
2011-11-13 13:19:52 +01:00
|
|
|
/** Interface to Bitcoin wallet from Qt view code. */
|
2011-06-30 18:05:29 +02:00
|
|
|
class WalletModel : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2011-07-29 14:36:35 +02:00
|
|
|
explicit WalletModel(CWallet *wallet, OptionsModel *optionsModel, QObject *parent = 0);
|
2012-05-06 19:40:58 +02:00
|
|
|
~WalletModel();
|
2011-06-30 18:05:29 +02:00
|
|
|
|
2011-08-31 16:08:31 +02:00
|
|
|
enum StatusCode // Returned by sendCoins
|
2011-06-30 18:05:29 +02:00
|
|
|
{
|
|
|
|
OK,
|
|
|
|
InvalidAmount,
|
|
|
|
InvalidAddress,
|
|
|
|
AmountExceedsBalance,
|
|
|
|
AmountWithFeeExceedsBalance,
|
2011-07-16 19:01:05 +02:00
|
|
|
DuplicateAddress,
|
2011-08-31 16:08:31 +02:00
|
|
|
TransactionCreationFailed, // Error returned when wallet is still locked
|
2011-07-16 19:01:05 +02:00
|
|
|
TransactionCommitFailed,
|
2012-04-15 12:42:52 +02:00
|
|
|
Aborted
|
2011-06-30 18:05:29 +02:00
|
|
|
};
|
|
|
|
|
2011-08-23 20:08:42 +02:00
|
|
|
enum EncryptionStatus
|
|
|
|
{
|
|
|
|
Unencrypted, // !wallet->IsCrypted()
|
|
|
|
Locked, // wallet->IsCrypted() && wallet->IsLocked()
|
|
|
|
Unlocked // wallet->IsCrypted() && !wallet->IsLocked()
|
|
|
|
};
|
|
|
|
|
2011-06-30 18:05:29 +02:00
|
|
|
OptionsModel *getOptionsModel();
|
|
|
|
AddressTableModel *getAddressTableModel();
|
|
|
|
TransactionTableModel *getTransactionTableModel();
|
|
|
|
|
|
|
|
qint64 getBalance() const;
|
2011-07-11 20:42:10 +02:00
|
|
|
qint64 getUnconfirmedBalance() const;
|
2012-02-14 12:08:00 +01:00
|
|
|
qint64 getImmatureBalance() const;
|
2011-06-30 18:05:29 +02:00
|
|
|
int getNumTransactions() const;
|
2011-08-23 20:08:42 +02:00
|
|
|
EncryptionStatus getEncryptionStatus() const;
|
|
|
|
|
2011-07-16 19:01:05 +02:00
|
|
|
// Check address for validity
|
|
|
|
bool validateAddress(const QString &address);
|
|
|
|
|
2011-08-08 17:38:17 +02:00
|
|
|
// Return status record for SendCoins, contains error id + information
|
2011-07-16 19:01:05 +02:00
|
|
|
struct SendCoinsReturn
|
|
|
|
{
|
|
|
|
SendCoinsReturn(StatusCode status,
|
|
|
|
qint64 fee=0,
|
|
|
|
QString hex=QString()):
|
|
|
|
status(status), fee(fee), hex(hex) {}
|
|
|
|
StatusCode status;
|
2011-08-08 17:38:17 +02:00
|
|
|
qint64 fee; // is used in case status is "AmountWithFeeExceedsBalance"
|
|
|
|
QString hex; // is filled with the transaction hash if status is "OK"
|
2011-07-16 19:01:05 +02:00
|
|
|
};
|
|
|
|
|
2011-08-08 17:38:17 +02:00
|
|
|
// Send coins to a list of recipients
|
2011-07-16 19:01:05 +02:00
|
|
|
SendCoinsReturn sendCoins(const QList<SendCoinsRecipient> &recipients);
|
2011-08-24 22:07:26 +02:00
|
|
|
|
|
|
|
// Wallet encryption
|
2011-11-26 07:02:04 +01:00
|
|
|
bool setWalletEncrypted(bool encrypted, const SecureString &passphrase);
|
2011-08-24 22:07:26 +02:00
|
|
|
// Passphrase only needed when unlocking
|
2011-11-26 07:02:04 +01:00
|
|
|
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString());
|
|
|
|
bool changePassphrase(const SecureString &oldPass, const SecureString &newPass);
|
2012-02-14 13:14:43 +01:00
|
|
|
// Wallet backup
|
|
|
|
bool backupWallet(const QString &filename);
|
2011-08-24 22:07:26 +02:00
|
|
|
|
|
|
|
// RAI object for unlocking wallet, returned by requestUnlock()
|
|
|
|
class UnlockContext
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
UnlockContext(WalletModel *wallet, bool valid, bool relock);
|
|
|
|
~UnlockContext();
|
|
|
|
|
|
|
|
bool isValid() const { return valid; }
|
|
|
|
|
2011-08-31 16:08:31 +02:00
|
|
|
// Copy operator and constructor transfer the context
|
|
|
|
UnlockContext(const UnlockContext& obj) { CopyFrom(obj); }
|
|
|
|
UnlockContext& operator=(const UnlockContext& rhs) { CopyFrom(rhs); return *this; }
|
2011-08-24 22:07:26 +02:00
|
|
|
private:
|
|
|
|
WalletModel *wallet;
|
|
|
|
bool valid;
|
|
|
|
mutable bool relock; // mutable, as it can be set to false by copying
|
|
|
|
|
|
|
|
void CopyFrom(const UnlockContext& rhs);
|
|
|
|
};
|
|
|
|
|
|
|
|
UnlockContext requestUnlock();
|
|
|
|
|
2011-06-30 18:05:29 +02:00
|
|
|
private:
|
|
|
|
CWallet *wallet;
|
|
|
|
|
|
|
|
// Wallet has an options model for wallet-specific options
|
|
|
|
// (transaction fee, for example)
|
|
|
|
OptionsModel *optionsModel;
|
|
|
|
|
|
|
|
AddressTableModel *addressTableModel;
|
|
|
|
TransactionTableModel *transactionTableModel;
|
|
|
|
|
2011-08-31 16:08:31 +02:00
|
|
|
// Cache some values to be able to detect changes
|
2011-07-17 14:06:43 +02:00
|
|
|
qint64 cachedBalance;
|
|
|
|
qint64 cachedUnconfirmedBalance;
|
2012-02-14 12:08:00 +01:00
|
|
|
qint64 cachedImmatureBalance;
|
2011-07-17 14:06:43 +02:00
|
|
|
qint64 cachedNumTransactions;
|
2011-08-23 20:08:42 +02:00
|
|
|
EncryptionStatus cachedEncryptionStatus;
|
2012-07-05 17:43:28 +02:00
|
|
|
int cachedNumBlocks;
|
|
|
|
|
|
|
|
QTimer *pollTimer;
|
2011-07-17 14:06:43 +02:00
|
|
|
|
2012-05-06 19:40:58 +02:00
|
|
|
void subscribeToCoreSignals();
|
|
|
|
void unsubscribeFromCoreSignals();
|
2012-07-05 17:43:28 +02:00
|
|
|
void checkBalanceChanged();
|
|
|
|
|
2011-06-30 18:05:29 +02:00
|
|
|
signals:
|
2011-08-31 16:08:31 +02:00
|
|
|
// Signal that balance in wallet changed
|
2012-02-14 12:08:00 +01:00
|
|
|
void balanceChanged(qint64 balance, qint64 unconfirmedBalance, qint64 immatureBalance);
|
2011-08-31 16:08:31 +02:00
|
|
|
|
|
|
|
// Number of transactions in wallet changed
|
2011-06-30 18:05:29 +02:00
|
|
|
void numTransactionsChanged(int count);
|
2011-08-31 16:08:31 +02:00
|
|
|
|
|
|
|
// Encryption status of wallet changed
|
2011-08-23 20:08:42 +02:00
|
|
|
void encryptionStatusChanged(int status);
|
2011-08-31 16:08:31 +02:00
|
|
|
|
|
|
|
// Signal emitted when wallet needs to be unlocked
|
|
|
|
// It is valid behaviour for listeners to keep the wallet locked after this signal;
|
|
|
|
// this means that the unlocking failed or was cancelled.
|
2011-08-24 22:07:26 +02:00
|
|
|
void requireUnlock();
|
2011-06-30 18:05:29 +02:00
|
|
|
|
|
|
|
// Asynchronous error notification
|
2012-03-25 20:47:33 +02:00
|
|
|
void error(const QString &title, const QString &message, bool modal);
|
2011-06-30 18:05:29 +02:00
|
|
|
|
|
|
|
public slots:
|
2012-05-05 16:07:14 +02:00
|
|
|
/* Wallet status might have changed */
|
|
|
|
void updateStatus();
|
|
|
|
/* New transaction, or transaction changed status */
|
|
|
|
void updateTransaction(const QString &hash, int status);
|
|
|
|
/* New, updated or removed address book entry */
|
2012-05-06 22:41:35 +02:00
|
|
|
void updateAddressBook(const QString &address, const QString &label, bool isMine, int status);
|
2012-07-05 17:43:28 +02:00
|
|
|
/* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
|
|
|
|
void pollBalanceChanged();
|
2011-06-30 18:05:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // WALLETMODEL_H
|