File indexing completed on 2024-11-03 07:45:11

0001 // SPDX-FileCopyrightText: 2020 Tobias Fella <tobias.fella@kde.org>
0002 // SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 #pragma once
0005 
0006 #include <QObject>
0007 #include <QQmlEngine>
0008 #include <QUrl>
0009 
0010 class NeoChatConnection;
0011 
0012 /**
0013  * @class LoginHelper
0014  *
0015  * A helper class for logging into a Matrix account.
0016  */
0017 class LoginHelper : public QObject
0018 {
0019     Q_OBJECT
0020     QML_ELEMENT
0021     QML_SINGLETON
0022 
0023     /**
0024      * @brief Whether the home server for the account is reachable.
0025      */
0026     Q_PROPERTY(bool homeserverReachable READ homeserverReachable NOTIFY homeserverReachableChanged)
0027 
0028     /**
0029      * @brief Whether the connection to the home server is being tested.
0030      *
0031      * True if NeoChat is trying to resolve the home server, false if not started
0032      * or complete.
0033      */
0034     Q_PROPERTY(bool testing READ testing NOTIFY testingChanged)
0035 
0036     /**
0037      * @brief The Matrix ID of the account that is being logged into.
0038      */
0039     Q_PROPERTY(QString matrixId READ matrixId WRITE setMatrixId NOTIFY matrixIdChanged)
0040 
0041     /**
0042      * @brief The password entered by the user to login to the account.
0043      */
0044     Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged)
0045 
0046     /**
0047      * @brief The device name to assign to this session.
0048      */
0049     Q_PROPERTY(QString deviceName READ deviceName WRITE setDeviceName NOTIFY deviceNameChanged)
0050 
0051     /**
0052      * @brief Whether the home server of the account supports single sign on login.
0053      */
0054     Q_PROPERTY(bool supportsSso READ supportsSso NOTIFY loginFlowsChanged STORED false)
0055 
0056     /**
0057      * @brief Whether the home server of the account supports password login.
0058      */
0059     Q_PROPERTY(bool supportsPassword READ supportsPassword NOTIFY loginFlowsChanged STORED false)
0060 
0061     /**
0062      * @brief The URL for the single sign on session.
0063      */
0064     Q_PROPERTY(QUrl ssoUrl READ ssoUrl NOTIFY ssoUrlChanged)
0065 
0066     /**
0067      * @brief Whether login process is ongoing.
0068      */
0069     Q_PROPERTY(bool isLoggingIn READ isLoggingIn NOTIFY isLoggingInChanged)
0070 
0071     /**
0072      * @brief Whether login has successfully completed.
0073      */
0074     Q_PROPERTY(bool isLoggedIn READ isLoggedIn NOTIFY isLoggedInChanged)
0075 
0076     /**
0077      * @brief Whether the password (or the username) is invalid.
0078      */
0079     Q_PROPERTY(bool isInvalidPassword READ isInvalidPassword NOTIFY isInvalidPasswordChanged)
0080 
0081 public:
0082     static LoginHelper &instance()
0083     {
0084         static LoginHelper _instance;
0085         return _instance;
0086     }
0087 
0088     static LoginHelper *create(QQmlEngine *engine, QJSEngine *)
0089     {
0090         engine->setObjectOwnership(&instance(), QQmlEngine::CppOwnership);
0091         return &instance();
0092     }
0093 
0094     Q_INVOKABLE void init();
0095 
0096     bool homeserverReachable() const;
0097 
0098     QString matrixId() const;
0099     void setMatrixId(const QString &matrixId);
0100 
0101     QString password() const;
0102     void setPassword(const QString &password);
0103 
0104     QString deviceName() const;
0105     void setDeviceName(const QString &deviceName);
0106 
0107     bool supportsPassword() const;
0108     bool supportsSso() const;
0109 
0110     bool testing() const;
0111 
0112     QUrl ssoUrl() const;
0113 
0114     bool isLoggingIn() const;
0115 
0116     bool isLoggedIn() const;
0117 
0118     bool isInvalidPassword() const;
0119     void setInvalidPassword(bool invalid);
0120 
0121     Q_INVOKABLE void login();
0122     Q_INVOKABLE void loginWithSso();
0123 
0124 Q_SIGNALS:
0125     void homeserverReachableChanged();
0126     void testHomeserverFinished();
0127     void matrixIdChanged();
0128     void passwordChanged();
0129     void deviceNameChanged();
0130     void loginFlowsChanged();
0131     void ssoUrlChanged();
0132     void connected();
0133     void errorOccured(const QString &message);
0134     void testingChanged();
0135     void isLoggingInChanged();
0136     void isLoggedInChanged();
0137     void isInvalidPasswordChanged();
0138     void loaded();
0139 
0140 private:
0141     void setHomeserverReachable(bool reachable);
0142 
0143     bool m_homeserverReachable;
0144     QString m_matrixId;
0145     QString m_password;
0146     QString m_deviceName;
0147     bool m_supportsSso = false;
0148     bool m_supportsPassword = false;
0149     NeoChatConnection *m_connection = nullptr;
0150     QUrl m_ssoUrl;
0151     bool m_testing = false;
0152     bool m_isLoggingIn = false;
0153     bool m_isLoggedIn = false;
0154     bool m_invalidPassword = false;
0155     explicit LoginHelper(QObject *parent = nullptr);
0156 };