NOISSUE FTB pack code implementation, cleaned up

This commit is contained in:
Jannis Lübke 2018-08-02 00:52:31 +02:00 committed by Petr Mrázek
parent 6cee50eac6
commit 6aada8adf7
14 changed files with 547 additions and 165 deletions

View File

@ -425,6 +425,9 @@ set(FTB_SOURCES
modplatform/ftb/FtbPackInstallTask.h modplatform/ftb/FtbPackInstallTask.h
modplatform/ftb/FtbPackInstallTask.cpp modplatform/ftb/FtbPackInstallTask.cpp
modplatform/ftb/FtbPrivatePackManager.h
modplatform/ftb/FtbPrivatePackManager.cpp
modplatform/ftb/PackHelpers.h modplatform/ftb/PackHelpers.h
) )

View File

@ -1,16 +1,12 @@
#include "FtbPackFetchTask.h" #include "FtbPackFetchTask.h"
#include <QDomDocument> #include <QDomDocument>
#include "FtbPrivatePackManager.h"
FtbPackFetchTask::FtbPackFetchTask()
{
}
FtbPackFetchTask::~FtbPackFetchTask()
{
}
void FtbPackFetchTask::fetch() void FtbPackFetchTask::fetch()
{ {
publicPacks.clear();
thirdPartyPacks.clear();
NetJob *netJob = new NetJob("FtbModpackFetch"); NetJob *netJob = new NetJob("FtbModpackFetch");
QUrl publicPacksUrl = QUrl("https://ftb.cursecdn.com/FTB2/static/modpacks.xml"); QUrl publicPacksUrl = QUrl("https://ftb.cursecdn.com/FTB2/static/modpacks.xml");
@ -28,23 +24,67 @@ void FtbPackFetchTask::fetch()
netJob->start(); netJob->start();
} }
void FtbPackFetchTask::fetchPrivate(const QStringList & toFetch)
{
QString privatePackBaseUrl = QString("https://ftb.cursecdn.com/FTB2/static/%1.xml");
for (auto &packCode: toFetch)
{
QByteArray *data = new QByteArray();
NetJob *job = new NetJob("Fetching private pack");
job->addNetAction(Net::Download::makeByteArray(privatePackBaseUrl.arg(packCode), data));
QObject::connect(job, &NetJob::succeeded, this, [this, job, data, packCode]
{
FtbModpackList packs;
parseAndAddPacks(*data, FtbPackType::Private, packs);
foreach(FtbModpack currentPack, packs)
{
currentPack.packCode = packCode;
emit privateFileDownloadFinished(currentPack);
}
job->deleteLater();
data->clear();
delete data;
});
QObject::connect(job, &NetJob::failed, this, [this, job, packCode, data](QString reason)
{
emit privateFileDownloadFailed(reason, packCode);
job->deleteLater();
data->clear();
delete data;
});
job->start();
}
}
void FtbPackFetchTask::fileDownloadFinished() void FtbPackFetchTask::fileDownloadFinished()
{ {
jobPtr.reset(); jobPtr.reset();
QStringList failedLists; QStringList failedLists;
if(!parseAndAddPacks(publicModpacksXmlFileData, FtbPackType::Public, publicPacks)) { if(!parseAndAddPacks(publicModpacksXmlFileData, FtbPackType::Public, publicPacks))
{
failedLists.append(tr("Public Packs")); failedLists.append(tr("Public Packs"));
} }
if(!parseAndAddPacks(thirdPartyModpacksXmlFileData, FtbPackType::ThirdParty, thirdPartyPacks)) { if(!parseAndAddPacks(thirdPartyModpacksXmlFileData, FtbPackType::ThirdParty, thirdPartyPacks))
{
failedLists.append(tr("Third Party Packs")); failedLists.append(tr("Third Party Packs"));
} }
if(failedLists.size() > 0) { if(failedLists.size() > 0)
emit failed(QString("Failed to download some pack lists:%1").arg(failedLists.join("\n- "))); {
} else { emit failed(tr("Failed to download some pack lists: %1").arg(failedLists.join("\n- ")));
}
else
{
emit finished(publicPacks, thirdPartyPacks); emit finished(publicPacks, thirdPartyPacks);
} }
} }
@ -57,15 +97,17 @@ bool FtbPackFetchTask::parseAndAddPacks(QByteArray &data, FtbPackType packType,
int errorLine = -1; int errorLine = -1;
int errorCol = -1; int errorCol = -1;
if(!doc.setContent(data, false, &errorMsg, &errorLine, &errorCol)){ if(!doc.setContent(data, false, &errorMsg, &errorLine, &errorCol))
auto fullErrMsg = QString("Failed to fetch modpack data: %s %d:%d!").arg(errorMsg, errorLine, errorCol); {
auto fullErrMsg = QString("Failed to fetch modpack data: %1 %2:3d!").arg(errorMsg, errorLine, errorCol);
qWarning() << fullErrMsg; qWarning() << fullErrMsg;
data.clear(); data.clear();
return false; return false;
} }
QDomNodeList nodes = doc.elementsByTagName("modpack"); QDomNodeList nodes = doc.elementsByTagName("modpack");
for(int i = 0; i < nodes.length(); i++) { for(int i = 0; i < nodes.length(); i++)
{
QDomElement element = nodes.at(i).toElement(); QDomElement element = nodes.at(i).toElement();
FtbModpack modpack; FtbModpack modpack;
@ -80,19 +122,25 @@ bool FtbPackFetchTask::parseAndAddPacks(QByteArray &data, FtbPackType packType,
modpack.bugged = false; modpack.bugged = false;
//remove empty if the xml is bugged //remove empty if the xml is bugged
for(QString curr : modpack.oldVersions) { for(QString curr : modpack.oldVersions)
if(curr.isNull() || curr.isEmpty()) { {
if(curr.isNull() || curr.isEmpty())
{
modpack.oldVersions.removeAll(curr); modpack.oldVersions.removeAll(curr);
modpack.bugged = true; modpack.bugged = true;
qWarning() << "Removed some empty versions from" << modpack.name; qWarning() << "Removed some empty versions from" << modpack.name;
} }
} }
if(modpack.oldVersions.size() < 1) { if(modpack.oldVersions.size() < 1)
if(!modpack.currentVersion.isNull() && !modpack.currentVersion.isEmpty()) { {
if(!modpack.currentVersion.isNull() && !modpack.currentVersion.isEmpty())
{
modpack.oldVersions.append(modpack.currentVersion); modpack.oldVersions.append(modpack.currentVersion);
qWarning() << "Added current version to oldVersions because oldVersions was empty! (" + modpack.name + ")"; qWarning() << "Added current version to oldVersions because oldVersions was empty! (" + modpack.name + ")";
} else { }
else
{
modpack.broken = true; modpack.broken = true;
qWarning() << "Broken pack:" << modpack.name << " => No valid version!"; qWarning() << "Broken pack:" << modpack.name << " => No valid version!";
} }
@ -111,7 +159,8 @@ bool FtbPackFetchTask::parseAndAddPacks(QByteArray &data, FtbPackType packType,
return true; return true;
} }
void FtbPackFetchTask::fileDownloadFailed(QString reason){ void FtbPackFetchTask::fileDownloadFailed(QString reason)
qWarning() << "Fetching FtbPacks failed: " << reason; {
qWarning() << "Fetching FtbPacks failed:" << reason;
emit failed(reason); emit failed(reason);
} }

View File

@ -11,10 +11,11 @@ class MULTIMC_LOGIC_EXPORT FtbPackFetchTask : public QObject {
Q_OBJECT Q_OBJECT
public: public:
FtbPackFetchTask(); FtbPackFetchTask() = default;
virtual ~FtbPackFetchTask(); virtual ~FtbPackFetchTask() = default;
void fetch(); void fetch();
void fetchPrivate(const QStringList &toFetch);
private: private:
NetJobPtr jobPtr; NetJobPtr jobPtr;
@ -34,4 +35,6 @@ signals:
void finished(FtbModpackList publicPacks, FtbModpackList thirdPartyPacks); void finished(FtbModpackList publicPacks, FtbModpackList thirdPartyPacks);
void failed(QString reason); void failed(QString reason);
void privateFileDownloadFinished(FtbModpack modpack);
void privateFileDownloadFailed(QString reason, QString packCode);
}; };

View File

@ -29,7 +29,15 @@ void FtbPackInstallTask::downloadPack()
NetJob *job = new NetJob("Download FTB Pack"); NetJob *job = new NetJob("Download FTB Pack");
entry->setStale(true); entry->setStale(true);
QString url = QString("http://ftb.cursecdn.com/FTB2/modpacks/%1").arg(packoffset); QString url;
if(m_pack.type == FtbPackType::Private)
{
url = QString("http://ftb.cursecdn.com/FTB2/privatepacks/%1").arg(packoffset);
}
else
{
url = QString("http://ftb.cursecdn.com/FTB2/modpacks/%1").arg(packoffset);
}
job->addNetAction(Net::Download::makeCached(url, entry)); job->addNetAction(Net::Download::makeCached(url, entry));
archivePath = entry->getFullPath(); archivePath = entry->getFullPath();

View File

@ -8,8 +8,8 @@
#include "meta/VersionList.h" #include "meta/VersionList.h"
#include "modplatform/ftb/PackHelpers.h" #include "modplatform/ftb/PackHelpers.h"
class MULTIMC_LOGIC_EXPORT FtbPackInstallTask : public InstanceTask { class MULTIMC_LOGIC_EXPORT FtbPackInstallTask : public InstanceTask
{
Q_OBJECT Q_OBJECT
public: public:

View File

@ -0,0 +1,36 @@
#include "FtbPrivatePackManager.h"
#include <QDebug>
#include "FileSystem.h"
void FtbPrivatePackManager::load()
{
try
{
currentPacks = QString::fromUtf8(FS::read(m_filename)).split('\n', QString::SkipEmptyParts).toSet();
dirty = false;
}
catch(...)
{
currentPacks = {};
qWarning() << "Failed to read third party FTB pack codes from" << m_filename;
}
}
void FtbPrivatePackManager::save() const
{
if(!dirty)
{
return;
}
try
{
FS::write(m_filename, currentPacks.toList().join('\n').toUtf8());
dirty = false;
}
catch(...)
{
qWarning() << "Failed to write third party FTB pack codes to" << m_filename;
}
}

View File

@ -0,0 +1,40 @@
#pragma once
#include <QSet>
#include <QString>
#include <QFile>
#include "multimc_logic_export.h"
class MULTIMC_LOGIC_EXPORT FtbPrivatePackManager
{
public:
~FtbPrivatePackManager()
{
save();
}
void load();
void save() const;
bool empty() const
{
return currentPacks.empty();
}
const QSet<QString> &getCurrentPackCodes() const
{
return currentPacks;
}
void add(const QString &code)
{
currentPacks.insert(code);
dirty = true;
}
void remove(const QString &code)
{
currentPacks.remove(code);
dirty = true;
}
private:
QSet<QString> currentPacks;
QString m_filename = "private_packs.txt";
mutable bool dirty = false;
};

View File

@ -6,13 +6,15 @@
#include <QMetaType> #include <QMetaType>
//Header for structs etc... //Header for structs etc...
enum FtbPackType { enum class FtbPackType
{
Public, Public,
ThirdParty, ThirdParty,
Private Private
}; };
struct FtbModpack { struct FtbModpack
{
QString name; QString name;
QString description; QString description;
QString author; QString author;
@ -30,6 +32,7 @@ struct FtbModpack {
bool broken = false; bool broken = false;
FtbPackType type; FtbPackType type;
QString packCode;
}; };
//We need it for the proxy model //We need it for the proxy model

View File

@ -144,6 +144,12 @@ void NewInstanceDialog::setSuggestedPack(const QString& name, InstanceTask* task
creationTask.reset(task); creationTask.reset(task);
ui->instNameTextBox->setPlaceholderText(name); ui->instNameTextBox->setPlaceholderText(name);
if(!task)
{
ui->iconButton->setIcon(MMC->icons()->getIcon("default"));
importIcon = false;
}
auto allowOK = task && !instName().isEmpty(); auto allowOK = task && !instName().isEmpty();
m_buttons->button(QDialogButtonBox::Ok)->setEnabled(allowOK); m_buttons->button(QDialogButtonBox::Ok)->setEnabled(allowOK);
} }

View File

@ -1,20 +1,23 @@
#include "FTBPage.h" #include "FTBPage.h"
#include "ui_FTBPage.h" #include "ui_FTBPage.h"
#include <QInputDialog>
#include "MultiMC.h" #include "MultiMC.h"
#include "dialogs/CustomMessageBox.h" #include "dialogs/CustomMessageBox.h"
#include "dialogs/NewInstanceDialog.h" #include "dialogs/NewInstanceDialog.h"
#include "modplatform/ftb/FtbPackFetchTask.h" #include "modplatform/ftb/FtbPackFetchTask.h"
#include "modplatform/ftb/FtbPackInstallTask.h" #include "modplatform/ftb/FtbPackInstallTask.h"
#include "modplatform/ftb/FtbPrivatePackManager.h"
#include "FtbListModel.h" #include "FtbListModel.h"
FTBPage::FTBPage(NewInstanceDialog* dialog, QWidget *parent) FTBPage::FTBPage(NewInstanceDialog* dialog, QWidget *parent)
: QWidget(parent), dialog(dialog), ui(new Ui::FTBPage) : QWidget(parent), dialog(dialog), ui(new Ui::FTBPage)
{ {
ftbFetchTask = new FtbPackFetchTask(); ftbFetchTask.reset(new FtbPackFetchTask());
ftbPrivatePacks.reset(new FtbPrivatePackManager());
ui->setupUi(this); ui->setupUi(this);
ui->tabWidget->tabBar()->hide();
{ {
publicFilterModel = new FtbFilterModel(this); publicFilterModel = new FtbFilterModel(this);
@ -49,29 +52,47 @@ FTBPage::FTBPage(NewInstanceDialog* dialog, QWidget *parent)
thirdPartyFilterModel->setSorting(publicFilterModel->getCurrentSorting()); thirdPartyFilterModel->setSorting(publicFilterModel->getCurrentSorting());
} }
ui->packVersionSelection->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); {
ui->packVersionSelection->view()->parentWidget()->setMaximumHeight(300); privateFilterModel = new FtbFilterModel(this);
privateListModel = new FtbListModel(this);
privateFilterModel->setSourceModel(privateListModel);
ui->privatePackList->setModel(privateFilterModel);
ui->privatePackList->setSortingEnabled(true);
ui->privatePackList->header()->hide();
ui->privatePackList->setIndentation(0);
ui->privatePackList->setIconSize(QSize(42, 42));
privateFilterModel->setSorting(publicFilterModel->getCurrentSorting());
}
ui->versionSelectionBox->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
ui->versionSelectionBox->view()->parentWidget()->setMaximumHeight(300);
connect(ui->sortByBox, &QComboBox::currentTextChanged, this, &FTBPage::onSortingSelectionChanged); connect(ui->sortByBox, &QComboBox::currentTextChanged, this, &FTBPage::onSortingSelectionChanged);
connect(ui->packVersionSelection, &QComboBox::currentTextChanged, this, &FTBPage::onVersionSelectionItemChanged); connect(ui->versionSelectionBox, &QComboBox::currentTextChanged, this, &FTBPage::onVersionSelectionItemChanged);
connect(ui->publicPackList->selectionModel(), &QItemSelectionModel::currentChanged, this, &FTBPage::onPublicPackSelectionChanged); connect(ui->publicPackList->selectionModel(), &QItemSelectionModel::currentChanged, this, &FTBPage::onPublicPackSelectionChanged);
connect(ui->thirdPartyPackList->selectionModel(), &QItemSelectionModel::currentChanged, this, &FTBPage::onThirdPartyPackSelectionChanged); connect(ui->thirdPartyPackList->selectionModel(), &QItemSelectionModel::currentChanged, this, &FTBPage::onThirdPartyPackSelectionChanged);
connect(ui->privatePackList->selectionModel(), &QItemSelectionModel::currentChanged, this, &FTBPage::onPrivatePackSelectionChanged);
connect(ui->ftbTabWidget, &QTabWidget::currentChanged, this, &FTBPage::onTabChanged); connect(ui->addPackBtn, &QPushButton::pressed, this, &FTBPage::onAddPackClicked);
connect(ui->removePackBtn, &QPushButton::pressed, this, &FTBPage::onRemovePackClicked);
ui->modpackInfo->setOpenExternalLinks(true); connect(ui->tabWidget, &QTabWidget::currentChanged, this, &FTBPage::onTabChanged);
// ui->modpackInfo->setOpenExternalLinks(true);
ui->publicPackList->selectionModel()->reset(); ui->publicPackList->selectionModel()->reset();
ui->thirdPartyPackList->selectionModel()->reset(); ui->thirdPartyPackList->selectionModel()->reset();
ui->privatePackList->selectionModel()->reset();
onTabChanged(ui->tabWidget->currentIndex());
} }
FTBPage::~FTBPage() FTBPage::~FTBPage()
{ {
delete ui; delete ui;
if(ftbFetchTask) {
ftbFetchTask->deleteLater();
}
} }
bool FTBPage::shouldDisplay() const bool FTBPage::shouldDisplay() const
@ -83,9 +104,15 @@ void FTBPage::openedImpl()
{ {
if(!initialized) if(!initialized)
{ {
connect(ftbFetchTask, &FtbPackFetchTask::finished, this, &FTBPage::ftbPackDataDownloadSuccessfully); connect(ftbFetchTask.get(), &FtbPackFetchTask::finished, this, &FTBPage::ftbPackDataDownloadSuccessfully);
connect(ftbFetchTask, &FtbPackFetchTask::failed, this, &FTBPage::ftbPackDataDownloadFailed); connect(ftbFetchTask.get(), &FtbPackFetchTask::failed, this, &FTBPage::ftbPackDataDownloadFailed);
connect(ftbFetchTask.get(), &FtbPackFetchTask::privateFileDownloadFinished, this, &FTBPage::ftbPrivatePackDataDownloadSuccessfully);
connect(ftbFetchTask.get(), &FtbPackFetchTask::privateFileDownloadFailed, this, &FTBPage::ftbPrivatePackDataDownloadFailed);
ftbFetchTask->fetch(); ftbFetchTask->fetch();
ftbPrivatePacks->load();
ftbFetchTask->fetchPrivate(ftbPrivatePacks->getCurrentPackCodes().toList());
initialized = true; initialized = true;
} }
suggestCurrent(); suggestCurrent();
@ -98,13 +125,37 @@ void FTBPage::suggestCurrent()
if(!selected.broken) if(!selected.broken)
{ {
dialog->setSuggestedPack(selected.name, new FtbPackInstallTask(selected, selectedVersion)); dialog->setSuggestedPack(selected.name, new FtbPackInstallTask(selected, selectedVersion));
if(selected.type == FtbPackType::Public) { QString editedLogoName;
publicListModel->getLogo(selected.logo, [this](QString logo){ if(selected.logo.toLower().startsWith("ftb"))
dialog->setSuggestedIconFromFile(logo, "ftb_" + selected.name); {
editedLogoName = selected.logo;
}
else
{
editedLogoName = "ftb_" + selected.logo;
}
editedLogoName = editedLogoName.left(editedLogoName.lastIndexOf(".png"));
if(selected.type == FtbPackType::Public)
{
publicListModel->getLogo(selected.logo, [this, editedLogoName](QString logo)
{
dialog->setSuggestedIconFromFile(logo, editedLogoName);
}); });
} else if (selected.type == FtbPackType::ThirdParty) { }
thirdPartyModel->getLogo(selected.logo, [this](QString logo){ else if (selected.type == FtbPackType::ThirdParty)
dialog->setSuggestedIconFromFile(logo, "ftb_" + selected.name); {
thirdPartyModel->getLogo(selected.logo, [this, editedLogoName](QString logo)
{
dialog->setSuggestedIconFromFile(logo, editedLogoName);
});
}
else if (selected.type == FtbPackType::Private)
{
privateListModel->getLogo(selected.logo, [this, editedLogoName](QString logo)
{
dialog->setSuggestedIconFromFile(logo, editedLogoName);
}); });
} }
} }
@ -126,6 +177,24 @@ void FTBPage::ftbPackDataDownloadFailed(QString reason)
//TODO: Display the error //TODO: Display the error
} }
void FTBPage::ftbPrivatePackDataDownloadSuccessfully(FtbModpack pack)
{
privateListModel->addPack(pack);
}
void FTBPage::ftbPrivatePackDataDownloadFailed(QString reason, QString packCode)
{
auto reply = QMessageBox::question(
this,
tr("FTB private packs"),
tr("Failed to download pack information for code %1.\nShould it be removed now?").arg(packCode)
);
if(reply == QMessageBox::Yes)
{
ftbPrivatePacks->remove(packCode);
}
}
void FTBPage::onPublicPackSelectionChanged(QModelIndex now, QModelIndex prev) void FTBPage::onPublicPackSelectionChanged(QModelIndex now, QModelIndex prev)
{ {
if(!now.isValid()) if(!now.isValid())
@ -148,13 +217,25 @@ void FTBPage::onThirdPartyPackSelectionChanged(QModelIndex now, QModelIndex prev
onPackSelectionChanged(&selectedPack); onPackSelectionChanged(&selectedPack);
} }
void FTBPage::onPrivatePackSelectionChanged(QModelIndex now, QModelIndex prev)
{
if(!now.isValid())
{
onPackSelectionChanged();
return;
}
FtbModpack selectedPack = privateFilterModel->data(now, Qt::UserRole).value<FtbModpack>();
onPackSelectionChanged(&selectedPack);
}
void FTBPage::onPackSelectionChanged(FtbModpack* pack) void FTBPage::onPackSelectionChanged(FtbModpack* pack)
{ {
ui->packVersionSelection->clear(); ui->versionSelectionBox->clear();
if(pack) if(pack)
{ {
ui->modpackInfo->setHtml("Pack by <b>" + pack->author + "</b>" + "<br>Minecraft " + pack->mcVersion + "<br>" currentModpackInfo->setHtml("Pack by <b>" + pack->author + "</b>" +
"<br>" + pack->description + "<ul><li>" + pack->mods.replace(";", "</li><li>") + "</li></ul>"); "<br>Minecraft " + pack->mcVersion + "<br>" + "<br>" + pack->description + "<ul><li>" + pack->mods.replace(";", "</li><li>")
+ "</li></ul>");
bool currentAdded = false; bool currentAdded = false;
for(int i = 0; i < pack->oldVersions.size(); i++) for(int i = 0; i < pack->oldVersions.size(); i++)
@ -163,15 +244,25 @@ void FTBPage::onPackSelectionChanged(FtbModpack* pack)
{ {
currentAdded = true; currentAdded = true;
} }
ui->packVersionSelection->addItem(pack->oldVersions.at(i)); ui->versionSelectionBox->addItem(pack->oldVersions.at(i));
} }
if(!currentAdded) if(!currentAdded)
{ {
ui->packVersionSelection->addItem(pack->currentVersion); ui->versionSelectionBox->addItem(pack->currentVersion);
} }
selected = *pack; selected = *pack;
} }
else
{
currentModpackInfo->setHtml("");
ui->versionSelectionBox->clear();
if(isOpened)
{
dialog->setSuggestedPack();
}
return;
}
suggestCurrent(); suggestCurrent();
} }
@ -192,22 +283,31 @@ void FTBPage::onSortingSelectionChanged(QString data)
FtbFilterModel::Sorting toSet = publicFilterModel->getAvailableSortings().value(data); FtbFilterModel::Sorting toSet = publicFilterModel->getAvailableSortings().value(data);
publicFilterModel->setSorting(toSet); publicFilterModel->setSorting(toSet);
thirdPartyFilterModel->setSorting(toSet); thirdPartyFilterModel->setSorting(toSet);
privateFilterModel->setSorting(toSet);
} }
void FTBPage::onTabChanged(int tab) void FTBPage::onTabChanged(int tab)
{ {
FtbFilterModel* currentModel = nullptr; if(tab == 1)
QTreeView* currentList = nullptr;
if (tab == 0)
{
currentModel = publicFilterModel;
currentList = ui->publicPackList;
}
else
{ {
currentModel = thirdPartyFilterModel; currentModel = thirdPartyFilterModel;
currentList = ui->thirdPartyPackList; currentList = ui->thirdPartyPackList;
currentModpackInfo = ui->thirdPartyPackDescription;
} }
else if(tab == 2)
{
currentModel = privateFilterModel;
currentList = ui->privatePackList;
currentModpackInfo = ui->privatePackDescription;
}
else
{
currentModel = publicFilterModel;
currentList = ui->publicPackList;
currentModpackInfo = ui->publicPackDescription;
}
currentList->selectionModel()->reset();
QModelIndex idx = currentList->currentIndex(); QModelIndex idx = currentList->currentIndex();
if(idx.isValid()) if(idx.isValid())
{ {
@ -219,3 +319,46 @@ void FTBPage::onTabChanged(int tab)
onPackSelectionChanged(); onPackSelectionChanged();
} }
} }
void FTBPage::onAddPackClicked()
{
bool ok;
QString text = QInputDialog::getText(
this,
tr("Add FTB pack"),
tr("Enter pack code:"),
QLineEdit::Normal,
QString(),
&ok
);
if(ok && !text.isEmpty())
{
ftbPrivatePacks->add(text);
ftbFetchTask->fetchPrivate({text});
}
}
void FTBPage::onRemovePackClicked()
{
auto index = ui->privatePackList->currentIndex();
if(!index.isValid())
{
return;
}
auto row = index.row();
FtbModpack pack = privateListModel->at(row);
auto answer = QMessageBox::question(
this,
tr("Remove pack"),
tr("Are you sure you want to remove pack %1?").arg(pack.name),
QMessageBox::Yes | QMessageBox::No
);
if(answer != QMessageBox::Yes)
{
return;
}
ftbPrivatePacks->remove(pack.packCode);
privateListModel->remove(row);
onPackSelectionChanged();
}

View File

@ -16,12 +16,15 @@
#pragma once #pragma once
#include <QWidget> #include <QWidget>
#include <QTreeView>
#include <QTextBrowser>
#include "pages/BasePage.h" #include "pages/BasePage.h"
#include <MultiMC.h> #include <MultiMC.h>
#include "tasks/Task.h" #include "tasks/Task.h"
#include "modplatform/ftb/PackHelpers.h" #include "modplatform/ftb/PackHelpers.h"
#include "modplatform/ftb/FtbPackFetchTask.h" #include "modplatform/ftb/FtbPackFetchTask.h"
#include "QObjectPtr.h"
namespace Ui namespace Ui
{ {
@ -31,6 +34,9 @@ class FTBPage;
class FtbListModel; class FtbListModel;
class FtbFilterModel; class FtbFilterModel;
class NewInstanceDialog; class NewInstanceDialog;
class FtbPrivatePackListModel;
class FtbPrivatePackFilterModel;
class FtbPrivatePackManager;
class FTBPage : public QWidget, public BasePage class FTBPage : public QWidget, public BasePage
{ {
@ -66,15 +72,26 @@ private slots:
void ftbPackDataDownloadSuccessfully(FtbModpackList publicPacks, FtbModpackList thirdPartyPacks); void ftbPackDataDownloadSuccessfully(FtbModpackList publicPacks, FtbModpackList thirdPartyPacks);
void ftbPackDataDownloadFailed(QString reason); void ftbPackDataDownloadFailed(QString reason);
void ftbPrivatePackDataDownloadSuccessfully(FtbModpack pack);
void ftbPrivatePackDataDownloadFailed(QString reason, QString packCode);
void onSortingSelectionChanged(QString data); void onSortingSelectionChanged(QString data);
void onVersionSelectionItemChanged(QString data); void onVersionSelectionItemChanged(QString data);
void onPublicPackSelectionChanged(QModelIndex first, QModelIndex second); void onPublicPackSelectionChanged(QModelIndex first, QModelIndex second);
void onThirdPartyPackSelectionChanged(QModelIndex first, QModelIndex second); void onThirdPartyPackSelectionChanged(QModelIndex first, QModelIndex second);
void onPrivatePackSelectionChanged(QModelIndex first, QModelIndex second);
void onTabChanged(int tab); void onTabChanged(int tab);
void onAddPackClicked();
void onRemovePackClicked();
private: private:
FtbFilterModel* currentModel = nullptr;
QTreeView* currentList = nullptr;
QTextBrowser* currentModpackInfo = nullptr;
bool initialized = false; bool initialized = false;
FtbModpack selected; FtbModpack selected;
QString selectedVersion; QString selectedVersion;
@ -85,7 +102,12 @@ private:
FtbListModel *thirdPartyModel = nullptr; FtbListModel *thirdPartyModel = nullptr;
FtbFilterModel *thirdPartyFilterModel = nullptr; FtbFilterModel *thirdPartyFilterModel = nullptr;
FtbPackFetchTask *ftbFetchTask = nullptr; FtbListModel *privateListModel = nullptr;
FtbFilterModel *privateFilterModel = nullptr;
unique_qobject_ptr<FtbPackFetchTask> ftbFetchTask;
std::unique_ptr<FtbPrivatePackManager> ftbPrivatePacks;
NewInstanceDialog* dialog = nullptr; NewInstanceDialog* dialog = nullptr;
Ui::FTBPage *ui = nullptr; Ui::FTBPage *ui = nullptr;

View File

@ -11,18 +11,6 @@
</rect> </rect>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item> <item>
<widget class="QTabWidget" name="tabWidget"> <widget class="QTabWidget" name="tabWidget">
<property name="currentIndex"> <property name="currentIndex">
@ -30,86 +18,107 @@
</property> </property>
<widget class="QWidget" name="tab"> <widget class="QWidget" name="tab">
<attribute name="title"> <attribute name="title">
<string notr="true"/> <string>Public</string>
</attribute> </attribute>
<layout class="QGridLayout" name="gridLayout" columnstretch="0,0,0,0"> <layout class="QGridLayout" name="gridLayout">
<item row="1" column="2" colspan="2"> <item row="0" column="0">
<widget class="QTextBrowser" name="modpackInfo"> <widget class="QTreeView" name="publicPackList">
<property name="verticalScrollBarPolicy"> <property name="maximumSize">
<enum>Qt::ScrollBarAsNeeded</enum> <size>
<width>250</width>
<height>16777215</height>
</size>
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="2"> <item row="0" column="1">
<widget class="QLabel" name="selectedVersionLabel"> <widget class="QTextBrowser" name="publicPackDescription"/>
<property name="text">
<string>Version selected:</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
</widget>
</item> </item>
<item row="2" column="3"> </layout>
<widget class="QComboBox" name="packVersionSelection"/> </widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>3rd Party</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="1">
<widget class="QTextBrowser" name="thirdPartyPackDescription"/>
</item> </item>
<item row="2" column="0"> <item row="0" column="0">
<widget class="QComboBox" name="sortByBox"/> <widget class="QTreeView" name="thirdPartyPackList">
</item> <property name="maximumSize">
<item row="0" column="0" rowspan="2"> <size>
<widget class="QTabWidget" name="ftbTabWidget"> <width>250</width>
<property name="sizePolicy"> <height>16777215</height>
<sizepolicy hsizetype="Preferred" vsizetype="Expanding"> </size>
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property> </property>
<property name="tabShape">
<enum>QTabWidget::Rounded</enum>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Public Packs</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QTreeView" name="publicPackList">
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAsNeeded</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_3">
<attribute name="title">
<string>3rd Party Packs</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QTreeView" name="thirdPartyPackList">
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAsNeeded</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
</widget>
</item>
</layout>
</widget>
</widget> </widget>
</item> </item>
</layout> </layout>
</widget> </widget>
<widget class="QWidget" name="tab_3">
<attribute name="title">
<string>Private</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QTreeView" name="privatePackList">
<property name="maximumSize">
<size>
<width>250</width>
<height>16777215</height>
</size>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QPushButton" name="addPackBtn">
<property name="text">
<string>Add pack</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="removePackBtn">
<property name="text">
<string>Remove selected pack</string>
</property>
</widget>
</item>
<item row="0" column="1" rowspan="3">
<widget class="QTextBrowser" name="privatePackDescription"/>
</item>
</layout>
</widget>
</widget> </widget>
</item> </item>
<item>
<layout class="QGridLayout" name="gridLayout_4">
<item row="0" column="1">
<widget class="QLabel" name="label">
<property name="text">
<string>Version selected:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QComboBox" name="versionSelectionBox"/>
</item>
<item row="0" column="0">
<widget class="QComboBox" name="sortByBox">
<property name="minimumSize">
<size>
<width>265</width>
<height>0</height>
</size>
</property>
</widget>
</item>
</layout>
</item>
</layout> </layout>
</widget> </widget>
<resources/> <resources/>

View File

@ -72,15 +72,17 @@ FtbListModel::~FtbListModel()
QString FtbListModel::translatePackType(FtbPackType type) const QString FtbListModel::translatePackType(FtbPackType type) const
{ {
if(type == FtbPackType::Public) { switch(type)
return tr("Public Modpack"); {
} else if(type == FtbPackType::ThirdParty) { case FtbPackType::Public:
return tr("Third Party Modpack"); return tr("Public Modpack");
} else if(type == FtbPackType::Private) { case FtbPackType::ThirdParty:
return tr("Private Modpack"); return tr("Third Party Modpack");
} else { case FtbPackType::Private:
return tr("Unknown Type"); return tr("Private Modpack");
} }
qWarning() << "Unknown FTB modpack type:" << int(type);
return QString();
} }
int FtbListModel::rowCount(const QModelIndex &parent) const int FtbListModel::rowCount(const QModelIndex &parent) const
@ -96,15 +98,20 @@ int FtbListModel::columnCount(const QModelIndex &parent) const
QVariant FtbListModel::data(const QModelIndex &index, int role) const QVariant FtbListModel::data(const QModelIndex &index, int role) const
{ {
int pos = index.row(); int pos = index.row();
if(pos >= modpacks.size() || pos < 0 || !index.isValid()) { if(pos >= modpacks.size() || pos < 0 || !index.isValid())
{
return QString("INVALID INDEX %1").arg(pos); return QString("INVALID INDEX %1").arg(pos);
} }
FtbModpack pack = modpacks.at(pos); FtbModpack pack = modpacks.at(pos);
if(role == Qt::DisplayRole) { if(role == Qt::DisplayRole)
{
return pack.name + "\n" + translatePackType(pack.type); return pack.name + "\n" + translatePackType(pack.type);
} else if (role == Qt::ToolTipRole) { }
if(pack.description.length() > 100) { else if (role == Qt::ToolTipRole)
{
if(pack.description.length() > 100)
{
//some magic to prevent to long tooltips and replace html linebreaks //some magic to prevent to long tooltips and replace html linebreaks
QString edit = pack.description.left(97); QString edit = pack.description.left(97);
edit = edit.left(edit.lastIndexOf("<br>")).left(edit.lastIndexOf(" ")).append("..."); edit = edit.left(edit.lastIndexOf("<br>")).left(edit.lastIndexOf(" ")).append("...");
@ -112,23 +119,33 @@ QVariant FtbListModel::data(const QModelIndex &index, int role) const
} }
return pack.description; return pack.description;
} else if(role == Qt::DecorationRole) { }
if(m_logoMap.contains(pack.logo)) { else if(role == Qt::DecorationRole)
{
if(m_logoMap.contains(pack.logo))
{
return (m_logoMap.value(pack.logo)); return (m_logoMap.value(pack.logo));
} }
QIcon icon = MMC->getThemedIcon("screenshot-placeholder"); QIcon icon = MMC->getThemedIcon("screenshot-placeholder");
((FtbListModel *)this)->requestLogo(pack.logo); ((FtbListModel *)this)->requestLogo(pack.logo);
return icon; return icon;
} else if(role == Qt::TextColorRole) { }
if(pack.broken) { else if(role == Qt::TextColorRole)
{
if(pack.broken)
{
//FIXME: Hardcoded color //FIXME: Hardcoded color
return QColor(255, 0, 50); return QColor(255, 0, 50);
} else if(pack.bugged) { }
else if(pack.bugged)
{
//FIXME: Hardcoded color //FIXME: Hardcoded color
//bugged pack, currently only indicates bugged xml //bugged pack, currently only indicates bugged xml
return QColor(244, 229, 66); return QColor(244, 229, 66);
} }
} else if(role == Qt::UserRole) { }
else if(role == Qt::UserRole)
{
QVariant v; QVariant v;
v.setValue(pack); v.setValue(pack);
return v; return v;
@ -144,11 +161,37 @@ void FtbListModel::fill(FtbModpackList modpacks)
endResetModel(); endResetModel();
} }
void FtbListModel::addPack(FtbModpack modpack)
{
beginResetModel();
this->modpacks.append(modpack);
endResetModel();
}
void FtbListModel::clear()
{
beginResetModel();
modpacks.clear();
endResetModel();
}
FtbModpack FtbListModel::at(int row) FtbModpack FtbListModel::at(int row)
{ {
return modpacks.at(row); return modpacks.at(row);
} }
void FtbListModel::remove(int row)
{
if(row < 0 || row >= modpacks.size())
{
qWarning() << "Attempt to remove FTB modpacks with invalid row" << row;
return;
}
beginRemoveRows(QModelIndex(), row, row);
modpacks.removeAt(row);
endRemoveRows();
}
void FtbListModel::logoLoaded(QString logo, QIcon out) void FtbListModel::logoLoaded(QString logo, QIcon out)
{ {
m_loadingLogos.removeAll(logo); m_loadingLogos.removeAll(logo);
@ -164,7 +207,8 @@ void FtbListModel::logoFailed(QString logo)
void FtbListModel::requestLogo(QString file) void FtbListModel::requestLogo(QString file)
{ {
if(m_loadingLogos.contains(file) || m_failedLogos.contains(file)) { if(m_loadingLogos.contains(file) || m_failedLogos.contains(file))
{
return; return;
} }
@ -173,14 +217,17 @@ void FtbListModel::requestLogo(QString file)
job->addNetAction(Net::Download::makeCached(QUrl(QString("https://ftb.cursecdn.com/FTB2/static/%1").arg(file)), entry)); job->addNetAction(Net::Download::makeCached(QUrl(QString("https://ftb.cursecdn.com/FTB2/static/%1").arg(file)), entry));
auto fullPath = entry->getFullPath(); auto fullPath = entry->getFullPath();
QObject::connect(job, &NetJob::finished, this, [this, file, fullPath]{ QObject::connect(job, &NetJob::finished, this, [this, file, fullPath]
{
emit logoLoaded(file, QIcon(fullPath)); emit logoLoaded(file, QIcon(fullPath));
if(waitingCallbacks.contains(file)) { if(waitingCallbacks.contains(file))
{
waitingCallbacks.value(file)(fullPath); waitingCallbacks.value(file)(fullPath);
} }
}); });
QObject::connect(job, &NetJob::failed, this, [this, file]{ QObject::connect(job, &NetJob::failed, this, [this, file]
{
emit logoFailed(file); emit logoFailed(file);
}); });
@ -191,9 +238,17 @@ void FtbListModel::requestLogo(QString file)
void FtbListModel::getLogo(const QString &logo, LogoCallback callback) void FtbListModel::getLogo(const QString &logo, LogoCallback callback)
{ {
if(m_logoMap.contains(logo)) { if(m_logoMap.contains(logo))
{
callback(ENV.metacache()->resolveEntry("FTBPacks", QString("logos/%1").arg(logo.section(".", 0, 0)))->getFullPath()); callback(ENV.metacache()->resolveEntry("FTBPacks", QString("logos/%1").arg(logo.section(".", 0, 0)))->getFullPath());
} else { }
else
{
requestLogo(logo); requestLogo(logo);
} }
} }
Qt::ItemFlags FtbListModel::flags(const QModelIndex &index) const
{
return QAbstractListModel::flags(index);
}

View File

@ -7,6 +7,7 @@
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include <QThreadPool> #include <QThreadPool>
#include <QIcon> #include <QIcon>
#include <QStyledItemDelegate>
#include <functional> #include <functional>
@ -60,8 +61,12 @@ public:
int rowCount(const QModelIndex &parent) const override; int rowCount(const QModelIndex &parent) const override;
int columnCount(const QModelIndex &parent) const override; int columnCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const override; QVariant data(const QModelIndex &index, int role) const override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
void fill(FtbModpackList modpacks); void fill(FtbModpackList modpacks);
void addPack(FtbModpack modpack);
void clear();
void remove(int row);
FtbModpack at(int row); FtbModpack at(int row);
void getLogo(const QString &logo, LogoCallback callback); void getLogo(const QString &logo, LogoCallback callback);