Copilot: Fix crash when closing settings while checking state

Change-Id: I06a9df91690fcdea8f30e9f9f6b5a38dfe19c8ea
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Marcus Tillmanns
2024-07-29 09:11:40 +02:00
parent e75b9f32a9
commit 8c91d12ca1

View File

@@ -81,6 +81,15 @@ void AuthWidget::setState(const QString &buttonText, const QString &errorText, b
m_button->setEnabled(!working);
}
template<class O, class F>
auto guardCallback(O *guardObject, const F &method)
{
return [gp = QPointer<O>(guardObject), method](auto &&...args) {
if (gp)
method(std::forward<decltype(args)>(args)...);
};
}
void AuthWidget::checkStatus()
{
if (!isEnabled())
@@ -90,7 +99,8 @@ void AuthWidget::checkStatus()
setState("Checking status ...", {}, true);
m_client->requestCheckStatus(false, [this](const CheckStatusRequest::Response &response) {
m_client->requestCheckStatus(
false, guardCallback(this, [this](const CheckStatusRequest::Response &response) {
if (response.error()) {
setState("Failed to authenticate", response.error()->message(), false);
return;
@@ -105,7 +115,7 @@ void AuthWidget::checkStatus()
setState("Sign out " + result.user(), {}, false);
m_status = Status::SignedIn;
});
}));
}
void AuthWidget::updateClient(const FilePath &nodeJs, const FilePath &agent)
@@ -136,7 +146,8 @@ void AuthWidget::signIn()
setState("Signing in ...", {}, true);
m_client->requestSignInInitiate([this](const SignInInitiateRequest::Response &response) {
m_client->requestSignInInitiate(
guardCallback(this, [this](const SignInInitiateRequest::Response &response) {
QTC_ASSERT(!response.error(), return);
Utils::setClipboardAndSelection(response.result()->userCode());
@@ -148,23 +159,21 @@ void AuthWidget::signIn()
.arg(response.result()->userCode()));
m_statusLabel->setVisible(true);
m_client
->requestSignInConfirm(response.result()->userCode(),
[this](const SignInConfirmRequest::Response &response) {
m_client->requestSignInConfirm(
response.result()->userCode(),
guardCallback(this, [this](const SignInConfirmRequest::Response &response) {
if (response.error()) {
QMessageBox::critical(this,
QMessageBox::critical(
this,
Tr::tr("Login Failed"),
Tr::tr(
"The login request failed: %1")
.arg(response.error()
->message()));
Tr::tr("The login request failed: %1").arg(response.error()->message()));
setState("Sign in", response.error()->message(), false);
return;
}
setState("Sign Out " + response.result()->user(), {}, false);
});
});
}));
}));
}
void AuthWidget::signOut()
@@ -173,12 +182,12 @@ void AuthWidget::signOut()
setState("Signing out ...", {}, true);
m_client->requestSignOut([this](const SignOutRequest::Response &response) {
m_client->requestSignOut(guardCallback(this, [this](const SignOutRequest::Response &response) {
QTC_ASSERT(!response.error(), return);
QTC_ASSERT(response.result()->status() == "NotSignedIn", return);
checkStatus();
});
}));
}
} // namespace Copilot