On initial block chain download, show a progress bar
[novacoin.git] / src / qt / clientmodel.cpp
1 #include "clientmodel.h"
2 #include "main.h"
3 #include "guiconstants.h"
4 #include "optionsmodel.h"
5 #include "addresstablemodel.h"
6 #include "transactiontablemodel.h"
7
8 #include <QTimer>
9
10 ClientModel::ClientModel(QObject *parent) :
11     QObject(parent), optionsModel(0), addressTableModel(0),
12     transactionTableModel(0)
13 {
14     // Until signal notifications is built into the bitcoin core,
15     //  simply update everything after polling using a timer.
16     QTimer *timer = new QTimer(this);
17     connect(timer, SIGNAL(timeout()), this, SLOT(update()));
18     timer->start(MODEL_UPDATE_DELAY);
19
20     optionsModel = new OptionsModel(this);
21     addressTableModel = new AddressTableModel(this);
22     transactionTableModel = new TransactionTableModel(this);
23 }
24
25 qint64 ClientModel::getBalance() const
26 {
27     return GetBalance();
28 }
29
30 QString ClientModel::getAddress() const
31 {
32     std::vector<unsigned char> vchPubKey;
33     if (CWalletDB("r").ReadDefaultKey(vchPubKey))
34     {
35         return QString::fromStdString(PubKeyToAddress(vchPubKey));
36     }
37     else
38     {
39         return QString();
40     }
41 }
42
43 int ClientModel::getNumConnections() const
44 {
45     return vNodes.size();
46 }
47
48 int ClientModel::getNumBlocks() const
49 {
50     return nBestHeight;
51 }
52
53 int ClientModel::getNumTransactions() const
54 {
55     int numTransactions = 0;
56     CRITICAL_BLOCK(cs_mapWallet)
57     {
58         numTransactions = mapWallet.size();
59     }
60     return numTransactions;
61 }
62
63 void ClientModel::update()
64 {
65     // Plainly emit all signals for now. To be more efficient this should check
66     //   whether the values actually changed first.
67     emit balanceChanged(getBalance());
68     emit addressChanged(getAddress());
69     emit numConnectionsChanged(getNumConnections());
70     emit numBlocksChanged(getNumBlocks());
71     emit numTransactionsChanged(getNumTransactions());
72 }
73
74 void ClientModel::setAddress(const QString &defaultAddress)
75 {
76     uint160 hash160;
77     std::string strAddress = defaultAddress.toStdString();
78     if (!AddressToHash160(strAddress, hash160))
79         return;
80     if (!mapPubKeys.count(hash160))
81         return;
82     CWalletDB().WriteDefaultKey(mapPubKeys[hash160]);
83 }
84
85 ClientModel::StatusCode ClientModel::sendCoins(const QString &payTo, qint64 payAmount)
86 {
87     uint160 hash160 = 0;
88     bool valid = false;
89
90     if(!AddressToHash160(payTo.toUtf8().constData(), hash160))
91     {
92         return InvalidAddress;
93     }
94
95     if(payAmount <= 0)
96     {
97         return InvalidAmount;
98     }
99
100     if(payAmount > getBalance())
101     {
102         return AmountExceedsBalance;
103     }
104
105     if((payAmount + nTransactionFee) > getBalance())
106     {
107         return AmountWithFeeExceedsBalance;
108     }
109
110     CRITICAL_BLOCK(cs_main)
111     {
112         // Send to bitcoin address
113         CWalletTx wtx;
114         CScript scriptPubKey;
115         scriptPubKey << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG;
116
117         std::string strError = SendMoney(scriptPubKey, payAmount, wtx, true);
118         if (strError == "")
119         {
120             return OK;
121         }
122         else if (strError == "ABORTED")
123         {
124             return Aborted;
125         }
126         else
127         {
128             emit error(tr("Sending..."), QString::fromStdString(strError));
129             return MiscError;
130         }
131     }
132     // Add addresses that we've sent to to the address book
133     std::string strAddress = payTo.toStdString();
134     CRITICAL_BLOCK(cs_mapAddressBook)
135         if (!mapAddressBook.count(strAddress))
136             SetAddressBookName(strAddress, "");
137
138     return OK;
139 }
140
141 bool ClientModel::inInitialBlockDownload() const
142 {
143     return IsInitialBlockDownload();
144 }
145
146 int ClientModel::getTotalBlocksEstimate() const
147 {
148     return GetTotalBlocksEstimate();
149 }
150
151
152 OptionsModel *ClientModel::getOptionsModel()
153 {
154     return optionsModel;
155 }
156
157 AddressTableModel *ClientModel::getAddressTableModel()
158 {
159     return addressTableModel;
160 }
161
162 TransactionTableModel *ClientModel::getTransactionTableModel()
163 {
164     return transactionTableModel;
165 }