diff --git a/src/dialog/RestoreHeightDialog.cpp b/src/dialog/RestoreHeightDialog.cpp new file mode 100644 index 00000000..ca7cfda8 --- /dev/null +++ b/src/dialog/RestoreHeightDialog.cpp @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: BSD-3-Clause +// Copyright (c) 2020-2021, The Monero Project. + +#include "RestoreHeightDialog.h" + +#include +#include +#include + +RestoreHeightDialog::RestoreHeightDialog(QWidget *parent, quint64 currentRestoreHeight) + : QDialog(parent) + , m_restoreHeightWidget(new RestoreHeightWidget(this)) +{ + auto *layout = new QVBoxLayout(this); + + auto *label = new QLabel(this); + label->setText("Enter a wallet creation date or restore height."); + + auto *buttonBox = new QDialogButtonBox(this); + buttonBox->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Ok); + + layout->addWidget(label); + layout->addWidget(m_restoreHeightWidget); + layout->addWidget(buttonBox); + + this->setLayout(layout); + + if (currentRestoreHeight) { + m_restoreHeightWidget->setHeight(currentRestoreHeight); + } + + connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); + connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); + + this->adjustSize(); +} + +int RestoreHeightDialog::getHeight() { + return m_restoreHeightWidget->getHeight(); +} \ No newline at end of file diff --git a/src/dialog/RestoreHeightDialog.h b/src/dialog/RestoreHeightDialog.h new file mode 100644 index 00000000..16159c9d --- /dev/null +++ b/src/dialog/RestoreHeightDialog.h @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: BSD-3-Clause +// Copyright (c) 2020-2021, The Monero Project. + +#ifndef FEATHER_RESTOREHEIGHTDIALOG_H +#define FEATHER_RESTOREHEIGHTDIALOG_H + +#include +#include "widgets/RestoreHeightWidget.h" + +class RestoreHeightDialog : public QDialog +{ +Q_OBJECT + +public: + explicit RestoreHeightDialog(QWidget *parent = nullptr, quint64 currentRestoreHeight = 0); + int getHeight(); + +private: + RestoreHeightWidget *m_restoreHeightWidget; +}; + + +#endif //FEATHER_RESTOREHEIGHTDIALOG_H diff --git a/src/dialog/restoredialog.cpp b/src/dialog/restoredialog.cpp deleted file mode 100644 index fa9679a4..00000000 --- a/src/dialog/restoredialog.cpp +++ /dev/null @@ -1,38 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// Copyright (c) 2020-2021, The Monero Project. - -#include "restoredialog.h" -#include "ui_restoredialog.h" - -#include "constants.h" - -RestoreDialog::RestoreDialog(QSharedPointer ctx, QWidget *parent) - : QDialog(parent) - , ui(new Ui::RestoreDialog) - , m_ctx(std::move(ctx)) -{ - ui->setupUi(this); - this->setWindowIcon(QIcon("://assets/images/appicons/64x64.png")); - connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &RestoreDialog::accepted); - connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &RestoreDialog::rejected); - - if(constants::networkType == NetworkType::Type::TESTNET) { - ui->restoreHeightWidget->hideSlider(); - } else { - // load restoreHeight lookup db - ui->restoreHeightWidget->initRestoreHeights(appData()->restoreHeights[constants::networkType]); - } -} - -int RestoreDialog::getHeight() { - return ui->restoreHeightWidget->getHeight(); -} - -void RestoreDialog::initRestoreHeights(RestoreHeightLookup *lookup) { - ui->restoreHeightWidget->initRestoreHeights(lookup); -} - -RestoreDialog::~RestoreDialog() { - delete ui; -} - diff --git a/src/dialog/restoredialog.h b/src/dialog/restoredialog.h deleted file mode 100644 index e6118c81..00000000 --- a/src/dialog/restoredialog.h +++ /dev/null @@ -1,39 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// Copyright (c) 2020-2021, The Monero Project. - -#ifndef RESTOREDIALOG_H -#define RESTOREDIALOG_H - -#include -#include -#include -#include -#include - -#include "utils/RestoreHeightLookup.h" -#include "appcontext.h" - -namespace Ui { - class RestoreDialog; -} - -class RestoreDialog : public QDialog -{ -Q_OBJECT - -public: - explicit RestoreDialog(QSharedPointer ctx, QWidget *parent = nullptr); - void initRestoreHeights(RestoreHeightLookup *lookup); - int getHeight(); - ~RestoreDialog() override; - -signals: - void accepted(); - void rejected(); - -private: - Ui::RestoreDialog *ui; - QSharedPointer m_ctx; -}; - -#endif // RESTOREDIALOG_H diff --git a/src/dialog/restoredialog.ui b/src/dialog/restoredialog.ui deleted file mode 100644 index 5c914bb6..00000000 --- a/src/dialog/restoredialog.ui +++ /dev/null @@ -1,56 +0,0 @@ - - - RestoreDialog - - - - 0 - 0 - 584 - 99 - - - - specify restore height - - - - - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - - - - - - - RestoreHeightWidget - QWidget -
widgets/restoreheightwidget.h
- 1 -
-
- - -
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 4de7ad6a..baedfd42 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -735,26 +735,17 @@ void MainWindow::updatePasswordIcon() { void MainWindow::showRestoreHeightDialog() { // settings custom restore height is only available for 25 word seeds auto seed = m_ctx->wallet->getCacheAttribute("feather.seed"); - if(!seed.isEmpty()) { + if(!seed.isEmpty()) { // TODO: update this warning (import tx, delete cache, restore from seed) const auto msg = "This wallet has a 14 word mnemonic seed which has the restore height embedded."; QMessageBox::warning(this, "Cannot set custom restore height", msg); return; } - m_restoreDialog = new RestoreDialog(m_ctx, this); - m_restoreDialog->show(); - - connect(m_restoreDialog, &RestoreDialog::accepted, [=]{ - auto height = m_restoreDialog->getHeight(); - m_restoreDialog->disconnect(); - m_restoreDialog->deleteLater(); - m_ctx->onSetRestoreHeight(height); - }); - - connect(m_restoreDialog, &RestoreDialog::rejected, [=]{ - m_restoreDialog->disconnect(); - m_restoreDialog->deleteLater(); - }); + RestoreHeightDialog dialog{this, m_ctx->wallet->getWalletCreationHeight()}; + if (dialog.exec() == QDialog::Accepted) { + int restoreHeight = dialog.getHeight(); + m_ctx->onSetRestoreHeight(restoreHeight); + } } void MainWindow::showKeysDialog() { diff --git a/src/mainwindow.h b/src/mainwindow.h index 3dfb3f62..56f2dc09 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -21,7 +21,7 @@ #include "dialog/passwordchangedialog.h" #include "dialog/keysdialog.h" #include "dialog/aboutdialog.h" -#include "dialog/restoredialog.h" +#include "dialog/RestoreHeightDialog.h" #include "dialog/splashdialog.h" #include "libwalletqt/Wallet.h" #include "model/SubaddressModel.h" @@ -212,7 +212,6 @@ private: Settings *m_windowSettings = nullptr; CalcWindow *m_windowCalc = nullptr; - RestoreDialog *m_restoreDialog = nullptr; SplashDialog *m_splashDialog = nullptr; XMRigWidget *m_xmrig = nullptr; diff --git a/src/widgets/RestoreHeightWidget.cpp b/src/widgets/RestoreHeightWidget.cpp new file mode 100644 index 00000000..b3250f17 --- /dev/null +++ b/src/widgets/RestoreHeightWidget.cpp @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: BSD-3-Clause +// Copyright (c) 2020-2021, The Monero Project. + +#include "RestoreHeightWidget.h" +#include "ui_RestoreHeightWidget.h" + +#include + +#include "AppData.h" +#include "constants.h" + +RestoreHeightWidget::RestoreHeightWidget(QWidget *parent) + : QWidget(parent) + , ui(new Ui::RestoreHeightWidget) +{ + ui->setupUi(this); + + QRegExp yearRe(R"(\d{2,4}-\d{1,2}-\d{1,2})"); + QValidator *yearValidator = new QRegExpValidator(yearRe, this); + ui->line_creationDate->setValidator(yearValidator); + + QRegExp heightRe(R"(\d{7})"); + QValidator *heightValidator = new QRegExpValidator(heightRe, this); + ui->line_restoreHeight->setValidator(heightValidator); + + connect(ui->line_creationDate, &QLineEdit::textEdited, this, &RestoreHeightWidget::onCreationDateChanged); + connect(ui->line_restoreHeight, &QLineEdit::textEdited, this, &RestoreHeightWidget::onRestoreHeightChanged); +} + +void RestoreHeightWidget::setHeight(quint64 restoreHeight) { + ui->line_restoreHeight->setText(QString::number(restoreHeight)); + this->onRestoreHeightChanged(); +} + +int RestoreHeightWidget::getHeight() { + return ui->line_restoreHeight->text().toInt(); +} + +void RestoreHeightWidget::onCreationDateChanged() { + auto curDate = QDateTime::currentDateTime().addDays(-7); + auto date = QDateTime::fromString(ui->line_creationDate->text(), "yyyy-MM-dd"); + if (!date.isValid()) { + return; + } + + QDateTime restoreDate = date > curDate ? curDate : date; + qint64 timestamp = restoreDate.toSecsSinceEpoch(); + + QString restoreHeight = QString::number(appData()->restoreHeights[constants::networkType]->dateToHeight(timestamp)); + ui->line_restoreHeight->setText(restoreHeight); +} + +void RestoreHeightWidget::onRestoreHeightChanged() { + int restoreHeight = ui->line_restoreHeight->text().toInt(); + QDateTime date = appData()->restoreHeights[constants::networkType]->heightToDate(restoreHeight); + ui->line_creationDate->setText(date.toString("yyyy-MM-dd")); +} + +RestoreHeightWidget::~RestoreHeightWidget() { + delete ui; +} \ No newline at end of file diff --git a/src/widgets/restoreheightwidget.h b/src/widgets/RestoreHeightWidget.h similarity index 53% rename from src/widgets/restoreheightwidget.h rename to src/widgets/RestoreHeightWidget.h index a1418c35..a7f466bd 100644 --- a/src/widgets/restoreheightwidget.h +++ b/src/widgets/RestoreHeightWidget.h @@ -1,13 +1,10 @@ // SPDX-License-Identifier: BSD-3-Clause // Copyright (c) 2020-2021, The Monero Project. -#ifndef RESTOREHEIGHTWIDGET_H -#define RESTOREHEIGHTWIDGET_H +#ifndef FEATHER_RESTOREHEIGHTWIDGET_H +#define FEATHER_RESTOREHEIGHTWIDGET_H #include -#include - -#include "appcontext.h" namespace Ui { class RestoreHeightWidget; @@ -19,17 +16,18 @@ class RestoreHeightWidget : public QWidget public: explicit RestoreHeightWidget(QWidget *parent = nullptr); - void initRestoreHeights(RestoreHeightLookup *lookup); - void setHeight(int height); - int getHeight(); - void hideSlider(); ~RestoreHeightWidget() override; -private: - void updateTimestamp(int date); + void setHeight(quint64 restoreHeight); + int getHeight(); - RestoreHeightLookup *m_restoreHeightLookup = nullptr; +private slots: + void onCreationDateChanged(); + void onRestoreHeightChanged(); + +private: Ui::RestoreHeightWidget *ui; }; -#endif // RESTOREHEIGHTWIDGET_H + +#endif //FEATHER_RESTOREHEIGHTWIDGET_H diff --git a/src/widgets/RestoreHeightWidget.ui b/src/widgets/RestoreHeightWidget.ui new file mode 100644 index 00000000..491c9fdf --- /dev/null +++ b/src/widgets/RestoreHeightWidget.ui @@ -0,0 +1,61 @@ + + + RestoreHeightWidget + + + + 0 + 0 + 622 + 88 + + + + Form + + + + + + Wallet creation date: + + + + + + + + + + (YYYY-MM-DD) + + + + + + + Restore height: + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + diff --git a/src/widgets/restoreheightwidget.cpp b/src/widgets/restoreheightwidget.cpp deleted file mode 100644 index f8b6a1d0..00000000 --- a/src/widgets/restoreheightwidget.cpp +++ /dev/null @@ -1,71 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// Copyright (c) 2020-2021, The Monero Project. - -#include -#include -#include - -#include "restoreheightwidget.h" -#include "ui_restoreheightwidget.h" - -RestoreHeightWidget::RestoreHeightWidget(QWidget *parent) - : QWidget(parent) - , ui(new Ui::RestoreHeightWidget) -{ - ui->setupUi(this); - ui->lineEdit_restoreHeight->setValidator(new QIntValidator(0, 2147483647, this)); - - connect(ui->lineEdit_restoreHeight, &QLineEdit::textEdited, [this](const QString &val){ - if (val.isEmpty()) return; - this->setHeight(val.toInt()); - }); -} - -void RestoreHeightWidget::hideSlider(){ - ui->restoreGrid->hide(); -} - -void RestoreHeightWidget::setHeight(int height) { - if (height < 0) - height = 0; - - // Update lineEdit - ui->lineEdit_restoreHeight->setText(QString::number(height)); - - // Update slider - int date = m_restoreHeightLookup->heightToTimestamp(height); - ui->restoreSlider->setValue(date); - - this->updateTimestamp(date); -} - -void RestoreHeightWidget::initRestoreHeights(RestoreHeightLookup *lookup) { - // init slider - m_restoreHeightLookup = lookup; - int now = std::time(nullptr); - QList blockDates = m_restoreHeightLookup->data.keys(); - ui->restoreSlider->setMinimum(blockDates[0]); - ui->restoreSlider->setMaximum(now); - - connect(ui->restoreSlider, &QSlider::sliderMoved, [this](int date){ - // Update lineEdit - int blockHeight = m_restoreHeightLookup->dateToHeight(date); - ui->lineEdit_restoreHeight->setText(QString::number(blockHeight)); - - this->updateTimestamp(date); - }); -} - -void RestoreHeightWidget::updateTimestamp(int date) { - QDateTime timestamp; - timestamp.setTime_t(date); - ui->label_restoreHeightDate->setText(timestamp.toString("yyyy-MM-dd")); -} - -int RestoreHeightWidget::getHeight() { - return ui->lineEdit_restoreHeight->text().toInt(); -} - -RestoreHeightWidget::~RestoreHeightWidget() { - delete ui; -} diff --git a/src/widgets/restoreheightwidget.ui b/src/widgets/restoreheightwidget.ui deleted file mode 100644 index 00cb7d37..00000000 --- a/src/widgets/restoreheightwidget.ui +++ /dev/null @@ -1,112 +0,0 @@ - - - RestoreHeightWidget - - - - 0 - 0 - 360 - 89 - - - - Form - - - - 0 - - - 0 - - - 0 - - - 0 - - - - - QFrame::NoFrame - - - QFrame::Plain - - - 0 - - - - 0 - - - 0 - - - 0 - - - 0 - - - - - 2014-04-18 - - - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - - - - - - Restore height - - - - - - - Qt::Horizontal - - - - - - - - - - - - - - 1 - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - - - - -