move back to original directory structure
[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     */
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(this);
22     addressTableModel = new AddressTableModel(this);
23     transactionTableModel = new TransactionTableModel(this);
24 }
25
26 qint64 ClientModel::getBalance()
27 {
28     return GetBalance();
29 }
30
31 QString ClientModel::getAddress()
32 {
33     std::vector<unsigned char> vchPubKey;
34     if (CWalletDB("r").ReadDefaultKey(vchPubKey))
35     {
36         return QString::fromStdString(PubKeyToAddress(vchPubKey));
37     }
38     else
39     {
40         return QString();
41     }
42 }
43
44 int ClientModel::getNumConnections()
45 {
46     return vNodes.size();
47 }
48
49 int ClientModel::getNumBlocks()
50 {
51     return nBestHeight;
52 }
53
54 int ClientModel::getNumTransactions()
55 {
56     int numTransactions = 0;
57     CRITICAL_BLOCK(cs_mapWallet)
58     {
59         numTransactions = mapWallet.size();
60     }
61     return numTransactions;
62 }
63
64 void ClientModel::update()
65 {
66     /* Plainly emit all signals for now. To be precise this should check
67        wether the values actually changed first.
68      */
69     emit balanceChanged(getBalance());
70     emit addressChanged(getAddress());
71     emit numConnectionsChanged(getNumConnections());
72     emit numBlocksChanged(getNumBlocks());
73     emit numTransactionsChanged(getNumTransactions());
74 }
75
76 void ClientModel::setAddress(const QString &defaultAddress)
77 {
78     uint160 hash160;
79     std::string strAddress = defaultAddress.toStdString();
80     if (!AddressToHash160(strAddress, hash160))
81         return;
82     if (!mapPubKeys.count(hash160))
83         return;
84     CWalletDB().WriteDefaultKey(mapPubKeys[hash160]);
85 }
86
87 ClientModel::StatusCode ClientModel::sendCoins(const QString &payTo, qint64 payAmount)
88 {
89     uint160 hash160 = 0;
90     bool valid = false;
91
92     if(!AddressToHash160(payTo.toUtf8().constData(), hash160))
93     {
94         return InvalidAddress;
95     }
96
97     if(payAmount <= 0)
98     {
99         return InvalidAmount;
100     }
101
102     if(payAmount > getBalance())
103     {
104         return AmountExceedsBalance;
105     }
106
107     if((payAmount + nTransactionFee) > getBalance())
108     {
109         return AmountWithFeeExceedsBalance;
110     }
111
112     CRITICAL_BLOCK(cs_main)
113     {
114         // Send to bitcoin address
115         CWalletTx wtx;
116         CScript scriptPubKey;
117         scriptPubKey << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG;
118
119         std::string strError = SendMoney(scriptPubKey, payAmount, wtx, true);
120         if (strError == "")
121         {
122             return OK;
123         }
124         else if (strError == "ABORTED")
125         {
126             return Aborted;
127         }
128         else
129         {
130             emit error(tr("Sending..."), QString::fromStdString(strError));
131             return MiscError;
132         }
133     }
134     // Add addresses that we've sent to to the address book
135     std::string strAddress = payTo.toStdString();
136     CRITICAL_BLOCK(cs_mapAddressBook)
137         if (!mapAddressBook.count(strAddress))
138             SetAddressBookName(strAddress, "");
139
140     return OK;
141 }
142
143 OptionsModel *ClientModel::getOptionsModel()
144 {
145     return optionsModel;
146 }
147
148 AddressTableModel *ClientModel::getAddressTableModel()
149 {
150     return addressTableModel;
151 }
152
153 TransactionTableModel *ClientModel::getTransactionTableModel()
154 {
155     return transactionTableModel;
156 }