Fixed leaks

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97
2023-06-18 23:27:26 +03:00
parent 147366bc0a
commit 0161520b33
10 changed files with 164 additions and 229 deletions

View File

@ -35,14 +35,15 @@
#include "ListModel.h"
#include "Application.h"
#include "QObjectPtr.h"
#include "net/HttpMetaCache.h"
#include "net/NetJob.h"
#include "StringUtils.h"
#include <Version.h>
#include "StringUtils.h"
#include <QtMath>
#include <QLabel>
#include <QtMath>
#include <RWStorage.h>
@ -50,33 +51,33 @@
namespace LegacyFTB {
FilterModel::FilterModel(QObject *parent) : QSortFilterProxyModel(parent)
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);
}
bool FilterModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
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 (currentSorting == Sorting::ByGameVersion) {
Version lv(leftPack.mcVersion);
Version rv(rightPack.mcVersion);
return lv < rv;
} else if(currentSorting == Sorting::ByName) {
} else if (currentSorting == Sorting::ByName) {
return StringUtils::naturalCompare(leftPack.name, rightPack.name, Qt::CaseSensitive) >= 0;
}
//UHM, some inavlid value set?!
// UHM, some inavlid value set?!
qWarning() << "Invalid sorting set!";
return true;
}
bool FilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
bool FilterModel::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
{
return true;
}
@ -102,18 +103,13 @@ FilterModel::Sorting FilterModel::getCurrentSorting()
return currentSorting;
}
ListModel::ListModel(QObject *parent) : QAbstractListModel(parent)
{
}
ListModel::ListModel(QObject* parent) : QAbstractListModel(parent) {}
ListModel::~ListModel()
{
}
ListModel::~ListModel() {}
QString ListModel::translatePackType(PackType type) const
{
switch(type)
{
switch (type) {
case PackType::Public:
return tr("Public Modpack");
case PackType::ThirdParty:
@ -125,67 +121,51 @@ QString ListModel::translatePackType(PackType type) const
return QString();
}
int ListModel::rowCount(const QModelIndex &parent) const
int ListModel::rowCount(const QModelIndex& parent) const
{
return parent.isValid() ? 0 : modpacks.size();
}
int ListModel::columnCount(const QModelIndex &parent) const
int ListModel::columnCount(const QModelIndex& parent) const
{
return parent.isValid() ? 0 : 1;
}
QVariant ListModel::data(const QModelIndex &index, int role) const
QVariant ListModel::data(const QModelIndex& index, int role) const
{
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);
}
Modpack pack = modpacks.at(pos);
if(role == Qt::DisplayRole)
{
if (role == Qt::DisplayRole) {
return pack.name + "\n" + translatePackType(pack.type);
}
else if (role == Qt::ToolTipRole)
{
if(pack.description.length() > 100)
{
//some magic to prevent to long tooltips and replace html linebreaks
} else if (role == Qt::ToolTipRole) {
if (pack.description.length() > 100) {
// some magic to prevent to long tooltips and replace html linebreaks
QString edit = pack.description.left(97);
edit = edit.left(edit.lastIndexOf("<br>")).left(edit.lastIndexOf(" ")).append("...");
return edit;
}
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));
}
QIcon icon = APPLICATION->getThemedIcon("screenshot-placeholder");
((ListModel *)this)->requestLogo(pack.logo);
((ListModel*)this)->requestLogo(pack.logo);
return icon;
}
else if(role == Qt::ForegroundRole)
{
if(pack.broken)
{
//FIXME: Hardcoded color
} else if (role == Qt::ForegroundRole) {
if (pack.broken) {
// FIXME: Hardcoded color
return QColor(255, 0, 50);
}
else if(pack.bugged)
{
//FIXME: Hardcoded color
//bugged pack, currently only indicates bugged xml
} else if (pack.bugged) {
// FIXME: Hardcoded color
// bugged pack, currently only indicates bugged xml
return QColor(244, 229, 66);
}
}
else if(role == Qt::UserRole)
{
} else if (role == Qt::UserRole) {
QVariant v;
v.setValue(pack);
return v;
@ -222,8 +202,7 @@ Modpack ListModel::at(int row)
void ListModel::remove(int row)
{
if(row < 0 || row >= modpacks.size())
{
if (row < 0 || row >= modpacks.size()) {
qWarning() << "Attempt to remove FTB modpacks with invalid row" << row;
return;
}
@ -247,27 +226,25 @@ void ListModel::logoFailed(QString logo)
void ListModel::requestLogo(QString file)
{
if(m_loadingLogos.contains(file) || m_failedLogos.contains(file))
{
if (m_loadingLogos.contains(file) || m_failedLogos.contains(file)) {
return;
}
MetaEntryPtr entry = APPLICATION->metacache()->resolveEntry("FTBPacks", QString("logos/%1").arg(file.section(".", 0, 0)));
NetJob *job = new NetJob(QString("FTB Icon Download for %1").arg(file), APPLICATION->network());
NetJob* job = new NetJob(QString("FTB Icon Download for %1").arg(file), APPLICATION->network());
job->addNetAction(Net::Download::makeCached(QUrl(QString(BuildConfig.LEGACY_FTB_CDN_BASE_URL + "static/%1").arg(file)), entry));
auto fullPath = entry->getFullPath();
QObject::connect(job, &NetJob::finished, this, [this, file, fullPath]
{
QObject::connect(job, &NetJob::finished, this, [this, file, fullPath, job] {
job->deleteLater();
emit logoLoaded(file, QIcon(fullPath));
if(waitingCallbacks.contains(file))
{
if (waitingCallbacks.contains(file)) {
waitingCallbacks.value(file)(fullPath);
}
});
QObject::connect(job, &NetJob::failed, this, [this, file]
{
QObject::connect(job, &NetJob::failed, this, [this, file, job] {
job->deleteLater();
emit logoFailed(file);
});
@ -276,21 +253,18 @@ void ListModel::requestLogo(QString file)
m_loadingLogos.append(file);
}
void ListModel::getLogo(const QString &logo, LogoCallback callback)
void ListModel::getLogo(const QString& logo, LogoCallback callback)
{
if(m_logoMap.contains(logo))
{
if (m_logoMap.contains(logo)) {
callback(APPLICATION->metacache()->resolveEntry("FTBPacks", QString("logos/%1").arg(logo.section(".", 0, 0)))->getFullPath());
}
else
{
} else {
requestLogo(logo);
}
}
Qt::ItemFlags ListModel::flags(const QModelIndex &index) const
Qt::ItemFlags ListModel::flags(const QModelIndex& index) const
{
return QAbstractListModel::flags(index);
}
}
} // namespace LegacyFTB