e39bb7ecb1d601bb012014ffe09e9f3b861d9bd7
[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, const QString &addToAddressBookAs)
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             // 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
111     // Add addresses that we've sent to to the address book
112     std::string strAddress = payTo.toStdString();
113     CRITICAL_BLOCK(cs_mapAddressBook)
114         if (!mapAddressBook.count(strAddress))
115             SetAddressBookName(strAddress, addToAddressBookAs.toStdString());
116
117     return OK;
118 }
119
120 bool ClientModel::inInitialBlockDownload() const
121 {
122     return IsInitialBlockDownload();
123 }
124
125 int ClientModel::getTotalBlocksEstimate() const
126 {
127     return GetTotalBlocksEstimate();
128 }
129
130
131 OptionsModel *ClientModel::getOptionsModel()
132 {
133     return optionsModel;
134 }
135
136 AddressTableModel *ClientModel::getAddressTableModel()
137 {
138     return addressTableModel;
139 }
140
141 TransactionTableModel *ClientModel::getTransactionTableModel()
142 {
143     return transactionTableModel;
144 }