Clean up WalletKeysFilesModel

This commit is contained in:
tobtoht 2021-05-20 21:39:53 +02:00
parent b0834f8729
commit 3d646fb345
6 changed files with 81 additions and 78 deletions

View file

@ -1,49 +1,41 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2014-2021, The Monero Project.
#include "keysfiles.h"
#include "WalletKeysFilesModel.h"
#include "utils/utils.h"
#include <QDir>
#include <QDateTime>
#include "config.h"
using namespace std::chrono;
WalletKeysFiles::WalletKeysFiles(const QFileInfo &info, int networkType, QString address) :
m_fileName(info.fileName()),
m_modified(info.lastModified().toSecsSinceEpoch()),
m_path(QDir::toNativeSeparators(info.absoluteFilePath())),
m_networkType(networkType),
m_address(std::move(address))
WalletKeysFile::WalletKeysFile(const QFileInfo &info, int networkType, QString address)
: m_fileName(info.fileName())
, m_modified(getModified(info))
, m_path(QDir::toNativeSeparators(info.absoluteFilePath()))
, m_networkType(networkType)
, m_address(std::move(address))
{
}
qint64 WalletKeysFile::getModified(const QFileInfo &info) {
qint64 m = info.lastModified().toSecsSinceEpoch();
QFileInfo cacheFile = QFileInfo(info.absoluteFilePath().replace(QRegExp(".keys$"), ""));
qint64 cacheLastModified = cacheFile.lastModified().toSecsSinceEpoch();
if (cacheFile.exists()) {
m_modified = (cacheLastModified > m_modified) ? cacheLastModified : m_modified;
if (cacheFile.exists() && cacheLastModified > m) {
m = cacheLastModified;
}
return m;
}
QString WalletKeysFiles::fileName() const {
return m_fileName;
}
qint64 WalletKeysFiles::modified() const {
return m_modified;
}
QString WalletKeysFiles::address() const {
return m_address;
}
QString WalletKeysFiles::path() const {
return m_path;
}
int WalletKeysFiles::networkType() const {
return m_networkType;
}
// Model
WalletKeysFilesModel::WalletKeysFilesModel(QObject *parent)
: QAbstractTableModel(parent)
{
this->updateDirectories();
this->m_walletKeysFilesItemModel = qobject_cast<QAbstractItemModel *>(this);
}
void WalletKeysFilesModel::clear() {
@ -58,17 +50,23 @@ void WalletKeysFilesModel::refresh() {
endResetModel();
}
void WalletKeysFilesModel::updateDirectories() { // TODO
this->walletDirectories.clear();
void WalletKeysFilesModel::updateDirectories() {
m_walletDirectories.clear();
QDir defaultWalletDir = QDir(Utils::defaultWalletDir());
QString walletDir = defaultWalletDir.path();
defaultWalletDir.cdUp();
QString walletDirRoot = defaultWalletDir.path();
this->walletDirectories << walletDir;
this->walletDirectories << walletDirRoot;
this->walletDirectories << QDir::homePath();
this->walletDirectories.removeDuplicates();
m_walletDirectories << walletDir;
m_walletDirectories << walletDirRoot;
m_walletDirectories << QDir::homePath();
QString walletDirectory = config()->get(Config::walletDirectory).toString();
if (!walletDirectory.isEmpty())
m_walletDirectories << walletDirectory;
m_walletDirectories.removeDuplicates();
}
void WalletKeysFilesModel::findWallets() {
@ -79,9 +77,9 @@ void WalletKeysFilesModel::findWallets() {
rx.setPatternSyntax(QRegExp::Wildcard);
QStringList walletPaths;
for(auto i = 0; i != this->walletDirectories.length(); i++) {
for(auto i = 0; i != m_walletDirectories.length(); i++) {
// Scan default wallet dir (~/Monero/)
walletPaths << Utils::fileFind(rx, this->walletDirectories[i], 0, i == 0 ? 2 : 0, 200);
walletPaths << Utils::fileFind(rx, m_walletDirectories[i], 0, i == 0 ? 2 : 0, 200);
}
walletPaths.removeDuplicates();
@ -113,16 +111,16 @@ void WalletKeysFilesModel::findWallets() {
file.close();
}
this->addWalletKeysFile(WalletKeysFiles(fileInfo, networkType, std::move(addr)));
this->addWalletKeysFile(WalletKeysFile(fileInfo, networkType, std::move(addr)));
}
auto duration = duration_cast<milliseconds>(high_resolution_clock::now() - now).count();
qDebug() << QString("wallet .keys search completed in %1 ms").arg(duration);
}
void WalletKeysFilesModel::addWalletKeysFile(const WalletKeysFiles &walletKeysFile) {
void WalletKeysFilesModel::addWalletKeysFile(const WalletKeysFile &walletKeysFile) {
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_walletKeyFiles << walletKeysFile;
m_walletKeyFiles.append(walletKeysFile);
endInsertRows();
}
@ -131,7 +129,7 @@ int WalletKeysFilesModel::rowCount(const QModelIndex & parent) const {
}
int WalletKeysFilesModel::columnCount(const QModelIndex &) const {
return 4;
return Column::COUNT;
}
QVariant WalletKeysFilesModel::data(const QModelIndex &index, int role) const {
@ -142,7 +140,7 @@ QVariant WalletKeysFilesModel::data(const QModelIndex &index, int role) const {
if (role == Qt::DisplayRole) {
switch(index.column()) {
case ModelColumns::NetworkType: {
case Column::NetworkType: {
auto c = static_cast<NetworkType::Type>(walletKeyFile.networkType());
if (c == NetworkType::Type::STAGENET)
return QString("stage");
@ -150,9 +148,10 @@ QVariant WalletKeysFilesModel::data(const QModelIndex &index, int role) const {
return QString("test");
return QString("main");
}
case ModelColumns::FileName:
case Column::FileName: {
return walletKeyFile.fileName().replace(".keys", "");
case ModelColumns::Path: {
}
case Column::Path: {
auto fp = walletKeyFile.path();
#if defined(Q_OS_MAC) || defined(Q_OS_LINUX)
if (fp.startsWith(QDir::homePath())) {
@ -161,20 +160,20 @@ QVariant WalletKeysFilesModel::data(const QModelIndex &index, int role) const {
#endif
return fp;
}
case ModelColumns::Modified:
case Column::Modified:
return walletKeyFile.modified();
default:
break;
}
} else if(role == Qt::UserRole) {
switch(index.column()) {
case ModelColumns::NetworkType:
case Column::NetworkType:
return static_cast<int>(walletKeyFile.networkType());
case ModelColumns::FileName:
case Column::FileName:
return walletKeyFile.fileName();
case ModelColumns::Modified:
case Column::Modified:
return (int)walletKeyFile.modified();
case ModelColumns::Path:
case Column::Path:
return walletKeyFile.path();
default:
break;
@ -202,6 +201,8 @@ QVariant WalletKeysFilesModel::headerData(int section, Qt::Orientation orientati
return QVariant();
}
// ProxyModel
WalletKeysFilesProxyModel::WalletKeysFilesProxyModel(QObject *parent, NetworkType::Type nettype)
: QSortFilterProxyModel(parent)
, m_nettype(nettype)

View file

@ -1,25 +1,30 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2014-2021, The Monero Project.
#ifndef KEYSFILES_H
#define KEYSFILES_H
#ifndef FEATHER_WALLETKEYSFILESMODEL_H
#define FEATHER_WALLETKEYSFILESMODEL_H
#include <QObject>
#include <QFileInfo>
#include <QAbstractTableModel>
#include <QSortFilterProxyModel>
#include "libwalletqt/WalletManager.h"
#include "utils/networktype.h"
#include "utils/utils.h"
class WalletKeysFiles
class WalletKeysFile
{
public:
WalletKeysFiles(const QFileInfo &info, int networkType, QString address);
WalletKeysFile(const QFileInfo &info, int networkType, QString address);
QString fileName() const;
qint64 modified() const;
QString path() const;
int networkType() const;
QString address() const;
QString fileName() const {return m_fileName;};
qint64 modified() const {return m_modified;};
QString path() const {return m_path;};
int networkType() const {return m_networkType;};
QString address() const {return m_address;};
private:
static qint64 getModified(const QFileInfo &info);
QString m_fileName;
qint64 m_modified;
QString m_path;
@ -30,12 +35,14 @@ private:
class WalletKeysFilesModel : public QAbstractTableModel
{
Q_OBJECT
public:
enum ModelColumns {
enum Column {
NetworkType = 0,
FileName,
Path,
Modified
Modified,
COUNT
};
explicit WalletKeysFilesModel(QObject *parent = nullptr);
@ -44,25 +51,25 @@ public:
Q_INVOKABLE void clear();
void findWallets();
void addWalletKeysFile(const WalletKeysFiles &walletKeysFile);
void addWalletKeysFile(const WalletKeysFile &walletKeysFile);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QStringList walletDirectories;
private:
void updateDirectories();
QList<WalletKeysFiles> m_walletKeyFiles;
QAbstractItemModel *m_walletKeysFilesItemModel;
QSortFilterProxyModel m_walletKeysFilesModelProxy;
QStringList m_walletDirectories;
QList<WalletKeysFile> m_walletKeyFiles;
};
class WalletKeysFilesProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
explicit WalletKeysFilesProxyModel(QObject *parent, NetworkType::Type nettype);
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
@ -71,4 +78,4 @@ private:
NetworkType::Type m_nettype;
};
#endif // KEYSFILES_H
#endif //FEATHER_WALLETKEYSFILESMODEL_H

View file

@ -91,7 +91,7 @@ bool PageOpenWallet::validatePage() {
QMessageBox::warning(this, "Wallet not selected", "Please select a wallet from the list.");
return false;
}
QString walletPath = index.model()->data(index.siblingAtColumn(WalletKeysFilesModel::ModelColumns::Path), Qt::UserRole).toString();
QString walletPath = index.model()->data(index.siblingAtColumn(WalletKeysFilesModel::Column::Path), Qt::UserRole).toString();
auto autoWallet = ui->openOnStartup->isChecked() ? QString("%1%2").arg(constants::networkType).arg(walletPath) : "";
config()->set(Config::autoOpenWalletPath, autoWallet);

View file

@ -9,7 +9,7 @@
#include <QWidget>
#include "appcontext.h"
#include "utils/keysfiles.h"
#include "model/WalletKeysFilesModel.h"
namespace Ui {
class PageOpenWallet;

View file

@ -79,10 +79,6 @@ WalletWizard::WalletWizard(QWidget *parent)
});
}
WalletWizard::~WalletWizard() {
qDebug() << "We're killing the walletwizard";
}
void WalletWizard::onCreateWallet() {
auto walletPath = QString("%1/%2").arg(m_wizardFields.walletDir, m_wizardFields.walletName);

View file

@ -9,7 +9,7 @@
#include <QRadioButton>
#include "appcontext.h"
#include "utils/keysfiles.h"
#include "model/WalletKeysFilesModel.h"
#include "utils/RestoreHeightLookup.h"
#include "utils/config.h"
@ -56,7 +56,6 @@ public:
};
explicit WalletWizard(QWidget *parent = nullptr);
~WalletWizard() override;
signals:
void initialNetworkConfigured();