Skip to content

Commit

Permalink
show more files in curseforge
Browse files Browse the repository at this point in the history
  • Loading branch information
kaniol-lck committed Jun 4, 2024
1 parent 30be938 commit 4ad2268
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 54 deletions.
16 changes: 12 additions & 4 deletions src/curseforge/curseforgeapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,17 @@ Reply<CurseforgeFileInfo> CurseforgeAPI::getFileInfo(int id, int FileID)
} };
}

Reply<QList<CurseforgeFileInfo> > CurseforgeAPI::getFiles(int id)
Reply<QList<CurseforgeFileInfo>, int> CurseforgeAPI::getFiles(int id, int index)
{
//TODO: page
QUrl url = PREFIX + "/v1/mods/" + QString::number(id) + "/files";
//url query
QUrlQuery urlQuery;
//index
urlQuery.addQueryItem("index", QString::number(index));
//page size
urlQuery.addQueryItem("pageSize", QString::number(Config().getSearchResultCount()));
url.setQuery(urlQuery);

QNetworkRequest request(url);
request.setRawHeader("x-api-key",XAPIKEY);
Expand All @@ -168,15 +175,16 @@ Reply<QList<CurseforgeFileInfo> > CurseforgeAPI::getFiles(int id)
QJsonDocument jsonDocument = QJsonDocument::fromJson(reply->readAll(), &error);
if (error.error != QJsonParseError::NoError) {
qDebug("%s", error.errorString().toUtf8().constData());
return QList<CurseforgeFileInfo>{};
return std::make_pair(QList<CurseforgeFileInfo>{}, 0);
}
auto resultList = value(jsonDocument.toVariant(),"data").toList();
auto resultList = value(jsonDocument.toVariant(), "data").toList();
auto totalCount = value(jsonDocument.toVariant(), "pagination", "totalCount").toInt();

QList<CurseforgeFileInfo> fileInfoList;
for(const auto &result : qAsConst(resultList))
fileInfoList << CurseforgeFileInfo::fromVariant(result);

return fileInfoList;
return std::make_pair(fileInfoList, totalCount);
} };
}

Expand Down
2 changes: 1 addition & 1 deletion src/curseforge/curseforgeapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CurseforgeAPI : public QObject
Reply<QString> getChangelog(int id, int FileID);
Reply<QString> getDownloadUrl(int id, int FileID);
Reply<CurseforgeFileInfo> getFileInfo(int id, int FileID);
Reply<QList<CurseforgeFileInfo> > getFiles(int id);
Reply<QList<CurseforgeFileInfo>, int> getFiles(int id, int index);
Reply<CurseforgeModInfo> getInfo(int id);
Reply<QString> getTimestamp();
Reply<QList<GameVersion> > getMinecraftVersionList();
Expand Down
14 changes: 7 additions & 7 deletions src/curseforge/curseforgemod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ void CurseforgeMod::acquireDescription()
});
}

std::shared_ptr<Reply<QList<CurseforgeFileInfo>>> CurseforgeMod::acquireAllFileList()
std::shared_ptr<Reply<QList<CurseforgeFileInfo>, int>> CurseforgeMod::acquireMoreFileList()
{
if(allFileListGetter_ && allFileListGetter_->isRunning()) return allFileListGetter_;
qDebug()<<modInfo_.id()<<modInfo_.name();
allFileListGetter_ = api_->getFiles(modInfo_.id()).asUnique();
allFileListGetter_->setOnFinished(this, [=](const QList<CurseforgeFileInfo> &fileList){
modInfo_.allFileList_ = fileList;
emit allFileListReady(fileList);
allFileListGetter_ = api_->getFiles(modInfo_.id(), modInfo().allFileList().size()).asUnique();
allFileListGetter_->setOnFinished(this, [=](const QList<CurseforgeFileInfo> &fileList, int count){
modInfo_.allFileList_ << fileList;
modInfo_.totalFileCount_ = count;
emit moreFileListReady(fileList);
}, [=](auto){
emit allFileListReady({});
emit moreFileListReady({});
});
return allFileListGetter_;
}
Expand Down
6 changes: 3 additions & 3 deletions src/curseforge/curseforgemod.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class CurseforgeMod : public QObject, public Tagable
void acquireBasicInfo();
void acquireIcon();
void acquireDescription();
std::shared_ptr<Reply<QList<CurseforgeFileInfo>>> acquireAllFileList();
std::shared_ptr<Reply<QList<CurseforgeFileInfo>, int>> acquireMoreFileList();

const CurseforgeModInfo &modInfo() const;
void download(const CurseforgeFileInfo &fileInfo, LocalModPath *downloadPath = nullptr);
Expand All @@ -38,7 +38,7 @@ class CurseforgeMod : public QObject, public Tagable
void basicInfoReady();
void iconReady();
void descriptionReady();
void allFileListReady(QList<CurseforgeFileInfo> fileInfos);
void moreFileListReady(QList<CurseforgeFileInfo> fileInfos);

void downloadStarted();

Expand All @@ -50,7 +50,7 @@ class CurseforgeMod : public QObject, public Tagable
std::shared_ptr<Reply<CurseforgeModInfo>> basicInfoGetter_;
bool gettingIcon_ = false;
std::shared_ptr<Reply<QString>> descriptionGetter_;
std::shared_ptr<Reply<QList<CurseforgeFileInfo>>> allFileListGetter_;
std::shared_ptr<Reply<QList<CurseforgeFileInfo>, int>> allFileListGetter_;
};

#endif // CURSEFORGEMOD_H
15 changes: 15 additions & 0 deletions src/curseforge/curseforgemodinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,21 @@ bool CurseforgeModInfo::hasBasicInfo() const
return basicInfo_;
}

