File: ghdialog.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (198 lines) | stat: -rw-r--r-- 7,158 bytes parent folder | download
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* This file is part of KDevelop
 *
 * Copyright (C) 2012-2013 Miquel Sabaté <mikisabate@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "ghdialog.h"

#include <ghaccount.h>
#include <ghresource.h>

#include <KLocalizedString>
#include <KMessageBox>
#include <KPasswordDialog>

#include <QLabel>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QPointer>
#include <QInputDialog>

static QString invalidAccountText()
{
    return i18n("You have not authorized KDevelop to use your GitHub account. "
                "If you authorize KDevelop, you will be able to fetch your "
                "public/private repositories and the repositories from your "
                "organizations.");
}

static QString tokenLinkStatementText()
{
    return i18nc("%1 is the URL with the GitHub token settings",
                 "You can check the authorization for this application and "
                 "others at %1",
                 QStringLiteral("https://github.com/settings/tokens."));
}

namespace gh
{

Dialog::Dialog(QWidget *parent, Account *account)
    : QDialog(parent)
    , m_account(account)
{
    auto mainWidget = new QWidget(this);
    auto mainLayout = new QVBoxLayout;
    setLayout(mainLayout);
    mainLayout->addWidget(mainWidget);

    auto buttonBox = new QDialogButtonBox();

    if (m_account->validAccount()) {
        m_text = new QLabel(i18n("You are logged in as <b>%1</b>.<br/>%2",
                                 m_account->name(), tokenLinkStatementText()), this);

        auto logOutButton = new QPushButton;
        logOutButton->setText(i18nc("@action:button", "Log Out"));
        logOutButton->setIcon(QIcon::fromTheme(QStringLiteral("dialog-cancel")));
        buttonBox->addButton(logOutButton, QDialogButtonBox::ActionRole);
        connect(logOutButton, &QPushButton::clicked, this, &Dialog::revokeAccess);

        auto forceSyncButton = new QPushButton;
        forceSyncButton->setText(i18nc("@action:button", "Force Sync"));
        forceSyncButton->setIcon(QIcon::fromTheme(QStringLiteral("view-refresh")));
        buttonBox->addButton(forceSyncButton, QDialogButtonBox::ActionRole);
        connect(forceSyncButton, &QPushButton::clicked, this, &Dialog::syncUser);

        connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
        connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
    } else {
        m_text = new QLabel(invalidAccountText(), this);

        buttonBox->addButton(QDialogButtonBox::Cancel);

        auto authorizeButton = new QPushButton;
        buttonBox->addButton(authorizeButton, QDialogButtonBox::ActionRole);
        authorizeButton->setText(i18nc("@action:button", "Authorize"));
        authorizeButton->setIcon(QIcon::fromTheme(QStringLiteral("dialog-ok")));
        connect(authorizeButton, &QPushButton::clicked, this, &Dialog::authorizeClicked);
    }

    m_text->setWordWrap(true);
    m_text->setOpenExternalLinks(true);
    setMinimumWidth(350);
    mainLayout->addWidget(m_text);

    mainLayout->addWidget(buttonBox);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);

    setWindowTitle(i18nc("@title:window", "GitHub Account"));
}

void Dialog::authorizeClicked()
{
    QPointer<KPasswordDialog> dlg = new KPasswordDialog(this, KPasswordDialog::ShowUsernameLine);
    dlg->setPrompt(i18n("Enter a login and a password"));
    if(dlg->exec()) {
        m_text->setAlignment(Qt::AlignCenter);
        m_text->setText(i18n("Waiting for response"));
        m_account->setName(dlg->username());

        Resource *rs = m_account->resource();
        rs->authenticate(dlg->username(), dlg->password());
        connect(rs, &Resource::twoFactorAuthRequested,
                this, &Dialog::twoFactorResponse);
        connect(rs, &Resource::authenticated,
                this, &Dialog::authorizeResponse);
    }
    delete dlg;
}

void Dialog::authorizeResponse(const QByteArray &id, const QByteArray &token, const QString &tokenName)
{
    Q_UNUSED(tokenName);

    Resource *rs = m_account->resource();
    disconnect(rs, &Resource::authenticated,
               this, &Dialog::authorizeResponse);

    if (id.isEmpty()) {
        m_text->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
        m_text->setText(invalidAccountText());
        m_account->setName(QString());
        KMessageBox::sorry(this, i18n("Authentication failed. Please try again.\n\n"
                                      "Could not create token: \"%1\"\n%2", tokenName,
                                      tokenLinkStatementText()),
                                 i18nc("@title:window", "GitHub Authorization Failed"));
        return;
    }
    else{
        KMessageBox::information(this, i18n("Authentication succeeded.\n\n"
                                            "Created token: \"%1\"\n%2", tokenName,
                                            tokenLinkStatementText()),
                                       i18nc("@title:window", "GitHub Account Authorized"));
    }
    m_account->saveToken(id, token);
    syncUser();
}

void Dialog::twoFactorResponse(const QString &transferHeader)
{
    auto code = QInputDialog::getText(this, i18nc("@title:window", "Authentication Code"), i18nc("@label:textbox", "OTP Code:"));
    Resource* rs = m_account->resource();
    disconnect(rs, &Resource::twoFactorAuthRequested,
               this, &Dialog::twoFactorResponse);
    rs->twoFactorAuthenticate(transferHeader, code);
}

void Dialog::syncUser()
{
    Resource *rs = m_account->resource();
    connect(rs, &Resource::orgsUpdated,
            this, &Dialog::updateOrgs);
    m_text->setAlignment(Qt::AlignCenter);
    m_text->setText(i18n("Waiting for response"));
    rs->getOrgs(m_account->token());
}

void Dialog::updateOrgs(const QStringList& orgs)
{
    Resource *rs = m_account->resource();
    disconnect(rs, &Resource::orgsUpdated,
              this, &Dialog::updateOrgs);

    if (!orgs.isEmpty())
        m_account->setOrgs(orgs);
    emit shouldUpdate();
    close();
}

void Dialog::revokeAccess()
{
    QPointer<KPasswordDialog> dlg = new KPasswordDialog(this);
    dlg->setPrompt(i18n("Please, write your password here."));
    if(dlg->exec()) {
        m_account->invalidate(dlg->password());
        emit shouldUpdate();
        close();
    }
    delete dlg;
}

} // End of namespace gh