Line data Source code
1 : // Copyright (c) 2011-2019 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 : #if defined(HAVE_CONFIG_H)
6 : #include <config/bitcoin-config.h>
7 : #endif
8 :
9 : #include <qt/addressbookpage.h>
10 : #include <qt/forms/ui_addressbookpage.h>
11 :
12 : #include <qt/addresstablemodel.h>
13 : #include <qt/csvmodelwriter.h>
14 : #include <qt/editaddressdialog.h>
15 : #include <qt/guiutil.h>
16 : #include <qt/platformstyle.h>
17 :
18 : #include <QIcon>
19 : #include <QMenu>
20 : #include <QMessageBox>
21 : #include <QSortFilterProxyModel>
22 :
23 0 : class AddressBookSortFilterProxyModel final : public QSortFilterProxyModel
24 : {
25 : const QString m_type;
26 :
27 : public:
28 0 : AddressBookSortFilterProxyModel(const QString& type, QObject* parent)
29 0 : : QSortFilterProxyModel(parent)
30 0 : , m_type(type)
31 0 : {
32 0 : setDynamicSortFilter(true);
33 0 : setFilterCaseSensitivity(Qt::CaseInsensitive);
34 0 : setSortCaseSensitivity(Qt::CaseInsensitive);
35 0 : }
36 :
37 : protected:
38 0 : bool filterAcceptsRow(int row, const QModelIndex& parent) const override
39 : {
40 0 : auto model = sourceModel();
41 0 : auto label = model->index(row, AddressTableModel::Label, parent);
42 :
43 0 : if (model->data(label, AddressTableModel::TypeRole).toString() != m_type) {
44 0 : return false;
45 : }
46 :
47 0 : auto address = model->index(row, AddressTableModel::Address, parent);
48 :
49 0 : if (filterRegExp().indexIn(model->data(address).toString()) < 0 &&
50 0 : filterRegExp().indexIn(model->data(label).toString()) < 0) {
51 0 : return false;
52 : }
53 :
54 0 : return true;
55 0 : }
56 : };
57 :
58 0 : AddressBookPage::AddressBookPage(const PlatformStyle *platformStyle, Mode _mode, Tabs _tab, QWidget *parent) :
59 0 : QDialog(parent),
60 0 : ui(new Ui::AddressBookPage),
61 0 : model(nullptr),
62 0 : mode(_mode),
63 0 : tab(_tab)
64 0 : {
65 0 : ui->setupUi(this);
66 :
67 0 : if (!platformStyle->getImagesOnButtons()) {
68 0 : ui->newAddress->setIcon(QIcon());
69 0 : ui->copyAddress->setIcon(QIcon());
70 0 : ui->deleteAddress->setIcon(QIcon());
71 0 : ui->exportButton->setIcon(QIcon());
72 0 : } else {
73 0 : ui->newAddress->setIcon(platformStyle->SingleColorIcon(":/icons/add"));
74 0 : ui->copyAddress->setIcon(platformStyle->SingleColorIcon(":/icons/editcopy"));
75 0 : ui->deleteAddress->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
76 0 : ui->exportButton->setIcon(platformStyle->SingleColorIcon(":/icons/export"));
77 : }
78 :
79 0 : switch(mode)
80 : {
81 : case ForSelection:
82 0 : switch(tab)
83 : {
84 0 : case SendingTab: setWindowTitle(tr("Choose the address to send coins to")); break;
85 0 : case ReceivingTab: setWindowTitle(tr("Choose the address to receive coins with")); break;
86 : }
87 0 : connect(ui->tableView, &QTableView::doubleClicked, this, &QDialog::accept);
88 0 : ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
89 0 : ui->tableView->setFocus();
90 0 : ui->closeButton->setText(tr("C&hoose"));
91 0 : ui->exportButton->hide();
92 : break;
93 : case ForEditing:
94 0 : switch(tab)
95 : {
96 0 : case SendingTab: setWindowTitle(tr("Sending addresses")); break;
97 0 : case ReceivingTab: setWindowTitle(tr("Receiving addresses")); break;
98 : }
99 : break;
100 : }
101 0 : switch(tab)
102 : {
103 : case SendingTab:
104 0 : ui->labelExplanation->setText(tr("These are your Bitcoin addresses for sending payments. Always check the amount and the receiving address before sending coins."));
105 0 : ui->deleteAddress->setVisible(true);
106 0 : ui->newAddress->setVisible(true);
107 : break;
108 : case ReceivingTab:
109 0 : ui->labelExplanation->setText(tr("These are your Bitcoin addresses for receiving payments. Use the 'Create new receiving address' button in the receive tab to create new addresses.\nSigning is only possible with addresses of the type 'legacy'."));
110 0 : ui->deleteAddress->setVisible(false);
111 0 : ui->newAddress->setVisible(false);
112 : break;
113 : }
114 :
115 : // Context menu actions
116 0 : QAction *copyAddressAction = new QAction(tr("&Copy Address"), this);
117 0 : QAction *copyLabelAction = new QAction(tr("Copy &Label"), this);
118 0 : QAction *editAction = new QAction(tr("&Edit"), this);
119 0 : deleteAction = new QAction(ui->deleteAddress->text(), this);
120 :
121 : // Build context menu
122 0 : contextMenu = new QMenu(this);
123 0 : contextMenu->addAction(copyAddressAction);
124 0 : contextMenu->addAction(copyLabelAction);
125 0 : contextMenu->addAction(editAction);
126 0 : if(tab == SendingTab)
127 0 : contextMenu->addAction(deleteAction);
128 0 : contextMenu->addSeparator();
129 :
130 : // Connect signals for context menu actions
131 0 : connect(copyAddressAction, &QAction::triggered, this, &AddressBookPage::on_copyAddress_clicked);
132 0 : connect(copyLabelAction, &QAction::triggered, this, &AddressBookPage::onCopyLabelAction);
133 0 : connect(editAction, &QAction::triggered, this, &AddressBookPage::onEditAction);
134 0 : connect(deleteAction, &QAction::triggered, this, &AddressBookPage::on_deleteAddress_clicked);
135 :
136 0 : connect(ui->tableView, &QWidget::customContextMenuRequested, this, &AddressBookPage::contextualMenu);
137 :
138 0 : connect(ui->closeButton, &QPushButton::clicked, this, &QDialog::accept);
139 :
140 0 : GUIUtil::handleCloseWindowShortcut(this);
141 0 : }
142 :
143 0 : AddressBookPage::~AddressBookPage()
144 0 : {
145 0 : delete ui;
146 0 : }
147 :
148 0 : void AddressBookPage::setModel(AddressTableModel *_model)
149 : {
150 0 : this->model = _model;
151 0 : if(!_model)
152 : return;
153 :
154 0 : auto type = tab == ReceivingTab ? AddressTableModel::Receive : AddressTableModel::Send;
155 0 : proxyModel = new AddressBookSortFilterProxyModel(type, this);
156 0 : proxyModel->setSourceModel(_model);
157 :
158 0 : connect(ui->searchLineEdit, &QLineEdit::textChanged, proxyModel, &QSortFilterProxyModel::setFilterWildcard);
159 :
160 0 : ui->tableView->setModel(proxyModel);
161 0 : ui->tableView->sortByColumn(0, Qt::AscendingOrder);
162 :
163 : // Set column widths
164 0 : ui->tableView->horizontalHeader()->setSectionResizeMode(AddressTableModel::Label, QHeaderView::Stretch);
165 0 : ui->tableView->horizontalHeader()->setSectionResizeMode(AddressTableModel::Address, QHeaderView::ResizeToContents);
166 :
167 0 : connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged,
168 : this, &AddressBookPage::selectionChanged);
169 :
170 : // Select row for newly created address
171 0 : connect(_model, &AddressTableModel::rowsInserted, this, &AddressBookPage::selectNewAddress);
172 :
173 0 : selectionChanged();
174 0 : }
175 :
176 0 : void AddressBookPage::on_copyAddress_clicked()
177 : {
178 0 : GUIUtil::copyEntryData(ui->tableView, AddressTableModel::Address);
179 0 : }
180 :
181 0 : void AddressBookPage::onCopyLabelAction()
182 : {
183 0 : GUIUtil::copyEntryData(ui->tableView, AddressTableModel::Label);
184 0 : }
185 :
186 0 : void AddressBookPage::onEditAction()
187 : {
188 0 : if(!model)
189 : return;
190 :
191 0 : if(!ui->tableView->selectionModel())
192 : return;
193 0 : QModelIndexList indexes = ui->tableView->selectionModel()->selectedRows();
194 0 : if(indexes.isEmpty())
195 0 : return;
196 :
197 0 : EditAddressDialog dlg(
198 0 : tab == SendingTab ?
199 : EditAddressDialog::EditSendingAddress :
200 0 : EditAddressDialog::EditReceivingAddress, this);
201 0 : dlg.setModel(model);
202 0 : QModelIndex origIndex = proxyModel->mapToSource(indexes.at(0));
203 0 : dlg.loadRow(origIndex.row());
204 0 : dlg.exec();
205 0 : }
206 :
207 0 : void AddressBookPage::on_newAddress_clicked()
208 : {
209 0 : if(!model)
210 : return;
211 :
212 0 : if (tab == ReceivingTab) {
213 : return;
214 : }
215 :
216 0 : EditAddressDialog dlg(EditAddressDialog::NewSendingAddress, this);
217 0 : dlg.setModel(model);
218 0 : if(dlg.exec())
219 : {
220 0 : newAddressToSelect = dlg.getAddress();
221 0 : }
222 0 : }
223 :
224 0 : void AddressBookPage::on_deleteAddress_clicked()
225 : {
226 0 : QTableView *table = ui->tableView;
227 0 : if(!table->selectionModel())
228 0 : return;
229 :
230 0 : QModelIndexList indexes = table->selectionModel()->selectedRows();
231 0 : if(!indexes.isEmpty())
232 : {
233 0 : table->model()->removeRow(indexes.at(0).row());
234 0 : }
235 0 : }
236 :
237 0 : void AddressBookPage::selectionChanged()
238 : {
239 : // Set button states based on selected tab and selection
240 0 : QTableView *table = ui->tableView;
241 0 : if(!table->selectionModel())
242 0 : return;
243 :
244 0 : if(table->selectionModel()->hasSelection())
245 : {
246 0 : switch(tab)
247 : {
248 : case SendingTab:
249 : // In sending tab, allow deletion of selection
250 0 : ui->deleteAddress->setEnabled(true);
251 0 : ui->deleteAddress->setVisible(true);
252 0 : deleteAction->setEnabled(true);
253 0 : break;
254 : case ReceivingTab:
255 : // Deleting receiving addresses, however, is not allowed
256 0 : ui->deleteAddress->setEnabled(false);
257 0 : ui->deleteAddress->setVisible(false);
258 0 : deleteAction->setEnabled(false);
259 0 : break;
260 : }
261 0 : ui->copyAddress->setEnabled(true);
262 0 : }
263 : else
264 : {
265 0 : ui->deleteAddress->setEnabled(false);
266 0 : ui->copyAddress->setEnabled(false);
267 : }
268 0 : }
269 :
270 0 : void AddressBookPage::done(int retval)
271 : {
272 0 : QTableView *table = ui->tableView;
273 0 : if(!table->selectionModel() || !table->model())
274 0 : return;
275 :
276 : // Figure out which address was selected, and return it
277 0 : QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
278 :
279 0 : for (const QModelIndex& index : indexes) {
280 0 : QVariant address = table->model()->data(index);
281 0 : returnValue = address.toString();
282 0 : }
283 :
284 0 : if(returnValue.isEmpty())
285 : {
286 : // If no address entry selected, return rejected
287 : retval = Rejected;
288 0 : }
289 :
290 0 : QDialog::done(retval);
291 0 : }
292 :
293 0 : void AddressBookPage::on_exportButton_clicked()
294 : {
295 : // CSV is currently the only supported format
296 0 : QString filename = GUIUtil::getSaveFileName(this,
297 0 : tr("Export Address List"), QString(),
298 0 : tr("Comma separated file (*.csv)"), nullptr);
299 :
300 0 : if (filename.isNull())
301 0 : return;
302 :
303 0 : CSVModelWriter writer(filename);
304 :
305 : // name, column, role
306 0 : writer.setModel(proxyModel);
307 0 : writer.addColumn("Label", AddressTableModel::Label, Qt::EditRole);
308 0 : writer.addColumn("Address", AddressTableModel::Address, Qt::EditRole);
309 :
310 0 : if(!writer.write()) {
311 0 : QMessageBox::critical(this, tr("Exporting Failed"),
312 0 : tr("There was an error trying to save the address list to %1. Please try again.").arg(filename));
313 0 : }
314 0 : }
315 :
316 0 : void AddressBookPage::contextualMenu(const QPoint &point)
317 : {
318 0 : QModelIndex index = ui->tableView->indexAt(point);
319 0 : if(index.isValid())
320 : {
321 0 : contextMenu->exec(QCursor::pos());
322 0 : }
323 0 : }
324 :
325 0 : void AddressBookPage::selectNewAddress(const QModelIndex &parent, int begin, int /*end*/)
326 : {
327 0 : QModelIndex idx = proxyModel->mapFromSource(model->index(begin, AddressTableModel::Address, parent));
328 0 : if(idx.isValid() && (idx.data(Qt::EditRole).toString() == newAddressToSelect))
329 : {
330 : // Select row of newly created address, once
331 0 : ui->tableView->setFocus();
332 0 : ui->tableView->selectRow(idx.row());
333 0 : newAddressToSelect.clear();
334 0 : }
335 0 : }
|