166b9edd3062cdbb37e5a9e262040cbb0f75fb9a
[novacoin.git] / src / qt / multisigaddressentry.cpp
1 #include <QApplication>
2 #include <QClipboard>
3 #include <string>
4 #include <vector>
5
6 #include "addressbookpage.h"
7 #include "addresstablemodel.h"
8 #include "base58.h"
9 #include "guiutil.h"
10 #include "key.h"
11 #include "multisigaddressentry.h"
12 #include "ui_multisigaddressentry.h"
13 #include "walletmodel.h"
14
15
16 MultisigAddressEntry::MultisigAddressEntry(QWidget *parent) : QFrame(parent), ui(new Ui::MultisigAddressEntry), model(0)
17 {
18     ui->setupUi(this);
19     GUIUtil::setupAddressWidget(ui->address, this);
20 }
21
22 MultisigAddressEntry::~MultisigAddressEntry()
23 {
24     delete ui;
25 }
26
27 void MultisigAddressEntry::setModel(WalletModel *model)
28 {
29     this->model = model;
30     clear();
31 }
32
33 void MultisigAddressEntry::clear()
34 {
35     ui->pubkey->clear();
36     ui->address->clear();
37     ui->label->clear();
38     ui->pubkey->setFocus();
39 }
40
41 bool MultisigAddressEntry::validate()
42 {
43     return !ui->pubkey->text().isEmpty();
44 }
45
46 QString MultisigAddressEntry::getPubkey()
47 {
48     return ui->pubkey->text();
49 }
50
51 void MultisigAddressEntry::setRemoveEnabled(bool enabled)
52 {
53     ui->deleteButton->setEnabled(enabled);
54 }
55
56 void MultisigAddressEntry::on_pasteButton_clicked()
57 {
58     ui->address->setText(QApplication::clipboard()->text());
59 }
60
61 void MultisigAddressEntry::on_deleteButton_clicked()
62 {
63     emit removeEntry(this);
64 }
65
66 void MultisigAddressEntry::on_addressBookButton_clicked()
67 {
68     if(!model)
69         return;
70
71     AddressBookPage dlg(AddressBookPage::ForSending, AddressBookPage::ReceivingTab, this);
72     dlg.setModel(model->getAddressTableModel());
73     if(dlg.exec())
74     {
75         ui->address->setText(dlg.getReturnValue());
76     }
77 }
78
79 void MultisigAddressEntry::on_pubkey_textChanged(const QString &pubkey)
80 {
81     // Compute address from public key
82     std::vector<unsigned char> vchPubKey(ParseHex(pubkey.toStdString().c_str()));
83     CPubKey pkey(vchPubKey);
84     CKeyID keyID = pkey.GetID();
85     CBitcoinAddress address(keyID);
86     ui->address->setText(address.ToString().c_str());
87
88     if(!model)
89         return;
90
91     // Get label of address
92     QString associatedLabel = model->getAddressTableModel()->labelForAddress(address.ToString().c_str());
93     if(!associatedLabel.isEmpty())
94         ui->label->setText(associatedLabel);
95     else
96         ui->label->setText(QString());
97 }
98
99 void MultisigAddressEntry::on_address_textChanged(const QString &address)
100 {
101     if(!model)
102         return;
103
104     // Get public key of address
105     CBitcoinAddress addr(address.toStdString().c_str());
106     CKeyID keyID;
107     if(addr.GetKeyID(keyID))
108     {
109         CPubKey vchPubKey;
110         model->getPubKey(keyID, vchPubKey);
111         std::string pubkey = HexStr(vchPubKey.Raw());
112         if(!pubkey.empty())
113             ui->pubkey->setText(pubkey.c_str());
114     }
115
116     // Get label of address
117     QString associatedLabel = model->getAddressTableModel()->labelForAddress(address);
118     if(!associatedLabel.isEmpty())
119         ui->label->setText(associatedLabel);
120 }