feather/src/wizard/PageSetRestoreHeight.cpp

120 lines
4.2 KiB
C++
Raw Normal View History

2021-03-12 18:26:48 +00:00
// SPDX-License-Identifier: BSD-3-Clause
2023-01-02 19:30:11 +00:00
// SPDX-FileCopyrightText: 2020-2023 The Monero Project
2021-03-12 18:26:48 +00:00
#include <QValidator>
#include "PageSetRestoreHeight.h"
#include "ui_PageSetRestoreHeight.h"
#include "WalletWizard.h"
2021-05-18 15:59:18 +00:00
#include "constants.h"
2021-03-12 18:26:48 +00:00
2021-05-18 15:59:18 +00:00
PageSetRestoreHeight::PageSetRestoreHeight(WizardFields *fields, QWidget *parent)
2021-03-12 18:26:48 +00:00
: QWizardPage(parent)
, ui(new Ui::PageSetRestoreHeight)
, m_fields(fields)
{
ui->setupUi(this);
2022-03-04 10:05:20 +00:00
QRegularExpression yearRe(R"(\d{2,4}-\d{1,2}-\d{1,2})");
QValidator *yearValidator = new QRegularExpressionValidator(yearRe, this);
2021-03-12 18:26:48 +00:00
ui->line_creationDate->setValidator(yearValidator);
2022-03-04 10:05:20 +00:00
QRegularExpression heightRe(R"(\d{7})");
QValidator *heightValidator = new QRegularExpressionValidator(heightRe, this);
2021-03-12 18:26:48 +00:00
ui->line_restoreHeight->setValidator(heightValidator);
QPixmap pixmap = QPixmap(":/assets/images/unpaid.png");
ui->icon->setPixmap(pixmap.scaledToWidth(32, Qt::SmoothTransformation));
2021-05-02 18:22:38 +00:00
QPixmap pixmap2 = QPixmap(":/assets/images/info2.svg");
2021-03-12 18:26:48 +00:00
ui->warningIcon->setPixmap(pixmap2.scaledToWidth(32, Qt::SmoothTransformation));
ui->infoIcon->setPixmap(pixmap2.scaledToWidth(32, Qt::SmoothTransformation));
connect(ui->line_creationDate, &QLineEdit::textEdited, [this]{
this->onCreationDateEdited();
this->completeChanged();
});
connect(ui->line_restoreHeight, &QLineEdit::textEdited, [this]{
this->onRestoreHeightEdited();
this->completeChanged();
});
}
void PageSetRestoreHeight::initializePage() {
this->setTitle("Restore height");
ui->line_creationDate->setText("");
ui->line_restoreHeight->setText("");
2021-07-02 22:22:20 +00:00
ui->frame_scanWarning->hide();
ui->frame_walletAgeWarning->hide();
if (m_fields->showSetRestoreHeightPage && m_fields->mode == WizardMode::RestoreFromSeed) {
auto creationDate = QDateTime::fromSecsSinceEpoch(m_fields->seed.time);
ui->line_creationDate->setText(creationDate.toString("yyyy-MM-dd"));
this->onCreationDateEdited();
}
2021-03-12 18:26:48 +00:00
}
void PageSetRestoreHeight::onCreationDateEdited() {
auto curDate = QDateTime::currentDateTime().addDays(-7);
auto date = QDateTime::fromString(ui->line_creationDate->text(), "yyyy-MM-dd");
if (!date.isValid()) {
ui->frame_walletAgeWarning->hide();
ui->frame_scanWarning->hide();
ui->line_restoreHeight->setText("");
return;
}
QDateTime restoreDate = date > curDate ? curDate : date;
int timestamp = restoreDate.toSecsSinceEpoch();
2021-05-22 19:32:48 +00:00
QString restoreHeight = QString::number(appData()->restoreHeights[constants::networkType]->dateToHeight(timestamp));
2021-03-12 18:26:48 +00:00
ui->line_restoreHeight->setText(restoreHeight);
this->showScanWarning(restoreDate);
this->showWalletAgeWarning(restoreDate);
}
void PageSetRestoreHeight::onRestoreHeightEdited() {
int restoreHeight = ui->line_restoreHeight->text().toInt();
if (restoreHeight == 0) {
2021-07-02 22:22:20 +00:00
restoreHeight = 1;
2021-03-12 18:26:48 +00:00
}
2021-05-22 19:32:48 +00:00
QDateTime date = appData()->restoreHeights[constants::networkType]->heightToDate(restoreHeight);
2021-03-12 18:26:48 +00:00
ui->line_creationDate->setText(date.toString("yyyy-MM-dd"));
this->showScanWarning(date);
this->showWalletAgeWarning(date);
}
void PageSetRestoreHeight::showScanWarning(const QDateTime &date) {
2021-05-20 21:41:12 +00:00
QString dateString = date.toString("MMMM dd, yyyy");
2021-03-12 18:26:48 +00:00
ui->label_scanWarning->setText(QString("Wallet will not scan for transactions before %1").arg(dateString));
ui->frame_scanWarning->show();
}
void PageSetRestoreHeight::showWalletAgeWarning(const QDateTime &date) {
QDateTime yearAgo = QDateTime::currentDateTime().addYears(-1);
ui->frame_walletAgeWarning->setVisible(date < yearAgo);
}
bool PageSetRestoreHeight::validatePage() {
2021-07-02 22:22:20 +00:00
m_fields->restoreHeight = std::max(1, ui->line_restoreHeight->text().toInt());
2021-03-12 18:26:48 +00:00
return true;
}
int PageSetRestoreHeight::nextId() const {
if (m_fields->showSetSeedPassphrasePage) {
return WalletWizard::Page_SetSeedPassphrase;
}
if (m_fields->showSetSubaddressLookaheadPage) {
return WalletWizard::Page_SetSubaddressLookahead;
}
2021-03-12 18:26:48 +00:00
return WalletWizard::Page_WalletFile;
}
bool PageSetRestoreHeight::isComplete() const {
return !ui->line_restoreHeight->text().isEmpty();
}