Merge #12079: Improve prioritisetransaction test coverage
[bitcoinplatinum.git] / src / qt / editaddressdialog.cpp
blob6b7df2f2b9c17f2e419898ccea8e614e626ba86c
1 // Copyright (c) 2011-2017 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include <qt/editaddressdialog.h>
6 #include <qt/forms/ui_editaddressdialog.h>
8 #include <qt/addresstablemodel.h>
9 #include <qt/guiutil.h>
11 #include <QDataWidgetMapper>
12 #include <QMessageBox>
14 EditAddressDialog::EditAddressDialog(Mode _mode, QWidget *parent) :
15 QDialog(parent),
16 ui(new Ui::EditAddressDialog),
17 mapper(0),
18 mode(_mode),
19 model(0)
21 ui->setupUi(this);
23 GUIUtil::setupAddressWidget(ui->addressEdit, this);
25 switch(mode)
27 case NewReceivingAddress:
28 setWindowTitle(tr("New receiving address"));
29 ui->addressEdit->setEnabled(false);
30 break;
31 case NewSendingAddress:
32 setWindowTitle(tr("New sending address"));
33 break;
34 case EditReceivingAddress:
35 setWindowTitle(tr("Edit receiving address"));
36 ui->addressEdit->setEnabled(false);
37 break;
38 case EditSendingAddress:
39 setWindowTitle(tr("Edit sending address"));
40 break;
43 mapper = new QDataWidgetMapper(this);
44 mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
47 EditAddressDialog::~EditAddressDialog()
49 delete ui;
52 void EditAddressDialog::setModel(AddressTableModel *_model)
54 this->model = _model;
55 if(!_model)
56 return;
58 mapper->setModel(_model);
59 mapper->addMapping(ui->labelEdit, AddressTableModel::Label);
60 mapper->addMapping(ui->addressEdit, AddressTableModel::Address);
63 void EditAddressDialog::loadRow(int row)
65 mapper->setCurrentIndex(row);
68 bool EditAddressDialog::saveCurrentRow()
70 if(!model)
71 return false;
73 switch(mode)
75 case NewReceivingAddress:
76 case NewSendingAddress:
77 address = model->addRow(
78 mode == NewSendingAddress ? AddressTableModel::Send : AddressTableModel::Receive,
79 ui->labelEdit->text(),
80 ui->addressEdit->text());
81 break;
82 case EditReceivingAddress:
83 case EditSendingAddress:
84 if(mapper->submit())
86 address = ui->addressEdit->text();
88 break;
90 return !address.isEmpty();
93 void EditAddressDialog::accept()
95 if(!model)
96 return;
98 if(!saveCurrentRow())
100 switch(model->getEditStatus())
102 case AddressTableModel::OK:
103 // Failed with unknown reason. Just reject.
104 break;
105 case AddressTableModel::NO_CHANGES:
106 // No changes were made during edit operation. Just reject.
107 break;
108 case AddressTableModel::INVALID_ADDRESS:
109 QMessageBox::warning(this, windowTitle(),
110 tr("The entered address \"%1\" is not a valid Bitcoin address.").arg(ui->addressEdit->text()),
111 QMessageBox::Ok, QMessageBox::Ok);
112 break;
113 case AddressTableModel::DUPLICATE_ADDRESS:
114 QMessageBox::warning(this, windowTitle(),
115 tr("The entered address \"%1\" is already in the address book.").arg(ui->addressEdit->text()),
116 QMessageBox::Ok, QMessageBox::Ok);
117 break;
118 case AddressTableModel::WALLET_UNLOCK_FAILURE:
119 QMessageBox::critical(this, windowTitle(),
120 tr("Could not unlock wallet."),
121 QMessageBox::Ok, QMessageBox::Ok);
122 break;
123 case AddressTableModel::KEY_GENERATION_FAILURE:
124 QMessageBox::critical(this, windowTitle(),
125 tr("New key generation failed."),
126 QMessageBox::Ok, QMessageBox::Ok);
127 break;
130 return;
132 QDialog::accept();
135 QString EditAddressDialog::getAddress() const
137 return address;
140 void EditAddressDialog::setAddress(const QString &_address)
142 this->address = _address;
143 ui->addressEdit->setText(_address);