Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update FTB App import instance path detection #2166

Merged
merged 3 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions launcher/ui/pages/modplatform/import_ftb/ImportFTBPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "ui_ImportFTBPage.h"

#include <QFileDialog>
#include <QFileInfo>
#include <QWidget>
#include "FileSystem.h"
#include "ListModel.h"
Expand Down Expand Up @@ -58,8 +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 path = listModel->getPath();
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);
});
Expand Down
101 changes: 66 additions & 35 deletions launcher/ui/pages/modplatform/import_ftb/ListModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,57 +24,88 @@
#include <QIcon>
#include <QProcessEnvironment>
#include "Application.h"
#include "Exception.h"
#include "FileSystem.h"
#include "Json.h"
#include "StringUtils.h"
#include "modplatform/import_ftb/PackHelpers.h"
#include "ui/widgets/ProjectItem.h"

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");
}

static const QString FTB_APP_PATH = FS::PathCombine(getStaticPath(), "instances");
QString getDynamicPath()
{
auto settingsPath = FS::PathCombine(getFTBRoot(), "storage", "settings.json");
if (!QFileInfo::exists(settingsPath))
settingsPath = FS::PathCombine(getFTBRoot(), "bin", "settings.json");
if (!QFileInfo::exists(settingsPath)) {
qWarning() << "The ftb app setings doesn't exist.";
return {};
}
try {
auto doc = Json::requireDocument(FS::read(settingsPath));
return Json::requireString(Json::requireObject(doc), "instanceLocation");
} catch (const Exception& e) {
qCritical() << "Could not read ftb settings file: " << e.cause();
}
return {};
}

ListModel::ListModel(QObject* parent) : QAbstractListModel(parent), m_instances_path(getDynamicPath()) {}

void ListModel::update()
{
beginResetModel();
modpacks.clear();
m_modpacks.clear();

QString instancesPath = getPath();
if (auto instancesInfo = QFileInfo(instancesPath); instancesInfo.exists() && instancesInfo.isDir()) {
QDirIterator directoryIterator(instancesPath, QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable | QDir::Hidden,
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 modpack = parseDirectory(directoryIterator.next());
if (!modpack.path.isEmpty())
modpacks.append(modpack);
auto currentPath = directoryIterator.next();
if (!wasPathAdded(currentPath)) {
auto modpack = parseDirectory(currentPath);
if (!modpack.path.isEmpty())
m_modpacks.append(modpack);
}
}
} else {
qDebug() << "Couldn't find ftb instances folder: " << instancesPath;
}
};

scanPath(APPLICATION->settings()->get("FTBAppInstancesPath").toString());
scanPath(m_instances_path);

endResetModel();
}

QVariant ListModel::data(const QModelIndex& index, int role) const
{
int pos = index.row();
if (pos >= modpacks.size() || pos < 0 || !index.isValid()) {
if (pos >= m_modpacks.size() || pos < 0 || !index.isValid()) {
return QVariant();
}

auto pack = modpacks.at(pos);
auto pack = m_modpacks.at(pos);
if (role == Qt::ToolTipRole) {
}

Expand Down Expand Up @@ -110,22 +141,22 @@ QVariant ListModel::data(const QModelIndex& index, int role) const

FilterModel::FilterModel(QObject* parent) : QSortFilterProxyModel(parent)
{
currentSorting = Sorting::ByGameVersion;
sortings.insert(tr("Sort by Name"), Sorting::ByName);
sortings.insert(tr("Sort by Game Version"), Sorting::ByGameVersion);
m_currentSorting = Sorting::ByGameVersion;
m_sortings.insert(tr("Sort by Name"), Sorting::ByName);
m_sortings.insert(tr("Sort by Game Version"), Sorting::ByGameVersion);
}

bool FilterModel::lessThan(const QModelIndex& left, const QModelIndex& right) const
{
Modpack leftPack = sourceModel()->data(left, Qt::UserRole).value<Modpack>();
Modpack rightPack = sourceModel()->data(right, Qt::UserRole).value<Modpack>();

if (currentSorting == Sorting::ByGameVersion) {
if (m_currentSorting == Sorting::ByGameVersion) {
Version lv(leftPack.mcVersion);
Version rv(rightPack.mcVersion);
return lv < rv;

} else if (currentSorting == Sorting::ByName) {
} else if (m_currentSorting == Sorting::ByName) {
return StringUtils::naturalCompare(leftPack.name, rightPack.name, Qt::CaseSensitive) >= 0;
}

Expand All @@ -136,51 +167,51 @@ bool FilterModel::lessThan(const QModelIndex& left, const QModelIndex& right) co

bool FilterModel::filterAcceptsRow([[maybe_unused]] int sourceRow, [[maybe_unused]] const QModelIndex& sourceParent) const
{
if (searchTerm.isEmpty()) {
if (m_searchTerm.isEmpty()) {
return true;
}
QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
Modpack pack = sourceModel()->data(index, Qt::UserRole).value<Modpack>();
return pack.name.contains(searchTerm, Qt::CaseInsensitive);
return pack.name.contains(m_searchTerm, Qt::CaseInsensitive);
}

void FilterModel::setSearchTerm(const QString term)
{
searchTerm = term.trimmed();
m_searchTerm = term.trimmed();
invalidate();
}

const QMap<QString, FilterModel::Sorting> FilterModel::getAvailableSortings()
{
return sortings;
return m_sortings;
}

QString FilterModel::translateCurrentSorting()
{
return sortings.key(currentSorting);
return m_sortings.key(m_currentSorting);
}

void FilterModel::setSorting(Sorting s)
{
currentSorting = s;
m_currentSorting = s;
invalidate();
}

FilterModel::Sorting FilterModel::getCurrentSorting()
{
return currentSorting;
return m_currentSorting;
}
void ListModel::setPath(QString path)
{
APPLICATION->settings()->set("FTBAppInstancesPath", path);
update();
}

QString ListModel::getPath()
QString ListModel::getUserPath()
{
auto path = APPLICATION->settings()->get("FTBAppInstancesPath").toString();
if (path.isEmpty() || !QFileInfo(path).exists())
path = FTB_APP_PATH;
if (path.isEmpty())
path = m_instances_path;
return path;
}
} // namespace FTBImportAPP
15 changes: 8 additions & 7 deletions launcher/ui/pages/modplatform/import_ftb/ListModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,29 @@ class FilterModel : public QSortFilterProxyModel {
bool lessThan(const QModelIndex& left, const QModelIndex& right) const override;

private:
QMap<QString, Sorting> sortings;
Sorting currentSorting;
QString searchTerm;
QMap<QString, Sorting> m_sortings;
Sorting m_currentSorting;
QString m_searchTerm;
};

class ListModel : public QAbstractListModel {
Q_OBJECT

public:
ListModel(QObject* parent) : QAbstractListModel(parent) {}
ListModel(QObject* parent);
virtual ~ListModel() = default;

int rowCount(const QModelIndex& parent) const { return modpacks.size(); }
int rowCount(const QModelIndex& parent) const { return m_modpacks.size(); }
int columnCount(const QModelIndex& parent) const { return 1; }
QVariant data(const QModelIndex& index, int role) const;

void update();

QString getPath();
QString getUserPath();
void setPath(QString path);

private:
ModpackList modpacks;
ModpackList m_modpacks;
const QString m_instances_path;
};
} // namespace FTBImportAPP