Removed static path

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2024-02-27 17:11:19 +02:00
parent 344398f238
commit 27e76d0dcb
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
3 changed files with 40 additions and 45 deletions

View File

@ -59,15 +59,8 @@ ImportFTBPage::ImportFTBPage(NewInstanceDialog* dialog, QWidget* parent) : QWidg
connect(ui->searchEdit, &QLineEdit::textChanged, this, &ImportFTBPage::triggerSearch); connect(ui->searchEdit, &QLineEdit::textChanged, this, &ImportFTBPage::triggerSearch);
connect(ui->browseButton, &QPushButton::clicked, this, [this] { connect(ui->browseButton, &QPushButton::clicked, this, [this] {
auto paths = listModel->getPosiblePaths(); QString dir = QFileDialog::getExistingDirectory(this, tr("Select FTBApp instances directory"), listModel->getUserPath(),
QString path; QFileDialog::ShowDirsOnly);
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);
if (!dir.isEmpty()) if (!dir.isEmpty())
listModel->setPath(dir); listModel->setPath(dir);
}); });

View File

@ -33,22 +33,18 @@
namespace FTBImportAPP { namespace FTBImportAPP {
QString getStaticPath() QString getFTBRoot()
{ {
QString partialPath; QString partialPath = QDir::homePath();
#if defined(Q_OS_OSX) #if defined(Q_OS_OSX)
partialPath = FS::PathCombine(QDir::homePath(), "Library/Application Support"); partialPath = FS::PathCombine(partialPath, "Library/Application Support");
#elif defined(Q_OS_WIN32)
partialPath = QProcessEnvironment::systemEnvironment().value("LOCALAPPDATA", "");
#else
partialPath = QDir::homePath();
#endif #endif
return FS::PathCombine(partialPath, ".ftba", "instances"); return FS::PathCombine(partialPath, ".ftba");
} }
QString getDynamicPath() QString getDynamicPath()
{ {
auto settingsPath = FS::PathCombine(QDir::homePath(), ".ftba", "bin", "settings.json"); auto settingsPath = FS::PathCombine(getFTBRoot(), "bin", "settings.json");
if (!QFileInfo::exists(settingsPath)) { if (!QFileInfo::exists(settingsPath)) {
qWarning() << "The ftb app setings doesn't exist."; qWarning() << "The ftb app setings doesn't exist.";
return {}; return {};
@ -62,36 +58,40 @@ QString getDynamicPath()
return {}; 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() void ListModel::update()
{ {
beginResetModel(); beginResetModel();
m_modpacks.clear(); m_modpacks.clear();
auto paths = getPosiblePaths(); auto wasPathAdded = [this](QString path) {
paths.removeDuplicates(); for (auto pack : m_modpacks) {
for (auto instancesPath : paths) { if (pack.path == path)
if (auto instancesInfo = QFileInfo(instancesPath); instancesInfo.exists() && instancesInfo.isDir()) { return true;
QDirIterator directoryIterator(instancesPath, QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable | QDir::Hidden, }
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); QDirIterator::FollowSymlinks);
while (directoryIterator.hasNext()) { while (directoryIterator.hasNext()) {
auto currentPath = directoryIterator.next(); auto currentPath = directoryIterator.next();
bool wasAdded = false; if (!wasPathAdded(currentPath)) {
for (auto pack : m_modpacks) {
if (pack.path == currentPath) {
wasAdded = true;
break;
}
}
if (!wasAdded) {
auto modpack = parseDirectory(currentPath); auto modpack = parseDirectory(currentPath);
if (!modpack.path.isEmpty()) if (!modpack.path.isEmpty())
m_modpacks.append(modpack); m_modpacks.append(modpack);
} }
} }
} };
}
scanPath(APPLICATION->settings()->get("FTBAppInstancesPath").toString());
scanPath(m_instances_path);
endResetModel(); endResetModel();
} }
@ -205,8 +205,11 @@ void ListModel::setPath(QString path)
update(); 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 } // namespace FTBImportAPP

View File

@ -60,12 +60,11 @@ class ListModel : public QAbstractListModel {
void update(); void update();
QStringList getPosiblePaths(); QString getUserPath();
void setPath(QString path); void setPath(QString path);
private: private:
ModpackList m_modpacks; ModpackList m_modpacks;
const QString m_static_path; const QString m_instances_path;
const QString m_dynamic_path;
}; };
} // namespace FTBImportAPP } // namespace FTBImportAPP