Skip to content

Commit

Permalink
add update check cancelling (still problematic) (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaniol-lck committed Nov 27, 2021
1 parent ad636cd commit c95828e
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/curseforge/curseforgemod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ void CurseforgeMod::acquireDescription()
});
}

void CurseforgeMod::acquireAllFileList(std::function<void (QList<CurseforgeFileInfo>)> callback, std::function<void ()> failed)
QMetaObject::Connection CurseforgeMod::acquireAllFileList(std::function<void (QList<CurseforgeFileInfo>)> callback, std::function<void ()> failed)
{
if(gettingAllFileList_) return;
if(gettingAllFileList_) return {};
gettingAllFileList_ = true;
auto conn = api_->getFiles(modInfo_.id(), [=](const QList<CurseforgeFileInfo> &fileList){
gettingAllFileList_ = false;
Expand All @@ -104,6 +104,7 @@ void CurseforgeMod::acquireAllFileList(std::function<void (QList<CurseforgeFileI
connect(this, &QObject::destroyed, this, [=]{
disconnect(conn);
});
return conn;
}

const CurseforgeModInfo &CurseforgeMod::modInfo() const
Expand Down
2 changes: 1 addition & 1 deletion src/curseforge/curseforgemod.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CurseforgeMod : public QObject
void acquireBasicInfo();
void acquireIcon();
void acquireDescription();
void acquireAllFileList(std::function<void (QList<CurseforgeFileInfo>)> callback = [](QList<CurseforgeFileInfo>){}, std::function<void ()> failed = []{});
QMetaObject::Connection acquireAllFileList(std::function<void (QList<CurseforgeFileInfo>)> callback = [](QList<CurseforgeFileInfo>){}, std::function<void ()> failed = []{});

const CurseforgeModInfo &modInfo() const;
QList<Tag> tags() const;
Expand Down
22 changes: 16 additions & 6 deletions src/local/localmod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "util/tutil.hpp"
#include "config.hpp"
#include "util/mmlogger.h"
#include "util/funcutil.h"

LocalMod::LocalMod(QObject *parent, LocalModFile *file) :
QObject(parent),
Expand Down Expand Up @@ -166,18 +167,25 @@ void LocalMod::checkUpdates(bool force)
if(config.getUseCurseforgeUpdate() && curseforgeMod_ && curseforgeUpdate_.currentFileInfo()){
(*count)++;
checkCurseforgeUpdate(force);
connect(this, &LocalMod::curseforgeUpdateReady, foo);
connect(this, &LocalMod::checkCancelled, disconnecter(
connect(this, &LocalMod::curseforgeUpdateReady, foo)));
noSource = false;
}
if(config.getUseModrinthUpdate() && modrinthMod_ && modrinthUpdate_.currentFileInfo()){
(*count)++;
checkModrinthUpdate(force);
connect(this, &LocalMod::modrinthUpdateReady, foo);
connect(this, &LocalMod::checkCancelled, disconnecter(
connect(this, &LocalMod::modrinthUpdateReady, foo)));
noSource = false;
}
if(noSource) emit updateReady({});
}

void LocalMod::cancelChecking()
{
emit checkCancelled();
}

void LocalMod::checkCurseforgeUpdate(bool force)
{
if(!curseforgeMod_){
Expand All @@ -189,12 +197,13 @@ void LocalMod::checkCurseforgeUpdate(bool force)

//update file list
if(force || curseforgeMod_->modInfo().allFileList().isEmpty()){
curseforgeMod_->acquireAllFileList([=](const QList<CurseforgeFileInfo> &fileList){
connect(this, &LocalMod::checkCancelled, disconnecter(
curseforgeMod_->acquireAllFileList([=](const QList<CurseforgeFileInfo> &fileList){
bool bl = curseforgeUpdate_.findUpdate(fileList, targetVersion_, targetLoaderType_);
emit curseforgeUpdateReady(bl);
}, [=]{
emit curseforgeUpdateReady(false, false);
});
})));
}else{
bool bl = curseforgeUpdate_.findUpdate(curseforgeMod_->modInfo().allFileList(), targetVersion_, targetLoaderType_);
emit curseforgeUpdateReady(bl);
Expand All @@ -214,12 +223,13 @@ void LocalMod::checkModrinthUpdate(bool force)
bool bl = modrinthUpdate_.findUpdate(modrinthMod_->modInfo().fileList(), targetVersion_, targetLoaderType_);
emit modrinthUpdateReady(bl);
}else {
modrinthMod_->acquireFileList([=](const QList<ModrinthFileInfo> &fileList){
connect(this, &LocalMod::checkCancelled, disconnecter(
modrinthMod_->acquireFileList([=](const QList<ModrinthFileInfo> &fileList){
bool bl = modrinthUpdate_.findUpdate(fileList, targetVersion_, targetLoaderType_);
emit modrinthUpdateReady(bl);
}, [=]{
emit modrinthUpdateReady(false, false);
});
})));
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/local/localmod.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class LocalMod : public QObject
//update
bool perpareUpdate();
void checkUpdates(bool force = true);
void cancelChecking();
void checkCurseforgeUpdate(bool force = true);
void checkModrinthUpdate(bool force = true);

Expand Down Expand Up @@ -125,6 +126,7 @@ class LocalMod : public QObject
void checkWebsiteStarted();
void websiteReady(bool bl);
void checkUpdatesStarted();
void checkCancelled();
void updateReady(QList<ModWebsiteType> types, bool success = true);
void searchOnCurseforgeFinished(bool bl);
void searchOnModrinthFinished(bool bl);
Expand Down
18 changes: 14 additions & 4 deletions src/local/localmodpath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,7 @@ void LocalModPath::checkModUpdates(bool force) // force = true by default
auto failedCount = std::make_shared<int>(0);
for(auto &&map : modMaps())
for(const auto &mod : map){
//cancel on reloading
connect(this, &LocalModPath::loadStarted, disconnecter(
connect(mod, &LocalMod::updateReady, this, [=](QList<ModWebsiteType> types, bool success){
auto conn = connect(mod, &LocalMod::updateReady, this, [=](QList<ModWebsiteType> types, bool success){
(*checkedCount)++;
if(!types.isEmpty()) (*updateCount)++;
if(!success) (*failedCount) ++;
Expand All @@ -492,7 +490,13 @@ void LocalModPath::checkModUpdates(bool force) // force = true by default
isChecking_ = false;
emit updatesReady(*failedCount);
}
})));
});
//cancel on reloading
connect(this, &LocalModPath::loadStarted, disconnecter(conn));
connect(this, &LocalModPath::checkCancelled, [=]{
mod->cancelChecking();
disconnect(conn);
});
mod->checkUpdates();
}
} else if(interval != Config::Never){
Expand All @@ -502,6 +506,12 @@ void LocalModPath::checkModUpdates(bool force) // force = true by default
}
}

void LocalModPath::cancelChecking()
{
isChecking_ = false;
emit checkCancelled();
}

void LocalModPath::updateMods(QList<QPair<LocalMod *, ModWebsiteType> > modUpdateList)
{
if(modUpdateList.isEmpty()) return;
Expand Down
2 changes: 2 additions & 0 deletions src/local/localmodpath.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class LocalModPath : public QObject

void searchOnWebsites();
void checkModUpdates(bool force = true);
void cancelChecking();
void updateMods(QList<QPair<LocalMod *, ModWebsiteType> > modUpdateList);

//download new mod
Expand Down Expand Up @@ -66,6 +67,7 @@ class LocalModPath : public QObject
void loadStarted();
void loadProgress(int loadedCount, int totalCount);
void loadFinished();
void checkCancelled();

void checkWebsitesStarted();
void websiteCheckedCountUpdated(int checkedCount);
Expand Down
5 changes: 3 additions & 2 deletions src/modrinth/modrinthmod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ void ModrinthMod::acquireFullInfo()
});
}

void ModrinthMod::acquireFileList(std::function<void (QList<ModrinthFileInfo>)> callback, std::function<void ()> failed)
QMetaObject::Connection ModrinthMod::acquireFileList(std::function<void (QList<ModrinthFileInfo>)> callback, std::function<void ()> failed)
{
if(gettingFileList_) return;
if(gettingFileList_) return {};
gettingFileList_ = true;

auto conn = api_->getVersions(modInfo_.modId_, [=](const auto &files){
Expand All @@ -123,4 +123,5 @@ void ModrinthMod::acquireFileList(std::function<void (QList<ModrinthFileInfo>)>
connect(this, &QObject::destroyed, this, [=]{
disconnect(conn);
});
return conn;
}
2 changes: 1 addition & 1 deletion src/modrinth/modrinthmod.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ModrinthMod : public QObject
void acquireAuthor();
void acquireIcon();
void acquireFullInfo();
void acquireFileList(std::function<void (QList<ModrinthFileInfo>)> callback = [](QList<ModrinthFileInfo>){}, std::function<void ()> failed = []{});
QMetaObject::Connection acquireFileList(std::function<void (QList<ModrinthFileInfo>)> callback = [](QList<ModrinthFileInfo>){}, std::function<void ()> failed = []{});

signals:
void authorReady();
Expand Down
18 changes: 14 additions & 4 deletions src/ui/local/localmodbrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ LocalModBrowser::LocalModBrowser(QWidget *parent, LocalModPath *modPath) :
connect(modPath_, &LocalModPath::checkWebsitesStarted, this, &LocalModBrowser::onCheckWebsitesStarted);
connect(modPath_, &LocalModPath::websitesReady, this, &LocalModBrowser::onWebsitesReady);
connect(modPath_, &LocalModPath::checkUpdatesStarted, this, &LocalModBrowser::onCheckUpdatesStarted);
connect(modPath_, &LocalModPath::checkCancelled, this, &LocalModBrowser::onCheckCancelled);
connect(modPath_, &LocalModPath::updateCheckedCountUpdated, this, &LocalModBrowser::onUpdateCheckedCountUpdated);
connect(modPath_, &LocalModPath::updatesReady, this, &LocalModBrowser::onUpdatesReady);
connect(modPath_, &LocalModPath::updatableCountChanged, this, &LocalModBrowser::onUpdatableCountChanged);
Expand All @@ -161,6 +162,7 @@ LocalModBrowser::LocalModBrowser(QWidget *parent, LocalModPath *modPath) :
connect(modPath_, &LocalModPath::websiteCheckedCountUpdated, this, &LocalModBrowser::updateProgressBar);
connect(modPath_, &LocalModPath::websitesReady, this, &LocalModBrowser::updateProgressBar);
connect(modPath_, &LocalModPath::checkUpdatesStarted, this, &LocalModBrowser::updateProgressBar);
connect(modPath_, &LocalModPath::checkCancelled, this, &LocalModBrowser::updateProgressBar);
connect(modPath_, &LocalModPath::updatableCountChanged, this, &LocalModBrowser::updateProgressBar);
connect(modPath_, &LocalModPath::updatesReady, this, &LocalModBrowser::updateProgressBar);

Expand Down Expand Up @@ -281,13 +283,18 @@ void LocalModBrowser::onWebsitesReady()
void LocalModBrowser::onCheckUpdatesStarted()
{
isChecking_ = true;
ui->checkUpdatesButton->setEnabled(false);
// ui->checkUpdatesButton->setEnabled(false);
ui->updateWidget->setVisible(false);
onUpdateCheckedCountUpdated(0, 0, 0);
ui->checkUpdatesButton->setText(tr("Checking updates..."));
ui->checkUpdatesButton->setText(tr("Cancel Checking"));
progressBar_->setMaximum(modPath_->modCount());
}

void LocalModBrowser::onCheckCancelled()
{
onUpdatesReady();
}

void LocalModBrowser::onUpdateCheckedCountUpdated(int updateCount, int checkedCount, int totalCount)
{
if(!isChecking_) return;
Expand All @@ -299,7 +306,7 @@ void LocalModBrowser::onUpdatesReady(int failedCount)
{
isChecking_ = false;
if(isUpdating_) return;
ui->checkUpdatesButton->setEnabled(true);
// ui->checkUpdatesButton->setEnabled(true);
ui->checkUpdatesButton->setText(tr("Check updates"));
onUpdatableCountChanged();
updateStatusText();
Expand Down Expand Up @@ -433,7 +440,10 @@ void LocalModBrowser::on_comboBox_currentIndexChanged(int index)

void LocalModBrowser::on_checkUpdatesButton_clicked()
{
modPath_->checkModUpdates();
if(isChecking_)
modPath_->cancelChecking();
else
modPath_->checkModUpdates();
}

void LocalModBrowser::on_openFolderButton_clicked()
Expand Down
1 change: 1 addition & 0 deletions src/ui/local/localmodbrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ private slots:
void onWebsitesReady();

void onCheckUpdatesStarted();
void onCheckCancelled();
void onUpdateCheckedCountUpdated(int updateCount, int checkedCount, int totalCount);
void onUpdatesReady(int failedCount = 0);
void onUpdatableCountChanged();
Expand Down

0 comments on commit c95828e

Please sign in to comment.