feather/src/wizard/PageWalletFile.cpp

135 lines
4.1 KiB
C++
Raw Normal View History

2021-03-12 18:26:48 +00:00
// SPDX-License-Identifier: BSD-3-Clause
2024-01-01 17:07:58 +00:00
// SPDX-FileCopyrightText: 2020-2024 The Monero Project
2021-03-12 18:26:48 +00:00
#include "WalletWizard.h"
#include "PageWalletFile.h"
#include "ui_PageWalletFile.h"
#include <QFileDialog>
2023-09-15 12:29:58 +00:00
#include "utils/Icons.h"
#include "utils/Utils.h"
2021-05-18 15:59:18 +00:00
PageWalletFile::PageWalletFile(WizardFields *fields, QWidget *parent)
2021-03-12 18:26:48 +00:00
: QWizardPage(parent)
, ui(new Ui::PageWalletFile)
, m_fields(fields)
{
ui->setupUi(this);
this->setButtonText(QWizard::FinishButton, "Open wallet");
2023-09-15 12:29:58 +00:00
ui->frame_wallet->setInfo(icons()->icon("file"), "Choose a name and directory for your wallet files.");
2021-03-12 18:26:48 +00:00
connect(ui->btnChange, &QPushButton::clicked, [=] {
QString currentWalletDir = conf()->get(Config::walletDirectory).toString();
2021-05-18 15:59:18 +00:00
QString walletDir = QFileDialog::getExistingDirectory(this, "Select wallet directory ", currentWalletDir, QFileDialog::ShowDirsOnly);
if (walletDir.isEmpty()) {
return;
}
2021-03-12 18:26:48 +00:00
ui->line_walletDir->setText(walletDir);
});
connect(ui->line_walletName, &QLineEdit::textChanged, this, &PageWalletFile::validateWidgets);
connect(ui->line_walletDir, &QLineEdit::textChanged, this, [this](){
ui->check_defaultWalletDirectory->setVisible(true);
this->validateWidgets();
});
2021-03-12 18:26:48 +00:00
}
void PageWalletFile::initializePage() {
this->setTitle(m_fields->modeText);
ui->line_walletDir->setText(conf()->get(Config::walletDirectory).toString());
2021-03-12 18:26:48 +00:00
ui->line_walletName->setText(this->defaultWalletName());
ui->check_defaultWalletDirectory->setVisible(false);
ui->check_defaultWalletDirectory->setChecked(false);
if (!m_fields->walletName.isEmpty()) {
ui->line_walletName->setText(m_fields->walletName);
}
2021-03-12 18:26:48 +00:00
}
bool PageWalletFile::validateWidgets(){
QString walletName = ui->line_walletName->text();
QString walletDir = ui->line_walletDir->text();
m_validated = true;
ui->line_walletName->setStyleSheet("");
ui->line_walletDir->setStyleSheet("");
QString errStyle = "QLineEdit{border: 1px solid red;}";
if (walletDir.isEmpty()) {
ui->line_walletDir->setStyleSheet(errStyle);
m_validated = false;
}
if (!Utils::dirExists(walletDir)) {
ui->line_walletDir->setStyleSheet(errStyle);
m_validated = false;
}
if (walletName.isEmpty()) {
ui->line_walletName->setStyleSheet(errStyle);
m_validated = false;
}
if (this->walletPathExists(walletName)) {
ui->line_walletName->setStyleSheet(errStyle);
m_validated = false;
}
this->completeChanged();
return m_validated;
}
int PageWalletFile::nextId() const {
return WalletWizard::Page_SetPasswordPage;
}
bool PageWalletFile::validatePage() {
if (!this->validateWidgets()) {
return false;
}
m_fields->walletName = ui->line_walletName->text();
m_fields->walletDir = ui->line_walletDir->text();
QString walletDir = ui->line_walletDir->text();
bool dirChanged = conf()->get(Config::walletDirectory).toString() != walletDir;
if (dirChanged && ui->check_defaultWalletDirectory->isChecked()) {
conf()->set(Config::walletDirectory, walletDir);
}
2021-03-12 18:26:48 +00:00
return true;
}
bool PageWalletFile::isComplete() const {
return m_validated;
}
QString PageWalletFile::defaultWalletName() {
int count = 1;
QString walletName;
do {
2021-05-04 23:09:19 +00:00
QString walletStr = QString("wallet_%1");
if (m_fields->mode == WizardMode::CreateWalletFromDevice) {
2021-07-01 21:00:47 +00:00
switch (m_fields->deviceType) {
2022-05-23 08:56:11 +00:00
case DeviceType::LEDGER:
2021-07-01 21:00:47 +00:00
walletStr = QString("ledger_%1");
break;
2022-05-23 08:56:11 +00:00
case DeviceType::TREZOR:
2021-07-01 21:00:47 +00:00
walletStr = QString("trezor_%1");
}
2021-05-04 23:09:19 +00:00
}
walletName = walletStr.arg(count);
2021-03-12 18:26:48 +00:00
count++;
} while (this->walletPathExists(walletName));
return walletName;
}
bool PageWalletFile::walletPathExists(const QString &walletName) {
QDir walletDir = QDir(ui->line_walletDir->text());
return QFile::exists(walletDir.filePath(walletName)) ||
QFile::exists(walletDir.filePath(QString("%1.keys").arg(walletName)));
}