Skip to content

Commit

Permalink
Merge pull request #1501 from Nheko-Reborn/qmlRecaptcha
Browse files Browse the repository at this point in the history
QML the reCAPTCHA dialog
  • Loading branch information
deepbluev7 authored Jul 6, 2023
2 parents 1abb527 + 8c17c4f commit 8112922
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 134 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,6 @@ set(SRC_FILES
# Dialogs
src/dialogs/FallbackAuth.cpp
src/dialogs/FallbackAuth.h
src/dialogs/ReCaptcha.cpp
src/dialogs/ReCaptcha.h

# Emoji
src/emoji/Provider.cpp
Expand Down Expand Up @@ -487,6 +485,8 @@ set(SRC_FILES
src/PowerlevelsEditModels.h
src/ReadReceiptsModel.cpp
src/ReadReceiptsModel.h
src/ReCaptcha.cpp
src/ReCaptcha.h
src/RegisterPage.cpp
src/RegisterPage.h
src/RoomDirectoryModel.cpp
Expand Down Expand Up @@ -779,6 +779,7 @@ set(QML_SOURCES
resources/qml/dialogs/PowerLevelSpacesApplyDialog.qml
resources/qml/dialogs/RawMessageDialog.qml
resources/qml/dialogs/ReadReceipts.qml
resources/qml/dialogs/ReCaptchaDialog.qml
resources/qml/dialogs/RoomDirectory.qml
resources/qml/dialogs/RoomMembers.qml
resources/qml/dialogs/AllowedRoomsSettingsDialog.qml
Expand Down
22 changes: 13 additions & 9 deletions resources/qml/Root.qml
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,6 @@ Pane {
return UIA.submit3pidToken(t);
}
}
Platform.MessageDialog {
id: uiaErrorDialog

buttons: Platform.MessageDialog.Ok
}
Platform.MessageDialog {
id: uiaConfirmationLinkDialog

Expand All @@ -360,17 +355,14 @@ Pane {

onAccepted: UIA.continue3pidReceived()
}

Connections {
function onConfirm3pidToken() {
uiaConfirmationLinkDialog.open();
}
function onEmail() {
uiaEmailPrompt.show();
}
function onError(msg) {
uiaErrorDialog.text = msg;
uiaErrorDialog.open();
}
function onPassword() {
console.log("UIA: password needed");
uiaPassPrompt.show();
Expand All @@ -381,6 +373,18 @@ Pane {
function onPrompt3pidToken() {
uiaTokenPrompt.show();
}
function onReCaptcha(recaptcha) {
var component = Qt.createComponent("qrc:/resources/qml/dialogs/ReCaptchaDialog.qml");
if (component.status == Component.Ready) {
var dialog = component.createObject(timelineRoot, {
"recaptcha": recaptcha
});
dialog.show();
destroyOnClose(dialog);
} else {
console.error("Failed to create component: " + component.errorString());
}
}

target: UIA
}
Expand Down
63 changes: 63 additions & 0 deletions resources/qml/dialogs/ReCaptchaDialog.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-FileCopyrightText: Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

import QtQuick
import QtQuick.Controls
import im.nheko

ApplicationWindow {
id: recaptchaRoot

required property ReCaptcha recaptcha

function accept() {
recaptcha.confirm();
recaptchaRoot.close();
}

function reject() {
recaptcha.cancel();
recaptchaRoot.close();
}

color: palette.window
title: recaptcha.context
flags: Qt.Tool | Qt.WindowStaysOnTopHint | Qt.WindowCloseButtonHint | Qt.WindowTitleHint
height: msg.implicitHeight + footer.implicitHeight
width: Math.max(msg.implicitWidth, footer.implicitWidth)

Shortcut {
sequence: StandardKey.Cancel
onActivated: recaptchaRoot.reject()
}

Label {
id: msg

anchors.fill: parent
padding: 8
text: qsTr("Solve the reCAPTCHA and press the confirm button")
}

footer: DialogButtonBox {
onAccepted: recaptchaRoot.accept()
onRejected: recaptchaRoot.reject()

Button {
text: qsTr("Open reCAPTCHA")
onClicked: recaptcha.openReCaptcha()
}

Button {
text: qsTr("Cancel")
DialogButtonBox.buttonRole: DialogButtonBox.RejectRole
}

Button {
text: qsTr("Confirm")
DialogButtonBox.buttonRole: DialogButtonBox.AcceptRole
}
}

}
1 change: 0 additions & 1 deletion src/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ namespace dialogs {
class CreateRoom;
class InviteUsers;
class MemberList;
class ReCaptcha;
}

class NhekoFixupPaletteEventFilter final : public QObject
Expand Down
29 changes: 29 additions & 0 deletions src/ReCaptcha.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-FileCopyrightText: Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "ReCaptcha.h"

#include <QDesktopServices>
#include <QUrl>

#include "MatrixClient.h"

ReCaptcha::ReCaptcha(const QString &session, const QString &context, QObject *parent)
: QObject{parent}
, m_session{session}
, m_context{context}
{
}

void
ReCaptcha::openReCaptcha()
{
const auto url = QString("https://%1:%2/_matrix/client/r0/auth/m.login.recaptcha/"
"fallback/web?session=%3")
.arg(QString::fromStdString(http::client()->server()))
.arg(http::client()->port())
.arg(m_session);

QDesktopServices::openUrl(url);
}
32 changes: 32 additions & 0 deletions src/ReCaptcha.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-FileCopyrightText: Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

#pragma once

#include <QQmlEngine>

class ReCaptcha : public QObject
{
Q_OBJECT
QML_ELEMENT
QML_UNCREATABLE("")

Q_PROPERTY(QString context MEMBER m_context CONSTANT)
Q_PROPERTY(QString session MEMBER m_session CONSTANT)

public:
ReCaptcha(const QString &session, const QString &context, QObject *parent = nullptr);

Q_INVOKABLE void openReCaptcha();
Q_INVOKABLE void confirm() { emit confirmation(); }
Q_INVOKABLE void cancel() { emit cancelled(); }

signals:
void confirmation();
void cancelled();

private:
QString m_session;
QString m_context;
};
74 changes: 0 additions & 74 deletions src/dialogs/ReCaptcha.cpp

This file was deleted.

29 changes: 0 additions & 29 deletions src/dialogs/ReCaptcha.h

This file was deleted.

28 changes: 10 additions & 18 deletions src/ui/UIA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

#include "Logging.h"
#include "MatrixClient.h"
#include "ReCaptcha.h"
#include "dialogs/FallbackAuth.h"
#include "dialogs/ReCaptcha.h"

UIA *
UIA::instance()
{
static UIA uia;
static UIA uia{nullptr};
return &uia;
}

Expand Down Expand Up @@ -99,24 +99,16 @@ UIA::genericHandler(QString context)
} else if (current_stage == mtx::user_interactive::auth_types::msisdn) {
emit phoneNumber();
} else if (current_stage == mtx::user_interactive::auth_types::recaptcha) {
auto captchaDialog =
new dialogs::ReCaptcha(QString::fromStdString(u.session), nullptr);
captchaDialog->setWindowTitle(context);

connect(
captchaDialog, &dialogs::ReCaptcha::confirmation, this, [captchaDialog, h, u]() {
captchaDialog->close();
captchaDialog->deleteLater();
h.next(mtx::user_interactive::Auth{u.session,
mtx::user_interactive::auth::Fallback{}});
});

connect(captchaDialog, &dialogs::ReCaptcha::cancel, this, [this]() {
auto captcha = new ReCaptcha(QString::fromStdString(u.session), context, nullptr);
QQmlEngine::setObjectOwnership(captcha, QQmlEngine::JavaScriptOwnership);
connect(captcha, &ReCaptcha::confirmation, this, [h, u]() {
h.next(mtx::user_interactive::Auth{u.session,
mtx::user_interactive::auth::Fallback{}});
});
connect(captcha, &ReCaptcha::cancelled, this, [this]() {
emit error(tr("Registration aborted"));
});

QTimer::singleShot(0, this, [captchaDialog]() { captchaDialog->show(); });

emit reCaptcha(captcha);
} else if (current_stage == mtx::user_interactive::auth_types::dummy) {
h.next(
mtx::user_interactive::Auth{u.session, mtx::user_interactive::auth::Dummy{}});
Expand Down
5 changes: 4 additions & 1 deletion src/ui/UIA.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include <mtxclient/http/client.hpp>

#include "ReCaptcha.h"

class UIA final : public QObject
{
Q_OBJECT
Expand Down Expand Up @@ -39,7 +41,7 @@ class UIA final : public QObject
return instance();
}

UIA(QObject *parent = nullptr)
UIA(QObject *parent)
: QObject(parent)
{
}
Expand All @@ -59,6 +61,7 @@ public slots:
void password();
void email();
void phoneNumber();
void reCaptcha(ReCaptcha *recaptcha);

void confirm3pidToken();
void prompt3pidToken();
Expand Down

0 comments on commit 8112922

Please sign in to comment.