Add context menu on transaction list: copy label, copy address, edit label, show...
[novacoin.git] / src / qt / walletmodel.cpp
1 #include "walletmodel.h"
2 #include "guiconstants.h"
3 #include "optionsmodel.h"
4 #include "addresstablemodel.h"
5 #include "transactiontablemodel.h"
6
7 #include "headers.h"
8
9 #include <QTimer>
10
11 WalletModel::WalletModel(CWallet *wallet, QObject *parent) :
12     QObject(parent), wallet(wallet), optionsModel(0), addressTableModel(0),
13     transactionTableModel(0)
14 {
15     // Until signal notifications is built into the bitcoin core,
16     //  simply update everything after polling using a timer.
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(wallet, this);
22     addressTableModel = new AddressTableModel(wallet, this);
23     transactionTableModel = new TransactionTableModel(wallet, this);
24 }
25
26 qint64 WalletModel::getBalance() const
27 {
28     return wallet->GetBalance();
29 }
30
31 int WalletModel::getNumTransactions() const
32 {
33     int numTransactions = 0;
34     CRITICAL_BLOCK(wallet->cs_mapWallet)
35     {
36         numTransactions = wallet->mapWallet.size();
37     }
38     return numTransactions;
39 }
40
41 void WalletModel::update()
42 {
43     // Plainly emit all signals for now. To be more efficient this should check
44     //   whether the values actually changed first, although it'd be even better if these
45     //   were events coming in from the bitcoin core.
46     emit balanceChanged(getBalance());
47     emit numTransactionsChanged(getNumTransactions());
48
49     addressTableModel->update();
50 }
51
52 WalletModel::StatusCode WalletModel::sendCoins(const QString &payTo, qint64 payAmount, const QString &addToAddressBookAs)
53 {
54     uint160 hash160 = 0;
55     bool valid = false;
56
57     if(!AddressToHash160(payTo.toUtf8().constData(), hash160))
58     {
59         return InvalidAddress;
60     }
61
62     if(payAmount <= 0)
63     {
64         return InvalidAmount;
65     }
66
67     if(payAmount > getBalance())
68     {
69         return AmountExceedsBalance;
70     }
71
72     if((payAmount + nTransactionFee) > getBalance())
73     {
74         return AmountWithFeeExceedsBalance;
75     }
76
77     CRITICAL_BLOCK(cs_main)
78     {
79         // Send to bitcoin address
80         CWalletTx wtx;
81         CScript scriptPubKey;
82         scriptPubKey << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG;
83
84         std::string strError = wallet->SendMoney(scriptPubKey, payAmount, wtx, true);
85         if (strError == "")
86         {
87             // OK
88         }
89         else if (strError == "ABORTED")
90         {
91             return Aborted;
92         }
93         else
94         {
95             emit error(tr("Sending..."), QString::fromStdString(strError));
96             return MiscError;
97         }
98     }
99
100     // Add addresses that we've sent to to the address book
101     std::string strAddress = payTo.toStdString();
102     CRITICAL_BLOCK(wallet->cs_mapAddressBook)
103     {
104         if (!wallet->mapAddressBook.count(strAddress))
105             wallet->SetAddressBookName(strAddress, addToAddressBookAs.toStdString());
106     }
107
108     return OK;
109 }
110
111 OptionsModel *WalletModel::getOptionsModel()
112 {
113     return optionsModel;
114 }
115
116 AddressTableModel *WalletModel::getAddressTableModel()
117 {
118     return addressTableModel;
119 }
120
121 TransactionTableModel *WalletModel::getTransactionTableModel()
122 {
123     return transactionTableModel;
124 }
125
126