Line data Source code
1 : // Copyright (c) 2011-2018 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. 4 : 5 : #include <qt/editaddressdialog.h> 6 : #include <qt/forms/ui_editaddressdialog.h> 7 : 8 : #include <qt/addresstablemodel.h> 9 : #include <qt/guiutil.h> 10 : 11 : #include <QDataWidgetMapper> 12 : #include <QMessageBox> 13 : 14 : 15 0 : EditAddressDialog::EditAddressDialog(Mode _mode, QWidget *parent) : 16 0 : QDialog(parent), 17 0 : ui(new Ui::EditAddressDialog), 18 0 : mapper(nullptr), 19 0 : mode(_mode), 20 0 : model(nullptr) 21 0 : { 22 0 : ui->setupUi(this); 23 : 24 0 : GUIUtil::setupAddressWidget(ui->addressEdit, this); 25 : 26 0 : switch(mode) 27 : { 28 : case NewSendingAddress: 29 0 : setWindowTitle(tr("New sending address")); 30 0 : break; 31 : case EditReceivingAddress: 32 0 : setWindowTitle(tr("Edit receiving address")); 33 0 : ui->addressEdit->setEnabled(false); 34 : break; 35 : case EditSendingAddress: 36 0 : setWindowTitle(tr("Edit sending address")); 37 0 : break; 38 : } 39 : 40 0 : mapper = new QDataWidgetMapper(this); 41 0 : mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit); 42 : 43 0 : GUIUtil::ItemDelegate* delegate = new GUIUtil::ItemDelegate(mapper); 44 0 : connect(delegate, &GUIUtil::ItemDelegate::keyEscapePressed, this, &EditAddressDialog::reject); 45 0 : mapper->setItemDelegate(delegate); 46 : 47 0 : GUIUtil::handleCloseWindowShortcut(this); 48 0 : } 49 : 50 0 : EditAddressDialog::~EditAddressDialog() 51 0 : { 52 0 : delete ui; 53 0 : } 54 : 55 0 : void EditAddressDialog::setModel(AddressTableModel *_model) 56 : { 57 0 : this->model = _model; 58 0 : if(!_model) 59 : return; 60 : 61 0 : mapper->setModel(_model); 62 0 : mapper->addMapping(ui->labelEdit, AddressTableModel::Label); 63 0 : mapper->addMapping(ui->addressEdit, AddressTableModel::Address); 64 0 : } 65 : 66 0 : void EditAddressDialog::loadRow(int row) 67 : { 68 0 : mapper->setCurrentIndex(row); 69 0 : } 70 : 71 0 : bool EditAddressDialog::saveCurrentRow() 72 : { 73 0 : if(!model) 74 0 : return false; 75 : 76 0 : switch(mode) 77 : { 78 : case NewSendingAddress: 79 0 : address = model->addRow( 80 : AddressTableModel::Send, 81 0 : ui->labelEdit->text(), 82 0 : ui->addressEdit->text(), 83 0 : model->GetDefaultAddressType()); 84 0 : break; 85 : case EditReceivingAddress: 86 : case EditSendingAddress: 87 0 : if(mapper->submit()) 88 : { 89 0 : address = ui->addressEdit->text(); 90 0 : } 91 : break; 92 : } 93 0 : return !address.isEmpty(); 94 0 : } 95 : 96 0 : void EditAddressDialog::accept() 97 : { 98 0 : if(!model) 99 : return; 100 : 101 0 : if(!saveCurrentRow()) 102 : { 103 0 : switch(model->getEditStatus()) 104 : { 105 : case AddressTableModel::OK: 106 : // Failed with unknown reason. Just reject. 107 : break; 108 : case AddressTableModel::NO_CHANGES: 109 : // No changes were made during edit operation. Just reject. 110 : break; 111 : case AddressTableModel::INVALID_ADDRESS: 112 0 : QMessageBox::warning(this, windowTitle(), 113 0 : tr("The entered address \"%1\" is not a valid Bitcoin address.").arg(ui->addressEdit->text()), 114 : QMessageBox::Ok, QMessageBox::Ok); 115 0 : break; 116 : case AddressTableModel::DUPLICATE_ADDRESS: 117 0 : QMessageBox::warning(this, windowTitle(), 118 0 : getDuplicateAddressWarning(), 119 : QMessageBox::Ok, QMessageBox::Ok); 120 0 : break; 121 : case AddressTableModel::WALLET_UNLOCK_FAILURE: 122 0 : QMessageBox::critical(this, windowTitle(), 123 0 : tr("Could not unlock wallet."), 124 : QMessageBox::Ok, QMessageBox::Ok); 125 0 : break; 126 : case AddressTableModel::KEY_GENERATION_FAILURE: 127 0 : QMessageBox::critical(this, windowTitle(), 128 0 : tr("New key generation failed."), 129 : QMessageBox::Ok, QMessageBox::Ok); 130 0 : break; 131 : 132 : } 133 : return; 134 : } 135 0 : QDialog::accept(); 136 0 : } 137 : 138 0 : QString EditAddressDialog::getDuplicateAddressWarning() const 139 : { 140 0 : QString dup_address = ui->addressEdit->text(); 141 0 : QString existing_label = model->labelForAddress(dup_address); 142 0 : QString existing_purpose = model->purposeForAddress(dup_address); 143 : 144 0 : if (existing_purpose == "receive" && 145 0 : (mode == NewSendingAddress || mode == EditSendingAddress)) { 146 0 : return tr( 147 : "Address \"%1\" already exists as a receiving address with label " 148 : "\"%2\" and so cannot be added as a sending address." 149 0 : ).arg(dup_address).arg(existing_label); 150 : } 151 0 : return tr( 152 : "The entered address \"%1\" is already in the address book with " 153 : "label \"%2\"." 154 0 : ).arg(dup_address).arg(existing_label); 155 0 : } 156 : 157 0 : QString EditAddressDialog::getAddress() const 158 : { 159 0 : return address; 160 : } 161 : 162 0 : void EditAddressDialog::setAddress(const QString &_address) 163 : { 164 0 : this->address = _address; 165 0 : ui->addressEdit->setText(_address); 166 0 : }