]>
Commit | Line | Data |
---|---|---|
18cab09a WL |
1 | #include "clientmodel.h" |
2 | #include "main.h" | |
63760fa1 | 3 | #include "guiconstants.h" |
92f20d53 | 4 | #include "optionsmodel.h" |
2547f1f7 | 5 | #include "addresstablemodel.h" |
467c31ea | 6 | #include "transactiontablemodel.h" |
18cab09a WL |
7 | |
8 | #include <QTimer> | |
9 | ||
18cab09a | 10 | ClientModel::ClientModel(QObject *parent) : |
467c31ea WL |
11 | QObject(parent), optionsModel(0), addressTableModel(0), |
12 | transactionTableModel(0) | |
18cab09a | 13 | { |
0f3981be WL |
14 | // Until signal notifications is built into the bitcoin core, |
15 | // simply update everything after polling using a timer. | |
18cab09a WL |
16 | QTimer *timer = new QTimer(this); |
17 | connect(timer, SIGNAL(timeout()), this, SLOT(update())); | |
18 | timer->start(MODEL_UPDATE_DELAY); | |
92f20d53 | 19 | |
2547f1f7 WL |
20 | optionsModel = new OptionsModel(this); |
21 | addressTableModel = new AddressTableModel(this); | |
467c31ea | 22 | transactionTableModel = new TransactionTableModel(this); |
18cab09a WL |
23 | } |
24 | ||
7df70c00 | 25 | qint64 ClientModel::getBalance() const |
18cab09a WL |
26 | { |
27 | return GetBalance(); | |
28 | } | |
29 | ||
7df70c00 | 30 | QString ClientModel::getAddress() const |
18cab09a WL |
31 | { |
32 | std::vector<unsigned char> vchPubKey; | |
33 | if (CWalletDB("r").ReadDefaultKey(vchPubKey)) | |
34 | { | |
35 | return QString::fromStdString(PubKeyToAddress(vchPubKey)); | |
8e86dca2 WL |
36 | } |
37 | else | |
38 | { | |
18cab09a WL |
39 | return QString(); |
40 | } | |
41 | } | |
42 | ||
7df70c00 | 43 | int ClientModel::getNumConnections() const |
18cab09a WL |
44 | { |
45 | return vNodes.size(); | |
46 | } | |
47 | ||
7df70c00 | 48 | int ClientModel::getNumBlocks() const |
18cab09a WL |
49 | { |
50 | return nBestHeight; | |
51 | } | |
52 | ||
7df70c00 | 53 | int ClientModel::getNumTransactions() const |
18cab09a | 54 | { |
dd8e82f7 WL |
55 | int numTransactions = 0; |
56 | CRITICAL_BLOCK(cs_mapWallet) | |
57 | { | |
58 | numTransactions = mapWallet.size(); | |
59 | } | |
60 | return numTransactions; | |
18cab09a WL |
61 | } |
62 | ||
63 | void ClientModel::update() | |
64 | { | |
0f3981be WL |
65 | // Plainly emit all signals for now. To be more efficient this should check |
66 | // whether the values actually changed first. | |
18cab09a WL |
67 | emit balanceChanged(getBalance()); |
68 | emit addressChanged(getAddress()); | |
69 | emit numConnectionsChanged(getNumConnections()); | |
70 | emit numBlocksChanged(getNumBlocks()); | |
71 | emit numTransactionsChanged(getNumTransactions()); | |
72 | } | |
6630c1cb | 73 | |
9d9a4e87 WL |
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 | ||
6630c1cb WL |
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 == "") | |
8e86dca2 | 119 | { |
6630c1cb | 120 | return OK; |
8e86dca2 | 121 | } |
6630c1cb | 122 | else if (strError == "ABORTED") |
8e86dca2 | 123 | { |
6630c1cb | 124 | return Aborted; |
8e86dca2 | 125 | } |
6630c1cb WL |
126 | else |
127 | { | |
128 | emit error(tr("Sending..."), QString::fromStdString(strError)); | |
129 | return MiscError; | |
130 | } | |
131 | } | |
c51f0512 WL |
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, ""); | |
6630c1cb WL |
137 | |
138 | return OK; | |
139 | } | |
140 | ||
7df70c00 WL |
141 | bool ClientModel::inInitialBlockDownload() const |
142 | { | |
143 | return IsInitialBlockDownload(); | |
144 | } | |
145 | ||
92f20d53 WL |
146 | OptionsModel *ClientModel::getOptionsModel() |
147 | { | |
2547f1f7 WL |
148 | return optionsModel; |
149 | } | |
150 | ||
151 | AddressTableModel *ClientModel::getAddressTableModel() | |
152 | { | |
153 | return addressTableModel; | |
92f20d53 | 154 | } |
467c31ea WL |
155 | |
156 | TransactionTableModel *ClientModel::getTransactionTableModel() | |
157 | { | |
158 | return transactionTableModel; | |
159 | } |