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