Debian Codegraph @

src/LoginPage.h

raw symbols SCIP
1 // SPDX-FileCopyrightText: Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 #pragma once
6
7 #include <QObject>
8 #include <QQmlEngine>
9 #include <QVariantList>
10
11 #include <mtx/responses/login.hpp>
12
13 struct SSOProvider
14 {
15 Q_GADGET
16 Q_PROPERTY(QString avatarUrl READ avatarUrl CONSTANT)
17 Q_PROPERTY(QString name READ name CONSTANT)
18 Q_PROPERTY(QString id READ id CONSTANT)
19
20 public:
21 [[nodiscard]] QString avatarUrl() const { return avatarUrl_; }
22 [[nodiscard]] QString name() const { return name_.toHtmlEscaped(); }
23 [[nodiscard]] QString id() const { return id_; }
24
25 QString avatarUrl_;
26 QString name_;
27 QString id_;
28 };
29
30 class LoginPage : public QObject
31 {
32 Q_OBJECT
33 QML_NAMED_ELEMENT(Login)
34
35 Q_PROPERTY(QString mxid READ mxid WRITE setMxid NOTIFY matrixIdChanged)
36 Q_PROPERTY(QString homeserver READ homeserver WRITE setHomeserver NOTIFY homeserverChanged)
37
38 Q_PROPERTY(QString mxidError READ mxidError NOTIFY mxidErrorChanged)
39 Q_PROPERTY(QString error READ error NOTIFY errorOccurred)
40 Q_PROPERTY(bool lookingUpHs READ lookingUpHs NOTIFY lookingUpHsChanged)
41 Q_PROPERTY(bool homeserverValid READ homeserverValid NOTIFY lookingUpHsChanged)
42 Q_PROPERTY(bool loggingIn READ loggingIn NOTIFY loggingInChanged)
43 Q_PROPERTY(bool passwordSupported READ passwordSupported NOTIFY versionLookedUp)
44 Q_PROPERTY(bool ssoSupported READ ssoSupported NOTIFY versionLookedUp)
45 Q_PROPERTY(bool homeserverNeeded READ homeserverNeeded NOTIFY versionLookedUp)
46
47 Q_PROPERTY(QVariantList identityProviders READ identityProviders NOTIFY versionLookedUp)
48
49 public:
50 enum class LoginMethod
51 {
52 Password,
53 SSO,
54 };
55 Q_ENUM(LoginMethod)
56
57 LoginPage(QObject *parent = nullptr);
58
59 Q_INVOKABLE QString initialDeviceName() const
60 {
61 return QString::fromStdString(initialDeviceName_());
62 }
63
64 bool lookingUpHs() const { return lookingUpHs_; }
65 bool loggingIn() const { return loggingIn_; }
66 bool passwordSupported() const { return passwordSupported_; }
67 bool ssoSupported() const { return ssoSupported_; }
68 bool homeserverNeeded() const { return homeserverNeeded_; }
69 bool homeserverValid() const { return homeserverValid_; }
70 QVariantList identityProviders() const { return identityProviders_; }
71
72 QString homeserver() { return homeserver_; }
73 QString mxid() { return mxid_; }
74
75 QString error() { return error_; }
76 QString mxidError() { return mxidError_; }
77
78 void setHomeserver(const QString &hs);
79 void setMxid(QString id)
80 {
81 if (id != mxid_) {
82 mxid_ = id;
83 emit matrixIdChanged();
84 onMatrixIdEntered();
85 }
86 }
87
88 static std::string initialDeviceName_()
89 {
90 #if defined(Q_OS_MAC)
91 return "Nheko on macOS";
92 #elif defined(Q_OS_LINUX)
93 return "Nheko on Linux";
94 #elif defined(Q_OS_WIN)
95 return "Nheko on Windows";
96 #elif defined(Q_OS_FREEBSD)
97 return "Nheko on FreeBSD";
98 #elif defined(Q_OS_OPENBSD)
99 return "Nheko on OpenBSD";
100 #else
101 return "Nheko";
102 #endif
103 }
104
105 signals:
106 void loggingInChanged();
107 void errorOccurred();
108
109 //! Used to trigger the corresponding slot outside of the main thread.
110 void versionErrorCb(const QString &err);
111 void versionOkCb(bool passwordSupported, bool ssoSupported, QVariantList identityProviders);
112
113 void loginOk(const mtx::responses::Login &res);
114
115 void onServerAddressEntered();
116
117 void matrixIdChanged();
118 void homeserverChanged();
119
120 void mxidErrorChanged();
121 void lookingUpHsChanged();
122 void versionLookedUp();
123 void versionLookupFinished();
124
125 public slots:
126 // Displays errors produced during the login.
127 void showError(const QString &msg);
128
129 // Callback for the login button.
130 void onLoginButtonClicked(LoginMethod loginMethod,
131 const QString &userid,
132 const QString &password,
133 const QString &deviceName);
134
135 // Callback for errors produced during server probing
136 void versionError(const QString &error_message);
137 // Callback for successful server probing
138 void versionOk(bool passwordSupported, bool ssoSupported, QVariantList identityProviders);
139
140 private:
141 void checkHomeserverVersion();
142 void onMatrixIdEntered();
143 void clearErrors()
144 {
145 error_.clear();
146 mxidError_.clear();
147 emit errorOccurred();
148 emit mxidErrorChanged();
149 }
150
151 QString inferredServerAddress_;
152
153 QString mxid_;
154 QString homeserver_;
155
156 QString mxidError_;
157 QString error_;
158
159 QVariantList identityProviders_;
160
161 bool passwordSupported_ = true;
162 bool ssoSupported_ = false;
163
164 bool lookingUpHs_ = false;
165 bool loggingIn_ = false;
166 bool homeserverNeeded_ = false;
167 bool homeserverValid_ = false;
168 };