From 27e76d0dcb69d0f34fa64fca89964b231c194387 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Tue, 27 Feb 2024 17:11:19 +0200 Subject: [PATCH] Removed static path Signed-off-by: Trial97 --- .../modplatform/import_ftb/ImportFTBPage.cpp | 11 +-- .../modplatform/import_ftb/ListModel.cpp | 69 ++++++++++--------- .../pages/modplatform/import_ftb/ListModel.h | 5 +- 3 files changed, 40 insertions(+), 45 deletions(-) diff --git a/launcher/ui/pages/modplatform/import_ftb/ImportFTBPage.cpp b/launcher/ui/pages/modplatform/import_ftb/ImportFTBPage.cpp index c72a5a9da..db59fe10a 100644 --- a/launcher/ui/pages/modplatform/import_ftb/ImportFTBPage.cpp +++ b/launcher/ui/pages/modplatform/import_ftb/ImportFTBPage.cpp @@ -59,15 +59,8 @@ ImportFTBPage::ImportFTBPage(NewInstanceDialog* dialog, QWidget* parent) : QWidg connect(ui->searchEdit, &QLineEdit::textChanged, this, &ImportFTBPage::triggerSearch); connect(ui->browseButton, &QPushButton::clicked, this, [this] { - auto paths = listModel->getPosiblePaths(); - QString path; - for (auto p : paths) { - if (p != "" && QFileInfo::exists(p)) { - path = p; - break; - } - } - QString dir = QFileDialog::getExistingDirectory(this, tr("Select FTBApp instances directory"), path, QFileDialog::ShowDirsOnly); + QString dir = QFileDialog::getExistingDirectory(this, tr("Select FTBApp instances directory"), listModel->getUserPath(), + QFileDialog::ShowDirsOnly); if (!dir.isEmpty()) listModel->setPath(dir); }); diff --git a/launcher/ui/pages/modplatform/import_ftb/ListModel.cpp b/launcher/ui/pages/modplatform/import_ftb/ListModel.cpp index ad03d571b..52671ea8e 100644 --- a/launcher/ui/pages/modplatform/import_ftb/ListModel.cpp +++ b/launcher/ui/pages/modplatform/import_ftb/ListModel.cpp @@ -33,22 +33,18 @@ namespace FTBImportAPP { -QString getStaticPath() +QString getFTBRoot() { - QString partialPath; + QString partialPath = QDir::homePath(); #if defined(Q_OS_OSX) - partialPath = FS::PathCombine(QDir::homePath(), "Library/Application Support"); -#elif defined(Q_OS_WIN32) - partialPath = QProcessEnvironment::systemEnvironment().value("LOCALAPPDATA", ""); -#else - partialPath = QDir::homePath(); + partialPath = FS::PathCombine(partialPath, "Library/Application Support"); #endif - return FS::PathCombine(partialPath, ".ftba", "instances"); + return FS::PathCombine(partialPath, ".ftba"); } QString getDynamicPath() { - auto settingsPath = FS::PathCombine(QDir::homePath(), ".ftba", "bin", "settings.json"); + auto settingsPath = FS::PathCombine(getFTBRoot(), "bin", "settings.json"); if (!QFileInfo::exists(settingsPath)) { qWarning() << "The ftb app setings doesn't exist."; return {}; @@ -62,36 +58,40 @@ QString getDynamicPath() return {}; } -ListModel::ListModel(QObject* parent) : QAbstractListModel(parent), m_static_path(getStaticPath()), m_dynamic_path(getDynamicPath()) {} +ListModel::ListModel(QObject* parent) : QAbstractListModel(parent), m_instances_path(getDynamicPath()) {} void ListModel::update() { beginResetModel(); m_modpacks.clear(); - auto paths = getPosiblePaths(); - paths.removeDuplicates(); - for (auto instancesPath : paths) { - if (auto instancesInfo = QFileInfo(instancesPath); instancesInfo.exists() && instancesInfo.isDir()) { - QDirIterator directoryIterator(instancesPath, QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable | QDir::Hidden, - QDirIterator::FollowSymlinks); - while (directoryIterator.hasNext()) { - auto currentPath = directoryIterator.next(); - bool wasAdded = false; - for (auto pack : m_modpacks) { - if (pack.path == currentPath) { - wasAdded = true; - break; - } - } - if (!wasAdded) { - auto modpack = parseDirectory(currentPath); - if (!modpack.path.isEmpty()) - m_modpacks.append(modpack); - } + auto wasPathAdded = [this](QString path) { + for (auto pack : m_modpacks) { + if (pack.path == path) + return true; + } + return false; + }; + + auto scanPath = [this, wasPathAdded](QString path) { + if (path.isEmpty()) + return; + if (auto instancesInfo = QFileInfo(path); !instancesInfo.exists() || !instancesInfo.isDir()) + return; + QDirIterator directoryIterator(path, QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable | QDir::Hidden, + QDirIterator::FollowSymlinks); + while (directoryIterator.hasNext()) { + auto currentPath = directoryIterator.next(); + if (!wasPathAdded(currentPath)) { + auto modpack = parseDirectory(currentPath); + if (!modpack.path.isEmpty()) + m_modpacks.append(modpack); } } - } + }; + + scanPath(APPLICATION->settings()->get("FTBAppInstancesPath").toString()); + scanPath(m_instances_path); endResetModel(); } @@ -205,8 +205,11 @@ void ListModel::setPath(QString path) update(); } -QStringList ListModel::getPosiblePaths() +QString ListModel::getUserPath() { - return { APPLICATION->settings()->get("FTBAppInstancesPath").toString(), m_dynamic_path, m_static_path }; + auto path = APPLICATION->settings()->get("FTBAppInstancesPath").toString(); + if (path.isEmpty()) + path = m_instances_path; + return path; } } // namespace FTBImportAPP \ No newline at end of file diff --git a/launcher/ui/pages/modplatform/import_ftb/ListModel.h b/launcher/ui/pages/modplatform/import_ftb/ListModel.h index 393836b26..a842ac8ff 100644 --- a/launcher/ui/pages/modplatform/import_ftb/ListModel.h +++ b/launcher/ui/pages/modplatform/import_ftb/ListModel.h @@ -60,12 +60,11 @@ class ListModel : public QAbstractListModel { void update(); - QStringList getPosiblePaths(); + QString getUserPath(); void setPath(QString path); private: ModpackList m_modpacks; - const QString m_static_path; - const QString m_dynamic_path; + const QString m_instances_path; }; } // namespace FTBImportAPP \ No newline at end of file