move getTransactionFee to OptionsModel
[novacoin.git] / gui / src / clientmodel.cpp
1 #include "clientmodel.h"
2 #include "main.h"
3 #include "guiconstants.h"
4 #include "optionsmodel.h"
5
6 #include <QTimer>
7
8 ClientModel::ClientModel(QObject *parent) :
9     QObject(parent), options_model(0)
10 {
11     /* Until we build signal notifications into the bitcoin core,
12        simply update everything using a timer.
13     */
14     QTimer *timer = new QTimer(this);
15     connect(timer, SIGNAL(timeout()), this, SLOT(update()));
16     timer->start(MODEL_UPDATE_DELAY);
17
18     options_model = new OptionsModel(this);
19 }
20
21 qint64 ClientModel::getBalance()
22 {
23     return GetBalance();
24 }
25
26 QString ClientModel::getAddress()
27 {
28     std::vector<unsigned char> vchPubKey;
29     if (CWalletDB("r").ReadDefaultKey(vchPubKey))
30     {
31         return QString::fromStdString(PubKeyToAddress(vchPubKey));
32     } else {
33         return QString();
34     }
35 }
36
37 int ClientModel::getNumConnections()
38 {
39     return vNodes.size();
40 }
41
42 int ClientModel::getNumBlocks()
43 {
44     return nBestHeight;
45 }
46
47 int ClientModel::getNumTransactions()
48 {
49     int numTransactions = 0;
50     CRITICAL_BLOCK(cs_mapWallet)
51     {
52         numTransactions = mapWallet.size();
53     }
54     return numTransactions;
55 }
56
57 void ClientModel::update()
58 {
59     emit balanceChanged(getBalance());
60     emit addressChanged(getAddress());
61     emit numConnectionsChanged(getNumConnections());
62     emit numBlocksChanged(getNumBlocks());
63     emit numTransactionsChanged(getNumTransactions());
64 }
65
66 ClientModel::StatusCode ClientModel::sendCoins(const QString &payTo, qint64 payAmount)
67 {
68     uint160 hash160 = 0;
69     bool valid = false;
70
71     if(!AddressToHash160(payTo.toUtf8().constData(), hash160))
72     {
73         return InvalidAddress;
74     }
75
76     if(payAmount <= 0)
77     {
78         return InvalidAmount;
79     }
80
81     if(payAmount > getBalance())
82     {
83         return AmountExceedsBalance;
84     }
85
86     if((payAmount + nTransactionFee) > getBalance())
87     {
88         return AmountWithFeeExceedsBalance;
89     }
90
91     CRITICAL_BLOCK(cs_main)
92     {
93         // Send to bitcoin address
94         CWalletTx wtx;
95         CScript scriptPubKey;
96         scriptPubKey << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG;
97
98         std::string strError = SendMoney(scriptPubKey, payAmount, wtx, true);
99         if (strError == "")
100             return OK;
101         else if (strError == "ABORTED")
102             return Aborted;
103         else
104         {
105             emit error(tr("Sending..."), QString::fromStdString(strError));
106             return MiscError;
107         }
108     }
109
110     return OK;
111 }
112
113 OptionsModel *ClientModel::getOptionsModel()
114 {
115     return options_model;
116 }