From 3d646fb345783a443c2822292ccef19194f2f9ce Mon Sep 17 00:00:00 2001 From: tobtoht Date: Thu, 20 May 2021 21:39:53 +0200 Subject: [PATCH] Clean up WalletKeysFilesModel --- .../WalletKeysFilesModel.cpp} | 101 +++++++++--------- .../WalletKeysFilesModel.h} | 47 ++++---- src/wizard/PageOpenWallet.cpp | 2 +- src/wizard/PageOpenWallet.h | 2 +- src/wizard/WalletWizard.cpp | 4 - src/wizard/WalletWizard.h | 3 +- 6 files changed, 81 insertions(+), 78 deletions(-) rename src/{utils/keysfiles.cpp => model/WalletKeysFilesModel.cpp} (74%) rename src/{utils/keysfiles.h => model/WalletKeysFilesModel.h} (59%) diff --git a/src/utils/keysfiles.cpp b/src/model/WalletKeysFilesModel.cpp similarity index 74% rename from src/utils/keysfiles.cpp rename to src/model/WalletKeysFilesModel.cpp index 8a0a2c3..37fd03d 100644 --- a/src/utils/keysfiles.cpp +++ b/src/model/WalletKeysFilesModel.cpp @@ -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 +#include +#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) + : QAbstractTableModel(parent) { this->updateDirectories(); - this->m_walletKeysFilesItemModel = qobject_cast(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(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(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(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) diff --git a/src/utils/keysfiles.h b/src/model/WalletKeysFilesModel.h similarity index 59% rename from src/utils/keysfiles.h rename to src/model/WalletKeysFilesModel.h index efd773f..cdabe08 100644 --- a/src/utils/keysfiles.h +++ b/src/model/WalletKeysFilesModel.h @@ -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 +#include +#include +#include -#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; @@ -29,13 +34,15 @@ private: class WalletKeysFilesModel : public QAbstractTableModel { - Q_OBJECT +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 m_walletKeyFiles; - QAbstractItemModel *m_walletKeysFilesItemModel; - QSortFilterProxyModel m_walletKeysFilesModelProxy; + QStringList m_walletDirectories; + + QList 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 diff --git a/src/wizard/PageOpenWallet.cpp b/src/wizard/PageOpenWallet.cpp index 34f1a6f..4c0e188 100644 --- a/src/wizard/PageOpenWallet.cpp +++ b/src/wizard/PageOpenWallet.cpp @@ -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); diff --git a/src/wizard/PageOpenWallet.h b/src/wizard/PageOpenWallet.h index 3ed45a1..1385203 100644 --- a/src/wizard/PageOpenWallet.h +++ b/src/wizard/PageOpenWallet.h @@ -9,7 +9,7 @@ #include #include "appcontext.h" -#include "utils/keysfiles.h" +#include "model/WalletKeysFilesModel.h" namespace Ui { class PageOpenWallet; diff --git a/src/wizard/WalletWizard.cpp b/src/wizard/WalletWizard.cpp index 6b55157..5501b6d 100644 --- a/src/wizard/WalletWizard.cpp +++ b/src/wizard/WalletWizard.cpp @@ -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); diff --git a/src/wizard/WalletWizard.h b/src/wizard/WalletWizard.h index 6161f71..2c2db39 100644 --- a/src/wizard/WalletWizard.h +++ b/src/wizard/WalletWizard.h @@ -9,7 +9,7 @@ #include #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();