2020-10-07 10:36:04 +00:00
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
// Copyright (c) 2020, The Monero Project.
|
|
|
|
|
|
|
|
#include "wizard/openwallet.h"
|
|
|
|
#include "ui_openwallet.h"
|
|
|
|
#include "appcontext.h"
|
|
|
|
#include "utils/config.h"
|
|
|
|
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QFileDialog>
|
2020-12-14 02:20:05 +00:00
|
|
|
#include <QMessageBox>
|
2020-10-07 10:36:04 +00:00
|
|
|
|
2020-12-23 02:57:01 +00:00
|
|
|
OpenWalletPage::OpenWalletPage(AppContext *ctx, WalletKeysFilesModel *wallets, QWidget *parent)
|
|
|
|
: QWizardPage(parent)
|
|
|
|
, ui(new Ui::OpenWalletPage)
|
|
|
|
, m_ctx(ctx)
|
|
|
|
, m_walletKeysFilesModel(wallets)
|
|
|
|
{
|
2020-10-07 10:36:04 +00:00
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
connect(ui->btnBrowse, &QPushButton::clicked, [=]{
|
|
|
|
// manually browsing for wallet
|
|
|
|
auto walletPath = config()->get(Config::walletPath).toString();
|
|
|
|
if(walletPath.isEmpty())
|
|
|
|
walletPath = m_ctx->defaultWalletDir;
|
|
|
|
QString path = QFileDialog::getOpenFileName(this, "Select your wallet file", walletPath, "Wallet file (*.keys)");
|
|
|
|
if(path.isEmpty()) return;
|
|
|
|
|
|
|
|
QFileInfo infoPath(path);
|
|
|
|
if(!infoPath.isReadable()) {
|
2020-12-14 02:20:05 +00:00
|
|
|
QMessageBox::warning(this, "Cannot read wallet file", "Permission error.");
|
2020-10-07 10:36:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setField("walletPath", path);
|
|
|
|
|
|
|
|
if(ui->openOnStartup->isChecked())
|
|
|
|
config()->set(Config::autoOpenWalletPath, QString("%1%2").arg(m_ctx->networkType).arg(path));
|
|
|
|
|
|
|
|
emit openWallet(path);
|
|
|
|
});
|
|
|
|
|
|
|
|
this->setTitle("Open wallet file");
|
|
|
|
this->setButtonText(QWizard::FinishButton, "Open wallet");
|
|
|
|
|
|
|
|
ui->walletTable->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
|
|
ui->walletTable->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
|
2020-12-18 22:39:27 +00:00
|
|
|
m_keysProxy = new WalletKeysFilesProxyModel(this, m_ctx->networkType);
|
2020-12-23 02:57:01 +00:00
|
|
|
m_keysProxy->setSourceModel(m_walletKeysFilesModel);
|
2020-10-07 10:36:04 +00:00
|
|
|
m_keysProxy->setSortRole(Qt::UserRole);
|
|
|
|
|
|
|
|
ui->walletTable->setModel(m_keysProxy);
|
2020-12-18 22:39:27 +00:00
|
|
|
ui->walletTable->hideColumn(WalletKeysFilesModel::NetworkType);
|
2020-12-18 23:13:33 +00:00
|
|
|
ui->walletTable->hideColumn(WalletKeysFilesModel::Path);
|
|
|
|
ui->walletTable->hideColumn(WalletKeysFilesModel::Modified);
|
|
|
|
ui->walletTable->header()->setSectionResizeMode(WalletKeysFilesModel::FileName, QHeaderView::Stretch);
|
2020-10-07 10:36:04 +00:00
|
|
|
ui->walletTable->setSortingEnabled(true);
|
2020-12-18 23:13:33 +00:00
|
|
|
ui->walletTable->sortByColumn(WalletKeysFilesModel::Modified, Qt::DescendingOrder);
|
2020-10-07 10:36:04 +00:00
|
|
|
ui->walletTable->show();
|
2020-12-18 23:13:33 +00:00
|
|
|
|
|
|
|
connect(ui->walletTable->selectionModel(), &QItemSelectionModel::currentRowChanged, [this](QModelIndex current, QModelIndex prev){
|
|
|
|
this->updatePath();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-23 02:57:01 +00:00
|
|
|
void OpenWalletPage::initializePage() {
|
|
|
|
m_walletKeysFilesModel->refresh();
|
|
|
|
}
|
|
|
|
|
2020-12-18 23:13:33 +00:00
|
|
|
void OpenWalletPage::updatePath() {
|
|
|
|
QModelIndex index = ui->walletTable->currentIndex();
|
|
|
|
if (!index.isValid()) {
|
|
|
|
ui->labelPath->clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString path = index.model()->data(index.siblingAtColumn(WalletKeysFilesModel::Path), Qt::DisplayRole).toString();
|
|
|
|
ui->labelPath->setText(path);
|
2020-10-07 10:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int OpenWalletPage::nextId() const {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OpenWalletPage::validatePage() {
|
|
|
|
QModelIndex index = ui->walletTable->currentIndex();
|
2020-10-23 17:57:06 +00:00
|
|
|
if(!index.isValid()) {
|
2020-12-14 02:20:05 +00:00
|
|
|
QMessageBox::warning(this, "Wallet not selected", "Please select a wallet from the list.");
|
2020-10-23 17:57:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-10-07 10:36:04 +00:00
|
|
|
QString walletPath = index.model()->data(index.siblingAtColumn(WalletKeysFilesModel::ModelColumns::Path), Qt::UserRole).toString();
|
|
|
|
|
|
|
|
auto autoWallet = ui->openOnStartup->isChecked() ? QString("%1%2").arg(m_ctx->networkType).arg(walletPath) : "";
|
|
|
|
config()->set(Config::autoOpenWalletPath, autoWallet);
|
|
|
|
|
|
|
|
emit openWallet(walletPath);
|
|
|
|
return true;
|
|
|
|
}
|