update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
[novacoin.git] / src / qt / clientmodel.cpp
1 #include "clientmodel.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 ClientModel::ClientModel(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 ClientModel::getBalance() const
27 {
28     return wallet->GetBalance();
29 }
30
31 int ClientModel::getNumConnections() const
32 {
33     return vNodes.size();
34 }
35
36 int ClientModel::getNumBlocks() const
37 {
38     return nBestHeight;
39 }
40
41 int ClientModel::getNumTransactions() const
42 {
43     int numTransactions = 0;
44     CRITICAL_BLOCK(wallet->cs_mapWallet)
45     {
46         numTransactions = wallet->mapWallet.size();
47     }
48     return numTransactions;
49 }
50
51 void ClientModel::update()
52 {
53     // Plainly emit all signals for now. To be more efficient this should check
54     //   whether the values actually changed first, although it'd be even better if these
55     //   were events coming in from the bitcoin core.
56     emit balanceChanged(getBalance());
57     emit numConnectionsChanged(getNumConnections());
58     emit numBlocksChanged(getNumBlocks());
59     emit numTransactionsChanged(getNumTransactions());
60
61     addressTableModel->update();
62 }
63
64 ClientModel::StatusCode ClientModel::sendCoins(const QString &payTo, qint64 payAmount, const QString &addToAddressBookAs)
65 {
66     uint160 hash160 = 0;
67     bool valid = false;
68
69     if(!AddressToHash160(payTo.toUtf8().constData(), hash160))
70     {
71         return InvalidAddress;
72     }
73
74     if(payAmount <= 0)
75     {
76         return InvalidAmount;
77     }
78
79     if(payAmount > getBalance())
80     {
81         return AmountExceedsBalance;
82     }
83
84     if((payAmount + nTransactionFee) > getBalance())
85     {
86         return AmountWithFeeExceedsBalance;
87     }
88
89     CRITICAL_BLOCK(cs_main)
90     {
91         // Send to bitcoin address
92         CWalletTx wtx;
93         CScript scriptPubKey;
94         scriptPubKey << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG;
95
96         std::string strError = wallet->SendMoney(scriptPubKey, payAmount, wtx, true);
97         if (strError == "")
98         {
99             // OK
100         }
101         else if (strError == "ABORTED")
102         {
103             return Aborted;
104         }
105         else
106         {
107             emit error(tr("Sending..."), QString::fromStdString(strError));
108             return MiscError;
109         }
110     }
111
112     // Add addresses that we've sent to to the address book
113     std::string strAddress = payTo.toStdString();
114     CRITICAL_BLOCK(wallet->cs_mapAddressBook)
115     {
116         if (!wallet->mapAddressBook.count(strAddress))
117             wallet->SetAddressBookName(strAddress, addToAddressBookAs.toStdString());
118     }
119
120     return OK;
121 }
122
123 bool ClientModel::inInitialBlockDownload() const
124 {
125     return IsInitialBlockDownload();
126 }
127
128 int ClientModel::getTotalBlocksEstimate() const
129 {
130     return GetTotalBlocksEstimate();
131 }
132
133
134 OptionsModel *ClientModel::getOptionsModel()
135 {
136     return optionsModel;
137 }
138
139 AddressTableModel *ClientModel::getAddressTableModel()
140 {
141     return addressTableModel;
142 }
143
144 TransactionTableModel *ClientModel::getTransactionTableModel()
145 {
146     return transactionTableModel;
147 }