aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/copilot/authwidget.cpp
blob: f29e3d3a15047449d9ebbe338a46f900d345e006 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "authwidget.h"

#include "copilotclient.h"
#include "copilottr.h"

#include <utils/layoutbuilder.h>
#include <utils/stringutils.h>

#include <languageclient/languageclientmanager.h>

#include <QDesktopServices>
#include <QMessageBox>

using namespace LanguageClient;
using namespace Copilot::Internal;

namespace Copilot {

AuthWidget::AuthWidget(QWidget *parent)
    : QWidget(parent)
{
    using namespace Layouting;

    m_button = new QPushButton(Tr::tr("Sign in"));
    m_button->setEnabled(false);
    m_progressIndicator = new Utils::ProgressIndicator(Utils::ProgressIndicatorSize::Small);
    m_progressIndicator->setVisible(false);
    m_statusLabel = new QLabel();
    m_statusLabel->setVisible(false);

    // clang-format off
    Column {
        Row {
            m_button, m_progressIndicator, st
        },
        m_statusLabel
    }.attachTo(this);
    // clang-format on

    connect(m_button, &QPushButton::clicked, this, [this]() {
        if (m_status == Status::SignedIn)
            signOut();
        else if (m_status == Status::SignedOut)
            signIn();
    });
}

AuthWidget::~AuthWidget()
{
    if (m_client)
        LanguageClientManager::shutdownClient(m_client);
}

void AuthWidget::setState(const QString &buttonText, bool working)
{
    m_button->setText(buttonText);
    m_button->setVisible(true);
    m_progressIndicator->setVisible(working);
    m_statusLabel->setVisible(!m_statusLabel->text().isEmpty());
    m_button->setEnabled(!working);
}

void AuthWidget::checkStatus()
{
    QTC_ASSERT(m_client && m_client->reachable(), return);

    setState("Checking status ...", true);

    m_client->requestCheckStatus(false, [this](const CheckStatusRequest::Response &response) {
        if (response.error()) {
            setState("failed: " + 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 Utils::FilePath &nodeJs, const Utils::FilePath &agent)
{
    LanguageClientManager::shutdownClient(m_client);
    m_client = nullptr;
    setState(Tr::tr("Sign in"), false);
    m_button->setEnabled(false);
    if (!nodeJs.isExecutableFile() || !agent.exists()) {
        return;
    }

    setState(Tr::tr("Sign in"), true);

    m_client = new CopilotClient(nodeJs, agent);
    connect(m_client, &Client::initialized, this, &AuthWidget::checkStatus);
}

void AuthWidget::signIn()
{
    qCritical() << "Not implemented";
    QTC_ASSERT(m_client && m_client->reachable(), return);

    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) {
                                       m_statusLabel->setText("");

                                       if (response.error()) {
                                           QMessageBox::critical(this,
                                                                 Tr::tr("Login failed"),
                                                                 Tr::tr(
                                                                     "The login request failed: ")
                                                                     + response.error()->message());
                                           setState("Sign in", false);
                                           return;
                                       }

                                       setState("Sign Out " + response.result()->user(), false);
                                   });
    });
}

void AuthWidget::signOut()
{
    QTC_ASSERT(m_client && m_client->reachable(), return);

    setState("Signing out ...", true);

    m_client->requestSignOut([this](const SignOutRequest::Response &response) {
        QTC_ASSERT(!response.error(), return);
        QTC_ASSERT(response.result()->status() == "NotSignedIn", return);

        checkStatus();
    });
}

} // namespace Copilot