aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/copilot
diff options
context:
space:
mode:
authorMarcus Tillmanns <[email protected]>2024-07-29 09:11:40 +0200
committerMarcus Tillmanns <[email protected]>2024-07-30 12:03:33 +0000
commit8c91d12ca123d131266dffa19923f7aad718bc77 (patch)
treee1bdf67cc4388b56cb3fb96be4558ba0c1e9e330 /src/plugins/copilot
parente75b9f32a9fd54476d75abfa35df67cf07aeac80 (diff)
Copilot: Fix crash when closing settings while checking state
Diffstat (limited to 'src/plugins/copilot')
-rw-r--r--src/plugins/copilot/authwidget.cpp103
1 files changed, 56 insertions, 47 deletions
diff --git a/src/plugins/copilot/authwidget.cpp b/src/plugins/copilot/authwidget.cpp
index 0b5cfd2d93d..ce2029c022f 100644
--- a/src/plugins/copilot/authwidget.cpp
+++ b/src/plugins/copilot/authwidget.cpp
@@ -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,22 +99,23 @@ void AuthWidget::checkStatus()
setState("Checking status ...", {}, true);
- m_client->requestCheckStatus(false, [this](const CheckStatusRequest::Response &response) {
- if (response.error()) {
- setState("Failed to authenticate", response.error()->message(), false);
- return;
- }
- const CheckStatusResponse result = *response.result();
-
- if (result.user().isEmpty()) {
- setState("Sign in", {}, false);
- m_status = Status::SignedOut;
- return;
- }
-
- setState("Sign out " + result.user(), {}, false);
- m_status = Status::SignedIn;
- });
+ m_client->requestCheckStatus(
+ false, guardCallback(this, [this](const CheckStatusRequest::Response &response) {
+ if (response.error()) {
+ setState("Failed to authenticate", response.error()->message(), false);
+ return;
+ }
+ const CheckStatusResponse result = *response.result();
+
+ if (result.user().isEmpty()) {
+ setState("Sign in", {}, false);
+ m_status = Status::SignedOut;
+ return;
+ }
+
+ setState("Sign out " + result.user(), {}, false);
+ m_status = Status::SignedIn;
+ }));
}
void AuthWidget::updateClient(const FilePath &nodeJs, const FilePath &agent)
@@ -136,35 +146,34 @@ void AuthWidget::signIn()
setState("Signing in ...", {}, true);
- m_client->requestSignInInitiate([this](const SignInInitiateRequest::Response &response) {
- QTC_ASSERT(!response.error(), return);
-
- Utils::setClipboardAndSelection(response.result()->userCode());
-
- QDesktopServices::openUrl(QUrl(response.result()->verificationUri()));
-
- m_statusLabel->setText(Tr::tr("A browser window will open. Enter the code %1 when "
- "asked.\nThe code has been copied to your clipboard.")
- .arg(response.result()->userCode()));
- m_statusLabel->setVisible(true);
-
- m_client
- ->requestSignInConfirm(response.result()->userCode(),
- [this](const SignInConfirmRequest::Response &response) {
- if (response.error()) {
- QMessageBox::critical(this,
- Tr::tr("Login Failed"),
- 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);
- });
- });
+ m_client->requestSignInInitiate(
+ guardCallback(this, [this](const SignInInitiateRequest::Response &response) {
+ QTC_ASSERT(!response.error(), return);
+
+ Utils::setClipboardAndSelection(response.result()->userCode());
+
+ QDesktopServices::openUrl(QUrl(response.result()->verificationUri()));
+
+ m_statusLabel->setText(Tr::tr("A browser window will open. Enter the code %1 when "
+ "asked.\nThe code has been copied to your clipboard.")
+ .arg(response.result()->userCode()));
+ m_statusLabel->setVisible(true);
+
+ m_client->requestSignInConfirm(
+ response.result()->userCode(),
+ guardCallback(this, [this](const SignInConfirmRequest::Response &response) {
+ if (response.error()) {
+ QMessageBox::critical(
+ this,
+ Tr::tr("Login Failed"),
+ 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