bool CurseforgeModInfo::fileCompleted() const
{
return allFileList_.size() != 0 && allFileList_.size() == totalFileCount_;
}

int CurseforgeModInfo::totalFileCount() const
{
return totalFileCount_;
}

void CurseforgeModInfo::setTotalFileCount(int newTotalFileCount)
{
totalFileCount_ = newTotalFileCount;
}

const QList<CurseforgeCategoryInfo> &CurseforgeModInfo::categories() const
{
return categories_;
Expand Down
5 changes: 5 additions & 0 deletions src/curseforge/curseforgemodinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class CurseforgeModInfo : public CurseforgeModCacheInfo, Tagable
void setLatestFiles(const QList<CurseforgeFileInfo> &newLatestFiles);
bool hasBasicInfo() const;

bool fileCompleted() const;
int totalFileCount() const;
void setTotalFileCount(int newTotalFileCount);

private:
QUrl websiteUrl_;
QList<Attachment> images_;
Expand All @@ -64,6 +68,7 @@ class CurseforgeModInfo : public CurseforgeModCacheInfo, Tagable
QList<ModLoaderType::Type> loaderTypes_;
QList<CurseforgeFileInfo> latestFileList_;
QList<CurseforgeFileInfo> allFileList_;
int totalFileCount_ = 0;
QList<CurseforgeCategoryInfo> categories_;
QDateTime dateModified_;
QDateTime dateCreated_;
Expand Down
9 changes: 5 additions & 4 deletions src/local/localmod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void LocalMod::setCurseforgeMod(CurseforgeMod *newCurseforgeMod)
removeSubTagable(modrinthMod_);
curseforgeMod_ = newCurseforgeMod;
if(curseforgeMod_){
connect(curseforgeMod_, &CurseforgeMod::allFileListReady, this, &LocalMod::setCurseforgeFileInfos);
connect(curseforgeMod_, &CurseforgeMod::moreFileListReady, this, &LocalMod::setCurseforgeFileInfos);
addSubTagable(curseforgeMod_);
curseforgeMod_->setParent(this);
curseforgeMod_->acquireBasicInfo();
Expand All @@ -114,7 +114,7 @@ void LocalMod::setCurseforgeId(int id, bool cache)
if(id != 0){
curseforgeMod_ = new CurseforgeMod(this, id);
addSubTagable(curseforgeMod_);
connect(curseforgeMod_, &CurseforgeMod::allFileListReady, this, &LocalMod::setCurseforgeFileInfos);
connect(curseforgeMod_, &CurseforgeMod::moreFileListReady, this, &LocalMod::setCurseforgeFileInfos);
emit curseforgeReady(true);
emit modInfoChanged();
emit modCacheUpdated();
Expand Down Expand Up @@ -162,8 +162,9 @@ void LocalMod::checkCurseforgeUpdate(bool force)
emit checkCurseforgeUpdateStarted();
//update file list
if(force || curseforgeMod_->modInfo().allFileList().isEmpty()){
curseforgeFileListGetter_ = curseforgeMod_->acquireAllFileList();
curseforgeFileListGetter_->setOnFinished(this, [=](const QList<CurseforgeFileInfo> &){
//TODO
curseforgeFileListGetter_ = curseforgeMod_->acquireMoreFileList();
curseforgeFileListGetter_->setOnFinished(this, [=](const QList<CurseforgeFileInfo> &, int count){
curseforgeUpdater_.findUpdate(modFile_->linker()->curseforgeFileInfo());
emit curseforgeUpdateReady(true);
}, [=](auto){
Expand Down
2 changes: 1 addition & 1 deletion src/local/localmod.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public slots:
Updater<Modrinth> modrinthUpdater_;

CheckSheet *updateChecker_;
std::shared_ptr<Reply<QList<CurseforgeFileInfo> > > curseforgeFileListGetter_;
std::shared_ptr<Reply<QList<CurseforgeFileInfo>, int> > curseforgeFileListGetter_;
std::shared_ptr<Reply<QList<ModrinthFileInfo> > > modrinthFileListGetter_;

//dependencies
Expand Down
20 changes: 16 additions & 4 deletions src/ui/curseforge/curseforgefilelistwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ CurseforgeFileListWidget::CurseforgeFileListWidget(CurseforgeModBrowser *parent)
ui->fileListView->setVerticalScrollBar(new SmoothScrollBar(this));
ui->fileListView->setProperty("class", "ModList");
connect(ui->fileListView->verticalScrollBar(), &QAbstractSlider::valueChanged, this , &CurseforgeFileListWidget::updateIndexWidget);
connect(ui->fileListView->verticalScrollBar(), &QSlider::valueChanged, this, &CurseforgeFileListWidget::onListSliderChanged);
ui->downloadPathSelect->hide();
downloadPathSelectMenu_ = parent->downloadPathSelectMenu();
}
Expand All @@ -33,7 +34,8 @@ CurseforgeFileListWidget::CurseforgeFileListWidget(QWidget *parent, LocalMod *lo
ui->fileListView->setModel(&model_);
ui->fileListView->setVerticalScrollBar(new SmoothScrollBar(this));
ui->fileListView->setProperty("class", "ModList");
connect(ui->fileListView->verticalScrollBar(), &QAbstractSlider::valueChanged, this , &CurseforgeFileListWidget::updateIndexWidget);
connect(ui->fileListView->verticalScrollBar(), &QAbstractSlider::valueChanged, this, &CurseforgeFileListWidget::updateIndexWidget);
connect(ui->fileListView->verticalScrollBar(), &QSlider::valueChanged, this, &CurseforgeFileListWidget::onListSliderChanged);
ui->downloadPathSelect->hide();
downloadPathSelectMenu_ = new DownloadPathSelectMenu(this);
ui->downloadPathSelect->setDefaultAction(downloadPathSelectMenu_->menuAction());
Expand All @@ -53,13 +55,13 @@ void CurseforgeFileListWidget::setMod(CurseforgeMod *mod)
ui->fileListView->setVisible(mod_);
if(!mod_) return;
connect(this, &CurseforgeFileListWidget::modChanged, disconnecter(
connect(mod_, &CurseforgeMod::allFileListReady, this, &CurseforgeFileListWidget::updateFileList),
connect(mod_, &CurseforgeMod::moreFileListReady, this, &CurseforgeFileListWidget::updateFileList),
connect(mod_, &QObject::destroyed, this, [=]{ setMod(nullptr); })));

updateFileList();
if(mod_->modInfo().allFileList().isEmpty()){
if(!mod_->modInfo().fileCompleted()){
ui->fileListView->setCursor(Qt::BusyCursor);
mod->acquireAllFileList();
mod_->acquireMoreFileList();
}
}

Expand Down Expand Up @@ -130,3 +132,13 @@ void CurseforgeFileListWidget::updateIndexWidget()
item->setSizeHint(QSize(0, itemWidget->height()));
}
}

void CurseforgeFileListWidget::onListSliderChanged(int i)
{
if(i >= ui->fileListView->verticalScrollBar()->maximum() - 1000){
if(!mod_->modInfo().fileCompleted()){
ui->fileListView->setCursor(Qt::BusyCursor);
mod_->acquireMoreFileList();
}
}
}
1 change: 1 addition & 0 deletions src/ui/curseforge/curseforgefilelistwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public slots:
private slots:
void updateFileList();
void updateIndexWidget();
void onListSliderChanged(int i);

protected:
void paintEvent(QPaintEvent *event) override;
Expand Down
29 changes: 1 addition & 28 deletions src/ui/tagsflowwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,9 @@ TagsFlowWidget::TagsFlowWidget(QWidget *parent) :
setContextMenuPolicy(Qt::CustomContextMenu);
}

void TagsFlowWidget::setTagable(const Tagable &tagable)
{
//tags
for(auto widget : qAsConst(tagWidgets_)){
layout()->removeWidget(widget);
widget->deleteLater();
}
tagWidgets_.clear();
for(auto &&tag : tagable.tags(Config().getShowTagCategories())){
auto label = new QLabel(this);
if(!tag.iconName().isEmpty())
label->setText(QString(R"(<img src="%1" height="16" width="16"/> %2)").arg(tag.iconName(), tag.name()));
else
label->setText(tag.name());
label->setToolTip(tr("%1: %2").arg(tag.category().name(), tag.name()));
label->setStyleSheet(QString("color: #fff; background-color: %1; border-radius:10px; padding:2px 4px;").arg(tag.category().color().name()));
layout()->addWidget(label);
tagWidgets_ << label;
}
}

void TagsFlowWidget::updateUi()
{
//tags
for(auto widget : qAsConst(tagWidgets_)){
layout()->removeWidget(widget);
widget->deleteLater();
}
tagWidgets_.clear();
if(!tagableObject_) return;
if(!tagableObject_) return;
for(auto &&tag : tagableObject_->tags(Config().getShowTagCategories())){
auto label = new QLabel(this);
if(!tag.iconName().isEmpty())
Expand Down
2 changes: 0 additions & 2 deletions src/ui/tagsflowwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ class TagsFlowWidget : public QWidget
public:
explicit TagsFlowWidget(QWidget *parent = nullptr);

void setTagable(const Tagable &tagable);

template<typename T>
void setTagableObject(T* tagableObject)
{
Expand Down

0 comments on commit 4ad2268

Please sign in to comment.