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); 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() void AuthWidget::checkStatus()
{ {
if (!isEnabled()) if (!isEnabled())
@@ -90,22 +99,23 @@ void AuthWidget::checkStatus()
setState("Checking status ...", {}, true); setState("Checking status ...", {}, true);
m_client->requestCheckStatus(false, [this](const CheckStatusRequest::Response &response) { m_client->requestCheckStatus(
if (response.error()) { false, guardCallback(this, [this](const CheckStatusRequest::Response &response) {
setState("Failed to authenticate", response.error()->message(), false); if (response.error()) {
return; setState("Failed to authenticate", response.error()->message(), false);
} return;
const CheckStatusResponse result = *response.result(); }
const CheckStatusResponse result = *response.result();
if (result.user().isEmpty()) { if (result.user().isEmpty()) {
setState("Sign in", {}, false); setState("Sign in", {}, false);
m_status = Status::SignedOut; m_status = Status::SignedOut;
return; return;
} }
setState("Sign out " + result.user(), {}, false); setState("Sign out " + result.user(), {}, false);
m_status = Status::SignedIn; m_status = Status::SignedIn;
}); }));
} }
void AuthWidget::updateClient(const FilePath &nodeJs, const FilePath &agent) void AuthWidget::updateClient(const FilePath &nodeJs, const FilePath &agent)
@@ -136,35 +146,34 @@ void AuthWidget::signIn()
setState("Signing in ...", {}, true); setState("Signing in ...", {}, true);
m_client->requestSignInInitiate([this](const SignInInitiateRequest::Response &response) { m_client->requestSignInInitiate(
QTC_ASSERT(!response.error(), return); guardCallback(this, [this](const SignInInitiateRequest::Response &response) {
QTC_ASSERT(!response.error(), return);
Utils::setClipboardAndSelection(response.result()->userCode()); Utils::setClipboardAndSelection(response.result()->userCode());
QDesktopServices::openUrl(QUrl(response.result()->verificationUri())); QDesktopServices::openUrl(QUrl(response.result()->verificationUri()));
m_statusLabel->setText(Tr::tr("A browser window will open. Enter the code %1 when " m_statusLabel->setText(Tr::tr("A browser window will open. Enter the code %1 when "
"asked.\nThe code has been copied to your clipboard.") "asked.\nThe code has been copied to your clipboard.")
.arg(response.result()->userCode())); .arg(response.result()->userCode()));
m_statusLabel->setVisible(true); m_statusLabel->setVisible(true);
m_client m_client->requestSignInConfirm(
->requestSignInConfirm(response.result()->userCode(), response.result()->userCode(),
[this](const SignInConfirmRequest::Response &response) { guardCallback(this, [this](const SignInConfirmRequest::Response &response) {
if (response.error()) { if (response.error()) {
QMessageBox::critical(this, QMessageBox::critical(
Tr::tr("Login Failed"), this,
Tr::tr( Tr::tr("Login Failed"),
"The login request failed: %1") Tr::tr("The login request failed: %1").arg(response.error()->message()));
.arg(response.error() setState("Sign in", response.error()->message(), false);
->message())); return;
setState("Sign in", response.error()->message(), false); }
return;
}
setState("Sign Out " + response.result()->user(), {}, false); setState("Sign Out " + response.result()->user(), {}, false);
}); }));
}); }));
} }
void AuthWidget::signOut() void AuthWidget::signOut()
@@ -173,12 +182,12 @@ void AuthWidget::signOut()
setState("Signing out ...", {}, true); 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.error(), return);
QTC_ASSERT(response.result()->status() == "NotSignedIn", return); QTC_ASSERT(response.result()->status() == "NotSignedIn", return);
checkStatus(); checkStatus();
}); }));
} }
} // namespace Copilot } // namespace Copilot