File indexing completed on 2024-04-28 04:59:46

0001 // SPDX-FileCopyrightText: 2023 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 
0009 #include <QCoroTask>
0010 #include <Quotient/connection.h>
0011 
0012 class NeoChatConnection : public Quotient::Connection
0013 {
0014     Q_OBJECT
0015     QML_ELEMENT
0016     QML_UNCREATABLE("")
0017 
0018     /**
0019      * @brief The account label for this account.
0020      *
0021      * Account labels are a concept specific to NeoChat, allowing accounts to be
0022      * labelled, e.g. for "Work", "Private", etc.
0023      *
0024      * Set to an empty string to remove the label.
0025      */
0026     Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged)
0027     Q_PROPERTY(QString deviceKey READ deviceKey CONSTANT)
0028     Q_PROPERTY(QString encryptionKey READ encryptionKey CONSTANT)
0029 
0030     /**
0031      * @brief The total number of notifications for all direct chats.
0032      */
0033     Q_PROPERTY(qsizetype directChatNotifications READ directChatNotifications NOTIFY directChatNotificationsChanged)
0034 
0035     /**
0036      * @brief Whether there is at least one invite to a direct chat.
0037      */
0038     Q_PROPERTY(bool directChatInvites READ directChatInvites NOTIFY directChatInvitesChanged)
0039 
0040     /**
0041      * @brief Whether NeoChat is currently able to connect to the server.
0042      */
0043     Q_PROPERTY(bool isOnline READ isOnline WRITE setIsOnline NOTIFY isOnlineChanged)
0044 
0045 public:
0046     /**
0047      * @brief Defines the status after an attempt to change the password on an account.
0048      */
0049     enum PasswordStatus {
0050         Success, /**< The password was successfully changed. */
0051         Wrong, /**< The current password entered was wrong. */
0052         Other, /**< An unknown problem occurred. */
0053     };
0054     Q_ENUM(PasswordStatus)
0055 
0056     NeoChatConnection(QObject *parent = nullptr);
0057     NeoChatConnection(const QUrl &server, QObject *parent = nullptr);
0058 
0059     Q_INVOKABLE void logout(bool serverSideLogout);
0060     Q_INVOKABLE QVariantList getSupportedRoomVersions() const;
0061 
0062     /**
0063      * @brief Change the password for an account.
0064      *
0065      * The function emits a passwordStatus signal with a PasswordStatus value when
0066      * complete.
0067      *
0068      * @sa PasswordStatus, passwordStatus
0069      */
0070     Q_INVOKABLE void changePassword(const QString &currentPassword, const QString &newPassword);
0071 
0072     /**
0073      * @brief Change the avatar for an account.
0074      */
0075     Q_INVOKABLE bool setAvatar(const QUrl &avatarSource);
0076 
0077     [[nodiscard]] QString label() const;
0078     void setLabel(const QString &label);
0079 
0080     Q_INVOKABLE void deactivateAccount(const QString &password);
0081 
0082     /**
0083      * @brief Create new room for a group chat.
0084      */
0085     Q_INVOKABLE void createRoom(const QString &name, const QString &topic, const QString &parent = {}, bool setChildParent = false);
0086 
0087     /**
0088      * @brief Create new space.
0089      */
0090     Q_INVOKABLE void createSpace(const QString &name, const QString &topic, const QString &parent = {}, bool setChildParent = false);
0091 
0092     /**
0093      * @brief Whether a direct chat with the user exists.
0094      */
0095     Q_INVOKABLE bool directChatExists(Quotient::User *user);
0096 
0097     /**
0098      * @brief Join a direct chat with the given user ID.
0099      *
0100      * If a direct chat with the user doesn't exist one is created and then joined.
0101      */
0102     Q_INVOKABLE void openOrCreateDirectChat(const QString &userId);
0103 
0104     /**
0105      * @brief Join a direct chat with the given user object.
0106      *
0107      * If a direct chat with the user doesn't exist one is created and then joined.
0108      */
0109     Q_INVOKABLE void openOrCreateDirectChat(Quotient::User *user);
0110 
0111     qsizetype directChatNotifications() const;
0112     bool directChatInvites() const;
0113 
0114     // note: this is intentionally a copied QString because
0115     // the reference could be destroyed before the task is finished
0116     QCoro::Task<void> setupPushNotifications(QString endpoint);
0117 
0118     QString deviceKey() const;
0119     QString encryptionKey() const;
0120 
0121     bool isOnline() const;
0122 
0123 Q_SIGNALS:
0124     void labelChanged();
0125     void directChatNotificationsChanged();
0126     void directChatInvitesChanged();
0127     void isOnlineChanged();
0128     void passwordStatus(NeoChatConnection::PasswordStatus status);
0129     void userConsentRequired(QUrl url);
0130 
0131 private:
0132     bool m_isOnline = true;
0133     void setIsOnline(bool isOnline);
0134 
0135     void connectSignals();
0136 };