forked from digibyte/digibyte
-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathddaddressbookpage.cpp
More file actions
538 lines (474 loc) · 17.9 KB
/
Copy pathddaddressbookpage.cpp
File metadata and controls
538 lines (474 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
// Copyright (c) 2025 The DigiByte Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <qt/ddaddressbookpage.h>
#include <qt/walletmodel.h>
#include <qt/guiutil.h>
#include <qt/optionsmodel.h>
#include <qt/platformstyle.h>
#include <base58.h>
#include <key_io.h>
#include <wallet/types.h>
#include <QTableWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QMenu>
#include <QLineEdit>
#include <QHeaderView>
#include <QClipboard>
#include <QApplication>
#include <QMessageBox>
#include <QInputDialog>
#include <QFile>
#include <QFileDialog>
#include <QPalette>
#include <QTextStream>
namespace {
bool UseDarkDigiDollarAddressBookTheme(const WalletModel* model, const QWidget* widget)
{
if (model && model->getOptionsModel()) {
const QString theme = model->getOptionsModel()->data(
model->getOptionsModel()->index(OptionsModel::Theme), Qt::EditRole).toString();
return theme.isEmpty() || theme == QLatin1String("dark");
}
const QPalette palette = widget ? widget->palette() : qApp->palette();
return palette.color(QPalette::Window).lightness() < 128;
}
QString DigiDollarAddressBookStyleSheet(bool dark_theme)
{
if (dark_theme) {
return QStringLiteral(
"QDialog#DDAddressBookPage {"
" background-color: #0b2419;"
" color: #ffffff;"
"}"
"QDialog#DDAddressBookPage QLabel {"
" color: #ffffff;"
" font-weight: bold;"
"}"
"QDialog#DDAddressBookPage QTableWidget {"
" background-color: #113a29;"
" alternate-background-color: #164532;"
" color: #ffffff;"
" selection-background-color: #16804f;"
" selection-color: #ffffff;"
" border: 1px solid #42d884;"
" gridline-color: #42d884;"
" font-size: 11pt;"
"}"
"QDialog#DDAddressBookPage QTableWidget::item {"
" color: #ffffff;"
" padding: 8px;"
"}"
"QDialog#DDAddressBookPage QTableWidget::item:alternate {"
" background-color: #164532;"
"}"
"QDialog#DDAddressBookPage QTableWidget::item:selected {"
" background-color: #16804f;"
" color: #ffffff;"
"}"
"QDialog#DDAddressBookPage QTableWidget::item:hover {"
" background-color: #1b5b3f;"
" color: #ffffff;"
"}"
"QDialog#DDAddressBookPage QHeaderView::section {"
" background-color: #16804f;"
" color: #ffffff;"
" border: none;"
" padding: 8px;"
" font-weight: bold;"
"}"
"QDialog#DDAddressBookPage QLineEdit {"
" background-color: #113a29;"
" color: #ffffff;"
" border: 2px solid #42d884;"
" border-radius: 4px;"
" padding: 6px;"
"}"
"QDialog#DDAddressBookPage QLineEdit:focus {"
" border-color: #21a866;"
" background-color: #164532;"
"}"
"QDialog#DDAddressBookPage QPushButton {"
" background-color: #16804f;"
" color: #ffffff;"
" border: 2px solid #16804f;"
" border-radius: 4px;"
" padding: 8px 16px;"
" font-weight: bold;"
" min-height: 28px;"
"}"
"QDialog#DDAddressBookPage QPushButton:hover {"
" background-color: #21a866;"
" border-color: #21a866;"
"}"
"QDialog#DDAddressBookPage QPushButton:pressed {"
" background-color: #0f633c;"
" border-color: #0f633c;"
"}"
"QDialog#DDAddressBookPage QMenu {"
" background-color: #113a29;"
" color: #ffffff;"
" border: 1px solid #42d884;"
"}"
"QDialog#DDAddressBookPage QMenu::item:selected {"
" background-color: #16804f;"
" color: #ffffff;"
"}");
}
return QStringLiteral(
"QDialog#DDAddressBookPage {"
" background-color: #eef9f2;"
" color: #123f2b;"
"}"
"QDialog#DDAddressBookPage QLabel {"
" color: #123f2b;"
" font-weight: bold;"
"}"
"QDialog#DDAddressBookPage QTableWidget {"
" background-color: #ffffff;"
" alternate-background-color: #e6f7ec;"
" color: #123f2b;"
" selection-background-color: #1f9d57;"
" selection-color: #ffffff;"
" border: 1px solid #1f9d57;"
" gridline-color: #c8ead6;"
" font-size: 11pt;"
"}"
"QDialog#DDAddressBookPage QTableWidget::item {"
" color: #123f2b;"
" padding: 8px;"
"}"
"QDialog#DDAddressBookPage QTableWidget::item:alternate {"
" background-color: #e6f7ec;"
"}"
"QDialog#DDAddressBookPage QTableWidget::item:selected {"
" background-color: #1f9d57;"
" color: #ffffff;"
"}"
"QDialog#DDAddressBookPage QTableWidget::item:hover {"
" background-color: #d7f2e1;"
" color: #123f2b;"
"}"
"QDialog#DDAddressBookPage QHeaderView::section {"
" background-color: #1f9d57;"
" color: #ffffff;"
" border: none;"
" padding: 8px;"
" font-weight: bold;"
"}"
"QDialog#DDAddressBookPage QLineEdit {"
" background-color: #ffffff;"
" color: #123f2b;"
" border: 2px solid #1f9d57;"
" border-radius: 4px;"
" padding: 6px;"
"}"
"QDialog#DDAddressBookPage QLineEdit:focus {"
" border-color: #26b96a;"
" background-color: #f6fff9;"
"}"
"QDialog#DDAddressBookPage QPushButton {"
" background-color: #1f9d57;"
" color: #ffffff;"
" border: 2px solid #1f9d57;"
" border-radius: 4px;"
" padding: 8px 16px;"
" font-weight: bold;"
" min-height: 28px;"
"}"
"QDialog#DDAddressBookPage QPushButton:hover {"
" background-color: #26b96a;"
" border-color: #26b96a;"
"}"
"QDialog#DDAddressBookPage QPushButton:pressed {"
" background-color: #147a42;"
" border-color: #147a42;"
"}"
"QDialog#DDAddressBookPage QMenu {"
" background-color: #ffffff;"
" color: #123f2b;"
" border: 1px solid #1f9d57;"
"}"
"QDialog#DDAddressBookPage QMenu::item:selected {"
" background-color: #1f9d57;"
" color: #ffffff;"
"}");
}
} // namespace
DDAddressBookPage::DDAddressBookPage(const PlatformStyle *platformStyle, Mode mode, QWidget* parent)
: QDialog(parent, GUIUtil::dialog_flags)
, m_mode(mode)
, m_walletModel(nullptr)
, m_platformStyle(platformStyle)
, m_mainLayout(nullptr)
, m_explanationLabel(nullptr)
, m_searchEdit(nullptr)
, m_table(nullptr)
, m_buttonLayout(nullptr)
, m_newButton(nullptr)
, m_copyButton(nullptr)
, m_deleteButton(nullptr)
, m_exportButton(nullptr)
, m_closeButton(nullptr)
, m_contextMenu(nullptr)
{
setObjectName("DDAddressBookPage");
if (m_mode == ForSelection) {
setWindowTitle(tr("Choose the address to send coins to"));
} else {
setWindowTitle(tr("DigiDollar Address Book"));
}
setMinimumSize(760, 380);
setupUI();
applyTheme();
GUIUtil::handleCloseWindowShortcut(this);
}
DDAddressBookPage::~DDAddressBookPage()
{
}
void DDAddressBookPage::setupUI()
{
m_mainLayout = new QVBoxLayout(this);
m_explanationLabel = new QLabel(this);
m_explanationLabel->setWordWrap(true);
m_explanationLabel->setText(tr("These are your DigiDollar addresses for sending payments. Always check the amount and the receiving address before sending coins."));
m_mainLayout->addWidget(m_explanationLabel);
m_searchEdit = new QLineEdit(this);
m_searchEdit->setPlaceholderText(tr("Enter address or label to search"));
connect(m_searchEdit, &QLineEdit::textChanged, this, &DDAddressBookPage::onSearchTextChanged);
m_mainLayout->addWidget(m_searchEdit);
m_table = new QTableWidget(this);
m_table->setColumnCount(Column::ColumnCount);
m_table->setHorizontalHeaderLabels({tr("Label"), tr("Address")});
m_table->setSelectionBehavior(QAbstractItemView::SelectRows);
m_table->setSelectionMode(QAbstractItemView::SingleSelection);
m_table->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_table->setSortingEnabled(true);
m_table->setAlternatingRowColors(true);
m_table->setShowGrid(false);
m_table->setContextMenuPolicy(Qt::CustomContextMenu);
m_table->verticalHeader()->setVisible(false);
m_table->horizontalHeader()->setSectionResizeMode(Column::Label, QHeaderView::Stretch);
m_table->horizontalHeader()->setSectionResizeMode(Column::Address, QHeaderView::ResizeToContents);
m_table->setToolTip(tr("Right-click to edit address or label"));
connect(m_table, &QTableWidget::customContextMenuRequested, this, &DDAddressBookPage::showContextMenu);
connect(m_table, &QTableWidget::itemSelectionChanged, this, &DDAddressBookPage::selectionChanged);
connect(m_table, &QTableWidget::doubleClicked, this, &DDAddressBookPage::accept);
m_mainLayout->addWidget(m_table);
m_buttonLayout = new QHBoxLayout();
m_newButton = new QPushButton(tr("&New"), this);
m_newButton->setToolTip(tr("Create a new DigiDollar address"));
if (m_platformStyle && m_platformStyle->getImagesOnButtons()) {
m_newButton->setIcon(m_platformStyle->SingleColorIcon(":/icons/add"));
}
connect(m_newButton, &QPushButton::clicked, this, &DDAddressBookPage::onNewAddress);
m_copyButton = new QPushButton(tr("&Copy"), this);
m_copyButton->setToolTip(tr("Copy the selected address to clipboard"));
m_copyButton->setEnabled(false);
if (m_platformStyle && m_platformStyle->getImagesOnButtons()) {
m_copyButton->setIcon(m_platformStyle->SingleColorIcon(":/icons/editcopy"));
}
connect(m_copyButton, &QPushButton::clicked, this, &DDAddressBookPage::onCopyAddress);
m_deleteButton = new QPushButton(tr("&Delete"), this);
m_deleteButton->setToolTip(tr("Delete the selected address"));
m_deleteButton->setEnabled(false);
if (m_platformStyle && m_platformStyle->getImagesOnButtons()) {
m_deleteButton->setIcon(m_platformStyle->SingleColorIcon(":/icons/remove"));
}
connect(m_deleteButton, &QPushButton::clicked, this, &DDAddressBookPage::onDeleteAddress);
m_exportButton = new QPushButton(tr("&Export"), this);
m_exportButton->setToolTip(tr("Export address list to CSV"));
if (m_platformStyle && m_platformStyle->getImagesOnButtons()) {
m_exportButton->setIcon(m_platformStyle->SingleColorIcon(":/icons/export"));
}
connect(m_exportButton, &QPushButton::clicked, this, &DDAddressBookPage::onExport);
m_closeButton = new QPushButton(m_mode == ForSelection ? tr("C&hoose") : tr("C&lose"), this);
connect(m_closeButton, &QPushButton::clicked, this, &DDAddressBookPage::accept);
m_buttonLayout->addWidget(m_newButton);
m_buttonLayout->addWidget(m_copyButton);
m_buttonLayout->addWidget(m_deleteButton);
m_buttonLayout->addStretch();
if (m_mode == ForSelection) {
m_exportButton->hide();
} else {
m_buttonLayout->addWidget(m_exportButton);
}
m_buttonLayout->addWidget(m_closeButton);
m_mainLayout->addLayout(m_buttonLayout);
m_contextMenu = new QMenu(this);
m_contextMenu->addAction(tr("&Copy Address"), this, &DDAddressBookPage::onCopyAddress);
m_contextMenu->addAction(tr("Copy &Label"), this, [this]() {
int row = m_table->currentRow();
if (row >= 0) {
QTableWidgetItem* item = m_table->item(row, Column::Label);
if (item) QApplication::clipboard()->setText(item->text());
}
});
m_contextMenu->addAction(tr("&Edit"), this, &DDAddressBookPage::onEditAddress);
m_contextMenu->addAction(tr("&Delete"), this, &DDAddressBookPage::onDeleteAddress);
setLayout(m_mainLayout);
}
void DDAddressBookPage::setWalletModel(WalletModel* model)
{
m_walletModel = model;
applyTheme();
if (m_walletModel) {
refreshAddressList();
}
}
void DDAddressBookPage::applyTheme()
{
setStyleSheet(DigiDollarAddressBookStyleSheet(
UseDarkDigiDollarAddressBookTheme(m_walletModel, this)));
}
void DDAddressBookPage::refreshAddressList()
{
if (!m_walletModel) return;
m_table->setRowCount(0);
m_table->setSortingEnabled(false);
QString searchText = m_searchEdit ? m_searchEdit->text().toLower() : QString();
for (const auto& addr : m_walletModel->wallet().getAddresses()) {
if (addr.purpose == wallet::AddressPurpose::DIGIDOLLAR) {
QString label = QString::fromStdString(addr.name);
QString addressStr = QString::fromStdString(EncodeDigiDollarAddress(addr.dest));
if (!searchText.isEmpty()) {
if (!label.toLower().contains(searchText) && !addressStr.toLower().contains(searchText)) {
continue;
}
}
int row = m_table->rowCount();
m_table->insertRow(row);
QTableWidgetItem* labelItem = new QTableWidgetItem(label);
m_table->setItem(row, Column::Label, labelItem);
QTableWidgetItem* addrItem = new QTableWidgetItem(addressStr);
addrItem->setFont(GUIUtil::fixedPitchFont());
m_table->setItem(row, Column::Address, addrItem);
}
}
m_table->setSortingEnabled(true);
selectionChanged();
}
void DDAddressBookPage::onSearchTextChanged(const QString& /*text*/)
{
refreshAddressList();
}
QString DDAddressBookPage::getSelectedAddress() const
{
int row = m_table->currentRow();
if (row >= 0) {
QTableWidgetItem* item = m_table->item(row, Column::Address);
if (item) return item->text();
}
return QString();
}
void DDAddressBookPage::accept()
{
if (m_mode == ForSelection) {
m_returnValue = getSelectedAddress();
}
QDialog::accept();
}
void DDAddressBookPage::onNewAddress()
{
if (!m_walletModel) return;
bool ok;
QString label = QInputDialog::getText(this, tr("New DigiDollar Address"),
tr("Label:"), QLineEdit::Normal, QString(), &ok);
if (ok) {
QString newAddress = m_walletModel->getNewDigiDollarAddress(label);
if (!newAddress.isEmpty()) {
refreshAddressList();
} else {
QMessageBox::warning(this, tr("Error"),
tr("Failed to create new DigiDollar address."));
}
}
}
void DDAddressBookPage::onEditAddress()
{
if (!m_walletModel) return;
int row = m_table->currentRow();
if (row < 0) return;
QTableWidgetItem* labelItem = m_table->item(row, Column::Label);
QTableWidgetItem* addrItem = m_table->item(row, Column::Address);
if (!labelItem || !addrItem) return;
bool ok;
QString newLabel = QInputDialog::getText(this, tr("Edit Label"),
tr("Label:"), QLineEdit::Normal, labelItem->text(), &ok);
if (ok && newLabel != labelItem->text()) {
CTxDestination dest = DecodeDigiDollarAddress(addrItem->text().toStdString());
if (IsValidDestination(dest)) {
m_walletModel->wallet().setAddressBook(dest, newLabel.toStdString(),
wallet::AddressPurpose::DIGIDOLLAR);
refreshAddressList();
}
}
}
void DDAddressBookPage::onDeleteAddress()
{
if (!m_walletModel) return;
QString address = getSelectedAddress();
if (address.isEmpty()) return;
QMessageBox::StandardButton reply = QMessageBox::question(this, tr("Delete Address"),
tr("Are you sure you want to delete this address from your address book?\n\n%1")
.arg(address), QMessageBox::Yes | QMessageBox::No);
if (reply == QMessageBox::Yes) {
CTxDestination dest = DecodeDigiDollarAddress(address.toStdString());
if (IsValidDestination(dest)) {
m_walletModel->wallet().delAddressBook(dest);
refreshAddressList();
}
}
}
void DDAddressBookPage::onCopyAddress()
{
QString address = getSelectedAddress();
if (!address.isEmpty()) {
QApplication::clipboard()->setText(address);
}
}
void DDAddressBookPage::onExport()
{
QString filename = QFileDialog::getSaveFileName(this,
tr("Export DigiDollar Address Book"),
QString(),
tr("Comma separated file") + QLatin1String(" (*.csv)"));
if (filename.isEmpty()) return;
if (!filename.endsWith(".csv", Qt::CaseInsensitive)) {
filename += ".csv";
}
QFile file(filename);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::critical(this, tr("Export Failed"),
tr("Could not open file %1 for writing.").arg(filename));
return;
}
QTextStream out(&file);
out << "\"Label\",\"Address\"\n";
for (int row = 0; row < m_table->rowCount(); ++row) {
QString label = m_table->item(row, Column::Label)->text();
QString address = m_table->item(row, Column::Address)->text();
label.replace("\"", "\"\"");
out << "\"" << label << "\",\"" << address << "\"\n";
}
file.close();
if (file.error() == QFile::NoError) {
QMessageBox::information(this, tr("Export Successful"),
tr("Address book was successfully saved to %1.").arg(filename));
}
}
void DDAddressBookPage::showContextMenu(const QPoint& pos)
{
if (m_table->currentRow() >= 0) {
m_contextMenu->popup(m_table->viewport()->mapToGlobal(pos));
}
}
void DDAddressBookPage::selectionChanged()
{
bool hasSelection = m_table->currentRow() >= 0;
m_copyButton->setEnabled(hasSelection);
m_deleteButton->setEnabled(hasSelection);
}