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