aboutsummaryrefslogtreecommitdiff
path: root/src/qt/clientmodel.cpp
blob: 86fc8b32d3a14862e9f02803675d77434ad5b395 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "clientmodel.h"
#include "main.h"
#include "guiconstants.h"
#include "optionsmodel.h"
#include "addresstablemodel.h"
#include "transactiontablemodel.h"

#include <QTimer>

ClientModel::ClientModel(QObject *parent) :
    QObject(parent), optionsModel(0), addressTableModel(0),
    transactionTableModel(0)
{
    // Until signal notifications is built into the bitcoin core,
    //  simply update everything after polling using a timer.
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    timer->start(MODEL_UPDATE_DELAY);

    optionsModel = new OptionsModel(this);
    addressTableModel = new AddressTableModel(this);
    transactionTableModel = new TransactionTableModel(this);
}

qint64 ClientModel::getBalance() const
{
    return GetBalance();
}

QString ClientModel::getAddress() const
{
    std::vector<unsigned char> vchPubKey;
    if (CWalletDB("r").ReadDefaultKey(vchPubKey))
    {
        return QString::fromStdString(PubKeyToAddress(vchPubKey));
    }
    else
    {
        return QString();
    }
}

int ClientModel::getNumConnections() const
{
    return vNodes.size();
}

int ClientModel::getNumBlocks() const
{
    return nBestHeight;
}

int ClientModel::getNumTransactions() const
{
    int numTransactions = 0;
    CRITICAL_BLOCK(cs_mapWallet)
    {
        numTransactions = mapWallet.size();
    }
    return numTransactions;
}

void ClientModel::update()
{
    // Plainly emit all signals for now. To be more efficient this should check
    //   whether the values actually changed first.
    emit balanceChanged(getBalance());
    emit addressChanged(getAddress());
    emit numConnectionsChanged(getNumConnections());
    emit numBlocksChanged(getNumBlocks());
    emit numTransactionsChanged(getNumTransactions());
}

void ClientModel::setAddress(const QString &defaultAddress)
{
    uint160 hash160;
    std::string strAddress = defaultAddress.toStdString();
    if (!AddressToHash160(strAddress, hash160))
        return;
    if (!mapPubKeys.count(hash160))
        return;
    CWalletDB().WriteDefaultKey(mapPubKeys[hash160]);
}

ClientModel::StatusCode ClientModel::sendCoins(const QString &payTo, qint64 payAmount)
{
    uint160 hash160 = 0;
    bool valid = false;

    if(!AddressToHash160(payTo.toUtf8().constData(), hash160))
    {
        return InvalidAddress;
    }

    if(payAmount <= 0)
    {
        return InvalidAmount;
    }

    if(payAmount > getBalance())
    {
        return AmountExceedsBalance;
    }

    if((payAmount + nTransactionFee) > getBalance())
    {
        return AmountWithFeeExceedsBalance;
    }

    CRITICAL_BLOCK(cs_main)
    {
        // Send to bitcoin address
        CWalletTx wtx;
        CScript scriptPubKey;
        scriptPubKey << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG;

        std::string strError = SendMoney(scriptPubKey, payAmount, wtx, true);
        if (strError == "")
        {
            return OK;
        }
        else if (strError == "ABORTED")
        {
            return Aborted;
        }
        else
        {
            emit error(tr("Sending..."), QString::fromStdString(strError));
            return MiscError;
        }
    }
    // Add addresses that we've sent to to the address book
    std::string strAddress = payTo.toStdString();
    CRITICAL_BLOCK(cs_mapAddressBook)
        if (!mapAddressBook.count(strAddress))
            SetAddressBookName(strAddress, "");

    return OK;
}

bool ClientModel::inInitialBlockDownload() const
{
    return IsInitialBlockDownload();
}

int ClientModel::getTotalBlocksEstimate() const
{
    return GetTotalBlocksEstimate();
}


OptionsModel *ClientModel::getOptionsModel()
{
    return optionsModel;
}

AddressTableModel *ClientModel::getAddressTableModel()
{
    return addressTableModel;
}

TransactionTableModel *ClientModel::getTransactionTableModel()
{
    return transactionTableModel;
}