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

View file

@ -1,25 +1,30 @@
// SPDX-License-Identifier: BSD-3-Clause // SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2014-2021, The Monero Project. // Copyright (c) 2014-2021, The Monero Project.
#ifndef KEYSFILES_H #ifndef FEATHER_WALLETKEYSFILESMODEL_H
#define KEYSFILES_H #define FEATHER_WALLETKEYSFILESMODEL_H
#include <QObject>
#include <QFileInfo>
#include <QAbstractTableModel>
#include <QSortFilterProxyModel>
#include "libwalletqt/WalletManager.h"
#include "utils/networktype.h" #include "utils/networktype.h"
#include "utils/utils.h"
class WalletKeysFiles class WalletKeysFile
{ {
public: public:
WalletKeysFiles(const QFileInfo &info, int networkType, QString address); WalletKeysFile(const QFileInfo &info, int networkType, QString address);
QString fileName() const; QString fileName() const {return m_fileName;};
qint64 modified() const; qint64 modified() const {return m_modified;};
QString path() const; QString path() const {return m_path;};
int networkType() const; int networkType() const {return m_networkType;};
QString address() const; QString address() const {return m_address;};
private: private:
static qint64 getModified(const QFileInfo &info);
QString m_fileName; QString m_fileName;
qint64 m_modified; qint64 m_modified;
QString m_path; QString m_path;
@ -29,13 +34,15 @@ private:
class WalletKeysFilesModel : public QAbstractTableModel class WalletKeysFilesModel : public QAbstractTableModel
{ {
Q_OBJECT Q_OBJECT
public: public:
enum ModelColumns { enum Column {
NetworkType = 0, NetworkType = 0,
FileName, FileName,
Path, Path,
Modified Modified,
COUNT
}; };
explicit WalletKeysFilesModel(QObject *parent = nullptr); explicit WalletKeysFilesModel(QObject *parent = nullptr);
@ -44,25 +51,25 @@ public:
Q_INVOKABLE void clear(); Q_INVOKABLE void clear();
void findWallets(); void findWallets();
void addWalletKeysFile(const WalletKeysFiles &walletKeysFile); void addWalletKeysFile(const WalletKeysFile &walletKeysFile);
int rowCount(const QModelIndex &parent = QModelIndex()) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(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 headerData(int section, Qt::Orientation orientation, int role) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QStringList walletDirectories;
private: private:
void updateDirectories(); void updateDirectories();
QList<WalletKeysFiles> m_walletKeyFiles; QStringList m_walletDirectories;
QAbstractItemModel *m_walletKeysFilesItemModel;
QSortFilterProxyModel m_walletKeysFilesModelProxy; QList<WalletKeysFile> m_walletKeyFiles;
}; };
class WalletKeysFilesProxyModel : public QSortFilterProxyModel class WalletKeysFilesProxyModel : public QSortFilterProxyModel
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit WalletKeysFilesProxyModel(QObject *parent, NetworkType::Type nettype); explicit WalletKeysFilesProxyModel(QObject *parent, NetworkType::Type nettype);
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override; bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
@ -71,4 +78,4 @@ private:
NetworkType::Type m_nettype; 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."); QMessageBox::warning(this, "Wallet not selected", "Please select a wallet from the list.");
return false; 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) : ""; auto autoWallet = ui->openOnStartup->isChecked() ? QString("%1%2").arg(constants::networkType).arg(walletPath) : "";
config()->set(Config::autoOpenWalletPath, autoWallet); config()->set(Config::autoOpenWalletPath, autoWallet);

View file

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

View file

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

View file

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