Copilot: robustify authentication widget

- fix initial state
- prevent accessing unreachable servers

Change-Id: Ia8d66ad215b864b672d5a1d1651123ca5848effa
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
David Schulz
2023-03-06 19:37:55 +01:00
parent d558741985
commit 2fc801e153

View File

@@ -15,22 +15,24 @@
#include <QMessageBox> #include <QMessageBox>
using namespace LanguageClient; using namespace LanguageClient;
using namespace Copilot::Internal;
namespace Copilot { namespace Copilot {
bool isCopilotClient(Client *client) bool isCopilotClient(Client *client)
{ {
return dynamic_cast<Internal::CopilotClient *>(client) != nullptr; return dynamic_cast<CopilotClient *>(client) != nullptr;
} }
Internal::CopilotClient *coPilotClient(Client *client) CopilotClient *asCoPilotClient(Client *client)
{ {
return static_cast<Internal::CopilotClient *>(client); return static_cast<CopilotClient *>(client);
} }
Internal::CopilotClient *findClient() Internal::CopilotClient *findClient()
{ {
return Internal::CopilotClient::instance(); CopilotClient *client = Internal::CopilotClient::instance();
return client && client->reachable() ? client : nullptr;
} }
AuthWidget::AuthWidget(QWidget *parent) AuthWidget::AuthWidget(QWidget *parent)
@@ -38,9 +40,12 @@ AuthWidget::AuthWidget(QWidget *parent)
{ {
using namespace Utils::Layouting; using namespace Utils::Layouting;
m_button = new QPushButton(); m_button = new QPushButton(Tr::tr("Sign in"));
m_button->setEnabled(false);
m_progressIndicator = new Utils::ProgressIndicator(Utils::ProgressIndicatorSize::Small); m_progressIndicator = new Utils::ProgressIndicator(Utils::ProgressIndicatorSize::Small);
m_progressIndicator->setVisible(false);
m_statusLabel = new QLabel(); m_statusLabel = new QLabel();
m_statusLabel->setVisible(false);
// clang-format off // clang-format off
Column { Column {
@@ -51,8 +56,6 @@ AuthWidget::AuthWidget(QWidget *parent)
}.attachTo(this); }.attachTo(this);
// clang-format on // clang-format on
setState("Checking status ...", true);
connect(LanguageClientManager::instance(), connect(LanguageClientManager::instance(),
&LanguageClientManager::clientAdded, &LanguageClientManager::clientAdded,
this, this,
@@ -65,7 +68,7 @@ AuthWidget::AuthWidget(QWidget *parent)
signIn(); signIn();
}); });
auto client = findClient(); CopilotClient *client = findClient();
if (client) if (client)
checkStatus(client); checkStatus(client);
@@ -74,20 +77,30 @@ AuthWidget::AuthWidget(QWidget *parent)
void AuthWidget::setState(const QString &buttonText, bool working) void AuthWidget::setState(const QString &buttonText, bool working)
{ {
m_button->setText(buttonText); m_button->setText(buttonText);
m_button->setVisible(true);
m_progressIndicator->setVisible(working); m_progressIndicator->setVisible(working);
m_statusLabel->setVisible(!m_statusLabel->text().isEmpty()); m_statusLabel->setVisible(!m_statusLabel->text().isEmpty());
m_button->setEnabled(!working); m_button->setEnabled(!working);
} }
void AuthWidget::onClientAdded(LanguageClient::Client *client) void AuthWidget::onClientAdded(Client *client)
{ {
if (isCopilotClient(client)) { if (isCopilotClient(client)) {
checkStatus(coPilotClient(client)); auto coPilotClient = asCoPilotClient(client);
if (coPilotClient->reachable()) {
checkStatus(coPilotClient);
} else {
connect(client, &Client::initialized, this, [this, coPilotClient] {
checkStatus(coPilotClient);
});
}
} }
} }
void AuthWidget::checkStatus(Internal::CopilotClient *client) void AuthWidget::checkStatus(CopilotClient *client)
{ {
setState("Checking status ...", true);
client->requestCheckStatus(false, [this](const CheckStatusRequest::Response &response) { client->requestCheckStatus(false, [this](const CheckStatusRequest::Response &response) {
if (response.error()) { if (response.error()) {
setState("failed: " + response.error()->message(), false); setState("failed: " + response.error()->message(), false);