You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

walletmodel.cpp 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "walletmodel.h"
  2. #include "guiconstants.h"
  3. #include "optionsmodel.h"
  4. #include "addresstablemodel.h"
  5. #include "transactiontablemodel.h"
  6. #include "headers.h"
  7. #include <QTimer>
  8. WalletModel::WalletModel(CWallet *wallet, QObject *parent) :
  9. QObject(parent), wallet(wallet), optionsModel(0), addressTableModel(0),
  10. transactionTableModel(0)
  11. {
  12. // Until signal notifications is built into the bitcoin core,
  13. // simply update everything after polling using a timer.
  14. QTimer *timer = new QTimer(this);
  15. connect(timer, SIGNAL(timeout()), this, SLOT(update()));
  16. timer->start(MODEL_UPDATE_DELAY);
  17. optionsModel = new OptionsModel(wallet, this);
  18. addressTableModel = new AddressTableModel(wallet, this);
  19. transactionTableModel = new TransactionTableModel(wallet, this);
  20. }
  21. qint64 WalletModel::getBalance() const
  22. {
  23. return wallet->GetBalance();
  24. }
  25. qint64 WalletModel::getUnconfirmedBalance() const
  26. {
  27. return wallet->GetUnconfirmedBalance();
  28. }
  29. int WalletModel::getNumTransactions() const
  30. {
  31. int numTransactions = 0;
  32. CRITICAL_BLOCK(wallet->cs_mapWallet)
  33. {
  34. numTransactions = wallet->mapWallet.size();
  35. }
  36. return numTransactions;
  37. }
  38. void WalletModel::update()
  39. {
  40. // Plainly emit all signals for now. To be more efficient this should check
  41. // whether the values actually changed first, although it'd be even better if these
  42. // were events coming in from the bitcoin core.
  43. emit balanceChanged(getBalance());
  44. emit numTransactionsChanged(getNumTransactions());
  45. addressTableModel->update();
  46. }
  47. WalletModel::StatusCode WalletModel::sendCoins(const QString &payTo, qint64 payAmount, const QString &addToAddressBookAs)
  48. {
  49. uint160 hash160 = 0;
  50. bool valid = false;
  51. if(!AddressToHash160(payTo.toUtf8().constData(), hash160))
  52. {
  53. return InvalidAddress;
  54. }
  55. if(payAmount <= 0)
  56. {
  57. return InvalidAmount;
  58. }
  59. if(payAmount > getBalance())
  60. {
  61. return AmountExceedsBalance;
  62. }
  63. if((payAmount + nTransactionFee) > getBalance())
  64. {
  65. return AmountWithFeeExceedsBalance;
  66. }
  67. CRITICAL_BLOCK(cs_main)
  68. {
  69. // Send to bitcoin address
  70. CWalletTx wtx;
  71. CScript scriptPubKey;
  72. scriptPubKey << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG;
  73. std::string strError = wallet->SendMoney(scriptPubKey, payAmount, wtx, true);
  74. if (strError == "")
  75. {
  76. // OK
  77. }
  78. else if (strError == "ABORTED")
  79. {
  80. return Aborted;
  81. }
  82. else
  83. {
  84. emit error(tr("Sending..."), QString::fromStdString(strError));
  85. return MiscError;
  86. }
  87. }
  88. // Add addresses that we've sent to to the address book
  89. std::string strAddress = payTo.toStdString();
  90. CRITICAL_BLOCK(wallet->cs_mapAddressBook)
  91. {
  92. if (!wallet->mapAddressBook.count(strAddress))
  93. wallet->SetAddressBookName(strAddress, addToAddressBookAs.toStdString());
  94. }
  95. return OK;
  96. }
  97. OptionsModel *WalletModel::getOptionsModel()
  98. {
  99. return optionsModel;
  100. }
  101. AddressTableModel *WalletModel::getAddressTableModel()
  102. {
  103. return addressTableModel;
  104. }
  105. TransactionTableModel *WalletModel::getTransactionTableModel()
  106. {
  107. return transactionTableModel;
  108. }