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

View File

@ -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

View File

@ -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