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